Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 20 | "flag" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 21 | "os" |
| 22 | "os/signal" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 23 | "reflect" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 24 | "strconv" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 25 | "strings" |
| 26 | "sync" |
Mahir Gunyel | 32dfd72 | 2019-08-05 16:18:06 +0300 | [diff] [blame] | 27 | "time" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 28 | |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame] | 29 | "github.com/opencord/voltha-bbsim/common/logger" |
| 30 | "github.com/opencord/voltha-bbsim/device" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 31 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 32 | ) |
| 33 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 34 | const ( |
| 35 | DEFAULT Mode = iota |
| 36 | AAA |
| 37 | BOTH |
| 38 | ) |
| 39 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 40 | // Store emulation mode |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 41 | type Mode int |
| 42 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 43 | // AutoONUActivate is flag for Auto ONU Add on/off. |
| 44 | var AutoONUActivate int |
| 45 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 46 | type option struct { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 47 | address string |
| 48 | port uint32 |
| 49 | mgmtGrpcPort uint32 |
| 50 | mgmtRestPort uint32 |
| 51 | oltid uint32 |
| 52 | npon uint32 |
| 53 | nonus uint32 |
| 54 | aaawait int |
| 55 | dhcpwait int |
| 56 | dhcpservip string |
Mahir Gunyel | 32dfd72 | 2019-08-05 16:18:06 +0300 | [diff] [blame] | 57 | intvl time.Duration |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 58 | interactiveOnuActivation bool |
| 59 | Mode Mode |
| 60 | KafkaBroker string |
| 61 | Debuglvl string |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 62 | } |
| 63 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 64 | // GetOptions receives command line options and stores them in option structure |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 65 | func GetOptions() *option { |
| 66 | o := new(option) |
| 67 | addressport := flag.String("H", ":50060", "IP address:port") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 68 | oltid := flag.Int("id", 0, "OLT-ID") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 69 | npon := flag.Int("i", 1, "Number of PON-IF ports") |
| 70 | nonus := flag.Int("n", 1, "Number of ONUs per PON-IF port") |
| 71 | modeopt := flag.String("m", "default", "Emulation mode (default, aaa, both (aaa & dhcp))") |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 72 | aaawait := flag.Int("aw", 2, "Wait time (sec) for activation WPA supplicants after EAPOL flow entry installed") |
| 73 | dhcpwait := flag.Int("dw", 2, "Wait time (sec) for activation DHCP clients after DHCP flow entry installed") |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 74 | dhcpservip := flag.String("s", "182.21.0.128", "DHCP Server IP Address") |
Mahir Gunyel | 32dfd72 | 2019-08-05 16:18:06 +0300 | [diff] [blame] | 75 | intvl := flag.Duration("v", 0, "Interval each Discovery Indication, in the form of unit+suffix, such as '10ms', '1s' or '1m''. defaults to 0") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 76 | kafkaBroker := flag.String("k", "", "Kafka broker") |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 77 | interactiveOnuActivation := flag.Bool("ia", false, "Enable interactive activation of ONUs") |
| 78 | mgmtGrpcPort := flag.Int("grpc", 50061, "BBSim API server gRPC port") |
| 79 | mgmtRestPort := flag.Int("rest", 50062, "BBSim API server REST port") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 80 | o.Mode = DEFAULT |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 81 | debg := flag.String("d", "DEBUG", "Debug Level(TRACE DEBUG INFO WARN ERROR)") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 82 | flag.Parse() |
| 83 | if *modeopt == "aaa" { |
| 84 | o.Mode = AAA |
| 85 | } else if *modeopt == "both" { |
| 86 | o.Mode = BOTH |
| 87 | } |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 88 | o.Debuglvl = *debg |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 89 | o.oltid = uint32(*oltid) |
| 90 | o.npon = uint32(*npon) |
| 91 | o.nonus = uint32(*nonus) |
| 92 | o.aaawait = *aaawait |
| 93 | o.dhcpwait = *dhcpwait |
| 94 | o.dhcpservip = *dhcpservip |
| 95 | o.intvl = *intvl |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 96 | o.interactiveOnuActivation = *interactiveOnuActivation |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 97 | o.KafkaBroker = *kafkaBroker |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 98 | o.address = strings.Split(*addressport, ":")[0] |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 99 | tmp, _ := strconv.Atoi(strings.Split(*addressport, ":")[1]) |
| 100 | o.port = uint32(tmp) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 101 | o.mgmtGrpcPort = uint32(*mgmtGrpcPort) |
| 102 | o.mgmtRestPort = uint32(*mgmtRestPort) |
| 103 | |
| 104 | if o.interactiveOnuActivation == true { |
| 105 | log.Info("Automatic ONU activation disabled: use BBSim API to activate ONUs") |
| 106 | } |
| 107 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 108 | return o |
| 109 | } |
| 110 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 111 | type mediator struct { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 112 | opt *option |
| 113 | server *Server |
| 114 | testmanager *TestManager |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 117 | // NewMediator returns a new mediator object |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 118 | func NewMediator(o *option) *mediator { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 119 | m := new(mediator) |
| 120 | m.opt = o |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 121 | logger.WithFields(log.Fields{ |
| 122 | "ip": o.address, |
| 123 | "baseport": o.port, |
| 124 | "pon_ports": o.npon, |
| 125 | "onus": o.nonus, |
| 126 | "mode": o.Mode, |
| 127 | }).Debug("New mediator") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 128 | return m |
| 129 | } |
| 130 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 131 | // Start mediator |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 132 | func (m *mediator) Start() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 133 | var wg sync.WaitGroup |
| 134 | opt := m.opt |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 135 | server := NewCore(opt) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 136 | server.wg = &sync.WaitGroup{} |
| 137 | server.wg.Add(1) |
| 138 | go server.StartServerActionLoop(&wg) |
| 139 | server.serverActionCh <- "start" |
| 140 | go server.startMgmtServer(&wg) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 141 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 142 | tm := NewTestManager(opt) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 143 | m.server = server |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 144 | m.testmanager = tm |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 145 | go func() { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 146 | m.Mediate() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 147 | }() |
| 148 | |
| 149 | c := make(chan os.Signal, 1) |
| 150 | signal.Notify(c, os.Interrupt) |
| 151 | go func() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 152 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 153 | logger.Debug("SIGINT catcher Done") |
| 154 | wg.Done() |
| 155 | }() |
| 156 | for sig := range c { |
| 157 | wg.Add(1) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 158 | logger.Debug("SIGINT %v", sig) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 159 | close(c) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 160 | server.Stop() // Non-blocking |
| 161 | tm.Stop() // Non-blocking |
| 162 | server.stopMgmtServer() |
| 163 | server.wg.Done() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 164 | return |
| 165 | } |
| 166 | }() |
| 167 | wg.Wait() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 168 | server.wg.Wait() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 169 | } |
| 170 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 171 | // Mediate method is invoked on OLT and ONU state change |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 172 | func (m *mediator) Mediate() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 173 | defer logger.Debug("Mediate Done") |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 174 | for sr := range m.server.stateRepCh { |
| 175 | next := sr.next |
| 176 | current := sr.current |
| 177 | dev := sr.device |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 178 | key := dev.GetDevkey() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 179 | if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 180 | logger.WithFields(log.Fields{ |
| 181 | "device": dev, |
| 182 | }).Debugf("Received OLT Device state change %v Current: %d Next: %d", key, current, next) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 183 | if err := transitOlt(current, next, m.testmanager, m.opt); err != nil { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 184 | logger.Error("%v", err) |
| 185 | } |
| 186 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 187 | logger.WithFields(log.Fields{ |
| 188 | "device": dev, |
| 189 | }).Debugf("Received ONU Device state change %v Current: %d Next: %d", key, current, next) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 190 | if err := transitOnu(key, current, next, m.testmanager, m.opt); err != nil { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 191 | logger.Error("%v", err) |
| 192 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 193 | } |
| 194 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 195 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 196 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 197 | func transitOlt(current device.DeviceState, next device.DeviceState, tm *TestManager, o *option) error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 198 | logger.Debug("trnsitOlt called current:%d , next:%d", current, next) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 199 | if current == device.OLT_PREACTIVE && next == device.OLT_ACTIVE { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 200 | tm.Start() |
Mahir Gunyel | 639c4f7 | 2019-04-26 12:05:32 -0700 | [diff] [blame] | 201 | nniup, _ := makeNniName(o.oltid) |
| 202 | activateDHCPServer(nniup, o.dhcpservip) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 203 | } else if current == device.OLT_ACTIVE && next == device.OLT_PREACTIVE { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 204 | tm.Stop() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 205 | } else if current == device.OLT_ACTIVE && next == device.OLT_INACTIVE { |
| 206 | // Reboot case |
| 207 | // TODO Point of discussion |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 208 | } |
| 209 | return nil |
| 210 | } |
| 211 | |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 212 | func transitOnu(key device.Devkey, previous device.DeviceState, current device.DeviceState, tm *TestManager, o *option) error { |
| 213 | logger.Debug("transitOnu called with key: %v, previous: %s, current: %s", key, device.ONUState[previous], device.ONUState[current]) |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 214 | if o.Mode == AAA || o.Mode == BOTH { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 215 | if previous == device.ONU_ACTIVE && current == device.ONU_OMCIACTIVE { |
| 216 | logger.Debug("Starting WPASupplicant for device %v", key) |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 217 | t := tm.CreateTester("AAA", o, key, activateWPASupplicant, o.aaawait) |
| 218 | if err := tm.StartTester(t); err != nil { |
| 219 | logger.Error("Cannot Start AAA Executer error:%v", err) |
| 220 | } |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 221 | } else if previous == device.ONU_OMCIACTIVE && current == device.ONU_INACTIVE { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 222 | if err := tm.StopTester("AAA", key); err != nil { |
| 223 | logger.Error("Cannot Stop AAA Executer error:%v", err) |
| 224 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 225 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 226 | } |
| 227 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 228 | if o.Mode == BOTH { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 229 | if previous == device.ONU_OMCIACTIVE && current == device.ONU_AUTHENTICATED { |
| 230 | logger.Debug("Starting DHCP client for device %v", key) |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 231 | t := tm.CreateTester("DHCP", o, key, activateDHCPClient, o.dhcpwait) |
| 232 | if err := tm.StartTester(t); err != nil { |
| 233 | logger.Error("Cannot Start DHCP Executer error:%v", err) |
| 234 | } |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 235 | } else if previous == device.ONU_AUTHENTICATED && current == device.ONU_INACTIVE { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 236 | if err := tm.StopTester("DHCP", key); err != nil { |
| 237 | logger.Error("Cannot Stop DHCP Executer error:%v", err) |
| 238 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 239 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 240 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 241 | return nil |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 242 | } |