blob: a2d9a7d3acbdc2eecb1f29d3a36b2b7949fabde4 [file] [log] [blame]
Keita NISHIMOTO9708e042018-10-27 09:24:44 +09001/*
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
17package core
18
19import (
20 "context"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090021 "os/exec"
Matteo Scandolo88e91892018-11-06 16:29:19 -080022 "gerrit.opencord.org/voltha-bbsim/common/logger"
Matteo Scandolo88e91892018-11-06 16:29:19 -080023 "golang.org/x/sync/errgroup"
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090024 "time"
25 "strconv"
26 "gerrit.opencord.org/voltha-bbsim/device"
27 "fmt"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090028)
29
30const (
31 DEFAULT Mode = iota
32 AAA
33 BOTH
34)
35
36type Mode int
37
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090038type TestManager struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090039 DhcpServerIP string
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090040 Pid []int
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090041 Intvl int
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090042 testers map[device.Devkey]*Tester
43 ctx context.Context
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090044 cancel context.CancelFunc
45}
46
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090047type Tester struct {
48 Key device.Devkey
49 Mode Mode
50 ctx context.Context
51 cancel context.CancelFunc
52}
53
54func NewTestManager(opt *option) *TestManager {
55 t := new(TestManager)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090056 t.DhcpServerIP = opt.dhcpservip
57 t.Intvl = opt.intvl_test
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090058 return t
59}
60
61func (*TestManager) CreateTester(opt *option, key device.Devkey) *Tester{
62 logger.Debug("CreateTester() called")
63 t := new(Tester)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090064 t.Mode = opt.Mode
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090065 t.Key = key
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090066 return t
67}
68
69//Blocking
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090070func (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
79func (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
88func (tm *TestManager) StartTester (key device.Devkey, t *Tester) error {
89 logger.Debug("StartTester called with key:%v", key)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090090 if t.Mode == DEFAULT {
91 //Empty
92 } else if t.Mode == AAA || t.Mode == BOTH {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090093 eg, child := errgroup.WithContext(tm.ctx)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090094 child, cancel := context.WithCancel(child)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090095 t.ctx = child
96 t.cancel = cancel
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090097 eg.Go(func() error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090098 err := activateWPASupplicant(key)
99 if err != nil {
100 return err
101 }
102 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900103 })
104
105 if t.Mode == BOTH {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900106 waitForDHCP := 3
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900107 eg.Go(func() error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900108 tick := time.NewTicker(time.Second)
109 counter := 0
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 defer func() {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900111 tick.Stop()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900112 logger.Debug("exeDHCPTest Done")
113 }()
114
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900115 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 NISHIMOTO9708e042018-10-27 09:24:44 +0900132 })
133 }
134 if err := eg.Wait(); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900135 return err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900136 }
137 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900138 tm.testers[key] = t
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900139 return nil
140}
141
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900142func (tm *TestManager) StopTester (key device.Devkey) error {
143 ts := tm.testers[key]
144 ts.cancel()
145 delete(tm.testers, key)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900146 return nil
147}
148
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900149func (tm *TestManager) Initialize() {
150 logger.Info("TestManager Initialize () called")
151 pids := tm.Pid
152 logger.Debug("Runnig Process: %v", pids)
153 KillProcesses(pids)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800154 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 NISHIMOTO9708e042018-10-27 09:24:44 +0900156}
157
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900158func KillProcesses(pids []int) error {
159 for _, pname := range pids {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900160 killProcess(pname)
161 }
162 return nil
163}
164
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900165func killProcess(pid int) error {
166 err := exec.Command("kill", strconv.Itoa(pid)).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900167 if err != nil {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900168 logger.Error("Fail to kill %d: %v", pid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900169 return err
170 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900171 logger.Info("Successfully killed %d", pid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900172 return nil
173}
174
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900175func 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 NISHIMOTO621a43d2019-01-30 20:53:03 +0900179 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900180 logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID)
181 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900182}
183
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900184func 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 NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900188 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900189 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900190}
191
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900192func activateDHCPServer (veth string, serverip string) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900193 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
194 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800195 logger.Error("Fail to add ip to %s address: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900196 return err
197 }
198 err = exec.Command("ip", "link", "set", veth, "up").Run()
199 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800200 logger.Error("Fail to set %s up: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900201 return err
202 }
203 cmd := "/usr/local/bin/dhcpd"
204 conf := "/etc/dhcp/dhcpd.conf"
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900205 logfile := "/tmp/dhcplog"
206 err = exec.Command(cmd, "-cf", conf, veth, "-tf", logfile).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900207 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800208 logger.Error("Fail to activateDHCP Server (): %s", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900209 return err
210 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800211 logger.Info("DHCP Server is successfully activated !")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900212 return err
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900213}