blob: fa9e649ac9afcd9a8b88ac3cd135ddbbbd4d0432 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package application
17
18import (
19 "context"
20 "encoding/hex"
21 "encoding/json"
22 "errors"
23 "fmt"
24 "net"
25 "strconv"
26 "strings"
27 "sync"
28 "time"
29
30 "github.com/google/gopacket"
31 "github.com/google/gopacket/layers"
32
33 "voltha-go-controller/internal/pkg/controller"
34 cntlr "voltha-go-controller/internal/pkg/controller"
35 "voltha-go-controller/database"
36 "voltha-go-controller/internal/pkg/intf"
37 "voltha-go-controller/internal/pkg/of"
38 "voltha-go-controller/internal/pkg/tasks"
39 "voltha-go-controller/internal/pkg/util"
40 errorCodes "voltha-go-controller/internal/pkg/errorcodes"
Tinoj Joseph1d108322022-07-13 10:07:39 +053041 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053042)
43
44var logger log.CLogger
45var ctx = context.TODO()
46
47func init() {
48 // Setup this package so that it's log level can be modified at run time
49 var err error
Tinoj Joseph1d108322022-07-13 10:07:39 +053050 logger, err = log.AddPackageWithDefaultParam()
Naveen Sampath04696f72022-06-13 15:19:14 +053051 if err != nil {
52 panic(err)
53 }
54}
55
56const (
57 // TODO - Need to identify a right place for this
58
59 // PriorityNone constant.
60 PriorityNone uint8 = 8
61 // AnyVlan constant.
62 AnyVlan uint16 = 0xFFFF
63)
64
65// List of Mac Learning Type
66const (
Tinoj Joseph1d108322022-07-13 10:07:39 +053067 MacLearningNone MacLearningType = iota
68 Learn
69 ReLearn
Naveen Sampath04696f72022-06-13 15:19:14 +053070)
71
72// MacLearningType represents Mac Learning Type
73type MacLearningType int
74
75var (
76 tickCount uint16
77 vgcRebooted bool
78 isUpgradeComplete bool
79)
80
81var db database.DBIntf
82
83// PacketHandlers : packet handler for different protocols
84var PacketHandlers map[string]CallBack
85
86// CallBack : registered call back function for different protocol packets
Tinoj Joseph07cc5372022-07-18 22:53:51 +053087type CallBack func(cntx context.Context, device string, port string, pkt gopacket.Packet)
Naveen Sampath04696f72022-06-13 15:19:14 +053088
89const (
90 // ARP packet
91 ARP string = "ARP"
92 // DHCPv4 packet
93 DHCPv4 string = "DHCPv4"
94 // DHCPv6 packet
95 DHCPv6 string = "DHCPv6"
96 // IGMP packet
97 IGMP string = "IGMP"
98 // PPPOE packet
99 PPPOE string = "PPPOE"
100 // US packet side
101 US string = "US"
102 // DS packet side
103 DS string = "DS"
104 // NNI port name
105 NNI string = "nni"
106)
107
108// RegisterPacketHandler : API to register callback function for every protocol
109func RegisterPacketHandler(protocol string, callback CallBack) {
110 if PacketHandlers == nil {
111 PacketHandlers = make(map[string]CallBack)
112 }
113 PacketHandlers[protocol] = callback
114}
115
116// ---------------------------------------------------------------------
117// VOLT Ports
118// ---------------------------------------------------------------------
119// VOLT Ports are ports associated with VOLT devices. Each port is classified into
120// Access/NNI. Each port is identified by Name (Identity known to the NB) and
121// Id (Identity used on the SB). Both identities are presented when a port is
122// discovered in the SB.
123
124// VoltPortType type for Port Type
125type VoltPortType uint8
126
127const (
128 // VoltPortTypeAccess constant.
129 VoltPortTypeAccess VoltPortType = 0
130 // VoltPortTypeNni constant.
131 VoltPortTypeNni VoltPortType = 1
132)
133
134// PortState type for Port State.
135type PortState uint8
136
137const (
138 // PortStateDown constant.
139 PortStateDown PortState = 0
140 // PortStateUp constant.
141 PortStateUp PortState = 1
142)
143
144// VoltPort structure that is used to store the ports. The name is the
145// the main identity used by the application. The SB and NB both present name
146// as the identity. The SB is abstracted by VPAgent and the VPAgent transacts
147// using name as identity
148type VoltPort struct {
149 ID uint32
150 Name string
151 Device string
152 PonPort uint32
153 Type VoltPortType
154 State PortState
155 ActiveChannels uint32
156 ChannelPerSubAlarmRaised bool
157}
158
159// NewVoltPort : Constructor for the port.
160func NewVoltPort(device string, name string, id uint32) *VoltPort {
161 var vp VoltPort
162 vp.Device = device
163 vp.Name = name
164 vp.ID = id
165 if util.IsNniPort(id) {
166 vp.Type = VoltPortTypeNni
167 } else {
168 vp.PonPort = GetPonPortIDFromUNIPort(id)
169 }
170 vp.State = PortStateDown
171 vp.ChannelPerSubAlarmRaised = false
172 return &vp
173}
174
175// SetPortID : The ID is used when constructing flows as the flows require ID.
176func (vp *VoltPort) SetPortID(id uint32) {
177 vp.ID = id
178 if util.IsNniPort(id) {
179 vp.Type = VoltPortTypeNni
180 }
181}
182
183// ---------------------------------------------------------------------
184// VOLT Device
185// ---------------------------------------------------------------------
186//
187// VoltDevice is an OLT which contains ports of type access and NNI. Each OLT
188// can only have one NNI port in the current release. The NNI port always uses
189// identity 65536 and all the access ports use identities less than 65535. The
190// identification of NNI is done by comparing the port identity with 65535
191
192// VoltDevice fields :
193// Name: This is the name presented by the device/VOLTHA. This doesn't
194// have any relation to the physical device
195// SerialNum: This is the serial number of the device and can be used to
196// correlate the devices
197// NniPort: The identity of the NNI port
198// Ports: List of all ports added to the device
199type VoltDevice struct {
200 Name string
201 SerialNum string
202 State controller.DeviceState
203 SouthBoundID string
204 NniPort string
205 Ports sync.Map
206 VlanPortStatus sync.Map
207 VpvsBySvlan *util.ConcurrentMap // map[svlan]map[vnet_port]*VoltPortVnet
208 IgmpDsFlowAppliedForMvlan map[uint16]bool
209 ConfiguredVlanForDeviceFlows *util.ConcurrentMap //map[string]map[string]bool
210 icmpv6GroupAdded bool
211 ActiveChannelsPerPon sync.Map // [PonPortID]*PonPortCfg
212 ActiveChannelCountLock sync.Mutex // This lock is used to update ActiveIGMPChannels
213 PonPortList sync.Map // [PonPortID]map[string]string
214 FlowAddEventMap *util.ConcurrentMap //map[string]*FlowEvent
215 FlowDelEventMap *util.ConcurrentMap //map[string]*FlowEvent
216 MigratingServices *util.ConcurrentMap //<vnetID,<RequestID, MigrateServicesRequest>>
217 GlobalDhcpFlowAdded bool
218}
219
220// NewVoltDevice : Constructor for the device
221func NewVoltDevice(name string, slno, southBoundID string) *VoltDevice {
222 var d VoltDevice
223 d.Name = name
224 d.SouthBoundID = southBoundID
225 d.State = controller.DeviceStateDOWN
226 d.NniPort = ""
227 d.SouthBoundID = southBoundID
228 d.SerialNum = slno
229 d.icmpv6GroupAdded = false
230 d.IgmpDsFlowAppliedForMvlan = make(map[uint16]bool)
231 d.ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
232 d.MigratingServices = util.NewConcurrentMap()
233 d.VpvsBySvlan = util.NewConcurrentMap()
234 d.FlowAddEventMap = util.NewConcurrentMap()
235 d.FlowDelEventMap = util.NewConcurrentMap()
236 d.GlobalDhcpFlowAdded = false
237 return &d
238}
239
240//GetAssociatedVpvsForDevice - return the associated VPVs for given device & svlan
241func (va *VoltApplication) GetAssociatedVpvsForDevice(device string, svlan of.VlanType) *util.ConcurrentMap {
242 if d := va.GetDevice(device); d != nil {
243 return d.GetAssociatedVpvs(svlan)
244 }
245 return nil
246}
247
248//AssociateVpvsToDevice - updates the associated VPVs for given device & svlan
249func (va *VoltApplication) AssociateVpvsToDevice(device string, vpv *VoltPortVnet) {
250 if d := va.GetDevice(device); d != nil {
251
252 vpvMap := d.GetAssociatedVpvs(vpv.SVlan)
253 vpvMap.Set(vpv, true)
254 d.VpvsBySvlan.Set(vpv.SVlan, vpvMap)
255 logger.Infow(ctx, "VPVMap: SET", log.Fields{"Map": vpvMap.Length()})
256 return
257 }
258 logger.Errorw(ctx, "Set VPVMap failed: Device Not Found", log.Fields{"Svlan": vpv.SVlan, "Device": device})
259}
260
261//DisassociateVpvsFromDevice - disassociated VPVs from given device & svlan
262func (va *VoltApplication) DisassociateVpvsFromDevice(device string, vpv *VoltPortVnet) {
263 if d := va.GetDevice(device); d != nil {
264 vpvMap := d.GetAssociatedVpvs(vpv.SVlan)
265 vpvMap.Remove(vpv)
266 d.VpvsBySvlan.Set(vpv.SVlan, vpvMap)
267 logger.Infow(ctx, "VPVMap: Remove", log.Fields{"Map": vpvMap.Length()})
268 return
269 }
270 logger.Errorw(ctx, "Remove VPVMap failed: Device Not Found", log.Fields{"Svlan": vpv.SVlan, "Device": device})
271}
272
273//GetAssociatedVpvs - returns the associated VPVs for the given Svlan
274func (d *VoltDevice) GetAssociatedVpvs(svlan of.VlanType) *util.ConcurrentMap {
275
276 var vpvMap *util.ConcurrentMap
277 var mapIntf interface{}
278 var ok bool
279
280 if mapIntf, ok = d.VpvsBySvlan.Get(svlan); ok {
281 vpvMap = mapIntf.(*util.ConcurrentMap)
282 } else {
283 vpvMap = util.NewConcurrentMap()
284 }
285 logger.Infow(ctx, "VPVMap: GET", log.Fields{"Map": vpvMap.Length()})
286 return vpvMap
287}
288
289// AddPort add port to the device.
290func (d *VoltDevice) AddPort(port string, id uint32) *VoltPort {
291 addPonPortFromUniPort := func(vPort *VoltPort) {
292 if vPort.Type == VoltPortTypeAccess {
293 ponPortID := GetPonPortIDFromUNIPort(vPort.ID)
294
295 if ponPortUniList, ok := d.PonPortList.Load(ponPortID); !ok {
296 uniList := make(map[string]uint32)
297 uniList[port] = vPort.ID
298 d.PonPortList.Store(ponPortID, uniList)
299 } else {
300 ponPortUniList.(map[string]uint32)[port] = vPort.ID
301 d.PonPortList.Store(ponPortID, ponPortUniList)
302 }
303 }
304 }
305 va := GetApplication()
306 if pIntf, ok := d.Ports.Load(port); ok {
307 voltPort := pIntf.(*VoltPort)
308 addPonPortFromUniPort(voltPort)
309 va.AggActiveChannelsCountPerSub(d.Name, port, voltPort)
310 d.Ports.Store(port, voltPort)
311 return voltPort
312 }
313 p := NewVoltPort(d.Name, port, id)
314 va.AggActiveChannelsCountPerSub(d.Name, port, p)
315 d.Ports.Store(port, p)
316 if util.IsNniPort(id) {
317 d.NniPort = port
318 }
319 addPonPortFromUniPort(p)
320 return p
321}
322
323// GetPort to get port information from the device.
324func (d *VoltDevice) GetPort(port string) *VoltPort {
325 if pIntf, ok := d.Ports.Load(port); ok {
326 return pIntf.(*VoltPort)
327 }
328 return nil
329}
330
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530331// GetPortByPortID to get port information from the device.
332func (d *VoltDevice) GetPortNameFromPortID(portID uint32) string {
333 portName := ""
334 d.Ports.Range(func(key, value interface{}) bool {
335 vp := value.(*VoltPort)
336 if vp.ID == portID {
337 portName = vp.Name
338 }
339 return true
340 })
341 return portName
342}
343
Naveen Sampath04696f72022-06-13 15:19:14 +0530344// DelPort to delete port from the device
345func (d *VoltDevice) DelPort(port string) {
346 if _, ok := d.Ports.Load(port); ok {
347 d.Ports.Delete(port)
348 } else {
349 logger.Warnw(ctx, "Port doesn't exist", log.Fields{"Device": d.Name, "Port": port})
350 }
351}
352
353// pushFlowsForUnis to send port-up-indication for uni ports.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530354func (d *VoltDevice) pushFlowsForUnis(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530355
356 logger.Info(ctx, "NNI Discovered, Sending Port UP Ind for UNIs")
357 d.Ports.Range(func(key, value interface{}) bool {
358 port := key.(string)
359 vp := value.(*VoltPort)
360
361 logger.Infow(ctx, "NNI Discovered. Sending Port UP Ind for UNI", log.Fields{"Port" : port})
362 //Ignore if UNI port is not UP
363 if vp.State != PortStateUp {
364 return true
365 }
366
367 //Obtain all VPVs associated with the port
368 vnets, ok := GetApplication().VnetsByPort.Load(port)
369 if !ok {
370 return true
371 }
372
373 for _, vpv := range vnets.([]*VoltPortVnet) {
374 vpv.VpvLock.Lock()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530375 vpv.PortUpInd(cntx, d, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530376 vpv.VpvLock.Unlock()
377
378 }
379 return true
380 })
381}
382
383// ----------------------------------------------------------
384// VOLT Application - hosts all other objects
385// ----------------------------------------------------------
386//
387// The VOLT application is a singleton implementation where
388// there is just one instance in the system and is the gateway
389// to all other components within the controller
390// The declaration of the singleton object
391var vapplication *VoltApplication
392
393// VoltApplication fields :
394// ServiceByName - Stores the services by the name as key
395// A record of NB configuration.
396// VnetsByPort - Stores the VNETs by the ports configured
397// from NB. A record of NB configuration.
398// VnetsByTag - Stores the VNETs by the VLANS configured
399// from NB. A record of NB configuration.
400// VnetsByName - Stores the VNETs by the name configured
401// from NB. A record of NB configuration.
402// DevicesDisc - Stores the devices discovered from SB.
403// Should be updated only by events from SB
404// PortsDisc - Stores the ports discovered from SB.
405// Should be updated only by events from SB
406type VoltApplication struct {
407 ServiceByName sync.Map // [serName]*VoltService
408 VnetsByPort sync.Map // [portName][]*VoltPortVnet
409 VnetsByTag sync.Map // [svlan-cvlan-uvlan]*VoltVnet
410 VnetsByName sync.Map // [vnetName]*VoltVnet
411 VnetsBySvlan *util.ConcurrentMap
412 DevicesDisc sync.Map
413 PortsDisc sync.Map
414 IgmpGroups sync.Map // [grpKey]*IgmpGroup
415 IgmpGroupIds []*IgmpGroup
416 MvlanProfilesByTag sync.Map
417 MvlanProfilesByName sync.Map
418 Icmpv6Receivers sync.Map
419 MeterMgr
420 IgmpTasks tasks.Tasks
421 IndicationsTasks tasks.Tasks
422 MulticastAlarmTasks tasks.Tasks
423 portLock sync.Mutex
424 DataMigrationInfo DataMigration
425 DeviceCounters sync.Map //[logicalDeviceId]*DeviceCounters
426 ServiceCounters sync.Map //[serviceName]*ServiceCounters
427 NbDevice sync.Map // [OLTSouthBoundID]*NbDevice
428 IgmpKPIsTasks tasks.Tasks
429 pppoeTasks tasks.Tasks
430 IgmpProfilesByName sync.Map
431 OltIgmpInfoBySerial sync.Map
432 McastConfigMap sync.Map //[OltSerialNo_MvlanProfileID]*McastConfig
433 // MacAddress-Port MAP to avoid swap of mac accross ports.
434 macPortLock sync.RWMutex
435 macPortMap map[string]string
436
437 IgmpPendingPool map[string]map[*IgmpGroup]bool //[grpkey, map[groupObj]bool] //mvlan_grpName/IP
438 PendingPoolLock sync.RWMutex
439
440 VnetsToDelete map[string]bool
441 ServicesToDelete map[string]bool
442 VoltPortVnetsToDelete map[*VoltPortVnet]bool
443 PortAlarmProfileCache map[string]map[string]int // [portAlarmID][ThresholdLevelString]ThresholdLevel
444 vendorID string
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530445 OltFlowServiceConfig OltFlowService
Naveen Sampath04696f72022-06-13 15:19:14 +0530446}
447
448// PonPortCfg contains NB port config and activeIGMPChannels count
449type PonPortCfg struct {
450 PortID uint32
451 MaxActiveChannels uint32
452 ActiveIGMPChannels uint32
453 EnableMulticastKPI bool
454 PortAlarmProfileID string
455}
456
457// NbDevice OLT Device info
458type NbDevice struct {
459 SouthBoundID string
460 PonPorts sync.Map // [PortID]*PonPortCfg
461}
462
463// RestoreNbDeviceFromDb restores the NB Device in case of VGC pod restart.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530464func (va *VoltApplication) RestoreNbDeviceFromDb(cntx context.Context, deviceID string) *NbDevice {
Naveen Sampath04696f72022-06-13 15:19:14 +0530465
466 nbDevice := NewNbDevice()
467 nbDevice.SouthBoundID = deviceID
468
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530469 nbPorts, _ := db.GetAllNbPorts(cntx, deviceID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530470
471 for key, p := range nbPorts {
472 b, ok := p.Value.([]byte)
473 if !ok {
474 logger.Warn(ctx, "The value type is not []byte")
475 continue
476 }
477 var port PonPortCfg
478 err := json.Unmarshal(b, &port)
479 if err != nil {
480 logger.Warn(ctx, "Unmarshal of PonPortCfg failed")
481 continue
482 }
483 logger.Debugw(ctx, "Port recovered", log.Fields{"port": port})
484 ponPortID, _ := strconv.Atoi(key)
485 nbDevice.PonPorts.Store(uint32(ponPortID), &port)
486 }
487 va.NbDevice.Store(deviceID, nbDevice)
488 return nbDevice
489}
490
491// NewNbDevice Constructor for NbDevice
492func NewNbDevice() *NbDevice {
493 var nbDevice NbDevice
494 return &nbDevice
495}
496
497// WriteToDb writes nb device port config to kv store
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530498func (nbd *NbDevice) WriteToDb(cntx context.Context, portID uint32, ponPort *PonPortCfg) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530499 b, err := json.Marshal(ponPort)
500 if err != nil {
501 logger.Errorw(ctx, "PonPortConfig-marshal-failed", log.Fields{"err": err})
502 return
503 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530504 db.PutNbDevicePort(cntx, nbd.SouthBoundID, portID, string(b))
Naveen Sampath04696f72022-06-13 15:19:14 +0530505}
506
507// AddPortToNbDevice Adds pon port to NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530508func (nbd *NbDevice) AddPortToNbDevice(cntx context.Context, portID, allowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530509 enableMulticastKPI bool, portAlarmProfileID string) *PonPortCfg {
510
511 ponPort := &PonPortCfg{
512 PortID: portID,
513 MaxActiveChannels: allowedChannels,
514 EnableMulticastKPI: enableMulticastKPI,
515 PortAlarmProfileID: portAlarmProfileID,
516 }
517 nbd.PonPorts.Store(portID, ponPort)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530518 nbd.WriteToDb(cntx, portID, ponPort)
Naveen Sampath04696f72022-06-13 15:19:14 +0530519 return ponPort
520}
521
522// UpdatePortToNbDevice Adds pon port to NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530523func (nbd *NbDevice) UpdatePortToNbDevice(cntx context.Context, portID, allowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) *PonPortCfg {
Naveen Sampath04696f72022-06-13 15:19:14 +0530524
525 p, exists := nbd.PonPorts.Load(portID)
526 if !exists {
527 logger.Errorw(ctx, "PON port not exists in nb-device", log.Fields{"portID": portID})
528 return nil
529 }
530 port := p.(*PonPortCfg)
531 if allowedChannels != 0 {
532 port.MaxActiveChannels = allowedChannels
533 port.EnableMulticastKPI = enableMulticastKPI
534 port.PortAlarmProfileID = portAlarmProfileID
535 }
536
537 nbd.PonPorts.Store(portID, port)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530538 nbd.WriteToDb(cntx, portID, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530539 return port
540}
541
542// DeletePortFromNbDevice Deletes pon port from NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530543func (nbd *NbDevice) DeletePortFromNbDevice(cntx context.Context, portID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530544
545 if _, ok := nbd.PonPorts.Load(portID); ok {
546 nbd.PonPorts.Delete(portID)
547 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530548 db.DelNbDevicePort(cntx, nbd.SouthBoundID, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530549}
550
551// GetApplication : Interface to access the singleton object
552func GetApplication() *VoltApplication {
553 if vapplication == nil {
554 vapplication = newVoltApplication()
555 }
556 return vapplication
557}
558
559// newVoltApplication : Constructor for the singleton object. Hence this is not
560// an exported function
561func newVoltApplication() *VoltApplication {
562 var va VoltApplication
563 va.IgmpTasks.Initialize(context.TODO())
564 va.MulticastAlarmTasks.Initialize(context.TODO())
565 va.IgmpKPIsTasks.Initialize(context.TODO())
566 va.pppoeTasks.Initialize(context.TODO())
567 va.storeIgmpProfileMap(DefaultIgmpProfID, newDefaultIgmpProfile())
568 va.MeterMgr.Init()
569 va.AddIgmpGroups(5000)
570 va.macPortMap = make(map[string]string)
571 va.IgmpPendingPool = make(map[string]map[*IgmpGroup]bool)
572 va.VnetsBySvlan = util.NewConcurrentMap()
573 va.VnetsToDelete = make(map[string]bool)
574 va.ServicesToDelete = make(map[string]bool)
575 va.VoltPortVnetsToDelete = make(map[*VoltPortVnet]bool)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530576 go va.Start(context.Background(), TimerCfg{tick: 100 * time.Millisecond}, tickTimer)
577 go va.Start(context.Background(), TimerCfg{tick: time.Duration(GroupExpiryTime) * time.Minute}, pendingPoolTimer)
Naveen Sampath04696f72022-06-13 15:19:14 +0530578 InitEventFuncMapper()
579 db = database.GetDatabase()
580 return &va
581}
582
583//GetFlowEventRegister - returs the register based on flow mod type
584func (d *VoltDevice) GetFlowEventRegister(flowModType of.Command) (*util.ConcurrentMap, error) {
585
586 switch flowModType {
587 case of.CommandDel:
588 return d.FlowDelEventMap, nil
589 case of.CommandAdd:
590 return d.FlowAddEventMap, nil
591 default:
592 logger.Error(ctx, "Unknown Flow Mod received")
593 }
594 return util.NewConcurrentMap(), errors.New("Unknown Flow Mod")
595}
596
597// RegisterFlowAddEvent to register a flow event.
598func (d *VoltDevice) RegisterFlowAddEvent(cookie string, event *FlowEvent) {
599 logger.Debugw(ctx, "Registered Flow Add Event", log.Fields{"Cookie": cookie, "Event": event})
600 d.FlowAddEventMap.MapLock.Lock()
601 defer d.FlowAddEventMap.MapLock.Unlock()
602 d.FlowAddEventMap.Set(cookie, event)
603}
604
605// RegisterFlowDelEvent to register a flow event.
606func (d *VoltDevice) RegisterFlowDelEvent(cookie string, event *FlowEvent) {
607 logger.Debugw(ctx, "Registered Flow Del Event", log.Fields{"Cookie": cookie, "Event": event})
608 d.FlowDelEventMap.MapLock.Lock()
609 defer d.FlowDelEventMap.MapLock.Unlock()
610 d.FlowDelEventMap.Set(cookie, event)
611}
612
613// UnRegisterFlowEvent to unregister a flow event.
614func (d *VoltDevice) UnRegisterFlowEvent(cookie string, flowModType of.Command) {
615 logger.Debugw(ctx, "UnRegistered Flow Add Event", log.Fields{"Cookie": cookie, "Type": flowModType})
616 flowEventMap, err := d.GetFlowEventRegister(flowModType)
617 if err != nil {
618 logger.Debugw(ctx, "Flow event map does not exists", log.Fields{"flowMod": flowModType, "Error": err})
619 return
620 }
621 flowEventMap.MapLock.Lock()
622 defer flowEventMap.MapLock.Unlock()
623 flowEventMap.Remove(cookie)
624}
625
626// AddIgmpGroups to add Igmp groups.
627func (va *VoltApplication) AddIgmpGroups(numOfGroups uint32) {
628 //TODO: Temp change to resolve group id issue in pOLT
629 //for i := 1; uint32(i) <= numOfGroups; i++ {
630 for i := 2; uint32(i) <= (numOfGroups + 1); i++ {
631 ig := IgmpGroup{}
632 ig.GroupID = uint32(i)
633 va.IgmpGroupIds = append(va.IgmpGroupIds, &ig)
634 }
635}
636
637// GetAvailIgmpGroupID to get id of available igmp group.
638func (va *VoltApplication) GetAvailIgmpGroupID() *IgmpGroup {
639 var ig *IgmpGroup
640 if len(va.IgmpGroupIds) > 0 {
641 ig, va.IgmpGroupIds = va.IgmpGroupIds[0], va.IgmpGroupIds[1:]
642 return ig
643 }
644 return nil
645}
646
647// GetIgmpGroupID to get id of igmp group.
648func (va *VoltApplication) GetIgmpGroupID(gid uint32) (*IgmpGroup, error) {
649 for id, ig := range va.IgmpGroupIds {
650 if ig.GroupID == gid {
651 va.IgmpGroupIds = append(va.IgmpGroupIds[0:id], va.IgmpGroupIds[id+1:]...)
652 return ig, nil
653 }
654 }
655 return nil, errors.New("Group Id Missing")
656}
657
658// PutIgmpGroupID to add id of igmp group.
659func (va *VoltApplication) PutIgmpGroupID(ig *IgmpGroup) {
660 va.IgmpGroupIds = append([]*IgmpGroup{ig}, va.IgmpGroupIds[0:]...)
661}
662
663//RestoreUpgradeStatus - gets upgrade/migration status from DB and updates local flags
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530664func (va *VoltApplication) RestoreUpgradeStatus(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530665 Migrate := new(DataMigration)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530666 if err := GetMigrationInfo(cntx, Migrate); err == nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530667 if Migrate.Status == MigrationInProgress {
668 isUpgradeComplete = false
669 return
670 }
671 }
672 isUpgradeComplete = true
673
674 logger.Infow(ctx, "Upgrade Status Restored", log.Fields{"Upgrade Completed": isUpgradeComplete})
675}
676
677// ReadAllFromDb : If we are restarted, learn from the database the current execution
678// stage
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530679func (va *VoltApplication) ReadAllFromDb(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530680 logger.Info(ctx, "Reading the meters from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530681 va.RestoreMetersFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530682 logger.Info(ctx, "Reading the VNETs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530683 va.RestoreVnetsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530684 logger.Info(ctx, "Reading the VPVs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530685 va.RestoreVpvsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530686 logger.Info(ctx, "Reading the Services from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530687 va.RestoreSvcsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530688 logger.Info(ctx, "Reading the MVLANs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530689 va.RestoreMvlansFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530690 logger.Info(ctx, "Reading the IGMP profiles from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530691 va.RestoreIGMPProfilesFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530692 logger.Info(ctx, "Reading the Mcast configs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530693 va.RestoreMcastConfigsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530694 logger.Info(ctx, "Reading the IGMP groups for DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530695 va.RestoreIgmpGroupsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530696 logger.Info(ctx, "Reading Upgrade status from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530697 va.RestoreUpgradeStatus(cntx)
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530698 logger.Info(ctx, "Reading OltFlowService from DB")
699 va.RestoreOltFlowService(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530700 logger.Info(ctx, "Reconciled from DB")
701}
702
703// InitStaticConfig to initialise static config.
704func (va *VoltApplication) InitStaticConfig() {
705 va.InitIgmpSrcMac()
706}
707
708// SetVendorID to set vendor id
709func (va *VoltApplication) SetVendorID(vendorID string) {
710 va.vendorID = vendorID
711}
712
713// GetVendorID to get vendor id
714func (va *VoltApplication) GetVendorID() string {
715 return va.vendorID
716}
717
718// SetRebootFlag to set reboot flag
719func (va *VoltApplication) SetRebootFlag(flag bool) {
720 vgcRebooted = flag
721}
722
723// GetUpgradeFlag to get reboot status
724func (va *VoltApplication) GetUpgradeFlag() bool {
725 return isUpgradeComplete
726}
727
728// SetUpgradeFlag to set reboot status
729func (va *VoltApplication) SetUpgradeFlag(flag bool) {
730 isUpgradeComplete = flag
731}
732
733// ------------------------------------------------------------
734// Device related functions
735
736// AddDevice : Add a device and typically the device stores the NNI port on the device
737// The NNI port is used when the packets are emitted towards the network.
738// The outport is selected as the NNI port of the device. Today, we support
739// a single NNI port per OLT. This is true whether the network uses any
740// protection mechanism (LAG, ERPS, etc.). The aggregate of the such protection
741// is represented by a single NNI port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530742func (va *VoltApplication) AddDevice(cntx context.Context, device string, slno, southBoundID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530743 logger.Warnw(ctx, "Received Device Ind: Add", log.Fields{"Device": device, "SrNo": slno})
744 if _, ok := va.DevicesDisc.Load(device); ok {
745 logger.Warnw(ctx, "Device Exists", log.Fields{"Device": device})
746 }
747 d := NewVoltDevice(device, slno, southBoundID)
748
749 addPort := func(key, value interface{}) bool {
750 portID := key.(uint32)
751 port := value.(*PonPortCfg)
752 va.AggActiveChannelsCountForPonPort(device, portID, port)
753 d.ActiveChannelsPerPon.Store(portID, port)
754 return true
755 }
756 if nbDevice, exists := va.NbDevice.Load(southBoundID); exists {
757 // Pon Ports added before OLT activate.
758 nbDevice.(*NbDevice).PonPorts.Range(addPort)
759 } else {
760 // Check if NbPort exists in DB. VGC restart case.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530761 nbd := va.RestoreNbDeviceFromDb(cntx, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530762 nbd.PonPorts.Range(addPort)
763 }
764 va.DevicesDisc.Store(device, d)
765}
766
767// GetDevice to get a device.
768func (va *VoltApplication) GetDevice(device string) *VoltDevice {
769 if d, ok := va.DevicesDisc.Load(device); ok {
770 return d.(*VoltDevice)
771 }
772 return nil
773}
774
775// DelDevice to delete a device.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530776func (va *VoltApplication) DelDevice(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530777 logger.Warnw(ctx, "Received Device Ind: Delete", log.Fields{"Device": device})
778 if vdIntf, ok := va.DevicesDisc.Load(device); ok {
779 vd := vdIntf.(*VoltDevice)
780 va.DevicesDisc.Delete(device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530781 _ = db.DelAllRoutesForDevice(cntx, device)
782 va.HandleFlowClearFlag(cntx, device, vd.SerialNum, vd.SouthBoundID)
783 _ = db.DelAllGroup(cntx, device)
784 _ = db.DelAllMeter(cntx, device)
785 _ = db.DelAllPorts(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530786 logger.Debugw(ctx, "Device deleted", log.Fields{"Device": device})
787 } else {
788 logger.Warnw(ctx, "Device Doesn't Exist", log.Fields{"Device": device})
789 }
790}
791
792// GetDeviceBySerialNo to get a device by serial number.
793// TODO - Transform this into a MAP instead
794func (va *VoltApplication) GetDeviceBySerialNo(slno string) *VoltDevice {
795 var device *VoltDevice
796 getserial := func(key interface{}, value interface{}) bool {
797 device = value.(*VoltDevice)
798 return device.SerialNum != slno
799 }
800 va.DevicesDisc.Range(getserial)
801 return device
802}
803
804// PortAddInd : This is a PORT add indication coming from the VPAgent, which is essentially
805// a request coming from VOLTHA. The device and identity of the port is provided
806// in this request. Add them to the application for further use
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530807func (va *VoltApplication) PortAddInd(cntx context.Context, device string, id uint32, portName string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530808 logger.Infow(ctx, "Received Port Ind: Add", log.Fields{"Device": device, "Port": portName})
809 va.portLock.Lock()
810 if d := va.GetDevice(device); d != nil {
811 p := d.AddPort(portName, id)
812 va.PortsDisc.Store(portName, p)
813 va.portLock.Unlock()
814 nni, _ := va.GetNniPort(device)
815 if nni == portName {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530816 d.pushFlowsForUnis(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530817 }
818 } else {
819 va.portLock.Unlock()
820 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Add", log.Fields{"Device": device, "Port": portName})
821 }
822}
823
824// PortDelInd : Only the NNI ports are recorded in the device for now. When port delete
825// arrives, only the NNI ports need adjustments.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530826func (va *VoltApplication) PortDelInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530827 logger.Infow(ctx, "Received Port Ind: Delete", log.Fields{"Device": device, "Port": port})
828 if d := va.GetDevice(device); d != nil {
829 p := d.GetPort(port)
830 if p != nil && p.State == PortStateUp {
831 logger.Infow(ctx, "Port state is UP. Trigerring Port Down Ind before deleting", log.Fields{"Port": p})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530832 va.PortDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530833 }
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530834 // if RemoveFlowsOnDisable is flase, then flows will be existing till port delete. Remove the flows now
835 if !va.OltFlowServiceConfig.RemoveFlowsOnDisable {
836 vpvs, ok := va.VnetsByPort.Load(port)
837 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
838 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
839 } else {
840 for _, vpv := range vpvs.([]*VoltPortVnet) {
841 vpv.VpvLock.Lock()
842 vpv.PortDownInd(cntx, device, port, true)
843 vpv.VpvLock.Unlock()
844 }
845 }
846 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530847 va.portLock.Lock()
848 defer va.portLock.Unlock()
849 d.DelPort(port)
850 if _, ok := va.PortsDisc.Load(port); ok {
851 va.PortsDisc.Delete(port)
852 }
853 } else {
854 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Delete", log.Fields{"Device": device, "Port": port})
855 }
856}
857
858//PortUpdateInd Updates port Id incase of ONU movement
859func (va *VoltApplication) PortUpdateInd(device string, portName string, id uint32) {
860 logger.Infow(ctx, "Received Port Ind: Update", log.Fields{"Device": device, "Port": portName})
861 va.portLock.Lock()
862 defer va.portLock.Unlock()
863 if d := va.GetDevice(device); d != nil {
864 vp := d.GetPort(portName)
865 vp.ID = id
866 } else {
867 logger.Warnw(ctx, "Device Not Found", log.Fields{"Device": device, "Port": portName})
868 }
869}
870
871// AddNbPonPort Add pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530872func (va *VoltApplication) AddNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530873 enableMulticastKPI bool, portAlarmProfileID string) error {
874
875 var nbd *NbDevice
876 nbDevice, ok := va.NbDevice.Load(oltSbID)
877
878 if !ok {
879 nbd = NewNbDevice()
880 nbd.SouthBoundID = oltSbID
881 } else {
882 nbd = nbDevice.(*NbDevice)
883 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530884 port := nbd.AddPortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530885
886 // Add this port to voltDevice
887 addPort := func(key, value interface{}) bool {
888 voltDevice := value.(*VoltDevice)
889 if oltSbID == voltDevice.SouthBoundID {
890 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); !exists {
891 voltDevice.ActiveChannelsPerPon.Store(portID, port)
892 }
893 return false
894 }
895 return true
896 }
897 va.DevicesDisc.Range(addPort)
898 va.NbDevice.Store(oltSbID, nbd)
899
900 return nil
901}
902
903// UpdateNbPonPort update pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530904func (va *VoltApplication) UpdateNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530905
906 var nbd *NbDevice
907 nbDevice, ok := va.NbDevice.Load(oltSbID)
908
909 if !ok {
910 logger.Errorw(ctx, "Device-doesn't-exists", log.Fields{"deviceID": oltSbID})
911 return fmt.Errorf("Device-doesn't-exists-%v", oltSbID)
912 }
913 nbd = nbDevice.(*NbDevice)
914
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530915 port := nbd.UpdatePortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530916 if port == nil {
917 return fmt.Errorf("Port-doesn't-exists-%v", portID)
918 }
919 va.NbDevice.Store(oltSbID, nbd)
920
921 // Add this port to voltDevice
922 updPort := func(key, value interface{}) bool {
923 voltDevice := value.(*VoltDevice)
924 if oltSbID == voltDevice.SouthBoundID {
925 voltDevice.ActiveChannelCountLock.Lock()
926 if p, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
927 oldPort := p.(*PonPortCfg)
928 if port.MaxActiveChannels != 0 {
929 oldPort.MaxActiveChannels = port.MaxActiveChannels
930 oldPort.EnableMulticastKPI = port.EnableMulticastKPI
931 voltDevice.ActiveChannelsPerPon.Store(portID, oldPort)
932 }
933 }
934 voltDevice.ActiveChannelCountLock.Unlock()
935 return false
936 }
937 return true
938 }
939 va.DevicesDisc.Range(updPort)
940
941 return nil
942}
943
944// DeleteNbPonPort Delete pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530945func (va *VoltApplication) DeleteNbPonPort(cntx context.Context, oltSbID string, portID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530946 nbDevice, ok := va.NbDevice.Load(oltSbID)
947 if ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530948 nbDevice.(*NbDevice).DeletePortFromNbDevice(cntx, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530949 va.NbDevice.Store(oltSbID, nbDevice.(*NbDevice))
950 } else {
951 logger.Warnw(ctx, "Delete pon received for unknown device", log.Fields{"oltSbID": oltSbID})
952 return nil
953 }
954 // Delete this port from voltDevice
955 delPort := func(key, value interface{}) bool {
956 voltDevice := value.(*VoltDevice)
957 if oltSbID == voltDevice.SouthBoundID {
958 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
959 voltDevice.ActiveChannelsPerPon.Delete(portID)
960 }
961 return false
962 }
963 return true
964 }
965 va.DevicesDisc.Range(delPort)
966 return nil
967}
968
969// GetNniPort : Get the NNI port for a device. Called from different other applications
970// as a port to match or destination for a packet out. The VOLT application
971// is written with the assumption that there is a single NNI port. The OLT
972// device is responsible for translating the combination of VLAN and the
973// NNI port ID to identify possibly a single physical port or a logical
974// port which is a result of protection methods applied.
975func (va *VoltApplication) GetNniPort(device string) (string, error) {
976 va.portLock.Lock()
977 defer va.portLock.Unlock()
978 d, ok := va.DevicesDisc.Load(device)
979 if !ok {
980 return "", errors.New("Device Doesn't Exist")
981 }
982 return d.(*VoltDevice).NniPort, nil
983}
984
985// NniDownInd process for Nni down indication.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530986func (va *VoltApplication) NniDownInd(cntx context.Context, deviceID string, devSrNo string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530987
988 logger.Debugw(ctx, "NNI Down Ind", log.Fields{"device": devSrNo})
989
990 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
991 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530992 mvProfile.removeIgmpMcastFlows(cntx, devSrNo)
Naveen Sampath04696f72022-06-13 15:19:14 +0530993 return true
994 }
995 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
996
997 //Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530998 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +0530999}
1000
1001// DeviceUpInd changes device state to up.
1002func (va *VoltApplication) DeviceUpInd(device string) {
1003 logger.Warnw(ctx, "Received Device Ind: UP", log.Fields{"Device": device})
1004 if d := va.GetDevice(device); d != nil {
1005 d.State = controller.DeviceStateUP
1006 } else {
1007 logger.Errorw(ctx, "Ignoring Device indication: UP. Device Missing", log.Fields{"Device": device})
1008 }
1009}
1010
1011// DeviceDownInd changes device state to down.
1012func (va *VoltApplication) DeviceDownInd(device string) {
1013 logger.Warnw(ctx, "Received Device Ind: DOWN", log.Fields{"Device": device})
1014 if d := va.GetDevice(device); d != nil {
1015 d.State = controller.DeviceStateDOWN
1016 } else {
1017 logger.Errorw(ctx, "Ignoring Device indication: DOWN. Device Missing", log.Fields{"Device": device})
1018 }
1019}
1020
1021// DeviceRebootInd process for handling flow clear flag for device reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301022func (va *VoltApplication) DeviceRebootInd(cntx context.Context, device string, serialNum string, southBoundID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301023 logger.Warnw(ctx, "Received Device Ind: Reboot", log.Fields{"Device": device, "SerialNumber": serialNum})
1024
1025 if d := va.GetDevice(device); d != nil {
1026 if d.State == controller.DeviceStateREBOOTED {
1027 logger.Warnw(ctx, "Ignoring Device Ind: Reboot, Device already in Reboot state", log.Fields{"Device": device, "SerialNumber": serialNum, "State": d.State})
1028 return
1029 }
1030 d.State = controller.DeviceStateREBOOTED
1031 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301032 va.HandleFlowClearFlag(cntx, device, serialNum, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301033
1034}
1035
1036// DeviceDisableInd handles device deactivation process
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301037func (va *VoltApplication) DeviceDisableInd(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301038 logger.Warnw(ctx, "Received Device Ind: Disable", log.Fields{"Device": device})
1039
1040 d := va.GetDevice(device)
1041 if d == nil {
1042 logger.Errorw(ctx, "Ignoring Device indication: DISABLED. Device Missing", log.Fields{"Device": device})
1043 return
1044 }
1045
1046 d.State = controller.DeviceStateDISABLED
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301047 va.HandleFlowClearFlag(cntx, device, d.SerialNum, d.SouthBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301048}
1049
1050// ProcessIgmpDSFlowForMvlan for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301051func (va *VoltApplication) ProcessIgmpDSFlowForMvlan(cntx context.Context, d *VoltDevice, mvp *MvlanProfile, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301052
1053 logger.Debugw(ctx, "Process IGMP DS Flows for MVlan", log.Fields{"device": d.Name, "Mvlan": mvp.Mvlan, "addFlow": addFlow})
1054 portState := false
1055 p := d.GetPort(d.NniPort)
1056 if p != nil && p.State == PortStateUp {
1057 portState = true
1058 }
1059
1060 if addFlow {
1061 if portState {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301062 mvp.pushIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301063 }
1064 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301065 mvp.removeIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301066 }
1067}
1068
1069// ProcessIgmpDSFlowForDevice for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301070func (va *VoltApplication) ProcessIgmpDSFlowForDevice(cntx context.Context, d *VoltDevice, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301071 logger.Debugw(ctx, "Process IGMP DS Flows for device", log.Fields{"device": d.Name, "addFlow": addFlow})
1072
1073 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1074 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301075 va.ProcessIgmpDSFlowForMvlan(cntx, d, mvProfile, addFlow)
Naveen Sampath04696f72022-06-13 15:19:14 +05301076 return true
1077 }
1078 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1079}
1080
1081// GetDeviceFromPort : This is suitable only for access ports as their naming convention
1082// makes them unique across all the OLTs. This must be called with
1083// port name that is an access port. Currently called from VNETs, attached
1084// only to access ports, and the services which are also attached only
1085// to access ports
1086func (va *VoltApplication) GetDeviceFromPort(port string) (*VoltDevice, error) {
1087 va.portLock.Lock()
1088 defer va.portLock.Unlock()
1089 var err error
1090 err = nil
1091 p, ok := va.PortsDisc.Load(port)
1092 if !ok {
1093 return nil, errorCodes.ErrPortNotFound
1094 }
1095 d := va.GetDevice(p.(*VoltPort).Device)
1096 if d == nil {
1097 err = errorCodes.ErrDeviceNotFound
1098 }
1099 return d, err
1100}
1101
1102// GetPortID : This too applies only to access ports. The ports can be indexed
1103// purely by their names without the device forming part of the key
1104func (va *VoltApplication) GetPortID(port string) (uint32, error) {
1105 va.portLock.Lock()
1106 defer va.portLock.Unlock()
1107 p, ok := va.PortsDisc.Load(port)
1108 if !ok {
1109 return 0, errorCodes.ErrPortNotFound
1110 }
1111 return p.(*VoltPort).ID, nil
1112}
1113
1114// GetPortName : This too applies only to access ports. The ports can be indexed
1115// purely by their names without the device forming part of the key
1116func (va *VoltApplication) GetPortName(port uint32) (string, error) {
1117 va.portLock.Lock()
1118 defer va.portLock.Unlock()
1119 var portName string
1120 va.PortsDisc.Range(func(key interface{}, value interface{}) bool {
1121 portInfo := value.(*VoltPort)
1122 if portInfo.ID == port {
1123 portName = portInfo.Name
1124 return false
1125 }
1126 return true
1127 })
1128 return portName, nil
1129}
1130
1131// GetPonFromUniPort to get Pon info from UniPort
1132func (va *VoltApplication) GetPonFromUniPort(port string) (string, error) {
1133 uniPortID, err := va.GetPortID(port)
1134 if err == nil {
1135 ponPortID := (uniPortID & 0x0FF00000) >> 20 //pon(8) + onu(8) + uni(12)
1136 return strconv.FormatUint(uint64(ponPortID), 10), nil
1137 }
1138 return "", err
1139}
1140
1141// GetPortState : This too applies only to access ports. The ports can be indexed
1142// purely by their names without the device forming part of the key
1143func (va *VoltApplication) GetPortState(port string) (PortState, error) {
1144 va.portLock.Lock()
1145 defer va.portLock.Unlock()
1146 p, ok := va.PortsDisc.Load(port)
1147 if !ok {
1148 return 0, errors.New("Port not configured")
1149 }
1150 return p.(*VoltPort).State, nil
1151}
1152
1153// GetIcmpv6Receivers to get Icmp v6 receivers
1154func (va *VoltApplication) GetIcmpv6Receivers(device string) []uint32 {
1155 var receiverList []uint32
1156 receivers, _ := va.Icmpv6Receivers.Load(device)
1157 if receivers != nil {
1158 receiverList = receivers.([]uint32)
1159 }
1160 return receiverList
1161}
1162
1163// AddIcmpv6Receivers to add Icmp v6 receivers
1164func (va *VoltApplication) AddIcmpv6Receivers(device string, portID uint32) []uint32 {
1165 var receiverList []uint32
1166 receivers, _ := va.Icmpv6Receivers.Load(device)
1167 if receivers != nil {
1168 receiverList = receivers.([]uint32)
1169 }
1170 receiverList = append(receiverList, portID)
1171 va.Icmpv6Receivers.Store(device, receiverList)
1172 logger.Debugw(ctx, "Receivers after addition", log.Fields{"Receivers": receiverList})
1173 return receiverList
1174}
1175
1176// DelIcmpv6Receivers to delete Icmp v6 receievers
1177func (va *VoltApplication) DelIcmpv6Receivers(device string, portID uint32) []uint32 {
1178 var receiverList []uint32
1179 receivers, _ := va.Icmpv6Receivers.Load(device)
1180 if receivers != nil {
1181 receiverList = receivers.([]uint32)
1182 }
1183 for i, port := range receiverList {
1184 if port == portID {
1185 receiverList = append(receiverList[0:i], receiverList[i+1:]...)
1186 va.Icmpv6Receivers.Store(device, receiverList)
1187 break
1188 }
1189 }
1190 logger.Debugw(ctx, "Receivers After deletion", log.Fields{"Receivers": receiverList})
1191 return receiverList
1192}
1193
1194// ProcessDevFlowForDevice - Process DS ICMPv6 & ARP flow for provided device and vnet profile
1195// device - Device Obj
1196// vnet - vnet profile name
1197// enabled - vlan enabled/disabled - based on the status, the flow shall be added/removed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301198func (va *VoltApplication) ProcessDevFlowForDevice(cntx context.Context, device *VoltDevice, vnet *VoltVnet, enabled bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301199 _, applied := device.ConfiguredVlanForDeviceFlows.Get(VnetKey(vnet.SVlan, vnet.CVlan, 0))
1200 if enabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301201 va.PushDevFlowForVlan(cntx, vnet)
Naveen Sampath04696f72022-06-13 15:19:14 +05301202 } else if !enabled && applied {
1203 //va.DeleteDevFlowForVlan(vnet)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301204 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, device.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301205 }
1206}
1207
1208//NniVlanIndToIgmp - Trigger receiver up indication to all ports with igmp enabled
1209//and has the provided mvlan
1210func (va *VoltApplication) NniVlanIndToIgmp(device *VoltDevice, mvp *MvlanProfile) {
1211
1212 logger.Infow(ctx, "Sending Igmp Receiver UP indication for all Services", log.Fields{"Vlan": mvp.Mvlan})
1213
1214 //Trigger nni indication for receiver only for first time
1215 if device.IgmpDsFlowAppliedForMvlan[uint16(mvp.Mvlan)] {
1216 return
1217 }
1218 device.Ports.Range(func(key, value interface{}) bool {
1219 port := key.(string)
1220
1221 if state, _ := va.GetPortState(port); state == PortStateUp {
1222 vpvs, _ := va.VnetsByPort.Load(port)
1223 if vpvs == nil {
1224 return true
1225 }
1226 for _, vpv := range vpvs.([]*VoltPortVnet) {
1227 //Send indication only for subscribers with the received mvlan profile
1228 if vpv.IgmpEnabled && vpv.MvlanProfileName == mvp.Name {
1229 vpv.services.Range(ReceiverUpInd)
1230 }
1231 }
1232 }
1233 return true
1234 })
1235}
1236
1237// PortUpInd :
1238// -----------------------------------------------------------------------
1239// Port status change handling
1240// ----------------------------------------------------------------------
1241// Port UP indication is passed to all services associated with the port
1242// so that the services can configure flows applicable when the port goes
1243// up from down state
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301244func (va *VoltApplication) PortUpInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301245 d := va.GetDevice(device)
1246
1247 if d == nil {
1248 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: UP", log.Fields{"Device": device, "Port": port})
1249 return
1250 }
1251
1252 //Fixme: If Port Update Comes in large numbers, this will result in slow update per device
1253 va.portLock.Lock()
1254 // Do not defer the port mutex unlock here
1255 // Some of the following func calls needs the port lock, so defering the lock here
1256 // may lead to dead-lock
1257 p := d.GetPort(port)
1258
1259 if p == nil {
1260 logger.Infow(ctx, "Ignoring Port Ind: UP, Port doesnt exist", log.Fields{"Device": device, "PortName": port, "PortId": p})
1261 va.portLock.Unlock()
1262 return
1263 }
1264 p.State = PortStateUp
1265 va.portLock.Unlock()
1266
1267 logger.Infow(ctx, "Received SouthBound Port Ind: UP", log.Fields{"Device": device, "PortName": port, "PortId": p.ID})
1268 if p.Type == VoltPortTypeNni {
1269
1270 logger.Warnw(ctx, "Received NNI Port Ind: UP", log.Fields{"Device": device, "PortName": port, "PortId": p.ID})
1271 //va.PushDevFlowForDevice(d)
1272 //Build Igmp TrapFlowRule
1273 //va.ProcessIgmpDSFlowForDevice(d, true)
1274 }
1275 vpvs, ok := va.VnetsByPort.Load(port)
1276 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
1277 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
1278 //msgbus.ProcessPortInd(msgbus.PortUp, d.SerialNum, p.Name, false, getServiceList(port))
1279 return
1280 }
1281
1282 //If NNI port is not UP, do not push Flows
1283 if d.NniPort == "" {
1284 logger.Warnw(ctx, "NNI port not UP. Not sending Port UP Ind for VPVs", log.Fields{"NNI": d.NniPort})
1285 return
1286 }
1287
1288 vpvList := vpvs.([]*VoltPortVnet)
1289 if vpvList[0].PonPort != 0xFF && vpvList[0].PonPort != p.PonPort {
1290 logger.Errorw(ctx, "UNI port discovered on wrong PON Port. Dropping Port Indication", log.Fields{"Device": device, "Port": port, "DetectedPon": p.PonPort, "ExpectedPon": vpvList[0].PonPort})
1291
1292 //Remove the flow (if any) which are already installed - Valid for PON switching when VGC pod is DOWN
1293 for _, vpv := range vpvs.([]*VoltPortVnet) {
1294 vpv.VpvLock.Lock()
1295 logger.Warnw(ctx, "Removing existing VPVs/Services flows for for Subscriber: UNI Detected on wrong PON", log.Fields{"Port": vpv.Port, "Vnet": vpv.VnetName})
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301296 vpv.PortDownInd(cntx, device, port, false)
Naveen Sampath04696f72022-06-13 15:19:14 +05301297 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301298 va.ReceiverDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +05301299 }
1300 vpv.VpvLock.Unlock()
1301 }
1302 return
1303 }
1304
1305/*
1306 if p.Type != VoltPortTypeNni {
1307 // Process port up indication
1308 indTask := cntlr.NewAddPortInd(p.Name, msgbus.PortUp, d.SerialNum, true, getServiceList(port))
1309 cntlr.GetController().PostIndication(device, indTask)
1310 }
1311*/
1312
1313 for _, vpv := range vpvs.([]*VoltPortVnet) {
1314 vpv.VpvLock.Lock()
Tinoj Josephec742f62022-09-29 19:11:10 +05301315 //If no service is activated drop the portUpInd
1316 if vpv.IsServiceActivated(cntx) {
1317 //Do not trigger indication for the vpv which is already removed from vpv list as
1318 // part of service delete (during the lock wait duration)
1319 // In that case, the services associated wil be zero
1320 if vpv.servicesCount.Load() != 0 {
1321 vpv.PortUpInd(cntx, d, port)
1322 }
1323 } else {
1324 // Service not activated, still attach device to service
1325 vpv.setDevice(d.Name)
Naveen Sampath04696f72022-06-13 15:19:14 +05301326 }
1327 vpv.VpvLock.Unlock()
1328 }
1329 // At the end of processing inform the other entities that
1330 // are interested in the events
1331}
1332
1333/*
1334func getServiceList(port string) map[string]bool {
1335 serviceList := make(map[string]bool)
1336
1337 getServiceNames := func(key interface{}, value interface{}) bool {
1338 serviceList[key.(string)] = value.(*VoltService).DsHSIAFlowsApplied
1339 return true
1340 }
1341
1342 if vpvs, _ := GetApplication().VnetsByPort.Load(port); vpvs != nil {
1343 vpvList := vpvs.([]*VoltPortVnet)
1344 for _, vpv := range vpvList {
1345 vpv.services.Range(getServiceNames)
1346 }
1347 }
1348 return serviceList
1349
1350}*/
1351
1352//ReceiverUpInd - Send receiver up indication for service with Igmp enabled
1353func ReceiverUpInd(key, value interface{}) bool {
1354 svc := value.(*VoltService)
1355 var vlan of.VlanType
1356
1357 if !svc.IPAssigned() {
1358 logger.Infow(ctx, "IP Not assigned, skipping general query", log.Fields{"Service": svc})
1359 return false
1360 }
1361
1362 //Send port up indication to igmp only for service with igmp enabled
1363 if svc.IgmpEnabled {
1364 if svc.VlanControl == ONUCVlan || svc.VlanControl == ONUCVlanOLTSVlan {
1365 vlan = svc.CVlan
1366 } else {
1367 vlan = svc.UniVlan
1368 }
1369 if device, _ := GetApplication().GetDeviceFromPort(svc.Port); device != nil {
1370 GetApplication().ReceiverUpInd(device.Name, svc.Port, svc.MvlanProfileName, vlan, svc.Pbits)
1371 }
1372 return false
1373 }
1374 return true
1375}
1376
1377// PortDownInd : Port down indication is passed on to the services so that the services
1378// can make changes at this transition.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301379func (va *VoltApplication) PortDownInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301380 logger.Infow(ctx, "Received SouthBound Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1381 d := va.GetDevice(device)
1382
1383 if d == nil {
1384 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1385 return
1386 }
1387 //Fixme: If Port Update Comes in large numbers, this will result in slow update per device
1388 va.portLock.Lock()
1389 // Do not defer the port mutex unlock here
1390 // Some of the following func calls needs the port lock, so defering the lock here
1391 // may lead to dead-lock
1392 p := d.GetPort(port)
1393 if p == nil {
1394 logger.Infow(ctx, "Ignoring Port Ind: Down, Port doesnt exist", log.Fields{"Device": device, "PortName": port, "PortId": p})
1395 va.portLock.Unlock()
1396 return
1397 }
1398 p.State = PortStateDown
1399 va.portLock.Unlock()
1400
1401 if d.State == controller.DeviceStateREBOOTED {
1402 logger.Infow(ctx, "Ignoring Port Ind: Down, Device has been Rebooted", log.Fields{"Device": device, "PortName": port, "PortId": p})
1403 return
1404 }
1405
1406 if p.Type == VoltPortTypeNni {
1407 logger.Warnw(ctx, "Received NNI Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301408 va.DeleteDevFlowForDevice(cntx, d)
1409 va.NniDownInd(cntx, device, d.SerialNum)
1410 va.RemovePendingGroups(cntx, device, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301411 }
1412 vpvs, ok := va.VnetsByPort.Load(port)
1413 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
1414 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
1415 //msgbus.ProcessPortInd(msgbus.PortDown, d.SerialNum, p.Name, false, getServiceList(port))
1416 return
1417 }
1418/*
1419 if p.Type != VoltPortTypeNni {
1420 // Process port down indication
1421 indTask := cntlr.NewAddPortInd(p.Name, msgbus.PortDown, d.SerialNum, true, getServiceList(port))
1422 cntlr.GetController().PostIndication(device, indTask)
1423 }
1424*/
1425 for _, vpv := range vpvs.([]*VoltPortVnet) {
1426 vpv.VpvLock.Lock()
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301427 vpv.PortDownInd(cntx, device, port, false)
Naveen Sampath04696f72022-06-13 15:19:14 +05301428 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301429 va.ReceiverDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +05301430 }
1431 vpv.VpvLock.Unlock()
1432 }
1433}
1434
1435// PacketInInd :
1436// -----------------------------------------------------------------------
1437// PacketIn Processing
1438// Packet In Indication processing. It arrives with the identities of
1439// the device and port on which the packet is received. At first, the
1440// packet is decoded and the right processor is called. Currently, we
1441// plan to support only DHCP and IGMP. In future, we can add more
1442// capabilities as needed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301443func (va *VoltApplication) PacketInInd(cntx context.Context, device string, port string, pkt []byte) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301444 // Decode the incoming packet
1445 packetSide := US
1446 if strings.Contains(port, NNI) {
1447 packetSide = DS
1448 }
1449
1450 logger.Debugw(ctx, "Received a Packet-In Indication", log.Fields{"Device": device, "Port": port})
1451
1452 gopkt := gopacket.NewPacket(pkt, layers.LayerTypeEthernet, gopacket.Default)
1453
1454 var dot1qFound = false
1455 for _, l := range gopkt.Layers() {
1456 if l.LayerType() == layers.LayerTypeDot1Q {
1457 dot1qFound = true
1458 break
1459 }
1460 }
1461
1462 if !dot1qFound {
1463 logger.Debugw(ctx, "Ignoring Received Packet-In Indication without Dot1Q Header",
1464 log.Fields{"Device": device, "Port": port})
1465 return
1466 }
1467
1468 logger.Debugw(ctx, "Received Southbound Packet In", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1469
1470 // Classify the packet into packet types that we support
1471 // The supported types are DHCP and IGMP. The DHCP packet is
1472 // identified by matching the L4 protocol to UDP. The IGMP packet
1473 // is identified by matching L3 protocol to IGMP
1474 arpl := gopkt.Layer(layers.LayerTypeARP)
1475 if arpl != nil {
1476 if callBack, ok := PacketHandlers[ARP]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301477 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301478 } else {
1479 logger.Debugw(ctx, "ARP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1480 }
1481 return
1482 }
1483 ipv4l := gopkt.Layer(layers.LayerTypeIPv4)
1484 if ipv4l != nil {
1485 ip := ipv4l.(*layers.IPv4)
1486
1487 if ip.Protocol == layers.IPProtocolUDP {
1488 logger.Debugw(ctx, "Received Southbound UDP ipv4 packet in", log.Fields{"StreamSide": packetSide})
1489 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv4)
1490 if dhcpl != nil {
1491 if callBack, ok := PacketHandlers[DHCPv4]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301492 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301493 } else {
1494 logger.Debugw(ctx, "DHCPv4 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1495 }
1496 }
1497 } else if ip.Protocol == layers.IPProtocolIGMP {
1498 logger.Debugw(ctx, "Received Southbound IGMP packet in", log.Fields{"StreamSide": packetSide})
1499 if callBack, ok := PacketHandlers[IGMP]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301500 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301501 } else {
1502 logger.Debugw(ctx, "IGMP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1503 }
1504 }
1505 return
1506 }
1507 ipv6l := gopkt.Layer(layers.LayerTypeIPv6)
1508 if ipv6l != nil {
1509 ip := ipv6l.(*layers.IPv6)
1510 if ip.NextHeader == layers.IPProtocolUDP {
1511 logger.Debug(ctx, "Received Southbound UDP ipv6 packet in")
1512 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv6)
1513 if dhcpl != nil {
1514 if callBack, ok := PacketHandlers[DHCPv6]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301515 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301516 } else {
1517 logger.Debugw(ctx, "DHCPv6 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1518 }
1519 }
1520 }
1521 return
1522 }
1523
1524 pppoel := gopkt.Layer(layers.LayerTypePPPoE)
1525 if pppoel != nil {
1526 logger.Debugw(ctx, "Received Southbound PPPoE packet in", log.Fields{"StreamSide": packetSide})
1527 if callBack, ok := PacketHandlers[PPPOE]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301528 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301529 } else {
1530 logger.Debugw(ctx, "PPPoE handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1531 }
1532 }
1533}
1534
1535// GetVlans : This utility gets the VLANs from the packet. The VLANs are
1536// used to identify the right service that must process the incoming
1537// packet
1538func GetVlans(pkt gopacket.Packet) []of.VlanType {
1539 var vlans []of.VlanType
1540 for _, l := range pkt.Layers() {
1541 if l.LayerType() == layers.LayerTypeDot1Q {
1542 q, ok := l.(*layers.Dot1Q)
1543 if ok {
1544 vlans = append(vlans, of.VlanType(q.VLANIdentifier))
1545 }
1546 }
1547 }
1548 return vlans
1549}
1550
1551// GetPriority to get priority
1552func GetPriority(pkt gopacket.Packet) uint8 {
1553 for _, l := range pkt.Layers() {
1554 if l.LayerType() == layers.LayerTypeDot1Q {
1555 q, ok := l.(*layers.Dot1Q)
1556 if ok {
1557 return q.Priority
1558 }
1559 }
1560 }
1561 return PriorityNone
1562}
1563
1564// HandleFlowClearFlag to handle flow clear flag during reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301565func (va *VoltApplication) HandleFlowClearFlag(cntx context.Context, deviceID string, serialNum, southBoundID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301566 logger.Warnw(ctx, "Clear All flags for Device", log.Fields{"Device": deviceID, "SerialNum": serialNum, "SBID": southBoundID})
1567 dev, ok := va.DevicesDisc.Load(deviceID)
1568 if ok && dev != nil {
1569 logger.Infow(ctx, "Clear Flags for device", log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1570 dev.(*VoltDevice).icmpv6GroupAdded = false
1571 logger.Infow(ctx, "Clearing DS Icmpv6 Map",
1572 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1573 dev.(*VoltDevice).ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
1574 logger.Infow(ctx, "Clearing DS IGMP Map",
1575 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1576 for k := range dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan {
1577 delete(dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan, k)
1578 }
1579 //Delete group 1 - ICMPv6/ARP group
1580 if err := ProcessIcmpv6McGroup(deviceID, true); err != nil {
1581 logger.Errorw(ctx, "ProcessIcmpv6McGroup failed", log.Fields{"Device": deviceID, "Error": err})
1582 }
1583 } else {
1584 logger.Warnw(ctx, "VoltDevice not found for device ", log.Fields{"deviceID": deviceID})
1585 }
1586
1587 getVpvs := func(key interface{}, value interface{}) bool {
1588 vpvs := value.([]*VoltPortVnet)
1589 for _, vpv := range vpvs {
1590 if vpv.Device == deviceID {
1591 logger.Infow(ctx, "Clear Flags for vpv",
1592 log.Fields{"device": vpv.Device, "port": vpv.Port,
1593 "svlan": vpv.SVlan, "cvlan": vpv.CVlan, "univlan": vpv.UniVlan})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301594 vpv.ClearAllServiceFlags(cntx)
1595 vpv.ClearAllVpvFlags(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301596
1597 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301598 va.ReceiverDownInd(cntx, vpv.Device, vpv.Port)
Naveen Sampath04696f72022-06-13 15:19:14 +05301599 //Also clear service igmp stats
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301600 vpv.ClearServiceCounters(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301601 }
1602 }
1603 }
1604 return true
1605 }
1606 va.VnetsByPort.Range(getVpvs)
1607
1608 //Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301609 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301610
1611 logger.Warnw(ctx, "All flags cleared for device", log.Fields{"Device": deviceID})
1612
1613 //Reset pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301614 va.RemovePendingGroups(cntx, deviceID, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301615
1616 //Process all Migrate Service Request - force udpate all profiles since resources are already cleaned up
1617 if dev != nil {
1618 triggerForceUpdate := func(key, value interface{}) bool {
1619 msrList := value.(*util.ConcurrentMap)
1620 forceUpdateServices := func(key, value interface{}) bool {
1621 msr := value.(*MigrateServicesRequest)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301622 forceUpdateAllServices(cntx, msr)
Naveen Sampath04696f72022-06-13 15:19:14 +05301623 return true
1624 }
1625 msrList.Range(forceUpdateServices)
1626 return true
1627 }
1628 dev.(*VoltDevice).MigratingServices.Range(triggerForceUpdate)
1629 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301630 va.FetchAndProcessAllMigrateServicesReq(cntx, deviceID, forceUpdateAllServices)
Naveen Sampath04696f72022-06-13 15:19:14 +05301631 }
1632}
1633
1634//GetPonPortIDFromUNIPort to get pon port id from uni port
1635func GetPonPortIDFromUNIPort(uniPortID uint32) uint32 {
1636 ponPortID := (uniPortID & 0x0FF00000) >> 20
1637 return ponPortID
1638}
1639
1640//ProcessFlowModResultIndication - Processes Flow mod operation indications from controller
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301641func (va *VoltApplication) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301642
1643 d := va.GetDevice(flowStatus.Device)
1644 if d == nil {
1645 logger.Errorw(ctx, "Dropping Flow Mod Indication. Device not found", log.Fields{"Cookie": flowStatus.Cookie, "Device": flowStatus.Device})
1646 return
1647 }
1648
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301649 cookieExists := ExecuteFlowEvent(cntx, d, flowStatus.Cookie, flowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +05301650
1651 if flowStatus.Flow != nil {
1652 flowAdd := (flowStatus.FlowModType == of.CommandAdd)
1653 if !cookieExists && !isFlowStatusSuccess(flowStatus.Status, flowAdd) {
1654 pushFlowFailureNotif(flowStatus)
1655 }
1656 }
1657}
1658
1659func pushFlowFailureNotif(flowStatus intf.FlowStatus) {
1660 subFlow := flowStatus.Flow
1661 cookie := subFlow.Cookie
1662 uniPort := cookie >> 16 & 0xFFFFFFFF
1663 logger.Errorw(ctx, "Flow Failure Notification", log.Fields{"uniPort": uniPort, "Cookie": cookie})
1664/*
1665 device := flowStatus.Device
1666 priority := subFlow.Priority
1667 isIgmp := false
1668 var devSerialNum string
1669 var service *VoltService
1670
1671 if subFlow.Match.L4Protocol == of.IPProtocolIgmp {
1672 isIgmp = true
1673 } else if priority != of.HsiaFlowPriority {
1674 logger.Info(ctx, "Not HSIA flow, ignoring the failure notification")
1675 return
1676 }
1677
1678 cookie := subFlow.Cookie
1679 pbit := subFlow.Pbits
1680 uniPort := cookie >> 16 & 0xFFFFFFFF
1681 portName, _ := GetApplication().GetPortName(uint32(uniPort))
1682 portState := msgbus.PortDown
1683 logger.Errorw(ctx, "Construct Flow Failure Notification", log.Fields{"uniPort": uniPort, "Cookie": cookie, "Pbit": pbit, "isIgmp": isIgmp})
1684
1685 if isIgmp {
1686 cvlan := subFlow.TableMetadata & 0xFFFF
1687 service = GetApplication().GetMatchingMcastService(portName, device, of.VlanType(cvlan))
1688 } else {
1689 service = GetApplication().GetServiceNameFromCookie(cookie, portName, uint8(pbit), device, subFlow.TableMetadata)
1690 }
1691 var trigger infra.Reason
1692 if nil != service {
1693 logger.Errorw(ctx, "Sending Flow Failure Notification", log.Fields{"uniPort": uniPort, "Cookie": cookie, "Pbit": pbit, "Service": service.Name, "ErrorCode": flowStatus.Status})
1694 if vd := GetApplication().GetDevice(device); vd != nil {
1695 devSerialNum = vd.SerialNum
1696 if portSt, _ := GetApplication().GetPortState(service.Port); portSt == PortStateUp {
1697 portState = msgbus.PortUp
1698 }
1699 trigger = service.getSrvDeactTrigger(vd, portState)
1700 }
1701 msgbus.PostAccessConfigInd(msgbus.Failed, devSerialNum, msgbus.HSIA, service.Name, int(flowStatus.Status), subFlow.ErrorReason, trigger, portState)
1702 }
1703*/
1704}
1705
1706//UpdateMvlanProfilesForDevice to update mvlan profile for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301707func (va *VoltApplication) UpdateMvlanProfilesForDevice(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301708
1709 checkAndAddMvlanUpdateTask := func(key, value interface{}) bool {
1710 mvp := value.(*MvlanProfile)
1711 if mvp.IsUpdateInProgressForDevice(device) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301712 mvp.UpdateProfile(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301713 }
1714 return true
1715 }
1716 va.MvlanProfilesByName.Range(checkAndAddMvlanUpdateTask)
1717}
1718
1719// TaskInfo structure that is used to store the task Info.
1720type TaskInfo struct {
1721 ID string
1722 Name string
1723 Timestamp string
1724}
1725
1726// GetTaskList to get task list information.
1727func (va *VoltApplication) GetTaskList(device string) map[int]*TaskInfo {
1728 taskList := cntlr.GetController().GetTaskList(device)
1729 taskMap := make(map[int]*TaskInfo)
1730 for i, task := range taskList {
1731 taskID := strconv.Itoa(int(task.TaskID()))
1732 name := task.Name()
1733 timestamp := task.Timestamp()
1734 taskInfo := &TaskInfo{ID: taskID, Name: name, Timestamp: timestamp}
1735 taskMap[i] = taskInfo
1736 }
1737 return taskMap
1738}
1739
1740// UpdateDeviceSerialNumberList to update the device serial number list after device serial number is updated for vnet and mvlan
1741func (va *VoltApplication) UpdateDeviceSerialNumberList(oldOltSlNo string, newOltSlNo string) {
1742
1743 voltDevice := va.GetDeviceBySerialNo(oldOltSlNo)
1744
1745 if voltDevice != nil {
1746 // Device is present with old serial number ID
1747 logger.Errorw(ctx, "OLT Migration cannot be completed as there are dangling devices", log.Fields{"Serial Number": oldOltSlNo})
1748
1749 } else {
1750 logger.Infow(ctx, "No device present with old serial number", log.Fields{"Serial Number": oldOltSlNo})
1751
1752 // Add Serial Number to Blocked Devices List.
1753 cntlr.GetController().AddBlockedDevices(oldOltSlNo)
1754 cntlr.GetController().AddBlockedDevices(newOltSlNo)
1755
1756 updateSlNoForVnet := func(key, value interface{}) bool {
1757 vnet := value.(*VoltVnet)
1758 for i, deviceSlNo := range vnet.VnetConfig.DevicesList {
1759 if deviceSlNo == oldOltSlNo {
1760 vnet.VnetConfig.DevicesList[i] = newOltSlNo
1761 logger.Infow(ctx, "device serial number updated for vnet profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1762 break
1763 }
1764 }
1765 return true
1766 }
1767
1768 updateSlNoforMvlan := func(key interface{}, value interface{}) bool {
1769 mvProfile := value.(*MvlanProfile)
1770 for deviceSlNo := range mvProfile.DevicesList {
1771 if deviceSlNo == oldOltSlNo {
1772 mvProfile.DevicesList[newOltSlNo] = mvProfile.DevicesList[oldOltSlNo]
1773 delete(mvProfile.DevicesList, oldOltSlNo)
1774 logger.Infow(ctx, "device serial number updated for mvlan profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1775 break
1776 }
1777 }
1778 return true
1779 }
1780
1781 va.VnetsByName.Range(updateSlNoForVnet)
1782 va.MvlanProfilesByName.Range(updateSlNoforMvlan)
1783
1784 // Clear the serial number from Blocked Devices List
1785 cntlr.GetController().DelBlockedDevices(oldOltSlNo)
1786 cntlr.GetController().DelBlockedDevices(newOltSlNo)
1787
1788 }
1789}
1790
1791// GetVpvsForDsPkt to get vpv for downstream packets
1792func (va *VoltApplication) GetVpvsForDsPkt(cvlan of.VlanType, svlan of.VlanType, clientMAC net.HardwareAddr,
1793 pbit uint8) ([]*VoltPortVnet, error) {
1794
1795 var matchVPVs []*VoltPortVnet
1796 findVpv := func(key, value interface{}) bool {
1797 vpvs := value.([]*VoltPortVnet)
1798 for _, vpv := range vpvs {
1799 if vpv.isVlanMatching(cvlan, svlan) && vpv.MatchesPriority(pbit) != nil {
1800 var subMac net.HardwareAddr
1801 if NonZeroMacAddress(vpv.MacAddr) {
1802 subMac = vpv.MacAddr
1803 } else if vpv.LearntMacAddr != nil && NonZeroMacAddress(vpv.LearntMacAddr) {
1804 subMac = vpv.LearntMacAddr
1805 } else {
1806 matchVPVs = append(matchVPVs, vpv)
1807 continue
1808 }
1809 if util.MacAddrsMatch(subMac, clientMAC) {
1810 matchVPVs = append([]*VoltPortVnet{}, vpv)
1811 logger.Infow(ctx, "Matching VPV found", log.Fields{"Port": vpv.Port, "SVLAN": vpv.SVlan, "CVLAN": vpv.CVlan, "UNIVlan": vpv.UniVlan, "MAC": clientMAC})
1812 return false
1813 }
1814 }
1815 }
1816 return true
1817 }
1818 va.VnetsByPort.Range(findVpv)
1819
1820 if len(matchVPVs) != 1 {
1821 logger.Infow(ctx, "No matching VPV found or multiple vpvs found", log.Fields{"Match VPVs": matchVPVs, "MAC": clientMAC})
1822 return nil, errors.New("No matching VPV found or multiple vpvs found")
1823 }
1824 return matchVPVs, nil
1825}
1826
1827// GetMacInPortMap to get PORT value based on MAC key
1828func (va *VoltApplication) GetMacInPortMap(macAddr net.HardwareAddr) string {
1829 if NonZeroMacAddress(macAddr) {
1830 va.macPortLock.Lock()
1831 defer va.macPortLock.Unlock()
1832 if port, ok := va.macPortMap[macAddr.String()]; ok {
1833 logger.Debugw(ctx, "found-entry-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1834 return port
1835 }
1836 }
1837 logger.Infow(ctx, "entry-not-found-macportmap", log.Fields{"MacAddr": macAddr.String()})
1838 return ""
1839}
1840
1841// UpdateMacInPortMap to update MAC PORT (key value) information in MacPortMap
1842func (va *VoltApplication) UpdateMacInPortMap(macAddr net.HardwareAddr, port string) {
1843 if NonZeroMacAddress(macAddr) {
1844 va.macPortLock.Lock()
1845 va.macPortMap[macAddr.String()] = port
1846 va.macPortLock.Unlock()
1847 logger.Debugw(ctx, "updated-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1848 }
1849}
1850
1851// DeleteMacInPortMap to remove MAC key from MacPortMap
1852func (va *VoltApplication) DeleteMacInPortMap(macAddr net.HardwareAddr) {
1853 if NonZeroMacAddress(macAddr) {
1854 port := va.GetMacInPortMap(macAddr)
1855 va.macPortLock.Lock()
1856 delete(va.macPortMap, macAddr.String())
1857 va.macPortLock.Unlock()
1858 logger.Debugw(ctx, "deleted-from-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1859 }
1860}
1861
1862//AddGroupToPendingPool - adds the IgmpGroup with active group table entry to global pending pool
1863func (va *VoltApplication) AddGroupToPendingPool(ig *IgmpGroup) {
1864 var grpMap map[*IgmpGroup]bool
1865 var ok bool
1866
1867 va.PendingPoolLock.Lock()
1868 defer va.PendingPoolLock.Unlock()
1869
1870 logger.Infow(ctx, "Adding IgmpGroup to Global Pending Pool", log.Fields{"GroupID": ig.GroupID, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr, "PendingDevices": len(ig.Devices)})
1871 // Do Not Reset any current profile info since group table entry tied to mvlan profile
1872 // The PonVlan is part of set field in group installed
1873 // Hence, Group created is always tied to the same mvlan profile until deleted
1874
1875 for device := range ig.Devices {
1876 key := getPendingPoolKey(ig.Mvlan, device)
1877
1878 if grpMap, ok = va.IgmpPendingPool[key]; !ok {
1879 grpMap = make(map[*IgmpGroup]bool)
1880 }
1881 grpMap[ig] = true
1882
1883 //Add grpObj reference to all associated devices
1884 va.IgmpPendingPool[key] = grpMap
1885 }
1886}
1887
1888//RemoveGroupFromPendingPool - removes the group from global pending group pool
1889func (va *VoltApplication) RemoveGroupFromPendingPool(device string, ig *IgmpGroup) bool {
1890 GetApplication().PendingPoolLock.Lock()
1891 defer GetApplication().PendingPoolLock.Unlock()
1892
1893 logger.Infow(ctx, "Removing IgmpGroup from Global Pending Pool", log.Fields{"Device": device, "GroupID": ig.GroupID, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr, "PendingDevices": len(ig.Devices)})
1894
1895 key := getPendingPoolKey(ig.Mvlan, device)
1896 if _, ok := va.IgmpPendingPool[key]; ok {
1897 delete(va.IgmpPendingPool[key], ig)
1898 return true
1899 }
1900 return false
1901}
1902
1903//RemoveGroupsFromPendingPool - removes the group from global pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301904func (va *VoltApplication) RemoveGroupsFromPendingPool(cntx context.Context, device string, mvlan of.VlanType) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301905 GetApplication().PendingPoolLock.Lock()
1906 defer GetApplication().PendingPoolLock.Unlock()
1907
1908 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool for given Deivce & Mvlan", log.Fields{"Device": device, "Mvlan": mvlan.String()})
1909
1910 key := getPendingPoolKey(mvlan, device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301911 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301912}
1913
1914//RemoveGroupListFromPendingPool - removes the groups for provided key
1915// 1. Deletes the group from device
1916// 2. Delete the IgmpGroup obj and release the group ID to pool
1917// Note: Make sure to obtain PendingPoolLock lock before calling this func
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301918func (va *VoltApplication) RemoveGroupListFromPendingPool(cntx context.Context, key string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301919 if grpMap, ok := va.IgmpPendingPool[key]; ok {
1920 delete(va.IgmpPendingPool, key)
1921 for ig := range grpMap {
1922 for device := range ig.Devices {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301923 ig.DeleteIgmpGroupDevice(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301924 }
1925 }
1926 }
1927}
1928
1929//RemoveGroupDevicesFromPendingPool - removes the group from global pending group pool
1930func (va *VoltApplication) RemoveGroupDevicesFromPendingPool(ig *IgmpGroup) {
1931
1932 logger.Infow(ctx, "Removing IgmpGroup for all devices from Global Pending Pool", log.Fields{"GroupID": ig.GroupID, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr, "PendingDevices": len(ig.Devices)})
1933 for device := range ig.PendingGroupForDevice {
1934 va.RemoveGroupFromPendingPool(device, ig)
1935 }
1936}
1937
1938//GetGroupFromPendingPool - Returns IgmpGroup obj from global pending pool
1939func (va *VoltApplication) GetGroupFromPendingPool(mvlan of.VlanType, device string) *IgmpGroup {
1940
1941 var ig *IgmpGroup
1942
1943 va.PendingPoolLock.Lock()
1944 defer va.PendingPoolLock.Unlock()
1945
1946 key := getPendingPoolKey(mvlan, device)
1947 logger.Infow(ctx, "Getting IgmpGroup from Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String(), "Key": key})
1948
1949 //Gets all IgmpGrp Obj for the device
1950 grpMap, ok := va.IgmpPendingPool[key]
1951 if !ok || len(grpMap) == 0 {
1952 logger.Infow(ctx, "Matching IgmpGroup not found in Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String()})
1953 return nil
1954 }
1955
1956 //Gets a random obj from available grps
1957 for ig = range grpMap {
1958
1959 //Remove grp obj reference from all devices associated in pending pool
1960 for dev := range ig.Devices {
1961 key := getPendingPoolKey(mvlan, dev)
1962 delete(va.IgmpPendingPool[key], ig)
1963 }
1964
1965 //Safety check to avoid re-allocating group already in use
1966 if ig.NumDevicesActive() == 0 {
1967 return ig
1968 }
1969
1970 //Iteration will continue only if IG is not allocated
1971 }
1972 return nil
1973}
1974
1975//RemovePendingGroups - removes all pending groups for provided reference from global pending pool
1976// reference - mvlan/device ID
1977// isRefDevice - true - Device as reference
1978// false - Mvlan as reference
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301979func (va *VoltApplication) RemovePendingGroups(cntx context.Context, reference string, isRefDevice bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301980 va.PendingPoolLock.Lock()
1981 defer va.PendingPoolLock.Unlock()
1982
1983 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool", log.Fields{"Reference": reference, "isRefDevice": isRefDevice})
1984
1985 //Pending Pool key: "<mvlan>_<DeviceID>""
1986 paramPosition := 0
1987 if isRefDevice {
1988 paramPosition = 1
1989 }
1990
1991 // 1.Remove the Entry from pending pool
1992 // 2.Deletes the group from device
1993 // 3.Delete the IgmpGroup obj and release the group ID to pool
1994 for key := range va.IgmpPendingPool {
1995 keyParams := strings.Split(key, "_")
1996 if keyParams[paramPosition] == reference {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301997 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301998 }
1999 }
2000}
2001
2002func getPendingPoolKey(mvlan of.VlanType, device string) string {
2003 return mvlan.String() + "_" + device
2004}
2005
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302006func (va *VoltApplication) removeExpiredGroups(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302007 logger.Debug(ctx, "Check for expired Igmp Groups")
2008 removeExpiredGroups := func(key interface{}, value interface{}) bool {
2009 ig := value.(*IgmpGroup)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302010 ig.removeExpiredGroupFromDevice(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302011 return true
2012 }
2013 va.IgmpGroups.Range(removeExpiredGroups)
2014}
2015
2016//TriggerPendingProfileDeleteReq - trigger pending profile delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302017func (va *VoltApplication) TriggerPendingProfileDeleteReq(cntx context.Context, device string) {
2018 va.TriggerPendingServiceDeleteReq(cntx, device)
2019 va.TriggerPendingVpvDeleteReq(cntx, device)
2020 va.TriggerPendingVnetDeleteReq(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05302021 logger.Warnw(ctx, "All Pending Profile Delete triggered for device", log.Fields{"Device": device})
2022}
2023
2024//TriggerPendingServiceDeleteReq - trigger pending service delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302025func (va *VoltApplication) TriggerPendingServiceDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302026
2027 logger.Warnw(ctx, "Pending Services to be deleted", log.Fields{"Count": len(va.ServicesToDelete)})
2028 for serviceName := range va.ServicesToDelete {
2029 logger.Debugw(ctx, "Trigger Service Delete", log.Fields{"Service": serviceName})
2030 if vs := va.GetService(serviceName); vs != nil {
2031 if vs.Device == device {
2032 logger.Warnw(ctx, "Triggering Pending Service delete", log.Fields{"Service": vs.Name})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302033 vs.DelHsiaFlows(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302034 if vs.ForceDelete {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302035 vs.DelFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302036 /*
2037 portState := msgbus.PortDown
2038 if d, err := va.GetDeviceFromPort(vs.Port); d != nil {
2039
2040 if portSt, _ := GetApplication().GetPortState(vs.Port); portSt == PortStateUp {
2041 portState = msgbus.PortUp
2042 }
2043 indTask := cntlr.NewAddServiceIndTask(vs.Name, d.SerialNum, msgbus.DelHSIA, msgbus.Success, "", portState, infra.DelHSIAFromNB)
2044 cntlr.GetController().PostIndication(d.Name, indTask)
2045 } else {
2046 // Port Not found can occur during ONU movement. However, port delete had already handled flow deletion,
2047 // hence indication can be sent immediately
2048 var devSrNo string
2049 logger.Errorw(ctx, "Device/Port not found. Send indication directly", log.Fields{"serviceName": vs.Name, "error": err})
2050 if vd := va.GetDevice(vs.Device); vd != nil {
2051 devSrNo = vd.SerialNum
2052 }
2053 msgbus.PostAccessConfigInd(msgbus.Success, devSrNo, msgbus.DelHSIA, vs.Name, 0, "", infra.DelHSIAFromNB, portState)
2054 }*/
2055 }
2056 }
2057 } else {
2058 logger.Errorw(ctx, "Pending Service Not found", log.Fields{"Service": serviceName})
2059 }
2060 }
2061}
2062
2063//TriggerPendingVpvDeleteReq - trigger pending VPV delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302064func (va *VoltApplication) TriggerPendingVpvDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302065
2066 logger.Warnw(ctx, "Pending VPVs to be deleted", log.Fields{"Count": len(va.VoltPortVnetsToDelete)})
2067 for vpv := range va.VoltPortVnetsToDelete {
2068 if vpv.Device == device {
2069 logger.Warnw(ctx, "Triggering Pending VPv flow delete", log.Fields{"Port": vpv.Port, "Device": vpv.Device, "Vnet": vpv.VnetName})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302070 va.DelVnetFromPort(cntx, vpv.Port, vpv)
Naveen Sampath04696f72022-06-13 15:19:14 +05302071 }
2072 }
2073}
2074
2075//TriggerPendingVnetDeleteReq - trigger pending vnet delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302076func (va *VoltApplication) TriggerPendingVnetDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302077
2078 logger.Warnw(ctx, "Pending Vnets to be deleted", log.Fields{"Count": len(va.VnetsToDelete)})
2079 for vnetName := range va.VnetsToDelete {
2080 if vnetIntf, _ := va.VnetsByName.Load(vnetName); vnetIntf != nil {
2081 vnet := vnetIntf.(*VoltVnet)
2082 logger.Warnw(ctx, "Triggering Pending Vnet flows delete", log.Fields{"Vnet": vnet.Name})
2083 if d := va.GetDeviceBySerialNo(vnet.PendingDeviceToDelete); d != nil && d.SerialNum == vnet.PendingDeviceToDelete {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302084 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, vnet.PendingDeviceToDelete)
Naveen Sampath04696f72022-06-13 15:19:14 +05302085 va.deleteVnetConfig(vnet)
2086 } else {
Tinoj Joseph1d108322022-07-13 10:07:39 +05302087 logger.Warnw(ctx, "Vnet Delete Failed : Device Not Found", log.Fields{"Vnet": vnet.Name, "Device": vnet.PendingDeviceToDelete})
Naveen Sampath04696f72022-06-13 15:19:14 +05302088 }
2089 }
2090 }
2091}
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302092
2093type OltFlowService struct {
2094 EnableDhcpOnNni bool `json:"enableDhcpOnNni"`
2095 DefaultTechProfileId int `json:"defaultTechProfileId"`
2096 EnableIgmpOnNni bool `json:"enableIgmpOnNni"`
2097 EnableEapol bool `json:"enableEapol"`
2098 EnableDhcpV6 bool `json:"enableDhcpV6"`
2099 EnableDhcpV4 bool `json:"enableDhcpV4"`
2100 RemoveFlowsOnDisable bool `json:"removeFlowsOnDisable"`
2101}
2102
2103func (va *VoltApplication) UpdateOltFlowService(cntx context.Context, oltFlowService OltFlowService) {
2104 logger.Infow(ctx, "UpdateOltFlowService", log.Fields{"oldValue": va.OltFlowServiceConfig, "newValue": oltFlowService})
2105 va.OltFlowServiceConfig = oltFlowService
2106 b, err := json.Marshal(va.OltFlowServiceConfig)
2107 if err != nil {
2108 logger.Warnw(ctx, "Failed to Marshal OltFlowServiceConfig", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2109 return
2110 }
2111 _ = db.PutOltFlowService(cntx, string(b))
2112}
2113// RestoreOltFlowService to read from the DB and restore olt flow service config
2114func (va *VoltApplication) RestoreOltFlowService(cntx context.Context) {
2115 oltflowService, err := db.GetOltFlowService(cntx)
2116 if err != nil {
2117 logger.Warnw(ctx, "Failed to Get OltFlowServiceConfig from DB", log.Fields{"Error": err})
2118 return
2119 }
2120 err = json.Unmarshal([]byte(oltflowService), &va.OltFlowServiceConfig)
2121 if err != nil {
2122 logger.Warn(ctx, "Unmarshal of oltflowService failed")
2123 return
2124 }
2125 logger.Infow(ctx, "updated OltFlowServiceConfig from DB", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2126}