blob: 2349faee93e4b3762d4a3757b8ae0e6325b69d26 [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 {
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020089 _, child := errgroup.WithContext(tm.ctx)
90 child, cancel := context.WithCancel(child)
91 t.ctx = child
92 t.cancel = cancel
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090093 } else if t.Mode == AAA || t.Mode == BOTH {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090094 eg, child := errgroup.WithContext(tm.ctx)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090095 child, cancel := context.WithCancel(child)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090096 t.ctx = child
97 t.cancel = cancel
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090098 eg.Go(func() error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090099 err := activateWPASupplicant(key)
100 if err != nil {
101 return err
102 }
103 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900104 })
105
106 if t.Mode == BOTH {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900107 waitForDHCP := 3
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900108 eg.Go(func() error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900109 tick := time.NewTicker(time.Second)
110 counter := 0
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900111 defer func() {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900112 tick.Stop()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900113 logger.Debug("exeDHCPTest Done")
114 }()
115
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900116 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 NISHIMOTO9708e042018-10-27 09:24:44 +0900133 })
134 }
135 if err := eg.Wait(); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900136 return err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900137 }
138 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900139 tm.testers[key] = t
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900140 return nil
141}
142
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900143func (tm *TestManager) StopTester (key device.Devkey) error {
144 ts := tm.testers[key]
145 ts.cancel()
146 delete(tm.testers, key)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900147 return nil
148}
149
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900150func (tm *TestManager) Initialize() {
151 logger.Info("TestManager Initialize () called")
152 pids := tm.Pid
153 logger.Debug("Runnig Process: %v", pids)
154 KillProcesses(pids)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800155 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 NISHIMOTO9708e042018-10-27 09:24:44 +0900157}
158
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900159func KillProcesses(pids []int) error {
160 for _, pname := range pids {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900161 killProcess(pname)
162 }
163 return nil
164}
165
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900166func killProcess(pid int) error {
167 err := exec.Command("kill", strconv.Itoa(pid)).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900168 if err != nil {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900169 logger.Error("Fail to kill %d: %v", pid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900170 return err
171 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900172 logger.Info("Successfully killed %d", pid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900173 return nil
174}
175
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900176func 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 NISHIMOTO621a43d2019-01-30 20:53:03 +0900180 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900181 logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID)
182 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900183}
184
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900185func 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 NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900189 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900190 return nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900191}
192
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900193func activateDHCPServer (veth string, serverip string) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900194 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
195 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800196 logger.Error("Fail to add ip to %s address: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900197 return err
198 }
199 err = exec.Command("ip", "link", "set", veth, "up").Run()
200 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800201 logger.Error("Fail to set %s up: %s", veth, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900202 return err
203 }
204 cmd := "/usr/local/bin/dhcpd"
205 conf := "/etc/dhcp/dhcpd.conf"
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900206 logfile := "/tmp/dhcplog"
207 err = exec.Command(cmd, "-cf", conf, veth, "-tf", logfile).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900208 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800209 logger.Error("Fail to activateDHCP Server (): %s", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900210 return err
211 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800212 logger.Info("DHCP Server is successfully activated !")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900213 return err
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900214}