blob: fe2aa39fd8ea1cc277862ffcb2d152b74a0dde41 [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 NISHIMOTOdd9f6732019-02-09 09:41:31 +090021 "fmt"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020022 "os/exec"
23 "strconv"
24 "time"
25
26 "gerrit.opencord.org/voltha-bbsim/common/logger"
27 "gerrit.opencord.org/voltha-bbsim/device"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090028)
29
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020030// TestManager is the structure for test manager
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090031type TestManager struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090032 DhcpServerIP string
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090033 Pid []int
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090034 testers map[string]map[device.Devkey]*Tester
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090035 ctx context.Context
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090036 cancel context.CancelFunc
37}
38
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020039// Tester is the structure for Test
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090040type Tester struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020041 Type string
42 Key device.Devkey
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090043 Testfunc func(device.Devkey) error
44 Waitsec int
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020045 ctx context.Context
46 cancel context.CancelFunc
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090047}
48
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020049// NewTestManager returns new TestManager
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090050func NewTestManager(opt *option) *TestManager {
51 t := new(TestManager)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090052 t.DhcpServerIP = opt.dhcpservip
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090053 return t
54}
55
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020056// CreateTester creates instance of Tester
57func (*TestManager) CreateTester(testtype string, opt *option, key device.Devkey, fn func(device.Devkey) error, waitsec int) *Tester {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090058 logger.Debug("CreateTester() called")
59 t := new(Tester)
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090060 t.Type = testtype
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090061 t.Key = key
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090062 t.Testfunc = fn
63 t.Waitsec = waitsec
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090064 return t
65}
66
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020067// Start does starting action - Blocking Call
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090068func (tm *TestManager) Start() error {
69 ctx, cancel := context.WithCancel(context.Background())
70 tm.ctx = ctx
71 tm.cancel = cancel
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090072 tm.testers = make(map[string]map[device.Devkey]*Tester)
73 tm.testers["AAA"] = map[device.Devkey]*Tester{}
74 tm.testers["DHCP"] = map[device.Devkey]*Tester{}
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090075 logger.Info("TestManager start")
76 return nil
77}
78
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020079// Stop does stopping action
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090080func (tm *TestManager) Stop() error {
81 if tm.cancel != nil {
82 tm.cancel()
83 }
84 tm.Initialize()
85 logger.Debug("TestManager Done")
86 return nil
87}
88
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020089func (tm *TestManager) StartTester(t *Tester) error {
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090090 testtype := t.Type
91 key := t.Key
92 waitsec := t.Waitsec
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090093
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090094 logger.Debug("StartTester type:%s called with key:%v", testtype, key)
95 child, cancel := context.WithCancel(tm.ctx)
96 t.ctx = child
97 t.cancel = cancel
98 go func() error {
99 tick := time.NewTicker(time.Second)
100 counter := 0
101 defer func() {
102 tick.Stop()
103 logger.Debug("Tester type:%s with key %v Done", testtype, key)
104 }()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900105
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900106 L:
107 for counter < waitsec {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200108 select {
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900109 case <-tick.C:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200110 counter++
111 if counter == waitsec { // TODO: This should be fixed
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900112 break L
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900113 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900114 case <-child.Done():
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900115 return nil
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900116 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900117 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900118 err := t.Testfunc(key)
119 if err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900120 return err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900121 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900122 return nil
123 }()
124
125 tm.testers[testtype][key] = t
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900126 return nil
127}
128
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200129// StopTester stops the test
130func (tm *TestManager) StopTester(testtype string, key device.Devkey) error {
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900131 ts := tm.testers[testtype][key]
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900132 ts.cancel()
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900133 delete(tm.testers[testtype], key)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900134 return nil
135}
136
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200137// Initialize test manager
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900138func (tm *TestManager) Initialize() {
139 logger.Info("TestManager Initialize () called")
140 pids := tm.Pid
141 logger.Debug("Runnig Process: %v", pids)
142 KillProcesses(pids)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200143 exec.Command("rm", "/var/run/dhcpd.pid").Run() // This is for DHCP server activation
144 exec.Command("touch", "/var/run/dhcpd.pid").Run() // This is for DHCP server activation
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900145}
146
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200147// KillProcesses kill process by specified pid
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900148func KillProcesses(pids []int) error {
149 for _, pname := range pids {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900150 killProcess(pname)
151 }
152 return nil
153}
154
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900155func killProcess(pid int) error {
156 err := exec.Command("kill", strconv.Itoa(pid)).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900157 if err != nil {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900158 logger.Error("Fail to kill %d: %v", pid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900159 return err
160 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900161 logger.Info("Successfully killed %d", pid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900162 return nil
163}
164
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900165func activateWPASupplicant(key device.Devkey) (err error) {
166 if err = startEAPClient(key.Intfid, key.ID); err != nil {
167 errmsg := fmt.Sprintf("Failed to activate WPA Supplicant intfid: %d onuid: %d", key.Intfid, key.ID)
168 logger.Error(errmsg)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900169 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900170 logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID)
171 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900172}
173
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900174func activateDHCPClient(key device.Devkey) (err error) {
175 if err = startDHCPClient(key.Intfid, key.ID); err != nil {
176 errmsg := fmt.Sprintf("Failed to activate DHCP client intfid: %d onuid: %d", key.Intfid, key.ID)
177 logger.Error(errmsg)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900178 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900179 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900180}
181
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200182func activateDHCPServer(veth string, serverip string) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900183 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
184 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800185 logger.Error("Fail to add ip to %s address: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900186 return err
187 }
188 err = exec.Command("ip", "link", "set", veth, "up").Run()
189 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800190 logger.Error("Fail to set %s up: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900191 return err
192 }
193 cmd := "/usr/local/bin/dhcpd"
194 conf := "/etc/dhcp/dhcpd.conf"
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900195 logfile := "/tmp/dhcplog"
196 err = exec.Command(cmd, "-cf", conf, veth, "-tf", logfile).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900197 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800198 logger.Error("Fail to activateDHCP Server (): %s", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900199 return err
200 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800201 logger.Info("DHCP Server is successfully activated !")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900202 return err
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200203}