Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [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 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 17 | package devices |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
| 22 | "fmt" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 23 | "net" |
| 24 | "sync" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 25 | "time" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 26 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 27 | "github.com/google/gopacket" |
| 28 | "github.com/google/gopacket/layers" |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 29 | "github.com/google/gopacket/pcap" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 30 | "github.com/looplab/fsm" |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 31 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 32 | bbsim "github.com/opencord/bbsim/internal/bbsim/types" |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 33 | "github.com/opencord/bbsim/internal/common" |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 34 | omcisim "github.com/opencord/omci-sim" |
Matteo Scandolo | 3de9de0 | 2019-11-14 13:40:03 -0800 | [diff] [blame] | 35 | "github.com/opencord/voltha-protos/v2/go/openolt" |
| 36 | "github.com/opencord/voltha-protos/v2/go/tech_profile" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 37 | log "github.com/sirupsen/logrus" |
| 38 | "google.golang.org/grpc" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 39 | "google.golang.org/grpc/reflection" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 42 | var oltLogger = log.WithFields(log.Fields{ |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 43 | "module": "OLT", |
| 44 | }) |
| 45 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 46 | type OltDevice struct { |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 47 | sync.Mutex |
| 48 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 49 | // BBSIM Internals |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 50 | ID int |
| 51 | SerialNumber string |
| 52 | NumNni int |
| 53 | NumPon int |
| 54 | NumOnuPerPon int |
| 55 | InternalState *fsm.FSM |
| 56 | channel chan Message |
| 57 | nniPktInChannel chan *bbsim.PacketMsg // packets coming in from the NNI and going to VOLTHA |
| 58 | nniHandle *pcap.Handle // handle on the NNI interface, close it when shutting down the NNI channel |
| 59 | Delay int |
| 60 | ControlledActivation mode |
Matteo Scandolo | e33447a | 2019-10-31 12:38:23 -0700 | [diff] [blame] | 61 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 62 | Pons []*PonPort |
| 63 | Nnis []*NniPort |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 64 | |
| 65 | // OLT Attributes |
| 66 | OperState *fsm.FSM |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 67 | |
| 68 | enableContext context.Context |
| 69 | enableContextCancel context.CancelFunc |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 70 | |
| 71 | OpenoltStream *openolt.Openolt_EnableIndicationServer |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 74 | var olt OltDevice |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 75 | var oltServer *grpc.Server |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 76 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 77 | func GetOLT() *OltDevice { |
| 78 | return &olt |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 81 | func CreateOLT(oltId int, nni int, pon int, onuPerPon int, sTag int, cTagInit int, auth bool, dhcp bool, delay int, ca string, isMock bool) *OltDevice { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 82 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 83 | "ID": oltId, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 84 | "NumNni": nni, |
| 85 | "NumPon": pon, |
| 86 | "NumOnuPerPon": onuPerPon, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 87 | }).Debug("CreateOLT") |
| 88 | |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 89 | olt = OltDevice{ |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 90 | ID: oltId, |
| 91 | SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", oltId), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 92 | OperState: getOperStateFSM(func(e *fsm.Event) { |
| 93 | oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst) |
| 94 | }), |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 95 | NumNni: nni, |
| 96 | NumPon: pon, |
| 97 | NumOnuPerPon: onuPerPon, |
| 98 | Pons: []*PonPort{}, |
| 99 | Nnis: []*NniPort{}, |
| 100 | Delay: delay, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 103 | if val, ok := ControlledActivationModes[ca]; ok { |
| 104 | olt.ControlledActivation = val |
| 105 | } else { |
| 106 | oltLogger.Warn("Unknown ControlledActivation Mode given, running in Default mode") |
| 107 | olt.ControlledActivation = Default |
| 108 | } |
| 109 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 110 | // OLT State machine |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 111 | // NOTE do we need 2 state machines for the OLT? (InternalState and OperState) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 112 | olt.InternalState = fsm.NewFSM( |
| 113 | "created", |
| 114 | fsm.Events{ |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 115 | {Name: "initialize", Src: []string{"created", "deleted"}, Dst: "initialized"}, |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 116 | {Name: "enable", Src: []string{"initialized", "disabled"}, Dst: "enabled"}, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 117 | {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"}, |
Mahir Gunyel | 6dad445 | 2020-01-06 12:59:04 -0800 | [diff] [blame] | 118 | //delete event in enabled state below is for reboot OLT case. |
| 119 | {Name: "delete", Src: []string{"disabled", "enabled"}, Dst: "deleted"}, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 120 | }, |
| 121 | fsm.Callbacks{ |
| 122 | "enter_state": func(e *fsm.Event) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 123 | oltLogger.Debugf("Changing OLT InternalState from %s to %s", e.Src, e.Dst) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 124 | }, |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 125 | "enter_initialized": func(e *fsm.Event) { olt.InitOlt() }, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 126 | }, |
| 127 | ) |
| 128 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 129 | if isMock != true { |
| 130 | // create NNI Port |
| 131 | nniPort, err := CreateNNI(&olt) |
| 132 | if err != nil { |
| 133 | oltLogger.Fatalf("Couldn't create NNI Port: %v", err) |
| 134 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 135 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 136 | olt.Nnis = append(olt.Nnis, &nniPort) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 137 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 138 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 139 | // create PON ports |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 140 | availableCTag := cTagInit |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 141 | for i := 0; i < pon; i++ { |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 142 | p := CreatePonPort(&olt, uint32(i)) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 143 | |
| 144 | // create ONU devices |
| 145 | for j := 0; j < onuPerPon; j++ { |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 146 | o := CreateONU(&olt, *p, uint32(j+1), sTag, availableCTag, auth, dhcp, isMock) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 147 | p.Onus = append(p.Onus, o) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 148 | availableCTag = availableCTag + 1 |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 151 | olt.Pons = append(olt.Pons, p) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 152 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 153 | |
Matteo Scandolo | d32c382 | 2019-11-26 15:57:46 -0700 | [diff] [blame] | 154 | if isMock != true { |
| 155 | if err := olt.InternalState.Event("initialize"); err != nil { |
| 156 | log.Errorf("Error initializing OLT: %v", err) |
| 157 | return nil |
| 158 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 161 | return &olt |
| 162 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 163 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 164 | func (o *OltDevice) InitOlt() error { |
| 165 | |
| 166 | if oltServer == nil { |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 167 | oltServer, _ = o.newOltServer() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 168 | } else { |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 169 | // FIXME there should never be a server running if we are initializing the OLT |
| 170 | oltLogger.Fatal("OLT server already running.") |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // create new channel for processOltMessages Go routine |
| 174 | o.channel = make(chan Message) |
| 175 | |
| 176 | o.nniPktInChannel = make(chan *bbsim.PacketMsg, 1024) |
| 177 | // FIXME we are assuming we have only one NNI |
| 178 | if o.Nnis[0] != nil { |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 179 | // NOTE we want to make sure the state is down when we initialize the OLT, |
| 180 | // the NNI may be in a bad state after a disable/reboot as we are not disabling it for |
| 181 | // in-band management |
| 182 | o.Nnis[0].OperState.SetState("down") |
| 183 | ch, handle, err := o.Nnis[0].NewVethChan() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 184 | if err == nil { |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 185 | oltLogger.WithFields(log.Fields{ |
| 186 | "Type": o.Nnis[0].Type, |
| 187 | "IntfId": o.Nnis[0].ID, |
| 188 | "OperState": o.Nnis[0].OperState.Current(), |
| 189 | }).Info("NNI Channel created") |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 190 | o.nniPktInChannel = ch |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 191 | o.nniHandle = handle |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 192 | } else { |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 193 | oltLogger.Errorf("Error getting NNI channel: %v", err) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 197 | return nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 200 | func (o *OltDevice) RestartOLT() error { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 201 | |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 202 | rebootDelay := common.Options.Olt.OltRebootDelay |
| 203 | |
| 204 | oltLogger.WithFields(log.Fields{ |
| 205 | "oltId": o.ID, |
| 206 | }).Infof("Simulating OLT restart... (%ds)", rebootDelay) |
| 207 | |
| 208 | // transition internal state to deleted |
| 209 | if err := o.InternalState.Event("delete"); err != nil { |
| 210 | oltLogger.WithFields(log.Fields{ |
| 211 | "oltId": o.ID, |
| 212 | }).Errorf("Error deleting OLT: %v", err) |
| 213 | return err |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // TODO handle hard poweroff (i.e. no indications sent to Voltha) vs soft poweroff |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 217 | time.Sleep(1 * time.Second) // we need to give the OLT the time to respond to all the pending gRPC request before stopping the server |
| 218 | if err := o.StopOltServer(); err != nil { |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 219 | oltLogger.Errorf("Error in stopping OLT server") |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 220 | return err |
| 221 | } |
| 222 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 223 | for _, pon := range olt.Pons { |
| 224 | msg := Message{ |
| 225 | Type: PonIndication, |
| 226 | Data: PonIndicationMessage{ |
| 227 | OperState: DOWN, |
| 228 | PonPortID: pon.ID, |
| 229 | }, |
| 230 | } |
| 231 | o.channel <- msg |
| 232 | |
| 233 | for _, onu := range pon.Onus { |
| 234 | if onu.InternalState.Current() != "initialized" { |
| 235 | onu.InternalState.Event("disable") |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 240 | // terminate the OLT's processOltMessages go routine |
| 241 | close(o.channel) |
| 242 | // terminate the OLT's processNniPacketIns go routine |
Andy Bavier | 421f4d5 | 2020-01-21 15:39:22 -0700 | [diff] [blame] | 243 | go o.nniHandle.Close() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 244 | close(o.nniPktInChannel) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 245 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 246 | time.Sleep(time.Duration(rebootDelay) * time.Second) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 247 | |
| 248 | if err := o.InternalState.Event("initialize"); err != nil { |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 249 | oltLogger.WithFields(log.Fields{ |
| 250 | "oltId": o.ID, |
| 251 | }).Errorf("Error initializing OLT: %v", err) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 252 | return err |
| 253 | } |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 254 | oltLogger.WithFields(log.Fields{ |
| 255 | "oltId": o.ID, |
| 256 | }).Info("OLT restart completed") |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 257 | return nil |
| 258 | } |
| 259 | |
| 260 | // newOltServer launches a new grpc server for OpenOLT |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 261 | func (o *OltDevice) newOltServer() (*grpc.Server, error) { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 262 | address := common.Options.BBSim.OpenOltAddress |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 263 | lis, err := net.Listen("tcp", address) |
| 264 | if err != nil { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 265 | oltLogger.Fatalf("OLT failed to listen: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 266 | } |
| 267 | grpcServer := grpc.NewServer() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 268 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 269 | openolt.RegisterOpenoltServer(grpcServer, o) |
| 270 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 271 | reflection.Register(grpcServer) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 272 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 273 | go grpcServer.Serve(lis) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 274 | oltLogger.Debugf("OLT listening on %v", address) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 275 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 276 | return grpcServer, nil |
| 277 | } |
| 278 | |
| 279 | // StopOltServer stops the OpenOLT grpc server |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 280 | func (o *OltDevice) StopOltServer() error { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 281 | // TODO handle poweroff vs graceful shutdown |
| 282 | if oltServer != nil { |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 283 | oltLogger.WithFields(log.Fields{ |
| 284 | "oltId": o.SerialNumber, |
| 285 | }).Warnf("Stopping OLT gRPC server") |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 286 | oltServer.Stop() |
| 287 | oltServer = nil |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 288 | } |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 289 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 290 | return nil |
| 291 | } |
| 292 | |
| 293 | // Device Methods |
| 294 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 295 | // Enable implements the OpenOLT EnableIndicationServer functionality |
| 296 | func (o *OltDevice) Enable(stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 297 | oltLogger.Debug("Enable OLT called") |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 298 | rebootFlag := false |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 299 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 300 | // If enabled has already been called then an enabled context has |
| 301 | // been created. If this is the case then we want to cancel all the |
| 302 | // proessing loops associated with that enable before we recreate |
| 303 | // new ones |
| 304 | o.Lock() |
| 305 | if o.enableContext != nil && o.enableContextCancel != nil { |
| 306 | o.enableContextCancel() |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 307 | rebootFlag = true |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 308 | } |
| 309 | o.enableContext, o.enableContextCancel = context.WithCancel(context.TODO()) |
| 310 | o.Unlock() |
| 311 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 312 | wg := sync.WaitGroup{} |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 313 | wg.Add(3) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 314 | |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 315 | o.OpenoltStream = &stream |
| 316 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 317 | // create Go routine to process all OLT events |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 318 | go o.processOltMessages(o.enableContext, stream, &wg) |
| 319 | go o.processNniPacketIns(o.enableContext, stream, &wg) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 320 | |
| 321 | // enable the OLT |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 322 | oltMsg := Message{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 323 | Type: OltIndication, |
| 324 | Data: OltIndicationMessage{ |
| 325 | OperState: UP, |
| 326 | }, |
| 327 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 328 | o.channel <- oltMsg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 329 | |
| 330 | // send NNI Port Indications |
| 331 | for _, nni := range o.Nnis { |
| 332 | msg := Message{ |
| 333 | Type: NniIndication, |
| 334 | Data: NniIndicationMessage{ |
| 335 | OperState: UP, |
| 336 | NniPortID: nni.ID, |
| 337 | }, |
| 338 | } |
| 339 | o.channel <- msg |
| 340 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 341 | |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 342 | go o.processOmciMessages(o.enableContext, stream, &wg) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 343 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 344 | if rebootFlag == true { |
| 345 | for _, pon := range o.Pons { |
| 346 | if pon.InternalState.Current() == "disabled" { |
| 347 | msg := Message{ |
| 348 | Type: PonIndication, |
| 349 | Data: PonIndicationMessage{ |
| 350 | OperState: UP, |
| 351 | PonPortID: pon.ID, |
| 352 | }, |
| 353 | } |
| 354 | o.channel <- msg |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 355 | } |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 356 | } |
| 357 | } else { |
| 358 | |
| 359 | // 1. controlledActivation == Default: Send both PON and ONUs indications |
| 360 | // 2. controlledActivation == only-onu: that means only ONUs will be controlled activated, so auto send PON indications |
| 361 | |
| 362 | if o.ControlledActivation == Default || o.ControlledActivation == OnlyONU { |
| 363 | // send PON Port indications |
| 364 | for _, pon := range o.Pons { |
| 365 | msg := Message{ |
| 366 | Type: PonIndication, |
| 367 | Data: PonIndicationMessage{ |
| 368 | OperState: UP, |
| 369 | PonPortID: pon.ID, |
| 370 | }, |
| 371 | } |
| 372 | o.channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 373 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 377 | oltLogger.Debug("Enable OLT Done") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 378 | wg.Wait() |
| 379 | return nil |
| 380 | } |
| 381 | |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 382 | func (o *OltDevice) processOmciMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) { |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 383 | ch := omcisim.GetChannel() |
| 384 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 385 | oltLogger.Debug("Starting OMCI Indication Channel") |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 386 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 387 | loop: |
| 388 | for { |
| 389 | select { |
| 390 | case <-ctx.Done(): |
| 391 | oltLogger.Debug("OMCI processing canceled via context") |
| 392 | break loop |
| 393 | case message, ok := <-ch: |
| 394 | if !ok || ctx.Err() != nil { |
| 395 | oltLogger.Debug("OMCI processing canceled via channel close") |
| 396 | break loop |
| 397 | } |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 398 | |
| 399 | oltLogger.WithFields(log.Fields{ |
| 400 | "messageType": message.Type, |
| 401 | "OnuId": message.Data.OnuId, |
| 402 | "IntfId": message.Data.IntfId, |
| 403 | }).Info("Received message on OMCI Sim channel") |
| 404 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 405 | onuId := message.Data.OnuId |
| 406 | intfId := message.Data.IntfId |
| 407 | onu, err := o.FindOnuById(intfId, onuId) |
| 408 | if err != nil { |
| 409 | oltLogger.Errorf("Failed to find onu: %v", err) |
| 410 | continue |
| 411 | } |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 412 | go onu.processOmciMessage(message, stream) |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 413 | } |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 414 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 415 | |
| 416 | wg.Done() |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 417 | } |
| 418 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 419 | // Helpers method |
| 420 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 421 | func (o OltDevice) GetPonById(id uint32) (*PonPort, error) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 422 | for _, pon := range o.Pons { |
| 423 | if pon.ID == id { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 424 | return pon, nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | return nil, errors.New(fmt.Sprintf("Cannot find PonPort with id %d in OLT %d", id, o.ID)) |
| 428 | } |
| 429 | |
| 430 | func (o OltDevice) getNniById(id uint32) (*NniPort, error) { |
| 431 | for _, nni := range o.Nnis { |
| 432 | if nni.ID == id { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 433 | return nni, nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | return nil, errors.New(fmt.Sprintf("Cannot find NniPort with id %d in OLT %d", id, o.ID)) |
| 437 | } |
| 438 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 439 | func (o *OltDevice) sendAlarmIndication(alarmInd *openolt.AlarmIndication, stream openolt.Openolt_EnableIndicationServer) { |
| 440 | data := &openolt.Indication_AlarmInd{AlarmInd: alarmInd} |
| 441 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 442 | oltLogger.Errorf("Failed to send Alarm Indication: %v", err) |
| 443 | return |
| 444 | } |
| 445 | |
| 446 | oltLogger.WithFields(log.Fields{ |
| 447 | "AlarmIndication": alarmInd, |
| 448 | }).Debug("Sent Indication_AlarmInd") |
| 449 | } |
| 450 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 451 | func (o *OltDevice) sendOltIndication(msg OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 452 | data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}} |
| 453 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 454 | oltLogger.Errorf("Failed to send Indication_OltInd: %v", err) |
Matteo Scandolo | 08badf4 | 2019-12-12 11:21:50 -0800 | [diff] [blame] | 455 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 458 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 459 | "OperState": msg.OperState, |
| 460 | }).Debug("Sent Indication_OltInd") |
| 461 | } |
| 462 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 463 | func (o *OltDevice) sendNniIndication(msg NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 464 | nni, _ := o.getNniById(msg.NniPortID) |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 465 | if msg.OperState == UP { |
| 466 | if err := nni.OperState.Event("enable"); err != nil { |
| 467 | log.WithFields(log.Fields{ |
| 468 | "Type": nni.Type, |
| 469 | "IntfId": nni.ID, |
| 470 | "OperState": nni.OperState.Current(), |
| 471 | }).Errorf("Can't move NNI Port to enabled state: %v", err) |
| 472 | } |
| 473 | } else if msg.OperState == DOWN { |
| 474 | if err := nni.OperState.Event("disable"); err != nil { |
| 475 | log.WithFields(log.Fields{ |
| 476 | "Type": nni.Type, |
| 477 | "IntfId": nni.ID, |
| 478 | "OperState": nni.OperState.Current(), |
| 479 | }).Errorf("Can't move NNI Port to disable state: %v", err) |
| 480 | } |
| 481 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 482 | // NOTE Operstate may need to be an integer |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 483 | operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 484 | Type: nni.Type, |
| 485 | IntfId: nni.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 486 | OperState: nni.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 487 | }} |
| 488 | |
| 489 | if err := stream.Send(&openolt.Indication{Data: operData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 490 | oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err) |
Matteo Scandolo | 08badf4 | 2019-12-12 11:21:50 -0800 | [diff] [blame] | 491 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 494 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 495 | "Type": nni.Type, |
| 496 | "IntfId": nni.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 497 | "OperState": nni.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 498 | }).Debug("Sent Indication_IntfOperInd for NNI") |
| 499 | } |
| 500 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 501 | func (o *OltDevice) sendPonIndication(ponPortID uint32) { |
| 502 | |
| 503 | stream := *o.OpenoltStream |
| 504 | pon, _ := o.GetPonById(ponPortID) |
| 505 | // Send IntfIndication for PON port |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 506 | discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 507 | IntfId: pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 508 | OperState: pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 509 | }} |
| 510 | |
| 511 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 512 | oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err) |
Matteo Scandolo | 08badf4 | 2019-12-12 11:21:50 -0800 | [diff] [blame] | 513 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 516 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 517 | "IntfId": pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 518 | "OperState": pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 519 | }).Debug("Sent Indication_IntfInd") |
| 520 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 521 | // Send IntfOperIndication for PON port |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 522 | operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 523 | Type: pon.Type, |
| 524 | IntfId: pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 525 | OperState: pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 526 | }} |
| 527 | |
| 528 | if err := stream.Send(&openolt.Indication{Data: operData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 529 | oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err) |
Matteo Scandolo | 08badf4 | 2019-12-12 11:21:50 -0800 | [diff] [blame] | 530 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 533 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 534 | "Type": pon.Type, |
| 535 | "IntfId": pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 536 | "OperState": pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 537 | }).Debug("Sent Indication_IntfOperInd for PON") |
| 538 | } |
| 539 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 540 | // processOltMessages handles messages received over the OpenOLT interface |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 541 | func (o *OltDevice) processOltMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 542 | oltLogger.Debug("Starting OLT Indication Channel") |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 543 | ch := o.channel |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 544 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 545 | loop: |
| 546 | for { |
| 547 | select { |
| 548 | case <-ctx.Done(): |
| 549 | oltLogger.Debug("OLT Indication processing canceled via context") |
| 550 | break loop |
| 551 | case message, ok := <-ch: |
| 552 | if !ok || ctx.Err() != nil { |
| 553 | oltLogger.Debug("OLT Indication processing canceled via closed channel") |
| 554 | break loop |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 555 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 556 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 557 | oltLogger.WithFields(log.Fields{ |
| 558 | "oltId": o.ID, |
| 559 | "messageType": message.Type, |
| 560 | }).Trace("Received message") |
| 561 | |
| 562 | switch message.Type { |
| 563 | case OltIndication: |
| 564 | msg, _ := message.Data.(OltIndicationMessage) |
| 565 | if msg.OperState == UP { |
| 566 | o.InternalState.Event("enable") |
| 567 | o.OperState.Event("enable") |
| 568 | } else if msg.OperState == DOWN { |
| 569 | o.InternalState.Event("disable") |
| 570 | o.OperState.Event("disable") |
| 571 | } |
| 572 | o.sendOltIndication(msg, stream) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 573 | case AlarmIndication: |
| 574 | alarmInd, _ := message.Data.(*openolt.AlarmIndication) |
| 575 | o.sendAlarmIndication(alarmInd, stream) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 576 | case NniIndication: |
| 577 | msg, _ := message.Data.(NniIndicationMessage) |
| 578 | o.sendNniIndication(msg, stream) |
| 579 | case PonIndication: |
| 580 | msg, _ := message.Data.(PonIndicationMessage) |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 581 | pon, _ := o.GetPonById(msg.PonPortID) |
| 582 | if msg.OperState == UP { |
| 583 | pon.OperState.Event("enable") |
| 584 | pon.InternalState.Event("enable") |
| 585 | } else if msg.OperState == DOWN { |
| 586 | pon.OperState.Event("disable") |
| 587 | pon.InternalState.Event("disable") |
| 588 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 589 | default: |
| 590 | oltLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type) |
| 591 | } |
| 592 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 593 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 594 | wg.Done() |
| 595 | oltLogger.Warn("Stopped handling OLT Indication Channel") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 598 | // processNniPacketIns handles messages received over the NNI interface |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 599 | func (o *OltDevice) processNniPacketIns(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 600 | oltLogger.WithFields(log.Fields{ |
| 601 | "nniChannel": o.nniPktInChannel, |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 602 | }).Debug("Started Processing Packets arriving from the NNI") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 603 | nniId := o.Nnis[0].ID // FIXME we are assuming we have only one NNI |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 604 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 605 | ch := o.nniPktInChannel |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 606 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 607 | loop: |
| 608 | for { |
| 609 | select { |
| 610 | case <-ctx.Done(): |
| 611 | oltLogger.Debug("NNI Indication processing canceled via context") |
| 612 | break loop |
| 613 | case message, ok := <-ch: |
| 614 | if !ok || ctx.Err() != nil { |
| 615 | oltLogger.Debug("NNI Indication processing canceled via channel closed") |
| 616 | break loop |
| 617 | } |
| 618 | oltLogger.Tracef("Received packets on NNI Channel") |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 619 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 620 | onuMac, err := packetHandlers.GetDstMacAddressFromPacket(message.Pkt) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 621 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 622 | if err != nil { |
| 623 | log.WithFields(log.Fields{ |
| 624 | "IntfType": "nni", |
| 625 | "IntfId": nniId, |
| 626 | "Pkt": message.Pkt.Data(), |
| 627 | }).Error("Can't find Dst MacAddress in packet") |
| 628 | return |
| 629 | } |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 630 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 631 | onu, err := o.FindOnuByMacAddress(onuMac) |
| 632 | if err != nil { |
| 633 | log.WithFields(log.Fields{ |
| 634 | "IntfType": "nni", |
| 635 | "IntfId": nniId, |
| 636 | "Pkt": message.Pkt.Data(), |
| 637 | "MacAddress": onuMac.String(), |
| 638 | }).Error("Can't find ONU with MacAddress") |
| 639 | return |
| 640 | } |
| 641 | |
| 642 | doubleTaggedPkt, err := packetHandlers.PushDoubleTag(onu.STag, onu.CTag, message.Pkt) |
| 643 | if err != nil { |
| 644 | log.Error("Fail to add double tag to packet") |
| 645 | } |
| 646 | |
| 647 | data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{ |
| 648 | IntfType: "nni", |
| 649 | IntfId: nniId, |
| 650 | Pkt: doubleTaggedPkt.Data()}} |
| 651 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 652 | oltLogger.WithFields(log.Fields{ |
| 653 | "IntfType": data.PktInd.IntfType, |
| 654 | "IntfId": nniId, |
| 655 | "Pkt": doubleTaggedPkt.Data(), |
| 656 | }).Errorf("Fail to send PktInd indication: %v", err) |
| 657 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 658 | oltLogger.WithFields(log.Fields{ |
| 659 | "IntfType": data.PktInd.IntfType, |
| 660 | "IntfId": nniId, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 661 | "Pkt": doubleTaggedPkt.Data(), |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 662 | "OnuSn": onu.Sn(), |
| 663 | }).Tracef("Sent PktInd indication") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 666 | wg.Done() |
| 667 | oltLogger.WithFields(log.Fields{ |
| 668 | "nniChannel": o.nniPktInChannel, |
| 669 | }).Warn("Stopped handling NNI Channel") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 672 | // returns an ONU with a given Serial Number |
| 673 | func (o OltDevice) FindOnuBySn(serialNumber string) (*Onu, error) { |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 674 | // TODO this function can be a performance bottleneck when we have many ONUs, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 675 | // memoizing it will remove the bottleneck |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 676 | for _, pon := range o.Pons { |
| 677 | for _, onu := range pon.Onus { |
| 678 | if onu.Sn() == serialNumber { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 679 | return onu, nil |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 684 | return &Onu{}, errors.New(fmt.Sprintf("cannot-find-onu-by-serial-number-%s", serialNumber)) |
| 685 | } |
| 686 | |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 687 | // returns an ONU with a given interface/Onu Id |
| 688 | func (o OltDevice) FindOnuById(intfId uint32, onuId uint32) (*Onu, error) { |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 689 | // TODO this function can be a performance bottleneck when we have many ONUs, |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 690 | // memoizing it will remove the bottleneck |
| 691 | for _, pon := range o.Pons { |
| 692 | if pon.ID == intfId { |
| 693 | for _, onu := range pon.Onus { |
| 694 | if onu.ID == onuId { |
| 695 | return onu, nil |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | return &Onu{}, errors.New(fmt.Sprintf("cannot-find-onu-by-id-%v-%v", intfId, onuId)) |
| 701 | } |
| 702 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 703 | // returns an ONU with a given Mac Address |
| 704 | func (o OltDevice) FindOnuByMacAddress(mac net.HardwareAddr) (*Onu, error) { |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 705 | // TODO this function can be a performance bottleneck when we have many ONUs, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 706 | // memoizing it will remove the bottleneck |
| 707 | for _, pon := range o.Pons { |
| 708 | for _, onu := range pon.Onus { |
| 709 | if onu.HwAddress.String() == mac.String() { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 710 | return onu, nil |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | return &Onu{}, errors.New(fmt.Sprintf("cannot-find-onu-by-mac-address-%s", mac)) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 718 | // GRPC Endpoints |
| 719 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 720 | func (o OltDevice) ActivateOnu(context context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 721 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 722 | "OnuSn": onuSnToString(onu.SerialNumber), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 723 | }).Info("Received ActivateOnu call from VOLTHA") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 724 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 725 | pon, _ := o.GetPonById(onu.IntfId) |
| 726 | _onu, _ := pon.GetOnuBySn(onu.SerialNumber) |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 727 | _onu.SetID(onu.OnuId) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 728 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 729 | if err := _onu.OperState.Event("enable"); err != nil { |
| 730 | oltLogger.WithFields(log.Fields{ |
| 731 | "IntfId": _onu.PonPortID, |
| 732 | "OnuSn": _onu.Sn(), |
| 733 | "OnuId": _onu.ID, |
| 734 | }).Infof("Failed to transition ONU.OperState to enabled state: %s", err.Error()) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 735 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 736 | if err := _onu.InternalState.Event("enable"); err != nil { |
| 737 | oltLogger.WithFields(log.Fields{ |
| 738 | "IntfId": _onu.PonPortID, |
| 739 | "OnuSn": _onu.Sn(), |
| 740 | "OnuId": _onu.ID, |
| 741 | }).Infof("Failed to transition ONU to enabled state: %s", err.Error()) |
| 742 | } |
| 743 | |
| 744 | // NOTE we need to immediately activate the ONU or the OMCI state machine won't start |
| 745 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 746 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 749 | func (o OltDevice) DeactivateOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 750 | oltLogger.Error("DeactivateOnu not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 751 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 754 | func (o OltDevice) DeleteOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
| 755 | oltLogger.WithFields(log.Fields{ |
| 756 | "IntfId": onu.IntfId, |
| 757 | "OnuId": onu.OnuId, |
| 758 | }).Info("Received DeleteOnu call from VOLTHA") |
| 759 | |
| 760 | pon, err := o.GetPonById(onu.IntfId) |
| 761 | if err != nil { |
| 762 | oltLogger.WithFields(log.Fields{ |
| 763 | "OnuId": onu.OnuId, |
| 764 | "IntfId": onu.IntfId, |
| 765 | "err": err, |
| 766 | }).Error("Can't find PonPort") |
| 767 | } |
| 768 | _onu, err := pon.GetOnuById(onu.OnuId) |
| 769 | if err != nil { |
| 770 | oltLogger.WithFields(log.Fields{ |
| 771 | "OnuId": onu.OnuId, |
| 772 | "IntfId": onu.IntfId, |
| 773 | "err": err, |
| 774 | }).Error("Can't find Onu") |
| 775 | } |
| 776 | |
| 777 | if err := _onu.InternalState.Event("initialize"); err != nil { |
| 778 | oltLogger.WithFields(log.Fields{ |
| 779 | "IntfId": _onu.PonPortID, |
| 780 | "OnuSn": _onu.Sn(), |
| 781 | "OnuId": _onu.ID, |
| 782 | }).Infof("Failed to transition ONU to initialized state: %s", err.Error()) |
| 783 | } |
| 784 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 785 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 788 | func (o OltDevice) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 789 | // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether? |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 790 | oltLogger.WithFields(log.Fields{ |
| 791 | "oltId": o.ID, |
| 792 | }).Info("Disabling OLT") |
| 793 | |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 794 | for _, pon := range o.Pons { |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 795 | if pon.InternalState.Current() == "enabled" { |
| 796 | // disable PONs |
| 797 | msg := Message{ |
| 798 | Type: PonIndication, |
| 799 | Data: PonIndicationMessage{ |
| 800 | OperState: DOWN, |
| 801 | PonPortID: pon.ID, |
| 802 | }, |
| 803 | } |
| 804 | o.channel <- msg |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 805 | } |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 808 | // Note that we are not disabling the NNI as the real OLT does not. |
| 809 | // The reason for that is in-band management |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 810 | |
| 811 | // disable OLT |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 812 | oltMsg := Message{ |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 813 | Type: OltIndication, |
| 814 | Data: OltIndicationMessage{ |
| 815 | OperState: DOWN, |
| 816 | }, |
| 817 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 818 | o.channel <- oltMsg |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 819 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 822 | func (o OltDevice) DisablePonIf(context.Context, *openolt.Interface) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 823 | oltLogger.Error("DisablePonIf not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 824 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 827 | func (o *OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 828 | oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 829 | o.Enable(stream) |
| 830 | return nil |
| 831 | } |
| 832 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 833 | func (o OltDevice) EnablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) { |
| 834 | oltLogger.Errorf("EnablePonIf request received for PON %d", intf.IntfId) |
| 835 | ponID := intf.GetIntfId() |
| 836 | msg := Message{ |
| 837 | Type: PonIndication, |
| 838 | Data: PonIndicationMessage{ |
| 839 | OperState: UP, |
| 840 | PonPortID: ponID, |
| 841 | }, |
| 842 | } |
| 843 | o.channel <- msg |
| 844 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 845 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 848 | func (o OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 849 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 850 | "IntfId": flow.AccessIntfId, |
| 851 | "OnuId": flow.OnuId, |
| 852 | "EthType": fmt.Sprintf("%x", flow.Classifier.EthType), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 853 | "InnerVlan": flow.Classifier.IVid, |
| 854 | "OuterVlan": flow.Classifier.OVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 855 | "FlowType": flow.FlowType, |
| 856 | "FlowId": flow.FlowId, |
| 857 | "UniID": flow.UniId, |
| 858 | "PortNo": flow.PortNo, |
| 859 | }).Tracef("OLT receives Flow") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 860 | // TODO optionally store flows somewhere |
| 861 | |
| 862 | if flow.AccessIntfId == -1 { |
| 863 | oltLogger.WithFields(log.Fields{ |
| 864 | "FlowId": flow.FlowId, |
| 865 | }).Debugf("This is an OLT flow") |
| 866 | } else { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 867 | pon, err := o.GetPonById(uint32(flow.AccessIntfId)) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 868 | if err != nil { |
| 869 | oltLogger.WithFields(log.Fields{ |
| 870 | "OnuId": flow.OnuId, |
| 871 | "IntfId": flow.AccessIntfId, |
| 872 | "err": err, |
| 873 | }).Error("Can't find PonPort") |
| 874 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 875 | onu, err := pon.GetOnuById(uint32(flow.OnuId)) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 876 | if err != nil { |
| 877 | oltLogger.WithFields(log.Fields{ |
| 878 | "OnuId": flow.OnuId, |
| 879 | "IntfId": flow.AccessIntfId, |
| 880 | "err": err, |
| 881 | }).Error("Can't find Onu") |
| 882 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 883 | |
| 884 | msg := Message{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 885 | Type: FlowUpdate, |
| 886 | Data: OnuFlowUpdateMessage{ |
| 887 | PonPortID: pon.ID, |
| 888 | OnuID: onu.ID, |
| 889 | Flow: flow, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 890 | }, |
| 891 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 892 | onu.Channel <- msg |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 895 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 898 | func (o OltDevice) FlowRemove(context.Context, *openolt.Flow) (*openolt.Empty, error) { |
| 899 | oltLogger.Tracef("received FlowRemove") |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 900 | // TODO store flows somewhere |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 901 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 904 | func (o OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) { |
Matteo Scandolo | 1885985 | 2020-01-15 13:33:57 -0800 | [diff] [blame] | 905 | res := openolt.Heartbeat{HeartbeatSignature: uint32(time.Now().Unix())} |
| 906 | oltLogger.WithFields(log.Fields{ |
| 907 | "signature": res.HeartbeatSignature, |
| 908 | }).Trace("HeartbeatCheck") |
| 909 | return &res, nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 912 | func (o OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 913 | |
Matteo Scandolo | da9cbe2 | 2019-08-19 16:05:10 -0700 | [diff] [blame] | 914 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 915 | "oltId": o.ID, |
Matteo Scandolo | da9cbe2 | 2019-08-19 16:05:10 -0700 | [diff] [blame] | 916 | "PonPorts": o.NumPon, |
| 917 | }).Info("OLT receives GetDeviceInfo call from VOLTHA") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 918 | devinfo := new(openolt.DeviceInfo) |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 919 | devinfo.Vendor = common.Options.Olt.Vendor |
| 920 | devinfo.Model = common.Options.Olt.Model |
| 921 | devinfo.HardwareVersion = common.Options.Olt.HardwareVersion |
| 922 | devinfo.FirmwareVersion = common.Options.Olt.FirmwareVersion |
| 923 | devinfo.Technology = common.Options.Olt.Technology |
Matteo Scandolo | da9cbe2 | 2019-08-19 16:05:10 -0700 | [diff] [blame] | 924 | devinfo.PonPorts = uint32(o.NumPon) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 925 | devinfo.OnuIdStart = 1 |
| 926 | devinfo.OnuIdEnd = 255 |
| 927 | devinfo.AllocIdStart = 1024 |
| 928 | devinfo.AllocIdEnd = 16383 |
| 929 | devinfo.GemportIdStart = 1024 |
| 930 | devinfo.GemportIdEnd = 65535 |
| 931 | devinfo.FlowIdStart = 1 |
| 932 | devinfo.FlowIdEnd = 16383 |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 933 | devinfo.DeviceSerialNumber = o.SerialNumber |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 934 | devinfo.DeviceId = common.Options.Olt.DeviceId |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 935 | |
| 936 | return devinfo, nil |
| 937 | } |
| 938 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 939 | func (o OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 940 | pon, _ := o.GetPonById(omci_msg.IntfId) |
| 941 | onu, _ := pon.GetOnuById(omci_msg.OnuId) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 942 | oltLogger.WithFields(log.Fields{ |
| 943 | "IntfId": onu.PonPortID, |
| 944 | "OnuId": onu.ID, |
| 945 | "OnuSn": onu.Sn(), |
| 946 | }).Tracef("Received OmciMsgOut") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 947 | msg := Message{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 948 | Type: OMCI, |
| 949 | Data: OmciMessage{ |
| 950 | OnuSN: onu.SerialNumber, |
| 951 | OnuID: onu.ID, |
| 952 | omciMsg: omci_msg, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 953 | }, |
| 954 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 955 | onu.Channel <- msg |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 956 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 959 | func (o OltDevice) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 960 | pon, err := o.GetPonById(onuPkt.IntfId) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 961 | if err != nil { |
| 962 | oltLogger.WithFields(log.Fields{ |
| 963 | "OnuId": onuPkt.OnuId, |
| 964 | "IntfId": onuPkt.IntfId, |
| 965 | "err": err, |
| 966 | }).Error("Can't find PonPort") |
| 967 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 968 | onu, err := pon.GetOnuById(onuPkt.OnuId) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 969 | if err != nil { |
| 970 | oltLogger.WithFields(log.Fields{ |
| 971 | "OnuId": onuPkt.OnuId, |
| 972 | "IntfId": onuPkt.IntfId, |
| 973 | "err": err, |
| 974 | }).Error("Can't find Onu") |
| 975 | } |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 976 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 977 | oltLogger.WithFields(log.Fields{ |
| 978 | "IntfId": onu.PonPortID, |
| 979 | "OnuId": onu.ID, |
| 980 | "OnuSn": onu.Sn(), |
| 981 | }).Tracef("Received OnuPacketOut") |
| 982 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 983 | rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 984 | pktType, err := packetHandlers.IsEapolOrDhcp(rawpkt) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 985 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 986 | msg := Message{ |
| 987 | Type: OnuPacketOut, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 988 | Data: OnuPacketMessage{ |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 989 | IntfId: onuPkt.IntfId, |
| 990 | OnuId: onuPkt.OnuId, |
| 991 | Packet: rawpkt, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 992 | Type: pktType, |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 993 | }, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 994 | } |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 995 | onu.Channel <- msg |
| 996 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 997 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1000 | func (o OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 1001 | oltLogger.WithFields(log.Fields{ |
| 1002 | "oltId": o.ID, |
| 1003 | }).Info("Shutting down") |
| 1004 | go o.RestartOLT() |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1005 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | func (o OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 1009 | oltLogger.WithFields(log.Fields{ |
| 1010 | "oltId": o.ID, |
| 1011 | }).Info("Received ReenableOlt request from VOLTHA") |
| 1012 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 1013 | // enable OLT |
| 1014 | oltMsg := Message{ |
| 1015 | Type: OltIndication, |
| 1016 | Data: OltIndicationMessage{ |
| 1017 | OperState: UP, |
| 1018 | }, |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 1019 | } |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 1020 | o.channel <- oltMsg |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 1021 | |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame^] | 1022 | for _, pon := range o.Pons { |
| 1023 | if pon.InternalState.Current() == "disabled" { |
| 1024 | msg := Message{ |
| 1025 | Type: PonIndication, |
| 1026 | Data: PonIndicationMessage{ |
| 1027 | OperState: UP, |
| 1028 | PonPortID: pon.ID, |
| 1029 | }, |
| 1030 | } |
| 1031 | o.channel <- msg |
| 1032 | } |
| 1033 | } |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 1034 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1035 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | func (o OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1039 | pkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 1040 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 1041 | o.Nnis[0].sendNniPacket(pkt) // FIXME we are assuming we have only one NNI |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1042 | // NOTE should we return an error if sendNniPakcet fails? |
| 1043 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1046 | func (o OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 1047 | oltLogger.Error("CollectStatistics not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1048 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1051 | func (o OltDevice) GetOnuInfo(context context.Context, packet *openolt.Onu) (*openolt.OnuIndication, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 1052 | oltLogger.Error("GetOnuInfo not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1053 | return new(openolt.OnuIndication), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | func (o OltDevice) GetPonIf(context context.Context, packet *openolt.Interface) (*openolt.IntfIndication, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 1057 | oltLogger.Error("GetPonIf not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1058 | return new(openolt.IntfIndication), nil |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | func (s OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 1062 | oltLogger.Info("received CreateTrafficQueues") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1063 | return new(openolt.Empty), nil |
| 1064 | } |
| 1065 | |
| 1066 | func (s OltDevice) RemoveTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 1067 | oltLogger.Info("received RemoveTrafficQueues") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1068 | return new(openolt.Empty), nil |
| 1069 | } |
| 1070 | |
| 1071 | func (s OltDevice) CreateTrafficSchedulers(context.Context, *tech_profile.TrafficSchedulers) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 1072 | oltLogger.Info("received CreateTrafficSchedulers") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1073 | return new(openolt.Empty), nil |
| 1074 | } |
| 1075 | |
| 1076 | func (s OltDevice) RemoveTrafficSchedulers(context.Context, *tech_profile.TrafficSchedulers) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 1077 | oltLogger.Info("received RemoveTrafficSchedulers") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1078 | return new(openolt.Empty), nil |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1079 | } |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 1080 | |
| 1081 | // assumes caller has properly formulated an openolt.AlarmIndication |
| 1082 | func (o OltDevice) SendAlarmIndication(context context.Context, ind *openolt.AlarmIndication) error { |
| 1083 | msg := Message{ |
| 1084 | Type: AlarmIndication, |
| 1085 | Data: ind, |
| 1086 | } |
| 1087 | |
| 1088 | o.channel <- msg |
| 1089 | return nil |
| 1090 | } |