blob: 002ce6d45d5693503f7a248e5dded82fca40e43c [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 (
Matteo Scandolo88e91892018-11-06 16:29:19 -080020 "flag"
21 "fmt"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090022 "os"
23 "os/signal"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090024 "strconv"
Matteo Scandolo88e91892018-11-06 16:29:19 -080025 "strings"
26 "sync"
27
28 "gerrit.opencord.org/voltha-bbsim/common/logger"
29 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090030 "gerrit.opencord.org/voltha-bbsim/device"
31 "reflect"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090032)
33
Matteo Scandolo88e91892018-11-06 16:29:19 -080034type option struct {
35 address string
36 port uint32
37 oltid uint32
38 npon uint32
39 nonus uint32
40 aaawait int
41 dhcpwait int
42 dhcpservip string
43 intvl int
44 intvl_test int
45 Mode Mode
46 KafkaBroker string
Mahir Gunyel09183342019-01-29 14:26:50 -080047 Debuglvl string
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090048}
49
50func GetOptions() *option {
51 o := new(option)
52 addressport := flag.String("H", ":50060", "IP address:port")
Matteo Scandolo88e91892018-11-06 16:29:19 -080053 oltid := flag.Int("id", 0, "OLT-ID")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090054 npon := flag.Int("i", 1, "Number of PON-IF ports")
55 nonus := flag.Int("n", 1, "Number of ONUs per PON-IF port")
56 modeopt := flag.String("m", "default", "Emulation mode (default, aaa, both (aaa & dhcp))")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090057 aaawait := flag.Int("aw", 10, "Wait time (sec) for activation WPA supplicants")
58 dhcpwait := flag.Int("dw", 20, "Wait time (sec) for activation DHCP clients")
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +090059 dhcpservip := flag.String("s", "182.21.0.128", "DHCP Server IP Address")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090060 intvl := flag.Int("v", 1, "Interval each Indication")
61 intvl_test := flag.Int("V", 1, "Interval each Indication")
Matteo Scandolo88e91892018-11-06 16:29:19 -080062 kafkaBroker := flag.String("k", "", "Kafka broker")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090063 o.Mode = DEFAULT
Mahir Gunyel09183342019-01-29 14:26:50 -080064 debg := flag.String("d", "DEBUG", "Debug Level(TRACE DEBUG INFO WARN ERROR)")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090065 flag.Parse()
66 if *modeopt == "aaa" {
67 o.Mode = AAA
68 } else if *modeopt == "both" {
69 o.Mode = BOTH
70 }
Mahir Gunyel09183342019-01-29 14:26:50 -080071 o.Debuglvl = *debg
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090072 o.oltid = uint32(*oltid)
73 o.npon = uint32(*npon)
74 o.nonus = uint32(*nonus)
75 o.aaawait = *aaawait
76 o.dhcpwait = *dhcpwait
77 o.dhcpservip = *dhcpservip
78 o.intvl = *intvl
79 o.intvl_test = *intvl_test
Matteo Scandolo88e91892018-11-06 16:29:19 -080080 o.KafkaBroker = *kafkaBroker
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090081 o.address = (strings.Split(*addressport, ":")[0])
82 tmp, _ := strconv.Atoi(strings.Split(*addressport, ":")[1])
83 o.port = uint32(tmp)
84 return o
85}
86
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090087type mediator struct {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090088 opt *option
89 server *Server
90 testmanager *TestManager
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090091}
92
Matteo Scandolo88e91892018-11-06 16:29:19 -080093func NewMediator(o *option) *mediator {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090094 m := new(mediator)
95 m.opt = o
Matteo Scandolo88e91892018-11-06 16:29:19 -080096 logger.WithFields(log.Fields{
97 "ip": o.address,
98 "baseport": o.port,
99 "pon_ports": o.npon,
100 "onus": o.nonus,
101 "mode": o.Mode,
102 }).Debug("New mediator")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900103 return m
104}
105
Matteo Scandolo88e91892018-11-06 16:29:19 -0800106func (m *mediator) Start() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900107 var wg sync.WaitGroup
108 opt := m.opt
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900109 server := NewCore(opt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 wg.Add(1)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800111 go func() {
112 if err := server.Start(); err != nil { //Blocking
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800113 logger.Error("Start %s", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900114 }
115 wg.Done()
116 return
117 }()
118
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900119 tm := NewTestManager(opt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900120 m.server = server
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900121 m.testmanager = tm
Matteo Scandolo88e91892018-11-06 16:29:19 -0800122 go func() {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900123 m.Mediate()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900124 }()
125
126 c := make(chan os.Signal, 1)
127 signal.Notify(c, os.Interrupt)
128 go func() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800129 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900130 logger.Debug("SIGINT catcher Done")
131 wg.Done()
132 }()
133 for sig := range c {
134 wg.Add(1)
135 fmt.Println("SIGINT", sig)
136 close(c)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900137 server.Stop() //Non-blocking
138 tm.Stop() //Non-blocking
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900139 return
140 }
141 }()
142 wg.Wait()
143 logger.Debug("Reach to the end line")
144}
145
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900146func (m *mediator) Mediate() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900147 defer logger.Debug("Mediate Done")
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900148 for sr := range m.server.stateRepCh {
149 next := sr.next
150 current := sr.current
151 dev := sr.device
152 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}){
153 logger.Debug("Received OLT Device %v Current: %d Next: %d", dev, current, next)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900154 if err := transitOlt(current, next, m.testmanager, m.opt); err != nil {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900155 logger.Error("%v", err)
156 }
157 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
158 logger.Debug("Received ONU Device %v Current: %d Next: %d", dev, current, next)
159 key := dev.GetDevkey()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900160 if err := transitOnu(key, current, next, m.testmanager, m.opt); err != nil {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900161 logger.Error("%v", err)
162 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900163 }
164 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800165}
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900166
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900167func transitOlt (current device.DeviceState, next device.DeviceState, tm *TestManager, o *option) error {
168 logger.Debug("trnsitOlt called current:%d , next:%d", current, next)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900169 if current == device.OLT_PREACTIVE && next == device.OLT_ACTIVE {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900170 tm.Start()
171 activateDHCPServer("nni_north0", o.dhcpservip)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900172 } else if current == device.OLT_ACTIVE && next == device.OLT_PREACTIVE{
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900173 tm.Stop()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900174 }
175 return nil
176}
177
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900178func transitOnu (key device.Devkey, current device.DeviceState, next device.DeviceState, tm *TestManager, o *option) error {
179 logger.Debug("trnsitOnu called with key:%s, current:%d , next:%d", key, current, next)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900180 if current == device.ONU_ACTIVE && next == device.ONU_OMCIACTIVE {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900181 t := tm.CreateTester(o, key)
182 if err := tm.StartTester(key, t); err != nil {
183 logger.Error("Cannot Start Executer error:%v", err)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900184 }
185 } else if (current == device.ONU_OMCIACTIVE || current == device.ONU_ACTIVE) &&
186 next == device.ONU_INACTIVE {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900187 if err := tm.StopTester(key); err != nil {
188 logger.Error("Cannot Start Executer error:%v", err)
189 }
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900190 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900191 return nil
Mahir Gunyel09183342019-01-29 14:26:50 -0800192}