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" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 23 | "golang.org/x/sync/errgroup" |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 24 | "time" |
| 25 | "strconv" |
| 26 | "gerrit.opencord.org/voltha-bbsim/device" |
| 27 | "fmt" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | DEFAULT Mode = iota |
| 32 | AAA |
| 33 | BOTH |
| 34 | ) |
| 35 | |
| 36 | type Mode int |
| 37 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 38 | type TestManager struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 39 | DhcpServerIP string |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 40 | Pid []int |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 41 | Intvl int |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 42 | testers map[device.Devkey]*Tester |
| 43 | ctx context.Context |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 44 | cancel context.CancelFunc |
| 45 | } |
| 46 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 47 | type Tester struct { |
| 48 | Key device.Devkey |
| 49 | Mode Mode |
| 50 | ctx context.Context |
| 51 | cancel context.CancelFunc |
| 52 | } |
| 53 | |
| 54 | func NewTestManager(opt *option) *TestManager { |
| 55 | t := new(TestManager) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 56 | t.DhcpServerIP = opt.dhcpservip |
| 57 | t.Intvl = opt.intvl_test |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 58 | return t |
| 59 | } |
| 60 | |
| 61 | func (*TestManager) CreateTester(opt *option, key device.Devkey) *Tester{ |
| 62 | logger.Debug("CreateTester() called") |
| 63 | t := new(Tester) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 64 | t.Mode = opt.Mode |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 65 | t.Key = key |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 66 | return t |
| 67 | } |
| 68 | |
| 69 | //Blocking |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 70 | func (tm *TestManager) Start() error { |
| 71 | ctx, cancel := context.WithCancel(context.Background()) |
| 72 | tm.ctx = ctx |
| 73 | tm.cancel = cancel |
| 74 | tm.testers = map[device.Devkey]*Tester{} |
| 75 | logger.Info("TestManager start") |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | func (tm *TestManager) Stop() error { |
| 80 | if tm.cancel != nil { |
| 81 | tm.cancel() |
| 82 | } |
| 83 | tm.Initialize() |
| 84 | logger.Debug("TestManager Done") |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func (tm *TestManager) StartTester (key device.Devkey, t *Tester) error { |
| 89 | logger.Debug("StartTester called with key:%v", key) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 90 | if t.Mode == DEFAULT { |
| 91 | //Empty |
| 92 | } else if t.Mode == AAA || t.Mode == BOTH { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 93 | eg, child := errgroup.WithContext(tm.ctx) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 94 | child, cancel := context.WithCancel(child) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 95 | t.ctx = child |
| 96 | t.cancel = cancel |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 97 | eg.Go(func() error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 98 | err := activateWPASupplicant(key) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 103 | }) |
| 104 | |
| 105 | if t.Mode == BOTH { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 106 | waitForDHCP := 3 |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 107 | eg.Go(func() error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 108 | tick := time.NewTicker(time.Second) |
| 109 | counter := 0 |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 110 | defer func() { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 111 | tick.Stop() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 112 | logger.Debug("exeDHCPTest Done") |
| 113 | }() |
| 114 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 115 | L: |
| 116 | for counter < waitForDHCP { |
| 117 | select{ |
| 118 | case <-tick.C: |
| 119 | counter ++ |
| 120 | if counter == waitForDHCP { // TODO: This should be fixed |
| 121 | break L |
| 122 | } |
| 123 | case <-child.Done(): |
| 124 | return nil |
| 125 | } |
| 126 | } |
| 127 | err := activateDHCPClient(key) |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 132 | }) |
| 133 | } |
| 134 | if err := eg.Wait(); err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 135 | return err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 136 | } |
| 137 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 138 | tm.testers[key] = t |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 139 | return nil |
| 140 | } |
| 141 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 142 | func (tm *TestManager) StopTester (key device.Devkey) error { |
| 143 | ts := tm.testers[key] |
| 144 | ts.cancel() |
| 145 | delete(tm.testers, key) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 146 | return nil |
| 147 | } |
| 148 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 149 | func (tm *TestManager) Initialize() { |
| 150 | logger.Info("TestManager Initialize () called") |
| 151 | pids := tm.Pid |
| 152 | logger.Debug("Runnig Process: %v", pids) |
| 153 | KillProcesses(pids) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 154 | exec.Command("rm", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation |
| 155 | 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] | 156 | } |
| 157 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 158 | func KillProcesses(pids []int) error { |
| 159 | for _, pname := range pids { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 160 | killProcess(pname) |
| 161 | } |
| 162 | return nil |
| 163 | } |
| 164 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 165 | func killProcess(pid int) error { |
| 166 | err := exec.Command("kill", strconv.Itoa(pid)).Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 167 | if err != nil { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 168 | logger.Error("Fail to kill %d: %v", pid, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 169 | return err |
| 170 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 171 | logger.Info("Successfully killed %d", pid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 172 | return nil |
| 173 | } |
| 174 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 175 | func activateWPASupplicant(key device.Devkey) (err error) { |
| 176 | if err = startEAPClient(key.Intfid, key.ID); err != nil { |
| 177 | errmsg := fmt.Sprintf("Failed to activate WPA Supplicant intfid: %d onuid: %d", key.Intfid, key.ID) |
| 178 | logger.Error(errmsg) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 179 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 180 | logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID) |
| 181 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 182 | } |
| 183 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 184 | func activateDHCPClient(key device.Devkey) (err error) { |
| 185 | if err = startDHCPClient(key.Intfid, key.ID); err != nil { |
| 186 | errmsg := fmt.Sprintf("Failed to activate DHCP client intfid: %d onuid: %d", key.Intfid, key.ID) |
| 187 | logger.Error(errmsg) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 188 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 189 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 190 | } |
| 191 | |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 192 | func activateDHCPServer (veth string, serverip string) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 193 | err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run() |
| 194 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 195 | logger.Error("Fail to add ip to %s address: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 196 | return err |
| 197 | } |
| 198 | err = exec.Command("ip", "link", "set", veth, "up").Run() |
| 199 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 200 | logger.Error("Fail to set %s up: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 201 | return err |
| 202 | } |
| 203 | cmd := "/usr/local/bin/dhcpd" |
| 204 | conf := "/etc/dhcp/dhcpd.conf" |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 205 | logfile := "/tmp/dhcplog" |
| 206 | err = exec.Command(cmd, "-cf", conf, veth, "-tf", logfile).Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 207 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 208 | logger.Error("Fail to activateDHCP Server (): %s", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 209 | return err |
| 210 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 211 | logger.Info("DHCP Server is successfully activated !") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 212 | return err |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 213 | } |