blob: 305a7ce5341724a15517fa020a845a343698270e [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"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020023 "net"
Matteo Scandolof9d43412021-01-12 11:11:34 -080024 "strconv"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020025 "sync"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010026 "time"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020027
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000028 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
29 "github.com/opencord/bbsim/internal/bbsim/types"
30 omcilib "github.com/opencord/bbsim/internal/common/omci"
31 "github.com/opencord/voltha-protos/v4/go/ext/config"
32
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070033 "github.com/google/gopacket"
34 "github.com/google/gopacket/layers"
Matteo Scandolo4747d292019-08-05 11:50:18 -070035 "github.com/looplab/fsm"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070036 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010037 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070038 common_protos "github.com/opencord/voltha-protos/v4/go/common"
39 "github.com/opencord/voltha-protos/v4/go/openolt"
40 "github.com/opencord/voltha-protos/v4/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070041 log "github.com/sirupsen/logrus"
42 "google.golang.org/grpc"
Pragya Arya8bdb4532020-03-02 17:08:09 +053043 "google.golang.org/grpc/codes"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010044 "google.golang.org/grpc/reflection"
Pragya Arya8bdb4532020-03-02 17:08:09 +053045 "google.golang.org/grpc/status"
Matteo Scandolo4747d292019-08-05 11:50:18 -070046)
47
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070048var oltLogger = log.WithFields(log.Fields{
Matteo Scandolo84f7d482019-08-08 19:00:47 -070049 "module": "OLT",
50})
51
Matteo Scandolocedde462021-03-09 17:37:16 -080052const (
53 onuIdStart = 1
54 onuIdEnd = 127
55 allocIdStart = 1024
56 allocIdEnd = 16383
57 gemportIdStart = 1024
58 gemportIdEnd = 65535
59 flowIdStart = 1
60 flowIdEnd = 65535
61)
62
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070063type OltDevice struct {
David Bainbridge103cf022019-12-16 20:11:35 +000064 sync.Mutex
Hardik Windlassefdb4b62021-03-18 10:33:24 +000065 OltServer *grpc.Server
David Bainbridge103cf022019-12-16 20:11:35 +000066
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070067 // BBSIM Internals
Pragya Arya2225f202020-01-29 18:05:01 +053068 ID int
69 SerialNumber string
70 NumNni int
71 NumPon int
72 NumOnuPerPon int
73 InternalState *fsm.FSM
Matteo Scandolof9d43412021-01-12 11:11:34 -080074 channel chan types.Message
Matteo Scandolo90d08f62020-10-29 12:06:55 -070075 dhcpServer dhcp.DHCPServerIf
Andrea Campanellabe8e12f2020-12-14 18:43:41 +010076 Flows sync.Map
Pragya Arya2225f202020-01-29 18:05:01 +053077 Delay int
78 ControlledActivation mode
Pragya Arya324337e2020-02-20 14:35:08 +053079 EventChannel chan common.Event
80 PublishEvents bool
Pragya Arya996a0892020-03-09 21:47:52 +053081 PortStatsInterval int
Matteo Scandolo96f89192021-03-12 13:17:26 -080082 PreviouslyConnected bool
Matteo Scandoloe33447a2019-10-31 12:38:23 -070083
Matteo Scandolo27428702019-10-11 16:21:16 -070084 Pons []*PonPort
85 Nnis []*NniPort
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070086
87 // OLT Attributes
88 OperState *fsm.FSM
David Bainbridge103cf022019-12-16 20:11:35 +000089
90 enableContext context.Context
91 enableContextCancel context.CancelFunc
Pragya Arya1cbefa42020-01-13 12:15:29 +053092
Matteo Scandolo4a036262020-08-17 15:56:13 -070093 OpenoltStream openolt.Openolt_EnableIndicationServer
Anand S Katti09541352020-01-29 15:54:01 +053094 enablePerf bool
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080095
96 // Allocated Resources
97 // this data are to verify that the openolt adapter does not duplicate resources
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000098 AllocIDsLock sync.RWMutex
99 AllocIDs map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool // map[ponPortId]map[OnuId]map[PortNo]map[AllocIds]map[FlowId]bool
100 GemPortIDsLock sync.RWMutex
101 GemPortIDs map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool // map[ponPortId]map[OnuId]map[PortNo]map[GemPortIDs]map[FlowId]bool
102 OmciResponseRate uint8
Matteo Scandolo4747d292019-08-05 11:50:18 -0700103}
104
Matteo Scandolo27428702019-10-11 16:21:16 -0700105var olt OltDevice
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700106
Matteo Scandolo27428702019-10-11 16:21:16 -0700107func GetOLT() *OltDevice {
108 return &olt
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700109}
110
Matteo Scandolo4a036262020-08-17 15:56:13 -0700111func CreateOLT(options common.GlobalConfig, services []common.ServiceYaml, isMock bool) *OltDevice {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700112 oltLogger.WithFields(log.Fields{
Pragya Arya996a0892020-03-09 21:47:52 +0530113 "ID": options.Olt.ID,
114 "NumNni": options.Olt.NniPorts,
115 "NumPon": options.Olt.PonPorts,
116 "NumOnuPerPon": options.Olt.OnusPonPort,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700117 }).Debug("CreateOLT")
118
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700119 olt = OltDevice{
Pragya Arya996a0892020-03-09 21:47:52 +0530120 ID: options.Olt.ID,
121 SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", options.Olt.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700122 OperState: getOperStateFSM(func(e *fsm.Event) {
123 oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst)
124 }),
Matteo Scandolo96f89192021-03-12 13:17:26 -0800125 NumNni: int(options.Olt.NniPorts),
126 NumPon: int(options.Olt.PonPorts),
127 NumOnuPerPon: int(options.Olt.OnusPonPort),
128 Pons: []*PonPort{},
129 Nnis: []*NniPort{},
130 Delay: options.BBSim.Delay,
131 enablePerf: options.BBSim.EnablePerf,
132 PublishEvents: options.BBSim.Events,
133 PortStatsInterval: options.Olt.PortStatsInterval,
134 dhcpServer: dhcp.NewDHCPServer(),
135 PreviouslyConnected: false,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800136 AllocIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool),
137 GemPortIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool),
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000138 OmciResponseRate: options.Olt.OmciResponseRate,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700139 }
140
Pragya Arya996a0892020-03-09 21:47:52 +0530141 if val, ok := ControlledActivationModes[options.BBSim.ControlledActivation]; ok {
Pragya Arya2225f202020-01-29 18:05:01 +0530142 olt.ControlledActivation = val
143 } else {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700144 // FIXME throw an error if the ControlledActivation is not valid
Pragya Arya2225f202020-01-29 18:05:01 +0530145 oltLogger.Warn("Unknown ControlledActivation Mode given, running in Default mode")
146 olt.ControlledActivation = Default
147 }
148
Matteo Scandolo4747d292019-08-05 11:50:18 -0700149 // OLT State machine
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700150 // NOTE do we need 2 state machines for the OLT? (InternalState and OperState)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700151 olt.InternalState = fsm.NewFSM(
152 "created",
153 fsm.Events{
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800154 {Name: "initialize", Src: []string{"created", "deleted"}, Dst: "initialized"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100155 {Name: "enable", Src: []string{"initialized", "disabled"}, Dst: "enabled"},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700156 {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"},
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700157 // delete event in enabled state below is for reboot OLT case.
Mahir Gunyel6dad4452020-01-06 12:59:04 -0800158 {Name: "delete", Src: []string{"disabled", "enabled"}, Dst: "deleted"},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700159 },
160 fsm.Callbacks{
161 "enter_state": func(e *fsm.Event) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700162 oltLogger.Debugf("Changing OLT InternalState from %s to %s", e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700163 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100164 "enter_initialized": func(e *fsm.Event) { olt.InitOlt() },
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800165 "enter_deleted": func(e *fsm.Event) {
166 // remove all the resource allocations
167 olt.clearAllResources()
168 },
Matteo Scandolo4747d292019-08-05 11:50:18 -0700169 },
170 )
171
Shrey Baid688b4242020-07-10 20:40:10 +0530172 if !isMock {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700173 // create NNI Port
174 nniPort, err := CreateNNI(&olt)
175 if err != nil {
176 oltLogger.Fatalf("Couldn't create NNI Port: %v", err)
177 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700178
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700179 olt.Nnis = append(olt.Nnis, &nniPort)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700180 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700181
Matteo Scandolo4a036262020-08-17 15:56:13 -0700182 // Create device and Services
183
184 nextCtag := map[string]int{}
185 nextStag := map[string]int{}
186
Matteo Scandolo4747d292019-08-05 11:50:18 -0700187 // create PON ports
Matteo Scandolo4a036262020-08-17 15:56:13 -0700188 for i := 0; i < olt.NumPon; i++ {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800189
190 // initialize the resource maps for every PON Ports
191 olt.AllocIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
192 olt.GemPortIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
193
Matteo Scandolo4a036262020-08-17 15:56:13 -0700194 p := CreatePonPort(&olt, uint32(i))
Matteo Scandolo4747d292019-08-05 11:50:18 -0700195
Matteo Scandolo4a036262020-08-17 15:56:13 -0700196 // create ONU devices
197 for j := 0; j < olt.NumOnuPerPon; j++ {
198 delay := time.Duration(olt.Delay*j) * time.Millisecond
199 o := CreateONU(&olt, p, uint32(j+1), delay, isMock)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700200
Matteo Scandolo4a036262020-08-17 15:56:13 -0700201 for k, s := range common.Services {
202
203 // find the correct cTag for this service
204 if _, ok := nextCtag[s.Name]; !ok {
205 // it's the first time we iterate over this service,
206 // so we start from the config value
207 nextCtag[s.Name] = s.CTag
208 } else {
209 // we have a previous value, so we check it
210 // if Allocation is unique, we increment,
211 // otherwise (shared) we do nothing
212 if s.CTagAllocation == common.TagAllocationUnique.String() {
213 nextCtag[s.Name] = nextCtag[s.Name] + 1
214 }
215 }
216
217 // find the correct sTag for this service
218 if _, ok := nextStag[s.Name]; !ok {
219 nextStag[s.Name] = s.STag
220 } else {
221 if s.STagAllocation == common.TagAllocationUnique.String() {
222 nextStag[s.Name] = nextStag[s.Name] + 1
223 }
224 }
225
226 mac := net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(p.ID), byte(o.ID), byte(k)}
227 service, err := NewService(s.Name, mac, o, nextCtag[s.Name], nextStag[s.Name],
228 s.NeedsEapol, s.NeedsDchp, s.NeedsIgmp, s.TechnologyProfileID, s.UniTagMatch,
229 s.ConfigureMacAddress, s.UsPonCTagPriority, s.UsPonSTagPriority, s.DsPonCTagPriority, s.DsPonSTagPriority)
230
231 if err != nil {
232 oltLogger.WithFields(log.Fields{
233 "Err": err.Error(),
234 }).Fatal("Can't create Service")
235 }
236
237 o.Services = append(o.Services, service)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700238 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700239 p.Onus = append(p.Onus, o)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700240 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700241 olt.Pons = append(olt.Pons, p)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700242 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100243
Shrey Baid688b4242020-07-10 20:40:10 +0530244 if !isMock {
Matteo Scandolod32c3822019-11-26 15:57:46 -0700245 if err := olt.InternalState.Event("initialize"); err != nil {
246 log.Errorf("Error initializing OLT: %v", err)
247 return nil
248 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100249 }
250
Pragya Arya324337e2020-02-20 14:35:08 +0530251 if olt.PublishEvents {
252 log.Debugf("BBSim event publishing is enabled")
253 // Create a channel to write event messages
254 olt.EventChannel = make(chan common.Event, 100)
255 }
256
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700257 return &olt
258}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700259
Shrey Baid688b4242020-07-10 20:40:10 +0530260func (o *OltDevice) InitOlt() {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100261
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000262 if o.OltServer == nil {
263 o.OltServer, _ = o.StartOltServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100264 } else {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800265 oltLogger.Fatal("OLT server already running.")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100266 }
267
268 // create new channel for processOltMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800269 o.channel = make(chan types.Message)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100270
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100271 // FIXME we are assuming we have only one NNI
272 if o.Nnis[0] != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800273 // NOTE we want to make sure the state is down when we initialize the OLT,
274 // the NNI may be in a bad state after a disable/reboot as we are not disabling it for
275 // in-band management
276 o.Nnis[0].OperState.SetState("down")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100277 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800278
279 for ponId := range o.Pons {
280 // initialize the resource maps for every PON Ports
281 olt.AllocIDs[uint32(ponId)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
282 olt.GemPortIDs[uint32(ponId)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
283 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700284}
285
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800286func (o *OltDevice) RestartOLT() error {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100287
Matteo Scandolo96f89192021-03-12 13:17:26 -0800288 o.PreviouslyConnected = false
289
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700290 softReboot := false
Matteo Scandolo4a036262020-08-17 15:56:13 -0700291 rebootDelay := common.Config.Olt.OltRebootDelay
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800292
293 oltLogger.WithFields(log.Fields{
294 "oltId": o.ID,
295 }).Infof("Simulating OLT restart... (%ds)", rebootDelay)
296
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700297 if o.InternalState.Is("enabled") {
298 oltLogger.WithFields(log.Fields{
299 "oltId": o.ID,
300 }).Info("This is an OLT soft reboot")
301 softReboot = true
302 }
303
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800304 // transition internal state to deleted
305 if err := o.InternalState.Event("delete"); err != nil {
306 oltLogger.WithFields(log.Fields{
307 "oltId": o.ID,
308 }).Errorf("Error deleting OLT: %v", err)
309 return err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100310 }
311
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700312 if softReboot {
313 for _, pon := range o.Pons {
314 if pon.InternalState.Current() == "enabled" {
315 // disable PONs
Matteo Scandolof9d43412021-01-12 11:11:34 -0800316 msg := types.Message{
317 Type: types.PonIndication,
318 Data: types.PonIndicationMessage{
319 OperState: types.DOWN,
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700320 PonPortID: pon.ID,
321 },
322 }
323 o.channel <- msg
324 }
325
326 for _, onu := range pon.Onus {
Matteo Scandolocedde462021-03-09 17:37:16 -0800327 _ = onu.InternalState.Event(OnuTxDisable)
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700328 }
329 }
330 } else {
331 // PONs are already handled in the Disable call
332 for _, pon := range olt.Pons {
333 // ONUs are not automatically disabled when a PON goes down
334 // as it's possible that it's an admin down and in that case the ONUs need to keep their state
335 for _, onu := range pon.Onus {
Matteo Scandolocedde462021-03-09 17:37:16 -0800336 _ = onu.InternalState.Event(OnuTxDisable)
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700337 }
Pragya Arya2225f202020-01-29 18:05:01 +0530338 }
339 }
340
Matteo Scandolob307d8a2021-05-10 15:19:27 -0700341 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
342 o.StopOltServer()
343
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100344 // terminate the OLT's processOltMessages go routine
345 close(o.channel)
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700346
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700347 o.enableContextCancel()
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100348 time.Sleep(time.Duration(rebootDelay) * time.Second)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100349
350 if err := o.InternalState.Event("initialize"); err != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800351 oltLogger.WithFields(log.Fields{
352 "oltId": o.ID,
353 }).Errorf("Error initializing OLT: %v", err)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100354 return err
355 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800356 oltLogger.WithFields(log.Fields{
357 "oltId": o.ID,
358 }).Info("OLT restart completed")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100359 return nil
360}
361
362// newOltServer launches a new grpc server for OpenOLT
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800363func (o *OltDevice) newOltServer() (*grpc.Server, error) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700364 address := common.Config.BBSim.OpenOltAddress
Matteo Scandolo4747d292019-08-05 11:50:18 -0700365 lis, err := net.Listen("tcp", address)
366 if err != nil {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700367 oltLogger.Fatalf("OLT failed to listen: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700368 }
369 grpcServer := grpc.NewServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100370
Matteo Scandolo4747d292019-08-05 11:50:18 -0700371 openolt.RegisterOpenoltServer(grpcServer, o)
372
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100373 reflection.Register(grpcServer)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700374
Shrey Baid688b4242020-07-10 20:40:10 +0530375 go func() { _ = grpcServer.Serve(lis) }()
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100376 oltLogger.Debugf("OLT listening on %v", address)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700377
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100378 return grpcServer, nil
379}
380
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800381// StartOltServer will create the grpc server that VOLTHA uses
382// to communicate with the device
383func (o *OltDevice) StartOltServer() (*grpc.Server, error) {
384 oltServer, err := o.newOltServer()
385 if err != nil {
386 oltLogger.WithFields(log.Fields{
387 "err": err,
388 }).Error("Cannot OLT gRPC server")
389 return nil, err
390 }
391 return oltServer, nil
392}
393
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100394// StopOltServer stops the OpenOLT grpc server
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800395func (o *OltDevice) StopOltServer() {
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000396 if o.OltServer != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800397 oltLogger.WithFields(log.Fields{
398 "oltId": o.SerialNumber,
399 }).Warnf("Stopping OLT gRPC server")
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000400 o.OltServer.Stop()
401 o.OltServer = nil
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700402 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700403}
404
405// Device Methods
406
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100407// Enable implements the OpenOLT EnableIndicationServer functionality
Shrey Baid688b4242020-07-10 20:40:10 +0530408func (o *OltDevice) Enable(stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700409 oltLogger.Debug("Enable OLT called")
Pragya Arya2225f202020-01-29 18:05:01 +0530410 rebootFlag := false
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700411
David Bainbridge103cf022019-12-16 20:11:35 +0000412 // If enabled has already been called then an enabled context has
413 // been created. If this is the case then we want to cancel all the
414 // proessing loops associated with that enable before we recreate
415 // new ones
416 o.Lock()
417 if o.enableContext != nil && o.enableContextCancel != nil {
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700418 oltLogger.Info("This is an OLT reboot or a reconcile")
David Bainbridge103cf022019-12-16 20:11:35 +0000419 o.enableContextCancel()
Pragya Arya2225f202020-01-29 18:05:01 +0530420 rebootFlag = true
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700421 time.Sleep(1 * time.Second)
David Bainbridge103cf022019-12-16 20:11:35 +0000422 }
423 o.enableContext, o.enableContextCancel = context.WithCancel(context.TODO())
424 o.Unlock()
425
Matteo Scandolo4747d292019-08-05 11:50:18 -0700426 wg := sync.WaitGroup{}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700427
Matteo Scandolo4a036262020-08-17 15:56:13 -0700428 o.OpenoltStream = stream
Pragya Arya1cbefa42020-01-13 12:15:29 +0530429
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100430 // create Go routine to process all OLT events
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700431 wg.Add(1)
David Bainbridge103cf022019-12-16 20:11:35 +0000432 go o.processOltMessages(o.enableContext, stream, &wg)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700433
434 // enable the OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -0800435 oltMsg := types.Message{
436 Type: types.OltIndication,
437 Data: types.OltIndicationMessage{
438 OperState: types.UP,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700439 },
440 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100441 o.channel <- oltMsg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700442
443 // send NNI Port Indications
444 for _, nni := range o.Nnis {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800445 msg := types.Message{
446 Type: types.NniIndication,
447 Data: types.NniIndicationMessage{
448 OperState: types.UP,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700449 NniPortID: nni.ID,
450 },
451 }
452 o.channel <- msg
453 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100454
Shrey Baid688b4242020-07-10 20:40:10 +0530455 if rebootFlag {
Pragya Arya2225f202020-01-29 18:05:01 +0530456 for _, pon := range o.Pons {
457 if pon.InternalState.Current() == "disabled" {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800458 msg := types.Message{
459 Type: types.PonIndication,
460 Data: types.PonIndicationMessage{
461 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +0530462 PonPortID: pon.ID,
463 },
464 }
465 o.channel <- msg
Matteo Scandoloe60a5052020-02-07 00:31:14 +0000466 }
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700467 // when the enableContext was canceled the ONUs stopped listening on the channel
468 for _, onu := range pon.Onus {
469 go onu.ProcessOnuMessages(o.enableContext, stream, nil)
470
471 // update the stream on all the services
472 for _, service := range onu.Services {
473 service.UpdateStream(stream)
474 }
475 }
Pragya Arya2225f202020-01-29 18:05:01 +0530476 }
477 } else {
478
479 // 1. controlledActivation == Default: Send both PON and ONUs indications
480 // 2. controlledActivation == only-onu: that means only ONUs will be controlled activated, so auto send PON indications
481
482 if o.ControlledActivation == Default || o.ControlledActivation == OnlyONU {
483 // send PON Port indications
484 for _, pon := range o.Pons {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800485 msg := types.Message{
486 Type: types.PonIndication,
487 Data: types.PonIndicationMessage{
488 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +0530489 PonPortID: pon.ID,
490 },
491 }
492 o.channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700493 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700494 }
495 }
496
Pragya Arya996a0892020-03-09 21:47:52 +0530497 if !o.enablePerf {
498 // Start a go routine to send periodic port stats to openolt adapter
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700499 wg.Add(1)
500 go o.periodicPortStats(o.enableContext, &wg, stream)
Pragya Arya996a0892020-03-09 21:47:52 +0530501 }
502
Matteo Scandolo4747d292019-08-05 11:50:18 -0700503 wg.Wait()
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700504 oltLogger.WithFields(log.Fields{
505 "stream": stream,
506 }).Debug("OpenOLT Stream closed")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700507}
508
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700509func (o *OltDevice) periodicPortStats(ctx context.Context, wg *sync.WaitGroup, stream openolt.Openolt_EnableIndicationServer) {
Pragya Arya996a0892020-03-09 21:47:52 +0530510 var portStats *openolt.PortStatistics
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700511
512loop:
Pragya Arya996a0892020-03-09 21:47:52 +0530513 for {
514 select {
515 case <-time.After(time.Duration(o.PortStatsInterval) * time.Second):
516 // send NNI port stats
517 for _, port := range o.Nnis {
518 incrementStat := true
519 if port.OperState.Current() == "down" {
520 incrementStat = false
521 }
522 portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat)
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700523 o.sendPortStatsIndication(portStats, port.ID, port.Type, stream)
Pragya Arya996a0892020-03-09 21:47:52 +0530524 }
525
526 // send PON port stats
527 for _, port := range o.Pons {
528 incrementStat := true
529 // do not increment port stats if PON port is down or no ONU is activated on PON port
530 if port.OperState.Current() == "down" || port.GetNumOfActiveOnus() < 1 {
531 incrementStat = false
532 }
533 portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat)
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700534 o.sendPortStatsIndication(portStats, port.ID, port.Type, stream)
Pragya Arya996a0892020-03-09 21:47:52 +0530535 }
536 case <-ctx.Done():
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700537 oltLogger.Debug("Stop sending port stats")
538 break loop
Pragya Arya996a0892020-03-09 21:47:52 +0530539 }
Pragya Arya996a0892020-03-09 21:47:52 +0530540 }
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700541 wg.Done()
Pragya Arya996a0892020-03-09 21:47:52 +0530542}
543
Matteo Scandolo4747d292019-08-05 11:50:18 -0700544// Helpers method
545
Matteo Scandolof9d43412021-01-12 11:11:34 -0800546func (o *OltDevice) SetAlarm(interfaceId uint32, interfaceType string, alarmStatus string) error {
547
548 switch interfaceType {
549 case "nni":
550 if !o.HasNni(interfaceId) {
551 return status.Errorf(codes.NotFound, strconv.Itoa(int(interfaceId))+" NNI not present in olt")
552 }
553
554 case "pon":
555 if !o.HasPon(interfaceId) {
556 return status.Errorf(codes.NotFound, strconv.Itoa(int(interfaceId))+" PON not present in olt")
557 }
558 }
559
560 alarmIndication := &openolt.AlarmIndication{
561 Data: &openolt.AlarmIndication_LosInd{LosInd: &openolt.LosIndication{
562 Status: alarmStatus,
563 IntfId: InterfaceIDToPortNo(interfaceId, interfaceType),
564 }},
565 }
566
567 msg := types.Message{
568 Type: types.AlarmIndication,
569 Data: alarmIndication,
570 }
571
572 o.channel <- msg
573
574 return nil
575}
576
577func (o *OltDevice) HasNni(id uint32) bool {
578 for _, intf := range o.Nnis {
579 if intf.ID == id {
580 return true
581 }
582 }
583 return false
584}
585
586func (o *OltDevice) HasPon(id uint32) bool {
587 for _, intf := range o.Pons {
588 if intf.ID == id {
589 return true
590 }
591 }
592 return false
593}
594
Shrey Baid688b4242020-07-10 20:40:10 +0530595func (o *OltDevice) GetPonById(id uint32) (*PonPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700596 for _, pon := range o.Pons {
597 if pon.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700598 return pon, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700599 }
600 }
Shrey Baid688b4242020-07-10 20:40:10 +0530601 return nil, fmt.Errorf("Cannot find PonPort with id %d in OLT %d", id, o.ID)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700602}
603
Shrey Baid688b4242020-07-10 20:40:10 +0530604func (o *OltDevice) getNniById(id uint32) (*NniPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700605 for _, nni := range o.Nnis {
606 if nni.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700607 return nni, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700608 }
609 }
Shrey Baid688b4242020-07-10 20:40:10 +0530610 return nil, fmt.Errorf("Cannot find NniPort with id %d in OLT %d", id, o.ID)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700611}
612
Scott Baker41724b82020-01-21 19:54:53 -0800613func (o *OltDevice) sendAlarmIndication(alarmInd *openolt.AlarmIndication, stream openolt.Openolt_EnableIndicationServer) {
614 data := &openolt.Indication_AlarmInd{AlarmInd: alarmInd}
615 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
616 oltLogger.Errorf("Failed to send Alarm Indication: %v", err)
617 return
618 }
619
620 oltLogger.WithFields(log.Fields{
621 "AlarmIndication": alarmInd,
622 }).Debug("Sent Indication_AlarmInd")
623}
624
Matteo Scandolof9d43412021-01-12 11:11:34 -0800625func (o *OltDevice) sendOltIndication(msg types.OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700626 data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}}
627 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700628 oltLogger.Errorf("Failed to send Indication_OltInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800629 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700630 }
631
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700632 oltLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700633 "OperState": msg.OperState,
634 }).Debug("Sent Indication_OltInd")
635}
636
Matteo Scandolof9d43412021-01-12 11:11:34 -0800637func (o *OltDevice) sendNniIndication(msg types.NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700638 nni, _ := o.getNniById(msg.NniPortID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800639 if msg.OperState == types.UP {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800640 if err := nni.OperState.Event("enable"); err != nil {
641 log.WithFields(log.Fields{
642 "Type": nni.Type,
643 "IntfId": nni.ID,
644 "OperState": nni.OperState.Current(),
645 }).Errorf("Can't move NNI Port to enabled state: %v", err)
646 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800647 } else if msg.OperState == types.DOWN {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800648 if err := nni.OperState.Event("disable"); err != nil {
649 log.WithFields(log.Fields{
650 "Type": nni.Type,
651 "IntfId": nni.ID,
652 "OperState": nni.OperState.Current(),
653 }).Errorf("Can't move NNI Port to disable state: %v", err)
654 }
655 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700656 // NOTE Operstate may need to be an integer
Matteo Scandolo4747d292019-08-05 11:50:18 -0700657 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700658 Type: nni.Type,
659 IntfId: nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700660 OperState: nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700661 }}
662
663 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700664 oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800665 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700666 }
667
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700668 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700669 "Type": nni.Type,
670 "IntfId": nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700671 "OperState": nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700672 }).Debug("Sent Indication_IntfOperInd for NNI")
673}
674
Pragya Arya2225f202020-01-29 18:05:01 +0530675func (o *OltDevice) sendPonIndication(ponPortID uint32) {
676
Matteo Scandolo4a036262020-08-17 15:56:13 -0700677 stream := o.OpenoltStream
Pragya Arya2225f202020-01-29 18:05:01 +0530678 pon, _ := o.GetPonById(ponPortID)
679 // Send IntfIndication for PON port
Matteo Scandolo4747d292019-08-05 11:50:18 -0700680 discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700681 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700682 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700683 }}
684
685 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700686 oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800687 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700688 }
689
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700690 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700691 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700692 "OperState": pon.OperState.Current(),
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700693 }).Debug("Sent Indication_IntfInd for PON")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700694
Pragya Arya2225f202020-01-29 18:05:01 +0530695 // Send IntfOperIndication for PON port
Matteo Scandolo4747d292019-08-05 11:50:18 -0700696 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700697 Type: pon.Type,
698 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700699 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700700 }}
701
702 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700703 oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800704 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700705 }
706
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700707 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700708 "Type": pon.Type,
709 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700710 "OperState": pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700711 }).Debug("Sent Indication_IntfOperInd for PON")
712}
713
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700714func (o *OltDevice) sendPortStatsIndication(stats *openolt.PortStatistics, portID uint32, portType string, stream openolt.Openolt_EnableIndicationServer) {
Shrey Baid55f328c2020-07-07 19:20:42 +0530715 if o.InternalState.Current() == "enabled" {
716 oltLogger.WithFields(log.Fields{
717 "Type": portType,
718 "IntfId": portID,
719 }).Trace("Sending port stats")
720 stats.IntfId = InterfaceIDToPortNo(portID, portType)
721 data := &openolt.Indication_PortStats{
722 PortStats: stats,
723 }
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700724
Shrey Baid55f328c2020-07-07 19:20:42 +0530725 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
726 oltLogger.Errorf("Failed to send PortStats: %v", err)
727 return
728 }
Pragya Arya996a0892020-03-09 21:47:52 +0530729 }
730}
731
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100732// processOltMessages handles messages received over the OpenOLT interface
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700733func (o *OltDevice) processOltMessages(ctx context.Context, stream types.Stream, wg *sync.WaitGroup) {
734 oltLogger.WithFields(log.Fields{
735 "stream": stream,
736 }).Debug("Starting OLT Indication Channel")
David Bainbridge103cf022019-12-16 20:11:35 +0000737 ch := o.channel
Matteo Scandolo4747d292019-08-05 11:50:18 -0700738
David Bainbridge103cf022019-12-16 20:11:35 +0000739loop:
740 for {
741 select {
742 case <-ctx.Done():
743 oltLogger.Debug("OLT Indication processing canceled via context")
744 break loop
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700745 case <-stream.Context().Done():
746 oltLogger.Debug("OLT Indication processing canceled via stream context")
747 break loop
David Bainbridge103cf022019-12-16 20:11:35 +0000748 case message, ok := <-ch:
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700749 if !ok {
750 if ctx.Err() != nil {
751 oltLogger.WithField("err", ctx.Err()).Error("OLT EnableContext error")
752 }
753 oltLogger.Warn("OLT Indication processing canceled via closed channel")
David Bainbridge103cf022019-12-16 20:11:35 +0000754 break loop
Matteo Scandolo4747d292019-08-05 11:50:18 -0700755 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700756
David Bainbridge103cf022019-12-16 20:11:35 +0000757 oltLogger.WithFields(log.Fields{
758 "oltId": o.ID,
759 "messageType": message.Type,
760 }).Trace("Received message")
761
762 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800763 case types.OltIndication:
764 msg, _ := message.Data.(types.OltIndicationMessage)
765 if msg.OperState == types.UP {
Shrey Baid688b4242020-07-10 20:40:10 +0530766 _ = o.InternalState.Event("enable")
767 _ = o.OperState.Event("enable")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800768 } else if msg.OperState == types.DOWN {
Shrey Baid688b4242020-07-10 20:40:10 +0530769 _ = o.InternalState.Event("disable")
770 _ = o.OperState.Event("disable")
David Bainbridge103cf022019-12-16 20:11:35 +0000771 }
772 o.sendOltIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800773 case types.AlarmIndication:
Scott Baker41724b82020-01-21 19:54:53 -0800774 alarmInd, _ := message.Data.(*openolt.AlarmIndication)
775 o.sendAlarmIndication(alarmInd, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800776 case types.NniIndication:
777 msg, _ := message.Data.(types.NniIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000778 o.sendNniIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800779 case types.PonIndication:
780 msg, _ := message.Data.(types.PonIndicationMessage)
Pragya Arya2225f202020-01-29 18:05:01 +0530781 pon, _ := o.GetPonById(msg.PonPortID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800782 if msg.OperState == types.UP {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530783 if err := pon.OperState.Event("enable"); err != nil {
784 oltLogger.WithFields(log.Fields{
785 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800786 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530787 }).Error("Can't Enable Oper state for PON Port")
788 }
789 if err := pon.InternalState.Event("enable"); err != nil {
790 oltLogger.WithFields(log.Fields{
791 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800792 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530793 }).Error("Can't Enable Internal state for PON Port")
794 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800795 } else if msg.OperState == types.DOWN {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530796 if err := pon.OperState.Event("disable"); err != nil {
797 oltLogger.WithFields(log.Fields{
798 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800799 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530800 }).Error("Can't Disable Oper state for PON Port")
801 }
802 if err := pon.InternalState.Event("disable"); err != nil {
803 oltLogger.WithFields(log.Fields{
804 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800805 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530806 }).Error("Can't Disable Internal state for PON Port")
807 }
Pragya Arya2225f202020-01-29 18:05:01 +0530808 }
David Bainbridge103cf022019-12-16 20:11:35 +0000809 default:
810 oltLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
811 }
812 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700813 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100814 wg.Done()
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700815 oltLogger.WithFields(log.Fields{
816 "stream": stream,
817 }).Warn("Stopped handling OLT Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700818}
819
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700820// returns an ONU with a given Serial Number
Shrey Baid688b4242020-07-10 20:40:10 +0530821func (o *OltDevice) FindOnuBySn(serialNumber string) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200822 // TODO this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700823 // memoizing it will remove the bottleneck
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700824 for _, pon := range o.Pons {
825 for _, onu := range pon.Onus {
826 if onu.Sn() == serialNumber {
Matteo Scandolo27428702019-10-11 16:21:16 -0700827 return onu, nil
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700828 }
829 }
830 }
831
Shrey Baid688b4242020-07-10 20:40:10 +0530832 return &Onu{}, fmt.Errorf("cannot-find-onu-by-serial-number-%s", serialNumber)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700833}
834
William Kurkian9dadc5b2019-10-22 13:51:57 -0400835// returns an ONU with a given interface/Onu Id
Shrey Baid688b4242020-07-10 20:40:10 +0530836func (o *OltDevice) FindOnuById(intfId uint32, onuId uint32) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200837 // TODO this function can be a performance bottleneck when we have many ONUs,
William Kurkian9dadc5b2019-10-22 13:51:57 -0400838 // memoizing it will remove the bottleneck
839 for _, pon := range o.Pons {
840 if pon.ID == intfId {
841 for _, onu := range pon.Onus {
842 if onu.ID == onuId {
843 return onu, nil
844 }
845 }
846 }
847 }
Shrey Baid688b4242020-07-10 20:40:10 +0530848 return &Onu{}, fmt.Errorf("cannot-find-onu-by-id-%v-%v", intfId, onuId)
William Kurkian9dadc5b2019-10-22 13:51:57 -0400849}
850
Matteo Scandolo4a036262020-08-17 15:56:13 -0700851// returns a Service with a given Mac Address
852func (o *OltDevice) FindServiceByMacAddress(mac net.HardwareAddr) (ServiceIf, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200853 // TODO this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700854 // memoizing it will remove the bottleneck
855 for _, pon := range o.Pons {
856 for _, onu := range pon.Onus {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700857 s, err := onu.findServiceByMacAddress(mac)
858 if err == nil {
859 return s, nil
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700860 }
861 }
862 }
863
Matteo Scandolo4a036262020-08-17 15:56:13 -0700864 return nil, fmt.Errorf("cannot-find-service-by-mac-address-%s", mac)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700865}
866
Matteo Scandolo4747d292019-08-05 11:50:18 -0700867// GRPC Endpoints
868
Shrey Baid688b4242020-07-10 20:40:10 +0530869func (o *OltDevice) ActivateOnu(context context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700870 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700871 "OnuSn": onuSnToString(onu.SerialNumber),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700872 }).Info("Received ActivateOnu call from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +0530873 publishEvent("ONU-activate-indication-received", int32(onu.IntfId), int32(onu.OnuId), onuSnToString(onu.SerialNumber))
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700874
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700875 pon, _ := o.GetPonById(onu.IntfId)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800876
877 // Initialize the resource maps for this ONU
878 olt.AllocIDs[onu.IntfId][onu.OnuId] = make(map[uint32]map[int32]map[uint64]bool)
879 olt.GemPortIDs[onu.IntfId][onu.OnuId] = make(map[uint32]map[int32]map[uint64]bool)
880
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700881 _onu, _ := pon.GetOnuBySn(onu.SerialNumber)
William Kurkian0418bc82019-11-06 12:16:24 -0500882 _onu.SetID(onu.OnuId)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700883
Matteo Scandolocedde462021-03-09 17:37:16 -0800884 if err := _onu.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700885 oltLogger.WithFields(log.Fields{
886 "IntfId": _onu.PonPortID,
887 "OnuSn": _onu.Sn(),
888 "OnuId": _onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800889 }).Infof("Failed to transition ONU to %s state: %s", OnuStateEnabled, err.Error())
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700890 }
891
892 // NOTE we need to immediately activate the ONU or the OMCI state machine won't start
893
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700894 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700895}
896
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800897func (o *OltDevice) DeactivateOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700898 oltLogger.Error("DeactivateOnu not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700899 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700900}
901
Shrey Baid688b4242020-07-10 20:40:10 +0530902func (o *OltDevice) DeleteOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Pragya Arya1cbefa42020-01-13 12:15:29 +0530903 oltLogger.WithFields(log.Fields{
904 "IntfId": onu.IntfId,
905 "OnuId": onu.OnuId,
906 }).Info("Received DeleteOnu call from VOLTHA")
907
908 pon, err := o.GetPonById(onu.IntfId)
909 if err != nil {
910 oltLogger.WithFields(log.Fields{
911 "OnuId": onu.OnuId,
912 "IntfId": onu.IntfId,
913 "err": err,
914 }).Error("Can't find PonPort")
915 }
916 _onu, err := pon.GetOnuById(onu.OnuId)
917 if err != nil {
918 oltLogger.WithFields(log.Fields{
919 "OnuId": onu.OnuId,
920 "IntfId": onu.IntfId,
921 "err": err,
922 }).Error("Can't find Onu")
923 }
924
Matteo Scandolocedde462021-03-09 17:37:16 -0800925 if err := _onu.InternalState.Event(OnuTxDisable); err != nil {
Pragya Arya1cbefa42020-01-13 12:15:29 +0530926 oltLogger.WithFields(log.Fields{
927 "IntfId": _onu.PonPortID,
928 "OnuSn": _onu.Sn(),
929 "OnuId": _onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800930 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDisabled, err.Error())
Hardik Windlassad790cb2020-06-17 21:26:22 +0530931 }
932
Hardik Windlassad790cb2020-06-17 21:26:22 +0530933 // ONU Re-Discovery
934 if o.InternalState.Current() == "enabled" && pon.InternalState.Current() == "enabled" {
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530935 go _onu.ReDiscoverOnu()
Pragya Arya1cbefa42020-01-13 12:15:29 +0530936 }
937
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700938 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700939}
940
Shrey Baid688b4242020-07-10 20:40:10 +0530941func (o *OltDevice) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700942 // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether?
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800943 oltLogger.WithFields(log.Fields{
944 "oltId": o.ID,
945 }).Info("Disabling OLT")
Pragya Arya324337e2020-02-20 14:35:08 +0530946 publishEvent("OLT-disable-received", -1, -1, "")
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800947
Matteo Scandolo401503a2019-12-11 14:48:14 -0800948 for _, pon := range o.Pons {
Pragya Arya2225f202020-01-29 18:05:01 +0530949 if pon.InternalState.Current() == "enabled" {
950 // disable PONs
Matteo Scandolof9d43412021-01-12 11:11:34 -0800951 msg := types.Message{
952 Type: types.PonIndication,
953 Data: types.PonIndicationMessage{
954 OperState: types.DOWN,
Pragya Arya2225f202020-01-29 18:05:01 +0530955 PonPortID: pon.ID,
956 },
957 }
958 o.channel <- msg
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800959 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800960 }
961
Matteo Scandolo401503a2019-12-11 14:48:14 -0800962 // Note that we are not disabling the NNI as the real OLT does not.
963 // The reason for that is in-band management
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800964
965 // disable OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -0800966 oltMsg := types.Message{
967 Type: types.OltIndication,
968 Data: types.OltIndicationMessage{
969 OperState: types.DOWN,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700970 },
971 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100972 o.channel <- oltMsg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700973 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700974}
975
Shrey Baid688b4242020-07-10 20:40:10 +0530976func (o *OltDevice) DisablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530977 oltLogger.Infof("DisablePonIf request received for PON %d", intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200978 ponID := intf.GetIntfId()
979 pon, _ := o.GetPonById(intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200980
Matteo Scandolof9d43412021-01-12 11:11:34 -0800981 msg := types.Message{
982 Type: types.PonIndication,
983 Data: types.PonIndicationMessage{
984 OperState: types.DOWN,
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200985 PonPortID: ponID,
986 },
987 }
988 o.channel <- msg
989
990 for _, onu := range pon.Onus {
991
Matteo Scandolof9d43412021-01-12 11:11:34 -0800992 onuIndication := types.OnuIndicationMessage{
993 OperState: types.DOWN,
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200994 PonPortID: ponID,
995 OnuID: onu.ID,
996 OnuSN: onu.SerialNumber,
997 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700998 onu.sendOnuIndication(onuIndication, o.OpenoltStream)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200999
1000 }
1001
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001002 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001003}
1004
Zdravko Bozakov681364d2019-11-10 14:28:46 +01001005func (o *OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001006 oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +05301007 publishEvent("OLT-enable-received", -1, -1, "")
Matteo Scandolo4747d292019-08-05 11:50:18 -07001008 o.Enable(stream)
1009 return nil
1010}
1011
Shrey Baid688b4242020-07-10 20:40:10 +05301012func (o *OltDevice) EnablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Hardik Windlassad790cb2020-06-17 21:26:22 +05301013 oltLogger.Infof("EnablePonIf request received for PON %d", intf.IntfId)
Pragya Arya2225f202020-01-29 18:05:01 +05301014 ponID := intf.GetIntfId()
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001015 pon, _ := o.GetPonById(intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001016
Matteo Scandolof9d43412021-01-12 11:11:34 -08001017 msg := types.Message{
1018 Type: types.PonIndication,
1019 Data: types.PonIndicationMessage{
1020 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +05301021 PonPortID: ponID,
1022 },
1023 }
1024 o.channel <- msg
1025
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001026 for _, onu := range pon.Onus {
1027
Matteo Scandolof9d43412021-01-12 11:11:34 -08001028 onuIndication := types.OnuIndicationMessage{
1029 OperState: types.UP,
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001030 PonPortID: ponID,
1031 OnuID: onu.ID,
1032 OnuSN: onu.SerialNumber,
1033 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001034 onu.sendOnuIndication(onuIndication, o.OpenoltStream)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001035
1036 }
1037
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001038 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001039}
1040
Shrey Baid688b4242020-07-10 20:40:10 +05301041func (o *OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001042 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001043 "IntfId": flow.AccessIntfId,
1044 "OnuId": flow.OnuId,
1045 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001046 "InnerVlan": flow.Classifier.IVid,
1047 "OuterVlan": flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001048 "FlowType": flow.FlowType,
1049 "FlowId": flow.FlowId,
1050 "UniID": flow.UniId,
1051 "PortNo": flow.PortNo,
Pragya Arya8bdb4532020-03-02 17:08:09 +05301052 }).Tracef("OLT receives FlowAdd")
1053
1054 flowKey := FlowKey{}
1055 if !o.enablePerf {
1056 flowKey = FlowKey{ID: flow.FlowId, Direction: flow.FlowType}
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001057 olt.Flows.Store(flowKey, *flow)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301058 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001059
1060 if flow.AccessIntfId == -1 {
1061 oltLogger.WithFields(log.Fields{
1062 "FlowId": flow.FlowId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001063 }).Debug("Adding OLT flow")
Jonathan Hartb5fc46a2020-03-31 16:42:31 -07001064 } else if flow.FlowType == "multicast" {
1065 oltLogger.WithFields(log.Fields{
Matteo Scandolo618a6582020-09-09 12:21:29 -07001066 "Cookie": flow.Cookie,
1067 "DstPort": flow.Classifier.DstPort,
1068 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
1069 "FlowId": flow.FlowId,
1070 "FlowType": flow.FlowType,
1071 "GemportId": flow.GemportId,
1072 "InnerVlan": flow.Classifier.IVid,
1073 "IntfId": flow.AccessIntfId,
1074 "IpProto": flow.Classifier.IpProto,
1075 "OnuId": flow.OnuId,
1076 "OuterVlan": flow.Classifier.OVid,
1077 "PortNo": flow.PortNo,
1078 "SrcPort": flow.Classifier.SrcPort,
1079 "UniID": flow.UniId,
1080 "ClassifierOPbits": flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001081 }).Debug("Adding OLT multicast flow")
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001082 } else {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001083 pon, err := o.GetPonById(uint32(flow.AccessIntfId))
Matteo Scandolo27428702019-10-11 16:21:16 -07001084 if err != nil {
1085 oltLogger.WithFields(log.Fields{
1086 "OnuId": flow.OnuId,
1087 "IntfId": flow.AccessIntfId,
1088 "err": err,
1089 }).Error("Can't find PonPort")
1090 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001091 onu, err := pon.GetOnuById(uint32(flow.OnuId))
Matteo Scandolo27428702019-10-11 16:21:16 -07001092 if err != nil {
1093 oltLogger.WithFields(log.Fields{
1094 "OnuId": flow.OnuId,
1095 "IntfId": flow.AccessIntfId,
1096 "err": err,
1097 }).Error("Can't find Onu")
Jonathan Hartb5fc46a2020-03-31 16:42:31 -07001098 return nil, err
Matteo Scandolo27428702019-10-11 16:21:16 -07001099 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301100 if !o.enablePerf {
1101 onu.Flows = append(onu.Flows, flowKey)
Pragya Arya1d5ffb82020-03-20 18:51:37 +05301102 // Generate event on first flow for ONU
1103 if len(onu.Flows) == 1 {
1104 publishEvent("Flow-add-received", int32(onu.PonPortID), int32(onu.ID), onuSnToString(onu.SerialNumber))
1105 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301106 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001107
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001108 // validate that the flow reference correct IDs (Alloc, Gem)
1109 if err := o.validateFlow(flow); err != nil {
1110 oltLogger.WithFields(log.Fields{
1111 "OnuId": flow.OnuId,
1112 "IntfId": flow.AccessIntfId,
1113 "Flow": flow,
1114 "SerialNumber": onu.Sn(),
1115 "err": err,
1116 }).Error("invalid-flow-for-onu")
1117 return nil, err
1118 }
1119
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001120 o.storeGemPortIdByFlow(flow)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001121 o.storeAllocId(flow)
1122
Matteo Scandolof9d43412021-01-12 11:11:34 -08001123 msg := types.Message{
1124 Type: types.FlowAdd,
1125 Data: types.OnuFlowUpdateMessage{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001126 PonPortID: pon.ID,
1127 OnuID: onu.ID,
1128 Flow: flow,
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001129 },
1130 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001131 onu.Channel <- msg
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001132 }
1133
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001134 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001135}
1136
Pragya Arya8bdb4532020-03-02 17:08:09 +05301137// FlowRemove request from VOLTHA
Shrey Baid688b4242020-07-10 20:40:10 +05301138func (o *OltDevice) FlowRemove(_ context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001139
Pragya Arya8bdb4532020-03-02 17:08:09 +05301140 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001141 "AllocId": flow.AllocId,
1142 "Cookie": flow.Cookie,
1143 "FlowId": flow.FlowId,
1144 "FlowType": flow.FlowType,
1145 "GemportId": flow.GemportId,
1146 "IntfId": flow.AccessIntfId,
1147 "OnuId": flow.OnuId,
1148 "PortNo": flow.PortNo,
1149 "UniID": flow.UniId,
1150 "ReplicateFlow": flow.ReplicateFlow,
1151 "PbitToGemport": flow.PbitToGemport,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001152 }).Debug("OLT receives FlowRemove")
Pragya Arya8bdb4532020-03-02 17:08:09 +05301153
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001154 olt.freeGemPortId(flow)
1155 olt.freeAllocId(flow)
1156
Pragya Arya8bdb4532020-03-02 17:08:09 +05301157 if !o.enablePerf { // remove only if flow were stored
1158 flowKey := FlowKey{
1159 ID: flow.FlowId,
1160 Direction: flow.FlowType,
1161 }
1162
1163 // Check if flow exists
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001164 storedFlowIntf, ok := o.Flows.Load(flowKey)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301165 if !ok {
1166 oltLogger.Errorf("Flow %v not found", flow)
1167 return new(openolt.Empty), status.Errorf(codes.NotFound, "Flow not found")
1168 }
1169
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001170 storedFlow := storedFlowIntf.(openolt.Flow)
1171
Pragya Arya8bdb4532020-03-02 17:08:09 +05301172 // if its ONU flow remove it from ONU also
1173 if storedFlow.AccessIntfId != -1 {
Matteo Scandolocedde462021-03-09 17:37:16 -08001174 pon, err := o.GetPonById(uint32(storedFlow.AccessIntfId))
1175 if err != nil {
1176 oltLogger.WithFields(log.Fields{
1177 "OnuId": storedFlow.OnuId,
1178 "IntfId": storedFlow.AccessIntfId,
1179 "PONs": olt.Pons,
1180 "err": err,
1181 }).Error("PON-port-not-found")
1182 return new(openolt.Empty), nil
1183 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301184 onu, err := pon.GetOnuById(uint32(storedFlow.OnuId))
1185 if err != nil {
1186 oltLogger.WithFields(log.Fields{
1187 "OnuId": storedFlow.OnuId,
1188 "IntfId": storedFlow.AccessIntfId,
1189 "err": err,
Matteo Scandolocedde462021-03-09 17:37:16 -08001190 }).Error("ONU-not-found")
Pragya Arya8bdb4532020-03-02 17:08:09 +05301191 return new(openolt.Empty), nil
1192 }
1193 onu.DeleteFlow(flowKey)
Pragya Arya1d5ffb82020-03-20 18:51:37 +05301194 publishEvent("Flow-remove-received", int32(onu.PonPortID), int32(onu.ID), onuSnToString(onu.SerialNumber))
Pragya Arya8bdb4532020-03-02 17:08:09 +05301195 }
1196
1197 // delete from olt flows
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001198 o.Flows.Delete(flowKey)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301199 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001200
1201 if flow.AccessIntfId == -1 {
1202 oltLogger.WithFields(log.Fields{
1203 "FlowId": flow.FlowId,
1204 }).Debug("Removing OLT flow")
1205 } else if flow.FlowType == "multicast" {
1206 oltLogger.WithFields(log.Fields{
1207 "FlowId": flow.FlowId,
1208 }).Debug("Removing OLT multicast flow")
1209 } else {
1210
1211 onu, err := o.GetOnuByFlowId(flow.FlowId)
1212 if err != nil {
1213 oltLogger.WithFields(log.Fields{
1214 "OnuId": flow.OnuId,
1215 "IntfId": flow.AccessIntfId,
1216 "err": err,
1217 }).Error("Can't find Onu")
1218 return nil, err
1219 }
1220
Matteo Scandolof9d43412021-01-12 11:11:34 -08001221 msg := types.Message{
1222 Type: types.FlowRemoved,
1223 Data: types.OnuFlowUpdateMessage{
Shrey Baid55f328c2020-07-07 19:20:42 +05301224 Flow: flow,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001225 },
1226 }
1227 onu.Channel <- msg
1228 }
1229
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001230 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001231}
1232
Shrey Baid688b4242020-07-10 20:40:10 +05301233func (o *OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo18859852020-01-15 13:33:57 -08001234 res := openolt.Heartbeat{HeartbeatSignature: uint32(time.Now().Unix())}
1235 oltLogger.WithFields(log.Fields{
1236 "signature": res.HeartbeatSignature,
1237 }).Trace("HeartbeatCheck")
1238 return &res, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001239}
1240
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001241func (o *OltDevice) GetOnuByFlowId(flowId uint64) (*Onu, error) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001242 for _, pon := range o.Pons {
1243 for _, onu := range pon.Onus {
1244 for _, fId := range onu.FlowIds {
1245 if fId == flowId {
1246 return onu, nil
1247 }
1248 }
1249 }
1250 }
Shrey Baid688b4242020-07-10 20:40:10 +05301251 return nil, fmt.Errorf("Cannot find Onu by flowId %d", flowId)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001252}
1253
Shrey Baid688b4242020-07-10 20:40:10 +05301254func (o *OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -07001255
Matteo Scandolocedde462021-03-09 17:37:16 -08001256 intfIDs := []uint32{}
1257 for i := 0; i < o.NumPon; i++ {
1258 intfIDs = append(intfIDs, uint32(i))
1259 }
1260
1261 devinfo := &openolt.DeviceInfo{
1262 Vendor: common.Config.Olt.Vendor,
1263 Model: common.Config.Olt.Model,
1264 HardwareVersion: common.Config.Olt.HardwareVersion,
1265 FirmwareVersion: common.Config.Olt.FirmwareVersion,
1266 Technology: common.Config.Olt.Technology,
1267 PonPorts: uint32(o.NumPon),
1268 OnuIdStart: onuIdStart,
1269 OnuIdEnd: onuIdEnd,
1270 AllocIdStart: allocIdStart,
1271 AllocIdEnd: allocIdEnd,
1272 GemportIdStart: gemportIdStart,
1273 GemportIdEnd: gemportIdEnd,
1274 FlowIdStart: flowIdStart,
1275 FlowIdEnd: flowIdEnd,
1276 DeviceSerialNumber: o.SerialNumber,
1277 DeviceId: common.Config.Olt.DeviceId,
1278 PreviouslyConnected: o.PreviouslyConnected,
1279 Ranges: []*openolt.DeviceInfo_DeviceResourceRanges{
1280 {
1281 IntfIds: intfIDs,
1282 Technology: common.Config.Olt.Technology,
1283 Pools: []*openolt.DeviceInfo_DeviceResourceRanges_Pool{
1284 {
1285 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID,
1286 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1287 Start: onuIdStart,
1288 End: onuIdEnd,
1289 },
1290 {
1291 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID,
1292 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1293 Start: allocIdStart,
1294 End: allocIdEnd,
1295 },
1296 {
1297 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID,
1298 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1299 Start: gemportIdStart,
1300 End: gemportIdEnd,
1301 },
1302 {
1303 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID,
1304 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH,
1305 Start: flowIdStart,
1306 End: flowIdEnd,
1307 },
1308 },
1309 },
1310 },
1311 }
Matteo Scandolo96f89192021-03-12 13:17:26 -08001312
1313 oltLogger.WithFields(log.Fields{
1314 "Vendor": devinfo.Vendor,
1315 "Model": devinfo.Model,
1316 "HardwareVersion": devinfo.HardwareVersion,
1317 "FirmwareVersion": devinfo.FirmwareVersion,
1318 "Technology": devinfo.Technology,
1319 "PonPorts": devinfo.PonPorts,
1320 "OnuIdStart": devinfo.OnuIdStart,
1321 "OnuIdEnd": devinfo.OnuIdEnd,
1322 "AllocIdStart": devinfo.AllocIdStart,
1323 "AllocIdEnd": devinfo.AllocIdEnd,
1324 "GemportIdStart": devinfo.GemportIdStart,
1325 "GemportIdEnd": devinfo.GemportIdEnd,
1326 "FlowIdStart": devinfo.FlowIdStart,
1327 "FlowIdEnd": devinfo.FlowIdEnd,
1328 "DeviceSerialNumber": devinfo.DeviceSerialNumber,
1329 "DeviceId": devinfo.DeviceId,
1330 "PreviouslyConnected": devinfo.PreviouslyConnected,
1331 }).Info("OLT receives GetDeviceInfo call from VOLTHA")
1332
1333 // once we connect, set the flag
1334 o.PreviouslyConnected = true
Matteo Scandolo4747d292019-08-05 11:50:18 -07001335
1336 return devinfo, nil
1337}
1338
Shrey Baid688b4242020-07-10 20:40:10 +05301339func (o *OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) {
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001340 pon, err := o.GetPonById(omci_msg.IntfId)
1341 if err != nil {
1342 oltLogger.WithFields(log.Fields{
Matteo Scandolof65e6872020-04-15 15:18:43 -07001343 "error": err,
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001344 "onu_id": omci_msg.OnuId,
1345 "pon_id": omci_msg.IntfId,
1346 }).Error("pon ID not found")
1347 return nil, err
1348 }
1349
1350 onu, err := pon.GetOnuById(omci_msg.OnuId)
1351 if err != nil {
1352 oltLogger.WithFields(log.Fields{
Matteo Scandolof65e6872020-04-15 15:18:43 -07001353 "error": err,
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001354 "onu_id": omci_msg.OnuId,
1355 "pon_id": omci_msg.IntfId,
1356 }).Error("onu ID not found")
1357 return nil, err
1358 }
1359
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001360 oltLogger.WithFields(log.Fields{
1361 "IntfId": onu.PonPortID,
1362 "OnuId": onu.ID,
1363 "OnuSn": onu.Sn(),
1364 }).Tracef("Received OmciMsgOut")
Matteo Scandolob5913142021-03-19 16:10:18 -07001365 omciPkt, omciMsg, err := omcilib.ParseOpenOltOmciPacket(omci_msg.Pkt)
1366 if err != nil {
1367 log.WithFields(log.Fields{
1368 "IntfId": onu.PonPortID,
1369 "SerialNumber": onu.Sn(),
1370 "omciPacket": omcilib.HexDecode(omci_msg.Pkt),
1371 "err": err.Error(),
1372 }).Error("cannot-parse-OMCI-packet")
1373 return nil, fmt.Errorf("olt-received-malformed-omci-packet")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001374 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001375 if onu.InternalState.Current() == OnuStateDisabled {
1376 // if the ONU is disabled just drop the message
1377 log.WithFields(log.Fields{
1378 "IntfId": onu.PonPortID,
1379 "SerialNumber": onu.Sn(),
1380 "omciBytes": hex.EncodeToString(omciPkt.Data()),
1381 "omciPkt": omciPkt,
1382 "omciMsgType": omciMsg.MessageType,
1383 }).Warn("dropping-omci-message")
1384 } else {
1385 msg := types.Message{
1386 Type: types.OMCI,
1387 Data: types.OmciMessage{
1388 OnuSN: onu.SerialNumber,
1389 OnuID: onu.ID,
1390 OmciMsg: omciMsg,
1391 OmciPkt: omciPkt,
1392 },
1393 }
1394 onu.Channel <- msg
1395 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001396 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001397}
1398
Shrey Baid688b4242020-07-10 20:40:10 +05301399func (o *OltDevice) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001400 pon, err := o.GetPonById(onuPkt.IntfId)
Matteo Scandolo27428702019-10-11 16:21:16 -07001401 if err != nil {
1402 oltLogger.WithFields(log.Fields{
1403 "OnuId": onuPkt.OnuId,
1404 "IntfId": onuPkt.IntfId,
1405 "err": err,
1406 }).Error("Can't find PonPort")
1407 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001408 onu, err := pon.GetOnuById(onuPkt.OnuId)
Matteo Scandolo27428702019-10-11 16:21:16 -07001409 if err != nil {
1410 oltLogger.WithFields(log.Fields{
1411 "OnuId": onuPkt.OnuId,
1412 "IntfId": onuPkt.IntfId,
1413 "err": err,
1414 }).Error("Can't find Onu")
1415 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001416
Matteo Scandolo075b1892019-10-07 12:11:07 -07001417 oltLogger.WithFields(log.Fields{
1418 "IntfId": onu.PonPortID,
1419 "OnuId": onu.ID,
1420 "OnuSn": onu.Sn(),
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001421 "Packet": hex.EncodeToString(onuPkt.Pkt),
Matteo Scandolo75ed5b92020-09-03 09:03:16 -07001422 }).Trace("Received OnuPacketOut")
Matteo Scandolo075b1892019-10-07 12:11:07 -07001423
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001424 rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default)
Matteo Scandolo618a6582020-09-09 12:21:29 -07001425
1426 pktType, err := packetHandlers.GetPktType(rawpkt)
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001427 if err != nil {
1428 onuLogger.WithFields(log.Fields{
1429 "IntfId": onu.PonPortID,
1430 "OnuId": onu.ID,
1431 "OnuSn": onu.Sn(),
Matteo Scandolo618a6582020-09-09 12:21:29 -07001432 "Pkt": hex.EncodeToString(rawpkt.Data()),
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001433 }).Error("Can't find pktType in packet, droppint it")
1434 return new(openolt.Empty), nil
1435 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001436
Matteo Scandolo4a036262020-08-17 15:56:13 -07001437 pktMac, err := packetHandlers.GetDstMacAddressFromPacket(rawpkt)
Matteo Scandolo4a036262020-08-17 15:56:13 -07001438 if err != nil {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001439 onuLogger.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -07001440 "IntfId": onu.PonPortID,
1441 "OnuId": onu.ID,
1442 "OnuSn": onu.Sn(),
1443 "Pkt": rawpkt.Data(),
1444 }).Error("Can't find Dst MacAddress in packet, droppint it")
1445 return new(openolt.Empty), nil
1446 }
1447
Matteo Scandolof9d43412021-01-12 11:11:34 -08001448 msg := types.Message{
1449 Type: types.OnuPacketOut,
1450 Data: types.OnuPacketMessage{
Matteo Scandolo4a036262020-08-17 15:56:13 -07001451 IntfId: onuPkt.IntfId,
1452 OnuId: onuPkt.OnuId,
1453 Packet: rawpkt,
1454 Type: pktType,
1455 MacAddress: pktMac,
Matteo Scandolo075b1892019-10-07 12:11:07 -07001456 },
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001457 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001458
Matteo Scandolo075b1892019-10-07 12:11:07 -07001459 onu.Channel <- msg
1460
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001461 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001462}
1463
Shrey Baid688b4242020-07-10 20:40:10 +05301464func (o *OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo635b2bf2020-09-04 10:23:40 -07001465
1466 // OLT Reboot is called in two cases:
1467 // - 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)
1468 // - when an OLT needs to be rebooted (voltcl device reboot)
1469
Matteo Scandolod02b79b2019-12-05 16:42:13 -08001470 oltLogger.WithFields(log.Fields{
1471 "oltId": o.ID,
1472 }).Info("Shutting down")
Pragya Arya324337e2020-02-20 14:35:08 +05301473 publishEvent("OLT-reboot-received", -1, -1, "")
Shrey Baid688b4242020-07-10 20:40:10 +05301474 go func() { _ = o.RestartOLT() }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001475 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001476}
1477
Shrey Baid688b4242020-07-10 20:40:10 +05301478func (o *OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Pragya Arya6a708d62020-01-01 17:17:20 +05301479 oltLogger.WithFields(log.Fields{
1480 "oltId": o.ID,
1481 }).Info("Received ReenableOlt request from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +05301482 publishEvent("OLT-reenable-received", -1, -1, "")
Pragya Arya6a708d62020-01-01 17:17:20 +05301483
Pragya Arya2225f202020-01-29 18:05:01 +05301484 // enable OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -08001485 oltMsg := types.Message{
1486 Type: types.OltIndication,
1487 Data: types.OltIndicationMessage{
1488 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +05301489 },
Pragya Arya1881df02020-01-29 18:05:01 +05301490 }
Pragya Arya2225f202020-01-29 18:05:01 +05301491 o.channel <- oltMsg
Pragya Arya6a708d62020-01-01 17:17:20 +05301492
Pragya Arya2225f202020-01-29 18:05:01 +05301493 for _, pon := range o.Pons {
1494 if pon.InternalState.Current() == "disabled" {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001495 msg := types.Message{
1496 Type: types.PonIndication,
1497 Data: types.PonIndicationMessage{
1498 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +05301499 PonPortID: pon.ID,
1500 },
1501 }
1502 o.channel <- msg
1503 }
1504 }
Matteo Scandoloe60a5052020-02-07 00:31:14 +00001505
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001506 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001507}
1508
Shrey Baid688b4242020-07-10 20:40:10 +05301509func (o *OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001510 pkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
1511
Matteo Scandolo90d08f62020-10-29 12:06:55 -07001512 err := o.Nnis[0].handleNniPacket(pkt) // FIXME we are assuming we have only one NNI
1513
1514 if err != nil {
1515 return nil, err
1516 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001517 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001518}
1519
Shrey Baid688b4242020-07-10 20:40:10 +05301520func (o *OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001521 oltLogger.Error("CollectStatistics not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001522 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001523}
1524
Shrey Baid688b4242020-07-10 20:40:10 +05301525func (o *OltDevice) GetOnuInfo(context context.Context, packet *openolt.Onu) (*openolt.OnuIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001526 oltLogger.Error("GetOnuInfo not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001527 return new(openolt.OnuIndication), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001528}
1529
Shrey Baid688b4242020-07-10 20:40:10 +05301530func (o *OltDevice) GetPonIf(context context.Context, packet *openolt.Interface) (*openolt.IntfIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001531 oltLogger.Error("GetPonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001532 return new(openolt.IntfIndication), nil
Matteo Scandolod54283a2019-08-13 16:22:31 -07001533}
1534
Shrey Baid688b4242020-07-10 20:40:10 +05301535func (s *OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001536 oltLogger.Info("received CreateTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001537 return new(openolt.Empty), nil
1538}
1539
Shrey Baid688b4242020-07-10 20:40:10 +05301540func (s *OltDevice) RemoveTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001541 oltLogger.Info("received RemoveTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001542 return new(openolt.Empty), nil
1543}
1544
Shrey Baid688b4242020-07-10 20:40:10 +05301545func (s *OltDevice) CreateTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Anand S Katti09541352020-01-29 15:54:01 +05301546 oltLogger.WithFields(log.Fields{
1547 "OnuId": trafficSchedulers.OnuId,
1548 "IntfId": trafficSchedulers.IntfId,
1549 "OnuPortNo": trafficSchedulers.PortNo,
1550 }).Info("received CreateTrafficSchedulers")
1551
1552 if !s.enablePerf {
1553 pon, err := s.GetPonById(trafficSchedulers.IntfId)
1554 if err != nil {
1555 oltLogger.Errorf("Error retrieving PON by IntfId: %v", err)
1556 return new(openolt.Empty), err
1557 }
1558 onu, err := pon.GetOnuById(trafficSchedulers.OnuId)
1559 if err != nil {
1560 oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err)
1561 return new(openolt.Empty), err
1562 }
1563 onu.TrafficSchedulers = trafficSchedulers
1564 }
Matteo Scandolod54283a2019-08-13 16:22:31 -07001565 return new(openolt.Empty), nil
1566}
1567
Shrey Baid688b4242020-07-10 20:40:10 +05301568func (s *OltDevice) RemoveTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Anand S Katti09541352020-01-29 15:54:01 +05301569 oltLogger.WithFields(log.Fields{
1570 "OnuId": trafficSchedulers.OnuId,
1571 "IntfId": trafficSchedulers.IntfId,
1572 "OnuPortNo": trafficSchedulers.PortNo,
1573 }).Info("received RemoveTrafficSchedulers")
1574 if !s.enablePerf {
1575 pon, err := s.GetPonById(trafficSchedulers.IntfId)
1576 if err != nil {
1577 oltLogger.Errorf("Error retrieving PON by IntfId: %v", err)
1578 return new(openolt.Empty), err
1579 }
1580 onu, err := pon.GetOnuById(trafficSchedulers.OnuId)
1581 if err != nil {
1582 oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err)
1583 return new(openolt.Empty), err
1584 }
1585
1586 onu.TrafficSchedulers = nil
1587 }
Matteo Scandolod54283a2019-08-13 16:22:31 -07001588 return new(openolt.Empty), nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001589}
Scott Baker41724b82020-01-21 19:54:53 -08001590
Matteo Scandolo618a6582020-09-09 12:21:29 -07001591func (o *OltDevice) PerformGroupOperation(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) {
1592 oltLogger.WithFields(log.Fields{
1593 "GroupId": group.GroupId,
1594 "Command": group.Command,
1595 "Members": group.Members,
1596 "Action": group.Action,
1597 }).Debug("received PerformGroupOperation")
1598 return &openolt.Empty{}, nil
1599}
1600
1601func (o *OltDevice) DeleteGroup(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) {
1602 oltLogger.WithFields(log.Fields{
1603 "GroupId": group.GroupId,
1604 "Command": group.Command,
1605 "Members": group.Members,
1606 "Action": group.Action,
1607 }).Debug("received PerformGroupOperation")
1608 return &openolt.Empty{}, nil
1609}
1610
1611func (o *OltDevice) GetExtValue(ctx context.Context, in *openolt.ValueParam) (*common_protos.ReturnValues, error) {
1612 return &common_protos.ReturnValues{}, nil
1613}
1614
1615func (o *OltDevice) OnuItuPonAlarmSet(ctx context.Context, in *config.OnuItuPonAlarm) (*openolt.Empty, error) {
1616 return &openolt.Empty{}, nil
1617}
1618
1619func (o *OltDevice) GetLogicalOnuDistanceZero(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) {
1620 return &openolt.OnuLogicalDistance{}, nil
1621}
1622
1623func (o *OltDevice) GetLogicalOnuDistance(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) {
1624 return &openolt.OnuLogicalDistance{}, nil
1625}
Matteo Scandolo96f89192021-03-12 13:17:26 -08001626
1627func (o *OltDevice) GetGemPortStatistics(ctx context.Context, in *openolt.OnuPacket) (*openolt.GemPortStatistics, error) {
1628 return &openolt.GemPortStatistics{}, nil
1629}
1630
1631func (o *OltDevice) GetOnuStatistics(ctx context.Context, in *openolt.Onu) (*openolt.OnuStatistics, error) {
1632 return &openolt.OnuStatistics{}, nil
1633}
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001634
1635func (o *OltDevice) storeAllocId(flow *openolt.Flow) {
1636 o.AllocIDsLock.Lock()
1637 defer o.AllocIDsLock.Unlock()
1638
Matteo Scandolo21195d62021-04-07 14:31:23 -07001639 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)]; !ok {
1640 oltLogger.WithFields(log.Fields{
1641 "IntfId": flow.AccessIntfId,
1642 "OnuId": flow.OnuId,
1643 "PortNo": flow.PortNo,
1644 "GemportId": flow.GemportId,
1645 "FlowId": flow.FlowId,
1646 }).Error("trying-to-store-alloc-id-for-unknown-onu")
1647 }
1648
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001649 oltLogger.WithFields(log.Fields{
Matteo Scandolo21195d62021-04-07 14:31:23 -07001650 "IntfId": flow.AccessIntfId,
1651 "OnuId": flow.OnuId,
1652 "PortNo": flow.PortNo,
1653 "GemportId": flow.GemportId,
1654 "FlowId": flow.FlowId,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001655 }).Trace("storing-alloc-id-via-flow")
1656
1657 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo]; !ok {
1658 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo] = make(map[int32]map[uint64]bool)
1659 }
1660 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId]; !ok {
1661 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId] = make(map[uint64]bool)
1662 }
1663 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId][flow.FlowId] = true
1664}
1665
1666func (o *OltDevice) freeAllocId(flow *openolt.Flow) {
1667 // if this is the last flow referencing the AllocId then remove it
1668 o.AllocIDsLock.Lock()
1669 defer o.AllocIDsLock.Unlock()
1670
1671 oltLogger.WithFields(log.Fields{
1672 "IntfId": flow.AccessIntfId,
1673 "OnuId": flow.OnuId,
1674 "PortNo": flow.PortNo,
1675 "GemportId": flow.GemportId,
1676 }).Trace("freeing-alloc-id-via-flow")
1677
1678 // NOTE look at the freeGemPortId implementation for comments and context
1679 for ponId, ponValues := range o.AllocIDs {
1680 for onuId, onuValues := range ponValues {
1681 for uniId, uniValues := range onuValues {
1682 for allocId, flows := range uniValues {
1683 for flowId := range flows {
1684 // if the flow matches, remove it from the map.
1685 if flow.FlowId == flowId {
1686 delete(o.AllocIDs[ponId][onuId][uniId][allocId], flow.FlowId)
1687 }
1688 // if that was the last flow for a particular allocId, remove the entire allocId
1689 if len(o.AllocIDs[ponId][onuId][uniId][allocId]) == 0 {
1690 delete(o.AllocIDs[ponId][onuId][uniId], allocId)
1691 }
1692 }
1693 }
1694 }
1695 }
1696 }
1697}
1698
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001699func (o *OltDevice) storeGemPortId(ponId uint32, onuId uint32, portNo uint32, gemId int32, flowId uint64) {
Matteo Scandolo21195d62021-04-07 14:31:23 -07001700 o.GemPortIDsLock.Lock()
1701 defer o.GemPortIDsLock.Unlock()
1702
1703 if _, ok := o.GemPortIDs[ponId][onuId]; !ok {
1704 oltLogger.WithFields(log.Fields{
1705 "IntfId": ponId,
1706 "OnuId": onuId,
1707 "PortNo": portNo,
1708 "GemportId": gemId,
1709 "FlowId": flowId,
1710 }).Error("trying-to-store-gemport-for-unknown-onu")
1711 }
1712
1713 oltLogger.WithFields(log.Fields{
1714 "IntfId": ponId,
1715 "OnuId": onuId,
1716 "PortNo": portNo,
1717 "GemportId": gemId,
1718 "FlowId": flowId,
1719 }).Trace("storing-alloc-id-via-flow")
1720
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001721 if _, ok := o.GemPortIDs[ponId][onuId][portNo]; !ok {
1722 o.GemPortIDs[ponId][onuId][portNo] = make(map[int32]map[uint64]bool)
1723 }
1724 if _, ok := o.GemPortIDs[ponId][onuId][portNo][gemId]; !ok {
1725 o.GemPortIDs[ponId][onuId][portNo][gemId] = make(map[uint64]bool)
1726 }
1727 o.GemPortIDs[ponId][onuId][portNo][gemId][flowId] = true
1728}
1729
1730func (o *OltDevice) storeGemPortIdByFlow(flow *openolt.Flow) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001731 oltLogger.WithFields(log.Fields{
Matteo Scandolo21195d62021-04-07 14:31:23 -07001732 "IntfId": flow.AccessIntfId,
1733 "OnuId": flow.OnuId,
1734 "PortNo": flow.PortNo,
1735 "GemportId": flow.GemportId,
1736 "FlowId": flow.FlowId,
1737 "ReplicateFlow": flow.ReplicateFlow,
1738 "PbitToGemport": flow.PbitToGemport,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001739 }).Trace("storing-gem-port-id-via-flow")
1740
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001741 if flow.ReplicateFlow {
1742 for _, gem := range flow.PbitToGemport {
1743 o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, int32(gem), flow.FlowId)
1744 }
1745 } else {
1746 o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, flow.GemportId, flow.FlowId)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001747 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001748}
1749
1750func (o *OltDevice) freeGemPortId(flow *openolt.Flow) {
1751 // if this is the last flow referencing the GemPort then remove it
1752 o.GemPortIDsLock.Lock()
1753 defer o.GemPortIDsLock.Unlock()
1754
1755 oltLogger.WithFields(log.Fields{
1756 "IntfId": flow.AccessIntfId,
1757 "OnuId": flow.OnuId,
1758 "PortNo": flow.PortNo,
1759 "GemportId": flow.GemportId,
1760 }).Trace("freeing-gem-port-id-via-flow")
1761
1762 // NOTE that this loop is not very performant, it would be better if the flow carries
1763 // the same information that it carries during a FlowAdd. If so we can directly remove
1764 // items from the map
1765
1766 //delete(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.GemportId], flow.FlowId)
1767 //if len(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.GemportId]) == 0 {
1768 // delete(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo], flow.GemportId)
1769 //}
1770
1771 // NOTE this loop assumes that flow IDs are unique per device
1772 for ponId, ponValues := range o.GemPortIDs {
1773 for onuId, onuValues := range ponValues {
1774 for uniId, uniValues := range onuValues {
1775 for gemId, flows := range uniValues {
1776 for flowId := range flows {
1777 // if the flow matches, remove it from the map.
1778 if flow.FlowId == flowId {
1779 delete(o.GemPortIDs[ponId][onuId][uniId][gemId], flow.FlowId)
1780 }
1781 // if that was the last flow for a particular gem, remove the entire gem
1782 if len(o.GemPortIDs[ponId][onuId][uniId][gemId]) == 0 {
1783 delete(o.GemPortIDs[ponId][onuId][uniId], gemId)
1784 }
1785 }
1786 }
1787 }
1788 }
1789 }
1790}
1791
1792// validateFlow checks that:
1793// - the AllocId is not used in any flow referencing other ONUs/UNIs on the same PON
1794// - the GemPortId is not used in any flow referencing other ONUs/UNIs on the same PON
1795func (o *OltDevice) validateFlow(flow *openolt.Flow) error {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001796 // validate gemPort
1797 o.GemPortIDsLock.RLock()
Matteo Scandolo21195d62021-04-07 14:31:23 -07001798 defer o.GemPortIDsLock.RUnlock()
1799 for onuId, onu := range o.GemPortIDs[uint32(flow.AccessIntfId)] {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001800 if onuId == uint32(flow.OnuId) {
1801 continue
1802 }
1803 for uniId, uni := range onu {
1804 for gem := range uni {
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001805 if flow.ReplicateFlow {
1806 for _, flowGem := range flow.PbitToGemport {
1807 if gem == int32(flowGem) {
1808 return fmt.Errorf("gem-%d-already-in-use-on-uni-%d-onu-%d-replicated-flow-%d", gem, uniId, onuId, flow.FlowId)
1809 }
1810 }
1811 } else {
1812 if gem == flow.GemportId {
1813 return fmt.Errorf("gem-%d-already-in-use-on-uni-%d-onu-%d-flow-%d", gem, uniId, onuId, flow.FlowId)
1814 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001815 }
1816 }
1817 }
1818 }
1819
1820 o.AllocIDsLock.RLock()
Matteo Scandolo21195d62021-04-07 14:31:23 -07001821 defer o.AllocIDsLock.RUnlock()
1822 for onuId, onu := range o.AllocIDs[uint32(flow.AccessIntfId)] {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001823 if onuId == uint32(flow.OnuId) {
1824 continue
1825 }
1826 for uniId, uni := range onu {
1827 for allocId := range uni {
1828 if allocId == flow.AllocId {
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001829 return fmt.Errorf("allocId-%d-already-in-use-on-uni-%d-onu-%d-flow-%d", allocId, uniId, onuId, flow.FlowId)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001830 }
1831 }
1832 }
1833 }
1834
1835 return nil
1836}
1837
1838// clearAllResources is invoked up OLT Reboot to remove all the allocated
1839// GemPorts, AllocId and ONU-IDs across the PONs
1840func (o *OltDevice) clearAllResources() {
1841
1842 // remove the resources received via flows
1843 o.GemPortIDsLock.Lock()
1844 o.GemPortIDs = make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool)
1845 o.GemPortIDsLock.Unlock()
1846 o.AllocIDsLock.Lock()
1847 o.AllocIDs = make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool)
1848 o.AllocIDsLock.Unlock()
1849
1850 // remove the resources received via OMCI
1851 for _, pon := range o.Pons {
1852 pon.removeAllAllocIds()
1853 pon.removeAllGemPorts()
1854 pon.removeAllOnuIds()
1855 }
1856}