blob: b973493759f4efbda5fe6dd681c580b7da0e400f [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
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090047}
48
49func GetOptions() *option {
50 o := new(option)
51 addressport := flag.String("H", ":50060", "IP address:port")
Matteo Scandolo88e91892018-11-06 16:29:19 -080052 oltid := flag.Int("id", 0, "OLT-ID")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090053 npon := flag.Int("i", 1, "Number of PON-IF ports")
54 nonus := flag.Int("n", 1, "Number of ONUs per PON-IF port")
55 modeopt := flag.String("m", "default", "Emulation mode (default, aaa, both (aaa & dhcp))")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090056 aaawait := flag.Int("aw", 10, "Wait time (sec) for activation WPA supplicants")
57 dhcpwait := flag.Int("dw", 20, "Wait time (sec) for activation DHCP clients")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090058 dhcpservip := flag.String("s", "182.21.0.1", "DHCP Server IP Address")
59 intvl := flag.Int("v", 1, "Interval each Indication")
60 intvl_test := flag.Int("V", 1, "Interval each Indication")
Matteo Scandolo88e91892018-11-06 16:29:19 -080061 kafkaBroker := flag.String("k", "", "Kafka broker")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090062 o.Mode = DEFAULT
63 flag.Parse()
64 if *modeopt == "aaa" {
65 o.Mode = AAA
66 } else if *modeopt == "both" {
67 o.Mode = BOTH
68 }
69 o.oltid = uint32(*oltid)
70 o.npon = uint32(*npon)
71 o.nonus = uint32(*nonus)
72 o.aaawait = *aaawait
73 o.dhcpwait = *dhcpwait
74 o.dhcpservip = *dhcpservip
75 o.intvl = *intvl
76 o.intvl_test = *intvl_test
Matteo Scandolo88e91892018-11-06 16:29:19 -080077 o.KafkaBroker = *kafkaBroker
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090078 o.address = (strings.Split(*addressport, ":")[0])
79 tmp, _ := strconv.Atoi(strings.Split(*addressport, ":")[1])
80 o.port = uint32(tmp)
81 return o
82}
83
Matteo Scandolo88e91892018-11-06 16:29:19 -080084type handler struct {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090085 dst device.DeviceState
86 src device.DeviceState
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090087 method func(s *Server) error
88}
89
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090090type mediator struct {
Matteo Scandolo88e91892018-11-06 16:29:19 -080091 opt *option
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090092 server *Server
93 tester *Tester
94}
95
Matteo Scandolo88e91892018-11-06 16:29:19 -080096func NewMediator(o *option) *mediator {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090097 m := new(mediator)
98 m.opt = o
Matteo Scandolo88e91892018-11-06 16:29:19 -080099 logger.WithFields(log.Fields{
100 "ip": o.address,
101 "baseport": o.port,
102 "pon_ports": o.npon,
103 "onus": o.nonus,
104 "mode": o.Mode,
105 }).Debug("New mediator")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900106 return m
107}
108
Matteo Scandolo88e91892018-11-06 16:29:19 -0800109func (m *mediator) Start() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 var wg sync.WaitGroup
111 opt := m.opt
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900112 server := NewCore(opt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900113 wg.Add(1)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800114 go func() {
115 if err := server.Start(); err != nil { //Blocking
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800116 logger.Error("Start %s", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900117 }
118 wg.Done()
119 return
120 }()
121
122 tester := NewTester(opt)
123 m.server = server
124 m.tester = tester
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900125
Matteo Scandolo88e91892018-11-06 16:29:19 -0800126 go func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900127 m.Mediate(server)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900128 }()
129
130 c := make(chan os.Signal, 1)
131 signal.Notify(c, os.Interrupt)
132 go func() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800133 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900134 logger.Debug("SIGINT catcher Done")
135 wg.Done()
136 }()
137 for sig := range c {
138 wg.Add(1)
139 fmt.Println("SIGINT", sig)
140 close(c)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800141 server.Stop() //Non-blocking
142 tester.Stop(server) //Non-blocking
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900143 return
144 }
145 }()
146 wg.Wait()
147 logger.Debug("Reach to the end line")
148}
149
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900150func (m *mediator) Mediate(s *Server) {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900151 defer logger.Debug("Mediate Done")
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900152 for sr := range m.server.stateRepCh {
153 next := sr.next
154 current := sr.current
155 dev := sr.device
156 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}){
157 logger.Debug("Received OLT Device %v Current: %d Next: %d", dev, current, next)
158 if err := transitOlt(s, current, next, m.tester, m.opt); err != nil {
159 logger.Error("%v", err)
160 }
161 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
162 logger.Debug("Received ONU Device %v Current: %d Next: %d", dev, current, next)
163 key := dev.GetDevkey()
164 if err := transitOnu(s, key, current, next, m.tester, m.opt); err != nil {
165 logger.Error("%v", err)
166 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900167 }
168 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800169}
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900170
171func transitOlt (s *Server, current device.DeviceState, next device.DeviceState, tester *Tester, o *option) error {
172 if current == device.OLT_PREACTIVE && next == device.OLT_ACTIVE {
173 tester.Start(s)
174 } else if current == device.OLT_ACTIVE && next == device.OLT_PREACTIVE{
175 tester.Stop(s)
176 }
177 return nil
178}
179
180func transitOnu (s *Server, key device.Devkey, current device.DeviceState, next device.DeviceState, tester *Tester, o *option) error {
181 return nil
182}