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