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" |
| 21 | "gerrit.opencord.org/voltha-bbsim/common" |
| 22 | "golang.org/x/sync/errgroup" |
| 23 | "log" |
| 24 | "os/exec" |
| 25 | "time" |
| 26 | "sync" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | DEFAULT Mode = iota |
| 31 | AAA |
| 32 | BOTH |
| 33 | ) |
| 34 | |
| 35 | type Mode int |
| 36 | |
| 37 | type Tester struct { |
| 38 | Mode Mode |
| 39 | AAAWait int |
| 40 | DhcpWait int |
| 41 | DhcpServerIP string |
| 42 | Processes []string |
| 43 | Intvl int |
| 44 | cancel context.CancelFunc |
| 45 | } |
| 46 | |
| 47 | func NewTester(opt *option) *Tester{ |
| 48 | t := new(Tester) |
| 49 | t.AAAWait = opt.aaawait |
| 50 | t.DhcpWait = opt.dhcpwait |
| 51 | t.DhcpServerIP = opt.dhcpservip |
| 52 | t.Intvl = opt.intvl_test |
| 53 | t.Mode = opt.Mode |
| 54 | return t |
| 55 | } |
| 56 | |
| 57 | //Blocking |
| 58 | func (t *Tester) Start (s *Server) error { |
| 59 | ctx := context.Background() |
| 60 | ctx, cancel := context.WithCancel(ctx) |
| 61 | t.cancel = cancel |
| 62 | defer func(){ |
| 63 | cancel() |
| 64 | t.Initialize() |
| 65 | logger.Debug("Tester Done") |
| 66 | }() |
| 67 | logger.Info("Tester Run() start") |
| 68 | if t.Mode == DEFAULT { |
| 69 | //Empty |
| 70 | } else if t.Mode == AAA || t.Mode == BOTH { |
| 71 | eg, child := errgroup.WithContext(ctx) |
| 72 | child, cancel := context.WithCancel(child) |
| 73 | eg.Go(func() error { |
| 74 | defer func() { |
| 75 | logger.Debug("exeAAATest Done") |
| 76 | }() |
| 77 | err := t.exeAAATest(child, s, t.AAAWait) |
| 78 | return err |
| 79 | }) |
| 80 | |
| 81 | if t.Mode == BOTH { |
| 82 | eg.Go(func() error { |
| 83 | defer func() { |
| 84 | logger.Debug("exeDHCPTest Done") |
| 85 | }() |
| 86 | |
| 87 | err := t.exeDHCPTest(ctx, s, t.DhcpWait) |
| 88 | return err |
| 89 | }) |
| 90 | } |
| 91 | if err := eg.Wait(); err != nil { |
| 92 | logger.Error("Error happened in tester: %s", err) |
| 93 | cancel() |
| 94 | return err |
| 95 | } else { |
| 96 | logger.Debug("Test successfully finished") |
| 97 | } |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | func (t *Tester) Stop(s *Server) error { |
| 103 | if t.cancel != nil { |
| 104 | t.cancel() |
| 105 | } |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | func (t *Tester) Initialize (){ |
| 110 | logger.Info("Tester Initialize () called") |
| 111 | processes := t.Processes |
| 112 | logger.Debug("Runnig Process: %s", processes) |
| 113 | KillProcesses(processes) |
| 114 | exec.Command("rm", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation |
| 115 | exec.Command("touch", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation |
| 116 | } |
| 117 | |
| 118 | |
| 119 | func (t *Tester) exeAAATest(ctx context.Context, s *Server, wait int) error { |
| 120 | tick := time.NewTicker(time.Second) |
| 121 | defer tick.Stop() |
| 122 | logger.Info("exeAAATest stands by....") |
| 123 | infos, err := s.GetUniIoinfos("outside") |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | |
| 128 | univeths := []string{} |
| 129 | for _, info := range infos { |
| 130 | univeths = append(univeths, info.Name) |
| 131 | } |
| 132 | |
| 133 | for sec := 1; sec <= wait; sec ++ { |
| 134 | select { |
| 135 | case <-ctx.Done(): |
| 136 | logger.Debug("exeAAATest thread receives close ") |
| 137 | return nil |
| 138 | case <-tick.C: |
| 139 | logger.Info("exeAAATest stands by ... %dsec\n", wait - sec) |
| 140 | if sec == wait { |
| 141 | wg := sync.WaitGroup{} |
| 142 | wg.Add(1) |
| 143 | go func() error{ |
| 144 | defer wg.Done() |
| 145 | err = activateWPASups(ctx, univeths, t.Intvl) |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | logger.Info("WPA supplicants are successfully activated ") |
| 150 | t.Processes = append(t.Processes, "wpa_supplicant") |
| 151 | logger.Debug("Running Process:%s", t.Processes) |
| 152 | return nil |
| 153 | }() |
| 154 | wg.Wait() |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | func (t *Tester) exeDHCPTest(ctx context.Context, s *Server, wait int) error { |
| 162 | tick := time.NewTicker(time.Second) |
| 163 | defer tick.Stop() |
| 164 | logger.Info("exeDHCPTest stands by....") |
| 165 | info, err := s.IdentifyNniIoinfo("outside") |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
| 169 | |
| 170 | err = activateDHCPServer(info.Name, t.DhcpServerIP) |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | t.Processes = append(t.Processes, "dhcpd") |
| 175 | logger.Debug("Running Process:%s", t.Processes) |
| 176 | |
| 177 | infos, err := s.GetUniIoinfos("outside") |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | |
| 182 | univeths := []string{} |
| 183 | for _, info := range infos { |
| 184 | univeths = append(univeths, info.Name) |
| 185 | } |
| 186 | |
| 187 | for sec := 1; sec <= wait; sec ++ { |
| 188 | select { |
| 189 | case <- ctx.Done(): |
| 190 | logger.Debug("exeDHCPTest thread receives close ") |
| 191 | return nil |
| 192 | case <- tick.C: |
| 193 | logger.Info("exeDHCPTest stands by ... %dsec\n", wait- sec) |
| 194 | if sec == wait { |
| 195 | wg := sync.WaitGroup{} |
| 196 | wg.Add(1) |
| 197 | go func() error{ |
| 198 | defer wg.Done() |
| 199 | err = activateDHCPClients(ctx, univeths, t.Intvl) |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | logger.Info("DHCP clients are successfully activated ") |
| 204 | t.Processes = append(t.Processes, "dhclient") |
| 205 | logger.Debug("Running Process:%s", t.Processes) |
| 206 | return nil |
| 207 | }() |
| 208 | wg.Wait() |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | func KillProcesses(pnames []string) error { |
| 216 | for _, pname := range pnames { |
| 217 | killProcess(pname) |
| 218 | } |
| 219 | return nil |
| 220 | } |
| 221 | |
| 222 | func activateWPASups(ctx context.Context, vethnames []string, intvl int) error { |
| 223 | tick := time.NewTicker(time.Duration(intvl) * time.Second) |
| 224 | defer tick.Stop() |
| 225 | i := 0 |
| 226 | for { |
| 227 | select{ |
| 228 | case <- tick.C: |
| 229 | if i < len(vethnames) { |
| 230 | vethname := vethnames[i] |
| 231 | if err := activateWPASupplicant(vethname); err != nil { |
| 232 | return err |
| 233 | } |
| 234 | logger.Debug("activateWPASupplicant for interface %v\n", vethname) |
| 235 | i ++ |
| 236 | } |
| 237 | case <- ctx.Done(): |
| 238 | logger.Debug("activateWPASups was canceled by context.") |
| 239 | return nil |
| 240 | } |
| 241 | } |
| 242 | return nil |
| 243 | } |
| 244 | |
| 245 | func activateDHCPClients(ctx context.Context, vethnames []string, intvl int) error { |
| 246 | tick := time.NewTicker(time.Duration(intvl) * time.Second) |
| 247 | defer tick.Stop() |
| 248 | i := 0 |
| 249 | for { |
| 250 | select{ |
| 251 | case <- tick.C: |
| 252 | if i < len(vethnames){ |
| 253 | vethname := vethnames[i] |
| 254 | if err := activateDHCPClient(vethname); err != nil { |
| 255 | return err |
| 256 | } |
| 257 | logger.Debug("activateDHCPClient for interface %v\n", vethname) |
| 258 | i ++ |
| 259 | } |
| 260 | case <- ctx.Done(): |
| 261 | logger.Debug("activateDHCPClients was canceled by context.") |
| 262 | return nil |
| 263 | } |
| 264 | } |
| 265 | return nil |
| 266 | } |
| 267 | |
| 268 | func killProcess(name string) error { |
| 269 | err := exec.Command("pkill", name).Run() |
| 270 | if err != nil { |
| 271 | logger.Error("Fail to pkill %s: %v\n", name, err) |
| 272 | return err |
| 273 | } |
| 274 | logger.Info("Successfully killed %s\n", name) |
| 275 | return nil |
| 276 | } |
| 277 | |
| 278 | func activateWPASupplicant(vethname string) (err error) { |
| 279 | cmd := "/sbin/wpa_supplicant" |
| 280 | conf := "/etc/wpa_supplicant/wpa_supplicant.conf" |
| 281 | err = exec.Command(cmd, "-D", "wired", "-i", vethname, "-c", conf).Start() |
| 282 | if err != nil { |
| 283 | logger.Error("Fail to activateWPASupplicant() for :%s %v\n", vethname, err) |
| 284 | return |
| 285 | } |
| 286 | logger.Info("activateWPASupplicant() for :%s\n", vethname) |
| 287 | return |
| 288 | } |
| 289 | |
| 290 | func activateDHCPClient(vethname string) (err error) { |
| 291 | logger.Debug("activateDHCPClient() start for: %s\n", vethname) |
| 292 | cmd := exec.Command("/usr/local/bin/dhclient", vethname) |
| 293 | // if err := cmd.Run(); err != nil { |
| 294 | if err := cmd.Start(); err != nil { |
| 295 | logger.Error("Fail to activateDHCPClient() for: %s", vethname) |
| 296 | log.Panic(err) |
| 297 | } |
| 298 | logger.Debug("activateDHCPClient() done for: %s\n", vethname) |
| 299 | return |
| 300 | } |
| 301 | |
| 302 | func activateDHCPServer(veth string, serverip string) error { |
| 303 | err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run() |
| 304 | if err != nil { |
| 305 | logger.Error("Fail to add ip to %s address: %s\n", veth, err) |
| 306 | return err |
| 307 | } |
| 308 | err = exec.Command("ip", "link", "set", veth, "up").Run() |
| 309 | if err != nil { |
| 310 | logger.Error("Fail to set %s up: %s\n", veth, err) |
| 311 | return err |
| 312 | } |
| 313 | cmd := "/usr/local/bin/dhcpd" |
| 314 | conf := "/etc/dhcp/dhcpd.conf" |
| 315 | err = exec.Command(cmd, "-cf", conf, veth).Run() |
| 316 | if err != nil { |
| 317 | logger.Error("Fail to activateDHCP Server (): %s\n", err) |
| 318 | return err |
| 319 | } |
| 320 | logger.Info("DHCP Server is successfully activated !\n") |
| 321 | return err |
| 322 | } |