Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 21 | "fmt" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 22 | "os/exec" |
| 23 | "strconv" |
| 24 | "time" |
| 25 | |
| 26 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
| 27 | "gerrit.opencord.org/voltha-bbsim/device" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 28 | ) |
| 29 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 30 | // TestManager is the structure for test manager |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 31 | type TestManager struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 32 | DhcpServerIP string |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 33 | Pid []int |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 34 | testers map[string]map[device.Devkey]*Tester |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 35 | ctx context.Context |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 36 | cancel context.CancelFunc |
| 37 | } |
| 38 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 39 | // Tester is the structure for Test |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 40 | type Tester struct { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 41 | Type string |
| 42 | Key device.Devkey |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 43 | Testfunc func(device.Devkey) error |
| 44 | Waitsec int |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 45 | ctx context.Context |
| 46 | cancel context.CancelFunc |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 47 | } |
| 48 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 49 | // NewTestManager returns new TestManager |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 50 | func NewTestManager(opt *option) *TestManager { |
| 51 | t := new(TestManager) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 52 | t.DhcpServerIP = opt.dhcpservip |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 53 | return t |
| 54 | } |
| 55 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 56 | // CreateTester creates instance of Tester |
| 57 | func (*TestManager) CreateTester(testtype string, opt *option, key device.Devkey, fn func(device.Devkey) error, waitsec int) *Tester { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 58 | logger.Debug("CreateTester() called") |
| 59 | t := new(Tester) |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 60 | t.Type = testtype |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 61 | t.Key = key |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 62 | t.Testfunc = fn |
| 63 | t.Waitsec = waitsec |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 64 | return t |
| 65 | } |
| 66 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 67 | // Start does starting action - Blocking Call |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 68 | func (tm *TestManager) Start() error { |
| 69 | ctx, cancel := context.WithCancel(context.Background()) |
| 70 | tm.ctx = ctx |
| 71 | tm.cancel = cancel |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 72 | 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 NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 75 | logger.Info("TestManager start") |
| 76 | return nil |
| 77 | } |
| 78 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 79 | // Stop does stopping action |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 80 | func (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 Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 89 | func (tm *TestManager) StartTester(t *Tester) error { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 90 | testtype := t.Type |
| 91 | key := t.Key |
| 92 | waitsec := t.Waitsec |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 93 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 94 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 105 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 106 | L: |
| 107 | for counter < waitsec { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 108 | select { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 109 | case <-tick.C: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 110 | counter++ |
| 111 | if counter == waitsec { // TODO: This should be fixed |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 112 | break L |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 113 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 114 | case <-child.Done(): |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 115 | return nil |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 116 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 117 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 118 | err := t.Testfunc(key) |
| 119 | if err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 120 | return err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 121 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 122 | return nil |
| 123 | }() |
| 124 | |
| 125 | tm.testers[testtype][key] = t |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 126 | return nil |
| 127 | } |
| 128 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 129 | // StopTester stops the test |
| 130 | func (tm *TestManager) StopTester(testtype string, key device.Devkey) error { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 131 | ts := tm.testers[testtype][key] |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 132 | ts.cancel() |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 133 | delete(tm.testers[testtype], key) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 134 | return nil |
| 135 | } |
| 136 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 137 | // Initialize test manager |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 138 | func (tm *TestManager) Initialize() { |
| 139 | logger.Info("TestManager Initialize () called") |
| 140 | pids := tm.Pid |
| 141 | logger.Debug("Runnig Process: %v", pids) |
| 142 | KillProcesses(pids) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 143 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 147 | // KillProcesses kill process by specified pid |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 148 | func KillProcesses(pids []int) error { |
| 149 | for _, pname := range pids { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 150 | killProcess(pname) |
| 151 | } |
| 152 | return nil |
| 153 | } |
| 154 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 155 | func killProcess(pid int) error { |
| 156 | err := exec.Command("kill", strconv.Itoa(pid)).Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 157 | if err != nil { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 158 | logger.Error("Fail to kill %d: %v", pid, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 159 | return err |
| 160 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 161 | logger.Info("Successfully killed %d", pid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 162 | return nil |
| 163 | } |
| 164 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 165 | func 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 NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 169 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 170 | logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID) |
| 171 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 174 | func 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 NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 178 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 179 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 180 | } |
| 181 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 182 | func activateDHCPServer(veth string, serverip string) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 183 | err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run() |
| 184 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 185 | logger.Error("Fail to add ip to %s address: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 186 | return err |
| 187 | } |
| 188 | err = exec.Command("ip", "link", "set", veth, "up").Run() |
| 189 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 190 | logger.Error("Fail to set %s up: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 191 | return err |
| 192 | } |
| 193 | cmd := "/usr/local/bin/dhcpd" |
| 194 | conf := "/etc/dhcp/dhcpd.conf" |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 195 | logfile := "/tmp/dhcplog" |
| 196 | err = exec.Command(cmd, "-cf", conf, veth, "-tf", logfile).Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 197 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 198 | logger.Error("Fail to activateDHCP Server (): %s", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 199 | return err |
| 200 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 201 | logger.Info("DHCP Server is successfully activated !") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 202 | return err |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 203 | } |