blob: 6ad068dd01d5ac1400727c6e19907fd616746c15 [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 Scandolo90d08f62020-10-29 12:06:55 -070023 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
Matteo Scandolof9d43412021-01-12 11:11:34 -080024 "github.com/opencord/bbsim/internal/bbsim/types"
Matteo Scandolob5913142021-03-19 16:10:18 -070025 omcilib "github.com/opencord/bbsim/internal/common/omci"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070026 "github.com/opencord/voltha-protos/v4/go/ext/config"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020027 "net"
Matteo Scandolof9d43412021-01-12 11:11:34 -080028 "strconv"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020029 "sync"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010030 "time"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020031
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070032 "github.com/google/gopacket"
33 "github.com/google/gopacket/layers"
Matteo Scandolo4747d292019-08-05 11:50:18 -070034 "github.com/looplab/fsm"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070035 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010036 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070037 common_protos "github.com/opencord/voltha-protos/v4/go/common"
38 "github.com/opencord/voltha-protos/v4/go/openolt"
39 "github.com/opencord/voltha-protos/v4/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070040 log "github.com/sirupsen/logrus"
41 "google.golang.org/grpc"
Pragya Arya8bdb4532020-03-02 17:08:09 +053042 "google.golang.org/grpc/codes"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010043 "google.golang.org/grpc/reflection"
Pragya Arya8bdb4532020-03-02 17:08:09 +053044 "google.golang.org/grpc/status"
Matteo Scandolo4747d292019-08-05 11:50:18 -070045)
46
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070047var oltLogger = log.WithFields(log.Fields{
Matteo Scandolo84f7d482019-08-08 19:00:47 -070048 "module": "OLT",
49})
50
Matteo Scandolocedde462021-03-09 17:37:16 -080051const (
52 onuIdStart = 1
53 onuIdEnd = 127
54 allocIdStart = 1024
55 allocIdEnd = 16383
56 gemportIdStart = 1024
57 gemportIdEnd = 65535
58 flowIdStart = 1
59 flowIdEnd = 65535
60)
61
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070062type OltDevice struct {
David Bainbridge103cf022019-12-16 20:11:35 +000063 sync.Mutex
Hardik Windlassefdb4b62021-03-18 10:33:24 +000064 OltServer *grpc.Server
David Bainbridge103cf022019-12-16 20:11:35 +000065
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070066 // BBSIM Internals
Pragya Arya2225f202020-01-29 18:05:01 +053067 ID int
68 SerialNumber string
69 NumNni int
70 NumPon int
71 NumOnuPerPon int
72 InternalState *fsm.FSM
Matteo Scandolof9d43412021-01-12 11:11:34 -080073 channel chan types.Message
Matteo Scandolo90d08f62020-10-29 12:06:55 -070074 dhcpServer dhcp.DHCPServerIf
Andrea Campanellabe8e12f2020-12-14 18:43:41 +010075 Flows sync.Map
Pragya Arya2225f202020-01-29 18:05:01 +053076 Delay int
77 ControlledActivation mode
Pragya Arya324337e2020-02-20 14:35:08 +053078 EventChannel chan common.Event
79 PublishEvents bool
Pragya Arya996a0892020-03-09 21:47:52 +053080 PortStatsInterval int
Matteo Scandolo96f89192021-03-12 13:17:26 -080081 PreviouslyConnected bool
Matteo Scandoloe33447a2019-10-31 12:38:23 -070082
Matteo Scandolo27428702019-10-11 16:21:16 -070083 Pons []*PonPort
84 Nnis []*NniPort
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070085
86 // OLT Attributes
87 OperState *fsm.FSM
David Bainbridge103cf022019-12-16 20:11:35 +000088
89 enableContext context.Context
90 enableContextCancel context.CancelFunc
Pragya Arya1cbefa42020-01-13 12:15:29 +053091
Matteo Scandolo4a036262020-08-17 15:56:13 -070092 OpenoltStream openolt.Openolt_EnableIndicationServer
Anand S Katti09541352020-01-29 15:54:01 +053093 enablePerf bool
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080094
95 // Allocated Resources
96 // this data are to verify that the openolt adapter does not duplicate resources
97 AllocIDsLock sync.RWMutex
98 AllocIDs map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool // map[ponPortId]map[OnuId]map[PortNo]map[AllocIds]map[FlowId]bool
99 GemPortIDsLock sync.RWMutex
100 GemPortIDs map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool // map[ponPortId]map[OnuId]map[PortNo]map[GemPortIDs]map[FlowId]bool
Matteo Scandolo4747d292019-08-05 11:50:18 -0700101}
102
Matteo Scandolo27428702019-10-11 16:21:16 -0700103var olt OltDevice
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700104
Matteo Scandolo27428702019-10-11 16:21:16 -0700105func GetOLT() *OltDevice {
106 return &olt
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700107}
108
Matteo Scandolo4a036262020-08-17 15:56:13 -0700109func CreateOLT(options common.GlobalConfig, services []common.ServiceYaml, isMock bool) *OltDevice {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700110 oltLogger.WithFields(log.Fields{
Pragya Arya996a0892020-03-09 21:47:52 +0530111 "ID": options.Olt.ID,
112 "NumNni": options.Olt.NniPorts,
113 "NumPon": options.Olt.PonPorts,
114 "NumOnuPerPon": options.Olt.OnusPonPort,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700115 }).Debug("CreateOLT")
116
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700117 olt = OltDevice{
Pragya Arya996a0892020-03-09 21:47:52 +0530118 ID: options.Olt.ID,
119 SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", options.Olt.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700120 OperState: getOperStateFSM(func(e *fsm.Event) {
121 oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst)
122 }),
Matteo Scandolo96f89192021-03-12 13:17:26 -0800123 NumNni: int(options.Olt.NniPorts),
124 NumPon: int(options.Olt.PonPorts),
125 NumOnuPerPon: int(options.Olt.OnusPonPort),
126 Pons: []*PonPort{},
127 Nnis: []*NniPort{},
128 Delay: options.BBSim.Delay,
129 enablePerf: options.BBSim.EnablePerf,
130 PublishEvents: options.BBSim.Events,
131 PortStatsInterval: options.Olt.PortStatsInterval,
132 dhcpServer: dhcp.NewDHCPServer(),
133 PreviouslyConnected: false,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800134 AllocIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool),
135 GemPortIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700136 }
137
Pragya Arya996a0892020-03-09 21:47:52 +0530138 if val, ok := ControlledActivationModes[options.BBSim.ControlledActivation]; ok {
Pragya Arya2225f202020-01-29 18:05:01 +0530139 olt.ControlledActivation = val
140 } else {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700141 // FIXME throw an error if the ControlledActivation is not valid
Pragya Arya2225f202020-01-29 18:05:01 +0530142 oltLogger.Warn("Unknown ControlledActivation Mode given, running in Default mode")
143 olt.ControlledActivation = Default
144 }
145
Matteo Scandolo4747d292019-08-05 11:50:18 -0700146 // OLT State machine
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700147 // NOTE do we need 2 state machines for the OLT? (InternalState and OperState)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700148 olt.InternalState = fsm.NewFSM(
149 "created",
150 fsm.Events{
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800151 {Name: "initialize", Src: []string{"created", "deleted"}, Dst: "initialized"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100152 {Name: "enable", Src: []string{"initialized", "disabled"}, Dst: "enabled"},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700153 {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"},
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700154 // delete event in enabled state below is for reboot OLT case.
Mahir Gunyel6dad4452020-01-06 12:59:04 -0800155 {Name: "delete", Src: []string{"disabled", "enabled"}, Dst: "deleted"},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700156 },
157 fsm.Callbacks{
158 "enter_state": func(e *fsm.Event) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700159 oltLogger.Debugf("Changing OLT InternalState from %s to %s", e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700160 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100161 "enter_initialized": func(e *fsm.Event) { olt.InitOlt() },
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800162 "enter_deleted": func(e *fsm.Event) {
163 // remove all the resource allocations
164 olt.clearAllResources()
165 },
Matteo Scandolo4747d292019-08-05 11:50:18 -0700166 },
167 )
168
Shrey Baid688b4242020-07-10 20:40:10 +0530169 if !isMock {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700170 // create NNI Port
171 nniPort, err := CreateNNI(&olt)
172 if err != nil {
173 oltLogger.Fatalf("Couldn't create NNI Port: %v", err)
174 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700175
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700176 olt.Nnis = append(olt.Nnis, &nniPort)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700177 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700178
Matteo Scandolo4a036262020-08-17 15:56:13 -0700179 // Create device and Services
180
181 nextCtag := map[string]int{}
182 nextStag := map[string]int{}
183
Matteo Scandolo4747d292019-08-05 11:50:18 -0700184 // create PON ports
Matteo Scandolo4a036262020-08-17 15:56:13 -0700185 for i := 0; i < olt.NumPon; i++ {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800186
187 // initialize the resource maps for every PON Ports
188 olt.AllocIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
189 olt.GemPortIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
190
Matteo Scandolo4a036262020-08-17 15:56:13 -0700191 p := CreatePonPort(&olt, uint32(i))
Matteo Scandolo4747d292019-08-05 11:50:18 -0700192
Matteo Scandolo4a036262020-08-17 15:56:13 -0700193 // create ONU devices
194 for j := 0; j < olt.NumOnuPerPon; j++ {
195 delay := time.Duration(olt.Delay*j) * time.Millisecond
196 o := CreateONU(&olt, p, uint32(j+1), delay, isMock)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700197
Matteo Scandolo4a036262020-08-17 15:56:13 -0700198 for k, s := range common.Services {
199
200 // find the correct cTag for this service
201 if _, ok := nextCtag[s.Name]; !ok {
202 // it's the first time we iterate over this service,
203 // so we start from the config value
204 nextCtag[s.Name] = s.CTag
205 } else {
206 // we have a previous value, so we check it
207 // if Allocation is unique, we increment,
208 // otherwise (shared) we do nothing
209 if s.CTagAllocation == common.TagAllocationUnique.String() {
210 nextCtag[s.Name] = nextCtag[s.Name] + 1
211 }
212 }
213
214 // find the correct sTag for this service
215 if _, ok := nextStag[s.Name]; !ok {
216 nextStag[s.Name] = s.STag
217 } else {
218 if s.STagAllocation == common.TagAllocationUnique.String() {
219 nextStag[s.Name] = nextStag[s.Name] + 1
220 }
221 }
222
223 mac := net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(p.ID), byte(o.ID), byte(k)}
224 service, err := NewService(s.Name, mac, o, nextCtag[s.Name], nextStag[s.Name],
225 s.NeedsEapol, s.NeedsDchp, s.NeedsIgmp, s.TechnologyProfileID, s.UniTagMatch,
226 s.ConfigureMacAddress, s.UsPonCTagPriority, s.UsPonSTagPriority, s.DsPonCTagPriority, s.DsPonSTagPriority)
227
228 if err != nil {
229 oltLogger.WithFields(log.Fields{
230 "Err": err.Error(),
231 }).Fatal("Can't create Service")
232 }
233
234 o.Services = append(o.Services, service)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700235 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700236 p.Onus = append(p.Onus, o)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700237 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700238 olt.Pons = append(olt.Pons, p)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700239 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100240
Shrey Baid688b4242020-07-10 20:40:10 +0530241 if !isMock {
Matteo Scandolod32c3822019-11-26 15:57:46 -0700242 if err := olt.InternalState.Event("initialize"); err != nil {
243 log.Errorf("Error initializing OLT: %v", err)
244 return nil
245 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100246 }
247
Pragya Arya324337e2020-02-20 14:35:08 +0530248 if olt.PublishEvents {
249 log.Debugf("BBSim event publishing is enabled")
250 // Create a channel to write event messages
251 olt.EventChannel = make(chan common.Event, 100)
252 }
253
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700254 return &olt
255}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700256
Shrey Baid688b4242020-07-10 20:40:10 +0530257func (o *OltDevice) InitOlt() {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100258
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000259 if o.OltServer == nil {
260 o.OltServer, _ = o.StartOltServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100261 } else {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800262 oltLogger.Fatal("OLT server already running.")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100263 }
264
265 // create new channel for processOltMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800266 o.channel = make(chan types.Message)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100267
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100268 // FIXME we are assuming we have only one NNI
269 if o.Nnis[0] != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800270 // NOTE we want to make sure the state is down when we initialize the OLT,
271 // the NNI may be in a bad state after a disable/reboot as we are not disabling it for
272 // in-band management
273 o.Nnis[0].OperState.SetState("down")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100274 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800275
276 for ponId := range o.Pons {
277 // initialize the resource maps for every PON Ports
278 olt.AllocIDs[uint32(ponId)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
279 olt.GemPortIDs[uint32(ponId)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
280 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700281}
282
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800283func (o *OltDevice) RestartOLT() error {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100284
Matteo Scandolo96f89192021-03-12 13:17:26 -0800285 o.PreviouslyConnected = false
286
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700287 softReboot := false
Matteo Scandolo4a036262020-08-17 15:56:13 -0700288 rebootDelay := common.Config.Olt.OltRebootDelay
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800289
290 oltLogger.WithFields(log.Fields{
291 "oltId": o.ID,
292 }).Infof("Simulating OLT restart... (%ds)", rebootDelay)
293
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700294 if o.InternalState.Is("enabled") {
295 oltLogger.WithFields(log.Fields{
296 "oltId": o.ID,
297 }).Info("This is an OLT soft reboot")
298 softReboot = true
299 }
300
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800301 // transition internal state to deleted
302 if err := o.InternalState.Event("delete"); err != nil {
303 oltLogger.WithFields(log.Fields{
304 "oltId": o.ID,
305 }).Errorf("Error deleting OLT: %v", err)
306 return err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100307 }
308
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800309 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
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800310 o.StopOltServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100311
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
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100341 // terminate the OLT's processOltMessages go routine
342 close(o.channel)
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700343
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700344 o.enableContextCancel()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100345
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100346 time.Sleep(time.Duration(rebootDelay) * time.Second)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100347
348 if err := o.InternalState.Event("initialize"); err != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800349 oltLogger.WithFields(log.Fields{
350 "oltId": o.ID,
351 }).Errorf("Error initializing OLT: %v", err)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100352 return err
353 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800354 oltLogger.WithFields(log.Fields{
355 "oltId": o.ID,
356 }).Info("OLT restart completed")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100357 return nil
358}
359
360// newOltServer launches a new grpc server for OpenOLT
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800361func (o *OltDevice) newOltServer() (*grpc.Server, error) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700362 address := common.Config.BBSim.OpenOltAddress
Matteo Scandolo4747d292019-08-05 11:50:18 -0700363 lis, err := net.Listen("tcp", address)
364 if err != nil {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700365 oltLogger.Fatalf("OLT failed to listen: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700366 }
367 grpcServer := grpc.NewServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100368
Matteo Scandolo4747d292019-08-05 11:50:18 -0700369 openolt.RegisterOpenoltServer(grpcServer, o)
370
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100371 reflection.Register(grpcServer)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700372
Shrey Baid688b4242020-07-10 20:40:10 +0530373 go func() { _ = grpcServer.Serve(lis) }()
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100374 oltLogger.Debugf("OLT listening on %v", address)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700375
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100376 return grpcServer, nil
377}
378
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800379// StartOltServer will create the grpc server that VOLTHA uses
380// to communicate with the device
381func (o *OltDevice) StartOltServer() (*grpc.Server, error) {
382 oltServer, err := o.newOltServer()
383 if err != nil {
384 oltLogger.WithFields(log.Fields{
385 "err": err,
386 }).Error("Cannot OLT gRPC server")
387 return nil, err
388 }
389 return oltServer, nil
390}
391
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100392// StopOltServer stops the OpenOLT grpc server
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800393func (o *OltDevice) StopOltServer() {
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000394 if o.OltServer != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800395 oltLogger.WithFields(log.Fields{
396 "oltId": o.SerialNumber,
397 }).Warnf("Stopping OLT gRPC server")
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000398 o.OltServer.Stop()
399 o.OltServer = nil
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700400 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700401}
402
403// Device Methods
404
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100405// Enable implements the OpenOLT EnableIndicationServer functionality
Shrey Baid688b4242020-07-10 20:40:10 +0530406func (o *OltDevice) Enable(stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700407 oltLogger.Debug("Enable OLT called")
Pragya Arya2225f202020-01-29 18:05:01 +0530408 rebootFlag := false
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700409
David Bainbridge103cf022019-12-16 20:11:35 +0000410 // If enabled has already been called then an enabled context has
411 // been created. If this is the case then we want to cancel all the
412 // proessing loops associated with that enable before we recreate
413 // new ones
414 o.Lock()
415 if o.enableContext != nil && o.enableContextCancel != nil {
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700416 oltLogger.Info("This is an OLT reboot")
David Bainbridge103cf022019-12-16 20:11:35 +0000417 o.enableContextCancel()
Pragya Arya2225f202020-01-29 18:05:01 +0530418 rebootFlag = true
David Bainbridge103cf022019-12-16 20:11:35 +0000419 }
420 o.enableContext, o.enableContextCancel = context.WithCancel(context.TODO())
421 o.Unlock()
422
Matteo Scandolo4747d292019-08-05 11:50:18 -0700423 wg := sync.WaitGroup{}
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800424 wg.Add(3)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700425
Matteo Scandolo4a036262020-08-17 15:56:13 -0700426 o.OpenoltStream = stream
Pragya Arya1cbefa42020-01-13 12:15:29 +0530427
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100428 // create Go routine to process all OLT events
David Bainbridge103cf022019-12-16 20:11:35 +0000429 go o.processOltMessages(o.enableContext, stream, &wg)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700430
431 // enable the OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -0800432 oltMsg := types.Message{
433 Type: types.OltIndication,
434 Data: types.OltIndicationMessage{
435 OperState: types.UP,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700436 },
437 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100438 o.channel <- oltMsg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700439
440 // send NNI Port Indications
441 for _, nni := range o.Nnis {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800442 msg := types.Message{
443 Type: types.NniIndication,
444 Data: types.NniIndicationMessage{
445 OperState: types.UP,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700446 NniPortID: nni.ID,
447 },
448 }
449 o.channel <- msg
450 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100451
Shrey Baid688b4242020-07-10 20:40:10 +0530452 if rebootFlag {
Pragya Arya2225f202020-01-29 18:05:01 +0530453 for _, pon := range o.Pons {
454 if pon.InternalState.Current() == "disabled" {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800455 msg := types.Message{
456 Type: types.PonIndication,
457 Data: types.PonIndicationMessage{
458 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +0530459 PonPortID: pon.ID,
460 },
461 }
462 o.channel <- msg
Matteo Scandoloe60a5052020-02-07 00:31:14 +0000463 }
Pragya Arya2225f202020-01-29 18:05:01 +0530464 }
465 } else {
466
467 // 1. controlledActivation == Default: Send both PON and ONUs indications
468 // 2. controlledActivation == only-onu: that means only ONUs will be controlled activated, so auto send PON indications
469
470 if o.ControlledActivation == Default || o.ControlledActivation == OnlyONU {
471 // send PON Port indications
472 for _, pon := range o.Pons {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800473 msg := types.Message{
474 Type: types.PonIndication,
475 Data: types.PonIndicationMessage{
476 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +0530477 PonPortID: pon.ID,
478 },
479 }
480 o.channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700481 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700482 }
483 }
484
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800485 oltLogger.Debug("Enable OLT Done")
Pragya Arya996a0892020-03-09 21:47:52 +0530486
487 if !o.enablePerf {
488 // Start a go routine to send periodic port stats to openolt adapter
489 go o.periodicPortStats(o.enableContext)
490 }
491
Matteo Scandolo4747d292019-08-05 11:50:18 -0700492 wg.Wait()
Matteo Scandolo4747d292019-08-05 11:50:18 -0700493}
494
Pragya Arya996a0892020-03-09 21:47:52 +0530495func (o *OltDevice) periodicPortStats(ctx context.Context) {
496 var portStats *openolt.PortStatistics
497 for {
498 select {
499 case <-time.After(time.Duration(o.PortStatsInterval) * time.Second):
500 // send NNI port stats
501 for _, port := range o.Nnis {
502 incrementStat := true
503 if port.OperState.Current() == "down" {
504 incrementStat = false
505 }
506 portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat)
507 o.sendPortStatsIndication(portStats, port.ID, port.Type)
508 }
509
510 // send PON port stats
511 for _, port := range o.Pons {
512 incrementStat := true
513 // do not increment port stats if PON port is down or no ONU is activated on PON port
514 if port.OperState.Current() == "down" || port.GetNumOfActiveOnus() < 1 {
515 incrementStat = false
516 }
517 portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat)
518 o.sendPortStatsIndication(portStats, port.ID, port.Type)
519 }
520 case <-ctx.Done():
521 log.Debug("Stop sending port stats")
522 return
523 }
524
525 }
526}
527
Matteo Scandolo4747d292019-08-05 11:50:18 -0700528// Helpers method
529
Matteo Scandolof9d43412021-01-12 11:11:34 -0800530func (o *OltDevice) SetAlarm(interfaceId uint32, interfaceType string, alarmStatus string) error {
531
532 switch interfaceType {
533 case "nni":
534 if !o.HasNni(interfaceId) {
535 return status.Errorf(codes.NotFound, strconv.Itoa(int(interfaceId))+" NNI not present in olt")
536 }
537
538 case "pon":
539 if !o.HasPon(interfaceId) {
540 return status.Errorf(codes.NotFound, strconv.Itoa(int(interfaceId))+" PON not present in olt")
541 }
542 }
543
544 alarmIndication := &openolt.AlarmIndication{
545 Data: &openolt.AlarmIndication_LosInd{LosInd: &openolt.LosIndication{
546 Status: alarmStatus,
547 IntfId: InterfaceIDToPortNo(interfaceId, interfaceType),
548 }},
549 }
550
551 msg := types.Message{
552 Type: types.AlarmIndication,
553 Data: alarmIndication,
554 }
555
556 o.channel <- msg
557
558 return nil
559}
560
561func (o *OltDevice) HasNni(id uint32) bool {
562 for _, intf := range o.Nnis {
563 if intf.ID == id {
564 return true
565 }
566 }
567 return false
568}
569
570func (o *OltDevice) HasPon(id uint32) bool {
571 for _, intf := range o.Pons {
572 if intf.ID == id {
573 return true
574 }
575 }
576 return false
577}
578
Shrey Baid688b4242020-07-10 20:40:10 +0530579func (o *OltDevice) GetPonById(id uint32) (*PonPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700580 for _, pon := range o.Pons {
581 if pon.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700582 return pon, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700583 }
584 }
Shrey Baid688b4242020-07-10 20:40:10 +0530585 return nil, fmt.Errorf("Cannot find PonPort with id %d in OLT %d", id, o.ID)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700586}
587
Shrey Baid688b4242020-07-10 20:40:10 +0530588func (o *OltDevice) getNniById(id uint32) (*NniPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700589 for _, nni := range o.Nnis {
590 if nni.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700591 return nni, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700592 }
593 }
Shrey Baid688b4242020-07-10 20:40:10 +0530594 return nil, fmt.Errorf("Cannot find NniPort with id %d in OLT %d", id, o.ID)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700595}
596
Scott Baker41724b82020-01-21 19:54:53 -0800597func (o *OltDevice) sendAlarmIndication(alarmInd *openolt.AlarmIndication, stream openolt.Openolt_EnableIndicationServer) {
598 data := &openolt.Indication_AlarmInd{AlarmInd: alarmInd}
599 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
600 oltLogger.Errorf("Failed to send Alarm Indication: %v", err)
601 return
602 }
603
604 oltLogger.WithFields(log.Fields{
605 "AlarmIndication": alarmInd,
606 }).Debug("Sent Indication_AlarmInd")
607}
608
Matteo Scandolof9d43412021-01-12 11:11:34 -0800609func (o *OltDevice) sendOltIndication(msg types.OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700610 data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}}
611 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700612 oltLogger.Errorf("Failed to send Indication_OltInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800613 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700614 }
615
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700616 oltLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700617 "OperState": msg.OperState,
618 }).Debug("Sent Indication_OltInd")
619}
620
Matteo Scandolof9d43412021-01-12 11:11:34 -0800621func (o *OltDevice) sendNniIndication(msg types.NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700622 nni, _ := o.getNniById(msg.NniPortID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800623 if msg.OperState == types.UP {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800624 if err := nni.OperState.Event("enable"); err != nil {
625 log.WithFields(log.Fields{
626 "Type": nni.Type,
627 "IntfId": nni.ID,
628 "OperState": nni.OperState.Current(),
629 }).Errorf("Can't move NNI Port to enabled state: %v", err)
630 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800631 } else if msg.OperState == types.DOWN {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800632 if err := nni.OperState.Event("disable"); err != nil {
633 log.WithFields(log.Fields{
634 "Type": nni.Type,
635 "IntfId": nni.ID,
636 "OperState": nni.OperState.Current(),
637 }).Errorf("Can't move NNI Port to disable state: %v", err)
638 }
639 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700640 // NOTE Operstate may need to be an integer
Matteo Scandolo4747d292019-08-05 11:50:18 -0700641 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700642 Type: nni.Type,
643 IntfId: nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700644 OperState: nni.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 NNI: %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": nni.Type,
654 "IntfId": nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700655 "OperState": nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700656 }).Debug("Sent Indication_IntfOperInd for NNI")
657}
658
Pragya Arya2225f202020-01-29 18:05:01 +0530659func (o *OltDevice) sendPonIndication(ponPortID uint32) {
660
Matteo Scandolo4a036262020-08-17 15:56:13 -0700661 stream := o.OpenoltStream
Pragya Arya2225f202020-01-29 18:05:01 +0530662 pon, _ := o.GetPonById(ponPortID)
663 // Send IntfIndication for PON port
Matteo Scandolo4747d292019-08-05 11:50:18 -0700664 discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700665 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700666 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700667 }}
668
669 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700670 oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800671 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700672 }
673
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700674 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700675 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700676 "OperState": pon.OperState.Current(),
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700677 }).Debug("Sent Indication_IntfInd for PON")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700678
Pragya Arya2225f202020-01-29 18:05:01 +0530679 // Send IntfOperIndication for PON port
Matteo Scandolo4747d292019-08-05 11:50:18 -0700680 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700681 Type: pon.Type,
682 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700683 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700684 }}
685
686 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700687 oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800688 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700689 }
690
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700691 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700692 "Type": pon.Type,
693 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700694 "OperState": pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700695 }).Debug("Sent Indication_IntfOperInd for PON")
696}
697
Pragya Arya996a0892020-03-09 21:47:52 +0530698func (o *OltDevice) sendPortStatsIndication(stats *openolt.PortStatistics, portID uint32, portType string) {
Shrey Baid55f328c2020-07-07 19:20:42 +0530699 if o.InternalState.Current() == "enabled" {
700 oltLogger.WithFields(log.Fields{
701 "Type": portType,
702 "IntfId": portID,
703 }).Trace("Sending port stats")
704 stats.IntfId = InterfaceIDToPortNo(portID, portType)
705 data := &openolt.Indication_PortStats{
706 PortStats: stats,
707 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700708 stream := o.OpenoltStream
Shrey Baid55f328c2020-07-07 19:20:42 +0530709 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
710 oltLogger.Errorf("Failed to send PortStats: %v", err)
711 return
712 }
Pragya Arya996a0892020-03-09 21:47:52 +0530713 }
714}
715
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100716// processOltMessages handles messages received over the OpenOLT interface
David Bainbridge103cf022019-12-16 20:11:35 +0000717func (o *OltDevice) processOltMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100718 oltLogger.Debug("Starting OLT Indication Channel")
David Bainbridge103cf022019-12-16 20:11:35 +0000719 ch := o.channel
Matteo Scandolo4747d292019-08-05 11:50:18 -0700720
David Bainbridge103cf022019-12-16 20:11:35 +0000721loop:
722 for {
723 select {
724 case <-ctx.Done():
725 oltLogger.Debug("OLT Indication processing canceled via context")
726 break loop
727 case message, ok := <-ch:
728 if !ok || ctx.Err() != nil {
729 oltLogger.Debug("OLT Indication processing canceled via closed channel")
730 break loop
Matteo Scandolo4747d292019-08-05 11:50:18 -0700731 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700732
David Bainbridge103cf022019-12-16 20:11:35 +0000733 oltLogger.WithFields(log.Fields{
734 "oltId": o.ID,
735 "messageType": message.Type,
736 }).Trace("Received message")
737
738 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800739 case types.OltIndication:
740 msg, _ := message.Data.(types.OltIndicationMessage)
741 if msg.OperState == types.UP {
Shrey Baid688b4242020-07-10 20:40:10 +0530742 _ = o.InternalState.Event("enable")
743 _ = o.OperState.Event("enable")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800744 } else if msg.OperState == types.DOWN {
Shrey Baid688b4242020-07-10 20:40:10 +0530745 _ = o.InternalState.Event("disable")
746 _ = o.OperState.Event("disable")
David Bainbridge103cf022019-12-16 20:11:35 +0000747 }
748 o.sendOltIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800749 case types.AlarmIndication:
Scott Baker41724b82020-01-21 19:54:53 -0800750 alarmInd, _ := message.Data.(*openolt.AlarmIndication)
751 o.sendAlarmIndication(alarmInd, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800752 case types.NniIndication:
753 msg, _ := message.Data.(types.NniIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000754 o.sendNniIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800755 case types.PonIndication:
756 msg, _ := message.Data.(types.PonIndicationMessage)
Pragya Arya2225f202020-01-29 18:05:01 +0530757 pon, _ := o.GetPonById(msg.PonPortID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800758 if msg.OperState == types.UP {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530759 if err := pon.OperState.Event("enable"); err != nil {
760 oltLogger.WithFields(log.Fields{
761 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800762 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530763 }).Error("Can't Enable Oper state for PON Port")
764 }
765 if err := pon.InternalState.Event("enable"); err != nil {
766 oltLogger.WithFields(log.Fields{
767 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800768 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530769 }).Error("Can't Enable Internal state for PON Port")
770 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800771 } else if msg.OperState == types.DOWN {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530772 if err := pon.OperState.Event("disable"); err != nil {
773 oltLogger.WithFields(log.Fields{
774 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800775 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530776 }).Error("Can't Disable Oper state for PON Port")
777 }
778 if err := pon.InternalState.Event("disable"); err != nil {
779 oltLogger.WithFields(log.Fields{
780 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800781 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530782 }).Error("Can't Disable Internal state for PON Port")
783 }
Pragya Arya2225f202020-01-29 18:05:01 +0530784 }
David Bainbridge103cf022019-12-16 20:11:35 +0000785 default:
786 oltLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
787 }
788 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700789 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100790 wg.Done()
791 oltLogger.Warn("Stopped handling OLT Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700792}
793
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700794// returns an ONU with a given Serial Number
Shrey Baid688b4242020-07-10 20:40:10 +0530795func (o *OltDevice) FindOnuBySn(serialNumber string) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200796 // TODO this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700797 // memoizing it will remove the bottleneck
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700798 for _, pon := range o.Pons {
799 for _, onu := range pon.Onus {
800 if onu.Sn() == serialNumber {
Matteo Scandolo27428702019-10-11 16:21:16 -0700801 return onu, nil
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700802 }
803 }
804 }
805
Shrey Baid688b4242020-07-10 20:40:10 +0530806 return &Onu{}, fmt.Errorf("cannot-find-onu-by-serial-number-%s", serialNumber)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700807}
808
William Kurkian9dadc5b2019-10-22 13:51:57 -0400809// returns an ONU with a given interface/Onu Id
Shrey Baid688b4242020-07-10 20:40:10 +0530810func (o *OltDevice) FindOnuById(intfId uint32, onuId uint32) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200811 // TODO this function can be a performance bottleneck when we have many ONUs,
William Kurkian9dadc5b2019-10-22 13:51:57 -0400812 // memoizing it will remove the bottleneck
813 for _, pon := range o.Pons {
814 if pon.ID == intfId {
815 for _, onu := range pon.Onus {
816 if onu.ID == onuId {
817 return onu, nil
818 }
819 }
820 }
821 }
Shrey Baid688b4242020-07-10 20:40:10 +0530822 return &Onu{}, fmt.Errorf("cannot-find-onu-by-id-%v-%v", intfId, onuId)
William Kurkian9dadc5b2019-10-22 13:51:57 -0400823}
824
Matteo Scandolo4a036262020-08-17 15:56:13 -0700825// returns a Service with a given Mac Address
826func (o *OltDevice) FindServiceByMacAddress(mac net.HardwareAddr) (ServiceIf, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200827 // TODO this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700828 // memoizing it will remove the bottleneck
829 for _, pon := range o.Pons {
830 for _, onu := range pon.Onus {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700831 s, err := onu.findServiceByMacAddress(mac)
832 if err == nil {
833 return s, nil
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700834 }
835 }
836 }
837
Matteo Scandolo4a036262020-08-17 15:56:13 -0700838 return nil, fmt.Errorf("cannot-find-service-by-mac-address-%s", mac)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700839}
840
Matteo Scandolo4747d292019-08-05 11:50:18 -0700841// GRPC Endpoints
842
Shrey Baid688b4242020-07-10 20:40:10 +0530843func (o *OltDevice) ActivateOnu(context context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700844 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700845 "OnuSn": onuSnToString(onu.SerialNumber),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700846 }).Info("Received ActivateOnu call from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +0530847 publishEvent("ONU-activate-indication-received", int32(onu.IntfId), int32(onu.OnuId), onuSnToString(onu.SerialNumber))
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700848
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700849 pon, _ := o.GetPonById(onu.IntfId)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800850
851 // Initialize the resource maps for this ONU
852 olt.AllocIDs[onu.IntfId][onu.OnuId] = make(map[uint32]map[int32]map[uint64]bool)
853 olt.GemPortIDs[onu.IntfId][onu.OnuId] = make(map[uint32]map[int32]map[uint64]bool)
854
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700855 _onu, _ := pon.GetOnuBySn(onu.SerialNumber)
William Kurkian0418bc82019-11-06 12:16:24 -0500856 _onu.SetID(onu.OnuId)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700857
Matteo Scandolocedde462021-03-09 17:37:16 -0800858 if err := _onu.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700859 oltLogger.WithFields(log.Fields{
860 "IntfId": _onu.PonPortID,
861 "OnuSn": _onu.Sn(),
862 "OnuId": _onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800863 }).Infof("Failed to transition ONU to %s state: %s", OnuStateEnabled, err.Error())
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700864 }
865
866 // NOTE we need to immediately activate the ONU or the OMCI state machine won't start
867
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700868 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700869}
870
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800871func (o *OltDevice) DeactivateOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700872 oltLogger.Error("DeactivateOnu not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700873 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700874}
875
Shrey Baid688b4242020-07-10 20:40:10 +0530876func (o *OltDevice) DeleteOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Pragya Arya1cbefa42020-01-13 12:15:29 +0530877 oltLogger.WithFields(log.Fields{
878 "IntfId": onu.IntfId,
879 "OnuId": onu.OnuId,
880 }).Info("Received DeleteOnu call from VOLTHA")
881
882 pon, err := o.GetPonById(onu.IntfId)
883 if err != nil {
884 oltLogger.WithFields(log.Fields{
885 "OnuId": onu.OnuId,
886 "IntfId": onu.IntfId,
887 "err": err,
888 }).Error("Can't find PonPort")
889 }
890 _onu, err := pon.GetOnuById(onu.OnuId)
891 if err != nil {
892 oltLogger.WithFields(log.Fields{
893 "OnuId": onu.OnuId,
894 "IntfId": onu.IntfId,
895 "err": err,
896 }).Error("Can't find Onu")
897 }
898
Matteo Scandolocedde462021-03-09 17:37:16 -0800899 if err := _onu.InternalState.Event(OnuTxDisable); err != nil {
Pragya Arya1cbefa42020-01-13 12:15:29 +0530900 oltLogger.WithFields(log.Fields{
901 "IntfId": _onu.PonPortID,
902 "OnuSn": _onu.Sn(),
903 "OnuId": _onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800904 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDisabled, err.Error())
Hardik Windlassad790cb2020-06-17 21:26:22 +0530905 }
906
Hardik Windlassad790cb2020-06-17 21:26:22 +0530907 // ONU Re-Discovery
908 if o.InternalState.Current() == "enabled" && pon.InternalState.Current() == "enabled" {
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530909 go _onu.ReDiscoverOnu()
Pragya Arya1cbefa42020-01-13 12:15:29 +0530910 }
911
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) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700916 // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether?
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800917 oltLogger.WithFields(log.Fields{
918 "oltId": o.ID,
919 }).Info("Disabling OLT")
Pragya Arya324337e2020-02-20 14:35:08 +0530920 publishEvent("OLT-disable-received", -1, -1, "")
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800921
Matteo Scandolo401503a2019-12-11 14:48:14 -0800922 for _, pon := range o.Pons {
Pragya Arya2225f202020-01-29 18:05:01 +0530923 if pon.InternalState.Current() == "enabled" {
924 // disable PONs
Matteo Scandolof9d43412021-01-12 11:11:34 -0800925 msg := types.Message{
926 Type: types.PonIndication,
927 Data: types.PonIndicationMessage{
928 OperState: types.DOWN,
Pragya Arya2225f202020-01-29 18:05:01 +0530929 PonPortID: pon.ID,
930 },
931 }
932 o.channel <- msg
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800933 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800934 }
935
Matteo Scandolo401503a2019-12-11 14:48:14 -0800936 // Note that we are not disabling the NNI as the real OLT does not.
937 // The reason for that is in-band management
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800938
939 // disable OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -0800940 oltMsg := types.Message{
941 Type: types.OltIndication,
942 Data: types.OltIndicationMessage{
943 OperState: types.DOWN,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700944 },
945 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100946 o.channel <- oltMsg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700947 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700948}
949
Shrey Baid688b4242020-07-10 20:40:10 +0530950func (o *OltDevice) DisablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530951 oltLogger.Infof("DisablePonIf request received for PON %d", intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200952 ponID := intf.GetIntfId()
953 pon, _ := o.GetPonById(intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200954
Matteo Scandolof9d43412021-01-12 11:11:34 -0800955 msg := types.Message{
956 Type: types.PonIndication,
957 Data: types.PonIndicationMessage{
958 OperState: types.DOWN,
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200959 PonPortID: ponID,
960 },
961 }
962 o.channel <- msg
963
964 for _, onu := range pon.Onus {
965
Matteo Scandolof9d43412021-01-12 11:11:34 -0800966 onuIndication := types.OnuIndicationMessage{
967 OperState: types.DOWN,
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200968 PonPortID: ponID,
969 OnuID: onu.ID,
970 OnuSN: onu.SerialNumber,
971 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700972 onu.sendOnuIndication(onuIndication, o.OpenoltStream)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200973
974 }
975
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700976 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700977}
978
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100979func (o *OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700980 oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +0530981 publishEvent("OLT-enable-received", -1, -1, "")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700982 o.Enable(stream)
983 return nil
984}
985
Shrey Baid688b4242020-07-10 20:40:10 +0530986func (o *OltDevice) EnablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530987 oltLogger.Infof("EnablePonIf request received for PON %d", intf.IntfId)
Pragya Arya2225f202020-01-29 18:05:01 +0530988 ponID := intf.GetIntfId()
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200989 pon, _ := o.GetPonById(intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200990
Matteo Scandolof9d43412021-01-12 11:11:34 -0800991 msg := types.Message{
992 Type: types.PonIndication,
993 Data: types.PonIndicationMessage{
994 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +0530995 PonPortID: ponID,
996 },
997 }
998 o.channel <- msg
999
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001000 for _, onu := range pon.Onus {
1001
Matteo Scandolof9d43412021-01-12 11:11:34 -08001002 onuIndication := types.OnuIndicationMessage{
1003 OperState: types.UP,
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001004 PonPortID: ponID,
1005 OnuID: onu.ID,
1006 OnuSN: onu.SerialNumber,
1007 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001008 onu.sendOnuIndication(onuIndication, o.OpenoltStream)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001009
1010 }
1011
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001012 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001013}
1014
Shrey Baid688b4242020-07-10 20:40:10 +05301015func (o *OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001016 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001017 "IntfId": flow.AccessIntfId,
1018 "OnuId": flow.OnuId,
1019 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001020 "InnerVlan": flow.Classifier.IVid,
1021 "OuterVlan": flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001022 "FlowType": flow.FlowType,
1023 "FlowId": flow.FlowId,
1024 "UniID": flow.UniId,
1025 "PortNo": flow.PortNo,
Pragya Arya8bdb4532020-03-02 17:08:09 +05301026 }).Tracef("OLT receives FlowAdd")
1027
1028 flowKey := FlowKey{}
1029 if !o.enablePerf {
1030 flowKey = FlowKey{ID: flow.FlowId, Direction: flow.FlowType}
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001031 olt.Flows.Store(flowKey, *flow)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301032 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001033
1034 if flow.AccessIntfId == -1 {
1035 oltLogger.WithFields(log.Fields{
1036 "FlowId": flow.FlowId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001037 }).Debug("Adding OLT flow")
Jonathan Hartb5fc46a2020-03-31 16:42:31 -07001038 } else if flow.FlowType == "multicast" {
1039 oltLogger.WithFields(log.Fields{
Matteo Scandolo618a6582020-09-09 12:21:29 -07001040 "Cookie": flow.Cookie,
1041 "DstPort": flow.Classifier.DstPort,
1042 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
1043 "FlowId": flow.FlowId,
1044 "FlowType": flow.FlowType,
1045 "GemportId": flow.GemportId,
1046 "InnerVlan": flow.Classifier.IVid,
1047 "IntfId": flow.AccessIntfId,
1048 "IpProto": flow.Classifier.IpProto,
1049 "OnuId": flow.OnuId,
1050 "OuterVlan": flow.Classifier.OVid,
1051 "PortNo": flow.PortNo,
1052 "SrcPort": flow.Classifier.SrcPort,
1053 "UniID": flow.UniId,
1054 "ClassifierOPbits": flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001055 }).Debug("Adding OLT multicast flow")
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001056 } else {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001057 pon, err := o.GetPonById(uint32(flow.AccessIntfId))
Matteo Scandolo27428702019-10-11 16:21:16 -07001058 if err != nil {
1059 oltLogger.WithFields(log.Fields{
1060 "OnuId": flow.OnuId,
1061 "IntfId": flow.AccessIntfId,
1062 "err": err,
1063 }).Error("Can't find PonPort")
1064 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001065 onu, err := pon.GetOnuById(uint32(flow.OnuId))
Matteo Scandolo27428702019-10-11 16:21:16 -07001066 if err != nil {
1067 oltLogger.WithFields(log.Fields{
1068 "OnuId": flow.OnuId,
1069 "IntfId": flow.AccessIntfId,
1070 "err": err,
1071 }).Error("Can't find Onu")
Jonathan Hartb5fc46a2020-03-31 16:42:31 -07001072 return nil, err
Matteo Scandolo27428702019-10-11 16:21:16 -07001073 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301074 if !o.enablePerf {
1075 onu.Flows = append(onu.Flows, flowKey)
Pragya Arya1d5ffb82020-03-20 18:51:37 +05301076 // Generate event on first flow for ONU
1077 if len(onu.Flows) == 1 {
1078 publishEvent("Flow-add-received", int32(onu.PonPortID), int32(onu.ID), onuSnToString(onu.SerialNumber))
1079 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301080 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001081
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001082 // validate that the flow reference correct IDs (Alloc, Gem)
1083 if err := o.validateFlow(flow); err != nil {
1084 oltLogger.WithFields(log.Fields{
1085 "OnuId": flow.OnuId,
1086 "IntfId": flow.AccessIntfId,
1087 "Flow": flow,
1088 "SerialNumber": onu.Sn(),
1089 "err": err,
1090 }).Error("invalid-flow-for-onu")
1091 return nil, err
1092 }
1093
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001094 o.storeGemPortIdByFlow(flow)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001095 o.storeAllocId(flow)
1096
Matteo Scandolof9d43412021-01-12 11:11:34 -08001097 msg := types.Message{
1098 Type: types.FlowAdd,
1099 Data: types.OnuFlowUpdateMessage{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001100 PonPortID: pon.ID,
1101 OnuID: onu.ID,
1102 Flow: flow,
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001103 },
1104 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001105 onu.Channel <- msg
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001106 }
1107
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001108 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001109}
1110
Pragya Arya8bdb4532020-03-02 17:08:09 +05301111// FlowRemove request from VOLTHA
Shrey Baid688b4242020-07-10 20:40:10 +05301112func (o *OltDevice) FlowRemove(_ context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001113
Pragya Arya8bdb4532020-03-02 17:08:09 +05301114 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001115 "AllocId": flow.AllocId,
1116 "Cookie": flow.Cookie,
1117 "FlowId": flow.FlowId,
1118 "FlowType": flow.FlowType,
1119 "GemportId": flow.GemportId,
1120 "IntfId": flow.AccessIntfId,
1121 "OnuId": flow.OnuId,
1122 "PortNo": flow.PortNo,
1123 "UniID": flow.UniId,
1124 "ReplicateFlow": flow.ReplicateFlow,
1125 "PbitToGemport": flow.PbitToGemport,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001126 }).Debug("OLT receives FlowRemove")
Pragya Arya8bdb4532020-03-02 17:08:09 +05301127
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001128 olt.freeGemPortId(flow)
1129 olt.freeAllocId(flow)
1130
Pragya Arya8bdb4532020-03-02 17:08:09 +05301131 if !o.enablePerf { // remove only if flow were stored
1132 flowKey := FlowKey{
1133 ID: flow.FlowId,
1134 Direction: flow.FlowType,
1135 }
1136
1137 // Check if flow exists
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001138 storedFlowIntf, ok := o.Flows.Load(flowKey)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301139 if !ok {
1140 oltLogger.Errorf("Flow %v not found", flow)
1141 return new(openolt.Empty), status.Errorf(codes.NotFound, "Flow not found")
1142 }
1143
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001144 storedFlow := storedFlowIntf.(openolt.Flow)
1145
Pragya Arya8bdb4532020-03-02 17:08:09 +05301146 // if its ONU flow remove it from ONU also
1147 if storedFlow.AccessIntfId != -1 {
Matteo Scandolocedde462021-03-09 17:37:16 -08001148 pon, err := o.GetPonById(uint32(storedFlow.AccessIntfId))
1149 if err != nil {
1150 oltLogger.WithFields(log.Fields{
1151 "OnuId": storedFlow.OnuId,
1152 "IntfId": storedFlow.AccessIntfId,
1153 "PONs": olt.Pons,
1154 "err": err,
1155 }).Error("PON-port-not-found")
1156 return new(openolt.Empty), nil
1157 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301158 onu, err := pon.GetOnuById(uint32(storedFlow.OnuId))
1159 if err != nil {
1160 oltLogger.WithFields(log.Fields{
1161 "OnuId": storedFlow.OnuId,
1162 "IntfId": storedFlow.AccessIntfId,
1163 "err": err,
Matteo Scandolocedde462021-03-09 17:37:16 -08001164 }).Error("ONU-not-found")
Pragya Arya8bdb4532020-03-02 17:08:09 +05301165 return new(openolt.Empty), nil
1166 }
1167 onu.DeleteFlow(flowKey)
Pragya Arya1d5ffb82020-03-20 18:51:37 +05301168 publishEvent("Flow-remove-received", int32(onu.PonPortID), int32(onu.ID), onuSnToString(onu.SerialNumber))
Pragya Arya8bdb4532020-03-02 17:08:09 +05301169 }
1170
1171 // delete from olt flows
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001172 o.Flows.Delete(flowKey)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301173 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001174
1175 if flow.AccessIntfId == -1 {
1176 oltLogger.WithFields(log.Fields{
1177 "FlowId": flow.FlowId,
1178 }).Debug("Removing OLT flow")
1179 } else if flow.FlowType == "multicast" {
1180 oltLogger.WithFields(log.Fields{
1181 "FlowId": flow.FlowId,
1182 }).Debug("Removing OLT multicast flow")
1183 } else {
1184
1185 onu, err := o.GetOnuByFlowId(flow.FlowId)
1186 if err != nil {
1187 oltLogger.WithFields(log.Fields{
1188 "OnuId": flow.OnuId,
1189 "IntfId": flow.AccessIntfId,
1190 "err": err,
1191 }).Error("Can't find Onu")
1192 return nil, err
1193 }
1194
Matteo Scandolof9d43412021-01-12 11:11:34 -08001195 msg := types.Message{
1196 Type: types.FlowRemoved,
1197 Data: types.OnuFlowUpdateMessage{
Shrey Baid55f328c2020-07-07 19:20:42 +05301198 Flow: flow,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001199 },
1200 }
1201 onu.Channel <- msg
1202 }
1203
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001204 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001205}
1206
Shrey Baid688b4242020-07-10 20:40:10 +05301207func (o *OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo18859852020-01-15 13:33:57 -08001208 res := openolt.Heartbeat{HeartbeatSignature: uint32(time.Now().Unix())}
1209 oltLogger.WithFields(log.Fields{
1210 "signature": res.HeartbeatSignature,
1211 }).Trace("HeartbeatCheck")
1212 return &res, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001213}
1214
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001215func (o *OltDevice) GetOnuByFlowId(flowId uint64) (*Onu, error) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001216 for _, pon := range o.Pons {
1217 for _, onu := range pon.Onus {
1218 for _, fId := range onu.FlowIds {
1219 if fId == flowId {
1220 return onu, nil
1221 }
1222 }
1223 }
1224 }
Shrey Baid688b4242020-07-10 20:40:10 +05301225 return nil, fmt.Errorf("Cannot find Onu by flowId %d", flowId)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001226}
1227
Shrey Baid688b4242020-07-10 20:40:10 +05301228func (o *OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -07001229
Matteo Scandolocedde462021-03-09 17:37:16 -08001230 intfIDs := []uint32{}
1231 for i := 0; i < o.NumPon; i++ {
1232 intfIDs = append(intfIDs, uint32(i))
1233 }
1234
1235 devinfo := &openolt.DeviceInfo{
1236 Vendor: common.Config.Olt.Vendor,
1237 Model: common.Config.Olt.Model,
1238 HardwareVersion: common.Config.Olt.HardwareVersion,
1239 FirmwareVersion: common.Config.Olt.FirmwareVersion,
1240 Technology: common.Config.Olt.Technology,
1241 PonPorts: uint32(o.NumPon),
1242 OnuIdStart: onuIdStart,
1243 OnuIdEnd: onuIdEnd,
1244 AllocIdStart: allocIdStart,
1245 AllocIdEnd: allocIdEnd,
1246 GemportIdStart: gemportIdStart,
1247 GemportIdEnd: gemportIdEnd,
1248 FlowIdStart: flowIdStart,
1249 FlowIdEnd: flowIdEnd,
1250 DeviceSerialNumber: o.SerialNumber,
1251 DeviceId: common.Config.Olt.DeviceId,
1252 PreviouslyConnected: o.PreviouslyConnected,
1253 Ranges: []*openolt.DeviceInfo_DeviceResourceRanges{
1254 {
1255 IntfIds: intfIDs,
1256 Technology: common.Config.Olt.Technology,
1257 Pools: []*openolt.DeviceInfo_DeviceResourceRanges_Pool{
1258 {
1259 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID,
1260 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1261 Start: onuIdStart,
1262 End: onuIdEnd,
1263 },
1264 {
1265 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID,
1266 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1267 Start: allocIdStart,
1268 End: allocIdEnd,
1269 },
1270 {
1271 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID,
1272 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1273 Start: gemportIdStart,
1274 End: gemportIdEnd,
1275 },
1276 {
1277 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID,
1278 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH,
1279 Start: flowIdStart,
1280 End: flowIdEnd,
1281 },
1282 },
1283 },
1284 },
1285 }
Matteo Scandolo96f89192021-03-12 13:17:26 -08001286
1287 oltLogger.WithFields(log.Fields{
1288 "Vendor": devinfo.Vendor,
1289 "Model": devinfo.Model,
1290 "HardwareVersion": devinfo.HardwareVersion,
1291 "FirmwareVersion": devinfo.FirmwareVersion,
1292 "Technology": devinfo.Technology,
1293 "PonPorts": devinfo.PonPorts,
1294 "OnuIdStart": devinfo.OnuIdStart,
1295 "OnuIdEnd": devinfo.OnuIdEnd,
1296 "AllocIdStart": devinfo.AllocIdStart,
1297 "AllocIdEnd": devinfo.AllocIdEnd,
1298 "GemportIdStart": devinfo.GemportIdStart,
1299 "GemportIdEnd": devinfo.GemportIdEnd,
1300 "FlowIdStart": devinfo.FlowIdStart,
1301 "FlowIdEnd": devinfo.FlowIdEnd,
1302 "DeviceSerialNumber": devinfo.DeviceSerialNumber,
1303 "DeviceId": devinfo.DeviceId,
1304 "PreviouslyConnected": devinfo.PreviouslyConnected,
1305 }).Info("OLT receives GetDeviceInfo call from VOLTHA")
1306
1307 // once we connect, set the flag
1308 o.PreviouslyConnected = true
Matteo Scandolo4747d292019-08-05 11:50:18 -07001309
1310 return devinfo, nil
1311}
1312
Shrey Baid688b4242020-07-10 20:40:10 +05301313func (o *OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) {
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001314 pon, err := o.GetPonById(omci_msg.IntfId)
1315 if err != nil {
1316 oltLogger.WithFields(log.Fields{
Matteo Scandolof65e6872020-04-15 15:18:43 -07001317 "error": err,
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001318 "onu_id": omci_msg.OnuId,
1319 "pon_id": omci_msg.IntfId,
1320 }).Error("pon ID not found")
1321 return nil, err
1322 }
1323
1324 onu, err := pon.GetOnuById(omci_msg.OnuId)
1325 if err != nil {
1326 oltLogger.WithFields(log.Fields{
Matteo Scandolof65e6872020-04-15 15:18:43 -07001327 "error": err,
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001328 "onu_id": omci_msg.OnuId,
1329 "pon_id": omci_msg.IntfId,
1330 }).Error("onu ID not found")
1331 return nil, err
1332 }
1333
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001334 oltLogger.WithFields(log.Fields{
1335 "IntfId": onu.PonPortID,
1336 "OnuId": onu.ID,
1337 "OnuSn": onu.Sn(),
1338 }).Tracef("Received OmciMsgOut")
Matteo Scandolob5913142021-03-19 16:10:18 -07001339 omciPkt, omciMsg, err := omcilib.ParseOpenOltOmciPacket(omci_msg.Pkt)
1340 if err != nil {
1341 log.WithFields(log.Fields{
1342 "IntfId": onu.PonPortID,
1343 "SerialNumber": onu.Sn(),
1344 "omciPacket": omcilib.HexDecode(omci_msg.Pkt),
1345 "err": err.Error(),
1346 }).Error("cannot-parse-OMCI-packet")
1347 return nil, fmt.Errorf("olt-received-malformed-omci-packet")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001348 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001349 if onu.InternalState.Current() == OnuStateDisabled {
1350 // if the ONU is disabled just drop the message
1351 log.WithFields(log.Fields{
1352 "IntfId": onu.PonPortID,
1353 "SerialNumber": onu.Sn(),
1354 "omciBytes": hex.EncodeToString(omciPkt.Data()),
1355 "omciPkt": omciPkt,
1356 "omciMsgType": omciMsg.MessageType,
1357 }).Warn("dropping-omci-message")
1358 } else {
1359 msg := types.Message{
1360 Type: types.OMCI,
1361 Data: types.OmciMessage{
1362 OnuSN: onu.SerialNumber,
1363 OnuID: onu.ID,
1364 OmciMsg: omciMsg,
1365 OmciPkt: omciPkt,
1366 },
1367 }
1368 onu.Channel <- msg
1369 }
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) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001374 pon, err := o.GetPonById(onuPkt.IntfId)
Matteo Scandolo27428702019-10-11 16:21:16 -07001375 if err != nil {
1376 oltLogger.WithFields(log.Fields{
1377 "OnuId": onuPkt.OnuId,
1378 "IntfId": onuPkt.IntfId,
1379 "err": err,
1380 }).Error("Can't find PonPort")
1381 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001382 onu, err := pon.GetOnuById(onuPkt.OnuId)
Matteo Scandolo27428702019-10-11 16:21:16 -07001383 if err != nil {
1384 oltLogger.WithFields(log.Fields{
1385 "OnuId": onuPkt.OnuId,
1386 "IntfId": onuPkt.IntfId,
1387 "err": err,
1388 }).Error("Can't find Onu")
1389 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001390
Matteo Scandolo075b1892019-10-07 12:11:07 -07001391 oltLogger.WithFields(log.Fields{
1392 "IntfId": onu.PonPortID,
1393 "OnuId": onu.ID,
1394 "OnuSn": onu.Sn(),
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001395 "Packet": hex.EncodeToString(onuPkt.Pkt),
Matteo Scandolo75ed5b92020-09-03 09:03:16 -07001396 }).Trace("Received OnuPacketOut")
Matteo Scandolo075b1892019-10-07 12:11:07 -07001397
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001398 rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default)
Matteo Scandolo618a6582020-09-09 12:21:29 -07001399
1400 pktType, err := packetHandlers.GetPktType(rawpkt)
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001401 if err != nil {
1402 onuLogger.WithFields(log.Fields{
1403 "IntfId": onu.PonPortID,
1404 "OnuId": onu.ID,
1405 "OnuSn": onu.Sn(),
Matteo Scandolo618a6582020-09-09 12:21:29 -07001406 "Pkt": hex.EncodeToString(rawpkt.Data()),
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001407 }).Error("Can't find pktType in packet, droppint it")
1408 return new(openolt.Empty), nil
1409 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001410
Matteo Scandolo4a036262020-08-17 15:56:13 -07001411 pktMac, err := packetHandlers.GetDstMacAddressFromPacket(rawpkt)
Matteo Scandolo4a036262020-08-17 15:56:13 -07001412 if err != nil {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001413 onuLogger.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -07001414 "IntfId": onu.PonPortID,
1415 "OnuId": onu.ID,
1416 "OnuSn": onu.Sn(),
1417 "Pkt": rawpkt.Data(),
1418 }).Error("Can't find Dst MacAddress in packet, droppint it")
1419 return new(openolt.Empty), nil
1420 }
1421
Matteo Scandolof9d43412021-01-12 11:11:34 -08001422 msg := types.Message{
1423 Type: types.OnuPacketOut,
1424 Data: types.OnuPacketMessage{
Matteo Scandolo4a036262020-08-17 15:56:13 -07001425 IntfId: onuPkt.IntfId,
1426 OnuId: onuPkt.OnuId,
1427 Packet: rawpkt,
1428 Type: pktType,
1429 MacAddress: pktMac,
Matteo Scandolo075b1892019-10-07 12:11:07 -07001430 },
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001431 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001432
Matteo Scandolo075b1892019-10-07 12:11:07 -07001433 onu.Channel <- msg
1434
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001435 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001436}
1437
Shrey Baid688b4242020-07-10 20:40:10 +05301438func (o *OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo635b2bf2020-09-04 10:23:40 -07001439
1440 // OLT Reboot is called in two cases:
1441 // - 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)
1442 // - when an OLT needs to be rebooted (voltcl device reboot)
1443
Matteo Scandolod02b79b2019-12-05 16:42:13 -08001444 oltLogger.WithFields(log.Fields{
1445 "oltId": o.ID,
1446 }).Info("Shutting down")
Pragya Arya324337e2020-02-20 14:35:08 +05301447 publishEvent("OLT-reboot-received", -1, -1, "")
Shrey Baid688b4242020-07-10 20:40:10 +05301448 go func() { _ = o.RestartOLT() }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001449 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001450}
1451
Shrey Baid688b4242020-07-10 20:40:10 +05301452func (o *OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Pragya Arya6a708d62020-01-01 17:17:20 +05301453 oltLogger.WithFields(log.Fields{
1454 "oltId": o.ID,
1455 }).Info("Received ReenableOlt request from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +05301456 publishEvent("OLT-reenable-received", -1, -1, "")
Pragya Arya6a708d62020-01-01 17:17:20 +05301457
Pragya Arya2225f202020-01-29 18:05:01 +05301458 // enable OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -08001459 oltMsg := types.Message{
1460 Type: types.OltIndication,
1461 Data: types.OltIndicationMessage{
1462 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +05301463 },
Pragya Arya1881df02020-01-29 18:05:01 +05301464 }
Pragya Arya2225f202020-01-29 18:05:01 +05301465 o.channel <- oltMsg
Pragya Arya6a708d62020-01-01 17:17:20 +05301466
Pragya Arya2225f202020-01-29 18:05:01 +05301467 for _, pon := range o.Pons {
1468 if pon.InternalState.Current() == "disabled" {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001469 msg := types.Message{
1470 Type: types.PonIndication,
1471 Data: types.PonIndicationMessage{
1472 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +05301473 PonPortID: pon.ID,
1474 },
1475 }
1476 o.channel <- msg
1477 }
1478 }
Matteo Scandoloe60a5052020-02-07 00:31:14 +00001479
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001480 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001481}
1482
Shrey Baid688b4242020-07-10 20:40:10 +05301483func (o *OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001484 pkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
1485
Matteo Scandolo90d08f62020-10-29 12:06:55 -07001486 err := o.Nnis[0].handleNniPacket(pkt) // FIXME we are assuming we have only one NNI
1487
1488 if err != nil {
1489 return nil, err
1490 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001491 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001492}
1493
Shrey Baid688b4242020-07-10 20:40:10 +05301494func (o *OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001495 oltLogger.Error("CollectStatistics not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001496 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001497}
1498
Shrey Baid688b4242020-07-10 20:40:10 +05301499func (o *OltDevice) GetOnuInfo(context context.Context, packet *openolt.Onu) (*openolt.OnuIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001500 oltLogger.Error("GetOnuInfo not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001501 return new(openolt.OnuIndication), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001502}
1503
Shrey Baid688b4242020-07-10 20:40:10 +05301504func (o *OltDevice) GetPonIf(context context.Context, packet *openolt.Interface) (*openolt.IntfIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001505 oltLogger.Error("GetPonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001506 return new(openolt.IntfIndication), nil
Matteo Scandolod54283a2019-08-13 16:22:31 -07001507}
1508
Shrey Baid688b4242020-07-10 20:40:10 +05301509func (s *OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001510 oltLogger.Info("received CreateTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001511 return new(openolt.Empty), nil
1512}
1513
Shrey Baid688b4242020-07-10 20:40:10 +05301514func (s *OltDevice) RemoveTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001515 oltLogger.Info("received RemoveTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001516 return new(openolt.Empty), nil
1517}
1518
Shrey Baid688b4242020-07-10 20:40:10 +05301519func (s *OltDevice) CreateTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Anand S Katti09541352020-01-29 15:54:01 +05301520 oltLogger.WithFields(log.Fields{
1521 "OnuId": trafficSchedulers.OnuId,
1522 "IntfId": trafficSchedulers.IntfId,
1523 "OnuPortNo": trafficSchedulers.PortNo,
1524 }).Info("received CreateTrafficSchedulers")
1525
1526 if !s.enablePerf {
1527 pon, err := s.GetPonById(trafficSchedulers.IntfId)
1528 if err != nil {
1529 oltLogger.Errorf("Error retrieving PON by IntfId: %v", err)
1530 return new(openolt.Empty), err
1531 }
1532 onu, err := pon.GetOnuById(trafficSchedulers.OnuId)
1533 if err != nil {
1534 oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err)
1535 return new(openolt.Empty), err
1536 }
1537 onu.TrafficSchedulers = trafficSchedulers
1538 }
Matteo Scandolod54283a2019-08-13 16:22:31 -07001539 return new(openolt.Empty), nil
1540}
1541
Shrey Baid688b4242020-07-10 20:40:10 +05301542func (s *OltDevice) RemoveTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Anand S Katti09541352020-01-29 15:54:01 +05301543 oltLogger.WithFields(log.Fields{
1544 "OnuId": trafficSchedulers.OnuId,
1545 "IntfId": trafficSchedulers.IntfId,
1546 "OnuPortNo": trafficSchedulers.PortNo,
1547 }).Info("received RemoveTrafficSchedulers")
1548 if !s.enablePerf {
1549 pon, err := s.GetPonById(trafficSchedulers.IntfId)
1550 if err != nil {
1551 oltLogger.Errorf("Error retrieving PON by IntfId: %v", err)
1552 return new(openolt.Empty), err
1553 }
1554 onu, err := pon.GetOnuById(trafficSchedulers.OnuId)
1555 if err != nil {
1556 oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err)
1557 return new(openolt.Empty), err
1558 }
1559
1560 onu.TrafficSchedulers = nil
1561 }
Matteo Scandolod54283a2019-08-13 16:22:31 -07001562 return new(openolt.Empty), nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001563}
Scott Baker41724b82020-01-21 19:54:53 -08001564
Matteo Scandolo618a6582020-09-09 12:21:29 -07001565func (o *OltDevice) PerformGroupOperation(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) {
1566 oltLogger.WithFields(log.Fields{
1567 "GroupId": group.GroupId,
1568 "Command": group.Command,
1569 "Members": group.Members,
1570 "Action": group.Action,
1571 }).Debug("received PerformGroupOperation")
1572 return &openolt.Empty{}, nil
1573}
1574
1575func (o *OltDevice) DeleteGroup(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) {
1576 oltLogger.WithFields(log.Fields{
1577 "GroupId": group.GroupId,
1578 "Command": group.Command,
1579 "Members": group.Members,
1580 "Action": group.Action,
1581 }).Debug("received PerformGroupOperation")
1582 return &openolt.Empty{}, nil
1583}
1584
1585func (o *OltDevice) GetExtValue(ctx context.Context, in *openolt.ValueParam) (*common_protos.ReturnValues, error) {
1586 return &common_protos.ReturnValues{}, nil
1587}
1588
1589func (o *OltDevice) OnuItuPonAlarmSet(ctx context.Context, in *config.OnuItuPonAlarm) (*openolt.Empty, error) {
1590 return &openolt.Empty{}, nil
1591}
1592
1593func (o *OltDevice) GetLogicalOnuDistanceZero(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) {
1594 return &openolt.OnuLogicalDistance{}, nil
1595}
1596
1597func (o *OltDevice) GetLogicalOnuDistance(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) {
1598 return &openolt.OnuLogicalDistance{}, nil
1599}
Matteo Scandolo96f89192021-03-12 13:17:26 -08001600
1601func (o *OltDevice) GetGemPortStatistics(ctx context.Context, in *openolt.OnuPacket) (*openolt.GemPortStatistics, error) {
1602 return &openolt.GemPortStatistics{}, nil
1603}
1604
1605func (o *OltDevice) GetOnuStatistics(ctx context.Context, in *openolt.Onu) (*openolt.OnuStatistics, error) {
1606 return &openolt.OnuStatistics{}, nil
1607}
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001608
1609func (o *OltDevice) storeAllocId(flow *openolt.Flow) {
1610 o.AllocIDsLock.Lock()
1611 defer o.AllocIDsLock.Unlock()
1612
Matteo Scandolo21195d62021-04-07 14:31:23 -07001613 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)]; !ok {
1614 oltLogger.WithFields(log.Fields{
1615 "IntfId": flow.AccessIntfId,
1616 "OnuId": flow.OnuId,
1617 "PortNo": flow.PortNo,
1618 "GemportId": flow.GemportId,
1619 "FlowId": flow.FlowId,
1620 }).Error("trying-to-store-alloc-id-for-unknown-onu")
1621 }
1622
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001623 oltLogger.WithFields(log.Fields{
Matteo Scandolo21195d62021-04-07 14:31:23 -07001624 "IntfId": flow.AccessIntfId,
1625 "OnuId": flow.OnuId,
1626 "PortNo": flow.PortNo,
1627 "GemportId": flow.GemportId,
1628 "FlowId": flow.FlowId,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001629 }).Trace("storing-alloc-id-via-flow")
1630
1631 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo]; !ok {
1632 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo] = make(map[int32]map[uint64]bool)
1633 }
1634 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId]; !ok {
1635 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId] = make(map[uint64]bool)
1636 }
1637 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId][flow.FlowId] = true
1638}
1639
1640func (o *OltDevice) freeAllocId(flow *openolt.Flow) {
1641 // if this is the last flow referencing the AllocId then remove it
1642 o.AllocIDsLock.Lock()
1643 defer o.AllocIDsLock.Unlock()
1644
1645 oltLogger.WithFields(log.Fields{
1646 "IntfId": flow.AccessIntfId,
1647 "OnuId": flow.OnuId,
1648 "PortNo": flow.PortNo,
1649 "GemportId": flow.GemportId,
1650 }).Trace("freeing-alloc-id-via-flow")
1651
1652 // NOTE look at the freeGemPortId implementation for comments and context
1653 for ponId, ponValues := range o.AllocIDs {
1654 for onuId, onuValues := range ponValues {
1655 for uniId, uniValues := range onuValues {
1656 for allocId, flows := range uniValues {
1657 for flowId := range flows {
1658 // if the flow matches, remove it from the map.
1659 if flow.FlowId == flowId {
1660 delete(o.AllocIDs[ponId][onuId][uniId][allocId], flow.FlowId)
1661 }
1662 // if that was the last flow for a particular allocId, remove the entire allocId
1663 if len(o.AllocIDs[ponId][onuId][uniId][allocId]) == 0 {
1664 delete(o.AllocIDs[ponId][onuId][uniId], allocId)
1665 }
1666 }
1667 }
1668 }
1669 }
1670 }
1671}
1672
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001673func (o *OltDevice) storeGemPortId(ponId uint32, onuId uint32, portNo uint32, gemId int32, flowId uint64) {
Matteo Scandolo21195d62021-04-07 14:31:23 -07001674 o.GemPortIDsLock.Lock()
1675 defer o.GemPortIDsLock.Unlock()
1676
1677 if _, ok := o.GemPortIDs[ponId][onuId]; !ok {
1678 oltLogger.WithFields(log.Fields{
1679 "IntfId": ponId,
1680 "OnuId": onuId,
1681 "PortNo": portNo,
1682 "GemportId": gemId,
1683 "FlowId": flowId,
1684 }).Error("trying-to-store-gemport-for-unknown-onu")
1685 }
1686
1687 oltLogger.WithFields(log.Fields{
1688 "IntfId": ponId,
1689 "OnuId": onuId,
1690 "PortNo": portNo,
1691 "GemportId": gemId,
1692 "FlowId": flowId,
1693 }).Trace("storing-alloc-id-via-flow")
1694
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001695 if _, ok := o.GemPortIDs[ponId][onuId][portNo]; !ok {
1696 o.GemPortIDs[ponId][onuId][portNo] = make(map[int32]map[uint64]bool)
1697 }
1698 if _, ok := o.GemPortIDs[ponId][onuId][portNo][gemId]; !ok {
1699 o.GemPortIDs[ponId][onuId][portNo][gemId] = make(map[uint64]bool)
1700 }
1701 o.GemPortIDs[ponId][onuId][portNo][gemId][flowId] = true
1702}
1703
1704func (o *OltDevice) storeGemPortIdByFlow(flow *openolt.Flow) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001705 oltLogger.WithFields(log.Fields{
Matteo Scandolo21195d62021-04-07 14:31:23 -07001706 "IntfId": flow.AccessIntfId,
1707 "OnuId": flow.OnuId,
1708 "PortNo": flow.PortNo,
1709 "GemportId": flow.GemportId,
1710 "FlowId": flow.FlowId,
1711 "ReplicateFlow": flow.ReplicateFlow,
1712 "PbitToGemport": flow.PbitToGemport,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001713 }).Trace("storing-gem-port-id-via-flow")
1714
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001715 if flow.ReplicateFlow {
1716 for _, gem := range flow.PbitToGemport {
1717 o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, int32(gem), flow.FlowId)
1718 }
1719 } else {
1720 o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, flow.GemportId, flow.FlowId)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001721 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001722}
1723
1724func (o *OltDevice) freeGemPortId(flow *openolt.Flow) {
1725 // if this is the last flow referencing the GemPort then remove it
1726 o.GemPortIDsLock.Lock()
1727 defer o.GemPortIDsLock.Unlock()
1728
1729 oltLogger.WithFields(log.Fields{
1730 "IntfId": flow.AccessIntfId,
1731 "OnuId": flow.OnuId,
1732 "PortNo": flow.PortNo,
1733 "GemportId": flow.GemportId,
1734 }).Trace("freeing-gem-port-id-via-flow")
1735
1736 // NOTE that this loop is not very performant, it would be better if the flow carries
1737 // the same information that it carries during a FlowAdd. If so we can directly remove
1738 // items from the map
1739
1740 //delete(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.GemportId], flow.FlowId)
1741 //if len(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.GemportId]) == 0 {
1742 // delete(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo], flow.GemportId)
1743 //}
1744
1745 // NOTE this loop assumes that flow IDs are unique per device
1746 for ponId, ponValues := range o.GemPortIDs {
1747 for onuId, onuValues := range ponValues {
1748 for uniId, uniValues := range onuValues {
1749 for gemId, flows := range uniValues {
1750 for flowId := range flows {
1751 // if the flow matches, remove it from the map.
1752 if flow.FlowId == flowId {
1753 delete(o.GemPortIDs[ponId][onuId][uniId][gemId], flow.FlowId)
1754 }
1755 // if that was the last flow for a particular gem, remove the entire gem
1756 if len(o.GemPortIDs[ponId][onuId][uniId][gemId]) == 0 {
1757 delete(o.GemPortIDs[ponId][onuId][uniId], gemId)
1758 }
1759 }
1760 }
1761 }
1762 }
1763 }
1764}
1765
1766// validateFlow checks that:
1767// - the AllocId is not used in any flow referencing other ONUs/UNIs on the same PON
1768// - the GemPortId is not used in any flow referencing other ONUs/UNIs on the same PON
1769func (o *OltDevice) validateFlow(flow *openolt.Flow) error {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001770 // validate gemPort
1771 o.GemPortIDsLock.RLock()
Matteo Scandolo21195d62021-04-07 14:31:23 -07001772 defer o.GemPortIDsLock.RUnlock()
1773 for onuId, onu := range o.GemPortIDs[uint32(flow.AccessIntfId)] {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001774 if onuId == uint32(flow.OnuId) {
1775 continue
1776 }
1777 for uniId, uni := range onu {
1778 for gem := range uni {
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001779 if flow.ReplicateFlow {
1780 for _, flowGem := range flow.PbitToGemport {
1781 if gem == int32(flowGem) {
1782 return fmt.Errorf("gem-%d-already-in-use-on-uni-%d-onu-%d-replicated-flow-%d", gem, uniId, onuId, flow.FlowId)
1783 }
1784 }
1785 } else {
1786 if gem == flow.GemportId {
1787 return fmt.Errorf("gem-%d-already-in-use-on-uni-%d-onu-%d-flow-%d", gem, uniId, onuId, flow.FlowId)
1788 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001789 }
1790 }
1791 }
1792 }
1793
1794 o.AllocIDsLock.RLock()
Matteo Scandolo21195d62021-04-07 14:31:23 -07001795 defer o.AllocIDsLock.RUnlock()
1796 for onuId, onu := range o.AllocIDs[uint32(flow.AccessIntfId)] {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001797 if onuId == uint32(flow.OnuId) {
1798 continue
1799 }
1800 for uniId, uni := range onu {
1801 for allocId := range uni {
1802 if allocId == flow.AllocId {
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001803 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 -08001804 }
1805 }
1806 }
1807 }
1808
1809 return nil
1810}
1811
1812// clearAllResources is invoked up OLT Reboot to remove all the allocated
1813// GemPorts, AllocId and ONU-IDs across the PONs
1814func (o *OltDevice) clearAllResources() {
1815
1816 // remove the resources received via flows
1817 o.GemPortIDsLock.Lock()
1818 o.GemPortIDs = make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool)
1819 o.GemPortIDsLock.Unlock()
1820 o.AllocIDsLock.Lock()
1821 o.AllocIDs = make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool)
1822 o.AllocIDsLock.Unlock()
1823
1824 // remove the resources received via OMCI
1825 for _, pon := range o.Pons {
1826 pon.removeAllAllocIds()
1827 pon.removeAllGemPorts()
1828 pon.removeAllOnuIds()
1829 }
1830}