blob: c883e952bb84b7a5bcb9620e1dde70c14ae644ff [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
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 Scandolo4747d292019-08-05 11:50:18 -070017package devices
18
19import (
20 "context"
Matteo Scandolo4a036262020-08-17 15:56:13 -070021 "encoding/hex"
Matteo Scandolo4747d292019-08-05 11:50:18 -070022 "fmt"
Matteo Scandolo618a6582020-09-09 12:21:29 -070023 "github.com/opencord/voltha-protos/v3/go/ext/config"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020024 "net"
25 "sync"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010026 "time"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020027
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070028 "github.com/google/gopacket"
29 "github.com/google/gopacket/layers"
Matteo Scandolodf3f85d2020-01-15 12:50:48 -080030 "github.com/google/gopacket/pcap"
Matteo Scandolo4747d292019-08-05 11:50:18 -070031 "github.com/looplab/fsm"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070032 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070033 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010034 "github.com/opencord/bbsim/internal/common"
William Kurkian9dadc5b2019-10-22 13:51:57 -040035 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo618a6582020-09-09 12:21:29 -070036 common_protos "github.com/opencord/voltha-protos/v3/go/common"
37 "github.com/opencord/voltha-protos/v3/go/openolt"
38 "github.com/opencord/voltha-protos/v3/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070039 log "github.com/sirupsen/logrus"
40 "google.golang.org/grpc"
Pragya Arya8bdb4532020-03-02 17:08:09 +053041 "google.golang.org/grpc/codes"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010042 "google.golang.org/grpc/reflection"
Pragya Arya8bdb4532020-03-02 17:08:09 +053043 "google.golang.org/grpc/status"
Matteo Scandolo4747d292019-08-05 11:50:18 -070044)
45
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070046var oltLogger = log.WithFields(log.Fields{
Matteo Scandolo84f7d482019-08-08 19:00:47 -070047 "module": "OLT",
48})
49
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070050type OltDevice struct {
David Bainbridge103cf022019-12-16 20:11:35 +000051 sync.Mutex
52
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070053 // BBSIM Internals
Pragya Arya2225f202020-01-29 18:05:01 +053054 ID int
55 SerialNumber string
56 NumNni int
57 NumPon int
58 NumOnuPerPon int
59 InternalState *fsm.FSM
60 channel chan Message
61 nniPktInChannel chan *bbsim.PacketMsg // packets coming in from the NNI and going to VOLTHA
62 nniHandle *pcap.Handle // handle on the NNI interface, close it when shutting down the NNI channel
Pragya Arya8bdb4532020-03-02 17:08:09 +053063 Flows map[FlowKey]openolt.Flow
Pragya Arya2225f202020-01-29 18:05:01 +053064 Delay int
65 ControlledActivation mode
Pragya Arya324337e2020-02-20 14:35:08 +053066 EventChannel chan common.Event
67 PublishEvents bool
Pragya Arya996a0892020-03-09 21:47:52 +053068 PortStatsInterval int
Matteo Scandoloe33447a2019-10-31 12:38:23 -070069
Matteo Scandolo27428702019-10-11 16:21:16 -070070 Pons []*PonPort
71 Nnis []*NniPort
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070072
73 // OLT Attributes
74 OperState *fsm.FSM
David Bainbridge103cf022019-12-16 20:11:35 +000075
76 enableContext context.Context
77 enableContextCancel context.CancelFunc
Pragya Arya1cbefa42020-01-13 12:15:29 +053078
Matteo Scandolo4a036262020-08-17 15:56:13 -070079 OpenoltStream openolt.Openolt_EnableIndicationServer
Anand S Katti09541352020-01-29 15:54:01 +053080 enablePerf bool
Matteo Scandolo4747d292019-08-05 11:50:18 -070081}
82
Matteo Scandolo27428702019-10-11 16:21:16 -070083var olt OltDevice
Zdravko Bozakov681364d2019-11-10 14:28:46 +010084var oltServer *grpc.Server
Matteo Scandolo84f7d482019-08-08 19:00:47 -070085
Matteo Scandolo27428702019-10-11 16:21:16 -070086func GetOLT() *OltDevice {
87 return &olt
Matteo Scandolo84f7d482019-08-08 19:00:47 -070088}
89
Matteo Scandolo4a036262020-08-17 15:56:13 -070090func CreateOLT(options common.GlobalConfig, services []common.ServiceYaml, isMock bool) *OltDevice {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070091 oltLogger.WithFields(log.Fields{
Pragya Arya996a0892020-03-09 21:47:52 +053092 "ID": options.Olt.ID,
93 "NumNni": options.Olt.NniPorts,
94 "NumPon": options.Olt.PonPorts,
95 "NumOnuPerPon": options.Olt.OnusPonPort,
Matteo Scandolo4747d292019-08-05 11:50:18 -070096 }).Debug("CreateOLT")
97
Matteo Scandolo84f7d482019-08-08 19:00:47 -070098 olt = OltDevice{
Pragya Arya996a0892020-03-09 21:47:52 +053099 ID: options.Olt.ID,
100 SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", options.Olt.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700101 OperState: getOperStateFSM(func(e *fsm.Event) {
102 oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst)
103 }),
Pragya Arya996a0892020-03-09 21:47:52 +0530104 NumNni: int(options.Olt.NniPorts),
105 NumPon: int(options.Olt.PonPorts),
106 NumOnuPerPon: int(options.Olt.OnusPonPort),
107 Pons: []*PonPort{},
108 Nnis: []*NniPort{},
109 Delay: options.BBSim.Delay,
110 Flows: make(map[FlowKey]openolt.Flow),
111 enablePerf: options.BBSim.EnablePerf,
112 PublishEvents: options.BBSim.Events,
113 PortStatsInterval: options.Olt.PortStatsInterval,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700114 }
115
Pragya Arya996a0892020-03-09 21:47:52 +0530116 if val, ok := ControlledActivationModes[options.BBSim.ControlledActivation]; ok {
Pragya Arya2225f202020-01-29 18:05:01 +0530117 olt.ControlledActivation = val
118 } else {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700119 // FIXME throw an error if the ControlledActivation is not valid
Pragya Arya2225f202020-01-29 18:05:01 +0530120 oltLogger.Warn("Unknown ControlledActivation Mode given, running in Default mode")
121 olt.ControlledActivation = Default
122 }
123
Matteo Scandolo4747d292019-08-05 11:50:18 -0700124 // OLT State machine
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700125 // NOTE do we need 2 state machines for the OLT? (InternalState and OperState)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700126 olt.InternalState = fsm.NewFSM(
127 "created",
128 fsm.Events{
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800129 {Name: "initialize", Src: []string{"created", "deleted"}, Dst: "initialized"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100130 {Name: "enable", Src: []string{"initialized", "disabled"}, Dst: "enabled"},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700131 {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"},
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700132 // delete event in enabled state below is for reboot OLT case.
Mahir Gunyel6dad4452020-01-06 12:59:04 -0800133 {Name: "delete", Src: []string{"disabled", "enabled"}, Dst: "deleted"},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700134 },
135 fsm.Callbacks{
136 "enter_state": func(e *fsm.Event) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700137 oltLogger.Debugf("Changing OLT InternalState from %s to %s", e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700138 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100139 "enter_initialized": func(e *fsm.Event) { olt.InitOlt() },
Matteo Scandolo4747d292019-08-05 11:50:18 -0700140 },
141 )
142
Shrey Baid688b4242020-07-10 20:40:10 +0530143 if !isMock {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700144 // create NNI Port
145 nniPort, err := CreateNNI(&olt)
146 if err != nil {
147 oltLogger.Fatalf("Couldn't create NNI Port: %v", err)
148 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700149
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700150 olt.Nnis = append(olt.Nnis, &nniPort)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700151 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700152
Matteo Scandolo4a036262020-08-17 15:56:13 -0700153 // Create device and Services
154
155 nextCtag := map[string]int{}
156 nextStag := map[string]int{}
157
Matteo Scandolo4747d292019-08-05 11:50:18 -0700158 // create PON ports
Matteo Scandolo4a036262020-08-17 15:56:13 -0700159 for i := 0; i < olt.NumPon; i++ {
160 p := CreatePonPort(&olt, uint32(i))
Matteo Scandolo4747d292019-08-05 11:50:18 -0700161
Matteo Scandolo4a036262020-08-17 15:56:13 -0700162 // create ONU devices
163 for j := 0; j < olt.NumOnuPerPon; j++ {
164 delay := time.Duration(olt.Delay*j) * time.Millisecond
165 o := CreateONU(&olt, p, uint32(j+1), delay, isMock)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700166
Matteo Scandolo4a036262020-08-17 15:56:13 -0700167 for k, s := range common.Services {
168
169 // find the correct cTag for this service
170 if _, ok := nextCtag[s.Name]; !ok {
171 // it's the first time we iterate over this service,
172 // so we start from the config value
173 nextCtag[s.Name] = s.CTag
174 } else {
175 // we have a previous value, so we check it
176 // if Allocation is unique, we increment,
177 // otherwise (shared) we do nothing
178 if s.CTagAllocation == common.TagAllocationUnique.String() {
179 nextCtag[s.Name] = nextCtag[s.Name] + 1
180 }
181 }
182
183 // find the correct sTag for this service
184 if _, ok := nextStag[s.Name]; !ok {
185 nextStag[s.Name] = s.STag
186 } else {
187 if s.STagAllocation == common.TagAllocationUnique.String() {
188 nextStag[s.Name] = nextStag[s.Name] + 1
189 }
190 }
191
192 mac := net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(p.ID), byte(o.ID), byte(k)}
193 service, err := NewService(s.Name, mac, o, nextCtag[s.Name], nextStag[s.Name],
194 s.NeedsEapol, s.NeedsDchp, s.NeedsIgmp, s.TechnologyProfileID, s.UniTagMatch,
195 s.ConfigureMacAddress, s.UsPonCTagPriority, s.UsPonSTagPriority, s.DsPonCTagPriority, s.DsPonSTagPriority)
196
197 if err != nil {
198 oltLogger.WithFields(log.Fields{
199 "Err": err.Error(),
200 }).Fatal("Can't create Service")
201 }
202
203 o.Services = append(o.Services, service)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700204 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700205 p.Onus = append(p.Onus, o)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700206 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700207 olt.Pons = append(olt.Pons, p)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700208 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100209
Shrey Baid688b4242020-07-10 20:40:10 +0530210 if !isMock {
Matteo Scandolod32c3822019-11-26 15:57:46 -0700211 if err := olt.InternalState.Event("initialize"); err != nil {
212 log.Errorf("Error initializing OLT: %v", err)
213 return nil
214 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100215 }
216
Pragya Arya324337e2020-02-20 14:35:08 +0530217 if olt.PublishEvents {
218 log.Debugf("BBSim event publishing is enabled")
219 // Create a channel to write event messages
220 olt.EventChannel = make(chan common.Event, 100)
221 }
222
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700223 return &olt
224}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700225
Shrey Baid688b4242020-07-10 20:40:10 +0530226func (o *OltDevice) InitOlt() {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100227
228 if oltServer == nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800229 oltServer, _ = o.newOltServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100230 } else {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800231 // FIXME there should never be a server running if we are initializing the OLT
232 oltLogger.Fatal("OLT server already running.")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100233 }
234
235 // create new channel for processOltMessages Go routine
236 o.channel = make(chan Message)
237
238 o.nniPktInChannel = make(chan *bbsim.PacketMsg, 1024)
239 // FIXME we are assuming we have only one NNI
240 if o.Nnis[0] != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800241 // NOTE we want to make sure the state is down when we initialize the OLT,
242 // the NNI may be in a bad state after a disable/reboot as we are not disabling it for
243 // in-band management
244 o.Nnis[0].OperState.SetState("down")
245 ch, handle, err := o.Nnis[0].NewVethChan()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100246 if err == nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800247 oltLogger.WithFields(log.Fields{
248 "Type": o.Nnis[0].Type,
249 "IntfId": o.Nnis[0].ID,
250 "OperState": o.Nnis[0].OperState.Current(),
251 }).Info("NNI Channel created")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100252 o.nniPktInChannel = ch
Matteo Scandolo401503a2019-12-11 14:48:14 -0800253 o.nniHandle = handle
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100254 } else {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800255 oltLogger.Errorf("Error getting NNI channel: %v", err)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100256 }
257 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700258}
259
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800260func (o *OltDevice) RestartOLT() error {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100261
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700262 softReboot := false
Matteo Scandolo4a036262020-08-17 15:56:13 -0700263 rebootDelay := common.Config.Olt.OltRebootDelay
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800264
265 oltLogger.WithFields(log.Fields{
266 "oltId": o.ID,
267 }).Infof("Simulating OLT restart... (%ds)", rebootDelay)
268
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700269 if o.InternalState.Is("enabled") {
270 oltLogger.WithFields(log.Fields{
271 "oltId": o.ID,
272 }).Info("This is an OLT soft reboot")
273 softReboot = true
274 }
275
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800276 // transition internal state to deleted
277 if err := o.InternalState.Event("delete"); err != nil {
278 oltLogger.WithFields(log.Fields{
279 "oltId": o.ID,
280 }).Errorf("Error deleting OLT: %v", err)
281 return err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100282 }
283
284 // TODO handle hard poweroff (i.e. no indications sent to Voltha) vs soft poweroff
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800285 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
286 if err := o.StopOltServer(); err != nil {
Pragya Arya2225f202020-01-29 18:05:01 +0530287 oltLogger.Errorf("Error in stopping OLT server")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100288 return err
289 }
290
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700291 if softReboot {
292 for _, pon := range o.Pons {
293 if pon.InternalState.Current() == "enabled" {
294 // disable PONs
295 msg := Message{
296 Type: PonIndication,
297 Data: PonIndicationMessage{
298 OperState: DOWN,
299 PonPortID: pon.ID,
300 },
301 }
302 o.channel <- msg
303 }
304
305 for _, onu := range pon.Onus {
306 _ = onu.InternalState.Event("disable")
307 }
308 }
309 } else {
310 // PONs are already handled in the Disable call
311 for _, pon := range olt.Pons {
312 // ONUs are not automatically disabled when a PON goes down
313 // as it's possible that it's an admin down and in that case the ONUs need to keep their state
314 for _, onu := range pon.Onus {
315 _ = onu.InternalState.Event("disable")
316 }
Pragya Arya2225f202020-01-29 18:05:01 +0530317 }
318 }
319
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100320 // terminate the OLT's processOltMessages go routine
321 close(o.channel)
322 // terminate the OLT's processNniPacketIns go routine
Andy Bavier421f4d52020-01-21 15:39:22 -0700323 go o.nniHandle.Close()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100324 close(o.nniPktInChannel)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100325
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100326 time.Sleep(time.Duration(rebootDelay) * time.Second)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100327
328 if err := o.InternalState.Event("initialize"); err != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800329 oltLogger.WithFields(log.Fields{
330 "oltId": o.ID,
331 }).Errorf("Error initializing OLT: %v", err)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100332 return err
333 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800334 oltLogger.WithFields(log.Fields{
335 "oltId": o.ID,
336 }).Info("OLT restart completed")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100337 return nil
338}
339
340// newOltServer launches a new grpc server for OpenOLT
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800341func (o *OltDevice) newOltServer() (*grpc.Server, error) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700342 address := common.Config.BBSim.OpenOltAddress
Matteo Scandolo4747d292019-08-05 11:50:18 -0700343 lis, err := net.Listen("tcp", address)
344 if err != nil {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700345 oltLogger.Fatalf("OLT failed to listen: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700346 }
347 grpcServer := grpc.NewServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100348
Matteo Scandolo4747d292019-08-05 11:50:18 -0700349 openolt.RegisterOpenoltServer(grpcServer, o)
350
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100351 reflection.Register(grpcServer)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700352
Shrey Baid688b4242020-07-10 20:40:10 +0530353 go func() { _ = grpcServer.Serve(lis) }()
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100354 oltLogger.Debugf("OLT listening on %v", address)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700355
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100356 return grpcServer, nil
357}
358
359// StopOltServer stops the OpenOLT grpc server
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800360func (o *OltDevice) StopOltServer() error {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100361 // TODO handle poweroff vs graceful shutdown
362 if oltServer != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800363 oltLogger.WithFields(log.Fields{
364 "oltId": o.SerialNumber,
365 }).Warnf("Stopping OLT gRPC server")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100366 oltServer.Stop()
367 oltServer = nil
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700368 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800369
Matteo Scandolo4747d292019-08-05 11:50:18 -0700370 return nil
371}
372
373// Device Methods
374
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100375// Enable implements the OpenOLT EnableIndicationServer functionality
Shrey Baid688b4242020-07-10 20:40:10 +0530376func (o *OltDevice) Enable(stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700377 oltLogger.Debug("Enable OLT called")
Pragya Arya2225f202020-01-29 18:05:01 +0530378 rebootFlag := false
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700379
David Bainbridge103cf022019-12-16 20:11:35 +0000380 // If enabled has already been called then an enabled context has
381 // been created. If this is the case then we want to cancel all the
382 // proessing loops associated with that enable before we recreate
383 // new ones
384 o.Lock()
385 if o.enableContext != nil && o.enableContextCancel != nil {
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700386 oltLogger.Info("This is an OLT reboot")
David Bainbridge103cf022019-12-16 20:11:35 +0000387 o.enableContextCancel()
Pragya Arya2225f202020-01-29 18:05:01 +0530388 rebootFlag = true
David Bainbridge103cf022019-12-16 20:11:35 +0000389 }
390 o.enableContext, o.enableContextCancel = context.WithCancel(context.TODO())
391 o.Unlock()
392
Matteo Scandolo4747d292019-08-05 11:50:18 -0700393 wg := sync.WaitGroup{}
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800394 wg.Add(3)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700395
Matteo Scandolo4a036262020-08-17 15:56:13 -0700396 o.OpenoltStream = stream
Pragya Arya1cbefa42020-01-13 12:15:29 +0530397
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100398 // create Go routine to process all OLT events
David Bainbridge103cf022019-12-16 20:11:35 +0000399 go o.processOltMessages(o.enableContext, stream, &wg)
400 go o.processNniPacketIns(o.enableContext, stream, &wg)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700401
402 // enable the OLT
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100403 oltMsg := Message{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700404 Type: OltIndication,
405 Data: OltIndicationMessage{
406 OperState: UP,
407 },
408 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100409 o.channel <- oltMsg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700410
411 // send NNI Port Indications
412 for _, nni := range o.Nnis {
413 msg := Message{
414 Type: NniIndication,
415 Data: NniIndicationMessage{
416 OperState: UP,
417 NniPortID: nni.ID,
418 },
419 }
420 o.channel <- msg
421 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100422
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800423 go o.processOmciMessages(o.enableContext, stream, &wg)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100424
Shrey Baid688b4242020-07-10 20:40:10 +0530425 if rebootFlag {
Pragya Arya2225f202020-01-29 18:05:01 +0530426 for _, pon := range o.Pons {
427 if pon.InternalState.Current() == "disabled" {
428 msg := Message{
429 Type: PonIndication,
430 Data: PonIndicationMessage{
431 OperState: UP,
432 PonPortID: pon.ID,
433 },
434 }
435 o.channel <- msg
Matteo Scandoloe60a5052020-02-07 00:31:14 +0000436 }
Pragya Arya2225f202020-01-29 18:05:01 +0530437 }
438 } else {
439
440 // 1. controlledActivation == Default: Send both PON and ONUs indications
441 // 2. controlledActivation == only-onu: that means only ONUs will be controlled activated, so auto send PON indications
442
443 if o.ControlledActivation == Default || o.ControlledActivation == OnlyONU {
444 // send PON Port indications
445 for _, pon := range o.Pons {
446 msg := Message{
447 Type: PonIndication,
448 Data: PonIndicationMessage{
449 OperState: UP,
450 PonPortID: pon.ID,
451 },
452 }
453 o.channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700454 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700455 }
456 }
457
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800458 oltLogger.Debug("Enable OLT Done")
Pragya Arya996a0892020-03-09 21:47:52 +0530459
460 if !o.enablePerf {
461 // Start a go routine to send periodic port stats to openolt adapter
462 go o.periodicPortStats(o.enableContext)
463 }
464
Matteo Scandolo4747d292019-08-05 11:50:18 -0700465 wg.Wait()
Matteo Scandolo4747d292019-08-05 11:50:18 -0700466}
467
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800468func (o *OltDevice) processOmciMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400469 ch := omcisim.GetChannel()
470
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100471 oltLogger.Debug("Starting OMCI Indication Channel")
William Kurkian9dadc5b2019-10-22 13:51:57 -0400472
David Bainbridge103cf022019-12-16 20:11:35 +0000473loop:
474 for {
475 select {
476 case <-ctx.Done():
477 oltLogger.Debug("OMCI processing canceled via context")
478 break loop
479 case message, ok := <-ch:
480 if !ok || ctx.Err() != nil {
481 oltLogger.Debug("OMCI processing canceled via channel close")
482 break loop
483 }
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800484
485 oltLogger.WithFields(log.Fields{
486 "messageType": message.Type,
487 "OnuId": message.Data.OnuId,
488 "IntfId": message.Data.IntfId,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700489 }).Debug("Received message on OMCI Sim channel")
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800490
David Bainbridge103cf022019-12-16 20:11:35 +0000491 onuId := message.Data.OnuId
492 intfId := message.Data.IntfId
493 onu, err := o.FindOnuById(intfId, onuId)
494 if err != nil {
495 oltLogger.Errorf("Failed to find onu: %v", err)
496 continue
497 }
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800498 go onu.processOmciMessage(message, stream)
William Kurkian9dadc5b2019-10-22 13:51:57 -0400499 }
William Kurkian9dadc5b2019-10-22 13:51:57 -0400500 }
David Bainbridge103cf022019-12-16 20:11:35 +0000501
502 wg.Done()
William Kurkian9dadc5b2019-10-22 13:51:57 -0400503}
504
Pragya Arya996a0892020-03-09 21:47:52 +0530505func (o *OltDevice) periodicPortStats(ctx context.Context) {
506 var portStats *openolt.PortStatistics
507 for {
508 select {
509 case <-time.After(time.Duration(o.PortStatsInterval) * time.Second):
510 // send NNI port stats
511 for _, port := range o.Nnis {
512 incrementStat := true
513 if port.OperState.Current() == "down" {
514 incrementStat = false
515 }
516 portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat)
517 o.sendPortStatsIndication(portStats, port.ID, port.Type)
518 }
519
520 // send PON port stats
521 for _, port := range o.Pons {
522 incrementStat := true
523 // do not increment port stats if PON port is down or no ONU is activated on PON port
524 if port.OperState.Current() == "down" || port.GetNumOfActiveOnus() < 1 {
525 incrementStat = false
526 }
527 portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat)
528 o.sendPortStatsIndication(portStats, port.ID, port.Type)
529 }
530 case <-ctx.Done():
531 log.Debug("Stop sending port stats")
532 return
533 }
534
535 }
536}
537
Matteo Scandolo4747d292019-08-05 11:50:18 -0700538// Helpers method
539
Shrey Baid688b4242020-07-10 20:40:10 +0530540func (o *OltDevice) GetPonById(id uint32) (*PonPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700541 for _, pon := range o.Pons {
542 if pon.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700543 return pon, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700544 }
545 }
Shrey Baid688b4242020-07-10 20:40:10 +0530546 return nil, fmt.Errorf("Cannot find PonPort with id %d in OLT %d", id, o.ID)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700547}
548
Shrey Baid688b4242020-07-10 20:40:10 +0530549func (o *OltDevice) getNniById(id uint32) (*NniPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700550 for _, nni := range o.Nnis {
551 if nni.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700552 return nni, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700553 }
554 }
Shrey Baid688b4242020-07-10 20:40:10 +0530555 return nil, fmt.Errorf("Cannot find NniPort with id %d in OLT %d", id, o.ID)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700556}
557
Scott Baker41724b82020-01-21 19:54:53 -0800558func (o *OltDevice) sendAlarmIndication(alarmInd *openolt.AlarmIndication, stream openolt.Openolt_EnableIndicationServer) {
559 data := &openolt.Indication_AlarmInd{AlarmInd: alarmInd}
560 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
561 oltLogger.Errorf("Failed to send Alarm Indication: %v", err)
562 return
563 }
564
565 oltLogger.WithFields(log.Fields{
566 "AlarmIndication": alarmInd,
567 }).Debug("Sent Indication_AlarmInd")
568}
569
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100570func (o *OltDevice) sendOltIndication(msg OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700571 data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}}
572 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700573 oltLogger.Errorf("Failed to send Indication_OltInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800574 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700575 }
576
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700577 oltLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700578 "OperState": msg.OperState,
579 }).Debug("Sent Indication_OltInd")
580}
581
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100582func (o *OltDevice) sendNniIndication(msg NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700583 nni, _ := o.getNniById(msg.NniPortID)
Matteo Scandolo401503a2019-12-11 14:48:14 -0800584 if msg.OperState == UP {
585 if err := nni.OperState.Event("enable"); err != nil {
586 log.WithFields(log.Fields{
587 "Type": nni.Type,
588 "IntfId": nni.ID,
589 "OperState": nni.OperState.Current(),
590 }).Errorf("Can't move NNI Port to enabled state: %v", err)
591 }
592 } else if msg.OperState == DOWN {
593 if err := nni.OperState.Event("disable"); err != nil {
594 log.WithFields(log.Fields{
595 "Type": nni.Type,
596 "IntfId": nni.ID,
597 "OperState": nni.OperState.Current(),
598 }).Errorf("Can't move NNI Port to disable state: %v", err)
599 }
600 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700601 // NOTE Operstate may need to be an integer
Matteo Scandolo4747d292019-08-05 11:50:18 -0700602 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700603 Type: nni.Type,
604 IntfId: nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700605 OperState: nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700606 }}
607
608 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700609 oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800610 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700611 }
612
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700613 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700614 "Type": nni.Type,
615 "IntfId": nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700616 "OperState": nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700617 }).Debug("Sent Indication_IntfOperInd for NNI")
618}
619
Pragya Arya2225f202020-01-29 18:05:01 +0530620func (o *OltDevice) sendPonIndication(ponPortID uint32) {
621
Matteo Scandolo4a036262020-08-17 15:56:13 -0700622 stream := o.OpenoltStream
Pragya Arya2225f202020-01-29 18:05:01 +0530623 pon, _ := o.GetPonById(ponPortID)
624 // Send IntfIndication for PON port
Matteo Scandolo4747d292019-08-05 11:50:18 -0700625 discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700626 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700627 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700628 }}
629
630 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700631 oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800632 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700633 }
634
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700635 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700636 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700637 "OperState": pon.OperState.Current(),
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700638 }).Debug("Sent Indication_IntfInd for PON")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700639
Pragya Arya2225f202020-01-29 18:05:01 +0530640 // Send IntfOperIndication for PON port
Matteo Scandolo4747d292019-08-05 11:50:18 -0700641 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700642 Type: pon.Type,
643 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700644 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700645 }}
646
647 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700648 oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800649 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700650 }
651
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700652 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700653 "Type": pon.Type,
654 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700655 "OperState": pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700656 }).Debug("Sent Indication_IntfOperInd for PON")
657}
658
Pragya Arya996a0892020-03-09 21:47:52 +0530659func (o *OltDevice) sendPortStatsIndication(stats *openolt.PortStatistics, portID uint32, portType string) {
Shrey Baid55f328c2020-07-07 19:20:42 +0530660 if o.InternalState.Current() == "enabled" {
661 oltLogger.WithFields(log.Fields{
662 "Type": portType,
663 "IntfId": portID,
664 }).Trace("Sending port stats")
665 stats.IntfId = InterfaceIDToPortNo(portID, portType)
666 data := &openolt.Indication_PortStats{
667 PortStats: stats,
668 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700669 stream := o.OpenoltStream
Shrey Baid55f328c2020-07-07 19:20:42 +0530670 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
671 oltLogger.Errorf("Failed to send PortStats: %v", err)
672 return
673 }
Pragya Arya996a0892020-03-09 21:47:52 +0530674 }
675}
676
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100677// processOltMessages handles messages received over the OpenOLT interface
David Bainbridge103cf022019-12-16 20:11:35 +0000678func (o *OltDevice) processOltMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100679 oltLogger.Debug("Starting OLT Indication Channel")
David Bainbridge103cf022019-12-16 20:11:35 +0000680 ch := o.channel
Matteo Scandolo4747d292019-08-05 11:50:18 -0700681
David Bainbridge103cf022019-12-16 20:11:35 +0000682loop:
683 for {
684 select {
685 case <-ctx.Done():
686 oltLogger.Debug("OLT Indication processing canceled via context")
687 break loop
688 case message, ok := <-ch:
689 if !ok || ctx.Err() != nil {
690 oltLogger.Debug("OLT Indication processing canceled via closed channel")
691 break loop
Matteo Scandolo4747d292019-08-05 11:50:18 -0700692 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700693
David Bainbridge103cf022019-12-16 20:11:35 +0000694 oltLogger.WithFields(log.Fields{
695 "oltId": o.ID,
696 "messageType": message.Type,
697 }).Trace("Received message")
698
699 switch message.Type {
700 case OltIndication:
701 msg, _ := message.Data.(OltIndicationMessage)
702 if msg.OperState == UP {
Shrey Baid688b4242020-07-10 20:40:10 +0530703 _ = o.InternalState.Event("enable")
704 _ = o.OperState.Event("enable")
David Bainbridge103cf022019-12-16 20:11:35 +0000705 } else if msg.OperState == DOWN {
Shrey Baid688b4242020-07-10 20:40:10 +0530706 _ = o.InternalState.Event("disable")
707 _ = o.OperState.Event("disable")
David Bainbridge103cf022019-12-16 20:11:35 +0000708 }
709 o.sendOltIndication(msg, stream)
Scott Baker41724b82020-01-21 19:54:53 -0800710 case AlarmIndication:
711 alarmInd, _ := message.Data.(*openolt.AlarmIndication)
712 o.sendAlarmIndication(alarmInd, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000713 case NniIndication:
714 msg, _ := message.Data.(NniIndicationMessage)
715 o.sendNniIndication(msg, stream)
716 case PonIndication:
717 msg, _ := message.Data.(PonIndicationMessage)
Pragya Arya2225f202020-01-29 18:05:01 +0530718 pon, _ := o.GetPonById(msg.PonPortID)
719 if msg.OperState == UP {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530720 if err := pon.OperState.Event("enable"); err != nil {
721 oltLogger.WithFields(log.Fields{
722 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800723 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530724 }).Error("Can't Enable Oper state for PON Port")
725 }
726 if err := pon.InternalState.Event("enable"); err != nil {
727 oltLogger.WithFields(log.Fields{
728 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800729 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530730 }).Error("Can't Enable Internal state for PON Port")
731 }
Pragya Arya2225f202020-01-29 18:05:01 +0530732 } else if msg.OperState == DOWN {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530733 if err := pon.OperState.Event("disable"); err != nil {
734 oltLogger.WithFields(log.Fields{
735 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800736 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530737 }).Error("Can't Disable Oper state for PON Port")
738 }
739 if err := pon.InternalState.Event("disable"); err != nil {
740 oltLogger.WithFields(log.Fields{
741 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800742 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530743 }).Error("Can't Disable Internal state for PON Port")
744 }
Pragya Arya2225f202020-01-29 18:05:01 +0530745 }
David Bainbridge103cf022019-12-16 20:11:35 +0000746 default:
747 oltLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
748 }
749 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700750 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100751 wg.Done()
752 oltLogger.Warn("Stopped handling OLT Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700753}
754
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100755// processNniPacketIns handles messages received over the NNI interface
David Bainbridge103cf022019-12-16 20:11:35 +0000756func (o *OltDevice) processNniPacketIns(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700757 oltLogger.WithFields(log.Fields{
758 "nniChannel": o.nniPktInChannel,
Matteo Scandolo401503a2019-12-11 14:48:14 -0800759 }).Debug("Started Processing Packets arriving from the NNI")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700760 nniId := o.Nnis[0].ID // FIXME we are assuming we have only one NNI
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700761
David Bainbridge103cf022019-12-16 20:11:35 +0000762 ch := o.nniPktInChannel
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700763
David Bainbridge103cf022019-12-16 20:11:35 +0000764loop:
765 for {
766 select {
767 case <-ctx.Done():
768 oltLogger.Debug("NNI Indication processing canceled via context")
769 break loop
770 case message, ok := <-ch:
771 if !ok || ctx.Err() != nil {
772 oltLogger.Debug("NNI Indication processing canceled via channel closed")
773 break loop
774 }
775 oltLogger.Tracef("Received packets on NNI Channel")
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700776
David Bainbridge103cf022019-12-16 20:11:35 +0000777 onuMac, err := packetHandlers.GetDstMacAddressFromPacket(message.Pkt)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700778
David Bainbridge103cf022019-12-16 20:11:35 +0000779 if err != nil {
780 log.WithFields(log.Fields{
781 "IntfType": "nni",
782 "IntfId": nniId,
783 "Pkt": message.Pkt.Data(),
784 }).Error("Can't find Dst MacAddress in packet")
785 return
786 }
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700787
Matteo Scandolo4a036262020-08-17 15:56:13 -0700788 s, err := o.FindServiceByMacAddress(onuMac)
David Bainbridge103cf022019-12-16 20:11:35 +0000789 if err != nil {
790 log.WithFields(log.Fields{
791 "IntfType": "nni",
792 "IntfId": nniId,
793 "Pkt": message.Pkt.Data(),
794 "MacAddress": onuMac.String(),
795 }).Error("Can't find ONU with MacAddress")
796 return
797 }
798
Matteo Scandolo4a036262020-08-17 15:56:13 -0700799 service := s.(*Service)
800
Matteo Scandolo8d281372020-09-03 16:23:37 -0700801 doubleTaggedPkt, err := packetHandlers.PushDoubleTag(service.STag, service.CTag, message.Pkt, service.UsPonCTagPriority)
David Bainbridge103cf022019-12-16 20:11:35 +0000802 if err != nil {
803 log.Error("Fail to add double tag to packet")
804 }
805
806 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
807 IntfType: "nni",
808 IntfId: nniId,
809 Pkt: doubleTaggedPkt.Data()}}
810 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
811 oltLogger.WithFields(log.Fields{
812 "IntfType": data.PktInd.IntfType,
813 "IntfId": nniId,
814 "Pkt": doubleTaggedPkt.Data(),
815 }).Errorf("Fail to send PktInd indication: %v", err)
816 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700817 oltLogger.WithFields(log.Fields{
818 "IntfType": data.PktInd.IntfType,
819 "IntfId": nniId,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700820 "Pkt": hex.EncodeToString(doubleTaggedPkt.Data()),
821 "OnuSn": service.Onu.Sn(),
822 }).Trace("Sent PktInd indication (from NNI to VOLTHA)")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700823 }
824 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100825 wg.Done()
826 oltLogger.WithFields(log.Fields{
827 "nniChannel": o.nniPktInChannel,
828 }).Warn("Stopped handling NNI Channel")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700829}
830
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700831// returns an ONU with a given Serial Number
Shrey Baid688b4242020-07-10 20:40:10 +0530832func (o *OltDevice) FindOnuBySn(serialNumber string) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200833 // TODO this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700834 // memoizing it will remove the bottleneck
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700835 for _, pon := range o.Pons {
836 for _, onu := range pon.Onus {
837 if onu.Sn() == serialNumber {
Matteo Scandolo27428702019-10-11 16:21:16 -0700838 return onu, nil
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700839 }
840 }
841 }
842
Shrey Baid688b4242020-07-10 20:40:10 +0530843 return &Onu{}, fmt.Errorf("cannot-find-onu-by-serial-number-%s", serialNumber)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700844}
845
William Kurkian9dadc5b2019-10-22 13:51:57 -0400846// returns an ONU with a given interface/Onu Id
Shrey Baid688b4242020-07-10 20:40:10 +0530847func (o *OltDevice) FindOnuById(intfId uint32, onuId uint32) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200848 // TODO this function can be a performance bottleneck when we have many ONUs,
William Kurkian9dadc5b2019-10-22 13:51:57 -0400849 // memoizing it will remove the bottleneck
850 for _, pon := range o.Pons {
851 if pon.ID == intfId {
852 for _, onu := range pon.Onus {
853 if onu.ID == onuId {
854 return onu, nil
855 }
856 }
857 }
858 }
Shrey Baid688b4242020-07-10 20:40:10 +0530859 return &Onu{}, fmt.Errorf("cannot-find-onu-by-id-%v-%v", intfId, onuId)
William Kurkian9dadc5b2019-10-22 13:51:57 -0400860}
861
Matteo Scandolo4a036262020-08-17 15:56:13 -0700862// returns a Service with a given Mac Address
863func (o *OltDevice) FindServiceByMacAddress(mac net.HardwareAddr) (ServiceIf, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200864 // TODO this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700865 // memoizing it will remove the bottleneck
866 for _, pon := range o.Pons {
867 for _, onu := range pon.Onus {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700868 s, err := onu.findServiceByMacAddress(mac)
869 if err == nil {
870 return s, nil
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700871 }
872 }
873 }
874
Matteo Scandolo4a036262020-08-17 15:56:13 -0700875 return nil, fmt.Errorf("cannot-find-service-by-mac-address-%s", mac)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700876}
877
Matteo Scandolo4747d292019-08-05 11:50:18 -0700878// GRPC Endpoints
879
Shrey Baid688b4242020-07-10 20:40:10 +0530880func (o *OltDevice) ActivateOnu(context context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700881 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700882 "OnuSn": onuSnToString(onu.SerialNumber),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700883 }).Info("Received ActivateOnu call from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +0530884 publishEvent("ONU-activate-indication-received", int32(onu.IntfId), int32(onu.OnuId), onuSnToString(onu.SerialNumber))
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700885
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700886 pon, _ := o.GetPonById(onu.IntfId)
887 _onu, _ := pon.GetOnuBySn(onu.SerialNumber)
William Kurkian0418bc82019-11-06 12:16:24 -0500888 _onu.SetID(onu.OnuId)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700889
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700890 if err := _onu.OperState.Event("enable"); err != nil {
891 oltLogger.WithFields(log.Fields{
892 "IntfId": _onu.PonPortID,
893 "OnuSn": _onu.Sn(),
894 "OnuId": _onu.ID,
895 }).Infof("Failed to transition ONU.OperState to enabled state: %s", err.Error())
Matteo Scandolo4747d292019-08-05 11:50:18 -0700896 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700897 if err := _onu.InternalState.Event("enable"); err != nil {
898 oltLogger.WithFields(log.Fields{
899 "IntfId": _onu.PonPortID,
900 "OnuSn": _onu.Sn(),
901 "OnuId": _onu.ID,
902 }).Infof("Failed to transition ONU to enabled state: %s", err.Error())
903 }
904
905 // NOTE we need to immediately activate the ONU or the OMCI state machine won't start
906
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700907 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700908}
909
Shrey Baid688b4242020-07-10 20:40:10 +0530910func (o *OltDevice) DeactivateOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700911 oltLogger.Error("DeactivateOnu not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700912 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700913}
914
Shrey Baid688b4242020-07-10 20:40:10 +0530915func (o *OltDevice) DeleteOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Pragya Arya1cbefa42020-01-13 12:15:29 +0530916 oltLogger.WithFields(log.Fields{
917 "IntfId": onu.IntfId,
918 "OnuId": onu.OnuId,
919 }).Info("Received DeleteOnu call from VOLTHA")
920
921 pon, err := o.GetPonById(onu.IntfId)
922 if err != nil {
923 oltLogger.WithFields(log.Fields{
924 "OnuId": onu.OnuId,
925 "IntfId": onu.IntfId,
926 "err": err,
927 }).Error("Can't find PonPort")
928 }
929 _onu, err := pon.GetOnuById(onu.OnuId)
930 if err != nil {
931 oltLogger.WithFields(log.Fields{
932 "OnuId": onu.OnuId,
933 "IntfId": onu.IntfId,
934 "err": err,
935 }).Error("Can't find Onu")
936 }
937
Hardik Windlassad790cb2020-06-17 21:26:22 +0530938 if err := _onu.InternalState.Event("disable"); err != nil {
Pragya Arya1cbefa42020-01-13 12:15:29 +0530939 oltLogger.WithFields(log.Fields{
940 "IntfId": _onu.PonPortID,
941 "OnuSn": _onu.Sn(),
942 "OnuId": _onu.ID,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530943 }).Infof("Failed to transition ONU to disabled state: %s", err.Error())
944 }
945
Hardik Windlassad790cb2020-06-17 21:26:22 +0530946 // ONU Re-Discovery
947 if o.InternalState.Current() == "enabled" && pon.InternalState.Current() == "enabled" {
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530948 go _onu.ReDiscoverOnu()
Pragya Arya1cbefa42020-01-13 12:15:29 +0530949 }
950
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700951 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700952}
953
Shrey Baid688b4242020-07-10 20:40:10 +0530954func (o *OltDevice) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700955 // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether?
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800956 oltLogger.WithFields(log.Fields{
957 "oltId": o.ID,
958 }).Info("Disabling OLT")
Pragya Arya324337e2020-02-20 14:35:08 +0530959 publishEvent("OLT-disable-received", -1, -1, "")
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800960
Matteo Scandolo401503a2019-12-11 14:48:14 -0800961 for _, pon := range o.Pons {
Pragya Arya2225f202020-01-29 18:05:01 +0530962 if pon.InternalState.Current() == "enabled" {
963 // disable PONs
964 msg := Message{
965 Type: PonIndication,
966 Data: PonIndicationMessage{
967 OperState: DOWN,
968 PonPortID: pon.ID,
969 },
970 }
971 o.channel <- msg
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800972 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800973 }
974
Matteo Scandolo401503a2019-12-11 14:48:14 -0800975 // Note that we are not disabling the NNI as the real OLT does not.
976 // The reason for that is in-band management
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800977
978 // disable OLT
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100979 oltMsg := Message{
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700980 Type: OltIndication,
981 Data: OltIndicationMessage{
982 OperState: DOWN,
983 },
984 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100985 o.channel <- oltMsg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700986 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700987}
988
Shrey Baid688b4242020-07-10 20:40:10 +0530989func (o *OltDevice) DisablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530990 oltLogger.Infof("DisablePonIf request received for PON %d", intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200991 ponID := intf.GetIntfId()
992 pon, _ := o.GetPonById(intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200993
994 msg := Message{
995 Type: PonIndication,
996 Data: PonIndicationMessage{
997 OperState: DOWN,
998 PonPortID: ponID,
999 },
1000 }
1001 o.channel <- msg
1002
1003 for _, onu := range pon.Onus {
1004
1005 onuIndication := OnuIndicationMessage{
1006 OperState: DOWN,
1007 PonPortID: ponID,
1008 OnuID: onu.ID,
1009 OnuSN: onu.SerialNumber,
1010 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001011 onu.sendOnuIndication(onuIndication, o.OpenoltStream)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001012
1013 }
1014
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001015 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001016}
1017
Zdravko Bozakov681364d2019-11-10 14:28:46 +01001018func (o *OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001019 oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +05301020 publishEvent("OLT-enable-received", -1, -1, "")
Matteo Scandolo4747d292019-08-05 11:50:18 -07001021 o.Enable(stream)
1022 return nil
1023}
1024
Shrey Baid688b4242020-07-10 20:40:10 +05301025func (o *OltDevice) EnablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Hardik Windlassad790cb2020-06-17 21:26:22 +05301026 oltLogger.Infof("EnablePonIf request received for PON %d", intf.IntfId)
Pragya Arya2225f202020-01-29 18:05:01 +05301027 ponID := intf.GetIntfId()
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001028 pon, _ := o.GetPonById(intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001029
Pragya Arya2225f202020-01-29 18:05:01 +05301030 msg := Message{
1031 Type: PonIndication,
1032 Data: PonIndicationMessage{
1033 OperState: UP,
1034 PonPortID: ponID,
1035 },
1036 }
1037 o.channel <- msg
1038
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001039 for _, onu := range pon.Onus {
1040
1041 onuIndication := OnuIndicationMessage{
1042 OperState: UP,
1043 PonPortID: ponID,
1044 OnuID: onu.ID,
1045 OnuSN: onu.SerialNumber,
1046 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001047 onu.sendOnuIndication(onuIndication, o.OpenoltStream)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001048
1049 }
1050
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001051 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001052}
1053
Shrey Baid688b4242020-07-10 20:40:10 +05301054func (o *OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001055 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001056 "IntfId": flow.AccessIntfId,
1057 "OnuId": flow.OnuId,
1058 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001059 "InnerVlan": flow.Classifier.IVid,
1060 "OuterVlan": flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001061 "FlowType": flow.FlowType,
1062 "FlowId": flow.FlowId,
1063 "UniID": flow.UniId,
1064 "PortNo": flow.PortNo,
Pragya Arya8bdb4532020-03-02 17:08:09 +05301065 }).Tracef("OLT receives FlowAdd")
1066
1067 flowKey := FlowKey{}
1068 if !o.enablePerf {
1069 flowKey = FlowKey{ID: flow.FlowId, Direction: flow.FlowType}
1070 olt.Flows[flowKey] = *flow
1071 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001072
1073 if flow.AccessIntfId == -1 {
1074 oltLogger.WithFields(log.Fields{
1075 "FlowId": flow.FlowId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001076 }).Debug("Adding OLT flow")
Jonathan Hartb5fc46a2020-03-31 16:42:31 -07001077 } else if flow.FlowType == "multicast" {
1078 oltLogger.WithFields(log.Fields{
Matteo Scandolo618a6582020-09-09 12:21:29 -07001079 "Cookie": flow.Cookie,
1080 "DstPort": flow.Classifier.DstPort,
1081 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
1082 "FlowId": flow.FlowId,
1083 "FlowType": flow.FlowType,
1084 "GemportId": flow.GemportId,
1085 "InnerVlan": flow.Classifier.IVid,
1086 "IntfId": flow.AccessIntfId,
1087 "IpProto": flow.Classifier.IpProto,
1088 "OnuId": flow.OnuId,
1089 "OuterVlan": flow.Classifier.OVid,
1090 "PortNo": flow.PortNo,
1091 "SrcPort": flow.Classifier.SrcPort,
1092 "UniID": flow.UniId,
1093 "ClassifierOPbits": flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001094 }).Debug("Adding OLT multicast flow")
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001095 } else {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001096 pon, err := o.GetPonById(uint32(flow.AccessIntfId))
Matteo Scandolo27428702019-10-11 16:21:16 -07001097 if err != nil {
1098 oltLogger.WithFields(log.Fields{
1099 "OnuId": flow.OnuId,
1100 "IntfId": flow.AccessIntfId,
1101 "err": err,
1102 }).Error("Can't find PonPort")
1103 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001104 onu, err := pon.GetOnuById(uint32(flow.OnuId))
Matteo Scandolo27428702019-10-11 16:21:16 -07001105 if err != nil {
1106 oltLogger.WithFields(log.Fields{
1107 "OnuId": flow.OnuId,
1108 "IntfId": flow.AccessIntfId,
1109 "err": err,
1110 }).Error("Can't find Onu")
Jonathan Hartb5fc46a2020-03-31 16:42:31 -07001111 return nil, err
Matteo Scandolo27428702019-10-11 16:21:16 -07001112 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301113 if !o.enablePerf {
1114 onu.Flows = append(onu.Flows, flowKey)
Pragya Arya1d5ffb82020-03-20 18:51:37 +05301115 // Generate event on first flow for ONU
1116 if len(onu.Flows) == 1 {
1117 publishEvent("Flow-add-received", int32(onu.PonPortID), int32(onu.ID), onuSnToString(onu.SerialNumber))
1118 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301119 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001120
1121 msg := Message{
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001122 Type: FlowAdd,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001123 Data: OnuFlowUpdateMessage{
1124 PonPortID: pon.ID,
1125 OnuID: onu.ID,
1126 Flow: flow,
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001127 },
1128 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001129 onu.Channel <- msg
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001130 }
1131
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001132 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001133}
1134
Pragya Arya8bdb4532020-03-02 17:08:09 +05301135// FlowRemove request from VOLTHA
Shrey Baid688b4242020-07-10 20:40:10 +05301136func (o *OltDevice) FlowRemove(_ context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001137
Pragya Arya8bdb4532020-03-02 17:08:09 +05301138 oltLogger.WithFields(log.Fields{
Shrey Baid55f328c2020-07-07 19:20:42 +05301139 "FlowId": flow.FlowId,
1140 "FlowType": flow.FlowType,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001141 }).Debug("OLT receives FlowRemove")
Pragya Arya8bdb4532020-03-02 17:08:09 +05301142
1143 if !o.enablePerf { // remove only if flow were stored
1144 flowKey := FlowKey{
1145 ID: flow.FlowId,
1146 Direction: flow.FlowType,
1147 }
1148
1149 // Check if flow exists
1150 storedFlow, ok := o.Flows[flowKey]
1151 if !ok {
1152 oltLogger.Errorf("Flow %v not found", flow)
1153 return new(openolt.Empty), status.Errorf(codes.NotFound, "Flow not found")
1154 }
1155
1156 // if its ONU flow remove it from ONU also
1157 if storedFlow.AccessIntfId != -1 {
1158 pon := o.Pons[uint32(storedFlow.AccessIntfId)]
1159 onu, err := pon.GetOnuById(uint32(storedFlow.OnuId))
1160 if err != nil {
1161 oltLogger.WithFields(log.Fields{
1162 "OnuId": storedFlow.OnuId,
1163 "IntfId": storedFlow.AccessIntfId,
1164 "err": err,
1165 }).Error("ONU not found")
1166 return new(openolt.Empty), nil
1167 }
1168 onu.DeleteFlow(flowKey)
Pragya Arya1d5ffb82020-03-20 18:51:37 +05301169 publishEvent("Flow-remove-received", int32(onu.PonPortID), int32(onu.ID), onuSnToString(onu.SerialNumber))
Pragya Arya8bdb4532020-03-02 17:08:09 +05301170 }
1171
1172 // delete from olt flows
1173 delete(o.Flows, flowKey)
1174 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001175
1176 if flow.AccessIntfId == -1 {
1177 oltLogger.WithFields(log.Fields{
1178 "FlowId": flow.FlowId,
1179 }).Debug("Removing OLT flow")
1180 } else if flow.FlowType == "multicast" {
1181 oltLogger.WithFields(log.Fields{
1182 "FlowId": flow.FlowId,
1183 }).Debug("Removing OLT multicast flow")
1184 } else {
1185
1186 onu, err := o.GetOnuByFlowId(flow.FlowId)
1187 if err != nil {
1188 oltLogger.WithFields(log.Fields{
1189 "OnuId": flow.OnuId,
1190 "IntfId": flow.AccessIntfId,
1191 "err": err,
1192 }).Error("Can't find Onu")
1193 return nil, err
1194 }
1195
1196 msg := Message{
1197 Type: FlowRemoved,
1198 Data: OnuFlowUpdateMessage{
Shrey Baid55f328c2020-07-07 19:20:42 +05301199 Flow: flow,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001200 },
1201 }
1202 onu.Channel <- msg
1203 }
1204
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001205 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001206}
1207
Shrey Baid688b4242020-07-10 20:40:10 +05301208func (o *OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo18859852020-01-15 13:33:57 -08001209 res := openolt.Heartbeat{HeartbeatSignature: uint32(time.Now().Unix())}
1210 oltLogger.WithFields(log.Fields{
1211 "signature": res.HeartbeatSignature,
1212 }).Trace("HeartbeatCheck")
1213 return &res, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001214}
1215
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001216func (o *OltDevice) GetOnuByFlowId(flowId uint32) (*Onu, error) {
1217 for _, pon := range o.Pons {
1218 for _, onu := range pon.Onus {
1219 for _, fId := range onu.FlowIds {
1220 if fId == flowId {
1221 return onu, nil
1222 }
1223 }
1224 }
1225 }
Shrey Baid688b4242020-07-10 20:40:10 +05301226 return nil, fmt.Errorf("Cannot find Onu by flowId %d", flowId)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001227}
1228
Shrey Baid688b4242020-07-10 20:40:10 +05301229func (o *OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -07001230
Matteo Scandoloda9cbe22019-08-19 16:05:10 -07001231 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001232 "oltId": o.ID,
Matteo Scandoloda9cbe22019-08-19 16:05:10 -07001233 "PonPorts": o.NumPon,
1234 }).Info("OLT receives GetDeviceInfo call from VOLTHA")
Matteo Scandolo4747d292019-08-05 11:50:18 -07001235 devinfo := new(openolt.DeviceInfo)
Matteo Scandolo4a036262020-08-17 15:56:13 -07001236 devinfo.Vendor = common.Config.Olt.Vendor
1237 devinfo.Model = common.Config.Olt.Model
1238 devinfo.HardwareVersion = common.Config.Olt.HardwareVersion
1239 devinfo.FirmwareVersion = common.Config.Olt.FirmwareVersion
1240 devinfo.Technology = common.Config.Olt.Technology
Matteo Scandoloda9cbe22019-08-19 16:05:10 -07001241 devinfo.PonPorts = uint32(o.NumPon)
Matteo Scandolo4747d292019-08-05 11:50:18 -07001242 devinfo.OnuIdStart = 1
1243 devinfo.OnuIdEnd = 255
1244 devinfo.AllocIdStart = 1024
1245 devinfo.AllocIdEnd = 16383
1246 devinfo.GemportIdStart = 1024
1247 devinfo.GemportIdEnd = 65535
1248 devinfo.FlowIdStart = 1
1249 devinfo.FlowIdEnd = 16383
Matteo Scandolo8df63df2019-09-12 10:34:32 -07001250 devinfo.DeviceSerialNumber = o.SerialNumber
Matteo Scandolo4a036262020-08-17 15:56:13 -07001251 devinfo.DeviceId = common.Config.Olt.DeviceId
Matteo Scandolo4747d292019-08-05 11:50:18 -07001252
1253 return devinfo, nil
1254}
1255
Shrey Baid688b4242020-07-10 20:40:10 +05301256func (o *OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) {
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001257 pon, err := o.GetPonById(omci_msg.IntfId)
1258 if err != nil {
1259 oltLogger.WithFields(log.Fields{
Matteo Scandolof65e6872020-04-15 15:18:43 -07001260 "error": err,
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001261 "onu_id": omci_msg.OnuId,
1262 "pon_id": omci_msg.IntfId,
1263 }).Error("pon ID not found")
1264 return nil, err
1265 }
1266
1267 onu, err := pon.GetOnuById(omci_msg.OnuId)
1268 if err != nil {
1269 oltLogger.WithFields(log.Fields{
Matteo Scandolof65e6872020-04-15 15:18:43 -07001270 "error": err,
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001271 "onu_id": omci_msg.OnuId,
1272 "pon_id": omci_msg.IntfId,
1273 }).Error("onu ID not found")
1274 return nil, err
1275 }
1276
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001277 oltLogger.WithFields(log.Fields{
1278 "IntfId": onu.PonPortID,
1279 "OnuId": onu.ID,
1280 "OnuSn": onu.Sn(),
1281 }).Tracef("Received OmciMsgOut")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001282 msg := Message{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001283 Type: OMCI,
1284 Data: OmciMessage{
1285 OnuSN: onu.SerialNumber,
1286 OnuID: onu.ID,
1287 omciMsg: omci_msg,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001288 },
1289 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001290 onu.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001291 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001292}
1293
Shrey Baid688b4242020-07-10 20:40:10 +05301294func (o *OltDevice) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001295 pon, err := o.GetPonById(onuPkt.IntfId)
Matteo Scandolo27428702019-10-11 16:21:16 -07001296 if err != nil {
1297 oltLogger.WithFields(log.Fields{
1298 "OnuId": onuPkt.OnuId,
1299 "IntfId": onuPkt.IntfId,
1300 "err": err,
1301 }).Error("Can't find PonPort")
1302 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001303 onu, err := pon.GetOnuById(onuPkt.OnuId)
Matteo Scandolo27428702019-10-11 16:21:16 -07001304 if err != nil {
1305 oltLogger.WithFields(log.Fields{
1306 "OnuId": onuPkt.OnuId,
1307 "IntfId": onuPkt.IntfId,
1308 "err": err,
1309 }).Error("Can't find Onu")
1310 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001311
Matteo Scandolo075b1892019-10-07 12:11:07 -07001312 oltLogger.WithFields(log.Fields{
1313 "IntfId": onu.PonPortID,
1314 "OnuId": onu.ID,
1315 "OnuSn": onu.Sn(),
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001316 "Packet": hex.EncodeToString(onuPkt.Pkt),
Matteo Scandolo75ed5b92020-09-03 09:03:16 -07001317 }).Trace("Received OnuPacketOut")
Matteo Scandolo075b1892019-10-07 12:11:07 -07001318
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001319 rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default)
Matteo Scandolo618a6582020-09-09 12:21:29 -07001320
1321 pktType, err := packetHandlers.GetPktType(rawpkt)
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001322 if err != nil {
1323 onuLogger.WithFields(log.Fields{
1324 "IntfId": onu.PonPortID,
1325 "OnuId": onu.ID,
1326 "OnuSn": onu.Sn(),
Matteo Scandolo618a6582020-09-09 12:21:29 -07001327 "Pkt": hex.EncodeToString(rawpkt.Data()),
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001328 }).Error("Can't find pktType in packet, droppint it")
1329 return new(openolt.Empty), nil
1330 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001331
Matteo Scandolo4a036262020-08-17 15:56:13 -07001332 pktMac, err := packetHandlers.GetDstMacAddressFromPacket(rawpkt)
Matteo Scandolo4a036262020-08-17 15:56:13 -07001333 if err != nil {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001334 onuLogger.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -07001335 "IntfId": onu.PonPortID,
1336 "OnuId": onu.ID,
1337 "OnuSn": onu.Sn(),
1338 "Pkt": rawpkt.Data(),
1339 }).Error("Can't find Dst MacAddress in packet, droppint it")
1340 return new(openolt.Empty), nil
1341 }
1342
Matteo Scandolo075b1892019-10-07 12:11:07 -07001343 msg := Message{
1344 Type: OnuPacketOut,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001345 Data: OnuPacketMessage{
Matteo Scandolo4a036262020-08-17 15:56:13 -07001346 IntfId: onuPkt.IntfId,
1347 OnuId: onuPkt.OnuId,
1348 Packet: rawpkt,
1349 Type: pktType,
1350 MacAddress: pktMac,
Matteo Scandolo075b1892019-10-07 12:11:07 -07001351 },
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001352 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001353
Matteo Scandolo075b1892019-10-07 12:11:07 -07001354 onu.Channel <- msg
1355
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001356 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001357}
1358
Shrey Baid688b4242020-07-10 20:40:10 +05301359func (o *OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo635b2bf2020-09-04 10:23:40 -07001360
1361 // OLT Reboot is called in two cases:
1362 // - when an OLT is being removed (voltctl device disable -> voltctl device delete are called, then a new voltctl device create -> voltctl device enable will be issued)
1363 // - when an OLT needs to be rebooted (voltcl device reboot)
1364
Matteo Scandolod02b79b2019-12-05 16:42:13 -08001365 oltLogger.WithFields(log.Fields{
1366 "oltId": o.ID,
1367 }).Info("Shutting down")
Pragya Arya324337e2020-02-20 14:35:08 +05301368 publishEvent("OLT-reboot-received", -1, -1, "")
Shrey Baid688b4242020-07-10 20:40:10 +05301369 go func() { _ = o.RestartOLT() }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001370 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001371}
1372
Shrey Baid688b4242020-07-10 20:40:10 +05301373func (o *OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Pragya Arya6a708d62020-01-01 17:17:20 +05301374 oltLogger.WithFields(log.Fields{
1375 "oltId": o.ID,
1376 }).Info("Received ReenableOlt request from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +05301377 publishEvent("OLT-reenable-received", -1, -1, "")
Pragya Arya6a708d62020-01-01 17:17:20 +05301378
Pragya Arya2225f202020-01-29 18:05:01 +05301379 // enable OLT
1380 oltMsg := Message{
1381 Type: OltIndication,
1382 Data: OltIndicationMessage{
1383 OperState: UP,
1384 },
Pragya Arya1881df02020-01-29 18:05:01 +05301385 }
Pragya Arya2225f202020-01-29 18:05:01 +05301386 o.channel <- oltMsg
Pragya Arya6a708d62020-01-01 17:17:20 +05301387
Pragya Arya2225f202020-01-29 18:05:01 +05301388 for _, pon := range o.Pons {
1389 if pon.InternalState.Current() == "disabled" {
1390 msg := Message{
1391 Type: PonIndication,
1392 Data: PonIndicationMessage{
1393 OperState: UP,
1394 PonPortID: pon.ID,
1395 },
1396 }
1397 o.channel <- msg
1398 }
1399 }
Matteo Scandoloe60a5052020-02-07 00:31:14 +00001400
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001401 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001402}
1403
Shrey Baid688b4242020-07-10 20:40:10 +05301404func (o *OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001405 pkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
1406
Shrey Baid688b4242020-07-10 20:40:10 +05301407 _ = o.Nnis[0].sendNniPacket(pkt) // FIXME we are assuming we have only one NNI
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001408 // NOTE should we return an error if sendNniPakcet fails?
1409 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001410}
1411
Shrey Baid688b4242020-07-10 20:40:10 +05301412func (o *OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001413 oltLogger.Error("CollectStatistics not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001414 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001415}
1416
Shrey Baid688b4242020-07-10 20:40:10 +05301417func (o *OltDevice) GetOnuInfo(context context.Context, packet *openolt.Onu) (*openolt.OnuIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001418 oltLogger.Error("GetOnuInfo not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001419 return new(openolt.OnuIndication), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001420}
1421
Shrey Baid688b4242020-07-10 20:40:10 +05301422func (o *OltDevice) GetPonIf(context context.Context, packet *openolt.Interface) (*openolt.IntfIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001423 oltLogger.Error("GetPonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001424 return new(openolt.IntfIndication), nil
Matteo Scandolod54283a2019-08-13 16:22:31 -07001425}
1426
Shrey Baid688b4242020-07-10 20:40:10 +05301427func (s *OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001428 oltLogger.Info("received CreateTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001429 return new(openolt.Empty), nil
1430}
1431
Shrey Baid688b4242020-07-10 20:40:10 +05301432func (s *OltDevice) RemoveTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001433 oltLogger.Info("received RemoveTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001434 return new(openolt.Empty), nil
1435}
1436
Shrey Baid688b4242020-07-10 20:40:10 +05301437func (s *OltDevice) CreateTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Anand S Katti09541352020-01-29 15:54:01 +05301438 oltLogger.WithFields(log.Fields{
1439 "OnuId": trafficSchedulers.OnuId,
1440 "IntfId": trafficSchedulers.IntfId,
1441 "OnuPortNo": trafficSchedulers.PortNo,
1442 }).Info("received CreateTrafficSchedulers")
1443
1444 if !s.enablePerf {
1445 pon, err := s.GetPonById(trafficSchedulers.IntfId)
1446 if err != nil {
1447 oltLogger.Errorf("Error retrieving PON by IntfId: %v", err)
1448 return new(openolt.Empty), err
1449 }
1450 onu, err := pon.GetOnuById(trafficSchedulers.OnuId)
1451 if err != nil {
1452 oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err)
1453 return new(openolt.Empty), err
1454 }
1455 onu.TrafficSchedulers = trafficSchedulers
1456 }
Matteo Scandolod54283a2019-08-13 16:22:31 -07001457 return new(openolt.Empty), nil
1458}
1459
Shrey Baid688b4242020-07-10 20:40:10 +05301460func (s *OltDevice) RemoveTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Anand S Katti09541352020-01-29 15:54:01 +05301461 oltLogger.WithFields(log.Fields{
1462 "OnuId": trafficSchedulers.OnuId,
1463 "IntfId": trafficSchedulers.IntfId,
1464 "OnuPortNo": trafficSchedulers.PortNo,
1465 }).Info("received RemoveTrafficSchedulers")
1466 if !s.enablePerf {
1467 pon, err := s.GetPonById(trafficSchedulers.IntfId)
1468 if err != nil {
1469 oltLogger.Errorf("Error retrieving PON by IntfId: %v", err)
1470 return new(openolt.Empty), err
1471 }
1472 onu, err := pon.GetOnuById(trafficSchedulers.OnuId)
1473 if err != nil {
1474 oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err)
1475 return new(openolt.Empty), err
1476 }
1477
1478 onu.TrafficSchedulers = nil
1479 }
Matteo Scandolod54283a2019-08-13 16:22:31 -07001480 return new(openolt.Empty), nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001481}
Scott Baker41724b82020-01-21 19:54:53 -08001482
1483// assumes caller has properly formulated an openolt.AlarmIndication
Shrey Baid688b4242020-07-10 20:40:10 +05301484func (o *OltDevice) SendAlarmIndication(context context.Context, ind *openolt.AlarmIndication) error {
Scott Baker41724b82020-01-21 19:54:53 -08001485 msg := Message{
1486 Type: AlarmIndication,
1487 Data: ind,
1488 }
1489
1490 o.channel <- msg
1491 return nil
1492}
Matteo Scandolo618a6582020-09-09 12:21:29 -07001493
1494func (o *OltDevice) PerformGroupOperation(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) {
1495 oltLogger.WithFields(log.Fields{
1496 "GroupId": group.GroupId,
1497 "Command": group.Command,
1498 "Members": group.Members,
1499 "Action": group.Action,
1500 }).Debug("received PerformGroupOperation")
1501 return &openolt.Empty{}, nil
1502}
1503
1504func (o *OltDevice) DeleteGroup(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) {
1505 oltLogger.WithFields(log.Fields{
1506 "GroupId": group.GroupId,
1507 "Command": group.Command,
1508 "Members": group.Members,
1509 "Action": group.Action,
1510 }).Debug("received PerformGroupOperation")
1511 return &openolt.Empty{}, nil
1512}
1513
1514func (o *OltDevice) GetExtValue(ctx context.Context, in *openolt.ValueParam) (*common_protos.ReturnValues, error) {
1515 return &common_protos.ReturnValues{}, nil
1516}
1517
1518func (o *OltDevice) OnuItuPonAlarmSet(ctx context.Context, in *config.OnuItuPonAlarm) (*openolt.Empty, error) {
1519 return &openolt.Empty{}, nil
1520}
1521
1522func (o *OltDevice) GetLogicalOnuDistanceZero(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) {
1523 return &openolt.OnuLogicalDistance{}, nil
1524}
1525
1526func (o *OltDevice) GetLogicalOnuDistance(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) {
1527 return &openolt.OnuLogicalDistance{}, nil
1528}