blob: 85f06be779296a352fc9f12d61d67422e365930c [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
Akash Sonia8246972023-01-03 10:37:08 +0530418 PortAlarmProfileCache map[string]map[string]int // [portAlarmID][ThresholdLevelString]ThresholdLevel
419 vendorID string
vinokuma926cb3e2023-03-29 11:41:06 +0530420 ServiceByName sync.Map // [serName]*VoltService
421 VnetsByPort sync.Map // [portName][]*VoltPortVnet
422 VnetsByTag sync.Map // [svlan-cvlan-uvlan]*VoltVnet
423 VnetsByName sync.Map // [vnetName]*VoltVnet
424 DevicesDisc sync.Map
425 PortsDisc sync.Map
426 IgmpGroups sync.Map // [grpKey]*IgmpGroup
427 MvlanProfilesByTag sync.Map
428 MvlanProfilesByName sync.Map
429 Icmpv6Receivers sync.Map
430 DeviceCounters sync.Map //[logicalDeviceId]*DeviceCounters
431 ServiceCounters sync.Map //[serviceName]*ServiceCounters
432 NbDevice sync.Map // [OLTSouthBoundID]*NbDevice
433 OltIgmpInfoBySerial sync.Map
434 McastConfigMap sync.Map //[OltSerialNo_MvlanProfileID]*McastConfig
Akash Sonia8246972023-01-03 10:37:08 +0530435 DevicesConfig sync.Map //[serialNumber]*DeviceConfig
vinokuma926cb3e2023-03-29 11:41:06 +0530436 IgmpProfilesByName sync.Map
437 IgmpTasks tasks.Tasks
438 IndicationsTasks tasks.Tasks
439 MulticastAlarmTasks tasks.Tasks
440 IgmpKPIsTasks tasks.Tasks
441 pppoeTasks tasks.Tasks
442 OltFlowServiceConfig OltFlowService
443 PendingPoolLock sync.RWMutex
444 // MacAddress-Port MAP to avoid swap of mac across ports.
445 macPortLock sync.RWMutex
446 portLock sync.Mutex
Akash Sonia8246972023-01-03 10:37:08 +0530447}
Naveen Sampath04696f72022-06-13 15:19:14 +0530448
Akash Sonia8246972023-01-03 10:37:08 +0530449type DeviceConfig struct {
450 SerialNumber string `json:"id"`
451 HardwareIdentifier string `json:"hardwareIdentifier"`
Akash Soni87a19072023-02-28 00:46:59 +0530452 IPAddress string `json:"ipAddress"`
Akash Soni53da2852023-03-15 00:31:31 +0530453 UplinkPort string `json:"uplinkPort"`
Akash Sonia8246972023-01-03 10:37:08 +0530454 NasID string `json:"nasId"`
455 NniDhcpTrapVid int `json:"nniDhcpTrapVid"`
Naveen Sampath04696f72022-06-13 15:19:14 +0530456}
457
458// PonPortCfg contains NB port config and activeIGMPChannels count
459type PonPortCfg struct {
vinokuma926cb3e2023-03-29 11:41:06 +0530460 PortAlarmProfileID string
Naveen Sampath04696f72022-06-13 15:19:14 +0530461 PortID uint32
462 MaxActiveChannels uint32
463 ActiveIGMPChannels uint32
464 EnableMulticastKPI bool
Naveen Sampath04696f72022-06-13 15:19:14 +0530465}
466
467// NbDevice OLT Device info
468type NbDevice struct {
469 SouthBoundID string
470 PonPorts sync.Map // [PortID]*PonPortCfg
471}
472
473// RestoreNbDeviceFromDb restores the NB Device in case of VGC pod restart.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530474func (va *VoltApplication) RestoreNbDeviceFromDb(cntx context.Context, deviceID string) *NbDevice {
Naveen Sampath04696f72022-06-13 15:19:14 +0530475 nbDevice := NewNbDevice()
476 nbDevice.SouthBoundID = deviceID
477
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530478 nbPorts, _ := db.GetAllNbPorts(cntx, deviceID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530479
480 for key, p := range nbPorts {
481 b, ok := p.Value.([]byte)
482 if !ok {
483 logger.Warn(ctx, "The value type is not []byte")
484 continue
485 }
486 var port PonPortCfg
487 err := json.Unmarshal(b, &port)
488 if err != nil {
489 logger.Warn(ctx, "Unmarshal of PonPortCfg failed")
490 continue
491 }
492 logger.Debugw(ctx, "Port recovered", log.Fields{"port": port})
493 ponPortID, _ := strconv.Atoi(key)
494 nbDevice.PonPorts.Store(uint32(ponPortID), &port)
495 }
496 va.NbDevice.Store(deviceID, nbDevice)
497 return nbDevice
498}
499
500// NewNbDevice Constructor for NbDevice
501func NewNbDevice() *NbDevice {
502 var nbDevice NbDevice
503 return &nbDevice
504}
505
506// WriteToDb writes nb device port config to kv store
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530507func (nbd *NbDevice) WriteToDb(cntx context.Context, portID uint32, ponPort *PonPortCfg) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530508 b, err := json.Marshal(ponPort)
509 if err != nil {
510 logger.Errorw(ctx, "PonPortConfig-marshal-failed", log.Fields{"err": err})
511 return
512 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530513 db.PutNbDevicePort(cntx, nbd.SouthBoundID, portID, string(b))
Naveen Sampath04696f72022-06-13 15:19:14 +0530514}
515
516// AddPortToNbDevice Adds pon port to NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530517func (nbd *NbDevice) AddPortToNbDevice(cntx context.Context, portID, allowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530518 enableMulticastKPI bool, portAlarmProfileID string) *PonPortCfg {
Naveen Sampath04696f72022-06-13 15:19:14 +0530519 ponPort := &PonPortCfg{
520 PortID: portID,
521 MaxActiveChannels: allowedChannels,
522 EnableMulticastKPI: enableMulticastKPI,
523 PortAlarmProfileID: portAlarmProfileID,
524 }
525 nbd.PonPorts.Store(portID, ponPort)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530526 nbd.WriteToDb(cntx, portID, ponPort)
Naveen Sampath04696f72022-06-13 15:19:14 +0530527 return ponPort
528}
529
Akash Sonia8246972023-01-03 10:37:08 +0530530// RestoreDeviceConfigFromDb to restore vnet from port
531func (va *VoltApplication) RestoreDeviceConfigFromDb(cntx context.Context) {
532 // VNETS must be learnt first
533 dConfig, _ := db.GetDeviceConfig(cntx)
534 for _, device := range dConfig {
535 b, ok := device.Value.([]byte)
536 if !ok {
537 logger.Warn(ctx, "The value type is not []byte")
538 continue
539 }
540 devConfig := DeviceConfig{}
541 err := json.Unmarshal(b, &devConfig)
542 if err != nil {
543 logger.Warn(ctx, "Unmarshal of device configuration failed")
544 continue
545 }
546 logger.Debugw(ctx, "Retrieved device config", log.Fields{"Device Config": devConfig})
547 if err := va.AddDeviceConfig(cntx, devConfig.SerialNumber, devConfig.HardwareIdentifier, devConfig.NasID, devConfig.IPAddress, devConfig.UplinkPort, devConfig.NniDhcpTrapVid); err != nil {
548 logger.Warnw(ctx, "Add device config failed", log.Fields{"DeviceConfig": devConfig, "Error": err})
549 }
Akash Sonia8246972023-01-03 10:37:08 +0530550 }
551}
552
553// WriteDeviceConfigToDb writes sb device config to kv store
554func (dc *DeviceConfig) WriteDeviceConfigToDb(cntx context.Context, serialNum string, deviceConfig *DeviceConfig) error {
555 b, err := json.Marshal(deviceConfig)
556 if err != nil {
557 logger.Errorw(ctx, "deviceConfig-marshal-failed", log.Fields{"err": err})
558 return err
559 }
560 dberr := db.PutDeviceConfig(cntx, serialNum, string(b))
561 if dberr != nil {
562 logger.Errorw(ctx, "update device config failed", log.Fields{"err": err})
563 return dberr
564 }
565 return nil
566}
567
vinokuma926cb3e2023-03-29 11:41:06 +0530568func (va *VoltApplication) AddDeviceConfig(cntx context.Context, serialNum, hardwareIdentifier, nasID, ipAddress, uplinkPort string, nniDhcpTrapID int) error {
Akash Sonia8246972023-01-03 10:37:08 +0530569 var dc *DeviceConfig
570
Akash Soni87a19072023-02-28 00:46:59 +0530571 deviceConfig := &DeviceConfig{
572 SerialNumber: serialNum,
573 HardwareIdentifier: hardwareIdentifier,
574 NasID: nasID,
575 UplinkPort: uplinkPort,
576 IPAddress: ipAddress,
vinokuma926cb3e2023-03-29 11:41:06 +0530577 NniDhcpTrapVid: nniDhcpTrapID,
Akash Sonia8246972023-01-03 10:37:08 +0530578 }
Akash Soni87a19072023-02-28 00:46:59 +0530579 va.DevicesConfig.Store(serialNum, deviceConfig)
580 err := dc.WriteDeviceConfigToDb(cntx, serialNum, deviceConfig)
581 if err != nil {
582 logger.Errorw(ctx, "DB update for device config failed", log.Fields{"err": err})
583 return err
584 }
585
586 // If device is already discovered update the VoltDevice structure
587 device, id := va.GetDeviceBySerialNo(serialNum)
588 if device != nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530589 device.NniDhcpTrapVid = of.VlanType(nniDhcpTrapID)
Akash Soni87a19072023-02-28 00:46:59 +0530590 va.DevicesDisc.Store(id, device)
591 }
592
Akash Sonia8246972023-01-03 10:37:08 +0530593 return nil
594}
595
596// GetDeviceConfig to get a device config.
597func (va *VoltApplication) GetDeviceConfig(serNum string) *DeviceConfig {
598 if d, ok := va.DevicesConfig.Load(serNum); ok {
599 return d.(*DeviceConfig)
600 }
601 return nil
602}
603
Naveen Sampath04696f72022-06-13 15:19:14 +0530604// UpdatePortToNbDevice Adds pon port to NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530605func (nbd *NbDevice) UpdatePortToNbDevice(cntx context.Context, portID, allowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) *PonPortCfg {
Naveen Sampath04696f72022-06-13 15:19:14 +0530606 p, exists := nbd.PonPorts.Load(portID)
607 if !exists {
608 logger.Errorw(ctx, "PON port not exists in nb-device", log.Fields{"portID": portID})
609 return nil
610 }
611 port := p.(*PonPortCfg)
612 if allowedChannels != 0 {
613 port.MaxActiveChannels = allowedChannels
614 port.EnableMulticastKPI = enableMulticastKPI
615 port.PortAlarmProfileID = portAlarmProfileID
616 }
617
618 nbd.PonPorts.Store(portID, port)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530619 nbd.WriteToDb(cntx, portID, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530620 return port
621}
622
623// DeletePortFromNbDevice Deletes pon port from NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530624func (nbd *NbDevice) DeletePortFromNbDevice(cntx context.Context, portID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530625 if _, ok := nbd.PonPorts.Load(portID); ok {
626 nbd.PonPorts.Delete(portID)
627 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530628 db.DelNbDevicePort(cntx, nbd.SouthBoundID, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530629}
630
631// GetApplication : Interface to access the singleton object
632func GetApplication() *VoltApplication {
633 if vapplication == nil {
634 vapplication = newVoltApplication()
635 }
636 return vapplication
637}
638
639// newVoltApplication : Constructor for the singleton object. Hence this is not
640// an exported function
641func newVoltApplication() *VoltApplication {
642 var va VoltApplication
643 va.IgmpTasks.Initialize(context.TODO())
644 va.MulticastAlarmTasks.Initialize(context.TODO())
645 va.IgmpKPIsTasks.Initialize(context.TODO())
646 va.pppoeTasks.Initialize(context.TODO())
647 va.storeIgmpProfileMap(DefaultIgmpProfID, newDefaultIgmpProfile())
648 va.MeterMgr.Init()
649 va.AddIgmpGroups(5000)
650 va.macPortMap = make(map[string]string)
651 va.IgmpPendingPool = make(map[string]map[*IgmpGroup]bool)
652 va.VnetsBySvlan = util.NewConcurrentMap()
653 va.VnetsToDelete = make(map[string]bool)
654 va.ServicesToDelete = make(map[string]bool)
655 va.VoltPortVnetsToDelete = make(map[*VoltPortVnet]bool)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530656 go va.Start(context.Background(), TimerCfg{tick: 100 * time.Millisecond}, tickTimer)
657 go va.Start(context.Background(), TimerCfg{tick: time.Duration(GroupExpiryTime) * time.Minute}, pendingPoolTimer)
Naveen Sampath04696f72022-06-13 15:19:14 +0530658 InitEventFuncMapper()
659 db = database.GetDatabase()
660 return &va
661}
662
vinokuma926cb3e2023-03-29 11:41:06 +0530663// GetFlowEventRegister - returs the register based on flow mod type
Naveen Sampath04696f72022-06-13 15:19:14 +0530664func (d *VoltDevice) GetFlowEventRegister(flowModType of.Command) (*util.ConcurrentMap, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530665 switch flowModType {
666 case of.CommandDel:
667 return d.FlowDelEventMap, nil
668 case of.CommandAdd:
669 return d.FlowAddEventMap, nil
670 default:
671 logger.Error(ctx, "Unknown Flow Mod received")
672 }
673 return util.NewConcurrentMap(), errors.New("Unknown Flow Mod")
674}
675
676// RegisterFlowAddEvent to register a flow event.
677func (d *VoltDevice) RegisterFlowAddEvent(cookie string, event *FlowEvent) {
678 logger.Debugw(ctx, "Registered Flow Add Event", log.Fields{"Cookie": cookie, "Event": event})
679 d.FlowAddEventMap.MapLock.Lock()
680 defer d.FlowAddEventMap.MapLock.Unlock()
681 d.FlowAddEventMap.Set(cookie, event)
682}
683
684// RegisterFlowDelEvent to register a flow event.
685func (d *VoltDevice) RegisterFlowDelEvent(cookie string, event *FlowEvent) {
686 logger.Debugw(ctx, "Registered Flow Del Event", log.Fields{"Cookie": cookie, "Event": event})
687 d.FlowDelEventMap.MapLock.Lock()
688 defer d.FlowDelEventMap.MapLock.Unlock()
689 d.FlowDelEventMap.Set(cookie, event)
690}
691
692// UnRegisterFlowEvent to unregister a flow event.
693func (d *VoltDevice) UnRegisterFlowEvent(cookie string, flowModType of.Command) {
694 logger.Debugw(ctx, "UnRegistered Flow Add Event", log.Fields{"Cookie": cookie, "Type": flowModType})
695 flowEventMap, err := d.GetFlowEventRegister(flowModType)
696 if err != nil {
697 logger.Debugw(ctx, "Flow event map does not exists", log.Fields{"flowMod": flowModType, "Error": err})
698 return
699 }
700 flowEventMap.MapLock.Lock()
701 defer flowEventMap.MapLock.Unlock()
702 flowEventMap.Remove(cookie)
703}
704
705// AddIgmpGroups to add Igmp groups.
706func (va *VoltApplication) AddIgmpGroups(numOfGroups uint32) {
707 //TODO: Temp change to resolve group id issue in pOLT
708 //for i := 1; uint32(i) <= numOfGroups; i++ {
709 for i := 2; uint32(i) <= (numOfGroups + 1); i++ {
710 ig := IgmpGroup{}
711 ig.GroupID = uint32(i)
712 va.IgmpGroupIds = append(va.IgmpGroupIds, &ig)
713 }
714}
715
716// GetAvailIgmpGroupID to get id of available igmp group.
717func (va *VoltApplication) GetAvailIgmpGroupID() *IgmpGroup {
718 var ig *IgmpGroup
719 if len(va.IgmpGroupIds) > 0 {
720 ig, va.IgmpGroupIds = va.IgmpGroupIds[0], va.IgmpGroupIds[1:]
721 return ig
722 }
723 return nil
724}
725
726// GetIgmpGroupID to get id of igmp group.
727func (va *VoltApplication) GetIgmpGroupID(gid uint32) (*IgmpGroup, error) {
728 for id, ig := range va.IgmpGroupIds {
729 if ig.GroupID == gid {
730 va.IgmpGroupIds = append(va.IgmpGroupIds[0:id], va.IgmpGroupIds[id+1:]...)
731 return ig, nil
732 }
733 }
734 return nil, errors.New("Group Id Missing")
735}
736
737// PutIgmpGroupID to add id of igmp group.
738func (va *VoltApplication) PutIgmpGroupID(ig *IgmpGroup) {
739 va.IgmpGroupIds = append([]*IgmpGroup{ig}, va.IgmpGroupIds[0:]...)
740}
741
vinokuma926cb3e2023-03-29 11:41:06 +0530742// RestoreUpgradeStatus - gets upgrade/migration status from DB and updates local flags
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530743func (va *VoltApplication) RestoreUpgradeStatus(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530744 Migrate := new(DataMigration)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530745 if err := GetMigrationInfo(cntx, Migrate); err == nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530746 if Migrate.Status == MigrationInProgress {
747 isUpgradeComplete = false
748 return
749 }
750 }
751 isUpgradeComplete = true
752
753 logger.Infow(ctx, "Upgrade Status Restored", log.Fields{"Upgrade Completed": isUpgradeComplete})
754}
755
756// ReadAllFromDb : If we are restarted, learn from the database the current execution
757// stage
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530758func (va *VoltApplication) ReadAllFromDb(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530759 logger.Info(ctx, "Reading the meters from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530760 va.RestoreMetersFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530761 logger.Info(ctx, "Reading the VNETs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530762 va.RestoreVnetsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530763 logger.Info(ctx, "Reading the VPVs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530764 va.RestoreVpvsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530765 logger.Info(ctx, "Reading the Services from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530766 va.RestoreSvcsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530767 logger.Info(ctx, "Reading the MVLANs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530768 va.RestoreMvlansFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530769 logger.Info(ctx, "Reading the IGMP profiles from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530770 va.RestoreIGMPProfilesFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530771 logger.Info(ctx, "Reading the Mcast configs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530772 va.RestoreMcastConfigsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530773 logger.Info(ctx, "Reading the IGMP groups for DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530774 va.RestoreIgmpGroupsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530775 logger.Info(ctx, "Reading Upgrade status from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530776 va.RestoreUpgradeStatus(cntx)
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530777 logger.Info(ctx, "Reading OltFlowService from DB")
778 va.RestoreOltFlowService(cntx)
Akash Sonia8246972023-01-03 10:37:08 +0530779 logger.Info(ctx, "Reading device config from DB")
780 va.RestoreDeviceConfigFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530781 logger.Info(ctx, "Reconciled from DB")
782}
783
vinokuma926cb3e2023-03-29 11:41:06 +0530784// InitStaticConfig to initialize static config.
Naveen Sampath04696f72022-06-13 15:19:14 +0530785func (va *VoltApplication) InitStaticConfig() {
786 va.InitIgmpSrcMac()
787}
788
789// SetVendorID to set vendor id
790func (va *VoltApplication) SetVendorID(vendorID string) {
791 va.vendorID = vendorID
792}
793
794// GetVendorID to get vendor id
795func (va *VoltApplication) GetVendorID() string {
796 return va.vendorID
797}
798
799// SetRebootFlag to set reboot flag
800func (va *VoltApplication) SetRebootFlag(flag bool) {
801 vgcRebooted = flag
802}
803
804// GetUpgradeFlag to get reboot status
805func (va *VoltApplication) GetUpgradeFlag() bool {
806 return isUpgradeComplete
807}
808
809// SetUpgradeFlag to set reboot status
810func (va *VoltApplication) SetUpgradeFlag(flag bool) {
811 isUpgradeComplete = flag
812}
813
814// ------------------------------------------------------------
815// Device related functions
816
817// AddDevice : Add a device and typically the device stores the NNI port on the device
818// The NNI port is used when the packets are emitted towards the network.
819// The outport is selected as the NNI port of the device. Today, we support
820// a single NNI port per OLT. This is true whether the network uses any
821// protection mechanism (LAG, ERPS, etc.). The aggregate of the such protection
822// is represented by a single NNI port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530823func (va *VoltApplication) AddDevice(cntx context.Context, device string, slno, southBoundID string) {
Akash Soni53da2852023-03-15 00:31:31 +0530824 logger.Warnw(ctx, "Received Device Ind: Add", log.Fields{"Device": device, "SrNo": slno, "southBoundID": southBoundID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530825 if _, ok := va.DevicesDisc.Load(device); ok {
826 logger.Warnw(ctx, "Device Exists", log.Fields{"Device": device})
827 }
828 d := NewVoltDevice(device, slno, southBoundID)
829
830 addPort := func(key, value interface{}) bool {
831 portID := key.(uint32)
832 port := value.(*PonPortCfg)
833 va.AggActiveChannelsCountForPonPort(device, portID, port)
834 d.ActiveChannelsPerPon.Store(portID, port)
835 return true
836 }
837 if nbDevice, exists := va.NbDevice.Load(southBoundID); exists {
838 // Pon Ports added before OLT activate.
839 nbDevice.(*NbDevice).PonPorts.Range(addPort)
840 } else {
841 // Check if NbPort exists in DB. VGC restart case.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530842 nbd := va.RestoreNbDeviceFromDb(cntx, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530843 nbd.PonPorts.Range(addPort)
844 }
845 va.DevicesDisc.Store(device, d)
846}
847
848// GetDevice to get a device.
849func (va *VoltApplication) GetDevice(device string) *VoltDevice {
850 if d, ok := va.DevicesDisc.Load(device); ok {
851 return d.(*VoltDevice)
852 }
853 return nil
854}
855
856// DelDevice to delete a device.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530857func (va *VoltApplication) DelDevice(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530858 logger.Warnw(ctx, "Received Device Ind: Delete", log.Fields{"Device": device})
859 if vdIntf, ok := va.DevicesDisc.Load(device); ok {
860 vd := vdIntf.(*VoltDevice)
861 va.DevicesDisc.Delete(device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530862 _ = db.DelAllRoutesForDevice(cntx, device)
863 va.HandleFlowClearFlag(cntx, device, vd.SerialNum, vd.SouthBoundID)
864 _ = db.DelAllGroup(cntx, device)
865 _ = db.DelAllMeter(cntx, device)
866 _ = db.DelAllPorts(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530867 logger.Debugw(ctx, "Device deleted", log.Fields{"Device": device})
868 } else {
869 logger.Warnw(ctx, "Device Doesn't Exist", log.Fields{"Device": device})
870 }
871}
872
873// GetDeviceBySerialNo to get a device by serial number.
874// TODO - Transform this into a MAP instead
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530875func (va *VoltApplication) GetDeviceBySerialNo(slno string) (*VoltDevice, string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530876 var device *VoltDevice
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530877 var deviceID string
Naveen Sampath04696f72022-06-13 15:19:14 +0530878 getserial := func(key interface{}, value interface{}) bool {
879 device = value.(*VoltDevice)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530880 deviceID = key.(string)
Naveen Sampath04696f72022-06-13 15:19:14 +0530881 return device.SerialNum != slno
882 }
883 va.DevicesDisc.Range(getserial)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530884 return device, deviceID
Naveen Sampath04696f72022-06-13 15:19:14 +0530885}
886
887// PortAddInd : This is a PORT add indication coming from the VPAgent, which is essentially
888// a request coming from VOLTHA. The device and identity of the port is provided
889// in this request. Add them to the application for further use
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530890func (va *VoltApplication) PortAddInd(cntx context.Context, device string, id uint32, portName string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530891 logger.Infow(ctx, "Received Port Ind: Add", log.Fields{"Device": device, "Port": portName})
892 va.portLock.Lock()
893 if d := va.GetDevice(device); d != nil {
894 p := d.AddPort(portName, id)
895 va.PortsDisc.Store(portName, p)
896 va.portLock.Unlock()
897 nni, _ := va.GetNniPort(device)
898 if nni == portName {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530899 d.pushFlowsForUnis(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530900 }
901 } else {
902 va.portLock.Unlock()
903 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Add", log.Fields{"Device": device, "Port": portName})
904 }
905}
906
907// PortDelInd : Only the NNI ports are recorded in the device for now. When port delete
908// arrives, only the NNI ports need adjustments.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530909func (va *VoltApplication) PortDelInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530910 logger.Infow(ctx, "Received Port Ind: Delete", log.Fields{"Device": device, "Port": port})
911 if d := va.GetDevice(device); d != nil {
912 p := d.GetPort(port)
913 if p != nil && p.State == PortStateUp {
914 logger.Infow(ctx, "Port state is UP. Trigerring Port Down Ind before deleting", log.Fields{"Port": p})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530915 va.PortDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530916 }
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530917 // if RemoveFlowsOnDisable is flase, then flows will be existing till port delete. Remove the flows now
918 if !va.OltFlowServiceConfig.RemoveFlowsOnDisable {
Akash Sonia8246972023-01-03 10:37:08 +0530919 vpvs, ok := va.VnetsByPort.Load(port)
920 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530921 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
922 } else {
923 for _, vpv := range vpvs.([]*VoltPortVnet) {
924 vpv.VpvLock.Lock()
925 vpv.PortDownInd(cntx, device, port, true)
926 vpv.VpvLock.Unlock()
927 }
928 }
929 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530930 va.portLock.Lock()
931 defer va.portLock.Unlock()
932 d.DelPort(port)
933 if _, ok := va.PortsDisc.Load(port); ok {
934 va.PortsDisc.Delete(port)
935 }
936 } else {
937 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Delete", log.Fields{"Device": device, "Port": port})
938 }
939}
940
vinokuma926cb3e2023-03-29 11:41:06 +0530941// PortUpdateInd Updates port Id incase of ONU movement
Naveen Sampath04696f72022-06-13 15:19:14 +0530942func (va *VoltApplication) PortUpdateInd(device string, portName string, id uint32) {
943 logger.Infow(ctx, "Received Port Ind: Update", log.Fields{"Device": device, "Port": portName})
944 va.portLock.Lock()
945 defer va.portLock.Unlock()
946 if d := va.GetDevice(device); d != nil {
947 vp := d.GetPort(portName)
948 vp.ID = id
949 } else {
950 logger.Warnw(ctx, "Device Not Found", log.Fields{"Device": device, "Port": portName})
951 }
952}
953
954// AddNbPonPort Add pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530955func (va *VoltApplication) AddNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530956 enableMulticastKPI bool, portAlarmProfileID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530957 var nbd *NbDevice
958 nbDevice, ok := va.NbDevice.Load(oltSbID)
959
960 if !ok {
961 nbd = NewNbDevice()
962 nbd.SouthBoundID = oltSbID
963 } else {
964 nbd = nbDevice.(*NbDevice)
965 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530966 port := nbd.AddPortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530967
968 // Add this port to voltDevice
969 addPort := func(key, value interface{}) bool {
970 voltDevice := value.(*VoltDevice)
971 if oltSbID == voltDevice.SouthBoundID {
972 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); !exists {
973 voltDevice.ActiveChannelsPerPon.Store(portID, port)
974 }
975 return false
976 }
977 return true
978 }
979 va.DevicesDisc.Range(addPort)
980 va.NbDevice.Store(oltSbID, nbd)
981
982 return nil
983}
984
985// UpdateNbPonPort update pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530986func (va *VoltApplication) UpdateNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530987 var nbd *NbDevice
988 nbDevice, ok := va.NbDevice.Load(oltSbID)
989
990 if !ok {
991 logger.Errorw(ctx, "Device-doesn't-exists", log.Fields{"deviceID": oltSbID})
992 return fmt.Errorf("Device-doesn't-exists-%v", oltSbID)
993 }
994 nbd = nbDevice.(*NbDevice)
995
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530996 port := nbd.UpdatePortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530997 if port == nil {
998 return fmt.Errorf("Port-doesn't-exists-%v", portID)
999 }
1000 va.NbDevice.Store(oltSbID, nbd)
1001
1002 // Add this port to voltDevice
1003 updPort := func(key, value interface{}) bool {
1004 voltDevice := value.(*VoltDevice)
1005 if oltSbID == voltDevice.SouthBoundID {
1006 voltDevice.ActiveChannelCountLock.Lock()
1007 if p, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
1008 oldPort := p.(*PonPortCfg)
1009 if port.MaxActiveChannels != 0 {
1010 oldPort.MaxActiveChannels = port.MaxActiveChannels
1011 oldPort.EnableMulticastKPI = port.EnableMulticastKPI
1012 voltDevice.ActiveChannelsPerPon.Store(portID, oldPort)
1013 }
1014 }
1015 voltDevice.ActiveChannelCountLock.Unlock()
1016 return false
1017 }
1018 return true
1019 }
1020 va.DevicesDisc.Range(updPort)
1021
1022 return nil
1023}
1024
1025// DeleteNbPonPort Delete pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301026func (va *VoltApplication) DeleteNbPonPort(cntx context.Context, oltSbID string, portID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301027 nbDevice, ok := va.NbDevice.Load(oltSbID)
1028 if ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301029 nbDevice.(*NbDevice).DeletePortFromNbDevice(cntx, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301030 va.NbDevice.Store(oltSbID, nbDevice.(*NbDevice))
1031 } else {
1032 logger.Warnw(ctx, "Delete pon received for unknown device", log.Fields{"oltSbID": oltSbID})
1033 return nil
1034 }
1035 // Delete this port from voltDevice
1036 delPort := func(key, value interface{}) bool {
1037 voltDevice := value.(*VoltDevice)
1038 if oltSbID == voltDevice.SouthBoundID {
1039 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
1040 voltDevice.ActiveChannelsPerPon.Delete(portID)
1041 }
1042 return false
1043 }
1044 return true
1045 }
1046 va.DevicesDisc.Range(delPort)
1047 return nil
1048}
1049
1050// GetNniPort : Get the NNI port for a device. Called from different other applications
1051// as a port to match or destination for a packet out. The VOLT application
1052// is written with the assumption that there is a single NNI port. The OLT
1053// device is responsible for translating the combination of VLAN and the
1054// NNI port ID to identify possibly a single physical port or a logical
1055// port which is a result of protection methods applied.
1056func (va *VoltApplication) GetNniPort(device string) (string, error) {
1057 va.portLock.Lock()
1058 defer va.portLock.Unlock()
1059 d, ok := va.DevicesDisc.Load(device)
1060 if !ok {
1061 return "", errors.New("Device Doesn't Exist")
1062 }
1063 return d.(*VoltDevice).NniPort, nil
1064}
1065
1066// NniDownInd process for Nni down indication.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301067func (va *VoltApplication) NniDownInd(cntx context.Context, deviceID string, devSrNo string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301068 logger.Debugw(ctx, "NNI Down Ind", log.Fields{"device": devSrNo})
1069
1070 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1071 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301072 mvProfile.removeIgmpMcastFlows(cntx, devSrNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301073 return true
1074 }
1075 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1076
1077 //Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301078 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301079}
1080
1081// DeviceUpInd changes device state to up.
1082func (va *VoltApplication) DeviceUpInd(device string) {
1083 logger.Warnw(ctx, "Received Device Ind: UP", log.Fields{"Device": device})
1084 if d := va.GetDevice(device); d != nil {
1085 d.State = controller.DeviceStateUP
1086 } else {
1087 logger.Errorw(ctx, "Ignoring Device indication: UP. Device Missing", log.Fields{"Device": device})
1088 }
1089}
1090
1091// DeviceDownInd changes device state to down.
1092func (va *VoltApplication) DeviceDownInd(device string) {
1093 logger.Warnw(ctx, "Received Device Ind: DOWN", log.Fields{"Device": device})
1094 if d := va.GetDevice(device); d != nil {
1095 d.State = controller.DeviceStateDOWN
1096 } else {
1097 logger.Errorw(ctx, "Ignoring Device indication: DOWN. Device Missing", log.Fields{"Device": device})
1098 }
1099}
1100
1101// DeviceRebootInd process for handling flow clear flag for device reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301102func (va *VoltApplication) DeviceRebootInd(cntx context.Context, device string, serialNum string, southBoundID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301103 logger.Warnw(ctx, "Received Device Ind: Reboot", log.Fields{"Device": device, "SerialNumber": serialNum})
1104
1105 if d := va.GetDevice(device); d != nil {
1106 if d.State == controller.DeviceStateREBOOTED {
1107 logger.Warnw(ctx, "Ignoring Device Ind: Reboot, Device already in Reboot state", log.Fields{"Device": device, "SerialNumber": serialNum, "State": d.State})
1108 return
1109 }
1110 d.State = controller.DeviceStateREBOOTED
1111 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301112 va.HandleFlowClearFlag(cntx, device, serialNum, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301113}
1114
1115// DeviceDisableInd handles device deactivation process
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301116func (va *VoltApplication) DeviceDisableInd(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301117 logger.Warnw(ctx, "Received Device Ind: Disable", log.Fields{"Device": device})
1118
1119 d := va.GetDevice(device)
1120 if d == nil {
1121 logger.Errorw(ctx, "Ignoring Device indication: DISABLED. Device Missing", log.Fields{"Device": device})
1122 return
1123 }
1124
1125 d.State = controller.DeviceStateDISABLED
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301126 va.HandleFlowClearFlag(cntx, device, d.SerialNum, d.SouthBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301127}
1128
1129// ProcessIgmpDSFlowForMvlan for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301130func (va *VoltApplication) ProcessIgmpDSFlowForMvlan(cntx context.Context, d *VoltDevice, mvp *MvlanProfile, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301131 logger.Debugw(ctx, "Process IGMP DS Flows for MVlan", log.Fields{"device": d.Name, "Mvlan": mvp.Mvlan, "addFlow": addFlow})
1132 portState := false
1133 p := d.GetPort(d.NniPort)
1134 if p != nil && p.State == PortStateUp {
1135 portState = true
1136 }
1137
1138 if addFlow {
1139 if portState {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301140 mvp.pushIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301141 }
1142 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301143 mvp.removeIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301144 }
1145}
1146
1147// ProcessIgmpDSFlowForDevice for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301148func (va *VoltApplication) ProcessIgmpDSFlowForDevice(cntx context.Context, d *VoltDevice, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301149 logger.Debugw(ctx, "Process IGMP DS Flows for device", log.Fields{"device": d.Name, "addFlow": addFlow})
1150
1151 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1152 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301153 va.ProcessIgmpDSFlowForMvlan(cntx, d, mvProfile, addFlow)
Naveen Sampath04696f72022-06-13 15:19:14 +05301154 return true
1155 }
1156 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1157}
1158
1159// GetDeviceFromPort : This is suitable only for access ports as their naming convention
1160// makes them unique across all the OLTs. This must be called with
1161// port name that is an access port. Currently called from VNETs, attached
1162// only to access ports, and the services which are also attached only
1163// to access ports
1164func (va *VoltApplication) GetDeviceFromPort(port string) (*VoltDevice, error) {
1165 va.portLock.Lock()
1166 defer va.portLock.Unlock()
1167 var err error
1168 err = nil
1169 p, ok := va.PortsDisc.Load(port)
1170 if !ok {
1171 return nil, errorCodes.ErrPortNotFound
1172 }
1173 d := va.GetDevice(p.(*VoltPort).Device)
1174 if d == nil {
1175 err = errorCodes.ErrDeviceNotFound
1176 }
1177 return d, err
1178}
1179
1180// GetPortID : This too applies only to access ports. The ports can be indexed
1181// purely by their names without the device forming part of the key
1182func (va *VoltApplication) GetPortID(port string) (uint32, error) {
1183 va.portLock.Lock()
1184 defer va.portLock.Unlock()
1185 p, ok := va.PortsDisc.Load(port)
1186 if !ok {
1187 return 0, errorCodes.ErrPortNotFound
1188 }
1189 return p.(*VoltPort).ID, nil
1190}
1191
1192// GetPortName : This too applies only to access ports. The ports can be indexed
1193// purely by their names without the device forming part of the key
1194func (va *VoltApplication) GetPortName(port uint32) (string, error) {
1195 va.portLock.Lock()
1196 defer va.portLock.Unlock()
1197 var portName string
1198 va.PortsDisc.Range(func(key interface{}, value interface{}) bool {
1199 portInfo := value.(*VoltPort)
1200 if portInfo.ID == port {
1201 portName = portInfo.Name
1202 return false
1203 }
1204 return true
1205 })
1206 return portName, nil
1207}
1208
1209// GetPonFromUniPort to get Pon info from UniPort
1210func (va *VoltApplication) GetPonFromUniPort(port string) (string, error) {
1211 uniPortID, err := va.GetPortID(port)
1212 if err == nil {
1213 ponPortID := (uniPortID & 0x0FF00000) >> 20 //pon(8) + onu(8) + uni(12)
1214 return strconv.FormatUint(uint64(ponPortID), 10), nil
1215 }
1216 return "", err
1217}
1218
1219// GetPortState : This too applies only to access ports. The ports can be indexed
1220// purely by their names without the device forming part of the key
1221func (va *VoltApplication) GetPortState(port string) (PortState, error) {
1222 va.portLock.Lock()
1223 defer va.portLock.Unlock()
1224 p, ok := va.PortsDisc.Load(port)
1225 if !ok {
1226 return 0, errors.New("Port not configured")
1227 }
1228 return p.(*VoltPort).State, nil
1229}
1230
1231// GetIcmpv6Receivers to get Icmp v6 receivers
1232func (va *VoltApplication) GetIcmpv6Receivers(device string) []uint32 {
1233 var receiverList []uint32
1234 receivers, _ := va.Icmpv6Receivers.Load(device)
1235 if receivers != nil {
1236 receiverList = receivers.([]uint32)
1237 }
1238 return receiverList
1239}
1240
1241// AddIcmpv6Receivers to add Icmp v6 receivers
1242func (va *VoltApplication) AddIcmpv6Receivers(device string, portID uint32) []uint32 {
1243 var receiverList []uint32
1244 receivers, _ := va.Icmpv6Receivers.Load(device)
1245 if receivers != nil {
1246 receiverList = receivers.([]uint32)
1247 }
1248 receiverList = append(receiverList, portID)
1249 va.Icmpv6Receivers.Store(device, receiverList)
1250 logger.Debugw(ctx, "Receivers after addition", log.Fields{"Receivers": receiverList})
1251 return receiverList
1252}
1253
1254// DelIcmpv6Receivers to delete Icmp v6 receievers
1255func (va *VoltApplication) DelIcmpv6Receivers(device string, portID uint32) []uint32 {
1256 var receiverList []uint32
1257 receivers, _ := va.Icmpv6Receivers.Load(device)
1258 if receivers != nil {
1259 receiverList = receivers.([]uint32)
1260 }
1261 for i, port := range receiverList {
1262 if port == portID {
1263 receiverList = append(receiverList[0:i], receiverList[i+1:]...)
1264 va.Icmpv6Receivers.Store(device, receiverList)
1265 break
1266 }
1267 }
1268 logger.Debugw(ctx, "Receivers After deletion", log.Fields{"Receivers": receiverList})
1269 return receiverList
1270}
1271
1272// ProcessDevFlowForDevice - Process DS ICMPv6 & ARP flow for provided device and vnet profile
1273// device - Device Obj
1274// vnet - vnet profile name
1275// enabled - vlan enabled/disabled - based on the status, the flow shall be added/removed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301276func (va *VoltApplication) ProcessDevFlowForDevice(cntx context.Context, device *VoltDevice, vnet *VoltVnet, enabled bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301277 _, applied := device.ConfiguredVlanForDeviceFlows.Get(VnetKey(vnet.SVlan, vnet.CVlan, 0))
1278 if enabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301279 va.PushDevFlowForVlan(cntx, vnet)
Naveen Sampath04696f72022-06-13 15:19:14 +05301280 } else if !enabled && applied {
1281 //va.DeleteDevFlowForVlan(vnet)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301282 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, device.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301283 }
1284}
1285
vinokuma926cb3e2023-03-29 11:41:06 +05301286// NniVlanIndToIgmp - Trigger receiver up indication to all ports with igmp enabled
1287// and has the provided mvlan
Naveen Sampath04696f72022-06-13 15:19:14 +05301288func (va *VoltApplication) NniVlanIndToIgmp(device *VoltDevice, mvp *MvlanProfile) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301289 logger.Infow(ctx, "Sending Igmp Receiver UP indication for all Services", log.Fields{"Vlan": mvp.Mvlan})
1290
vinokuma926cb3e2023-03-29 11:41:06 +05301291 // Trigger nni indication for receiver only for first time
Naveen Sampath04696f72022-06-13 15:19:14 +05301292 if device.IgmpDsFlowAppliedForMvlan[uint16(mvp.Mvlan)] {
1293 return
1294 }
1295 device.Ports.Range(func(key, value interface{}) bool {
1296 port := key.(string)
1297
1298 if state, _ := va.GetPortState(port); state == PortStateUp {
1299 vpvs, _ := va.VnetsByPort.Load(port)
1300 if vpvs == nil {
1301 return true
1302 }
1303 for _, vpv := range vpvs.([]*VoltPortVnet) {
vinokuma926cb3e2023-03-29 11:41:06 +05301304 // Send indication only for subscribers with the received mvlan profile
Naveen Sampath04696f72022-06-13 15:19:14 +05301305 if vpv.IgmpEnabled && vpv.MvlanProfileName == mvp.Name {
1306 vpv.services.Range(ReceiverUpInd)
1307 }
1308 }
1309 }
1310 return true
1311 })
1312}
1313
1314// PortUpInd :
1315// -----------------------------------------------------------------------
1316// Port status change handling
1317// ----------------------------------------------------------------------
1318// Port UP indication is passed to all services associated with the port
1319// so that the services can configure flows applicable when the port goes
1320// up from down state
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301321func (va *VoltApplication) PortUpInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301322 d := va.GetDevice(device)
1323
1324 if d == nil {
1325 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: UP", log.Fields{"Device": device, "Port": port})
1326 return
1327 }
1328
vinokuma926cb3e2023-03-29 11:41:06 +05301329 // Fixme: If Port Update Comes in large numbers, this will result in slow update per device
Naveen Sampath04696f72022-06-13 15:19:14 +05301330 va.portLock.Lock()
1331 // Do not defer the port mutex unlock here
1332 // Some of the following func calls needs the port lock, so defering the lock here
1333 // may lead to dead-lock
1334 p := d.GetPort(port)
1335
1336 if p == nil {
1337 logger.Infow(ctx, "Ignoring Port Ind: UP, Port doesnt exist", log.Fields{"Device": device, "PortName": port, "PortId": p})
1338 va.portLock.Unlock()
1339 return
1340 }
1341 p.State = PortStateUp
1342 va.portLock.Unlock()
1343
1344 logger.Infow(ctx, "Received SouthBound Port Ind: UP", log.Fields{"Device": device, "PortName": port, "PortId": p.ID})
1345 if p.Type == VoltPortTypeNni {
Naveen Sampath04696f72022-06-13 15:19:14 +05301346 logger.Warnw(ctx, "Received NNI Port Ind: UP", log.Fields{"Device": device, "PortName": port, "PortId": p.ID})
1347 //va.PushDevFlowForDevice(d)
1348 //Build Igmp TrapFlowRule
1349 //va.ProcessIgmpDSFlowForDevice(d, true)
1350 }
1351 vpvs, ok := va.VnetsByPort.Load(port)
1352 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
1353 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
1354 //msgbus.ProcessPortInd(msgbus.PortUp, d.SerialNum, p.Name, false, getServiceList(port))
1355 return
1356 }
1357
vinokuma926cb3e2023-03-29 11:41:06 +05301358 // If NNI port is not UP, do not push Flows
Naveen Sampath04696f72022-06-13 15:19:14 +05301359 if d.NniPort == "" {
1360 logger.Warnw(ctx, "NNI port not UP. Not sending Port UP Ind for VPVs", log.Fields{"NNI": d.NniPort})
1361 return
1362 }
1363
1364 vpvList := vpvs.([]*VoltPortVnet)
1365 if vpvList[0].PonPort != 0xFF && vpvList[0].PonPort != p.PonPort {
1366 logger.Errorw(ctx, "UNI port discovered on wrong PON Port. Dropping Port Indication", log.Fields{"Device": device, "Port": port, "DetectedPon": p.PonPort, "ExpectedPon": vpvList[0].PonPort})
1367
vinokuma926cb3e2023-03-29 11:41:06 +05301368 // Remove the flow (if any) which are already installed - Valid for PON switching when VGC pod is DOWN
Naveen Sampath04696f72022-06-13 15:19:14 +05301369 for _, vpv := range vpvs.([]*VoltPortVnet) {
1370 vpv.VpvLock.Lock()
1371 logger.Warnw(ctx, "Removing existing VPVs/Services flows for for Subscriber: UNI Detected on wrong PON", log.Fields{"Port": vpv.Port, "Vnet": vpv.VnetName})
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301372 vpv.PortDownInd(cntx, device, port, false)
Naveen Sampath04696f72022-06-13 15:19:14 +05301373 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301374 va.ReceiverDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +05301375 }
1376 vpv.VpvLock.Unlock()
1377 }
1378 return
1379 }
1380
Naveen Sampath04696f72022-06-13 15:19:14 +05301381 for _, vpv := range vpvs.([]*VoltPortVnet) {
1382 vpv.VpvLock.Lock()
vinokuma926cb3e2023-03-29 11:41:06 +05301383 // If no service is activated drop the portUpInd
Tinoj Josephec742f62022-09-29 19:11:10 +05301384 if vpv.IsServiceActivated(cntx) {
vinokuma926cb3e2023-03-29 11:41:06 +05301385 // Do not trigger indication for the vpv which is already removed from vpv list as
Tinoj Josephec742f62022-09-29 19:11:10 +05301386 // part of service delete (during the lock wait duration)
1387 // In that case, the services associated wil be zero
1388 if vpv.servicesCount.Load() != 0 {
1389 vpv.PortUpInd(cntx, d, port)
1390 }
1391 } else {
1392 // Service not activated, still attach device to service
1393 vpv.setDevice(d.Name)
Naveen Sampath04696f72022-06-13 15:19:14 +05301394 }
1395 vpv.VpvLock.Unlock()
1396 }
1397 // At the end of processing inform the other entities that
1398 // are interested in the events
1399}
1400
1401/*
1402func getServiceList(port string) map[string]bool {
1403 serviceList := make(map[string]bool)
1404
1405 getServiceNames := func(key interface{}, value interface{}) bool {
1406 serviceList[key.(string)] = value.(*VoltService).DsHSIAFlowsApplied
1407 return true
1408 }
1409
1410 if vpvs, _ := GetApplication().VnetsByPort.Load(port); vpvs != nil {
1411 vpvList := vpvs.([]*VoltPortVnet)
1412 for _, vpv := range vpvList {
1413 vpv.services.Range(getServiceNames)
1414 }
1415 }
1416 return serviceList
1417
1418}*/
1419
vinokuma926cb3e2023-03-29 11:41:06 +05301420// ReceiverUpInd - Send receiver up indication for service with Igmp enabled
Naveen Sampath04696f72022-06-13 15:19:14 +05301421func ReceiverUpInd(key, value interface{}) bool {
1422 svc := value.(*VoltService)
1423 var vlan of.VlanType
1424
1425 if !svc.IPAssigned() {
1426 logger.Infow(ctx, "IP Not assigned, skipping general query", log.Fields{"Service": svc})
1427 return false
1428 }
1429
vinokuma926cb3e2023-03-29 11:41:06 +05301430 // Send port up indication to igmp only for service with igmp enabled
Naveen Sampath04696f72022-06-13 15:19:14 +05301431 if svc.IgmpEnabled {
1432 if svc.VlanControl == ONUCVlan || svc.VlanControl == ONUCVlanOLTSVlan {
1433 vlan = svc.CVlan
1434 } else {
1435 vlan = svc.UniVlan
1436 }
1437 if device, _ := GetApplication().GetDeviceFromPort(svc.Port); device != nil {
1438 GetApplication().ReceiverUpInd(device.Name, svc.Port, svc.MvlanProfileName, vlan, svc.Pbits)
1439 }
1440 return false
1441 }
1442 return true
1443}
1444
1445// PortDownInd : Port down indication is passed on to the services so that the services
1446// can make changes at this transition.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301447func (va *VoltApplication) PortDownInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301448 logger.Infow(ctx, "Received SouthBound Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1449 d := va.GetDevice(device)
1450
1451 if d == nil {
1452 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1453 return
1454 }
vinokuma926cb3e2023-03-29 11:41:06 +05301455 // Fixme: If Port Update Comes in large numbers, this will result in slow update per device
Naveen Sampath04696f72022-06-13 15:19:14 +05301456 va.portLock.Lock()
1457 // Do not defer the port mutex unlock here
1458 // Some of the following func calls needs the port lock, so defering the lock here
1459 // may lead to dead-lock
1460 p := d.GetPort(port)
1461 if p == nil {
1462 logger.Infow(ctx, "Ignoring Port Ind: Down, Port doesnt exist", log.Fields{"Device": device, "PortName": port, "PortId": p})
1463 va.portLock.Unlock()
1464 return
1465 }
1466 p.State = PortStateDown
1467 va.portLock.Unlock()
1468
1469 if d.State == controller.DeviceStateREBOOTED {
1470 logger.Infow(ctx, "Ignoring Port Ind: Down, Device has been Rebooted", log.Fields{"Device": device, "PortName": port, "PortId": p})
1471 return
1472 }
1473
1474 if p.Type == VoltPortTypeNni {
1475 logger.Warnw(ctx, "Received NNI Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301476 va.DeleteDevFlowForDevice(cntx, d)
1477 va.NniDownInd(cntx, device, d.SerialNum)
1478 va.RemovePendingGroups(cntx, device, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301479 }
1480 vpvs, ok := va.VnetsByPort.Load(port)
1481 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
1482 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
1483 //msgbus.ProcessPortInd(msgbus.PortDown, d.SerialNum, p.Name, false, getServiceList(port))
1484 return
1485 }
Akash Sonia8246972023-01-03 10:37:08 +05301486
Naveen Sampath04696f72022-06-13 15:19:14 +05301487 for _, vpv := range vpvs.([]*VoltPortVnet) {
1488 vpv.VpvLock.Lock()
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301489 vpv.PortDownInd(cntx, device, port, false)
Naveen Sampath04696f72022-06-13 15:19:14 +05301490 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301491 va.ReceiverDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +05301492 }
1493 vpv.VpvLock.Unlock()
1494 }
1495}
1496
1497// PacketInInd :
1498// -----------------------------------------------------------------------
1499// PacketIn Processing
1500// Packet In Indication processing. It arrives with the identities of
1501// the device and port on which the packet is received. At first, the
1502// packet is decoded and the right processor is called. Currently, we
1503// plan to support only DHCP and IGMP. In future, we can add more
1504// capabilities as needed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301505func (va *VoltApplication) PacketInInd(cntx context.Context, device string, port string, pkt []byte) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301506 // Decode the incoming packet
1507 packetSide := US
1508 if strings.Contains(port, NNI) {
1509 packetSide = DS
1510 }
1511
1512 logger.Debugw(ctx, "Received a Packet-In Indication", log.Fields{"Device": device, "Port": port})
1513
1514 gopkt := gopacket.NewPacket(pkt, layers.LayerTypeEthernet, gopacket.Default)
1515
1516 var dot1qFound = false
1517 for _, l := range gopkt.Layers() {
1518 if l.LayerType() == layers.LayerTypeDot1Q {
1519 dot1qFound = true
1520 break
1521 }
1522 }
1523
1524 if !dot1qFound {
1525 logger.Debugw(ctx, "Ignoring Received Packet-In Indication without Dot1Q Header",
1526 log.Fields{"Device": device, "Port": port})
1527 return
1528 }
1529
1530 logger.Debugw(ctx, "Received Southbound Packet In", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1531
1532 // Classify the packet into packet types that we support
1533 // The supported types are DHCP and IGMP. The DHCP packet is
1534 // identified by matching the L4 protocol to UDP. The IGMP packet
1535 // is identified by matching L3 protocol to IGMP
1536 arpl := gopkt.Layer(layers.LayerTypeARP)
1537 if arpl != nil {
1538 if callBack, ok := PacketHandlers[ARP]; 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, "ARP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1542 }
1543 return
1544 }
1545 ipv4l := gopkt.Layer(layers.LayerTypeIPv4)
1546 if ipv4l != nil {
1547 ip := ipv4l.(*layers.IPv4)
1548
1549 if ip.Protocol == layers.IPProtocolUDP {
1550 logger.Debugw(ctx, "Received Southbound UDP ipv4 packet in", log.Fields{"StreamSide": packetSide})
1551 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv4)
1552 if dhcpl != nil {
1553 if callBack, ok := PacketHandlers[DHCPv4]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301554 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301555 } else {
1556 logger.Debugw(ctx, "DHCPv4 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1557 }
1558 }
1559 } else if ip.Protocol == layers.IPProtocolIGMP {
1560 logger.Debugw(ctx, "Received Southbound IGMP packet in", log.Fields{"StreamSide": packetSide})
1561 if callBack, ok := PacketHandlers[IGMP]; 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, "IGMP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1565 }
1566 }
1567 return
1568 }
1569 ipv6l := gopkt.Layer(layers.LayerTypeIPv6)
1570 if ipv6l != nil {
1571 ip := ipv6l.(*layers.IPv6)
1572 if ip.NextHeader == layers.IPProtocolUDP {
1573 logger.Debug(ctx, "Received Southbound UDP ipv6 packet in")
1574 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv6)
1575 if dhcpl != nil {
1576 if callBack, ok := PacketHandlers[DHCPv6]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301577 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301578 } else {
1579 logger.Debugw(ctx, "DHCPv6 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1580 }
1581 }
1582 }
1583 return
1584 }
1585
1586 pppoel := gopkt.Layer(layers.LayerTypePPPoE)
1587 if pppoel != nil {
1588 logger.Debugw(ctx, "Received Southbound PPPoE packet in", log.Fields{"StreamSide": packetSide})
1589 if callBack, ok := PacketHandlers[PPPOE]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301590 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301591 } else {
1592 logger.Debugw(ctx, "PPPoE handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1593 }
1594 }
1595}
1596
1597// GetVlans : This utility gets the VLANs from the packet. The VLANs are
1598// used to identify the right service that must process the incoming
1599// packet
1600func GetVlans(pkt gopacket.Packet) []of.VlanType {
1601 var vlans []of.VlanType
1602 for _, l := range pkt.Layers() {
1603 if l.LayerType() == layers.LayerTypeDot1Q {
1604 q, ok := l.(*layers.Dot1Q)
1605 if ok {
1606 vlans = append(vlans, of.VlanType(q.VLANIdentifier))
1607 }
1608 }
1609 }
1610 return vlans
1611}
1612
1613// GetPriority to get priority
1614func GetPriority(pkt gopacket.Packet) uint8 {
1615 for _, l := range pkt.Layers() {
1616 if l.LayerType() == layers.LayerTypeDot1Q {
1617 q, ok := l.(*layers.Dot1Q)
1618 if ok {
1619 return q.Priority
1620 }
1621 }
1622 }
1623 return PriorityNone
1624}
1625
1626// HandleFlowClearFlag to handle flow clear flag during reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301627func (va *VoltApplication) HandleFlowClearFlag(cntx context.Context, deviceID string, serialNum, southBoundID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301628 logger.Warnw(ctx, "Clear All flags for Device", log.Fields{"Device": deviceID, "SerialNum": serialNum, "SBID": southBoundID})
1629 dev, ok := va.DevicesDisc.Load(deviceID)
1630 if ok && dev != nil {
1631 logger.Infow(ctx, "Clear Flags for device", log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1632 dev.(*VoltDevice).icmpv6GroupAdded = false
1633 logger.Infow(ctx, "Clearing DS Icmpv6 Map",
1634 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1635 dev.(*VoltDevice).ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
1636 logger.Infow(ctx, "Clearing DS IGMP Map",
1637 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1638 for k := range dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan {
1639 delete(dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan, k)
1640 }
vinokuma926cb3e2023-03-29 11:41:06 +05301641 // Delete group 1 - ICMPv6/ARP group
Naveen Sampath04696f72022-06-13 15:19:14 +05301642 if err := ProcessIcmpv6McGroup(deviceID, true); err != nil {
1643 logger.Errorw(ctx, "ProcessIcmpv6McGroup failed", log.Fields{"Device": deviceID, "Error": err})
1644 }
1645 } else {
1646 logger.Warnw(ctx, "VoltDevice not found for device ", log.Fields{"deviceID": deviceID})
1647 }
1648
1649 getVpvs := func(key interface{}, value interface{}) bool {
1650 vpvs := value.([]*VoltPortVnet)
1651 for _, vpv := range vpvs {
1652 if vpv.Device == deviceID {
1653 logger.Infow(ctx, "Clear Flags for vpv",
1654 log.Fields{"device": vpv.Device, "port": vpv.Port,
1655 "svlan": vpv.SVlan, "cvlan": vpv.CVlan, "univlan": vpv.UniVlan})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301656 vpv.ClearAllServiceFlags(cntx)
1657 vpv.ClearAllVpvFlags(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301658
1659 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301660 va.ReceiverDownInd(cntx, vpv.Device, vpv.Port)
vinokuma926cb3e2023-03-29 11:41:06 +05301661 // Also clear service igmp stats
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301662 vpv.ClearServiceCounters(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301663 }
1664 }
1665 }
1666 return true
1667 }
1668 va.VnetsByPort.Range(getVpvs)
1669
vinokuma926cb3e2023-03-29 11:41:06 +05301670 // Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301671 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301672
1673 logger.Warnw(ctx, "All flags cleared for device", log.Fields{"Device": deviceID})
1674
vinokuma926cb3e2023-03-29 11:41:06 +05301675 // Reset pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301676 va.RemovePendingGroups(cntx, deviceID, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301677
vinokuma926cb3e2023-03-29 11:41:06 +05301678 // Process all Migrate Service Request - force udpate all profiles since resources are already cleaned up
Naveen Sampath04696f72022-06-13 15:19:14 +05301679 if dev != nil {
1680 triggerForceUpdate := func(key, value interface{}) bool {
1681 msrList := value.(*util.ConcurrentMap)
1682 forceUpdateServices := func(key, value interface{}) bool {
1683 msr := value.(*MigrateServicesRequest)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301684 forceUpdateAllServices(cntx, msr)
Naveen Sampath04696f72022-06-13 15:19:14 +05301685 return true
1686 }
1687 msrList.Range(forceUpdateServices)
1688 return true
1689 }
1690 dev.(*VoltDevice).MigratingServices.Range(triggerForceUpdate)
1691 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301692 va.FetchAndProcessAllMigrateServicesReq(cntx, deviceID, forceUpdateAllServices)
Naveen Sampath04696f72022-06-13 15:19:14 +05301693 }
1694}
1695
vinokuma926cb3e2023-03-29 11:41:06 +05301696// GetPonPortIDFromUNIPort to get pon port id from uni port
Naveen Sampath04696f72022-06-13 15:19:14 +05301697func GetPonPortIDFromUNIPort(uniPortID uint32) uint32 {
1698 ponPortID := (uniPortID & 0x0FF00000) >> 20
1699 return ponPortID
1700}
1701
vinokuma926cb3e2023-03-29 11:41:06 +05301702// ProcessFlowModResultIndication - Processes Flow mod operation indications from controller
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301703func (va *VoltApplication) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301704 d := va.GetDevice(flowStatus.Device)
1705 if d == nil {
1706 logger.Errorw(ctx, "Dropping Flow Mod Indication. Device not found", log.Fields{"Cookie": flowStatus.Cookie, "Device": flowStatus.Device})
1707 return
1708 }
1709
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301710 cookieExists := ExecuteFlowEvent(cntx, d, flowStatus.Cookie, flowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +05301711
1712 if flowStatus.Flow != nil {
1713 flowAdd := (flowStatus.FlowModType == of.CommandAdd)
1714 if !cookieExists && !isFlowStatusSuccess(flowStatus.Status, flowAdd) {
1715 pushFlowFailureNotif(flowStatus)
1716 }
1717 }
1718}
1719
1720func pushFlowFailureNotif(flowStatus intf.FlowStatus) {
1721 subFlow := flowStatus.Flow
1722 cookie := subFlow.Cookie
1723 uniPort := cookie >> 16 & 0xFFFFFFFF
1724 logger.Errorw(ctx, "Flow Failure Notification", log.Fields{"uniPort": uniPort, "Cookie": cookie})
Naveen Sampath04696f72022-06-13 15:19:14 +05301725}
1726
vinokuma926cb3e2023-03-29 11:41:06 +05301727// UpdateMvlanProfilesForDevice to update mvlan profile for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301728func (va *VoltApplication) UpdateMvlanProfilesForDevice(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301729 checkAndAddMvlanUpdateTask := func(key, value interface{}) bool {
1730 mvp := value.(*MvlanProfile)
1731 if mvp.IsUpdateInProgressForDevice(device) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301732 mvp.UpdateProfile(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301733 }
1734 return true
1735 }
1736 va.MvlanProfilesByName.Range(checkAndAddMvlanUpdateTask)
1737}
1738
1739// TaskInfo structure that is used to store the task Info.
1740type TaskInfo struct {
1741 ID string
1742 Name string
1743 Timestamp string
1744}
1745
1746// GetTaskList to get task list information.
1747func (va *VoltApplication) GetTaskList(device string) map[int]*TaskInfo {
1748 taskList := cntlr.GetController().GetTaskList(device)
1749 taskMap := make(map[int]*TaskInfo)
1750 for i, task := range taskList {
1751 taskID := strconv.Itoa(int(task.TaskID()))
1752 name := task.Name()
1753 timestamp := task.Timestamp()
1754 taskInfo := &TaskInfo{ID: taskID, Name: name, Timestamp: timestamp}
1755 taskMap[i] = taskInfo
1756 }
1757 return taskMap
1758}
1759
1760// UpdateDeviceSerialNumberList to update the device serial number list after device serial number is updated for vnet and mvlan
1761func (va *VoltApplication) UpdateDeviceSerialNumberList(oldOltSlNo string, newOltSlNo string) {
Tinoj Joseph50d722c2022-12-06 22:53:22 +05301762 voltDevice, _ := va.GetDeviceBySerialNo(oldOltSlNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301763
1764 if voltDevice != nil {
1765 // Device is present with old serial number ID
1766 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 +05301767 } else {
1768 logger.Infow(ctx, "No device present with old serial number", log.Fields{"Serial Number": oldOltSlNo})
Naveen Sampath04696f72022-06-13 15:19:14 +05301769 // Add Serial Number to Blocked Devices List.
1770 cntlr.GetController().AddBlockedDevices(oldOltSlNo)
1771 cntlr.GetController().AddBlockedDevices(newOltSlNo)
1772
1773 updateSlNoForVnet := func(key, value interface{}) bool {
1774 vnet := value.(*VoltVnet)
1775 for i, deviceSlNo := range vnet.VnetConfig.DevicesList {
1776 if deviceSlNo == oldOltSlNo {
1777 vnet.VnetConfig.DevicesList[i] = newOltSlNo
1778 logger.Infow(ctx, "device serial number updated for vnet profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1779 break
1780 }
1781 }
1782 return true
1783 }
1784
1785 updateSlNoforMvlan := func(key interface{}, value interface{}) bool {
1786 mvProfile := value.(*MvlanProfile)
1787 for deviceSlNo := range mvProfile.DevicesList {
1788 if deviceSlNo == oldOltSlNo {
1789 mvProfile.DevicesList[newOltSlNo] = mvProfile.DevicesList[oldOltSlNo]
1790 delete(mvProfile.DevicesList, oldOltSlNo)
1791 logger.Infow(ctx, "device serial number updated for mvlan profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1792 break
1793 }
1794 }
1795 return true
1796 }
1797
1798 va.VnetsByName.Range(updateSlNoForVnet)
1799 va.MvlanProfilesByName.Range(updateSlNoforMvlan)
1800
1801 // Clear the serial number from Blocked Devices List
1802 cntlr.GetController().DelBlockedDevices(oldOltSlNo)
1803 cntlr.GetController().DelBlockedDevices(newOltSlNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301804 }
1805}
1806
1807// GetVpvsForDsPkt to get vpv for downstream packets
1808func (va *VoltApplication) GetVpvsForDsPkt(cvlan of.VlanType, svlan of.VlanType, clientMAC net.HardwareAddr,
1809 pbit uint8) ([]*VoltPortVnet, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301810 var matchVPVs []*VoltPortVnet
1811 findVpv := func(key, value interface{}) bool {
1812 vpvs := value.([]*VoltPortVnet)
1813 for _, vpv := range vpvs {
1814 if vpv.isVlanMatching(cvlan, svlan) && vpv.MatchesPriority(pbit) != nil {
1815 var subMac net.HardwareAddr
1816 if NonZeroMacAddress(vpv.MacAddr) {
1817 subMac = vpv.MacAddr
1818 } else if vpv.LearntMacAddr != nil && NonZeroMacAddress(vpv.LearntMacAddr) {
1819 subMac = vpv.LearntMacAddr
1820 } else {
1821 matchVPVs = append(matchVPVs, vpv)
1822 continue
1823 }
1824 if util.MacAddrsMatch(subMac, clientMAC) {
1825 matchVPVs = append([]*VoltPortVnet{}, vpv)
1826 logger.Infow(ctx, "Matching VPV found", log.Fields{"Port": vpv.Port, "SVLAN": vpv.SVlan, "CVLAN": vpv.CVlan, "UNIVlan": vpv.UniVlan, "MAC": clientMAC})
1827 return false
1828 }
1829 }
1830 }
1831 return true
1832 }
1833 va.VnetsByPort.Range(findVpv)
1834
1835 if len(matchVPVs) != 1 {
1836 logger.Infow(ctx, "No matching VPV found or multiple vpvs found", log.Fields{"Match VPVs": matchVPVs, "MAC": clientMAC})
1837 return nil, errors.New("No matching VPV found or multiple vpvs found")
1838 }
1839 return matchVPVs, nil
1840}
1841
1842// GetMacInPortMap to get PORT value based on MAC key
1843func (va *VoltApplication) GetMacInPortMap(macAddr net.HardwareAddr) string {
1844 if NonZeroMacAddress(macAddr) {
1845 va.macPortLock.Lock()
1846 defer va.macPortLock.Unlock()
1847 if port, ok := va.macPortMap[macAddr.String()]; ok {
1848 logger.Debugw(ctx, "found-entry-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1849 return port
1850 }
1851 }
1852 logger.Infow(ctx, "entry-not-found-macportmap", log.Fields{"MacAddr": macAddr.String()})
1853 return ""
1854}
1855
1856// UpdateMacInPortMap to update MAC PORT (key value) information in MacPortMap
1857func (va *VoltApplication) UpdateMacInPortMap(macAddr net.HardwareAddr, port string) {
1858 if NonZeroMacAddress(macAddr) {
1859 va.macPortLock.Lock()
1860 va.macPortMap[macAddr.String()] = port
1861 va.macPortLock.Unlock()
1862 logger.Debugw(ctx, "updated-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1863 }
1864}
1865
1866// DeleteMacInPortMap to remove MAC key from MacPortMap
1867func (va *VoltApplication) DeleteMacInPortMap(macAddr net.HardwareAddr) {
1868 if NonZeroMacAddress(macAddr) {
1869 port := va.GetMacInPortMap(macAddr)
1870 va.macPortLock.Lock()
1871 delete(va.macPortMap, macAddr.String())
1872 va.macPortLock.Unlock()
1873 logger.Debugw(ctx, "deleted-from-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1874 }
1875}
1876
vinokuma926cb3e2023-03-29 11:41:06 +05301877// AddGroupToPendingPool - adds the IgmpGroup with active group table entry to global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301878func (va *VoltApplication) AddGroupToPendingPool(ig *IgmpGroup) {
1879 var grpMap map[*IgmpGroup]bool
1880 var ok bool
1881
1882 va.PendingPoolLock.Lock()
1883 defer va.PendingPoolLock.Unlock()
1884
1885 logger.Infow(ctx, "Adding IgmpGroup to Global Pending Pool", log.Fields{"GroupID": ig.GroupID, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr, "PendingDevices": len(ig.Devices)})
1886 // Do Not Reset any current profile info since group table entry tied to mvlan profile
1887 // The PonVlan is part of set field in group installed
1888 // Hence, Group created is always tied to the same mvlan profile until deleted
1889
1890 for device := range ig.Devices {
1891 key := getPendingPoolKey(ig.Mvlan, device)
1892
1893 if grpMap, ok = va.IgmpPendingPool[key]; !ok {
1894 grpMap = make(map[*IgmpGroup]bool)
1895 }
1896 grpMap[ig] = true
1897
1898 //Add grpObj reference to all associated devices
1899 va.IgmpPendingPool[key] = grpMap
1900 }
1901}
1902
vinokuma926cb3e2023-03-29 11:41:06 +05301903// RemoveGroupFromPendingPool - removes the group from global pending group pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301904func (va *VoltApplication) RemoveGroupFromPendingPool(device string, ig *IgmpGroup) bool {
1905 GetApplication().PendingPoolLock.Lock()
1906 defer GetApplication().PendingPoolLock.Unlock()
1907
1908 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)})
1909
1910 key := getPendingPoolKey(ig.Mvlan, device)
1911 if _, ok := va.IgmpPendingPool[key]; ok {
1912 delete(va.IgmpPendingPool[key], ig)
1913 return true
1914 }
1915 return false
1916}
1917
vinokuma926cb3e2023-03-29 11:41:06 +05301918// RemoveGroupsFromPendingPool - removes the group from global pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301919func (va *VoltApplication) RemoveGroupsFromPendingPool(cntx context.Context, device string, mvlan of.VlanType) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301920 GetApplication().PendingPoolLock.Lock()
1921 defer GetApplication().PendingPoolLock.Unlock()
1922
1923 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool for given Deivce & Mvlan", log.Fields{"Device": device, "Mvlan": mvlan.String()})
1924
1925 key := getPendingPoolKey(mvlan, device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301926 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301927}
1928
vinokuma926cb3e2023-03-29 11:41:06 +05301929// RemoveGroupListFromPendingPool - removes the groups for provided key
Naveen Sampath04696f72022-06-13 15:19:14 +05301930// 1. Deletes the group from device
1931// 2. Delete the IgmpGroup obj and release the group ID to pool
1932// Note: Make sure to obtain PendingPoolLock lock before calling this func
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301933func (va *VoltApplication) RemoveGroupListFromPendingPool(cntx context.Context, key string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301934 if grpMap, ok := va.IgmpPendingPool[key]; ok {
1935 delete(va.IgmpPendingPool, key)
1936 for ig := range grpMap {
1937 for device := range ig.Devices {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301938 ig.DeleteIgmpGroupDevice(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301939 }
1940 }
1941 }
1942}
1943
vinokuma926cb3e2023-03-29 11:41:06 +05301944// RemoveGroupDevicesFromPendingPool - removes the group from global pending group pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301945func (va *VoltApplication) RemoveGroupDevicesFromPendingPool(ig *IgmpGroup) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301946 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)})
1947 for device := range ig.PendingGroupForDevice {
1948 va.RemoveGroupFromPendingPool(device, ig)
1949 }
1950}
1951
vinokuma926cb3e2023-03-29 11:41:06 +05301952// GetGroupFromPendingPool - Returns IgmpGroup obj from global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301953func (va *VoltApplication) GetGroupFromPendingPool(mvlan of.VlanType, device string) *IgmpGroup {
Naveen Sampath04696f72022-06-13 15:19:14 +05301954 var ig *IgmpGroup
1955
1956 va.PendingPoolLock.Lock()
1957 defer va.PendingPoolLock.Unlock()
1958
1959 key := getPendingPoolKey(mvlan, device)
1960 logger.Infow(ctx, "Getting IgmpGroup from Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String(), "Key": key})
1961
vinokuma926cb3e2023-03-29 11:41:06 +05301962 // Gets all IgmpGrp Obj for the device
Naveen Sampath04696f72022-06-13 15:19:14 +05301963 grpMap, ok := va.IgmpPendingPool[key]
1964 if !ok || len(grpMap) == 0 {
1965 logger.Infow(ctx, "Matching IgmpGroup not found in Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String()})
1966 return nil
1967 }
1968
vinokuma926cb3e2023-03-29 11:41:06 +05301969 // Gets a random obj from available grps
Naveen Sampath04696f72022-06-13 15:19:14 +05301970 for ig = range grpMap {
vinokuma926cb3e2023-03-29 11:41:06 +05301971 // Remove grp obj reference from all devices associated in pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301972 for dev := range ig.Devices {
1973 key := getPendingPoolKey(mvlan, dev)
1974 delete(va.IgmpPendingPool[key], ig)
1975 }
1976
vinokuma926cb3e2023-03-29 11:41:06 +05301977 // Safety check to avoid re-allocating group already in use
Naveen Sampath04696f72022-06-13 15:19:14 +05301978 if ig.NumDevicesActive() == 0 {
1979 return ig
1980 }
1981
vinokuma926cb3e2023-03-29 11:41:06 +05301982 // Iteration will continue only if IG is not allocated
Naveen Sampath04696f72022-06-13 15:19:14 +05301983 }
1984 return nil
1985}
1986
vinokuma926cb3e2023-03-29 11:41:06 +05301987// RemovePendingGroups - removes all pending groups for provided reference from global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301988// reference - mvlan/device ID
1989// isRefDevice - true - Device as reference
vinokuma926cb3e2023-03-29 11:41:06 +05301990// false - Mvlan as reference
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301991func (va *VoltApplication) RemovePendingGroups(cntx context.Context, reference string, isRefDevice bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301992 va.PendingPoolLock.Lock()
1993 defer va.PendingPoolLock.Unlock()
1994
1995 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool", log.Fields{"Reference": reference, "isRefDevice": isRefDevice})
1996
vinokuma926cb3e2023-03-29 11:41:06 +05301997 // Pending Pool key: "<mvlan>_<DeviceID>""
Naveen Sampath04696f72022-06-13 15:19:14 +05301998 paramPosition := 0
1999 if isRefDevice {
2000 paramPosition = 1
2001 }
2002
2003 // 1.Remove the Entry from pending pool
2004 // 2.Deletes the group from device
2005 // 3.Delete the IgmpGroup obj and release the group ID to pool
2006 for key := range va.IgmpPendingPool {
2007 keyParams := strings.Split(key, "_")
2008 if keyParams[paramPosition] == reference {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302009 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05302010 }
2011 }
2012}
2013
2014func getPendingPoolKey(mvlan of.VlanType, device string) string {
2015 return mvlan.String() + "_" + device
2016}
2017
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302018func (va *VoltApplication) removeExpiredGroups(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302019 logger.Debug(ctx, "Check for expired Igmp Groups")
2020 removeExpiredGroups := func(key interface{}, value interface{}) bool {
2021 ig := value.(*IgmpGroup)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302022 ig.removeExpiredGroupFromDevice(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302023 return true
2024 }
2025 va.IgmpGroups.Range(removeExpiredGroups)
2026}
2027
vinokuma926cb3e2023-03-29 11:41:06 +05302028// TriggerPendingProfileDeleteReq - trigger pending profile delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302029func (va *VoltApplication) TriggerPendingProfileDeleteReq(cntx context.Context, device string) {
2030 va.TriggerPendingServiceDeleteReq(cntx, device)
2031 va.TriggerPendingVpvDeleteReq(cntx, device)
2032 va.TriggerPendingVnetDeleteReq(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05302033 logger.Warnw(ctx, "All Pending Profile Delete triggered for device", log.Fields{"Device": device})
2034}
2035
vinokuma926cb3e2023-03-29 11:41:06 +05302036// TriggerPendingServiceDeleteReq - trigger pending service delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302037func (va *VoltApplication) TriggerPendingServiceDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302038 logger.Warnw(ctx, "Pending Services to be deleted", log.Fields{"Count": len(va.ServicesToDelete)})
2039 for serviceName := range va.ServicesToDelete {
2040 logger.Debugw(ctx, "Trigger Service Delete", log.Fields{"Service": serviceName})
2041 if vs := va.GetService(serviceName); vs != nil {
2042 if vs.Device == device {
2043 logger.Warnw(ctx, "Triggering Pending Service delete", log.Fields{"Service": vs.Name})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302044 vs.DelHsiaFlows(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302045 if vs.ForceDelete {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302046 vs.DelFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302047 }
2048 }
2049 } else {
2050 logger.Errorw(ctx, "Pending Service Not found", log.Fields{"Service": serviceName})
2051 }
2052 }
2053}
2054
vinokuma926cb3e2023-03-29 11:41:06 +05302055// TriggerPendingVpvDeleteReq - trigger pending VPV delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302056func (va *VoltApplication) TriggerPendingVpvDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302057 logger.Warnw(ctx, "Pending VPVs to be deleted", log.Fields{"Count": len(va.VoltPortVnetsToDelete)})
2058 for vpv := range va.VoltPortVnetsToDelete {
2059 if vpv.Device == device {
2060 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 +05302061 va.DelVnetFromPort(cntx, vpv.Port, vpv)
Naveen Sampath04696f72022-06-13 15:19:14 +05302062 }
2063 }
2064}
2065
vinokuma926cb3e2023-03-29 11:41:06 +05302066// TriggerPendingVnetDeleteReq - trigger pending vnet delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302067func (va *VoltApplication) TriggerPendingVnetDeleteReq(cntx context.Context, device string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302068 logger.Warnw(ctx, "Pending Vnets to be deleted", log.Fields{"Count": len(va.VnetsToDelete)})
2069 for vnetName := range va.VnetsToDelete {
2070 if vnetIntf, _ := va.VnetsByName.Load(vnetName); vnetIntf != nil {
2071 vnet := vnetIntf.(*VoltVnet)
2072 logger.Warnw(ctx, "Triggering Pending Vnet flows delete", log.Fields{"Vnet": vnet.Name})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302073 if d, _ := va.GetDeviceBySerialNo(vnet.PendingDeviceToDelete); d != nil && d.SerialNum == vnet.PendingDeviceToDelete {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302074 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, vnet.PendingDeviceToDelete)
Naveen Sampath04696f72022-06-13 15:19:14 +05302075 va.deleteVnetConfig(vnet)
2076 } else {
Tinoj Joseph1d108322022-07-13 10:07:39 +05302077 logger.Warnw(ctx, "Vnet Delete Failed : Device Not Found", log.Fields{"Vnet": vnet.Name, "Device": vnet.PendingDeviceToDelete})
Naveen Sampath04696f72022-06-13 15:19:14 +05302078 }
2079 }
2080 }
2081}
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302082
2083type OltFlowService struct {
vinokuma926cb3e2023-03-29 11:41:06 +05302084 DefaultTechProfileID int `json:"defaultTechProfileId"`
Akash Sonia8246972023-01-03 10:37:08 +05302085 EnableDhcpOnNni bool `json:"enableDhcpOnNni"`
Akash Sonia8246972023-01-03 10:37:08 +05302086 EnableIgmpOnNni bool `json:"enableIgmpOnNni"`
2087 EnableEapol bool `json:"enableEapol"`
2088 EnableDhcpV6 bool `json:"enableDhcpV6"`
2089 EnableDhcpV4 bool `json:"enableDhcpV4"`
2090 RemoveFlowsOnDisable bool `json:"removeFlowsOnDisable"`
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302091}
2092
2093func (va *VoltApplication) UpdateOltFlowService(cntx context.Context, oltFlowService OltFlowService) {
2094 logger.Infow(ctx, "UpdateOltFlowService", log.Fields{"oldValue": va.OltFlowServiceConfig, "newValue": oltFlowService})
2095 va.OltFlowServiceConfig = oltFlowService
2096 b, err := json.Marshal(va.OltFlowServiceConfig)
2097 if err != nil {
2098 logger.Warnw(ctx, "Failed to Marshal OltFlowServiceConfig", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2099 return
2100 }
2101 _ = db.PutOltFlowService(cntx, string(b))
2102}
Akash Sonia8246972023-01-03 10:37:08 +05302103
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302104// RestoreOltFlowService to read from the DB and restore olt flow service config
2105func (va *VoltApplication) RestoreOltFlowService(cntx context.Context) {
2106 oltflowService, err := db.GetOltFlowService(cntx)
2107 if err != nil {
2108 logger.Warnw(ctx, "Failed to Get OltFlowServiceConfig from DB", log.Fields{"Error": err})
2109 return
2110 }
2111 err = json.Unmarshal([]byte(oltflowService), &va.OltFlowServiceConfig)
2112 if err != nil {
2113 logger.Warn(ctx, "Unmarshal of oltflowService failed")
2114 return
2115 }
2116 logger.Infow(ctx, "updated OltFlowServiceConfig from DB", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2117}
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302118
Akash Soni87a19072023-02-28 00:46:59 +05302119func (va *VoltApplication) UpdateDeviceConfig(cntx context.Context, deviceConfig *DeviceConfig) {
2120 var dc *DeviceConfig
2121 va.DevicesConfig.Store(deviceConfig.SerialNumber, deviceConfig)
2122 err := dc.WriteDeviceConfigToDb(cntx, deviceConfig.SerialNumber, deviceConfig)
2123 if err != nil {
2124 logger.Errorw(ctx, "DB update for device config failed", log.Fields{"err": err})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302125 }
Akash Soni87a19072023-02-28 00:46:59 +05302126 logger.Infow(ctx, "Added OLT configurations", log.Fields{"DeviceInfo": deviceConfig})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302127 // If device is already discovered update the VoltDevice structure
Akash Soni87a19072023-02-28 00:46:59 +05302128 device, id := va.GetDeviceBySerialNo(deviceConfig.SerialNumber)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302129 if device != nil {
Akash Soni87a19072023-02-28 00:46:59 +05302130 device.NniDhcpTrapVid = of.VlanType(deviceConfig.NniDhcpTrapVid)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302131 va.DevicesDisc.Store(id, device)
2132 }
2133}