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 | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 21 | "os/exec" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 22 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 23 | "time" |
| 24 | "strconv" |
| 25 | "gerrit.opencord.org/voltha-bbsim/device" |
| 26 | "fmt" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 29 | type TestManager struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 30 | DhcpServerIP string |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 31 | Pid []int |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 32 | testers map[string]map[device.Devkey]*Tester |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 33 | ctx context.Context |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 34 | cancel context.CancelFunc |
| 35 | } |
| 36 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 37 | type Tester struct { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 38 | Type string |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 39 | Key device.Devkey |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 40 | Testfunc func(device.Devkey) error |
| 41 | Waitsec int |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 42 | ctx context.Context |
| 43 | cancel context.CancelFunc |
| 44 | } |
| 45 | |
| 46 | func NewTestManager(opt *option) *TestManager { |
| 47 | t := new(TestManager) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 48 | t.DhcpServerIP = opt.dhcpservip |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 49 | return t |
| 50 | } |
| 51 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 52 | 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] | 53 | logger.Debug("CreateTester() called") |
| 54 | t := new(Tester) |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 55 | t.Type = testtype |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 56 | t.Key = key |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 57 | t.Testfunc = fn |
| 58 | t.Waitsec = waitsec |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 59 | return t |
| 60 | } |
| 61 | |
| 62 | //Blocking |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 63 | func (tm *TestManager) Start() error { |
| 64 | ctx, cancel := context.WithCancel(context.Background()) |
| 65 | tm.ctx = ctx |
| 66 | tm.cancel = cancel |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 67 | 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 NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 70 | logger.Info("TestManager start") |
| 71 | return nil |
| 72 | } |
| 73 | |
| 74 | func (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 NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 83 | func (tm *TestManager) StartTester (t *Tester) error { |
| 84 | testtype := t.Type |
| 85 | key := t.Key |
| 86 | waitsec := t.Waitsec |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 87 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 88 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 99 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 100 | 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 NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 107 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 108 | case <-child.Done(): |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 109 | return nil |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 110 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 111 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 112 | err := t.Testfunc(key) |
| 113 | if err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 114 | return err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 115 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 116 | return nil |
| 117 | }() |
| 118 | |
| 119 | tm.testers[testtype][key] = t |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 120 | return nil |
| 121 | } |
| 122 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 123 | func (tm *TestManager) StopTester (testtype string, key device.Devkey) error { |
| 124 | ts := tm.testers[testtype][key] |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 125 | ts.cancel() |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 126 | delete(tm.testers[testtype], key) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 127 | return nil |
| 128 | } |
| 129 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 130 | func (tm *TestManager) Initialize() { |
| 131 | logger.Info("TestManager Initialize () called") |
| 132 | pids := tm.Pid |
| 133 | logger.Debug("Runnig Process: %v", pids) |
| 134 | KillProcesses(pids) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 135 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 137 | } |
| 138 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 139 | func KillProcesses(pids []int) error { |
| 140 | for _, pname := range pids { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 141 | killProcess(pname) |
| 142 | } |
| 143 | return nil |
| 144 | } |
| 145 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 146 | func killProcess(pid int) error { |
| 147 | err := exec.Command("kill", strconv.Itoa(pid)).Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 148 | if err != nil { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 149 | logger.Error("Fail to kill %d: %v", pid, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 150 | return err |
| 151 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 152 | logger.Info("Successfully killed %d", pid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 153 | return nil |
| 154 | } |
| 155 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 156 | func 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 NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 160 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 161 | logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID) |
| 162 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 163 | } |
| 164 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 165 | func 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 NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 169 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 170 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 173 | func activateDHCPServer (veth string, serverip string) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 174 | err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run() |
| 175 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 176 | logger.Error("Fail to add ip to %s address: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 177 | return err |
| 178 | } |
| 179 | err = exec.Command("ip", "link", "set", veth, "up").Run() |
| 180 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 181 | logger.Error("Fail to set %s up: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 182 | return err |
| 183 | } |
| 184 | cmd := "/usr/local/bin/dhcpd" |
| 185 | conf := "/etc/dhcp/dhcpd.conf" |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 186 | logfile := "/tmp/dhcplog" |
| 187 | err = exec.Command(cmd, "-cf", conf, veth, "-tf", logfile).Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 188 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 189 | logger.Error("Fail to activateDHCP Server (): %s", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 190 | return err |
| 191 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 192 | logger.Info("DHCP Server is successfully activated !") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 193 | return err |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 194 | } |