blob: 623447db5904eead5dfc9efdb38307c88f33da8f [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 (
Matteo Scandolof7b260d2019-07-31 13:43:02 -070020 "bytes"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090021 "context"
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090022 "fmt"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020023 "os/exec"
24 "strconv"
25 "time"
26
Zack Williams2abf3932019-08-05 14:07:05 -070027 "github.com/opencord/voltha-bbsim/common/logger"
28 "github.com/opencord/voltha-bbsim/device"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090029)
30
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020031// TestManager is the structure for test manager
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090032type TestManager struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090033 DhcpServerIP string
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090034 Pid []int
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090035 testers map[string]map[device.Devkey]*Tester
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090036 ctx context.Context
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090037 cancel context.CancelFunc
38}
39
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020040// Tester is the structure for Test
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090041type Tester struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020042 Type string
43 Key device.Devkey
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090044 Testfunc func(device.Devkey) error
45 Waitsec int
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020046 ctx context.Context
47 cancel context.CancelFunc
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090048}
49
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020050// NewTestManager returns new TestManager
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090051func NewTestManager(opt *option) *TestManager {
52 t := new(TestManager)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090053 t.DhcpServerIP = opt.dhcpservip
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090054 return t
55}
56
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020057// CreateTester creates instance of Tester
58func (*TestManager) CreateTester(testtype string, opt *option, key device.Devkey, fn func(device.Devkey) error, waitsec int) *Tester {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090059 logger.Debug("CreateTester() called")
60 t := new(Tester)
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090061 t.Type = testtype
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090062 t.Key = key
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090063 t.Testfunc = fn
64 t.Waitsec = waitsec
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090065 return t
66}
67
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020068// Start does starting action - Blocking Call
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090069func (tm *TestManager) Start() error {
70 ctx, cancel := context.WithCancel(context.Background())
71 tm.ctx = ctx
72 tm.cancel = cancel
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090073 tm.testers = make(map[string]map[device.Devkey]*Tester)
74 tm.testers["AAA"] = map[device.Devkey]*Tester{}
75 tm.testers["DHCP"] = map[device.Devkey]*Tester{}
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090076 logger.Info("TestManager start")
77 return nil
78}
79
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020080// Stop does stopping action
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090081func (tm *TestManager) Stop() error {
82 if tm.cancel != nil {
83 tm.cancel()
84 }
85 tm.Initialize()
86 logger.Debug("TestManager Done")
87 return nil
88}
89
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020090func (tm *TestManager) StartTester(t *Tester) error {
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090091 testtype := t.Type
92 key := t.Key
93 waitsec := t.Waitsec
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090094
Matteo Scandolo67add0c2019-08-01 16:41:14 -070095 // TODO use timers instead of this crazy loop: https://gobyexample.com/timers
96
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090097 logger.Debug("StartTester type:%s called with key:%v", testtype, key)
98 child, cancel := context.WithCancel(tm.ctx)
99 t.ctx = child
100 t.cancel = cancel
101 go func() error {
102 tick := time.NewTicker(time.Second)
103 counter := 0
104 defer func() {
105 tick.Stop()
106 logger.Debug("Tester type:%s with key %v Done", testtype, key)
107 }()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900108
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900109 L:
110 for counter < waitsec {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200111 select {
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900112 case <-tick.C:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200113 counter++
114 if counter == waitsec { // TODO: This should be fixed
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900115 break L
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900116 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900117 case <-child.Done():
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900118 return nil
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900119 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900120 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900121 err := t.Testfunc(key)
122 if err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900123 return err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900124 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900125 return nil
126 }()
127
128 tm.testers[testtype][key] = t
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900129 return nil
130}
131
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200132// StopTester stops the test
133func (tm *TestManager) StopTester(testtype string, key device.Devkey) error {
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900134 ts := tm.testers[testtype][key]
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900135 ts.cancel()
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900136 delete(tm.testers[testtype], key)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900137 return nil
138}
139
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200140// Initialize test manager
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900141func (tm *TestManager) Initialize() {
142 logger.Info("TestManager Initialize () called")
143 pids := tm.Pid
144 logger.Debug("Runnig Process: %v", pids)
145 KillProcesses(pids)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200146 exec.Command("rm", "/var/run/dhcpd.pid").Run() // This is for DHCP server activation
147 exec.Command("touch", "/var/run/dhcpd.pid").Run() // This is for DHCP server activation
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900148}
149
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200150// KillProcesses kill process by specified pid
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900151func KillProcesses(pids []int) error {
152 for _, pname := range pids {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900153 killProcess(pname)
154 }
155 return nil
156}
157
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900158func killProcess(pid int) error {
159 err := exec.Command("kill", strconv.Itoa(pid)).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900160 if err != nil {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900161 logger.Error("Fail to kill %d: %v", pid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900162 return err
163 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900164 logger.Info("Successfully killed %d", pid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900165 return nil
166}
167
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900168func activateWPASupplicant(key device.Devkey) (err error) {
169 if err = startEAPClient(key.Intfid, key.ID); err != nil {
170 errmsg := fmt.Sprintf("Failed to activate WPA Supplicant intfid: %d onuid: %d", key.Intfid, key.ID)
171 logger.Error(errmsg)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900172 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900173 logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID)
174 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900175}
176
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900177func activateDHCPClient(key device.Devkey) (err error) {
178 if err = startDHCPClient(key.Intfid, key.ID); err != nil {
179 errmsg := fmt.Sprintf("Failed to activate DHCP client intfid: %d onuid: %d", key.Intfid, key.ID)
180 logger.Error(errmsg)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900181 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900182 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900183}
184
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200185func activateDHCPServer(veth string, serverip string) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900186 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
187 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800188 logger.Error("Fail to add ip to %s address: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900189 return err
190 }
191 err = exec.Command("ip", "link", "set", veth, "up").Run()
192 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800193 logger.Error("Fail to set %s up: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900194 return err
195 }
Matteo Scandolof7b260d2019-07-31 13:43:02 -0700196 dhcp := "/usr/local/bin/dhcpd"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900197 conf := "/etc/dhcp/dhcpd.conf"
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900198 logfile := "/tmp/dhcplog"
Matteo Scandolof7b260d2019-07-31 13:43:02 -0700199 var stderr bytes.Buffer
200 cmd := exec.Command(dhcp, "-cf", conf, veth, "-tf", logfile)
201 cmd.Stderr = &stderr
202 err = cmd.Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900203 if err != nil {
Matteo Scandolof7b260d2019-07-31 13:43:02 -0700204 logger.Error("Fail to activateDHCP Server (): %s, %s", err, stderr.String())
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900205 return err
206 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800207 logger.Info("DHCP Server is successfully activated !")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900208 return err
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200209}