blob: 279d2c834f3366476c14fc67cde94247d3dbe59d [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 NISHIMOTOdd9f6732019-02-09 09:41:31 +090041 testers map[device.Devkey]*Tester
42 ctx context.Context
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090043 cancel context.CancelFunc
44}
45
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090046type Tester struct {
47 Key device.Devkey
48 Mode Mode
49 ctx context.Context
50 cancel context.CancelFunc
51}
52
53func NewTestManager(opt *option) *TestManager {
54 t := new(TestManager)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090055 t.DhcpServerIP = opt.dhcpservip
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090056 return t
57}
58
59func (*TestManager) CreateTester(opt *option, key device.Devkey) *Tester{
60 logger.Debug("CreateTester() called")
61 t := new(Tester)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090062 t.Mode = opt.Mode
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090063 t.Key = key
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090064 return t
65}
66
67//Blocking
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090068func (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
77func (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
86func (tm *TestManager) StartTester (key device.Devkey, t *Tester) error {
87 logger.Debug("StartTester called with key:%v", key)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090088 if t.Mode == DEFAULT {
89 //Empty
90 } else if t.Mode == AAA || t.Mode == BOTH {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090091 eg, child := errgroup.WithContext(tm.ctx)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090092 child, cancel := context.WithCancel(child)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090093 t.ctx = child
94 t.cancel = cancel
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090095 eg.Go(func() error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090096 err := activateWPASupplicant(key)
97 if err != nil {
98 return err
99 }
100 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900101 })
102
103 if t.Mode == BOTH {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900104 waitForDHCP := 3
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900105 eg.Go(func() error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900106 tick := time.NewTicker(time.Second)
107 counter := 0
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900108 defer func() {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900109 tick.Stop()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 logger.Debug("exeDHCPTest Done")
111 }()
112
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900113 L:
114 for counter < waitForDHCP {
115 select{
116 case <-tick.C:
117 counter ++
118 if counter == waitForDHCP { // TODO: This should be fixed
119 break L
120 }
121 case <-child.Done():
122 return nil
123 }
124 }
125 err := activateDHCPClient(key)
126 if err != nil {
127 return err
128 }
129 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900130 })
131 }
132 if err := eg.Wait(); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900133 return err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900134 }
135 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900136 tm.testers[key] = t
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900137 return nil
138}
139
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900140func (tm *TestManager) StopTester (key device.Devkey) error {
141 ts := tm.testers[key]
142 ts.cancel()
143 delete(tm.testers, key)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900144 return nil
145}
146
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900147func (tm *TestManager) Initialize() {
148 logger.Info("TestManager Initialize () called")
149 pids := tm.Pid
150 logger.Debug("Runnig Process: %v", pids)
151 KillProcesses(pids)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800152 exec.Command("rm", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation
153 exec.Command("touch", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900154}
155
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900156func KillProcesses(pids []int) error {
157 for _, pname := range pids {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900158 killProcess(pname)
159 }
160 return nil
161}
162
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900163func killProcess(pid int) error {
164 err := exec.Command("kill", strconv.Itoa(pid)).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900165 if err != nil {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900166 logger.Error("Fail to kill %d: %v", pid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900167 return err
168 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900169 logger.Info("Successfully killed %d", pid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900170 return nil
171}
172
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900173func activateWPASupplicant(key device.Devkey) (err error) {
174 if err = startEAPClient(key.Intfid, key.ID); err != nil {
175 errmsg := fmt.Sprintf("Failed to activate WPA Supplicant intfid: %d onuid: %d", key.Intfid, key.ID)
176 logger.Error(errmsg)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900177 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900178 logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID)
179 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900180}
181
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900182func activateDHCPClient(key device.Devkey) (err error) {
183 if err = startDHCPClient(key.Intfid, key.ID); err != nil {
184 errmsg := fmt.Sprintf("Failed to activate DHCP client intfid: %d onuid: %d", key.Intfid, key.ID)
185 logger.Error(errmsg)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900186 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900187 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900188}
189
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900190func activateDHCPServer (veth string, serverip string) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900191 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
192 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800193 logger.Error("Fail to add ip to %s address: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900194 return err
195 }
196 err = exec.Command("ip", "link", "set", veth, "up").Run()
197 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800198 logger.Error("Fail to set %s up: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900199 return err
200 }
201 cmd := "/usr/local/bin/dhcpd"
202 conf := "/etc/dhcp/dhcpd.conf"
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900203 logfile := "/tmp/dhcplog"
204 err = exec.Command(cmd, "-cf", conf, veth, "-tf", logfile).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900205 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800206 logger.Error("Fail to activateDHCP Server (): %s", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900207 return err
208 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800209 logger.Info("DHCP Server is successfully activated !")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900210 return err
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900211}