blob: 3e2f29910057a57a9cdab9b528e6a212bbfce57e [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
Akash Sonia8246972023-01-03 10:37:08 +053033 "voltha-go-controller/database"
Naveen Sampath04696f72022-06-13 15:19:14 +053034 "voltha-go-controller/internal/pkg/controller"
35 cntlr "voltha-go-controller/internal/pkg/controller"
Akash Sonia8246972023-01-03 10:37:08 +053036 errorCodes "voltha-go-controller/internal/pkg/errorcodes"
Naveen Sampath04696f72022-06-13 15:19:14 +053037 "voltha-go-controller/internal/pkg/intf"
38 "voltha-go-controller/internal/pkg/of"
39 "voltha-go-controller/internal/pkg/tasks"
40 "voltha-go-controller/internal/pkg/util"
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 {
Naveen Sampath04696f72022-06-13 15:19:14 +0530149 Name string
150 Device string
151 PonPort uint32
vinokuma926cb3e2023-03-29 11:41:06 +0530152 ActiveChannels uint32
153 ID uint32
Naveen Sampath04696f72022-06-13 15:19:14 +0530154 Type VoltPortType
155 State PortState
Naveen Sampath04696f72022-06-13 15:19:14 +0530156 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
vinokuma926cb3e2023-03-29 11:41:06 +0530194// have any relation to the physical device
Naveen Sampath04696f72022-06-13 15:19:14 +0530195// SerialNum: This is the serial number of the device and can be used to
vinokuma926cb3e2023-03-29 11:41:06 +0530196// correlate the devices
Naveen Sampath04696f72022-06-13 15:19:14 +0530197// NniPort: The identity of the NNI port
198// Ports: List of all ports added to the device
199type VoltDevice struct {
Naveen Sampath04696f72022-06-13 15:19:14 +0530200 FlowAddEventMap *util.ConcurrentMap //map[string]*FlowEvent
201 FlowDelEventMap *util.ConcurrentMap //map[string]*FlowEvent
202 MigratingServices *util.ConcurrentMap //<vnetID,<RequestID, MigrateServicesRequest>>
vinokuma926cb3e2023-03-29 11:41:06 +0530203 VpvsBySvlan *util.ConcurrentMap // map[svlan]map[vnet_port]*VoltPortVnet
204 ConfiguredVlanForDeviceFlows *util.ConcurrentMap //map[string]map[string]bool
205 IgmpDsFlowAppliedForMvlan map[uint16]bool
206 State controller.DeviceState
207 SouthBoundID string
208 NniPort string
209 Name string
210 SerialNum string
211 Ports sync.Map
212 VlanPortStatus sync.Map
213 ActiveChannelsPerPon sync.Map // [PonPortID]*PonPortCfg
214 PonPortList sync.Map // [PonPortID]map[string]string
215 ActiveChannelCountLock sync.Mutex // This lock is used to update ActiveIGMPChannels
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530216 NniDhcpTrapVid of.VlanType
vinokuma926cb3e2023-03-29 11:41:06 +0530217 GlobalDhcpFlowAdded bool
218 icmpv6GroupAdded bool
Naveen Sampath04696f72022-06-13 15:19:14 +0530219}
220
221// NewVoltDevice : Constructor for the device
222func NewVoltDevice(name string, slno, southBoundID string) *VoltDevice {
223 var d VoltDevice
224 d.Name = name
225 d.SouthBoundID = southBoundID
226 d.State = controller.DeviceStateDOWN
227 d.NniPort = ""
228 d.SouthBoundID = southBoundID
229 d.SerialNum = slno
230 d.icmpv6GroupAdded = false
231 d.IgmpDsFlowAppliedForMvlan = make(map[uint16]bool)
232 d.ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
233 d.MigratingServices = util.NewConcurrentMap()
234 d.VpvsBySvlan = util.NewConcurrentMap()
235 d.FlowAddEventMap = util.NewConcurrentMap()
236 d.FlowDelEventMap = util.NewConcurrentMap()
237 d.GlobalDhcpFlowAdded = false
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530238 if config, ok := GetApplication().DevicesConfig.Load(slno); ok {
239 //Update nni dhcp vid
Akash Soni53da2852023-03-15 00:31:31 +0530240 deviceConfig := config.(*DeviceConfig)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530241 d.NniDhcpTrapVid = of.VlanType(deviceConfig.NniDhcpTrapVid)
242 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530243 return &d
244}
245
vinokuma926cb3e2023-03-29 11:41:06 +0530246// GetAssociatedVpvsForDevice - return the associated VPVs for given device & svlan
Naveen Sampath04696f72022-06-13 15:19:14 +0530247func (va *VoltApplication) GetAssociatedVpvsForDevice(device string, svlan of.VlanType) *util.ConcurrentMap {
248 if d := va.GetDevice(device); d != nil {
249 return d.GetAssociatedVpvs(svlan)
250 }
251 return nil
252}
253
vinokuma926cb3e2023-03-29 11:41:06 +0530254// AssociateVpvsToDevice - updates the associated VPVs for given device & svlan
Naveen Sampath04696f72022-06-13 15:19:14 +0530255func (va *VoltApplication) AssociateVpvsToDevice(device string, vpv *VoltPortVnet) {
256 if d := va.GetDevice(device); d != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530257 vpvMap := d.GetAssociatedVpvs(vpv.SVlan)
258 vpvMap.Set(vpv, true)
259 d.VpvsBySvlan.Set(vpv.SVlan, vpvMap)
260 logger.Infow(ctx, "VPVMap: SET", log.Fields{"Map": vpvMap.Length()})
261 return
262 }
263 logger.Errorw(ctx, "Set VPVMap failed: Device Not Found", log.Fields{"Svlan": vpv.SVlan, "Device": device})
264}
265
vinokuma926cb3e2023-03-29 11:41:06 +0530266// DisassociateVpvsFromDevice - disassociated VPVs from given device & svlan
Naveen Sampath04696f72022-06-13 15:19:14 +0530267func (va *VoltApplication) DisassociateVpvsFromDevice(device string, vpv *VoltPortVnet) {
268 if d := va.GetDevice(device); d != nil {
269 vpvMap := d.GetAssociatedVpvs(vpv.SVlan)
270 vpvMap.Remove(vpv)
271 d.VpvsBySvlan.Set(vpv.SVlan, vpvMap)
272 logger.Infow(ctx, "VPVMap: Remove", log.Fields{"Map": vpvMap.Length()})
273 return
274 }
275 logger.Errorw(ctx, "Remove VPVMap failed: Device Not Found", log.Fields{"Svlan": vpv.SVlan, "Device": device})
276}
277
vinokuma926cb3e2023-03-29 11:41:06 +0530278// GetAssociatedVpvs - returns the associated VPVs for the given Svlan
Naveen Sampath04696f72022-06-13 15:19:14 +0530279func (d *VoltDevice) GetAssociatedVpvs(svlan of.VlanType) *util.ConcurrentMap {
Naveen Sampath04696f72022-06-13 15:19:14 +0530280 var vpvMap *util.ConcurrentMap
281 var mapIntf interface{}
282 var ok bool
283
284 if mapIntf, ok = d.VpvsBySvlan.Get(svlan); ok {
285 vpvMap = mapIntf.(*util.ConcurrentMap)
286 } else {
287 vpvMap = util.NewConcurrentMap()
288 }
289 logger.Infow(ctx, "VPVMap: GET", log.Fields{"Map": vpvMap.Length()})
290 return vpvMap
291}
292
293// AddPort add port to the device.
294func (d *VoltDevice) AddPort(port string, id uint32) *VoltPort {
295 addPonPortFromUniPort := func(vPort *VoltPort) {
296 if vPort.Type == VoltPortTypeAccess {
297 ponPortID := GetPonPortIDFromUNIPort(vPort.ID)
298
299 if ponPortUniList, ok := d.PonPortList.Load(ponPortID); !ok {
300 uniList := make(map[string]uint32)
301 uniList[port] = vPort.ID
302 d.PonPortList.Store(ponPortID, uniList)
303 } else {
304 ponPortUniList.(map[string]uint32)[port] = vPort.ID
305 d.PonPortList.Store(ponPortID, ponPortUniList)
306 }
307 }
308 }
309 va := GetApplication()
310 if pIntf, ok := d.Ports.Load(port); ok {
311 voltPort := pIntf.(*VoltPort)
312 addPonPortFromUniPort(voltPort)
313 va.AggActiveChannelsCountPerSub(d.Name, port, voltPort)
314 d.Ports.Store(port, voltPort)
315 return voltPort
316 }
317 p := NewVoltPort(d.Name, port, id)
318 va.AggActiveChannelsCountPerSub(d.Name, port, p)
319 d.Ports.Store(port, p)
320 if util.IsNniPort(id) {
321 d.NniPort = port
322 }
323 addPonPortFromUniPort(p)
324 return p
325}
326
327// GetPort to get port information from the device.
328func (d *VoltDevice) GetPort(port string) *VoltPort {
329 if pIntf, ok := d.Ports.Load(port); ok {
330 return pIntf.(*VoltPort)
331 }
332 return nil
333}
334
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530335// GetPortByPortID to get port information from the device.
336func (d *VoltDevice) GetPortNameFromPortID(portID uint32) string {
337 portName := ""
338 d.Ports.Range(func(key, value interface{}) bool {
339 vp := value.(*VoltPort)
340 if vp.ID == portID {
341 portName = vp.Name
342 }
343 return true
344 })
345 return portName
346}
347
Naveen Sampath04696f72022-06-13 15:19:14 +0530348// DelPort to delete port from the device
349func (d *VoltDevice) DelPort(port string) {
350 if _, ok := d.Ports.Load(port); ok {
351 d.Ports.Delete(port)
352 } else {
353 logger.Warnw(ctx, "Port doesn't exist", log.Fields{"Device": d.Name, "Port": port})
354 }
355}
356
357// pushFlowsForUnis to send port-up-indication for uni ports.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530358func (d *VoltDevice) pushFlowsForUnis(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530359 logger.Info(ctx, "NNI Discovered, Sending Port UP Ind for UNIs")
360 d.Ports.Range(func(key, value interface{}) bool {
361 port := key.(string)
362 vp := value.(*VoltPort)
363
Akash Sonia8246972023-01-03 10:37:08 +0530364 logger.Infow(ctx, "NNI Discovered. Sending Port UP Ind for UNI", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530365 //Ignore if UNI port is not UP
366 if vp.State != PortStateUp {
367 return true
368 }
369
370 //Obtain all VPVs associated with the port
371 vnets, ok := GetApplication().VnetsByPort.Load(port)
372 if !ok {
373 return true
374 }
375
376 for _, vpv := range vnets.([]*VoltPortVnet) {
377 vpv.VpvLock.Lock()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530378 vpv.PortUpInd(cntx, d, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530379 vpv.VpvLock.Unlock()
Naveen Sampath04696f72022-06-13 15:19:14 +0530380 }
381 return true
382 })
383}
384
385// ----------------------------------------------------------
386// VOLT Application - hosts all other objects
387// ----------------------------------------------------------
388//
389// The VOLT application is a singleton implementation where
390// there is just one instance in the system and is the gateway
391// to all other components within the controller
392// The declaration of the singleton object
393var vapplication *VoltApplication
394
395// VoltApplication fields :
396// ServiceByName - Stores the services by the name as key
vinokuma926cb3e2023-03-29 11:41:06 +0530397// A record of NB configuration.
Naveen Sampath04696f72022-06-13 15:19:14 +0530398// VnetsByPort - Stores the VNETs by the ports configured
vinokuma926cb3e2023-03-29 11:41:06 +0530399// from NB. A record of NB configuration.
Naveen Sampath04696f72022-06-13 15:19:14 +0530400// VnetsByTag - Stores the VNETs by the VLANS configured
vinokuma926cb3e2023-03-29 11:41:06 +0530401// from NB. A record of NB configuration.
Naveen Sampath04696f72022-06-13 15:19:14 +0530402// VnetsByName - Stores the VNETs by the name configured
vinokuma926cb3e2023-03-29 11:41:06 +0530403// from NB. A record of NB configuration.
Naveen Sampath04696f72022-06-13 15:19:14 +0530404// DevicesDisc - Stores the devices discovered from SB.
vinokuma926cb3e2023-03-29 11:41:06 +0530405// Should be updated only by events from SB
Naveen Sampath04696f72022-06-13 15:19:14 +0530406// PortsDisc - Stores the ports discovered from SB.
vinokuma926cb3e2023-03-29 11:41:06 +0530407// Should be updated only by events from SB
Naveen Sampath04696f72022-06-13 15:19:14 +0530408type VoltApplication struct {
Naveen Sampath04696f72022-06-13 15:19:14 +0530409 MeterMgr
vinokuma926cb3e2023-03-29 11:41:06 +0530410 DataMigrationInfo DataMigration
411 VnetsBySvlan *util.ConcurrentMap
412 IgmpGroupIds []*IgmpGroup
413 VoltPortVnetsToDelete map[*VoltPortVnet]bool
Akash Sonia8246972023-01-03 10:37:08 +0530414 IgmpPendingPool map[string]map[*IgmpGroup]bool //[grpkey, map[groupObj]bool] //mvlan_grpName/IP
vinokuma926cb3e2023-03-29 11:41:06 +0530415 macPortMap map[string]string
Akash Sonia8246972023-01-03 10:37:08 +0530416 VnetsToDelete map[string]bool
417 ServicesToDelete map[string]bool
Hitesh Chhabra64be2442023-06-21 17:06:34 +0530418 ServicesToDeactivate map[string]bool
Akash Sonia8246972023-01-03 10:37:08 +0530419 PortAlarmProfileCache map[string]map[string]int // [portAlarmID][ThresholdLevelString]ThresholdLevel
420 vendorID string
vinokuma926cb3e2023-03-29 11:41:06 +0530421 ServiceByName sync.Map // [serName]*VoltService
422 VnetsByPort sync.Map // [portName][]*VoltPortVnet
423 VnetsByTag sync.Map // [svlan-cvlan-uvlan]*VoltVnet
424 VnetsByName sync.Map // [vnetName]*VoltVnet
425 DevicesDisc sync.Map
426 PortsDisc sync.Map
427 IgmpGroups sync.Map // [grpKey]*IgmpGroup
428 MvlanProfilesByTag sync.Map
429 MvlanProfilesByName sync.Map
430 Icmpv6Receivers sync.Map
431 DeviceCounters sync.Map //[logicalDeviceId]*DeviceCounters
432 ServiceCounters sync.Map //[serviceName]*ServiceCounters
433 NbDevice sync.Map // [OLTSouthBoundID]*NbDevice
434 OltIgmpInfoBySerial sync.Map
435 McastConfigMap sync.Map //[OltSerialNo_MvlanProfileID]*McastConfig
Akash Sonia8246972023-01-03 10:37:08 +0530436 DevicesConfig sync.Map //[serialNumber]*DeviceConfig
vinokuma926cb3e2023-03-29 11:41:06 +0530437 IgmpProfilesByName sync.Map
438 IgmpTasks tasks.Tasks
439 IndicationsTasks tasks.Tasks
440 MulticastAlarmTasks tasks.Tasks
441 IgmpKPIsTasks tasks.Tasks
442 pppoeTasks tasks.Tasks
443 OltFlowServiceConfig OltFlowService
444 PendingPoolLock sync.RWMutex
445 // MacAddress-Port MAP to avoid swap of mac across ports.
446 macPortLock sync.RWMutex
447 portLock sync.Mutex
Akash Sonia8246972023-01-03 10:37:08 +0530448}
Naveen Sampath04696f72022-06-13 15:19:14 +0530449
Akash Sonia8246972023-01-03 10:37:08 +0530450type DeviceConfig struct {
451 SerialNumber string `json:"id"`
452 HardwareIdentifier string `json:"hardwareIdentifier"`
Akash Soni87a19072023-02-28 00:46:59 +0530453 IPAddress string `json:"ipAddress"`
Akash Soni53da2852023-03-15 00:31:31 +0530454 UplinkPort string `json:"uplinkPort"`
Akash Sonia8246972023-01-03 10:37:08 +0530455 NasID string `json:"nasId"`
456 NniDhcpTrapVid int `json:"nniDhcpTrapVid"`
Naveen Sampath04696f72022-06-13 15:19:14 +0530457}
458
459// PonPortCfg contains NB port config and activeIGMPChannels count
460type PonPortCfg struct {
vinokuma926cb3e2023-03-29 11:41:06 +0530461 PortAlarmProfileID string
Naveen Sampath04696f72022-06-13 15:19:14 +0530462 PortID uint32
463 MaxActiveChannels uint32
464 ActiveIGMPChannels uint32
465 EnableMulticastKPI bool
Naveen Sampath04696f72022-06-13 15:19:14 +0530466}
467
468// NbDevice OLT Device info
469type NbDevice struct {
470 SouthBoundID string
471 PonPorts sync.Map // [PortID]*PonPortCfg
472}
473
474// RestoreNbDeviceFromDb restores the NB Device in case of VGC pod restart.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530475func (va *VoltApplication) RestoreNbDeviceFromDb(cntx context.Context, deviceID string) *NbDevice {
Naveen Sampath04696f72022-06-13 15:19:14 +0530476 nbDevice := NewNbDevice()
477 nbDevice.SouthBoundID = deviceID
478
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530479 nbPorts, _ := db.GetAllNbPorts(cntx, deviceID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530480
481 for key, p := range nbPorts {
482 b, ok := p.Value.([]byte)
483 if !ok {
484 logger.Warn(ctx, "The value type is not []byte")
485 continue
486 }
487 var port PonPortCfg
488 err := json.Unmarshal(b, &port)
489 if err != nil {
490 logger.Warn(ctx, "Unmarshal of PonPortCfg failed")
491 continue
492 }
493 logger.Debugw(ctx, "Port recovered", log.Fields{"port": port})
494 ponPortID, _ := strconv.Atoi(key)
495 nbDevice.PonPorts.Store(uint32(ponPortID), &port)
496 }
497 va.NbDevice.Store(deviceID, nbDevice)
498 return nbDevice
499}
500
501// NewNbDevice Constructor for NbDevice
502func NewNbDevice() *NbDevice {
503 var nbDevice NbDevice
504 return &nbDevice
505}
506
507// WriteToDb writes nb device port config to kv store
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530508func (nbd *NbDevice) WriteToDb(cntx context.Context, portID uint32, ponPort *PonPortCfg) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530509 b, err := json.Marshal(ponPort)
510 if err != nil {
511 logger.Errorw(ctx, "PonPortConfig-marshal-failed", log.Fields{"err": err})
512 return
513 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530514 db.PutNbDevicePort(cntx, nbd.SouthBoundID, portID, string(b))
Naveen Sampath04696f72022-06-13 15:19:14 +0530515}
516
517// AddPortToNbDevice Adds pon port to NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530518func (nbd *NbDevice) AddPortToNbDevice(cntx context.Context, portID, allowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530519 enableMulticastKPI bool, portAlarmProfileID string) *PonPortCfg {
Naveen Sampath04696f72022-06-13 15:19:14 +0530520 ponPort := &PonPortCfg{
521 PortID: portID,
522 MaxActiveChannels: allowedChannels,
523 EnableMulticastKPI: enableMulticastKPI,
524 PortAlarmProfileID: portAlarmProfileID,
525 }
526 nbd.PonPorts.Store(portID, ponPort)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530527 nbd.WriteToDb(cntx, portID, ponPort)
Naveen Sampath04696f72022-06-13 15:19:14 +0530528 return ponPort
529}
530
Akash Sonia8246972023-01-03 10:37:08 +0530531// RestoreDeviceConfigFromDb to restore vnet from port
532func (va *VoltApplication) RestoreDeviceConfigFromDb(cntx context.Context) {
533 // VNETS must be learnt first
534 dConfig, _ := db.GetDeviceConfig(cntx)
535 for _, device := range dConfig {
536 b, ok := device.Value.([]byte)
537 if !ok {
538 logger.Warn(ctx, "The value type is not []byte")
539 continue
540 }
541 devConfig := DeviceConfig{}
542 err := json.Unmarshal(b, &devConfig)
543 if err != nil {
544 logger.Warn(ctx, "Unmarshal of device configuration failed")
545 continue
546 }
547 logger.Debugw(ctx, "Retrieved device config", log.Fields{"Device Config": devConfig})
548 if err := va.AddDeviceConfig(cntx, devConfig.SerialNumber, devConfig.HardwareIdentifier, devConfig.NasID, devConfig.IPAddress, devConfig.UplinkPort, devConfig.NniDhcpTrapVid); err != nil {
549 logger.Warnw(ctx, "Add device config failed", log.Fields{"DeviceConfig": devConfig, "Error": err})
550 }
Akash Sonia8246972023-01-03 10:37:08 +0530551 }
552}
553
554// WriteDeviceConfigToDb writes sb device config to kv store
555func (dc *DeviceConfig) WriteDeviceConfigToDb(cntx context.Context, serialNum string, deviceConfig *DeviceConfig) error {
556 b, err := json.Marshal(deviceConfig)
557 if err != nil {
558 logger.Errorw(ctx, "deviceConfig-marshal-failed", log.Fields{"err": err})
559 return err
560 }
561 dberr := db.PutDeviceConfig(cntx, serialNum, string(b))
562 if dberr != nil {
563 logger.Errorw(ctx, "update device config failed", log.Fields{"err": err})
564 return dberr
565 }
566 return nil
567}
568
vinokuma926cb3e2023-03-29 11:41:06 +0530569func (va *VoltApplication) AddDeviceConfig(cntx context.Context, serialNum, hardwareIdentifier, nasID, ipAddress, uplinkPort string, nniDhcpTrapID int) error {
Akash Sonia8246972023-01-03 10:37:08 +0530570 var dc *DeviceConfig
571
Akash Soni87a19072023-02-28 00:46:59 +0530572 deviceConfig := &DeviceConfig{
573 SerialNumber: serialNum,
574 HardwareIdentifier: hardwareIdentifier,
575 NasID: nasID,
576 UplinkPort: uplinkPort,
577 IPAddress: ipAddress,
vinokuma926cb3e2023-03-29 11:41:06 +0530578 NniDhcpTrapVid: nniDhcpTrapID,
Akash Sonia8246972023-01-03 10:37:08 +0530579 }
Akash Soni87a19072023-02-28 00:46:59 +0530580 va.DevicesConfig.Store(serialNum, deviceConfig)
581 err := dc.WriteDeviceConfigToDb(cntx, serialNum, deviceConfig)
582 if err != nil {
583 logger.Errorw(ctx, "DB update for device config failed", log.Fields{"err": err})
584 return err
585 }
586
587 // If device is already discovered update the VoltDevice structure
588 device, id := va.GetDeviceBySerialNo(serialNum)
589 if device != nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530590 device.NniDhcpTrapVid = of.VlanType(nniDhcpTrapID)
Akash Soni87a19072023-02-28 00:46:59 +0530591 va.DevicesDisc.Store(id, device)
592 }
593
Akash Sonia8246972023-01-03 10:37:08 +0530594 return nil
595}
596
597// GetDeviceConfig to get a device config.
598func (va *VoltApplication) GetDeviceConfig(serNum string) *DeviceConfig {
599 if d, ok := va.DevicesConfig.Load(serNum); ok {
600 return d.(*DeviceConfig)
601 }
602 return nil
603}
604
Naveen Sampath04696f72022-06-13 15:19:14 +0530605// UpdatePortToNbDevice Adds pon port to NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530606func (nbd *NbDevice) UpdatePortToNbDevice(cntx context.Context, portID, allowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) *PonPortCfg {
Naveen Sampath04696f72022-06-13 15:19:14 +0530607 p, exists := nbd.PonPorts.Load(portID)
608 if !exists {
609 logger.Errorw(ctx, "PON port not exists in nb-device", log.Fields{"portID": portID})
610 return nil
611 }
612 port := p.(*PonPortCfg)
613 if allowedChannels != 0 {
614 port.MaxActiveChannels = allowedChannels
615 port.EnableMulticastKPI = enableMulticastKPI
616 port.PortAlarmProfileID = portAlarmProfileID
617 }
618
619 nbd.PonPorts.Store(portID, port)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530620 nbd.WriteToDb(cntx, portID, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530621 return port
622}
623
624// DeletePortFromNbDevice Deletes pon port from NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530625func (nbd *NbDevice) DeletePortFromNbDevice(cntx context.Context, portID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530626 if _, ok := nbd.PonPorts.Load(portID); ok {
627 nbd.PonPorts.Delete(portID)
628 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530629 db.DelNbDevicePort(cntx, nbd.SouthBoundID, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530630}
631
632// GetApplication : Interface to access the singleton object
633func GetApplication() *VoltApplication {
634 if vapplication == nil {
635 vapplication = newVoltApplication()
636 }
637 return vapplication
638}
639
640// newVoltApplication : Constructor for the singleton object. Hence this is not
641// an exported function
642func newVoltApplication() *VoltApplication {
643 var va VoltApplication
644 va.IgmpTasks.Initialize(context.TODO())
645 va.MulticastAlarmTasks.Initialize(context.TODO())
646 va.IgmpKPIsTasks.Initialize(context.TODO())
647 va.pppoeTasks.Initialize(context.TODO())
648 va.storeIgmpProfileMap(DefaultIgmpProfID, newDefaultIgmpProfile())
649 va.MeterMgr.Init()
650 va.AddIgmpGroups(5000)
651 va.macPortMap = make(map[string]string)
652 va.IgmpPendingPool = make(map[string]map[*IgmpGroup]bool)
653 va.VnetsBySvlan = util.NewConcurrentMap()
654 va.VnetsToDelete = make(map[string]bool)
655 va.ServicesToDelete = make(map[string]bool)
Hitesh Chhabra64be2442023-06-21 17:06:34 +0530656 va.ServicesToDeactivate = make(map[string]bool)
Naveen Sampath04696f72022-06-13 15:19:14 +0530657 va.VoltPortVnetsToDelete = make(map[*VoltPortVnet]bool)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530658 go va.Start(context.Background(), TimerCfg{tick: 100 * time.Millisecond}, tickTimer)
659 go va.Start(context.Background(), TimerCfg{tick: time.Duration(GroupExpiryTime) * time.Minute}, pendingPoolTimer)
Naveen Sampath04696f72022-06-13 15:19:14 +0530660 InitEventFuncMapper()
661 db = database.GetDatabase()
662 return &va
663}
664
vinokuma926cb3e2023-03-29 11:41:06 +0530665// GetFlowEventRegister - returs the register based on flow mod type
Naveen Sampath04696f72022-06-13 15:19:14 +0530666func (d *VoltDevice) GetFlowEventRegister(flowModType of.Command) (*util.ConcurrentMap, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530667 switch flowModType {
668 case of.CommandDel:
669 return d.FlowDelEventMap, nil
670 case of.CommandAdd:
671 return d.FlowAddEventMap, nil
672 default:
673 logger.Error(ctx, "Unknown Flow Mod received")
674 }
675 return util.NewConcurrentMap(), errors.New("Unknown Flow Mod")
676}
677
678// RegisterFlowAddEvent to register a flow event.
679func (d *VoltDevice) RegisterFlowAddEvent(cookie string, event *FlowEvent) {
680 logger.Debugw(ctx, "Registered Flow Add Event", log.Fields{"Cookie": cookie, "Event": event})
681 d.FlowAddEventMap.MapLock.Lock()
682 defer d.FlowAddEventMap.MapLock.Unlock()
683 d.FlowAddEventMap.Set(cookie, event)
684}
685
686// RegisterFlowDelEvent to register a flow event.
687func (d *VoltDevice) RegisterFlowDelEvent(cookie string, event *FlowEvent) {
688 logger.Debugw(ctx, "Registered Flow Del Event", log.Fields{"Cookie": cookie, "Event": event})
689 d.FlowDelEventMap.MapLock.Lock()
690 defer d.FlowDelEventMap.MapLock.Unlock()
691 d.FlowDelEventMap.Set(cookie, event)
692}
693
694// UnRegisterFlowEvent to unregister a flow event.
695func (d *VoltDevice) UnRegisterFlowEvent(cookie string, flowModType of.Command) {
696 logger.Debugw(ctx, "UnRegistered Flow Add Event", log.Fields{"Cookie": cookie, "Type": flowModType})
697 flowEventMap, err := d.GetFlowEventRegister(flowModType)
698 if err != nil {
699 logger.Debugw(ctx, "Flow event map does not exists", log.Fields{"flowMod": flowModType, "Error": err})
700 return
701 }
702 flowEventMap.MapLock.Lock()
703 defer flowEventMap.MapLock.Unlock()
704 flowEventMap.Remove(cookie)
705}
706
707// AddIgmpGroups to add Igmp groups.
708func (va *VoltApplication) AddIgmpGroups(numOfGroups uint32) {
709 //TODO: Temp change to resolve group id issue in pOLT
710 //for i := 1; uint32(i) <= numOfGroups; i++ {
711 for i := 2; uint32(i) <= (numOfGroups + 1); i++ {
712 ig := IgmpGroup{}
713 ig.GroupID = uint32(i)
714 va.IgmpGroupIds = append(va.IgmpGroupIds, &ig)
715 }
716}
717
718// GetAvailIgmpGroupID to get id of available igmp group.
719func (va *VoltApplication) GetAvailIgmpGroupID() *IgmpGroup {
720 var ig *IgmpGroup
721 if len(va.IgmpGroupIds) > 0 {
722 ig, va.IgmpGroupIds = va.IgmpGroupIds[0], va.IgmpGroupIds[1:]
723 return ig
724 }
725 return nil
726}
727
728// GetIgmpGroupID to get id of igmp group.
729func (va *VoltApplication) GetIgmpGroupID(gid uint32) (*IgmpGroup, error) {
730 for id, ig := range va.IgmpGroupIds {
731 if ig.GroupID == gid {
732 va.IgmpGroupIds = append(va.IgmpGroupIds[0:id], va.IgmpGroupIds[id+1:]...)
733 return ig, nil
734 }
735 }
736 return nil, errors.New("Group Id Missing")
737}
738
739// PutIgmpGroupID to add id of igmp group.
740func (va *VoltApplication) PutIgmpGroupID(ig *IgmpGroup) {
741 va.IgmpGroupIds = append([]*IgmpGroup{ig}, va.IgmpGroupIds[0:]...)
742}
743
vinokuma926cb3e2023-03-29 11:41:06 +0530744// RestoreUpgradeStatus - gets upgrade/migration status from DB and updates local flags
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530745func (va *VoltApplication) RestoreUpgradeStatus(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530746 Migrate := new(DataMigration)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530747 if err := GetMigrationInfo(cntx, Migrate); err == nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530748 if Migrate.Status == MigrationInProgress {
749 isUpgradeComplete = false
750 return
751 }
752 }
753 isUpgradeComplete = true
754
755 logger.Infow(ctx, "Upgrade Status Restored", log.Fields{"Upgrade Completed": isUpgradeComplete})
756}
757
758// ReadAllFromDb : If we are restarted, learn from the database the current execution
759// stage
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530760func (va *VoltApplication) ReadAllFromDb(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530761 logger.Info(ctx, "Reading the meters from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530762 va.RestoreMetersFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530763 logger.Info(ctx, "Reading the VNETs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530764 va.RestoreVnetsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530765 logger.Info(ctx, "Reading the VPVs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530766 va.RestoreVpvsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530767 logger.Info(ctx, "Reading the Services from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530768 va.RestoreSvcsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530769 logger.Info(ctx, "Reading the MVLANs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530770 va.RestoreMvlansFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530771 logger.Info(ctx, "Reading the IGMP profiles from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530772 va.RestoreIGMPProfilesFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530773 logger.Info(ctx, "Reading the Mcast configs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530774 va.RestoreMcastConfigsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530775 logger.Info(ctx, "Reading the IGMP groups for DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530776 va.RestoreIgmpGroupsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530777 logger.Info(ctx, "Reading Upgrade status from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530778 va.RestoreUpgradeStatus(cntx)
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530779 logger.Info(ctx, "Reading OltFlowService from DB")
780 va.RestoreOltFlowService(cntx)
Akash Sonia8246972023-01-03 10:37:08 +0530781 logger.Info(ctx, "Reading device config from DB")
782 va.RestoreDeviceConfigFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530783 logger.Info(ctx, "Reconciled from DB")
784}
785
vinokuma926cb3e2023-03-29 11:41:06 +0530786// InitStaticConfig to initialize static config.
Naveen Sampath04696f72022-06-13 15:19:14 +0530787func (va *VoltApplication) InitStaticConfig() {
788 va.InitIgmpSrcMac()
789}
790
791// SetVendorID to set vendor id
792func (va *VoltApplication) SetVendorID(vendorID string) {
793 va.vendorID = vendorID
794}
795
796// GetVendorID to get vendor id
797func (va *VoltApplication) GetVendorID() string {
798 return va.vendorID
799}
800
801// SetRebootFlag to set reboot flag
802func (va *VoltApplication) SetRebootFlag(flag bool) {
803 vgcRebooted = flag
804}
805
806// GetUpgradeFlag to get reboot status
807func (va *VoltApplication) GetUpgradeFlag() bool {
808 return isUpgradeComplete
809}
810
811// SetUpgradeFlag to set reboot status
812func (va *VoltApplication) SetUpgradeFlag(flag bool) {
813 isUpgradeComplete = flag
814}
815
816// ------------------------------------------------------------
817// Device related functions
818
819// AddDevice : Add a device and typically the device stores the NNI port on the device
820// The NNI port is used when the packets are emitted towards the network.
821// The outport is selected as the NNI port of the device. Today, we support
822// a single NNI port per OLT. This is true whether the network uses any
823// protection mechanism (LAG, ERPS, etc.). The aggregate of the such protection
824// is represented by a single NNI port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530825func (va *VoltApplication) AddDevice(cntx context.Context, device string, slno, southBoundID string) {
Akash Soni53da2852023-03-15 00:31:31 +0530826 logger.Warnw(ctx, "Received Device Ind: Add", log.Fields{"Device": device, "SrNo": slno, "southBoundID": southBoundID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530827 if _, ok := va.DevicesDisc.Load(device); ok {
828 logger.Warnw(ctx, "Device Exists", log.Fields{"Device": device})
829 }
830 d := NewVoltDevice(device, slno, southBoundID)
831
832 addPort := func(key, value interface{}) bool {
833 portID := key.(uint32)
834 port := value.(*PonPortCfg)
835 va.AggActiveChannelsCountForPonPort(device, portID, port)
836 d.ActiveChannelsPerPon.Store(portID, port)
837 return true
838 }
839 if nbDevice, exists := va.NbDevice.Load(southBoundID); exists {
840 // Pon Ports added before OLT activate.
841 nbDevice.(*NbDevice).PonPorts.Range(addPort)
842 } else {
843 // Check if NbPort exists in DB. VGC restart case.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530844 nbd := va.RestoreNbDeviceFromDb(cntx, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530845 nbd.PonPorts.Range(addPort)
846 }
847 va.DevicesDisc.Store(device, d)
848}
849
850// GetDevice to get a device.
851func (va *VoltApplication) GetDevice(device string) *VoltDevice {
852 if d, ok := va.DevicesDisc.Load(device); ok {
853 return d.(*VoltDevice)
854 }
855 return nil
856}
857
858// DelDevice to delete a device.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530859func (va *VoltApplication) DelDevice(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530860 logger.Warnw(ctx, "Received Device Ind: Delete", log.Fields{"Device": device})
861 if vdIntf, ok := va.DevicesDisc.Load(device); ok {
862 vd := vdIntf.(*VoltDevice)
863 va.DevicesDisc.Delete(device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530864 _ = db.DelAllRoutesForDevice(cntx, device)
865 va.HandleFlowClearFlag(cntx, device, vd.SerialNum, vd.SouthBoundID)
866 _ = db.DelAllGroup(cntx, device)
867 _ = db.DelAllMeter(cntx, device)
868 _ = db.DelAllPorts(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530869 logger.Debugw(ctx, "Device deleted", log.Fields{"Device": device})
870 } else {
871 logger.Warnw(ctx, "Device Doesn't Exist", log.Fields{"Device": device})
872 }
873}
874
875// GetDeviceBySerialNo to get a device by serial number.
876// TODO - Transform this into a MAP instead
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530877func (va *VoltApplication) GetDeviceBySerialNo(slno string) (*VoltDevice, string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530878 var device *VoltDevice
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530879 var deviceID string
Naveen Sampath04696f72022-06-13 15:19:14 +0530880 getserial := func(key interface{}, value interface{}) bool {
881 device = value.(*VoltDevice)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530882 deviceID = key.(string)
Naveen Sampath04696f72022-06-13 15:19:14 +0530883 return device.SerialNum != slno
884 }
885 va.DevicesDisc.Range(getserial)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530886 return device, deviceID
Naveen Sampath04696f72022-06-13 15:19:14 +0530887}
888
889// PortAddInd : This is a PORT add indication coming from the VPAgent, which is essentially
890// a request coming from VOLTHA. The device and identity of the port is provided
891// in this request. Add them to the application for further use
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530892func (va *VoltApplication) PortAddInd(cntx context.Context, device string, id uint32, portName string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530893 logger.Infow(ctx, "Received Port Ind: Add", log.Fields{"Device": device, "Port": portName})
894 va.portLock.Lock()
895 if d := va.GetDevice(device); d != nil {
896 p := d.AddPort(portName, id)
897 va.PortsDisc.Store(portName, p)
898 va.portLock.Unlock()
899 nni, _ := va.GetNniPort(device)
900 if nni == portName {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530901 d.pushFlowsForUnis(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530902 }
903 } else {
904 va.portLock.Unlock()
905 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Add", log.Fields{"Device": device, "Port": portName})
906 }
907}
908
909// PortDelInd : Only the NNI ports are recorded in the device for now. When port delete
910// arrives, only the NNI ports need adjustments.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530911func (va *VoltApplication) PortDelInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530912 logger.Infow(ctx, "Received Port Ind: Delete", log.Fields{"Device": device, "Port": port})
913 if d := va.GetDevice(device); d != nil {
914 p := d.GetPort(port)
915 if p != nil && p.State == PortStateUp {
916 logger.Infow(ctx, "Port state is UP. Trigerring Port Down Ind before deleting", log.Fields{"Port": p})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530917 va.PortDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530918 }
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530919 // if RemoveFlowsOnDisable is flase, then flows will be existing till port delete. Remove the flows now
920 if !va.OltFlowServiceConfig.RemoveFlowsOnDisable {
Akash Sonia8246972023-01-03 10:37:08 +0530921 vpvs, ok := va.VnetsByPort.Load(port)
922 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530923 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
924 } else {
925 for _, vpv := range vpvs.([]*VoltPortVnet) {
926 vpv.VpvLock.Lock()
927 vpv.PortDownInd(cntx, device, port, true)
928 vpv.VpvLock.Unlock()
929 }
930 }
931 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530932 va.portLock.Lock()
933 defer va.portLock.Unlock()
934 d.DelPort(port)
935 if _, ok := va.PortsDisc.Load(port); ok {
936 va.PortsDisc.Delete(port)
937 }
938 } else {
939 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Delete", log.Fields{"Device": device, "Port": port})
940 }
941}
942
vinokuma926cb3e2023-03-29 11:41:06 +0530943// PortUpdateInd Updates port Id incase of ONU movement
Naveen Sampath04696f72022-06-13 15:19:14 +0530944func (va *VoltApplication) PortUpdateInd(device string, portName string, id uint32) {
945 logger.Infow(ctx, "Received Port Ind: Update", log.Fields{"Device": device, "Port": portName})
946 va.portLock.Lock()
947 defer va.portLock.Unlock()
948 if d := va.GetDevice(device); d != nil {
949 vp := d.GetPort(portName)
950 vp.ID = id
951 } else {
952 logger.Warnw(ctx, "Device Not Found", log.Fields{"Device": device, "Port": portName})
953 }
954}
955
956// AddNbPonPort Add pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530957func (va *VoltApplication) AddNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530958 enableMulticastKPI bool, portAlarmProfileID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530959 var nbd *NbDevice
960 nbDevice, ok := va.NbDevice.Load(oltSbID)
961
962 if !ok {
963 nbd = NewNbDevice()
964 nbd.SouthBoundID = oltSbID
965 } else {
966 nbd = nbDevice.(*NbDevice)
967 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530968 port := nbd.AddPortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530969
970 // Add this port to voltDevice
971 addPort := func(key, value interface{}) bool {
972 voltDevice := value.(*VoltDevice)
973 if oltSbID == voltDevice.SouthBoundID {
974 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); !exists {
975 voltDevice.ActiveChannelsPerPon.Store(portID, port)
976 }
977 return false
978 }
979 return true
980 }
981 va.DevicesDisc.Range(addPort)
982 va.NbDevice.Store(oltSbID, nbd)
983
984 return nil
985}
986
987// UpdateNbPonPort update pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530988func (va *VoltApplication) UpdateNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530989 var nbd *NbDevice
990 nbDevice, ok := va.NbDevice.Load(oltSbID)
991
992 if !ok {
993 logger.Errorw(ctx, "Device-doesn't-exists", log.Fields{"deviceID": oltSbID})
994 return fmt.Errorf("Device-doesn't-exists-%v", oltSbID)
995 }
996 nbd = nbDevice.(*NbDevice)
997
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530998 port := nbd.UpdatePortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530999 if port == nil {
1000 return fmt.Errorf("Port-doesn't-exists-%v", portID)
1001 }
1002 va.NbDevice.Store(oltSbID, nbd)
1003
1004 // Add this port to voltDevice
1005 updPort := func(key, value interface{}) bool {
1006 voltDevice := value.(*VoltDevice)
1007 if oltSbID == voltDevice.SouthBoundID {
1008 voltDevice.ActiveChannelCountLock.Lock()
1009 if p, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
1010 oldPort := p.(*PonPortCfg)
1011 if port.MaxActiveChannels != 0 {
1012 oldPort.MaxActiveChannels = port.MaxActiveChannels
1013 oldPort.EnableMulticastKPI = port.EnableMulticastKPI
1014 voltDevice.ActiveChannelsPerPon.Store(portID, oldPort)
1015 }
1016 }
1017 voltDevice.ActiveChannelCountLock.Unlock()
1018 return false
1019 }
1020 return true
1021 }
1022 va.DevicesDisc.Range(updPort)
1023
1024 return nil
1025}
1026
1027// DeleteNbPonPort Delete pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301028func (va *VoltApplication) DeleteNbPonPort(cntx context.Context, oltSbID string, portID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301029 nbDevice, ok := va.NbDevice.Load(oltSbID)
1030 if ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301031 nbDevice.(*NbDevice).DeletePortFromNbDevice(cntx, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301032 va.NbDevice.Store(oltSbID, nbDevice.(*NbDevice))
1033 } else {
1034 logger.Warnw(ctx, "Delete pon received for unknown device", log.Fields{"oltSbID": oltSbID})
1035 return nil
1036 }
1037 // Delete this port from voltDevice
1038 delPort := func(key, value interface{}) bool {
1039 voltDevice := value.(*VoltDevice)
1040 if oltSbID == voltDevice.SouthBoundID {
1041 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
1042 voltDevice.ActiveChannelsPerPon.Delete(portID)
1043 }
1044 return false
1045 }
1046 return true
1047 }
1048 va.DevicesDisc.Range(delPort)
1049 return nil
1050}
1051
1052// GetNniPort : Get the NNI port for a device. Called from different other applications
1053// as a port to match or destination for a packet out. The VOLT application
1054// is written with the assumption that there is a single NNI port. The OLT
1055// device is responsible for translating the combination of VLAN and the
1056// NNI port ID to identify possibly a single physical port or a logical
1057// port which is a result of protection methods applied.
1058func (va *VoltApplication) GetNniPort(device string) (string, error) {
1059 va.portLock.Lock()
1060 defer va.portLock.Unlock()
1061 d, ok := va.DevicesDisc.Load(device)
1062 if !ok {
1063 return "", errors.New("Device Doesn't Exist")
1064 }
1065 return d.(*VoltDevice).NniPort, nil
1066}
1067
1068// NniDownInd process for Nni down indication.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301069func (va *VoltApplication) NniDownInd(cntx context.Context, deviceID string, devSrNo string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301070 logger.Debugw(ctx, "NNI Down Ind", log.Fields{"device": devSrNo})
1071
1072 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1073 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301074 mvProfile.removeIgmpMcastFlows(cntx, devSrNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301075 return true
1076 }
1077 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1078
1079 //Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301080 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301081}
1082
1083// DeviceUpInd changes device state to up.
1084func (va *VoltApplication) DeviceUpInd(device string) {
1085 logger.Warnw(ctx, "Received Device Ind: UP", log.Fields{"Device": device})
1086 if d := va.GetDevice(device); d != nil {
1087 d.State = controller.DeviceStateUP
1088 } else {
1089 logger.Errorw(ctx, "Ignoring Device indication: UP. Device Missing", log.Fields{"Device": device})
1090 }
1091}
1092
1093// DeviceDownInd changes device state to down.
1094func (va *VoltApplication) DeviceDownInd(device string) {
1095 logger.Warnw(ctx, "Received Device Ind: DOWN", log.Fields{"Device": device})
1096 if d := va.GetDevice(device); d != nil {
1097 d.State = controller.DeviceStateDOWN
1098 } else {
1099 logger.Errorw(ctx, "Ignoring Device indication: DOWN. Device Missing", log.Fields{"Device": device})
1100 }
1101}
1102
1103// DeviceRebootInd process for handling flow clear flag for device reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301104func (va *VoltApplication) DeviceRebootInd(cntx context.Context, device string, serialNum string, southBoundID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301105 logger.Warnw(ctx, "Received Device Ind: Reboot", log.Fields{"Device": device, "SerialNumber": serialNum})
1106
1107 if d := va.GetDevice(device); d != nil {
1108 if d.State == controller.DeviceStateREBOOTED {
1109 logger.Warnw(ctx, "Ignoring Device Ind: Reboot, Device already in Reboot state", log.Fields{"Device": device, "SerialNumber": serialNum, "State": d.State})
1110 return
1111 }
1112 d.State = controller.DeviceStateREBOOTED
1113 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301114 va.HandleFlowClearFlag(cntx, device, serialNum, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301115}
1116
1117// DeviceDisableInd handles device deactivation process
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301118func (va *VoltApplication) DeviceDisableInd(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301119 logger.Warnw(ctx, "Received Device Ind: Disable", log.Fields{"Device": device})
1120
1121 d := va.GetDevice(device)
1122 if d == nil {
1123 logger.Errorw(ctx, "Ignoring Device indication: DISABLED. Device Missing", log.Fields{"Device": device})
1124 return
1125 }
1126
1127 d.State = controller.DeviceStateDISABLED
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301128 va.HandleFlowClearFlag(cntx, device, d.SerialNum, d.SouthBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301129}
1130
1131// ProcessIgmpDSFlowForMvlan for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301132func (va *VoltApplication) ProcessIgmpDSFlowForMvlan(cntx context.Context, d *VoltDevice, mvp *MvlanProfile, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301133 logger.Debugw(ctx, "Process IGMP DS Flows for MVlan", log.Fields{"device": d.Name, "Mvlan": mvp.Mvlan, "addFlow": addFlow})
1134 portState := false
1135 p := d.GetPort(d.NniPort)
1136 if p != nil && p.State == PortStateUp {
1137 portState = true
1138 }
1139
1140 if addFlow {
1141 if portState {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301142 mvp.pushIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301143 }
1144 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301145 mvp.removeIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301146 }
1147}
1148
1149// ProcessIgmpDSFlowForDevice for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301150func (va *VoltApplication) ProcessIgmpDSFlowForDevice(cntx context.Context, d *VoltDevice, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301151 logger.Debugw(ctx, "Process IGMP DS Flows for device", log.Fields{"device": d.Name, "addFlow": addFlow})
1152
1153 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1154 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301155 va.ProcessIgmpDSFlowForMvlan(cntx, d, mvProfile, addFlow)
Naveen Sampath04696f72022-06-13 15:19:14 +05301156 return true
1157 }
1158 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1159}
1160
1161// GetDeviceFromPort : This is suitable only for access ports as their naming convention
1162// makes them unique across all the OLTs. This must be called with
1163// port name that is an access port. Currently called from VNETs, attached
1164// only to access ports, and the services which are also attached only
1165// to access ports
1166func (va *VoltApplication) GetDeviceFromPort(port string) (*VoltDevice, error) {
1167 va.portLock.Lock()
1168 defer va.portLock.Unlock()
1169 var err error
1170 err = nil
1171 p, ok := va.PortsDisc.Load(port)
1172 if !ok {
1173 return nil, errorCodes.ErrPortNotFound
1174 }
1175 d := va.GetDevice(p.(*VoltPort).Device)
1176 if d == nil {
1177 err = errorCodes.ErrDeviceNotFound
1178 }
1179 return d, err
1180}
1181
1182// GetPortID : This too applies only to access ports. The ports can be indexed
1183// purely by their names without the device forming part of the key
1184func (va *VoltApplication) GetPortID(port string) (uint32, error) {
1185 va.portLock.Lock()
1186 defer va.portLock.Unlock()
1187 p, ok := va.PortsDisc.Load(port)
1188 if !ok {
1189 return 0, errorCodes.ErrPortNotFound
1190 }
1191 return p.(*VoltPort).ID, nil
1192}
1193
1194// GetPortName : This too applies only to access ports. The ports can be indexed
1195// purely by their names without the device forming part of the key
1196func (va *VoltApplication) GetPortName(port uint32) (string, error) {
1197 va.portLock.Lock()
1198 defer va.portLock.Unlock()
1199 var portName string
1200 va.PortsDisc.Range(func(key interface{}, value interface{}) bool {
1201 portInfo := value.(*VoltPort)
1202 if portInfo.ID == port {
1203 portName = portInfo.Name
1204 return false
1205 }
1206 return true
1207 })
1208 return portName, nil
1209}
1210
1211// GetPonFromUniPort to get Pon info from UniPort
1212func (va *VoltApplication) GetPonFromUniPort(port string) (string, error) {
1213 uniPortID, err := va.GetPortID(port)
1214 if err == nil {
1215 ponPortID := (uniPortID & 0x0FF00000) >> 20 //pon(8) + onu(8) + uni(12)
1216 return strconv.FormatUint(uint64(ponPortID), 10), nil
1217 }
1218 return "", err
1219}
1220
1221// GetPortState : This too applies only to access ports. The ports can be indexed
1222// purely by their names without the device forming part of the key
1223func (va *VoltApplication) GetPortState(port string) (PortState, error) {
1224 va.portLock.Lock()
1225 defer va.portLock.Unlock()
1226 p, ok := va.PortsDisc.Load(port)
1227 if !ok {
1228 return 0, errors.New("Port not configured")
1229 }
1230 return p.(*VoltPort).State, nil
1231}
1232
1233// GetIcmpv6Receivers to get Icmp v6 receivers
1234func (va *VoltApplication) GetIcmpv6Receivers(device string) []uint32 {
1235 var receiverList []uint32
1236 receivers, _ := va.Icmpv6Receivers.Load(device)
1237 if receivers != nil {
1238 receiverList = receivers.([]uint32)
1239 }
1240 return receiverList
1241}
1242
1243// AddIcmpv6Receivers to add Icmp v6 receivers
1244func (va *VoltApplication) AddIcmpv6Receivers(device string, portID uint32) []uint32 {
1245 var receiverList []uint32
1246 receivers, _ := va.Icmpv6Receivers.Load(device)
1247 if receivers != nil {
1248 receiverList = receivers.([]uint32)
1249 }
1250 receiverList = append(receiverList, portID)
1251 va.Icmpv6Receivers.Store(device, receiverList)
1252 logger.Debugw(ctx, "Receivers after addition", log.Fields{"Receivers": receiverList})
1253 return receiverList
1254}
1255
1256// DelIcmpv6Receivers to delete Icmp v6 receievers
1257func (va *VoltApplication) DelIcmpv6Receivers(device string, portID uint32) []uint32 {
1258 var receiverList []uint32
1259 receivers, _ := va.Icmpv6Receivers.Load(device)
1260 if receivers != nil {
1261 receiverList = receivers.([]uint32)
1262 }
1263 for i, port := range receiverList {
1264 if port == portID {
1265 receiverList = append(receiverList[0:i], receiverList[i+1:]...)
1266 va.Icmpv6Receivers.Store(device, receiverList)
1267 break
1268 }
1269 }
1270 logger.Debugw(ctx, "Receivers After deletion", log.Fields{"Receivers": receiverList})
1271 return receiverList
1272}
1273
1274// ProcessDevFlowForDevice - Process DS ICMPv6 & ARP flow for provided device and vnet profile
1275// device - Device Obj
1276// vnet - vnet profile name
1277// enabled - vlan enabled/disabled - based on the status, the flow shall be added/removed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301278func (va *VoltApplication) ProcessDevFlowForDevice(cntx context.Context, device *VoltDevice, vnet *VoltVnet, enabled bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301279 _, applied := device.ConfiguredVlanForDeviceFlows.Get(VnetKey(vnet.SVlan, vnet.CVlan, 0))
1280 if enabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301281 va.PushDevFlowForVlan(cntx, vnet)
Naveen Sampath04696f72022-06-13 15:19:14 +05301282 } else if !enabled && applied {
1283 //va.DeleteDevFlowForVlan(vnet)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301284 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, device.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301285 }
1286}
1287
vinokuma926cb3e2023-03-29 11:41:06 +05301288// NniVlanIndToIgmp - Trigger receiver up indication to all ports with igmp enabled
1289// and has the provided mvlan
Naveen Sampath04696f72022-06-13 15:19:14 +05301290func (va *VoltApplication) NniVlanIndToIgmp(device *VoltDevice, mvp *MvlanProfile) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301291 logger.Infow(ctx, "Sending Igmp Receiver UP indication for all Services", log.Fields{"Vlan": mvp.Mvlan})
1292
vinokuma926cb3e2023-03-29 11:41:06 +05301293 // Trigger nni indication for receiver only for first time
Naveen Sampath04696f72022-06-13 15:19:14 +05301294 if device.IgmpDsFlowAppliedForMvlan[uint16(mvp.Mvlan)] {
1295 return
1296 }
1297 device.Ports.Range(func(key, value interface{}) bool {
1298 port := key.(string)
1299
1300 if state, _ := va.GetPortState(port); state == PortStateUp {
1301 vpvs, _ := va.VnetsByPort.Load(port)
1302 if vpvs == nil {
1303 return true
1304 }
1305 for _, vpv := range vpvs.([]*VoltPortVnet) {
vinokuma926cb3e2023-03-29 11:41:06 +05301306 // Send indication only for subscribers with the received mvlan profile
Naveen Sampath04696f72022-06-13 15:19:14 +05301307 if vpv.IgmpEnabled && vpv.MvlanProfileName == mvp.Name {
1308 vpv.services.Range(ReceiverUpInd)
1309 }
1310 }
1311 }
1312 return true
1313 })
1314}
1315
1316// PortUpInd :
1317// -----------------------------------------------------------------------
1318// Port status change handling
1319// ----------------------------------------------------------------------
1320// Port UP indication is passed to all services associated with the port
1321// so that the services can configure flows applicable when the port goes
1322// up from down state
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301323func (va *VoltApplication) PortUpInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301324 d := va.GetDevice(device)
1325
1326 if d == nil {
1327 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: UP", log.Fields{"Device": device, "Port": port})
1328 return
1329 }
1330
vinokuma926cb3e2023-03-29 11:41:06 +05301331 // Fixme: If Port Update Comes in large numbers, this will result in slow update per device
Naveen Sampath04696f72022-06-13 15:19:14 +05301332 va.portLock.Lock()
1333 // Do not defer the port mutex unlock here
1334 // Some of the following func calls needs the port lock, so defering the lock here
1335 // may lead to dead-lock
1336 p := d.GetPort(port)
1337
1338 if p == nil {
1339 logger.Infow(ctx, "Ignoring Port Ind: UP, Port doesnt exist", log.Fields{"Device": device, "PortName": port, "PortId": p})
1340 va.portLock.Unlock()
1341 return
1342 }
1343 p.State = PortStateUp
1344 va.portLock.Unlock()
1345
1346 logger.Infow(ctx, "Received SouthBound Port Ind: UP", log.Fields{"Device": device, "PortName": port, "PortId": p.ID})
1347 if p.Type == VoltPortTypeNni {
Naveen Sampath04696f72022-06-13 15:19:14 +05301348 logger.Warnw(ctx, "Received NNI Port Ind: UP", log.Fields{"Device": device, "PortName": port, "PortId": p.ID})
1349 //va.PushDevFlowForDevice(d)
1350 //Build Igmp TrapFlowRule
1351 //va.ProcessIgmpDSFlowForDevice(d, true)
1352 }
1353 vpvs, ok := va.VnetsByPort.Load(port)
1354 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
1355 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
1356 //msgbus.ProcessPortInd(msgbus.PortUp, d.SerialNum, p.Name, false, getServiceList(port))
1357 return
1358 }
1359
vinokuma926cb3e2023-03-29 11:41:06 +05301360 // If NNI port is not UP, do not push Flows
Naveen Sampath04696f72022-06-13 15:19:14 +05301361 if d.NniPort == "" {
1362 logger.Warnw(ctx, "NNI port not UP. Not sending Port UP Ind for VPVs", log.Fields{"NNI": d.NniPort})
1363 return
1364 }
1365
Naveen Sampath04696f72022-06-13 15:19:14 +05301366 for _, vpv := range vpvs.([]*VoltPortVnet) {
1367 vpv.VpvLock.Lock()
vinokuma926cb3e2023-03-29 11:41:06 +05301368 // If no service is activated drop the portUpInd
Tinoj Josephec742f62022-09-29 19:11:10 +05301369 if vpv.IsServiceActivated(cntx) {
vinokuma926cb3e2023-03-29 11:41:06 +05301370 // Do not trigger indication for the vpv which is already removed from vpv list as
Tinoj Josephec742f62022-09-29 19:11:10 +05301371 // part of service delete (during the lock wait duration)
1372 // In that case, the services associated wil be zero
1373 if vpv.servicesCount.Load() != 0 {
1374 vpv.PortUpInd(cntx, d, port)
1375 }
1376 } else {
1377 // Service not activated, still attach device to service
1378 vpv.setDevice(d.Name)
Naveen Sampath04696f72022-06-13 15:19:14 +05301379 }
1380 vpv.VpvLock.Unlock()
1381 }
1382 // At the end of processing inform the other entities that
1383 // are interested in the events
1384}
1385
1386/*
1387func getServiceList(port string) map[string]bool {
1388 serviceList := make(map[string]bool)
1389
1390 getServiceNames := func(key interface{}, value interface{}) bool {
1391 serviceList[key.(string)] = value.(*VoltService).DsHSIAFlowsApplied
1392 return true
1393 }
1394
1395 if vpvs, _ := GetApplication().VnetsByPort.Load(port); vpvs != nil {
1396 vpvList := vpvs.([]*VoltPortVnet)
1397 for _, vpv := range vpvList {
1398 vpv.services.Range(getServiceNames)
1399 }
1400 }
1401 return serviceList
1402
1403}*/
1404
vinokuma926cb3e2023-03-29 11:41:06 +05301405// ReceiverUpInd - Send receiver up indication for service with Igmp enabled
Naveen Sampath04696f72022-06-13 15:19:14 +05301406func ReceiverUpInd(key, value interface{}) bool {
1407 svc := value.(*VoltService)
1408 var vlan of.VlanType
1409
1410 if !svc.IPAssigned() {
1411 logger.Infow(ctx, "IP Not assigned, skipping general query", log.Fields{"Service": svc})
1412 return false
1413 }
1414
vinokuma926cb3e2023-03-29 11:41:06 +05301415 // Send port up indication to igmp only for service with igmp enabled
Naveen Sampath04696f72022-06-13 15:19:14 +05301416 if svc.IgmpEnabled {
1417 if svc.VlanControl == ONUCVlan || svc.VlanControl == ONUCVlanOLTSVlan {
1418 vlan = svc.CVlan
1419 } else {
1420 vlan = svc.UniVlan
1421 }
1422 if device, _ := GetApplication().GetDeviceFromPort(svc.Port); device != nil {
1423 GetApplication().ReceiverUpInd(device.Name, svc.Port, svc.MvlanProfileName, vlan, svc.Pbits)
1424 }
1425 return false
1426 }
1427 return true
1428}
1429
1430// PortDownInd : Port down indication is passed on to the services so that the services
1431// can make changes at this transition.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301432func (va *VoltApplication) PortDownInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301433 logger.Infow(ctx, "Received SouthBound Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1434 d := va.GetDevice(device)
1435
1436 if d == nil {
1437 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1438 return
1439 }
vinokuma926cb3e2023-03-29 11:41:06 +05301440 // Fixme: If Port Update Comes in large numbers, this will result in slow update per device
Naveen Sampath04696f72022-06-13 15:19:14 +05301441 va.portLock.Lock()
1442 // Do not defer the port mutex unlock here
1443 // Some of the following func calls needs the port lock, so defering the lock here
1444 // may lead to dead-lock
1445 p := d.GetPort(port)
1446 if p == nil {
1447 logger.Infow(ctx, "Ignoring Port Ind: Down, Port doesnt exist", log.Fields{"Device": device, "PortName": port, "PortId": p})
1448 va.portLock.Unlock()
1449 return
1450 }
1451 p.State = PortStateDown
1452 va.portLock.Unlock()
1453
1454 if d.State == controller.DeviceStateREBOOTED {
1455 logger.Infow(ctx, "Ignoring Port Ind: Down, Device has been Rebooted", log.Fields{"Device": device, "PortName": port, "PortId": p})
1456 return
1457 }
1458
1459 if p.Type == VoltPortTypeNni {
1460 logger.Warnw(ctx, "Received NNI Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301461 va.DeleteDevFlowForDevice(cntx, d)
1462 va.NniDownInd(cntx, device, d.SerialNum)
1463 va.RemovePendingGroups(cntx, device, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301464 }
1465 vpvs, ok := va.VnetsByPort.Load(port)
1466 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
1467 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
1468 //msgbus.ProcessPortInd(msgbus.PortDown, d.SerialNum, p.Name, false, getServiceList(port))
1469 return
1470 }
Akash Sonia8246972023-01-03 10:37:08 +05301471
Naveen Sampath04696f72022-06-13 15:19:14 +05301472 for _, vpv := range vpvs.([]*VoltPortVnet) {
1473 vpv.VpvLock.Lock()
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301474 vpv.PortDownInd(cntx, device, port, false)
Naveen Sampath04696f72022-06-13 15:19:14 +05301475 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301476 va.ReceiverDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +05301477 }
1478 vpv.VpvLock.Unlock()
1479 }
1480}
1481
1482// PacketInInd :
1483// -----------------------------------------------------------------------
1484// PacketIn Processing
1485// Packet In Indication processing. It arrives with the identities of
1486// the device and port on which the packet is received. At first, the
1487// packet is decoded and the right processor is called. Currently, we
1488// plan to support only DHCP and IGMP. In future, we can add more
1489// capabilities as needed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301490func (va *VoltApplication) PacketInInd(cntx context.Context, device string, port string, pkt []byte) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301491 // Decode the incoming packet
1492 packetSide := US
1493 if strings.Contains(port, NNI) {
1494 packetSide = DS
1495 }
1496
1497 logger.Debugw(ctx, "Received a Packet-In Indication", log.Fields{"Device": device, "Port": port})
1498
1499 gopkt := gopacket.NewPacket(pkt, layers.LayerTypeEthernet, gopacket.Default)
1500
1501 var dot1qFound = false
1502 for _, l := range gopkt.Layers() {
1503 if l.LayerType() == layers.LayerTypeDot1Q {
1504 dot1qFound = true
1505 break
1506 }
1507 }
1508
1509 if !dot1qFound {
1510 logger.Debugw(ctx, "Ignoring Received Packet-In Indication without Dot1Q Header",
1511 log.Fields{"Device": device, "Port": port})
1512 return
1513 }
1514
1515 logger.Debugw(ctx, "Received Southbound Packet In", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1516
1517 // Classify the packet into packet types that we support
1518 // The supported types are DHCP and IGMP. The DHCP packet is
1519 // identified by matching the L4 protocol to UDP. The IGMP packet
1520 // is identified by matching L3 protocol to IGMP
1521 arpl := gopkt.Layer(layers.LayerTypeARP)
1522 if arpl != nil {
1523 if callBack, ok := PacketHandlers[ARP]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301524 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301525 } else {
1526 logger.Debugw(ctx, "ARP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1527 }
1528 return
1529 }
1530 ipv4l := gopkt.Layer(layers.LayerTypeIPv4)
1531 if ipv4l != nil {
1532 ip := ipv4l.(*layers.IPv4)
1533
1534 if ip.Protocol == layers.IPProtocolUDP {
1535 logger.Debugw(ctx, "Received Southbound UDP ipv4 packet in", log.Fields{"StreamSide": packetSide})
1536 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv4)
1537 if dhcpl != nil {
1538 if callBack, ok := PacketHandlers[DHCPv4]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301539 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301540 } else {
1541 logger.Debugw(ctx, "DHCPv4 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1542 }
1543 }
1544 } else if ip.Protocol == layers.IPProtocolIGMP {
1545 logger.Debugw(ctx, "Received Southbound IGMP packet in", log.Fields{"StreamSide": packetSide})
1546 if callBack, ok := PacketHandlers[IGMP]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301547 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301548 } else {
1549 logger.Debugw(ctx, "IGMP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1550 }
1551 }
1552 return
1553 }
1554 ipv6l := gopkt.Layer(layers.LayerTypeIPv6)
1555 if ipv6l != nil {
1556 ip := ipv6l.(*layers.IPv6)
1557 if ip.NextHeader == layers.IPProtocolUDP {
1558 logger.Debug(ctx, "Received Southbound UDP ipv6 packet in")
1559 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv6)
1560 if dhcpl != nil {
1561 if callBack, ok := PacketHandlers[DHCPv6]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301562 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301563 } else {
1564 logger.Debugw(ctx, "DHCPv6 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1565 }
1566 }
1567 }
1568 return
1569 }
1570
1571 pppoel := gopkt.Layer(layers.LayerTypePPPoE)
1572 if pppoel != nil {
1573 logger.Debugw(ctx, "Received Southbound PPPoE packet in", log.Fields{"StreamSide": packetSide})
1574 if callBack, ok := PacketHandlers[PPPOE]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301575 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301576 } else {
1577 logger.Debugw(ctx, "PPPoE handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1578 }
1579 }
1580}
1581
1582// GetVlans : This utility gets the VLANs from the packet. The VLANs are
1583// used to identify the right service that must process the incoming
1584// packet
1585func GetVlans(pkt gopacket.Packet) []of.VlanType {
1586 var vlans []of.VlanType
1587 for _, l := range pkt.Layers() {
1588 if l.LayerType() == layers.LayerTypeDot1Q {
1589 q, ok := l.(*layers.Dot1Q)
1590 if ok {
1591 vlans = append(vlans, of.VlanType(q.VLANIdentifier))
1592 }
1593 }
1594 }
1595 return vlans
1596}
1597
1598// GetPriority to get priority
1599func GetPriority(pkt gopacket.Packet) uint8 {
1600 for _, l := range pkt.Layers() {
1601 if l.LayerType() == layers.LayerTypeDot1Q {
1602 q, ok := l.(*layers.Dot1Q)
1603 if ok {
1604 return q.Priority
1605 }
1606 }
1607 }
1608 return PriorityNone
1609}
1610
1611// HandleFlowClearFlag to handle flow clear flag during reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301612func (va *VoltApplication) HandleFlowClearFlag(cntx context.Context, deviceID string, serialNum, southBoundID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301613 logger.Warnw(ctx, "Clear All flags for Device", log.Fields{"Device": deviceID, "SerialNum": serialNum, "SBID": southBoundID})
1614 dev, ok := va.DevicesDisc.Load(deviceID)
1615 if ok && dev != nil {
1616 logger.Infow(ctx, "Clear Flags for device", log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1617 dev.(*VoltDevice).icmpv6GroupAdded = false
1618 logger.Infow(ctx, "Clearing DS Icmpv6 Map",
1619 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1620 dev.(*VoltDevice).ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
1621 logger.Infow(ctx, "Clearing DS IGMP Map",
1622 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1623 for k := range dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan {
1624 delete(dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan, k)
1625 }
vinokuma926cb3e2023-03-29 11:41:06 +05301626 // Delete group 1 - ICMPv6/ARP group
Naveen Sampath04696f72022-06-13 15:19:14 +05301627 if err := ProcessIcmpv6McGroup(deviceID, true); err != nil {
1628 logger.Errorw(ctx, "ProcessIcmpv6McGroup failed", log.Fields{"Device": deviceID, "Error": err})
1629 }
1630 } else {
1631 logger.Warnw(ctx, "VoltDevice not found for device ", log.Fields{"deviceID": deviceID})
1632 }
1633
1634 getVpvs := func(key interface{}, value interface{}) bool {
1635 vpvs := value.([]*VoltPortVnet)
1636 for _, vpv := range vpvs {
1637 if vpv.Device == deviceID {
1638 logger.Infow(ctx, "Clear Flags for vpv",
1639 log.Fields{"device": vpv.Device, "port": vpv.Port,
1640 "svlan": vpv.SVlan, "cvlan": vpv.CVlan, "univlan": vpv.UniVlan})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301641 vpv.ClearAllServiceFlags(cntx)
1642 vpv.ClearAllVpvFlags(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301643
1644 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301645 va.ReceiverDownInd(cntx, vpv.Device, vpv.Port)
vinokuma926cb3e2023-03-29 11:41:06 +05301646 // Also clear service igmp stats
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301647 vpv.ClearServiceCounters(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301648 }
1649 }
1650 }
1651 return true
1652 }
1653 va.VnetsByPort.Range(getVpvs)
1654
vinokuma926cb3e2023-03-29 11:41:06 +05301655 // Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301656 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301657
1658 logger.Warnw(ctx, "All flags cleared for device", log.Fields{"Device": deviceID})
1659
vinokuma926cb3e2023-03-29 11:41:06 +05301660 // Reset pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301661 va.RemovePendingGroups(cntx, deviceID, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301662
vinokuma926cb3e2023-03-29 11:41:06 +05301663 // Process all Migrate Service Request - force udpate all profiles since resources are already cleaned up
Naveen Sampath04696f72022-06-13 15:19:14 +05301664 if dev != nil {
1665 triggerForceUpdate := func(key, value interface{}) bool {
1666 msrList := value.(*util.ConcurrentMap)
1667 forceUpdateServices := func(key, value interface{}) bool {
1668 msr := value.(*MigrateServicesRequest)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301669 forceUpdateAllServices(cntx, msr)
Naveen Sampath04696f72022-06-13 15:19:14 +05301670 return true
1671 }
1672 msrList.Range(forceUpdateServices)
1673 return true
1674 }
1675 dev.(*VoltDevice).MigratingServices.Range(triggerForceUpdate)
1676 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301677 va.FetchAndProcessAllMigrateServicesReq(cntx, deviceID, forceUpdateAllServices)
Naveen Sampath04696f72022-06-13 15:19:14 +05301678 }
1679}
1680
vinokuma926cb3e2023-03-29 11:41:06 +05301681// GetPonPortIDFromUNIPort to get pon port id from uni port
Naveen Sampath04696f72022-06-13 15:19:14 +05301682func GetPonPortIDFromUNIPort(uniPortID uint32) uint32 {
1683 ponPortID := (uniPortID & 0x0FF00000) >> 20
1684 return ponPortID
1685}
1686
vinokuma926cb3e2023-03-29 11:41:06 +05301687// ProcessFlowModResultIndication - Processes Flow mod operation indications from controller
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301688func (va *VoltApplication) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301689 d := va.GetDevice(flowStatus.Device)
1690 if d == nil {
1691 logger.Errorw(ctx, "Dropping Flow Mod Indication. Device not found", log.Fields{"Cookie": flowStatus.Cookie, "Device": flowStatus.Device})
1692 return
1693 }
1694
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301695 cookieExists := ExecuteFlowEvent(cntx, d, flowStatus.Cookie, flowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +05301696
1697 if flowStatus.Flow != nil {
1698 flowAdd := (flowStatus.FlowModType == of.CommandAdd)
1699 if !cookieExists && !isFlowStatusSuccess(flowStatus.Status, flowAdd) {
1700 pushFlowFailureNotif(flowStatus)
1701 }
1702 }
1703}
1704
1705func pushFlowFailureNotif(flowStatus intf.FlowStatus) {
1706 subFlow := flowStatus.Flow
1707 cookie := subFlow.Cookie
1708 uniPort := cookie >> 16 & 0xFFFFFFFF
1709 logger.Errorw(ctx, "Flow Failure Notification", log.Fields{"uniPort": uniPort, "Cookie": cookie})
Naveen Sampath04696f72022-06-13 15:19:14 +05301710}
1711
vinokuma926cb3e2023-03-29 11:41:06 +05301712// UpdateMvlanProfilesForDevice to update mvlan profile for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301713func (va *VoltApplication) UpdateMvlanProfilesForDevice(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301714 checkAndAddMvlanUpdateTask := func(key, value interface{}) bool {
1715 mvp := value.(*MvlanProfile)
1716 if mvp.IsUpdateInProgressForDevice(device) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301717 mvp.UpdateProfile(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301718 }
1719 return true
1720 }
1721 va.MvlanProfilesByName.Range(checkAndAddMvlanUpdateTask)
1722}
1723
1724// TaskInfo structure that is used to store the task Info.
1725type TaskInfo struct {
1726 ID string
1727 Name string
1728 Timestamp string
1729}
1730
1731// GetTaskList to get task list information.
1732func (va *VoltApplication) GetTaskList(device string) map[int]*TaskInfo {
1733 taskList := cntlr.GetController().GetTaskList(device)
1734 taskMap := make(map[int]*TaskInfo)
1735 for i, task := range taskList {
1736 taskID := strconv.Itoa(int(task.TaskID()))
1737 name := task.Name()
1738 timestamp := task.Timestamp()
1739 taskInfo := &TaskInfo{ID: taskID, Name: name, Timestamp: timestamp}
1740 taskMap[i] = taskInfo
1741 }
1742 return taskMap
1743}
1744
1745// UpdateDeviceSerialNumberList to update the device serial number list after device serial number is updated for vnet and mvlan
1746func (va *VoltApplication) UpdateDeviceSerialNumberList(oldOltSlNo string, newOltSlNo string) {
Tinoj Joseph50d722c2022-12-06 22:53:22 +05301747 voltDevice, _ := va.GetDeviceBySerialNo(oldOltSlNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301748
1749 if voltDevice != nil {
1750 // Device is present with old serial number ID
1751 logger.Errorw(ctx, "OLT Migration cannot be completed as there are dangling devices", log.Fields{"Serial Number": oldOltSlNo})
Naveen Sampath04696f72022-06-13 15:19:14 +05301752 } else {
1753 logger.Infow(ctx, "No device present with old serial number", log.Fields{"Serial Number": oldOltSlNo})
Naveen Sampath04696f72022-06-13 15:19:14 +05301754 // Add Serial Number to Blocked Devices List.
1755 cntlr.GetController().AddBlockedDevices(oldOltSlNo)
1756 cntlr.GetController().AddBlockedDevices(newOltSlNo)
1757
1758 updateSlNoForVnet := func(key, value interface{}) bool {
1759 vnet := value.(*VoltVnet)
1760 for i, deviceSlNo := range vnet.VnetConfig.DevicesList {
1761 if deviceSlNo == oldOltSlNo {
1762 vnet.VnetConfig.DevicesList[i] = newOltSlNo
1763 logger.Infow(ctx, "device serial number updated for vnet profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1764 break
1765 }
1766 }
1767 return true
1768 }
1769
1770 updateSlNoforMvlan := func(key interface{}, value interface{}) bool {
1771 mvProfile := value.(*MvlanProfile)
1772 for deviceSlNo := range mvProfile.DevicesList {
1773 if deviceSlNo == oldOltSlNo {
1774 mvProfile.DevicesList[newOltSlNo] = mvProfile.DevicesList[oldOltSlNo]
1775 delete(mvProfile.DevicesList, oldOltSlNo)
1776 logger.Infow(ctx, "device serial number updated for mvlan profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1777 break
1778 }
1779 }
1780 return true
1781 }
1782
1783 va.VnetsByName.Range(updateSlNoForVnet)
1784 va.MvlanProfilesByName.Range(updateSlNoforMvlan)
1785
1786 // Clear the serial number from Blocked Devices List
1787 cntlr.GetController().DelBlockedDevices(oldOltSlNo)
1788 cntlr.GetController().DelBlockedDevices(newOltSlNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301789 }
1790}
1791
1792// GetVpvsForDsPkt to get vpv for downstream packets
1793func (va *VoltApplication) GetVpvsForDsPkt(cvlan of.VlanType, svlan of.VlanType, clientMAC net.HardwareAddr,
1794 pbit uint8) ([]*VoltPortVnet, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301795 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
vinokuma926cb3e2023-03-29 11:41:06 +05301862// AddGroupToPendingPool - adds the IgmpGroup with active group table entry to global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301863func (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
vinokuma926cb3e2023-03-29 11:41:06 +05301888// RemoveGroupFromPendingPool - removes the group from global pending group pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301889func (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
vinokuma926cb3e2023-03-29 11:41:06 +05301903// 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
vinokuma926cb3e2023-03-29 11:41:06 +05301914// RemoveGroupListFromPendingPool - removes the groups for provided key
Naveen Sampath04696f72022-06-13 15:19:14 +05301915// 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
vinokuma926cb3e2023-03-29 11:41:06 +05301929// RemoveGroupDevicesFromPendingPool - removes the group from global pending group pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301930func (va *VoltApplication) RemoveGroupDevicesFromPendingPool(ig *IgmpGroup) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301931 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)})
1932 for device := range ig.PendingGroupForDevice {
1933 va.RemoveGroupFromPendingPool(device, ig)
1934 }
1935}
1936
vinokuma926cb3e2023-03-29 11:41:06 +05301937// GetGroupFromPendingPool - Returns IgmpGroup obj from global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301938func (va *VoltApplication) GetGroupFromPendingPool(mvlan of.VlanType, device string) *IgmpGroup {
Naveen Sampath04696f72022-06-13 15:19:14 +05301939 var ig *IgmpGroup
1940
1941 va.PendingPoolLock.Lock()
1942 defer va.PendingPoolLock.Unlock()
1943
1944 key := getPendingPoolKey(mvlan, device)
1945 logger.Infow(ctx, "Getting IgmpGroup from Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String(), "Key": key})
1946
vinokuma926cb3e2023-03-29 11:41:06 +05301947 // Gets all IgmpGrp Obj for the device
Naveen Sampath04696f72022-06-13 15:19:14 +05301948 grpMap, ok := va.IgmpPendingPool[key]
1949 if !ok || len(grpMap) == 0 {
1950 logger.Infow(ctx, "Matching IgmpGroup not found in Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String()})
1951 return nil
1952 }
1953
vinokuma926cb3e2023-03-29 11:41:06 +05301954 // Gets a random obj from available grps
Naveen Sampath04696f72022-06-13 15:19:14 +05301955 for ig = range grpMap {
vinokuma926cb3e2023-03-29 11:41:06 +05301956 // Remove grp obj reference from all devices associated in pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301957 for dev := range ig.Devices {
1958 key := getPendingPoolKey(mvlan, dev)
1959 delete(va.IgmpPendingPool[key], ig)
1960 }
1961
vinokuma926cb3e2023-03-29 11:41:06 +05301962 // Safety check to avoid re-allocating group already in use
Naveen Sampath04696f72022-06-13 15:19:14 +05301963 if ig.NumDevicesActive() == 0 {
1964 return ig
1965 }
1966
vinokuma926cb3e2023-03-29 11:41:06 +05301967 // Iteration will continue only if IG is not allocated
Naveen Sampath04696f72022-06-13 15:19:14 +05301968 }
1969 return nil
1970}
1971
vinokuma926cb3e2023-03-29 11:41:06 +05301972// RemovePendingGroups - removes all pending groups for provided reference from global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301973// reference - mvlan/device ID
1974// isRefDevice - true - Device as reference
vinokuma926cb3e2023-03-29 11:41:06 +05301975// false - Mvlan as reference
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301976func (va *VoltApplication) RemovePendingGroups(cntx context.Context, reference string, isRefDevice bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301977 va.PendingPoolLock.Lock()
1978 defer va.PendingPoolLock.Unlock()
1979
1980 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool", log.Fields{"Reference": reference, "isRefDevice": isRefDevice})
1981
vinokuma926cb3e2023-03-29 11:41:06 +05301982 // Pending Pool key: "<mvlan>_<DeviceID>""
Naveen Sampath04696f72022-06-13 15:19:14 +05301983 paramPosition := 0
1984 if isRefDevice {
1985 paramPosition = 1
1986 }
1987
1988 // 1.Remove the Entry from pending pool
1989 // 2.Deletes the group from device
1990 // 3.Delete the IgmpGroup obj and release the group ID to pool
1991 for key := range va.IgmpPendingPool {
1992 keyParams := strings.Split(key, "_")
1993 if keyParams[paramPosition] == reference {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301994 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301995 }
1996 }
1997}
1998
1999func getPendingPoolKey(mvlan of.VlanType, device string) string {
2000 return mvlan.String() + "_" + device
2001}
2002
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302003func (va *VoltApplication) removeExpiredGroups(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302004 logger.Debug(ctx, "Check for expired Igmp Groups")
2005 removeExpiredGroups := func(key interface{}, value interface{}) bool {
2006 ig := value.(*IgmpGroup)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302007 ig.removeExpiredGroupFromDevice(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302008 return true
2009 }
2010 va.IgmpGroups.Range(removeExpiredGroups)
2011}
2012
vinokuma926cb3e2023-03-29 11:41:06 +05302013// TriggerPendingProfileDeleteReq - trigger pending profile delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302014func (va *VoltApplication) TriggerPendingProfileDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302015 va.TriggerPendingServiceDeactivateReq(cntx, device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302016 va.TriggerPendingServiceDeleteReq(cntx, device)
2017 va.TriggerPendingVpvDeleteReq(cntx, device)
2018 va.TriggerPendingVnetDeleteReq(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05302019 logger.Warnw(ctx, "All Pending Profile Delete triggered for device", log.Fields{"Device": device})
2020}
2021
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302022// TriggerPendingServiceDeactivateReq - trigger pending service deactivate request
2023func (va *VoltApplication) TriggerPendingServiceDeactivateReq(cntx context.Context, device string) {
2024 logger.Infow(ctx, "Pending Services to be deactivated", log.Fields{"Count": len(va.ServicesToDeactivate)})
2025 for serviceName := range va.ServicesToDeactivate {
2026 logger.Infow(ctx, "Trigger Service Deactivate", log.Fields{"Service": serviceName})
2027 if vs := va.GetService(serviceName); vs != nil {
2028 if vs.Device == device {
2029 logger.Warnw(ctx, "Triggering Pending Service Deactivate", log.Fields{"Service": vs.Name})
2030 vpv := va.GetVnetByPort(vs.Port, vs.SVlan, vs.CVlan, vs.UniVlan)
2031 if vpv == nil {
2032 logger.Errorw(ctx, "Vpv Not found for Service", log.Fields{"vs": vs.Name, "port": vs.Port, "Vnet": vs.VnetID})
2033 continue
2034 }
2035
2036 vpv.DelTrapFlows(cntx)
2037 vs.DelHsiaFlows(cntx)
2038 vs.WriteToDb(cntx)
2039 vpv.ClearServiceCounters(cntx)
2040 }
2041 } else {
2042 logger.Errorw(ctx, "Pending Service Not found", log.Fields{"Service": serviceName})
2043 }
2044 }
2045}
2046
vinokuma926cb3e2023-03-29 11:41:06 +05302047// TriggerPendingServiceDeleteReq - trigger pending service delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302048func (va *VoltApplication) TriggerPendingServiceDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302049 logger.Warnw(ctx, "Pending Services to be deleted", log.Fields{"Count": len(va.ServicesToDelete)})
2050 for serviceName := range va.ServicesToDelete {
2051 logger.Debugw(ctx, "Trigger Service Delete", log.Fields{"Service": serviceName})
2052 if vs := va.GetService(serviceName); vs != nil {
2053 if vs.Device == device {
2054 logger.Warnw(ctx, "Triggering Pending Service delete", log.Fields{"Service": vs.Name})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302055 vs.DelHsiaFlows(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302056 if vs.ForceDelete {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302057 vs.DelFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302058 }
2059 }
2060 } else {
2061 logger.Errorw(ctx, "Pending Service Not found", log.Fields{"Service": serviceName})
2062 }
2063 }
2064}
2065
vinokuma926cb3e2023-03-29 11:41:06 +05302066// TriggerPendingVpvDeleteReq - trigger pending VPV delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302067func (va *VoltApplication) TriggerPendingVpvDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302068 logger.Warnw(ctx, "Pending VPVs to be deleted", log.Fields{"Count": len(va.VoltPortVnetsToDelete)})
2069 for vpv := range va.VoltPortVnetsToDelete {
2070 if vpv.Device == device {
2071 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 +05302072 va.DelVnetFromPort(cntx, vpv.Port, vpv)
Naveen Sampath04696f72022-06-13 15:19:14 +05302073 }
2074 }
2075}
2076
vinokuma926cb3e2023-03-29 11:41:06 +05302077// TriggerPendingVnetDeleteReq - trigger pending vnet delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302078func (va *VoltApplication) TriggerPendingVnetDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302079 logger.Warnw(ctx, "Pending Vnets to be deleted", log.Fields{"Count": len(va.VnetsToDelete)})
2080 for vnetName := range va.VnetsToDelete {
2081 if vnetIntf, _ := va.VnetsByName.Load(vnetName); vnetIntf != nil {
2082 vnet := vnetIntf.(*VoltVnet)
2083 logger.Warnw(ctx, "Triggering Pending Vnet flows delete", log.Fields{"Vnet": vnet.Name})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302084 if d, _ := va.GetDeviceBySerialNo(vnet.PendingDeviceToDelete); d != nil && d.SerialNum == vnet.PendingDeviceToDelete {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302085 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, vnet.PendingDeviceToDelete)
Naveen Sampath04696f72022-06-13 15:19:14 +05302086 va.deleteVnetConfig(vnet)
2087 } else {
Tinoj Joseph1d108322022-07-13 10:07:39 +05302088 logger.Warnw(ctx, "Vnet Delete Failed : Device Not Found", log.Fields{"Vnet": vnet.Name, "Device": vnet.PendingDeviceToDelete})
Naveen Sampath04696f72022-06-13 15:19:14 +05302089 }
2090 }
2091 }
2092}
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302093
2094type OltFlowService struct {
vinokuma926cb3e2023-03-29 11:41:06 +05302095 DefaultTechProfileID int `json:"defaultTechProfileId"`
Akash Sonia8246972023-01-03 10:37:08 +05302096 EnableDhcpOnNni bool `json:"enableDhcpOnNni"`
Akash Sonia8246972023-01-03 10:37:08 +05302097 EnableIgmpOnNni bool `json:"enableIgmpOnNni"`
2098 EnableEapol bool `json:"enableEapol"`
2099 EnableDhcpV6 bool `json:"enableDhcpV6"`
2100 EnableDhcpV4 bool `json:"enableDhcpV4"`
2101 RemoveFlowsOnDisable bool `json:"removeFlowsOnDisable"`
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302102}
2103
2104func (va *VoltApplication) UpdateOltFlowService(cntx context.Context, oltFlowService OltFlowService) {
2105 logger.Infow(ctx, "UpdateOltFlowService", log.Fields{"oldValue": va.OltFlowServiceConfig, "newValue": oltFlowService})
2106 va.OltFlowServiceConfig = oltFlowService
2107 b, err := json.Marshal(va.OltFlowServiceConfig)
2108 if err != nil {
2109 logger.Warnw(ctx, "Failed to Marshal OltFlowServiceConfig", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2110 return
2111 }
2112 _ = db.PutOltFlowService(cntx, string(b))
2113}
Akash Sonia8246972023-01-03 10:37:08 +05302114
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302115// RestoreOltFlowService to read from the DB and restore olt flow service config
2116func (va *VoltApplication) RestoreOltFlowService(cntx context.Context) {
2117 oltflowService, err := db.GetOltFlowService(cntx)
2118 if err != nil {
2119 logger.Warnw(ctx, "Failed to Get OltFlowServiceConfig from DB", log.Fields{"Error": err})
2120 return
2121 }
2122 err = json.Unmarshal([]byte(oltflowService), &va.OltFlowServiceConfig)
2123 if err != nil {
2124 logger.Warn(ctx, "Unmarshal of oltflowService failed")
2125 return
2126 }
2127 logger.Infow(ctx, "updated OltFlowServiceConfig from DB", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2128}
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302129
Akash Soni87a19072023-02-28 00:46:59 +05302130func (va *VoltApplication) UpdateDeviceConfig(cntx context.Context, deviceConfig *DeviceConfig) {
2131 var dc *DeviceConfig
2132 va.DevicesConfig.Store(deviceConfig.SerialNumber, deviceConfig)
2133 err := dc.WriteDeviceConfigToDb(cntx, deviceConfig.SerialNumber, deviceConfig)
2134 if err != nil {
2135 logger.Errorw(ctx, "DB update for device config failed", log.Fields{"err": err})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302136 }
Akash Soni87a19072023-02-28 00:46:59 +05302137 logger.Infow(ctx, "Added OLT configurations", log.Fields{"DeviceInfo": deviceConfig})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302138 // If device is already discovered update the VoltDevice structure
Akash Soni87a19072023-02-28 00:46:59 +05302139 device, id := va.GetDeviceBySerialNo(deviceConfig.SerialNumber)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302140 if device != nil {
Akash Soni87a19072023-02-28 00:46:59 +05302141 device.NniDhcpTrapVid = of.VlanType(deviceConfig.NniDhcpTrapVid)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302142 va.DevicesDisc.Store(id, device)
2143 }
2144}