blob: c9068bd17116e188492288aaff829c180ac953d0 [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
Elia Battistonac63b112022-01-12 18:40:49 +010028 "github.com/opencord/voltha-protos/v5/go/extension"
29
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000030 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
31 "github.com/opencord/bbsim/internal/bbsim/types"
32 omcilib "github.com/opencord/bbsim/internal/common/omci"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000033 "github.com/opencord/voltha-protos/v5/go/ext/config"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000034
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070035 "github.com/google/gopacket"
36 "github.com/google/gopacket/layers"
Matteo Scandolo4747d292019-08-05 11:50:18 -070037 "github.com/looplab/fsm"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070038 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010039 "github.com/opencord/bbsim/internal/common"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000040 "github.com/opencord/voltha-protos/v5/go/openolt"
41 "github.com/opencord/voltha-protos/v5/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070042 log "github.com/sirupsen/logrus"
43 "google.golang.org/grpc"
Pragya Arya8bdb4532020-03-02 17:08:09 +053044 "google.golang.org/grpc/codes"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010045 "google.golang.org/grpc/reflection"
Pragya Arya8bdb4532020-03-02 17:08:09 +053046 "google.golang.org/grpc/status"
Matteo Scandolo4747d292019-08-05 11:50:18 -070047)
48
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070049var oltLogger = log.WithFields(log.Fields{
Matteo Scandolo84f7d482019-08-08 19:00:47 -070050 "module": "OLT",
51})
52
Matteo Scandolocedde462021-03-09 17:37:16 -080053const (
Elia Battiston67e9e4c2022-02-15 16:38:40 +010054 //InternalState FSM states and transitions
55 OltInternalStateCreated = "created"
56 OltInternalStateInitialized = "initialized"
57 OltInternalStateEnabled = "enabled"
58 OltInternalStateDisabled = "disabled"
59 OltInternalStateDeleted = "deleted"
60
61 OltInternalTxInitialize = "initialize"
62 OltInternalTxEnable = "enable"
63 OltInternalTxDisable = "disable"
64 OltInternalTxDelete = "delete"
Matteo Scandolocedde462021-03-09 17:37:16 -080065)
66
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070067type OltDevice struct {
David Bainbridge103cf022019-12-16 20:11:35 +000068 sync.Mutex
Hardik Windlassefdb4b62021-03-18 10:33:24 +000069 OltServer *grpc.Server
David Bainbridge103cf022019-12-16 20:11:35 +000070
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070071 // BBSIM Internals
Pragya Arya2225f202020-01-29 18:05:01 +053072 ID int
73 SerialNumber string
74 NumNni int
Elia Battiston420c9092022-02-02 12:17:54 +010075 NniSpeed uint32
Pragya Arya2225f202020-01-29 18:05:01 +053076 NumPon int
77 NumOnuPerPon int
Mahir Gunyela1753ae2021-06-23 00:24:56 -070078 NumUni int
Elia Battistonac63b112022-01-12 18:40:49 +010079 NumPots int
Pragya Arya2225f202020-01-29 18:05:01 +053080 InternalState *fsm.FSM
Matteo Scandolof9d43412021-01-12 11:11:34 -080081 channel chan types.Message
Matteo Scandolo90d08f62020-10-29 12:06:55 -070082 dhcpServer dhcp.DHCPServerIf
Andrea Campanellabe8e12f2020-12-14 18:43:41 +010083 Flows sync.Map
Pragya Arya2225f202020-01-29 18:05:01 +053084 Delay int
85 ControlledActivation mode
Pragya Arya324337e2020-02-20 14:35:08 +053086 EventChannel chan common.Event
87 PublishEvents bool
Pragya Arya996a0892020-03-09 21:47:52 +053088 PortStatsInterval int
Matteo Scandolo96f89192021-03-12 13:17:26 -080089 PreviouslyConnected bool
Matteo Scandoloe33447a2019-10-31 12:38:23 -070090
Matteo Scandolo27428702019-10-11 16:21:16 -070091 Pons []*PonPort
92 Nnis []*NniPort
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070093
94 // OLT Attributes
95 OperState *fsm.FSM
David Bainbridge103cf022019-12-16 20:11:35 +000096
97 enableContext context.Context
98 enableContextCancel context.CancelFunc
Pragya Arya1cbefa42020-01-13 12:15:29 +053099
Matteo Scandolo4a036262020-08-17 15:56:13 -0700100 OpenoltStream openolt.Openolt_EnableIndicationServer
Anand S Katti09541352020-01-29 15:54:01 +0530101 enablePerf bool
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800102
103 // Allocated Resources
104 // this data are to verify that the openolt adapter does not duplicate resources
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000105 AllocIDsLock sync.RWMutex
106 AllocIDs map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool // map[ponPortId]map[OnuId]map[PortNo]map[AllocIds]map[FlowId]bool
107 GemPortIDsLock sync.RWMutex
108 GemPortIDs map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool // map[ponPortId]map[OnuId]map[PortNo]map[GemPortIDs]map[FlowId]bool
109 OmciResponseRate uint8
Matteo Scandolo4747d292019-08-05 11:50:18 -0700110}
111
Matteo Scandolo27428702019-10-11 16:21:16 -0700112var olt OltDevice
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700113
Matteo Scandolo27428702019-10-11 16:21:16 -0700114func GetOLT() *OltDevice {
115 return &olt
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700116}
117
Matteo Scandolo4a036262020-08-17 15:56:13 -0700118func CreateOLT(options common.GlobalConfig, services []common.ServiceYaml, isMock bool) *OltDevice {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700119 oltLogger.WithFields(log.Fields{
Pragya Arya996a0892020-03-09 21:47:52 +0530120 "ID": options.Olt.ID,
121 "NumNni": options.Olt.NniPorts,
Elia Battiston420c9092022-02-02 12:17:54 +0100122 "NniSpeed": options.Olt.NniSpeed,
Pragya Arya996a0892020-03-09 21:47:52 +0530123 "NumPon": options.Olt.PonPorts,
124 "NumOnuPerPon": options.Olt.OnusPonPort,
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700125 "NumUni": options.Olt.UniPorts,
Elia Battistonac63b112022-01-12 18:40:49 +0100126 "NumPots": options.Olt.PotsPorts,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700127 }).Debug("CreateOLT")
128
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700129 olt = OltDevice{
Pragya Arya996a0892020-03-09 21:47:52 +0530130 ID: options.Olt.ID,
131 SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", options.Olt.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700132 OperState: getOperStateFSM(func(e *fsm.Event) {
133 oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst)
134 }),
Matteo Scandolo96f89192021-03-12 13:17:26 -0800135 NumNni: int(options.Olt.NniPorts),
Elia Battiston420c9092022-02-02 12:17:54 +0100136 NniSpeed: options.Olt.NniSpeed,
Matteo Scandolo96f89192021-03-12 13:17:26 -0800137 NumPon: int(options.Olt.PonPorts),
138 NumOnuPerPon: int(options.Olt.OnusPonPort),
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700139 NumUni: int(options.Olt.UniPorts),
Elia Battistonac63b112022-01-12 18:40:49 +0100140 NumPots: int(options.Olt.PotsPorts),
Matteo Scandolo96f89192021-03-12 13:17:26 -0800141 Pons: []*PonPort{},
142 Nnis: []*NniPort{},
143 Delay: options.BBSim.Delay,
144 enablePerf: options.BBSim.EnablePerf,
145 PublishEvents: options.BBSim.Events,
146 PortStatsInterval: options.Olt.PortStatsInterval,
147 dhcpServer: dhcp.NewDHCPServer(),
148 PreviouslyConnected: false,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800149 AllocIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool),
150 GemPortIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool),
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000151 OmciResponseRate: options.Olt.OmciResponseRate,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700152 }
153
Pragya Arya996a0892020-03-09 21:47:52 +0530154 if val, ok := ControlledActivationModes[options.BBSim.ControlledActivation]; ok {
Pragya Arya2225f202020-01-29 18:05:01 +0530155 olt.ControlledActivation = val
156 } else {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700157 // FIXME throw an error if the ControlledActivation is not valid
Pragya Arya2225f202020-01-29 18:05:01 +0530158 oltLogger.Warn("Unknown ControlledActivation Mode given, running in Default mode")
159 olt.ControlledActivation = Default
160 }
161
Matteo Scandolo4747d292019-08-05 11:50:18 -0700162 // OLT State machine
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700163 // NOTE do we need 2 state machines for the OLT? (InternalState and OperState)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700164 olt.InternalState = fsm.NewFSM(
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100165 OltInternalStateCreated,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700166 fsm.Events{
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100167 {Name: OltInternalTxInitialize, Src: []string{OltInternalStateCreated, OltInternalStateDeleted}, Dst: OltInternalStateInitialized},
168 {Name: OltInternalTxEnable, Src: []string{OltInternalStateInitialized, OltInternalStateDisabled}, Dst: OltInternalStateEnabled},
169 {Name: OltInternalTxDisable, Src: []string{OltInternalStateEnabled}, Dst: OltInternalStateDisabled},
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700170 // delete event in enabled state below is for reboot OLT case.
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100171 {Name: OltInternalTxDelete, Src: []string{OltInternalStateDisabled, OltInternalStateEnabled}, Dst: OltInternalStateDeleted},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700172 },
173 fsm.Callbacks{
174 "enter_state": func(e *fsm.Event) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700175 oltLogger.Debugf("Changing OLT InternalState from %s to %s", e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700176 },
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100177 fmt.Sprintf("enter_%s", OltInternalStateInitialized): func(e *fsm.Event) { olt.InitOlt() },
178 fmt.Sprintf("enter_%s", OltInternalStateDeleted): func(e *fsm.Event) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800179 // remove all the resource allocations
180 olt.clearAllResources()
181 },
Matteo Scandolo4747d292019-08-05 11:50:18 -0700182 },
183 )
184
Shrey Baid688b4242020-07-10 20:40:10 +0530185 if !isMock {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700186 // create NNI Port
187 nniPort, err := CreateNNI(&olt)
188 if err != nil {
189 oltLogger.Fatalf("Couldn't create NNI Port: %v", err)
190 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700191
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700192 olt.Nnis = append(olt.Nnis, &nniPort)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700193 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700194
Matteo Scandolo4a036262020-08-17 15:56:13 -0700195 // Create device and Services
Matteo Scandolo4a036262020-08-17 15:56:13 -0700196 nextCtag := map[string]int{}
197 nextStag := map[string]int{}
198
Matteo Scandolo4747d292019-08-05 11:50:18 -0700199 // create PON ports
Matteo Scandolo4a036262020-08-17 15:56:13 -0700200 for i := 0; i < olt.NumPon; i++ {
Elia Battistonb7bea222022-02-18 16:25:00 +0100201 ponConf, err := common.GetPonConfigById(uint32(i))
202 if err != nil {
203 oltLogger.WithFields(log.Fields{
204 "Err": err,
205 "IntfId": i,
206 }).Fatal("cannot-get-pon-configuration")
207 }
208
209 tech, err := common.PonTechnologyFromString(ponConf.Technology)
210 if err != nil {
211 oltLogger.WithFields(log.Fields{
212 "Err": err,
213 "IntfId": i,
214 }).Fatal("unkown-pon-port-technology")
215 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800216
217 // initialize the resource maps for every PON Ports
218 olt.AllocIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
219 olt.GemPortIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
220
Elia Battistonb7bea222022-02-18 16:25:00 +0100221 p := CreatePonPort(&olt, uint32(i), tech)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700222
Matteo Scandolo4a036262020-08-17 15:56:13 -0700223 // create ONU devices
Elia Battistonb7bea222022-02-18 16:25:00 +0100224 if (ponConf.OnuRange.EndId - ponConf.OnuRange.StartId + 1) < uint32(olt.NumOnuPerPon) {
225 oltLogger.WithFields(log.Fields{
226 "OnuRange": ponConf.OnuRange,
227 "RangeSize": ponConf.OnuRange.EndId - ponConf.OnuRange.StartId + 1,
228 "NumOnuPerPon": olt.NumOnuPerPon,
229 "IntfId": i,
230 }).Fatal("onus-per-pon-bigger-than-resource-range-size")
231 }
232
Matteo Scandolo4a036262020-08-17 15:56:13 -0700233 for j := 0; j < olt.NumOnuPerPon; j++ {
234 delay := time.Duration(olt.Delay*j) * time.Millisecond
Matteo Scandolo8a574812021-05-20 15:18:53 -0700235 o := CreateONU(&olt, p, uint32(j+1), delay, nextCtag, nextStag, isMock)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700236
Matteo Scandolo4a036262020-08-17 15:56:13 -0700237 p.Onus = append(p.Onus, o)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700238 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700239 olt.Pons = append(olt.Pons, p)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700240 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100241
Shrey Baid688b4242020-07-10 20:40:10 +0530242 if !isMock {
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100243 if err := olt.InternalState.Event(OltInternalTxInitialize); err != nil {
Matteo Scandolod32c3822019-11-26 15:57:46 -0700244 log.Errorf("Error initializing OLT: %v", err)
245 return nil
246 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100247 }
248
Pragya Arya324337e2020-02-20 14:35:08 +0530249 if olt.PublishEvents {
250 log.Debugf("BBSim event publishing is enabled")
251 // Create a channel to write event messages
252 olt.EventChannel = make(chan common.Event, 100)
253 }
254
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700255 return &olt
256}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700257
Shrey Baid688b4242020-07-10 20:40:10 +0530258func (o *OltDevice) InitOlt() {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100259
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000260 if o.OltServer == nil {
261 o.OltServer, _ = o.StartOltServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100262 } else {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800263 oltLogger.Fatal("OLT server already running.")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100264 }
265
266 // create new channel for processOltMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800267 o.channel = make(chan types.Message)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100268
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100269 // FIXME we are assuming we have only one NNI
270 if o.Nnis[0] != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800271 // NOTE we want to make sure the state is down when we initialize the OLT,
272 // the NNI may be in a bad state after a disable/reboot as we are not disabling it for
273 // in-band management
274 o.Nnis[0].OperState.SetState("down")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100275 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800276
277 for ponId := range o.Pons {
278 // initialize the resource maps for every PON Ports
279 olt.AllocIDs[uint32(ponId)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
280 olt.GemPortIDs[uint32(ponId)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool)
281 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700282}
283
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800284func (o *OltDevice) RestartOLT() error {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100285
Matteo Scandolo96f89192021-03-12 13:17:26 -0800286 o.PreviouslyConnected = false
287
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700288 softReboot := false
Matteo Scandolo4a036262020-08-17 15:56:13 -0700289 rebootDelay := common.Config.Olt.OltRebootDelay
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800290
291 oltLogger.WithFields(log.Fields{
292 "oltId": o.ID,
293 }).Infof("Simulating OLT restart... (%ds)", rebootDelay)
294
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100295 if o.InternalState.Is(OltInternalStateEnabled) {
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700296 oltLogger.WithFields(log.Fields{
297 "oltId": o.ID,
298 }).Info("This is an OLT soft reboot")
299 softReboot = true
300 }
301
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800302 // transition internal state to deleted
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100303 if err := o.InternalState.Event(OltInternalTxDelete); err != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800304 oltLogger.WithFields(log.Fields{
305 "oltId": o.ID,
306 }).Errorf("Error deleting OLT: %v", err)
307 return err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100308 }
309
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700310 if softReboot {
311 for _, pon := range o.Pons {
312 if pon.InternalState.Current() == "enabled" {
313 // disable PONs
Matteo Scandolof9d43412021-01-12 11:11:34 -0800314 msg := types.Message{
315 Type: types.PonIndication,
316 Data: types.PonIndicationMessage{
317 OperState: types.DOWN,
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700318 PonPortID: pon.ID,
319 },
320 }
321 o.channel <- msg
322 }
323
324 for _, onu := range pon.Onus {
Matteo Scandolocedde462021-03-09 17:37:16 -0800325 _ = onu.InternalState.Event(OnuTxDisable)
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700326 }
327 }
328 } else {
329 // PONs are already handled in the Disable call
330 for _, pon := range olt.Pons {
331 // ONUs are not automatically disabled when a PON goes down
332 // as it's possible that it's an admin down and in that case the ONUs need to keep their state
333 for _, onu := range pon.Onus {
Matteo Scandolocedde462021-03-09 17:37:16 -0800334 _ = onu.InternalState.Event(OnuTxDisable)
Matteo Scandolo635b2bf2020-09-04 10:23:40 -0700335 }
Pragya Arya2225f202020-01-29 18:05:01 +0530336 }
337 }
338
Matteo Scandolob307d8a2021-05-10 15:19:27 -0700339 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
340 o.StopOltServer()
341
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100342 // terminate the OLT's processOltMessages go routine
343 close(o.channel)
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700344
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100345 //Prevents Enable to progress before the reboot is completed (VOL-4616)
346 o.Lock()
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)
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100349 o.Unlock()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100350
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100351 if err := o.InternalState.Event(OltInternalTxInitialize); err != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800352 oltLogger.WithFields(log.Fields{
353 "oltId": o.ID,
354 }).Errorf("Error initializing OLT: %v", err)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100355 return err
356 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800357 oltLogger.WithFields(log.Fields{
358 "oltId": o.ID,
359 }).Info("OLT restart completed")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100360 return nil
361}
362
363// newOltServer launches a new grpc server for OpenOLT
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800364func (o *OltDevice) newOltServer() (*grpc.Server, error) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700365 address := common.Config.BBSim.OpenOltAddress
Matteo Scandolo4747d292019-08-05 11:50:18 -0700366 lis, err := net.Listen("tcp", address)
367 if err != nil {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700368 oltLogger.Fatalf("OLT failed to listen: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700369 }
370 grpcServer := grpc.NewServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100371
Matteo Scandolo4747d292019-08-05 11:50:18 -0700372 openolt.RegisterOpenoltServer(grpcServer, o)
373
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100374 reflection.Register(grpcServer)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700375
Shrey Baid688b4242020-07-10 20:40:10 +0530376 go func() { _ = grpcServer.Serve(lis) }()
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100377 oltLogger.Debugf("OLT listening on %v", address)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700378
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100379 return grpcServer, nil
380}
381
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800382// StartOltServer will create the grpc server that VOLTHA uses
383// to communicate with the device
384func (o *OltDevice) StartOltServer() (*grpc.Server, error) {
385 oltServer, err := o.newOltServer()
386 if err != nil {
387 oltLogger.WithFields(log.Fields{
388 "err": err,
389 }).Error("Cannot OLT gRPC server")
390 return nil, err
391 }
392 return oltServer, nil
393}
394
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100395// StopOltServer stops the OpenOLT grpc server
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800396func (o *OltDevice) StopOltServer() {
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000397 if o.OltServer != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800398 oltLogger.WithFields(log.Fields{
399 "oltId": o.SerialNumber,
400 }).Warnf("Stopping OLT gRPC server")
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000401 o.OltServer.Stop()
402 o.OltServer = nil
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700403 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700404}
405
406// Device Methods
407
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100408// Enable implements the OpenOLT EnableIndicationServer functionality
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100409func (o *OltDevice) Enable(stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700410 oltLogger.Debug("Enable OLT called")
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100411
412 if o.InternalState.Is(OltInternalStateDeleted) {
413 err := fmt.Errorf("Cannot enable OLT while it is rebooting")
414 oltLogger.WithFields(log.Fields{
415 "oltId": o.SerialNumber,
416 "internalState": o.InternalState.Current(),
417 }).Error(err)
418 return err
419 }
420
Pragya Arya2225f202020-01-29 18:05:01 +0530421 rebootFlag := false
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700422
David Bainbridge103cf022019-12-16 20:11:35 +0000423 // If enabled has already been called then an enabled context has
424 // been created. If this is the case then we want to cancel all the
425 // proessing loops associated with that enable before we recreate
426 // new ones
427 o.Lock()
428 if o.enableContext != nil && o.enableContextCancel != nil {
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700429 oltLogger.Info("This is an OLT reboot or a reconcile")
David Bainbridge103cf022019-12-16 20:11:35 +0000430 o.enableContextCancel()
Pragya Arya2225f202020-01-29 18:05:01 +0530431 rebootFlag = true
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700432 time.Sleep(1 * time.Second)
David Bainbridge103cf022019-12-16 20:11:35 +0000433 }
434 o.enableContext, o.enableContextCancel = context.WithCancel(context.TODO())
435 o.Unlock()
436
Matteo Scandolo4747d292019-08-05 11:50:18 -0700437 wg := sync.WaitGroup{}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700438
Matteo Scandolo4a036262020-08-17 15:56:13 -0700439 o.OpenoltStream = stream
Pragya Arya1cbefa42020-01-13 12:15:29 +0530440
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100441 // create Go routine to process all OLT events
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700442 wg.Add(1)
David Bainbridge103cf022019-12-16 20:11:35 +0000443 go o.processOltMessages(o.enableContext, stream, &wg)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700444
445 // enable the OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -0800446 oltMsg := types.Message{
447 Type: types.OltIndication,
448 Data: types.OltIndicationMessage{
449 OperState: types.UP,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700450 },
451 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100452 o.channel <- oltMsg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700453
454 // send NNI Port Indications
455 for _, nni := range o.Nnis {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800456 msg := types.Message{
457 Type: types.NniIndication,
458 Data: types.NniIndicationMessage{
459 OperState: types.UP,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700460 NniPortID: nni.ID,
461 },
462 }
463 o.channel <- msg
464 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100465
Shrey Baid688b4242020-07-10 20:40:10 +0530466 if rebootFlag {
Pragya Arya2225f202020-01-29 18:05:01 +0530467 for _, pon := range o.Pons {
468 if pon.InternalState.Current() == "disabled" {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800469 msg := types.Message{
470 Type: types.PonIndication,
471 Data: types.PonIndicationMessage{
472 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +0530473 PonPortID: pon.ID,
474 },
475 }
476 o.channel <- msg
Matteo Scandoloe60a5052020-02-07 00:31:14 +0000477 }
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700478 // when the enableContext was canceled the ONUs stopped listening on the channel
479 for _, onu := range pon.Onus {
480 go onu.ProcessOnuMessages(o.enableContext, stream, nil)
481
482 // update the stream on all the services
Matteo Scandolo8a574812021-05-20 15:18:53 -0700483 for _, uni := range onu.UniPorts {
484 uni.UpdateStream(stream)
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700485 }
486 }
Pragya Arya2225f202020-01-29 18:05:01 +0530487 }
488 } else {
489
490 // 1. controlledActivation == Default: Send both PON and ONUs indications
491 // 2. controlledActivation == only-onu: that means only ONUs will be controlled activated, so auto send PON indications
492
493 if o.ControlledActivation == Default || o.ControlledActivation == OnlyONU {
494 // send PON Port indications
495 for _, pon := range o.Pons {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800496 msg := types.Message{
497 Type: types.PonIndication,
498 Data: types.PonIndicationMessage{
499 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +0530500 PonPortID: pon.ID,
501 },
502 }
503 o.channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700504 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700505 }
506 }
507
Pragya Arya996a0892020-03-09 21:47:52 +0530508 if !o.enablePerf {
509 // Start a go routine to send periodic port stats to openolt adapter
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700510 wg.Add(1)
511 go o.periodicPortStats(o.enableContext, &wg, stream)
Pragya Arya996a0892020-03-09 21:47:52 +0530512 }
513
Matteo Scandolo4747d292019-08-05 11:50:18 -0700514 wg.Wait()
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700515 oltLogger.WithFields(log.Fields{
516 "stream": stream,
517 }).Debug("OpenOLT Stream closed")
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100518
519 return nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700520}
521
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700522func (o *OltDevice) periodicPortStats(ctx context.Context, wg *sync.WaitGroup, stream openolt.Openolt_EnableIndicationServer) {
Pragya Arya996a0892020-03-09 21:47:52 +0530523 var portStats *openolt.PortStatistics
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700524
525loop:
Pragya Arya996a0892020-03-09 21:47:52 +0530526 for {
527 select {
528 case <-time.After(time.Duration(o.PortStatsInterval) * time.Second):
529 // send NNI port stats
530 for _, port := range o.Nnis {
531 incrementStat := true
532 if port.OperState.Current() == "down" {
533 incrementStat = false
534 }
535 portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat)
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700536 o.sendPortStatsIndication(portStats, port.ID, port.Type, stream)
Pragya Arya996a0892020-03-09 21:47:52 +0530537 }
538
539 // send PON port stats
540 for _, port := range o.Pons {
541 incrementStat := true
542 // do not increment port stats if PON port is down or no ONU is activated on PON port
543 if port.OperState.Current() == "down" || port.GetNumOfActiveOnus() < 1 {
544 incrementStat = false
545 }
546 portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat)
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700547 o.sendPortStatsIndication(portStats, port.ID, port.Type, stream)
Pragya Arya996a0892020-03-09 21:47:52 +0530548 }
549 case <-ctx.Done():
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700550 oltLogger.Debug("Stop sending port stats")
551 break loop
Pragya Arya996a0892020-03-09 21:47:52 +0530552 }
Pragya Arya996a0892020-03-09 21:47:52 +0530553 }
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700554 wg.Done()
Pragya Arya996a0892020-03-09 21:47:52 +0530555}
556
Matteo Scandolo4747d292019-08-05 11:50:18 -0700557// Helpers method
558
Matteo Scandolof9d43412021-01-12 11:11:34 -0800559func (o *OltDevice) SetAlarm(interfaceId uint32, interfaceType string, alarmStatus string) error {
560
561 switch interfaceType {
562 case "nni":
563 if !o.HasNni(interfaceId) {
564 return status.Errorf(codes.NotFound, strconv.Itoa(int(interfaceId))+" NNI not present in olt")
565 }
566
567 case "pon":
568 if !o.HasPon(interfaceId) {
569 return status.Errorf(codes.NotFound, strconv.Itoa(int(interfaceId))+" PON not present in olt")
570 }
571 }
572
573 alarmIndication := &openolt.AlarmIndication{
574 Data: &openolt.AlarmIndication_LosInd{LosInd: &openolt.LosIndication{
575 Status: alarmStatus,
576 IntfId: InterfaceIDToPortNo(interfaceId, interfaceType),
577 }},
578 }
579
580 msg := types.Message{
581 Type: types.AlarmIndication,
582 Data: alarmIndication,
583 }
584
585 o.channel <- msg
586
587 return nil
588}
589
590func (o *OltDevice) HasNni(id uint32) bool {
591 for _, intf := range o.Nnis {
592 if intf.ID == id {
593 return true
594 }
595 }
596 return false
597}
598
599func (o *OltDevice) HasPon(id uint32) bool {
600 for _, intf := range o.Pons {
601 if intf.ID == id {
602 return true
603 }
604 }
605 return false
606}
607
Shrey Baid688b4242020-07-10 20:40:10 +0530608func (o *OltDevice) GetPonById(id uint32) (*PonPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700609 for _, pon := range o.Pons {
610 if pon.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700611 return pon, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700612 }
613 }
Shrey Baid688b4242020-07-10 20:40:10 +0530614 return nil, fmt.Errorf("Cannot find PonPort with id %d in OLT %d", id, o.ID)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700615}
616
Shrey Baid688b4242020-07-10 20:40:10 +0530617func (o *OltDevice) getNniById(id uint32) (*NniPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700618 for _, nni := range o.Nnis {
619 if nni.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700620 return nni, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700621 }
622 }
Shrey Baid688b4242020-07-10 20:40:10 +0530623 return nil, fmt.Errorf("Cannot find NniPort with id %d in OLT %d", id, o.ID)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700624}
625
Scott Baker41724b82020-01-21 19:54:53 -0800626func (o *OltDevice) sendAlarmIndication(alarmInd *openolt.AlarmIndication, stream openolt.Openolt_EnableIndicationServer) {
627 data := &openolt.Indication_AlarmInd{AlarmInd: alarmInd}
628 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
629 oltLogger.Errorf("Failed to send Alarm Indication: %v", err)
630 return
631 }
632
633 oltLogger.WithFields(log.Fields{
634 "AlarmIndication": alarmInd,
635 }).Debug("Sent Indication_AlarmInd")
636}
637
Matteo Scandolof9d43412021-01-12 11:11:34 -0800638func (o *OltDevice) sendOltIndication(msg types.OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700639 data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}}
640 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700641 oltLogger.Errorf("Failed to send Indication_OltInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800642 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700643 }
644
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700645 oltLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700646 "OperState": msg.OperState,
647 }).Debug("Sent Indication_OltInd")
648}
649
Matteo Scandolof9d43412021-01-12 11:11:34 -0800650func (o *OltDevice) sendNniIndication(msg types.NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700651 nni, _ := o.getNniById(msg.NniPortID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800652 if msg.OperState == types.UP {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800653 if err := nni.OperState.Event("enable"); err != nil {
654 log.WithFields(log.Fields{
655 "Type": nni.Type,
656 "IntfId": nni.ID,
657 "OperState": nni.OperState.Current(),
658 }).Errorf("Can't move NNI Port to enabled state: %v", err)
659 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800660 } else if msg.OperState == types.DOWN {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800661 if err := nni.OperState.Event("disable"); err != nil {
662 log.WithFields(log.Fields{
663 "Type": nni.Type,
664 "IntfId": nni.ID,
665 "OperState": nni.OperState.Current(),
666 }).Errorf("Can't move NNI Port to disable state: %v", err)
667 }
668 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700669 // NOTE Operstate may need to be an integer
Matteo Scandolo4747d292019-08-05 11:50:18 -0700670 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700671 Type: nni.Type,
672 IntfId: nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700673 OperState: nni.OperState.Current(),
Elia Battiston420c9092022-02-02 12:17:54 +0100674 Speed: o.NniSpeed,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700675 }}
676
677 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700678 oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800679 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700680 }
681
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700682 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700683 "Type": nni.Type,
684 "IntfId": nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700685 "OperState": nni.OperState.Current(),
Elia Battiston420c9092022-02-02 12:17:54 +0100686 "Speed": o.NniSpeed,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700687 }).Debug("Sent Indication_IntfOperInd for NNI")
688}
689
Pragya Arya2225f202020-01-29 18:05:01 +0530690func (o *OltDevice) sendPonIndication(ponPortID uint32) {
691
Matteo Scandolo4a036262020-08-17 15:56:13 -0700692 stream := o.OpenoltStream
Pragya Arya2225f202020-01-29 18:05:01 +0530693 pon, _ := o.GetPonById(ponPortID)
694 // Send IntfIndication for PON port
Matteo Scandolo4747d292019-08-05 11:50:18 -0700695 discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700696 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700697 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700698 }}
699
700 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700701 oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800702 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700703 }
704
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700705 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700706 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700707 "OperState": pon.OperState.Current(),
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700708 }).Debug("Sent Indication_IntfInd for PON")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700709
Pragya Arya2225f202020-01-29 18:05:01 +0530710 // Send IntfOperIndication for PON port
Matteo Scandolo4747d292019-08-05 11:50:18 -0700711 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700712 Type: pon.Type,
713 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700714 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700715 }}
716
717 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700718 oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800719 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700720 }
721
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700722 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700723 "Type": pon.Type,
724 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700725 "OperState": pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700726 }).Debug("Sent Indication_IntfOperInd for PON")
727}
728
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700729func (o *OltDevice) sendPortStatsIndication(stats *openolt.PortStatistics, portID uint32, portType string, stream openolt.Openolt_EnableIndicationServer) {
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100730 if o.InternalState.Current() == OltInternalStateEnabled {
Shrey Baid55f328c2020-07-07 19:20:42 +0530731 oltLogger.WithFields(log.Fields{
732 "Type": portType,
733 "IntfId": portID,
734 }).Trace("Sending port stats")
735 stats.IntfId = InterfaceIDToPortNo(portID, portType)
736 data := &openolt.Indication_PortStats{
737 PortStats: stats,
738 }
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700739
Shrey Baid55f328c2020-07-07 19:20:42 +0530740 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
741 oltLogger.Errorf("Failed to send PortStats: %v", err)
742 return
743 }
Pragya Arya996a0892020-03-09 21:47:52 +0530744 }
745}
746
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100747// processOltMessages handles messages received over the OpenOLT interface
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700748func (o *OltDevice) processOltMessages(ctx context.Context, stream types.Stream, wg *sync.WaitGroup) {
749 oltLogger.WithFields(log.Fields{
750 "stream": stream,
751 }).Debug("Starting OLT Indication Channel")
David Bainbridge103cf022019-12-16 20:11:35 +0000752 ch := o.channel
Matteo Scandolo4747d292019-08-05 11:50:18 -0700753
David Bainbridge103cf022019-12-16 20:11:35 +0000754loop:
755 for {
756 select {
757 case <-ctx.Done():
758 oltLogger.Debug("OLT Indication processing canceled via context")
759 break loop
Matteo Scandoloba3e9cd2021-05-14 16:19:54 -0700760 // do not terminate this loop if the stream is closed,
761 // when we restart the gRPC server it will automatically reconnect and we need this loop to send indications
762 //case <-stream.Context().Done():
763 // oltLogger.Debug("OLT Indication processing canceled via stream context")
764 // break loop
David Bainbridge103cf022019-12-16 20:11:35 +0000765 case message, ok := <-ch:
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700766 if !ok {
767 if ctx.Err() != nil {
768 oltLogger.WithField("err", ctx.Err()).Error("OLT EnableContext error")
769 }
770 oltLogger.Warn("OLT Indication processing canceled via closed channel")
David Bainbridge103cf022019-12-16 20:11:35 +0000771 break loop
Matteo Scandolo4747d292019-08-05 11:50:18 -0700772 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700773
David Bainbridge103cf022019-12-16 20:11:35 +0000774 oltLogger.WithFields(log.Fields{
775 "oltId": o.ID,
776 "messageType": message.Type,
777 }).Trace("Received message")
778
779 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800780 case types.OltIndication:
781 msg, _ := message.Data.(types.OltIndicationMessage)
782 if msg.OperState == types.UP {
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100783 _ = o.InternalState.Event(OltInternalTxEnable)
Shrey Baid688b4242020-07-10 20:40:10 +0530784 _ = o.OperState.Event("enable")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800785 } else if msg.OperState == types.DOWN {
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100786 _ = o.InternalState.Event(OltInternalTxDisable)
Shrey Baid688b4242020-07-10 20:40:10 +0530787 _ = o.OperState.Event("disable")
David Bainbridge103cf022019-12-16 20:11:35 +0000788 }
789 o.sendOltIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800790 case types.AlarmIndication:
Scott Baker41724b82020-01-21 19:54:53 -0800791 alarmInd, _ := message.Data.(*openolt.AlarmIndication)
792 o.sendAlarmIndication(alarmInd, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800793 case types.NniIndication:
794 msg, _ := message.Data.(types.NniIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000795 o.sendNniIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800796 case types.PonIndication:
797 msg, _ := message.Data.(types.PonIndicationMessage)
Pragya Arya2225f202020-01-29 18:05:01 +0530798 pon, _ := o.GetPonById(msg.PonPortID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800799 if msg.OperState == types.UP {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530800 if err := pon.OperState.Event("enable"); err != nil {
801 oltLogger.WithFields(log.Fields{
802 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800803 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530804 }).Error("Can't Enable Oper state for PON Port")
805 }
806 if err := pon.InternalState.Event("enable"); err != nil {
807 oltLogger.WithFields(log.Fields{
808 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800809 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530810 }).Error("Can't Enable Internal state for PON Port")
811 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800812 } else if msg.OperState == types.DOWN {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530813 if err := pon.OperState.Event("disable"); err != nil {
814 oltLogger.WithFields(log.Fields{
815 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800816 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530817 }).Error("Can't Disable Oper state for PON Port")
818 }
819 if err := pon.InternalState.Event("disable"); err != nil {
820 oltLogger.WithFields(log.Fields{
821 "IntfId": msg.PonPortID,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800822 "Err": err,
Hardik Windlassad790cb2020-06-17 21:26:22 +0530823 }).Error("Can't Disable Internal state for PON Port")
824 }
Pragya Arya2225f202020-01-29 18:05:01 +0530825 }
David Bainbridge103cf022019-12-16 20:11:35 +0000826 default:
827 oltLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
828 }
829 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700830 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100831 wg.Done()
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700832 oltLogger.WithFields(log.Fields{
833 "stream": stream,
834 }).Warn("Stopped handling OLT Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700835}
836
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700837// returns an ONU with a given Serial Number
Shrey Baid688b4242020-07-10 20:40:10 +0530838func (o *OltDevice) FindOnuBySn(serialNumber string) (*Onu, error) {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700839 // NOTE this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700840 // memoizing it will remove the bottleneck
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700841 for _, pon := range o.Pons {
842 for _, onu := range pon.Onus {
843 if onu.Sn() == serialNumber {
Matteo Scandolo27428702019-10-11 16:21:16 -0700844 return onu, nil
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700845 }
846 }
847 }
848
Shrey Baid688b4242020-07-10 20:40:10 +0530849 return &Onu{}, fmt.Errorf("cannot-find-onu-by-serial-number-%s", serialNumber)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700850}
851
William Kurkian9dadc5b2019-10-22 13:51:57 -0400852// returns an ONU with a given interface/Onu Id
Shrey Baid688b4242020-07-10 20:40:10 +0530853func (o *OltDevice) FindOnuById(intfId uint32, onuId uint32) (*Onu, error) {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700854 // NOTE this function can be a performance bottleneck when we have many ONUs,
William Kurkian9dadc5b2019-10-22 13:51:57 -0400855 // memoizing it will remove the bottleneck
856 for _, pon := range o.Pons {
857 if pon.ID == intfId {
858 for _, onu := range pon.Onus {
859 if onu.ID == onuId {
860 return onu, nil
861 }
862 }
863 }
864 }
Shrey Baid688b4242020-07-10 20:40:10 +0530865 return &Onu{}, fmt.Errorf("cannot-find-onu-by-id-%v-%v", intfId, onuId)
William Kurkian9dadc5b2019-10-22 13:51:57 -0400866}
867
Matteo Scandolo4a036262020-08-17 15:56:13 -0700868// returns a Service with a given Mac Address
869func (o *OltDevice) FindServiceByMacAddress(mac net.HardwareAddr) (ServiceIf, error) {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700870 // NOTE this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700871 // memoizing it will remove the bottleneck
872 for _, pon := range o.Pons {
873 for _, onu := range pon.Onus {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700874 s, err := onu.findServiceByMacAddress(mac)
875 if err == nil {
876 return s, nil
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700877 }
878 }
879 }
880
Matteo Scandolo4a036262020-08-17 15:56:13 -0700881 return nil, fmt.Errorf("cannot-find-service-by-mac-address-%s", mac)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700882}
883
Matteo Scandolo4747d292019-08-05 11:50:18 -0700884// GRPC Endpoints
885
Shrey Baid688b4242020-07-10 20:40:10 +0530886func (o *OltDevice) ActivateOnu(context context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700887
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700888 pon, _ := o.GetPonById(onu.IntfId)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800889
Matteo Scandolo8a574812021-05-20 15:18:53 -0700890 // Enable the resource maps for this ONU
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800891 olt.AllocIDs[onu.IntfId][onu.OnuId] = make(map[uint32]map[int32]map[uint64]bool)
892 olt.GemPortIDs[onu.IntfId][onu.OnuId] = make(map[uint32]map[int32]map[uint64]bool)
893
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700894 _onu, _ := pon.GetOnuBySn(onu.SerialNumber)
Matteo Scandoloba3e9cd2021-05-14 16:19:54 -0700895
896 publishEvent("ONU-activate-indication-received", int32(onu.IntfId), int32(onu.OnuId), _onu.Sn())
897 oltLogger.WithFields(log.Fields{
898 "OnuSn": _onu.Sn(),
899 }).Info("Received ActivateOnu call from VOLTHA")
900
William Kurkian0418bc82019-11-06 12:16:24 -0500901 _onu.SetID(onu.OnuId)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700902
Matteo Scandolocedde462021-03-09 17:37:16 -0800903 if err := _onu.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700904 oltLogger.WithFields(log.Fields{
905 "IntfId": _onu.PonPortID,
906 "OnuSn": _onu.Sn(),
907 "OnuId": _onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800908 }).Infof("Failed to transition ONU to %s state: %s", OnuStateEnabled, err.Error())
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700909 }
910
911 // NOTE we need to immediately activate the ONU or the OMCI state machine won't start
912
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700913 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700914}
915
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800916func (o *OltDevice) DeactivateOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700917 oltLogger.Error("DeactivateOnu not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700918 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700919}
920
Shrey Baid688b4242020-07-10 20:40:10 +0530921func (o *OltDevice) DeleteOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Pragya Arya1cbefa42020-01-13 12:15:29 +0530922 oltLogger.WithFields(log.Fields{
923 "IntfId": onu.IntfId,
924 "OnuId": onu.OnuId,
925 }).Info("Received DeleteOnu call from VOLTHA")
926
927 pon, err := o.GetPonById(onu.IntfId)
928 if err != nil {
929 oltLogger.WithFields(log.Fields{
930 "OnuId": onu.OnuId,
931 "IntfId": onu.IntfId,
932 "err": err,
933 }).Error("Can't find PonPort")
934 }
935 _onu, err := pon.GetOnuById(onu.OnuId)
936 if err != nil {
937 oltLogger.WithFields(log.Fields{
938 "OnuId": onu.OnuId,
939 "IntfId": onu.IntfId,
940 "err": err,
941 }).Error("Can't find Onu")
942 }
943
Matteo Scandolocedde462021-03-09 17:37:16 -0800944 if err := _onu.InternalState.Event(OnuTxDisable); err != nil {
Pragya Arya1cbefa42020-01-13 12:15:29 +0530945 oltLogger.WithFields(log.Fields{
946 "IntfId": _onu.PonPortID,
947 "OnuSn": _onu.Sn(),
948 "OnuId": _onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800949 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDisabled, err.Error())
Hardik Windlassad790cb2020-06-17 21:26:22 +0530950 }
951
Hardik Windlassad790cb2020-06-17 21:26:22 +0530952 // ONU Re-Discovery
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100953 if o.InternalState.Current() == OltInternalStateEnabled && pon.InternalState.Current() == "enabled" {
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530954 go _onu.ReDiscoverOnu()
Pragya Arya1cbefa42020-01-13 12:15:29 +0530955 }
956
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700957 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700958}
959
Shrey Baid688b4242020-07-10 20:40:10 +0530960func (o *OltDevice) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700961 // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether?
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800962 oltLogger.WithFields(log.Fields{
963 "oltId": o.ID,
964 }).Info("Disabling OLT")
Pragya Arya324337e2020-02-20 14:35:08 +0530965 publishEvent("OLT-disable-received", -1, -1, "")
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800966
Matteo Scandolo401503a2019-12-11 14:48:14 -0800967 for _, pon := range o.Pons {
Pragya Arya2225f202020-01-29 18:05:01 +0530968 if pon.InternalState.Current() == "enabled" {
969 // disable PONs
Matteo Scandolof9d43412021-01-12 11:11:34 -0800970 msg := types.Message{
971 Type: types.PonIndication,
972 Data: types.PonIndicationMessage{
973 OperState: types.DOWN,
Pragya Arya2225f202020-01-29 18:05:01 +0530974 PonPortID: pon.ID,
975 },
976 }
977 o.channel <- msg
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800978 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800979 }
980
Matteo Scandolo401503a2019-12-11 14:48:14 -0800981 // Note that we are not disabling the NNI as the real OLT does not.
982 // The reason for that is in-band management
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800983
984 // disable OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -0800985 oltMsg := types.Message{
986 Type: types.OltIndication,
987 Data: types.OltIndicationMessage{
988 OperState: types.DOWN,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700989 },
990 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100991 o.channel <- oltMsg
Matteo Scandoloba3e9cd2021-05-14 16:19:54 -0700992
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700993 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700994}
995
Shrey Baid688b4242020-07-10 20:40:10 +0530996func (o *OltDevice) DisablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Hardik Windlassad790cb2020-06-17 21:26:22 +0530997 oltLogger.Infof("DisablePonIf request received for PON %d", intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +0200998 ponID := intf.GetIntfId()
999 pon, _ := o.GetPonById(intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001000
Matteo Scandolof9d43412021-01-12 11:11:34 -08001001 msg := types.Message{
1002 Type: types.PonIndication,
1003 Data: types.PonIndicationMessage{
1004 OperState: types.DOWN,
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001005 PonPortID: ponID,
1006 },
1007 }
1008 o.channel <- msg
1009
1010 for _, onu := range pon.Onus {
1011
Matteo Scandolof9d43412021-01-12 11:11:34 -08001012 onuIndication := types.OnuIndicationMessage{
1013 OperState: types.DOWN,
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001014 PonPortID: ponID,
1015 OnuID: onu.ID,
1016 OnuSN: onu.SerialNumber,
1017 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001018 onu.sendOnuIndication(onuIndication, o.OpenoltStream)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001019
1020 }
1021
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001022 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001023}
1024
Zdravko Bozakov681364d2019-11-10 14:28:46 +01001025func (o *OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001026 oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +05301027 publishEvent("OLT-enable-received", -1, -1, "")
Elia Battiston67e9e4c2022-02-15 16:38:40 +01001028 return o.Enable(stream)
Matteo Scandolo4747d292019-08-05 11:50:18 -07001029}
1030
Shrey Baid688b4242020-07-10 20:40:10 +05301031func (o *OltDevice) EnablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Hardik Windlassad790cb2020-06-17 21:26:22 +05301032 oltLogger.Infof("EnablePonIf request received for PON %d", intf.IntfId)
Pragya Arya2225f202020-01-29 18:05:01 +05301033 ponID := intf.GetIntfId()
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001034 pon, _ := o.GetPonById(intf.IntfId)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001035
Matteo Scandolof9d43412021-01-12 11:11:34 -08001036 msg := types.Message{
1037 Type: types.PonIndication,
1038 Data: types.PonIndicationMessage{
1039 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +05301040 PonPortID: ponID,
1041 },
1042 }
1043 o.channel <- msg
1044
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001045 for _, onu := range pon.Onus {
1046
Matteo Scandolof9d43412021-01-12 11:11:34 -08001047 onuIndication := types.OnuIndicationMessage{
1048 OperState: types.UP,
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001049 PonPortID: ponID,
1050 OnuID: onu.ID,
1051 OnuSN: onu.SerialNumber,
1052 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001053 onu.sendOnuIndication(onuIndication, o.OpenoltStream)
Andrea Campanella340b9ea2020-04-28 18:34:01 +02001054
1055 }
1056
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001057 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001058}
1059
Shrey Baid688b4242020-07-10 20:40:10 +05301060func (o *OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001061 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001062 "IntfId": flow.AccessIntfId,
1063 "OnuId": flow.OnuId,
1064 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001065 "InnerVlan": flow.Classifier.IVid,
1066 "OuterVlan": flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001067 "FlowType": flow.FlowType,
1068 "FlowId": flow.FlowId,
1069 "UniID": flow.UniId,
1070 "PortNo": flow.PortNo,
Pragya Arya8bdb4532020-03-02 17:08:09 +05301071 }).Tracef("OLT receives FlowAdd")
1072
Andrea Campanellacc9b1662022-01-26 17:47:48 +01001073 flowKey := FlowKey{}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301074 if !o.enablePerf {
yasin saplic07b9522022-01-27 11:23:54 +00001075 flowKey = FlowKey{ID: flow.FlowId}
Andrea Campanellacc9b1662022-01-26 17:47:48 +01001076 olt.Flows.Store(flowKey, *flow)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301077 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001078
1079 if flow.AccessIntfId == -1 {
1080 oltLogger.WithFields(log.Fields{
1081 "FlowId": flow.FlowId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001082 }).Debug("Adding OLT flow")
Jonathan Hartb5fc46a2020-03-31 16:42:31 -07001083 } else if flow.FlowType == "multicast" {
1084 oltLogger.WithFields(log.Fields{
Matteo Scandolo618a6582020-09-09 12:21:29 -07001085 "Cookie": flow.Cookie,
1086 "DstPort": flow.Classifier.DstPort,
1087 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
1088 "FlowId": flow.FlowId,
1089 "FlowType": flow.FlowType,
1090 "GemportId": flow.GemportId,
1091 "InnerVlan": flow.Classifier.IVid,
1092 "IntfId": flow.AccessIntfId,
1093 "IpProto": flow.Classifier.IpProto,
1094 "OnuId": flow.OnuId,
1095 "OuterVlan": flow.Classifier.OVid,
1096 "PortNo": flow.PortNo,
1097 "SrcPort": flow.Classifier.SrcPort,
1098 "UniID": flow.UniId,
1099 "ClassifierOPbits": flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001100 }).Debug("Adding OLT multicast flow")
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001101 } else {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001102 pon, err := o.GetPonById(uint32(flow.AccessIntfId))
Matteo Scandolo27428702019-10-11 16:21:16 -07001103 if err != nil {
1104 oltLogger.WithFields(log.Fields{
1105 "OnuId": flow.OnuId,
1106 "IntfId": flow.AccessIntfId,
1107 "err": err,
1108 }).Error("Can't find PonPort")
1109 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001110 onu, err := pon.GetOnuById(uint32(flow.OnuId))
Matteo Scandolo27428702019-10-11 16:21:16 -07001111 if err != nil {
1112 oltLogger.WithFields(log.Fields{
1113 "OnuId": flow.OnuId,
1114 "IntfId": flow.AccessIntfId,
1115 "err": err,
1116 }).Error("Can't find Onu")
Jonathan Hartb5fc46a2020-03-31 16:42:31 -07001117 return nil, err
Matteo Scandolo27428702019-10-11 16:21:16 -07001118 }
Matteo Scandoloba3e9cd2021-05-14 16:19:54 -07001119
1120 // if the ONU is disabled reject the flow
1121 // as per VOL-4061 there is a small window during which the ONU is disabled
1122 // but the port has not been reported as down to ONOS
1123 if onu.InternalState.Is(OnuStatePonDisabled) || onu.InternalState.Is(OnuStateDisabled) {
1124 oltLogger.WithFields(log.Fields{
1125 "OnuId": flow.OnuId,
1126 "IntfId": flow.AccessIntfId,
1127 "Flow": flow,
1128 "SerialNumber": onu.Sn(),
1129 "InternalState": onu.InternalState.Current(),
1130 }).Error("rejected-flow-because-of-onu-state")
1131 return nil, fmt.Errorf("onu-%s-is-currently-%s", onu.Sn(), onu.InternalState.Current())
1132 }
1133
Pragya Arya8bdb4532020-03-02 17:08:09 +05301134 if !o.enablePerf {
Andrea Campanellacc9b1662022-01-26 17:47:48 +01001135 onu.Flows = append(onu.Flows, flowKey)
Pragya Arya1d5ffb82020-03-20 18:51:37 +05301136 // Generate event on first flow for ONU
Andrea Campanellacc9b1662022-01-26 17:47:48 +01001137 if len(onu.Flows) == 1 {
Matteo Scandoloba3e9cd2021-05-14 16:19:54 -07001138 publishEvent("Flow-add-received", int32(onu.PonPortID), int32(onu.ID), onu.Sn())
Pragya Arya1d5ffb82020-03-20 18:51:37 +05301139 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301140 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001141
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001142 // validate that the flow reference correct IDs (Alloc, Gem)
1143 if err := o.validateFlow(flow); err != nil {
1144 oltLogger.WithFields(log.Fields{
1145 "OnuId": flow.OnuId,
1146 "IntfId": flow.AccessIntfId,
1147 "Flow": flow,
1148 "SerialNumber": onu.Sn(),
1149 "err": err,
1150 }).Error("invalid-flow-for-onu")
1151 return nil, err
1152 }
1153
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001154 o.storeGemPortIdByFlow(flow)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001155 o.storeAllocId(flow)
1156
Matteo Scandolof9d43412021-01-12 11:11:34 -08001157 msg := types.Message{
1158 Type: types.FlowAdd,
1159 Data: types.OnuFlowUpdateMessage{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001160 PonPortID: pon.ID,
1161 OnuID: onu.ID,
1162 Flow: flow,
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001163 },
1164 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001165 onu.Channel <- msg
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001166 }
1167
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001168 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001169}
1170
Pragya Arya8bdb4532020-03-02 17:08:09 +05301171// FlowRemove request from VOLTHA
Shrey Baid688b4242020-07-10 20:40:10 +05301172func (o *OltDevice) FlowRemove(_ context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001173
Pragya Arya8bdb4532020-03-02 17:08:09 +05301174 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001175 "AllocId": flow.AllocId,
1176 "Cookie": flow.Cookie,
1177 "FlowId": flow.FlowId,
1178 "FlowType": flow.FlowType,
1179 "GemportId": flow.GemportId,
1180 "IntfId": flow.AccessIntfId,
1181 "OnuId": flow.OnuId,
1182 "PortNo": flow.PortNo,
1183 "UniID": flow.UniId,
1184 "ReplicateFlow": flow.ReplicateFlow,
1185 "PbitToGemport": flow.PbitToGemport,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001186 }).Debug("OLT receives FlowRemove")
Pragya Arya8bdb4532020-03-02 17:08:09 +05301187
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001188 olt.freeGemPortId(flow)
1189 olt.freeAllocId(flow)
1190
Pragya Arya8bdb4532020-03-02 17:08:09 +05301191 if !o.enablePerf { // remove only if flow were stored
yasin saplic07b9522022-01-27 11:23:54 +00001192 flowKey := FlowKey{ID: flow.FlowId}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301193 // Check if flow exists
Andrea Campanellacc9b1662022-01-26 17:47:48 +01001194 storedFlowIntf, ok := o.Flows.Load(flowKey)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301195 if !ok {
1196 oltLogger.Errorf("Flow %v not found", flow)
1197 return new(openolt.Empty), status.Errorf(codes.NotFound, "Flow not found")
1198 }
1199
Andrea Campanellabe8e12f2020-12-14 18:43:41 +01001200 storedFlow := storedFlowIntf.(openolt.Flow)
1201
Pragya Arya8bdb4532020-03-02 17:08:09 +05301202 // if its ONU flow remove it from ONU also
1203 if storedFlow.AccessIntfId != -1 {
Matteo Scandolocedde462021-03-09 17:37:16 -08001204 pon, err := o.GetPonById(uint32(storedFlow.AccessIntfId))
1205 if err != nil {
1206 oltLogger.WithFields(log.Fields{
1207 "OnuId": storedFlow.OnuId,
1208 "IntfId": storedFlow.AccessIntfId,
1209 "PONs": olt.Pons,
1210 "err": err,
1211 }).Error("PON-port-not-found")
1212 return new(openolt.Empty), nil
1213 }
Pragya Arya8bdb4532020-03-02 17:08:09 +05301214 onu, err := pon.GetOnuById(uint32(storedFlow.OnuId))
1215 if err != nil {
1216 oltLogger.WithFields(log.Fields{
1217 "OnuId": storedFlow.OnuId,
1218 "IntfId": storedFlow.AccessIntfId,
1219 "err": err,
Matteo Scandolocedde462021-03-09 17:37:16 -08001220 }).Error("ONU-not-found")
Pragya Arya8bdb4532020-03-02 17:08:09 +05301221 return new(openolt.Empty), nil
1222 }
Andrea Campanellacc9b1662022-01-26 17:47:48 +01001223 onu.DeleteFlow(flowKey)
Matteo Scandoloba3e9cd2021-05-14 16:19:54 -07001224 publishEvent("Flow-remove-received", int32(onu.PonPortID), int32(onu.ID), onu.Sn())
Pragya Arya8bdb4532020-03-02 17:08:09 +05301225 }
1226
1227 // delete from olt flows
Andrea Campanellacc9b1662022-01-26 17:47:48 +01001228 o.Flows.Delete(flowKey)
Pragya Arya8bdb4532020-03-02 17:08:09 +05301229 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001230
1231 if flow.AccessIntfId == -1 {
1232 oltLogger.WithFields(log.Fields{
1233 "FlowId": flow.FlowId,
1234 }).Debug("Removing OLT flow")
1235 } else if flow.FlowType == "multicast" {
1236 oltLogger.WithFields(log.Fields{
1237 "FlowId": flow.FlowId,
1238 }).Debug("Removing OLT multicast flow")
1239 } else {
1240
1241 onu, err := o.GetOnuByFlowId(flow.FlowId)
1242 if err != nil {
1243 oltLogger.WithFields(log.Fields{
1244 "OnuId": flow.OnuId,
1245 "IntfId": flow.AccessIntfId,
1246 "err": err,
1247 }).Error("Can't find Onu")
1248 return nil, err
1249 }
1250
Matteo Scandolof9d43412021-01-12 11:11:34 -08001251 msg := types.Message{
1252 Type: types.FlowRemoved,
1253 Data: types.OnuFlowUpdateMessage{
Shrey Baid55f328c2020-07-07 19:20:42 +05301254 Flow: flow,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001255 },
1256 }
1257 onu.Channel <- msg
1258 }
1259
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001260 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001261}
1262
Shrey Baid688b4242020-07-10 20:40:10 +05301263func (o *OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo18859852020-01-15 13:33:57 -08001264 res := openolt.Heartbeat{HeartbeatSignature: uint32(time.Now().Unix())}
1265 oltLogger.WithFields(log.Fields{
1266 "signature": res.HeartbeatSignature,
1267 }).Trace("HeartbeatCheck")
1268 return &res, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001269}
1270
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001271func (o *OltDevice) GetOnuByFlowId(flowId uint64) (*Onu, error) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001272 for _, pon := range o.Pons {
1273 for _, onu := range pon.Onus {
1274 for _, fId := range onu.FlowIds {
1275 if fId == flowId {
1276 return onu, nil
1277 }
1278 }
1279 }
1280 }
Shrey Baid688b4242020-07-10 20:40:10 +05301281 return nil, fmt.Errorf("Cannot find Onu by flowId %d", flowId)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001282}
1283
Shrey Baid688b4242020-07-10 20:40:10 +05301284func (o *OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolocedde462021-03-09 17:37:16 -08001285 devinfo := &openolt.DeviceInfo{
1286 Vendor: common.Config.Olt.Vendor,
1287 Model: common.Config.Olt.Model,
1288 HardwareVersion: common.Config.Olt.HardwareVersion,
1289 FirmwareVersion: common.Config.Olt.FirmwareVersion,
Matteo Scandolocedde462021-03-09 17:37:16 -08001290 PonPorts: uint32(o.NumPon),
Matteo Scandolocedde462021-03-09 17:37:16 -08001291 DeviceSerialNumber: o.SerialNumber,
1292 DeviceId: common.Config.Olt.DeviceId,
1293 PreviouslyConnected: o.PreviouslyConnected,
Elia Battistonb7bea222022-02-18 16:25:00 +01001294 Ranges: []*openolt.DeviceInfo_DeviceResourceRanges{},
1295 }
1296
1297 for _, resRange := range common.PonsConfig.Ranges {
1298 intfIDs := []uint32{}
1299 for i := resRange.PonRange.StartId; i <= resRange.PonRange.EndId; i++ {
1300 intfIDs = append(intfIDs, uint32(i))
1301 }
1302
1303 devinfo.Ranges = append(devinfo.Ranges, &openolt.DeviceInfo_DeviceResourceRanges{
1304 IntfIds: intfIDs,
1305 Technology: resRange.Technology,
1306 Pools: []*openolt.DeviceInfo_DeviceResourceRanges_Pool{
1307 {
1308 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID,
1309 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1310 Start: resRange.OnuRange.StartId,
1311 End: resRange.OnuRange.EndId,
1312 },
1313 {
1314 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID,
1315 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1316 Start: resRange.AllocIdRange.StartId,
1317 End: resRange.AllocIdRange.EndId,
1318 },
1319 {
1320 Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID,
1321 Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF,
1322 Start: resRange.GemportRange.StartId,
1323 End: resRange.GemportRange.EndId,
Matteo Scandolocedde462021-03-09 17:37:16 -08001324 },
1325 },
Elia Battistonb7bea222022-02-18 16:25:00 +01001326 })
Matteo Scandolocedde462021-03-09 17:37:16 -08001327 }
Matteo Scandolo96f89192021-03-12 13:17:26 -08001328
1329 oltLogger.WithFields(log.Fields{
1330 "Vendor": devinfo.Vendor,
1331 "Model": devinfo.Model,
1332 "HardwareVersion": devinfo.HardwareVersion,
1333 "FirmwareVersion": devinfo.FirmwareVersion,
Matteo Scandolo96f89192021-03-12 13:17:26 -08001334 "PonPorts": devinfo.PonPorts,
Matteo Scandolo96f89192021-03-12 13:17:26 -08001335 "DeviceSerialNumber": devinfo.DeviceSerialNumber,
1336 "DeviceId": devinfo.DeviceId,
1337 "PreviouslyConnected": devinfo.PreviouslyConnected,
1338 }).Info("OLT receives GetDeviceInfo call from VOLTHA")
1339
1340 // once we connect, set the flag
1341 o.PreviouslyConnected = true
Matteo Scandolo4747d292019-08-05 11:50:18 -07001342
1343 return devinfo, nil
1344}
1345
Shrey Baid688b4242020-07-10 20:40:10 +05301346func (o *OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) {
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001347 pon, err := o.GetPonById(omci_msg.IntfId)
1348 if err != nil {
1349 oltLogger.WithFields(log.Fields{
Matteo Scandolof65e6872020-04-15 15:18:43 -07001350 "error": err,
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001351 "onu_id": omci_msg.OnuId,
1352 "pon_id": omci_msg.IntfId,
1353 }).Error("pon ID not found")
1354 return nil, err
1355 }
1356
1357 onu, err := pon.GetOnuById(omci_msg.OnuId)
1358 if err != nil {
1359 oltLogger.WithFields(log.Fields{
Matteo Scandolof65e6872020-04-15 15:18:43 -07001360 "error": err,
Jonathan Hartacfa20e2020-03-31 15:20:14 -07001361 "onu_id": omci_msg.OnuId,
1362 "pon_id": omci_msg.IntfId,
1363 }).Error("onu ID not found")
1364 return nil, err
1365 }
1366
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001367 oltLogger.WithFields(log.Fields{
1368 "IntfId": onu.PonPortID,
1369 "OnuId": onu.ID,
1370 "OnuSn": onu.Sn(),
1371 }).Tracef("Received OmciMsgOut")
Matteo Scandolob5913142021-03-19 16:10:18 -07001372 omciPkt, omciMsg, err := omcilib.ParseOpenOltOmciPacket(omci_msg.Pkt)
1373 if err != nil {
1374 log.WithFields(log.Fields{
1375 "IntfId": onu.PonPortID,
1376 "SerialNumber": onu.Sn(),
1377 "omciPacket": omcilib.HexDecode(omci_msg.Pkt),
1378 "err": err.Error(),
1379 }).Error("cannot-parse-OMCI-packet")
1380 return nil, fmt.Errorf("olt-received-malformed-omci-packet")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001381 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001382 if onu.InternalState.Current() == OnuStateDisabled {
1383 // if the ONU is disabled just drop the message
1384 log.WithFields(log.Fields{
1385 "IntfId": onu.PonPortID,
1386 "SerialNumber": onu.Sn(),
1387 "omciBytes": hex.EncodeToString(omciPkt.Data()),
1388 "omciPkt": omciPkt,
1389 "omciMsgType": omciMsg.MessageType,
1390 }).Warn("dropping-omci-message")
1391 } else {
1392 msg := types.Message{
1393 Type: types.OMCI,
1394 Data: types.OmciMessage{
1395 OnuSN: onu.SerialNumber,
1396 OnuID: onu.ID,
1397 OmciMsg: omciMsg,
1398 OmciPkt: omciPkt,
1399 },
1400 }
1401 onu.Channel <- msg
1402 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001403 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001404}
1405
Matteo Scandolo8a574812021-05-20 15:18:53 -07001406// this gRPC methods receives packets from VOLTHA and sends them to the subscriber on the ONU
Shrey Baid688b4242020-07-10 20:40:10 +05301407func (o *OltDevice) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001408 pon, err := o.GetPonById(onuPkt.IntfId)
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 PonPort")
1415 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001416 onu, err := pon.GetOnuById(onuPkt.OnuId)
Matteo Scandolo27428702019-10-11 16:21:16 -07001417 if err != nil {
1418 oltLogger.WithFields(log.Fields{
1419 "OnuId": onuPkt.OnuId,
1420 "IntfId": onuPkt.IntfId,
1421 "err": err,
1422 }).Error("Can't find Onu")
1423 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001424
Matteo Scandolo075b1892019-10-07 12:11:07 -07001425 oltLogger.WithFields(log.Fields{
1426 "IntfId": onu.PonPortID,
1427 "OnuId": onu.ID,
1428 "OnuSn": onu.Sn(),
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001429 "Packet": hex.EncodeToString(onuPkt.Pkt),
Matteo Scandolo75ed5b92020-09-03 09:03:16 -07001430 }).Trace("Received OnuPacketOut")
Matteo Scandolo075b1892019-10-07 12:11:07 -07001431
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001432 rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default)
Matteo Scandolo618a6582020-09-09 12:21:29 -07001433
1434 pktType, err := packetHandlers.GetPktType(rawpkt)
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001435 if err != nil {
1436 onuLogger.WithFields(log.Fields{
1437 "IntfId": onu.PonPortID,
1438 "OnuId": onu.ID,
1439 "OnuSn": onu.Sn(),
Matteo Scandolo618a6582020-09-09 12:21:29 -07001440 "Pkt": hex.EncodeToString(rawpkt.Data()),
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001441 }).Error("Can't find pktType in packet, droppint it")
1442 return new(openolt.Empty), nil
1443 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001444
Matteo Scandolo4a036262020-08-17 15:56:13 -07001445 pktMac, err := packetHandlers.GetDstMacAddressFromPacket(rawpkt)
Matteo Scandolo4a036262020-08-17 15:56:13 -07001446 if err != nil {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001447 onuLogger.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -07001448 "IntfId": onu.PonPortID,
1449 "OnuId": onu.ID,
1450 "OnuSn": onu.Sn(),
1451 "Pkt": rawpkt.Data(),
1452 }).Error("Can't find Dst MacAddress in packet, droppint it")
1453 return new(openolt.Empty), nil
1454 }
1455
Matteo Scandolof9d43412021-01-12 11:11:34 -08001456 msg := types.Message{
1457 Type: types.OnuPacketOut,
1458 Data: types.OnuPacketMessage{
Matteo Scandolo4a036262020-08-17 15:56:13 -07001459 IntfId: onuPkt.IntfId,
1460 OnuId: onuPkt.OnuId,
Matteo Scandolo8a574812021-05-20 15:18:53 -07001461 PortNo: onuPkt.PortNo,
Matteo Scandolo4a036262020-08-17 15:56:13 -07001462 Packet: rawpkt,
1463 Type: pktType,
1464 MacAddress: pktMac,
Matteo Scandolo075b1892019-10-07 12:11:07 -07001465 },
Matteo Scandolo47e69bb2019-08-28 15:41:12 -07001466 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001467
Matteo Scandolo075b1892019-10-07 12:11:07 -07001468 onu.Channel <- msg
1469
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001470 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001471}
1472
Shrey Baid688b4242020-07-10 20:40:10 +05301473func (o *OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo635b2bf2020-09-04 10:23:40 -07001474
1475 // OLT Reboot is called in two cases:
1476 // - 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)
1477 // - when an OLT needs to be rebooted (voltcl device reboot)
1478
Matteo Scandolod02b79b2019-12-05 16:42:13 -08001479 oltLogger.WithFields(log.Fields{
1480 "oltId": o.ID,
1481 }).Info("Shutting down")
Pragya Arya324337e2020-02-20 14:35:08 +05301482 publishEvent("OLT-reboot-received", -1, -1, "")
Shrey Baid688b4242020-07-10 20:40:10 +05301483 go func() { _ = o.RestartOLT() }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001484 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001485}
1486
Shrey Baid688b4242020-07-10 20:40:10 +05301487func (o *OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Pragya Arya6a708d62020-01-01 17:17:20 +05301488 oltLogger.WithFields(log.Fields{
1489 "oltId": o.ID,
1490 }).Info("Received ReenableOlt request from VOLTHA")
Pragya Arya324337e2020-02-20 14:35:08 +05301491 publishEvent("OLT-reenable-received", -1, -1, "")
Pragya Arya6a708d62020-01-01 17:17:20 +05301492
Pragya Arya2225f202020-01-29 18:05:01 +05301493 // enable OLT
Matteo Scandolof9d43412021-01-12 11:11:34 -08001494 oltMsg := types.Message{
1495 Type: types.OltIndication,
1496 Data: types.OltIndicationMessage{
1497 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +05301498 },
Pragya Arya1881df02020-01-29 18:05:01 +05301499 }
Pragya Arya2225f202020-01-29 18:05:01 +05301500 o.channel <- oltMsg
Pragya Arya6a708d62020-01-01 17:17:20 +05301501
Pragya Arya2225f202020-01-29 18:05:01 +05301502 for _, pon := range o.Pons {
1503 if pon.InternalState.Current() == "disabled" {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001504 msg := types.Message{
1505 Type: types.PonIndication,
1506 Data: types.PonIndicationMessage{
1507 OperState: types.UP,
Pragya Arya2225f202020-01-29 18:05:01 +05301508 PonPortID: pon.ID,
1509 },
1510 }
1511 o.channel <- msg
1512 }
1513 }
Matteo Scandoloe60a5052020-02-07 00:31:14 +00001514
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001515 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001516}
1517
Shrey Baid688b4242020-07-10 20:40:10 +05301518func (o *OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001519 pkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
1520
Matteo Scandolo90d08f62020-10-29 12:06:55 -07001521 err := o.Nnis[0].handleNniPacket(pkt) // FIXME we are assuming we have only one NNI
1522
1523 if err != nil {
1524 return nil, err
1525 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001526 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001527}
1528
Shrey Baid688b4242020-07-10 20:40:10 +05301529func (o *OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001530 oltLogger.Error("CollectStatistics not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001531 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001532}
1533
Shrey Baid688b4242020-07-10 20:40:10 +05301534func (o *OltDevice) GetOnuInfo(context context.Context, packet *openolt.Onu) (*openolt.OnuIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001535 oltLogger.Error("GetOnuInfo not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001536 return new(openolt.OnuIndication), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -07001537}
1538
Shrey Baid688b4242020-07-10 20:40:10 +05301539func (o *OltDevice) GetPonIf(context context.Context, packet *openolt.Interface) (*openolt.IntfIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -07001540 oltLogger.Error("GetPonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001541 return new(openolt.IntfIndication), nil
Matteo Scandolod54283a2019-08-13 16:22:31 -07001542}
1543
Shrey Baid688b4242020-07-10 20:40:10 +05301544func (s *OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001545 oltLogger.Info("received CreateTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001546 return new(openolt.Empty), nil
1547}
1548
Matteo Scandolo8a574812021-05-20 15:18:53 -07001549func (s *OltDevice) RemoveTrafficQueues(_ context.Context, tq *tech_profile.TrafficQueues) (*openolt.Empty, error) {
1550 oltLogger.WithFields(log.Fields{
1551 "OnuId": tq.OnuId,
1552 "IntfId": tq.IntfId,
1553 "OnuPortNo": tq.PortNo,
1554 "UniId": tq.UniId,
1555 }).Info("received RemoveTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001556 return new(openolt.Empty), nil
1557}
1558
Matteo Scandolo8a574812021-05-20 15:18:53 -07001559func (s *OltDevice) CreateTrafficSchedulers(_ context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Anand S Katti09541352020-01-29 15:54:01 +05301560 oltLogger.WithFields(log.Fields{
1561 "OnuId": trafficSchedulers.OnuId,
1562 "IntfId": trafficSchedulers.IntfId,
1563 "OnuPortNo": trafficSchedulers.PortNo,
Matteo Scandolo8a574812021-05-20 15:18:53 -07001564 "UniId": trafficSchedulers.UniId,
Anand S Katti09541352020-01-29 15:54:01 +05301565 }).Info("received CreateTrafficSchedulers")
1566
1567 if !s.enablePerf {
1568 pon, err := s.GetPonById(trafficSchedulers.IntfId)
1569 if err != nil {
1570 oltLogger.Errorf("Error retrieving PON by IntfId: %v", err)
1571 return new(openolt.Empty), err
1572 }
1573 onu, err := pon.GetOnuById(trafficSchedulers.OnuId)
1574 if err != nil {
1575 oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err)
1576 return new(openolt.Empty), err
1577 }
1578 onu.TrafficSchedulers = trafficSchedulers
1579 }
Matteo Scandolod54283a2019-08-13 16:22:31 -07001580 return new(openolt.Empty), nil
1581}
1582
Shrey Baid688b4242020-07-10 20:40:10 +05301583func (s *OltDevice) RemoveTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Anand S Katti09541352020-01-29 15:54:01 +05301584 oltLogger.WithFields(log.Fields{
1585 "OnuId": trafficSchedulers.OnuId,
1586 "IntfId": trafficSchedulers.IntfId,
1587 "OnuPortNo": trafficSchedulers.PortNo,
1588 }).Info("received RemoveTrafficSchedulers")
1589 if !s.enablePerf {
1590 pon, err := s.GetPonById(trafficSchedulers.IntfId)
1591 if err != nil {
1592 oltLogger.Errorf("Error retrieving PON by IntfId: %v", err)
1593 return new(openolt.Empty), err
1594 }
1595 onu, err := pon.GetOnuById(trafficSchedulers.OnuId)
1596 if err != nil {
1597 oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err)
1598 return new(openolt.Empty), err
1599 }
1600
1601 onu.TrafficSchedulers = nil
1602 }
Matteo Scandolod54283a2019-08-13 16:22:31 -07001603 return new(openolt.Empty), nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001604}
Scott Baker41724b82020-01-21 19:54:53 -08001605
Matteo Scandolo618a6582020-09-09 12:21:29 -07001606func (o *OltDevice) PerformGroupOperation(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) {
1607 oltLogger.WithFields(log.Fields{
1608 "GroupId": group.GroupId,
1609 "Command": group.Command,
1610 "Members": group.Members,
1611 "Action": group.Action,
1612 }).Debug("received PerformGroupOperation")
1613 return &openolt.Empty{}, nil
1614}
1615
1616func (o *OltDevice) DeleteGroup(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) {
1617 oltLogger.WithFields(log.Fields{
1618 "GroupId": group.GroupId,
1619 "Command": group.Command,
1620 "Members": group.Members,
1621 "Action": group.Action,
1622 }).Debug("received PerformGroupOperation")
1623 return &openolt.Empty{}, nil
1624}
1625
Matteo Scandolo1f9f4b22021-12-14 11:51:55 -08001626func (o *OltDevice) GetExtValue(ctx context.Context, in *openolt.ValueParam) (*extension.ReturnValues, error) {
1627 return &extension.ReturnValues{}, nil
Matteo Scandolo618a6582020-09-09 12:21:29 -07001628}
1629
1630func (o *OltDevice) OnuItuPonAlarmSet(ctx context.Context, in *config.OnuItuPonAlarm) (*openolt.Empty, error) {
1631 return &openolt.Empty{}, nil
1632}
1633
1634func (o *OltDevice) GetLogicalOnuDistanceZero(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) {
1635 return &openolt.OnuLogicalDistance{}, nil
1636}
1637
1638func (o *OltDevice) GetLogicalOnuDistance(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) {
1639 return &openolt.OnuLogicalDistance{}, nil
1640}
Matteo Scandolo96f89192021-03-12 13:17:26 -08001641
Girish Gowdra62f24292021-05-12 16:28:39 -07001642func (o *OltDevice) GetPonRxPower(ctx context.Context, in *openolt.Onu) (*openolt.PonRxPowerData, error) {
1643 return &openolt.PonRxPowerData{}, nil
1644}
1645
Matteo Scandolo96f89192021-03-12 13:17:26 -08001646func (o *OltDevice) GetGemPortStatistics(ctx context.Context, in *openolt.OnuPacket) (*openolt.GemPortStatistics, error) {
1647 return &openolt.GemPortStatistics{}, nil
1648}
1649
1650func (o *OltDevice) GetOnuStatistics(ctx context.Context, in *openolt.Onu) (*openolt.OnuStatistics, error) {
1651 return &openolt.OnuStatistics{}, nil
1652}
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001653
1654func (o *OltDevice) storeAllocId(flow *openolt.Flow) {
1655 o.AllocIDsLock.Lock()
1656 defer o.AllocIDsLock.Unlock()
1657
Matteo Scandolo21195d62021-04-07 14:31:23 -07001658 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)]; !ok {
1659 oltLogger.WithFields(log.Fields{
1660 "IntfId": flow.AccessIntfId,
1661 "OnuId": flow.OnuId,
1662 "PortNo": flow.PortNo,
1663 "GemportId": flow.GemportId,
1664 "FlowId": flow.FlowId,
1665 }).Error("trying-to-store-alloc-id-for-unknown-onu")
1666 }
1667
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001668 oltLogger.WithFields(log.Fields{
Matteo Scandolo21195d62021-04-07 14:31:23 -07001669 "IntfId": flow.AccessIntfId,
1670 "OnuId": flow.OnuId,
1671 "PortNo": flow.PortNo,
1672 "GemportId": flow.GemportId,
1673 "FlowId": flow.FlowId,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001674 }).Trace("storing-alloc-id-via-flow")
1675
1676 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo]; !ok {
1677 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo] = make(map[int32]map[uint64]bool)
1678 }
1679 if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId]; !ok {
1680 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId] = make(map[uint64]bool)
1681 }
1682 o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId][flow.FlowId] = true
1683}
1684
1685func (o *OltDevice) freeAllocId(flow *openolt.Flow) {
1686 // if this is the last flow referencing the AllocId then remove it
1687 o.AllocIDsLock.Lock()
1688 defer o.AllocIDsLock.Unlock()
1689
1690 oltLogger.WithFields(log.Fields{
1691 "IntfId": flow.AccessIntfId,
1692 "OnuId": flow.OnuId,
1693 "PortNo": flow.PortNo,
1694 "GemportId": flow.GemportId,
1695 }).Trace("freeing-alloc-id-via-flow")
1696
1697 // NOTE look at the freeGemPortId implementation for comments and context
1698 for ponId, ponValues := range o.AllocIDs {
1699 for onuId, onuValues := range ponValues {
1700 for uniId, uniValues := range onuValues {
1701 for allocId, flows := range uniValues {
1702 for flowId := range flows {
1703 // if the flow matches, remove it from the map.
1704 if flow.FlowId == flowId {
1705 delete(o.AllocIDs[ponId][onuId][uniId][allocId], flow.FlowId)
1706 }
1707 // if that was the last flow for a particular allocId, remove the entire allocId
1708 if len(o.AllocIDs[ponId][onuId][uniId][allocId]) == 0 {
1709 delete(o.AllocIDs[ponId][onuId][uniId], allocId)
1710 }
1711 }
1712 }
1713 }
1714 }
1715 }
1716}
1717
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001718func (o *OltDevice) storeGemPortId(ponId uint32, onuId uint32, portNo uint32, gemId int32, flowId uint64) {
Matteo Scandolo21195d62021-04-07 14:31:23 -07001719 o.GemPortIDsLock.Lock()
1720 defer o.GemPortIDsLock.Unlock()
1721
1722 if _, ok := o.GemPortIDs[ponId][onuId]; !ok {
1723 oltLogger.WithFields(log.Fields{
1724 "IntfId": ponId,
1725 "OnuId": onuId,
1726 "PortNo": portNo,
1727 "GemportId": gemId,
1728 "FlowId": flowId,
1729 }).Error("trying-to-store-gemport-for-unknown-onu")
1730 }
1731
1732 oltLogger.WithFields(log.Fields{
1733 "IntfId": ponId,
1734 "OnuId": onuId,
1735 "PortNo": portNo,
1736 "GemportId": gemId,
1737 "FlowId": flowId,
1738 }).Trace("storing-alloc-id-via-flow")
1739
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001740 if _, ok := o.GemPortIDs[ponId][onuId][portNo]; !ok {
1741 o.GemPortIDs[ponId][onuId][portNo] = make(map[int32]map[uint64]bool)
1742 }
1743 if _, ok := o.GemPortIDs[ponId][onuId][portNo][gemId]; !ok {
1744 o.GemPortIDs[ponId][onuId][portNo][gemId] = make(map[uint64]bool)
1745 }
1746 o.GemPortIDs[ponId][onuId][portNo][gemId][flowId] = true
1747}
1748
1749func (o *OltDevice) storeGemPortIdByFlow(flow *openolt.Flow) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001750 oltLogger.WithFields(log.Fields{
Matteo Scandolo21195d62021-04-07 14:31:23 -07001751 "IntfId": flow.AccessIntfId,
1752 "OnuId": flow.OnuId,
1753 "PortNo": flow.PortNo,
1754 "GemportId": flow.GemportId,
1755 "FlowId": flow.FlowId,
1756 "ReplicateFlow": flow.ReplicateFlow,
1757 "PbitToGemport": flow.PbitToGemport,
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001758 }).Trace("storing-gem-port-id-via-flow")
1759
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001760 if flow.ReplicateFlow {
1761 for _, gem := range flow.PbitToGemport {
1762 o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, int32(gem), flow.FlowId)
1763 }
1764 } else {
1765 o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, flow.GemportId, flow.FlowId)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001766 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001767}
1768
1769func (o *OltDevice) freeGemPortId(flow *openolt.Flow) {
1770 // if this is the last flow referencing the GemPort then remove it
1771 o.GemPortIDsLock.Lock()
1772 defer o.GemPortIDsLock.Unlock()
1773
1774 oltLogger.WithFields(log.Fields{
1775 "IntfId": flow.AccessIntfId,
1776 "OnuId": flow.OnuId,
1777 "PortNo": flow.PortNo,
1778 "GemportId": flow.GemportId,
1779 }).Trace("freeing-gem-port-id-via-flow")
1780
1781 // NOTE that this loop is not very performant, it would be better if the flow carries
1782 // the same information that it carries during a FlowAdd. If so we can directly remove
1783 // items from the map
1784
1785 //delete(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.GemportId], flow.FlowId)
1786 //if len(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.GemportId]) == 0 {
1787 // delete(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo], flow.GemportId)
1788 //}
1789
1790 // NOTE this loop assumes that flow IDs are unique per device
1791 for ponId, ponValues := range o.GemPortIDs {
1792 for onuId, onuValues := range ponValues {
1793 for uniId, uniValues := range onuValues {
1794 for gemId, flows := range uniValues {
1795 for flowId := range flows {
1796 // if the flow matches, remove it from the map.
1797 if flow.FlowId == flowId {
1798 delete(o.GemPortIDs[ponId][onuId][uniId][gemId], flow.FlowId)
1799 }
1800 // if that was the last flow for a particular gem, remove the entire gem
1801 if len(o.GemPortIDs[ponId][onuId][uniId][gemId]) == 0 {
1802 delete(o.GemPortIDs[ponId][onuId][uniId], gemId)
1803 }
1804 }
1805 }
1806 }
1807 }
1808 }
1809}
1810
1811// validateFlow checks that:
1812// - the AllocId is not used in any flow referencing other ONUs/UNIs on the same PON
1813// - the GemPortId is not used in any flow referencing other ONUs/UNIs on the same PON
1814func (o *OltDevice) validateFlow(flow *openolt.Flow) error {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001815 // validate gemPort
1816 o.GemPortIDsLock.RLock()
Matteo Scandolo21195d62021-04-07 14:31:23 -07001817 defer o.GemPortIDsLock.RUnlock()
1818 for onuId, onu := range o.GemPortIDs[uint32(flow.AccessIntfId)] {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001819 if onuId == uint32(flow.OnuId) {
1820 continue
1821 }
1822 for uniId, uni := range onu {
1823 for gem := range uni {
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001824 if flow.ReplicateFlow {
1825 for _, flowGem := range flow.PbitToGemport {
1826 if gem == int32(flowGem) {
1827 return fmt.Errorf("gem-%d-already-in-use-on-uni-%d-onu-%d-replicated-flow-%d", gem, uniId, onuId, flow.FlowId)
1828 }
1829 }
1830 } else {
1831 if gem == flow.GemportId {
1832 return fmt.Errorf("gem-%d-already-in-use-on-uni-%d-onu-%d-flow-%d", gem, uniId, onuId, flow.FlowId)
1833 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001834 }
1835 }
1836 }
1837 }
1838
1839 o.AllocIDsLock.RLock()
Matteo Scandolo21195d62021-04-07 14:31:23 -07001840 defer o.AllocIDsLock.RUnlock()
1841 for onuId, onu := range o.AllocIDs[uint32(flow.AccessIntfId)] {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001842 if onuId == uint32(flow.OnuId) {
1843 continue
1844 }
1845 for uniId, uni := range onu {
1846 for allocId := range uni {
1847 if allocId == flow.AllocId {
Matteo Scandoloa8eca492021-03-23 09:45:16 -07001848 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 -08001849 }
1850 }
1851 }
1852 }
1853
1854 return nil
1855}
1856
1857// clearAllResources is invoked up OLT Reboot to remove all the allocated
1858// GemPorts, AllocId and ONU-IDs across the PONs
1859func (o *OltDevice) clearAllResources() {
1860
1861 // remove the resources received via flows
1862 o.GemPortIDsLock.Lock()
1863 o.GemPortIDs = make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool)
1864 o.GemPortIDsLock.Unlock()
1865 o.AllocIDsLock.Lock()
1866 o.AllocIDs = make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool)
1867 o.AllocIDsLock.Unlock()
1868
1869 // remove the resources received via OMCI
1870 for _, pon := range o.Pons {
1871 pon.removeAllAllocIds()
1872 pon.removeAllGemPorts()
1873 pon.removeAllOnuIds()
1874 }
1875}