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