blob: b1d098d2c089afcbf92e55714950b1426b316c9f [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
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090034const (
35 DEFAULT Mode = iota
36 AAA
37 BOTH
38)
39
40type Mode int
41
Matteo Scandolo88e91892018-11-06 16:29:19 -080042type option struct {
43 address string
44 port uint32
45 oltid uint32
46 npon uint32
47 nonus uint32
48 aaawait int
49 dhcpwait int
50 dhcpservip string
51 intvl int
Matteo Scandolo88e91892018-11-06 16:29:19 -080052 Mode Mode
53 KafkaBroker string
Mahir Gunyel09183342019-01-29 14:26:50 -080054 Debuglvl string
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090055}
56
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020057// GetOptions receives command line options and stores them in option structure
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090058func GetOptions() *option {
59 o := new(option)
60 addressport := flag.String("H", ":50060", "IP address:port")
Matteo Scandolo88e91892018-11-06 16:29:19 -080061 oltid := flag.Int("id", 0, "OLT-ID")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090062 npon := flag.Int("i", 1, "Number of PON-IF ports")
63 nonus := flag.Int("n", 1, "Number of ONUs per PON-IF port")
64 modeopt := flag.String("m", "default", "Emulation mode (default, aaa, both (aaa & dhcp))")
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090065 aaawait := flag.Int("aw", 2, "Wait time (sec) for activation WPA supplicants after EAPOL flow entry installed")
66 dhcpwait := flag.Int("dw", 2, "Wait time (sec) for activation DHCP clients after DHCP flow entry installed")
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +090067 dhcpservip := flag.String("s", "182.21.0.128", "DHCP Server IP Address")
Keita NISHIMOTOd502e662019-03-01 12:02:51 +090068 intvl := flag.Int("v", 1000, "Interval each Indication (ms)")
Matteo Scandolo88e91892018-11-06 16:29:19 -080069 kafkaBroker := flag.String("k", "", "Kafka broker")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090070 o.Mode = DEFAULT
Mahir Gunyel09183342019-01-29 14:26:50 -080071 debg := flag.String("d", "DEBUG", "Debug Level(TRACE DEBUG INFO WARN ERROR)")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090072 flag.Parse()
73 if *modeopt == "aaa" {
74 o.Mode = AAA
75 } else if *modeopt == "both" {
76 o.Mode = BOTH
77 }
Mahir Gunyel09183342019-01-29 14:26:50 -080078 o.Debuglvl = *debg
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090079 o.oltid = uint32(*oltid)
80 o.npon = uint32(*npon)
81 o.nonus = uint32(*nonus)
82 o.aaawait = *aaawait
83 o.dhcpwait = *dhcpwait
84 o.dhcpservip = *dhcpservip
85 o.intvl = *intvl
Matteo Scandolo88e91892018-11-06 16:29:19 -080086 o.KafkaBroker = *kafkaBroker
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090087 o.address = (strings.Split(*addressport, ":")[0])
88 tmp, _ := strconv.Atoi(strings.Split(*addressport, ":")[1])
89 o.port = uint32(tmp)
90 return o
91}
92
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090093type mediator struct {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090094 opt *option
95 server *Server
96 testmanager *TestManager
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090097}
98
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020099// NewMediator returns a new mediator object
Matteo Scandolo88e91892018-11-06 16:29:19 -0800100func NewMediator(o *option) *mediator {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900101 m := new(mediator)
102 m.opt = o
Matteo Scandolo88e91892018-11-06 16:29:19 -0800103 logger.WithFields(log.Fields{
104 "ip": o.address,
105 "baseport": o.port,
106 "pon_ports": o.npon,
107 "onus": o.nonus,
108 "mode": o.Mode,
109 }).Debug("New mediator")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 return m
111}
112
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200113// Start mediator
Matteo Scandolo88e91892018-11-06 16:29:19 -0800114func (m *mediator) Start() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900115 var wg sync.WaitGroup
116 opt := m.opt
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900117 server := NewCore(opt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900118 wg.Add(1)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800119 go func() {
120 if err := server.Start(); err != nil { //Blocking
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800121 logger.Error("Start %s", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900122 }
123 wg.Done()
124 return
125 }()
126
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900127 tm := NewTestManager(opt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900128 m.server = server
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900129 m.testmanager = tm
Matteo Scandolo88e91892018-11-06 16:29:19 -0800130 go func() {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900131 m.Mediate()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900132 }()
133
134 c := make(chan os.Signal, 1)
135 signal.Notify(c, os.Interrupt)
136 go func() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800137 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900138 logger.Debug("SIGINT catcher Done")
139 wg.Done()
140 }()
141 for sig := range c {
142 wg.Add(1)
143 fmt.Println("SIGINT", sig)
144 close(c)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900145 server.Stop() //Non-blocking
146 tm.Stop() //Non-blocking
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900147 return
148 }
149 }()
150 wg.Wait()
151 logger.Debug("Reach to the end line")
152}
153
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200154// Mediate method is invoked on OLT and ONU state change
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900155func (m *mediator) Mediate() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900156 defer logger.Debug("Mediate Done")
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900157 for sr := range m.server.stateRepCh {
158 next := sr.next
159 current := sr.current
160 dev := sr.device
161 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}){
162 logger.Debug("Received OLT Device %v Current: %d Next: %d", dev, current, next)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900163 if err := transitOlt(current, next, m.testmanager, m.opt); err != nil {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900164 logger.Error("%v", err)
165 }
166 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
167 logger.Debug("Received ONU Device %v Current: %d Next: %d", dev, current, next)
168 key := dev.GetDevkey()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900169 if err := transitOnu(key, current, next, m.testmanager, m.opt); err != nil {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900170 logger.Error("%v", err)
171 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900172 }
173 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800174}
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900175
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900176func transitOlt (current device.DeviceState, next device.DeviceState, tm *TestManager, o *option) error {
177 logger.Debug("trnsitOlt called current:%d , next:%d", current, next)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900178 if current == device.OLT_PREACTIVE && next == device.OLT_ACTIVE {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900179 tm.Start()
Mahir Gunyel639c4f72019-04-26 12:05:32 -0700180 nniup, _ := makeNniName(o.oltid)
181 activateDHCPServer(nniup, o.dhcpservip)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900182 } else if current == device.OLT_ACTIVE && next == device.OLT_PREACTIVE{
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900183 tm.Stop()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900184 }
185 return nil
186}
187
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900188func transitOnu (key device.Devkey, current device.DeviceState, next device.DeviceState, tm *TestManager, o *option) error {
Zack Williamsb85f5932019-05-10 16:21:35 -0700189 logger.Debug("trnsitOnu called with key: %v, current: %d, next: %d", key, current, next)
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900190 if o.Mode == AAA || o.Mode == BOTH {
191 if current == device.ONU_ACTIVE && next == device.ONU_OMCIACTIVE {
192 t := tm.CreateTester("AAA", o, key, activateWPASupplicant, o.aaawait)
193 if err := tm.StartTester(t); err != nil {
194 logger.Error("Cannot Start AAA Executer error:%v", err)
195 }
196 } else if current == device.ONU_OMCIACTIVE && next == device.ONU_INACTIVE {
197 if err := tm.StopTester("AAA", key); err != nil {
198 logger.Error("Cannot Stop AAA Executer error:%v", err)
199 }
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900200 }
Keita NISHIMOTO2807c542019-06-04 22:58:32 +0900201 }
202
203 if o.Mode == BOTH{
204 if current == device.ONU_OMCIACTIVE && next == device.ONU_AUTHENTICATED {
205 t := tm.CreateTester("DHCP", o, key, activateDHCPClient, o.dhcpwait)
206 if err := tm.StartTester(t); err != nil {
207 logger.Error("Cannot Start DHCP Executer error:%v", err)
208 }
209 } else if current == device.ONU_AUTHENTICATED && next == device.ONU_INACTIVE {
210 if err := tm.StopTester("DHCP", key); err != nil {
211 logger.Error("Cannot Stop DHCP Executer error:%v", err)
212 }
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900213 }
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900214 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900215 return nil
Mahir Gunyel09183342019-01-29 14:26:50 -0800216}