blob: 4d1118ec08be546ced977a811130cba862500281 [file] [log] [blame]
Keita NISHIMOTO9708e042018-10-27 09:24:44 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package core
18
19import (
20 "context"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090021 "os/exec"
Matteo Scandolo88e91892018-11-06 16:29:19 -080022 "gerrit.opencord.org/voltha-bbsim/common/logger"
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090023 "time"
24 "strconv"
25 "gerrit.opencord.org/voltha-bbsim/device"
26 "fmt"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090027)
28
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090029type TestManager struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090030 DhcpServerIP string
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090031 Pid []int
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090032 testers map[string]map[device.Devkey]*Tester
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090033 ctx context.Context
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090034 cancel context.CancelFunc
35}
36
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090037type Tester struct {
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090038 Type string
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090039 Key device.Devkey
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090040 Testfunc func(device.Devkey) error
41 Waitsec int
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090042 ctx context.Context
43 cancel context.CancelFunc
44}
45
46func NewTestManager(opt *option) *TestManager {
47 t := new(TestManager)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090048 t.DhcpServerIP = opt.dhcpservip
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090049 return t
50}
51
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090052func (*TestManager) CreateTester(testtype string, opt *option, key device.Devkey, fn func(device.Devkey) error, waitsec int) *Tester{
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090053 logger.Debug("CreateTester() called")
54 t := new(Tester)
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090055 t.Type = testtype
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090056 t.Key = key
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090057 t.Testfunc = fn
58 t.Waitsec = waitsec
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090059 return t
60}
61
62//Blocking
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090063func (tm *TestManager) Start() error {
64 ctx, cancel := context.WithCancel(context.Background())
65 tm.ctx = ctx
66 tm.cancel = cancel
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090067 tm.testers = make(map[string]map[device.Devkey]*Tester)
68 tm.testers["AAA"] = map[device.Devkey]*Tester{}
69 tm.testers["DHCP"] = map[device.Devkey]*Tester{}
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090070 logger.Info("TestManager start")
71 return nil
72}
73
74func (tm *TestManager) Stop() error {
75 if tm.cancel != nil {
76 tm.cancel()
77 }
78 tm.Initialize()
79 logger.Debug("TestManager Done")
80 return nil
81}
82
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090083func (tm *TestManager) StartTester (t *Tester) error {
84 testtype := t.Type
85 key := t.Key
86 waitsec := t.Waitsec
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090087
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090088 logger.Debug("StartTester type:%s called with key:%v", testtype, key)
89 child, cancel := context.WithCancel(tm.ctx)
90 t.ctx = child
91 t.cancel = cancel
92 go func() error {
93 tick := time.NewTicker(time.Second)
94 counter := 0
95 defer func() {
96 tick.Stop()
97 logger.Debug("Tester type:%s with key %v Done", testtype, key)
98 }()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090099
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900100 L:
101 for counter < waitsec {
102 select{
103 case <-tick.C:
104 counter ++
105 if counter == waitsec { // TODO: This should be fixed
106 break L
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900107 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900108 case <-child.Done():
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900109 return nil
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900110 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900111 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900112 err := t.Testfunc(key)
113 if err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900114 return err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900115 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900116 return nil
117 }()
118
119 tm.testers[testtype][key] = t
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900120 return nil
121}
122
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900123func (tm *TestManager) StopTester (testtype string, key device.Devkey) error {
124 ts := tm.testers[testtype][key]
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900125 ts.cancel()
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900126 delete(tm.testers[testtype], key)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900127 return nil
128}
129
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900130func (tm *TestManager) Initialize() {
131 logger.Info("TestManager Initialize () called")
132 pids := tm.Pid
133 logger.Debug("Runnig Process: %v", pids)
134 KillProcesses(pids)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800135 exec.Command("rm", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation
136 exec.Command("touch", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900137}
138
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900139func KillProcesses(pids []int) error {
140 for _, pname := range pids {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900141 killProcess(pname)
142 }
143 return nil
144}
145
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900146func killProcess(pid int) error {
147 err := exec.Command("kill", strconv.Itoa(pid)).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900148 if err != nil {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900149 logger.Error("Fail to kill %d: %v", pid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900150 return err
151 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900152 logger.Info("Successfully killed %d", pid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900153 return nil
154}
155
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900156func activateWPASupplicant(key device.Devkey) (err error) {
157 if err = startEAPClient(key.Intfid, key.ID); err != nil {
158 errmsg := fmt.Sprintf("Failed to activate WPA Supplicant intfid: %d onuid: %d", key.Intfid, key.ID)
159 logger.Error(errmsg)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900160 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900161 logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID)
162 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900163}
164
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900165func activateDHCPClient(key device.Devkey) (err error) {
166 if err = startDHCPClient(key.Intfid, key.ID); err != nil {
167 errmsg := fmt.Sprintf("Failed to activate DHCP client intfid: %d onuid: %d", key.Intfid, key.ID)
168 logger.Error(errmsg)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900169 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900170 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900171}
172
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900173func activateDHCPServer (veth string, serverip string) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900174 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
175 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800176 logger.Error("Fail to add ip to %s address: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900177 return err
178 }
179 err = exec.Command("ip", "link", "set", veth, "up").Run()
180 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800181 logger.Error("Fail to set %s up: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900182 return err
183 }
184 cmd := "/usr/local/bin/dhcpd"
185 conf := "/etc/dhcp/dhcpd.conf"
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900186 logfile := "/tmp/dhcplog"
187 err = exec.Command(cmd, "-cf", conf, veth, "-tf", logfile).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900188 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800189 logger.Error("Fail to activateDHCP Server (): %s", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900190 return err
191 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800192 logger.Info("DHCP Server is successfully activated !")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900193 return err
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900194}