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