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