blob: 3ddb29c820a47f82e73337719ded354f205e539e [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) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530256 logger.Debugw(ctx, "AssociateVpvsToDevice", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +0530257 if d := va.GetDevice(device); d != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530258 vpvMap := d.GetAssociatedVpvs(vpv.SVlan)
259 vpvMap.Set(vpv, true)
260 d.VpvsBySvlan.Set(vpv.SVlan, vpvMap)
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530261 logger.Debugw(ctx, "VPVMap: SET", log.Fields{"Map": vpvMap.Length()})
Naveen Sampath04696f72022-06-13 15:19:14 +0530262 return
263 }
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530264 logger.Warnw(ctx, "Set VPVMap failed: Device Not Found", log.Fields{"Svlan": vpv.SVlan, "Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +0530265}
266
vinokuma926cb3e2023-03-29 11:41:06 +0530267// DisassociateVpvsFromDevice - disassociated VPVs from given device & svlan
Naveen Sampath04696f72022-06-13 15:19:14 +0530268func (va *VoltApplication) DisassociateVpvsFromDevice(device string, vpv *VoltPortVnet) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530269 logger.Debugw(ctx, "DisassociateVpvsToDevice", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +0530270 if d := va.GetDevice(device); d != nil {
271 vpvMap := d.GetAssociatedVpvs(vpv.SVlan)
272 vpvMap.Remove(vpv)
273 d.VpvsBySvlan.Set(vpv.SVlan, vpvMap)
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530274 logger.Debugw(ctx, "VPVMap: Remove", log.Fields{"Map": vpvMap.Length()})
Naveen Sampath04696f72022-06-13 15:19:14 +0530275 return
276 }
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530277 logger.Warnw(ctx, "Remove VPVMap failed: Device Not Found", log.Fields{"Svlan": vpv.SVlan, "Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +0530278}
279
vinokuma926cb3e2023-03-29 11:41:06 +0530280// GetAssociatedVpvs - returns the associated VPVs for the given Svlan
Naveen Sampath04696f72022-06-13 15:19:14 +0530281func (d *VoltDevice) GetAssociatedVpvs(svlan of.VlanType) *util.ConcurrentMap {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530282 logger.Debugw(ctx, "Received Get Associated Vpvs", log.Fields{"svlan": svlan})
Naveen Sampath04696f72022-06-13 15:19:14 +0530283 var vpvMap *util.ConcurrentMap
284 var mapIntf interface{}
285 var ok bool
286
287 if mapIntf, ok = d.VpvsBySvlan.Get(svlan); ok {
288 vpvMap = mapIntf.(*util.ConcurrentMap)
289 } else {
290 vpvMap = util.NewConcurrentMap()
291 }
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530292 logger.Debugw(ctx, "VPVMap: GET", log.Fields{"Map": vpvMap.Length()})
Naveen Sampath04696f72022-06-13 15:19:14 +0530293 return vpvMap
294}
295
296// AddPort add port to the device.
297func (d *VoltDevice) AddPort(port string, id uint32) *VoltPort {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530298 logger.Debugw(ctx, "Add Port", log.Fields{"Port": port, "ID": id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530299 addPonPortFromUniPort := func(vPort *VoltPort) {
300 if vPort.Type == VoltPortTypeAccess {
301 ponPortID := GetPonPortIDFromUNIPort(vPort.ID)
302
303 if ponPortUniList, ok := d.PonPortList.Load(ponPortID); !ok {
304 uniList := make(map[string]uint32)
305 uniList[port] = vPort.ID
306 d.PonPortList.Store(ponPortID, uniList)
307 } else {
308 ponPortUniList.(map[string]uint32)[port] = vPort.ID
309 d.PonPortList.Store(ponPortID, ponPortUniList)
310 }
311 }
312 }
313 va := GetApplication()
314 if pIntf, ok := d.Ports.Load(port); ok {
315 voltPort := pIntf.(*VoltPort)
316 addPonPortFromUniPort(voltPort)
317 va.AggActiveChannelsCountPerSub(d.Name, port, voltPort)
318 d.Ports.Store(port, voltPort)
319 return voltPort
320 }
321 p := NewVoltPort(d.Name, port, id)
322 va.AggActiveChannelsCountPerSub(d.Name, port, p)
323 d.Ports.Store(port, p)
324 if util.IsNniPort(id) {
325 d.NniPort = port
326 }
327 addPonPortFromUniPort(p)
328 return p
329}
330
331// GetPort to get port information from the device.
332func (d *VoltDevice) GetPort(port string) *VoltPort {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530333 logger.Debugw(ctx, "Get Port", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530334 if pIntf, ok := d.Ports.Load(port); ok {
335 return pIntf.(*VoltPort)
336 }
337 return nil
338}
339
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530340// GetPortByPortID to get port information from the device.
341func (d *VoltDevice) GetPortNameFromPortID(portID uint32) string {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530342 logger.Debugw(ctx, "Get Port Name from the device", log.Fields{"PortID": portID})
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530343 portName := ""
344 d.Ports.Range(func(key, value interface{}) bool {
345 vp := value.(*VoltPort)
346 if vp.ID == portID {
347 portName = vp.Name
348 }
349 return true
350 })
351 return portName
352}
353
Naveen Sampath04696f72022-06-13 15:19:14 +0530354// DelPort to delete port from the device
355func (d *VoltDevice) DelPort(port string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530356 logger.Debugw(ctx, "Delete Port from the device", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530357 if _, ok := d.Ports.Load(port); ok {
358 d.Ports.Delete(port)
359 } else {
360 logger.Warnw(ctx, "Port doesn't exist", log.Fields{"Device": d.Name, "Port": port})
361 }
362}
363
364// pushFlowsForUnis to send port-up-indication for uni ports.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530365func (d *VoltDevice) pushFlowsForUnis(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530366 logger.Info(ctx, "NNI Discovered, Sending Port UP Ind for UNIs")
367 d.Ports.Range(func(key, value interface{}) bool {
368 port := key.(string)
369 vp := value.(*VoltPort)
370
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530371 logger.Debugw(ctx, "NNI Discovered. Sending Port UP Ind for UNI", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530372 //Ignore if UNI port is not UP
373 if vp.State != PortStateUp {
374 return true
375 }
376
377 //Obtain all VPVs associated with the port
378 vnets, ok := GetApplication().VnetsByPort.Load(port)
379 if !ok {
380 return true
381 }
382
383 for _, vpv := range vnets.([]*VoltPortVnet) {
384 vpv.VpvLock.Lock()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530385 vpv.PortUpInd(cntx, d, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530386 vpv.VpvLock.Unlock()
Naveen Sampath04696f72022-06-13 15:19:14 +0530387 }
388 return true
389 })
390}
391
392// ----------------------------------------------------------
393// VOLT Application - hosts all other objects
394// ----------------------------------------------------------
395//
396// The VOLT application is a singleton implementation where
397// there is just one instance in the system and is the gateway
398// to all other components within the controller
399// The declaration of the singleton object
400var vapplication *VoltApplication
401
402// VoltApplication fields :
403// ServiceByName - Stores the services by the name as key
vinokuma926cb3e2023-03-29 11:41:06 +0530404// A record of NB configuration.
Naveen Sampath04696f72022-06-13 15:19:14 +0530405// VnetsByPort - Stores the VNETs by the ports configured
vinokuma926cb3e2023-03-29 11:41:06 +0530406// from NB. A record of NB configuration.
Naveen Sampath04696f72022-06-13 15:19:14 +0530407// VnetsByTag - Stores the VNETs by the VLANS configured
vinokuma926cb3e2023-03-29 11:41:06 +0530408// from NB. A record of NB configuration.
Naveen Sampath04696f72022-06-13 15:19:14 +0530409// VnetsByName - Stores the VNETs by the name configured
vinokuma926cb3e2023-03-29 11:41:06 +0530410// from NB. A record of NB configuration.
Naveen Sampath04696f72022-06-13 15:19:14 +0530411// DevicesDisc - Stores the devices discovered from SB.
vinokuma926cb3e2023-03-29 11:41:06 +0530412// Should be updated only by events from SB
Naveen Sampath04696f72022-06-13 15:19:14 +0530413// PortsDisc - Stores the ports discovered from SB.
vinokuma926cb3e2023-03-29 11:41:06 +0530414// Should be updated only by events from SB
Naveen Sampath04696f72022-06-13 15:19:14 +0530415type VoltApplication struct {
Naveen Sampath04696f72022-06-13 15:19:14 +0530416 MeterMgr
vinokuma926cb3e2023-03-29 11:41:06 +0530417 DataMigrationInfo DataMigration
418 VnetsBySvlan *util.ConcurrentMap
419 IgmpGroupIds []*IgmpGroup
420 VoltPortVnetsToDelete map[*VoltPortVnet]bool
Akash Sonia8246972023-01-03 10:37:08 +0530421 IgmpPendingPool map[string]map[*IgmpGroup]bool //[grpkey, map[groupObj]bool] //mvlan_grpName/IP
vinokuma926cb3e2023-03-29 11:41:06 +0530422 macPortMap map[string]string
Akash Sonia8246972023-01-03 10:37:08 +0530423 VnetsToDelete map[string]bool
424 ServicesToDelete map[string]bool
Hitesh Chhabra64be2442023-06-21 17:06:34 +0530425 ServicesToDeactivate map[string]bool
Akash Sonia8246972023-01-03 10:37:08 +0530426 PortAlarmProfileCache map[string]map[string]int // [portAlarmID][ThresholdLevelString]ThresholdLevel
427 vendorID string
vinokuma926cb3e2023-03-29 11:41:06 +0530428 ServiceByName sync.Map // [serName]*VoltService
429 VnetsByPort sync.Map // [portName][]*VoltPortVnet
430 VnetsByTag sync.Map // [svlan-cvlan-uvlan]*VoltVnet
431 VnetsByName sync.Map // [vnetName]*VoltVnet
432 DevicesDisc sync.Map
433 PortsDisc sync.Map
434 IgmpGroups sync.Map // [grpKey]*IgmpGroup
435 MvlanProfilesByTag sync.Map
436 MvlanProfilesByName sync.Map
437 Icmpv6Receivers sync.Map
438 DeviceCounters sync.Map //[logicalDeviceId]*DeviceCounters
439 ServiceCounters sync.Map //[serviceName]*ServiceCounters
440 NbDevice sync.Map // [OLTSouthBoundID]*NbDevice
441 OltIgmpInfoBySerial sync.Map
442 McastConfigMap sync.Map //[OltSerialNo_MvlanProfileID]*McastConfig
Akash Sonia8246972023-01-03 10:37:08 +0530443 DevicesConfig sync.Map //[serialNumber]*DeviceConfig
vinokuma926cb3e2023-03-29 11:41:06 +0530444 IgmpProfilesByName sync.Map
445 IgmpTasks tasks.Tasks
446 IndicationsTasks tasks.Tasks
447 MulticastAlarmTasks tasks.Tasks
448 IgmpKPIsTasks tasks.Tasks
449 pppoeTasks tasks.Tasks
450 OltFlowServiceConfig OltFlowService
451 PendingPoolLock sync.RWMutex
452 // MacAddress-Port MAP to avoid swap of mac across ports.
453 macPortLock sync.RWMutex
454 portLock sync.Mutex
Akash Sonia8246972023-01-03 10:37:08 +0530455}
Naveen Sampath04696f72022-06-13 15:19:14 +0530456
Akash Sonia8246972023-01-03 10:37:08 +0530457type DeviceConfig struct {
458 SerialNumber string `json:"id"`
459 HardwareIdentifier string `json:"hardwareIdentifier"`
Akash Soni87a19072023-02-28 00:46:59 +0530460 IPAddress string `json:"ipAddress"`
Akash Soni53da2852023-03-15 00:31:31 +0530461 UplinkPort string `json:"uplinkPort"`
Akash Sonia8246972023-01-03 10:37:08 +0530462 NasID string `json:"nasId"`
463 NniDhcpTrapVid int `json:"nniDhcpTrapVid"`
Naveen Sampath04696f72022-06-13 15:19:14 +0530464}
465
466// PonPortCfg contains NB port config and activeIGMPChannels count
467type PonPortCfg struct {
vinokuma926cb3e2023-03-29 11:41:06 +0530468 PortAlarmProfileID string
Naveen Sampath04696f72022-06-13 15:19:14 +0530469 PortID uint32
470 MaxActiveChannels uint32
471 ActiveIGMPChannels uint32
472 EnableMulticastKPI bool
Naveen Sampath04696f72022-06-13 15:19:14 +0530473}
474
475// NbDevice OLT Device info
476type NbDevice struct {
477 SouthBoundID string
478 PonPorts sync.Map // [PortID]*PonPortCfg
479}
480
481// RestoreNbDeviceFromDb restores the NB Device in case of VGC pod restart.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530482func (va *VoltApplication) RestoreNbDeviceFromDb(cntx context.Context, deviceID string) *NbDevice {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530483 logger.Debugw(ctx, "Received Restore Nb Device From Db", log.Fields{"deviceID": deviceID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530484 nbDevice := NewNbDevice()
485 nbDevice.SouthBoundID = deviceID
486
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530487 nbPorts, _ := db.GetAllNbPorts(cntx, deviceID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530488
489 for key, p := range nbPorts {
490 b, ok := p.Value.([]byte)
491 if !ok {
492 logger.Warn(ctx, "The value type is not []byte")
493 continue
494 }
495 var port PonPortCfg
496 err := json.Unmarshal(b, &port)
497 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530498 logger.Warnw(ctx, "Unmarshal of PonPortCfg failed", log.Fields{"deviceID": deviceID, "port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530499 continue
500 }
501 logger.Debugw(ctx, "Port recovered", log.Fields{"port": port})
502 ponPortID, _ := strconv.Atoi(key)
503 nbDevice.PonPorts.Store(uint32(ponPortID), &port)
504 }
505 va.NbDevice.Store(deviceID, nbDevice)
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530506 logger.Debugw(ctx, "Recovered NbDevice From Db", log.Fields{"deviceID": deviceID, "nbDevice": nbDevice})
Naveen Sampath04696f72022-06-13 15:19:14 +0530507 return nbDevice
508}
509
510// NewNbDevice Constructor for NbDevice
511func NewNbDevice() *NbDevice {
512 var nbDevice NbDevice
513 return &nbDevice
514}
515
516// WriteToDb writes nb device port config to kv store
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530517func (nbd *NbDevice) WriteToDb(cntx context.Context, portID uint32, ponPort *PonPortCfg) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530518 b, err := json.Marshal(ponPort)
519 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530520 logger.Errorw(ctx, "PonPortConfig-marshal-failed", log.Fields{"err": err, "ponPort": ponPort})
Naveen Sampath04696f72022-06-13 15:19:14 +0530521 return
522 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530523 db.PutNbDevicePort(cntx, nbd.SouthBoundID, portID, string(b))
Naveen Sampath04696f72022-06-13 15:19:14 +0530524}
525
526// AddPortToNbDevice Adds pon port to NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530527func (nbd *NbDevice) AddPortToNbDevice(cntx context.Context, portID, allowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530528 enableMulticastKPI bool, portAlarmProfileID string) *PonPortCfg {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530529 logger.Debugw(ctx, "AddPortToNbDevice", log.Fields{"PortID": portID, "EnableMulticastKPI": enableMulticastKPI, "PortAlarmProfileID": portAlarmProfileID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530530 ponPort := &PonPortCfg{
531 PortID: portID,
532 MaxActiveChannels: allowedChannels,
533 EnableMulticastKPI: enableMulticastKPI,
534 PortAlarmProfileID: portAlarmProfileID,
535 }
536 nbd.PonPorts.Store(portID, ponPort)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530537 nbd.WriteToDb(cntx, portID, ponPort)
Naveen Sampath04696f72022-06-13 15:19:14 +0530538 return ponPort
539}
540
Akash Sonia8246972023-01-03 10:37:08 +0530541// RestoreDeviceConfigFromDb to restore vnet from port
542func (va *VoltApplication) RestoreDeviceConfigFromDb(cntx context.Context) {
543 // VNETS must be learnt first
544 dConfig, _ := db.GetDeviceConfig(cntx)
545 for _, device := range dConfig {
546 b, ok := device.Value.([]byte)
547 if !ok {
548 logger.Warn(ctx, "The value type is not []byte")
549 continue
550 }
551 devConfig := DeviceConfig{}
552 err := json.Unmarshal(b, &devConfig)
553 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530554 logger.Warnw(ctx, "Unmarshal of device configuration failed", log.Fields{"Device Config": devConfig})
Akash Sonia8246972023-01-03 10:37:08 +0530555 continue
556 }
557 logger.Debugw(ctx, "Retrieved device config", log.Fields{"Device Config": devConfig})
558 if err := va.AddDeviceConfig(cntx, devConfig.SerialNumber, devConfig.HardwareIdentifier, devConfig.NasID, devConfig.IPAddress, devConfig.UplinkPort, devConfig.NniDhcpTrapVid); err != nil {
559 logger.Warnw(ctx, "Add device config failed", log.Fields{"DeviceConfig": devConfig, "Error": err})
560 }
Akash Sonia8246972023-01-03 10:37:08 +0530561 }
562}
563
564// WriteDeviceConfigToDb writes sb device config to kv store
565func (dc *DeviceConfig) WriteDeviceConfigToDb(cntx context.Context, serialNum string, deviceConfig *DeviceConfig) error {
566 b, err := json.Marshal(deviceConfig)
567 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530568 return fmt.Errorf("deviceConfig-marshal-failed - %w ", err)
Akash Sonia8246972023-01-03 10:37:08 +0530569 }
570 dberr := db.PutDeviceConfig(cntx, serialNum, string(b))
571 if dberr != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530572 return fmt.Errorf("update device config failed - %w ", err)
Akash Sonia8246972023-01-03 10:37:08 +0530573 }
574 return nil
575}
576
vinokuma926cb3e2023-03-29 11:41:06 +0530577func (va *VoltApplication) AddDeviceConfig(cntx context.Context, serialNum, hardwareIdentifier, nasID, ipAddress, uplinkPort string, nniDhcpTrapID int) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530578 logger.Debugw(ctx, "Received Add device config", log.Fields{"SerialNumber": serialNum, "HardwareIdentifier": hardwareIdentifier, "NasID": nasID, "IPAddress": ipAddress, "UplinkPort": uplinkPort, "NniDhcpTrapID": nniDhcpTrapID})
Akash Sonia8246972023-01-03 10:37:08 +0530579 var dc *DeviceConfig
580
Akash Soni87a19072023-02-28 00:46:59 +0530581 deviceConfig := &DeviceConfig{
582 SerialNumber: serialNum,
583 HardwareIdentifier: hardwareIdentifier,
584 NasID: nasID,
585 UplinkPort: uplinkPort,
586 IPAddress: ipAddress,
vinokuma926cb3e2023-03-29 11:41:06 +0530587 NniDhcpTrapVid: nniDhcpTrapID,
Akash Sonia8246972023-01-03 10:37:08 +0530588 }
Akash Soni87a19072023-02-28 00:46:59 +0530589 va.DevicesConfig.Store(serialNum, deviceConfig)
590 err := dc.WriteDeviceConfigToDb(cntx, serialNum, deviceConfig)
591 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530592 return fmt.Errorf("DB update for device config failed - %w ", err)
Akash Soni87a19072023-02-28 00:46:59 +0530593 }
594
595 // If device is already discovered update the VoltDevice structure
596 device, id := va.GetDeviceBySerialNo(serialNum)
597 if device != nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530598 device.NniDhcpTrapVid = of.VlanType(nniDhcpTrapID)
Akash Soni87a19072023-02-28 00:46:59 +0530599 va.DevicesDisc.Store(id, device)
600 }
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530601 logger.Debugw(ctx, "Added device config", log.Fields{"Device Config": deviceConfig})
Akash Sonia8246972023-01-03 10:37:08 +0530602 return nil
603}
604
605// GetDeviceConfig to get a device config.
606func (va *VoltApplication) GetDeviceConfig(serNum string) *DeviceConfig {
607 if d, ok := va.DevicesConfig.Load(serNum); ok {
608 return d.(*DeviceConfig)
609 }
610 return nil
611}
612
Naveen Sampath04696f72022-06-13 15:19:14 +0530613// UpdatePortToNbDevice Adds pon port to NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530614func (nbd *NbDevice) UpdatePortToNbDevice(cntx context.Context, portID, allowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) *PonPortCfg {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530615 logger.Debugw(ctx, "Received Update Port To NbDevice", log.Fields{"portID": portID, "AllowedChannels": allowedChannels, "EnableMulticastKPI": enableMulticastKPI, "PortAlarmProfileID": portAlarmProfileID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530616 p, exists := nbd.PonPorts.Load(portID)
617 if !exists {
618 logger.Errorw(ctx, "PON port not exists in nb-device", log.Fields{"portID": portID})
619 return nil
620 }
621 port := p.(*PonPortCfg)
622 if allowedChannels != 0 {
623 port.MaxActiveChannels = allowedChannels
624 port.EnableMulticastKPI = enableMulticastKPI
625 port.PortAlarmProfileID = portAlarmProfileID
626 }
627
628 nbd.PonPorts.Store(portID, port)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530629 nbd.WriteToDb(cntx, portID, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530630 return port
631}
632
633// DeletePortFromNbDevice Deletes pon port from NB Device and DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530634func (nbd *NbDevice) DeletePortFromNbDevice(cntx context.Context, portID uint32) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530635 logger.Debugw(ctx, "Received Delete Port from NbDevice", log.Fields{"portID": portID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530636 if _, ok := nbd.PonPorts.Load(portID); ok {
637 nbd.PonPorts.Delete(portID)
638 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530639 db.DelNbDevicePort(cntx, nbd.SouthBoundID, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530640}
641
642// GetApplication : Interface to access the singleton object
643func GetApplication() *VoltApplication {
644 if vapplication == nil {
645 vapplication = newVoltApplication()
646 }
647 return vapplication
648}
649
650// newVoltApplication : Constructor for the singleton object. Hence this is not
651// an exported function
652func newVoltApplication() *VoltApplication {
653 var va VoltApplication
654 va.IgmpTasks.Initialize(context.TODO())
655 va.MulticastAlarmTasks.Initialize(context.TODO())
656 va.IgmpKPIsTasks.Initialize(context.TODO())
657 va.pppoeTasks.Initialize(context.TODO())
658 va.storeIgmpProfileMap(DefaultIgmpProfID, newDefaultIgmpProfile())
659 va.MeterMgr.Init()
660 va.AddIgmpGroups(5000)
661 va.macPortMap = make(map[string]string)
662 va.IgmpPendingPool = make(map[string]map[*IgmpGroup]bool)
663 va.VnetsBySvlan = util.NewConcurrentMap()
664 va.VnetsToDelete = make(map[string]bool)
665 va.ServicesToDelete = make(map[string]bool)
Hitesh Chhabra64be2442023-06-21 17:06:34 +0530666 va.ServicesToDeactivate = make(map[string]bool)
Naveen Sampath04696f72022-06-13 15:19:14 +0530667 va.VoltPortVnetsToDelete = make(map[*VoltPortVnet]bool)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530668 go va.Start(context.Background(), TimerCfg{tick: 100 * time.Millisecond}, tickTimer)
669 go va.Start(context.Background(), TimerCfg{tick: time.Duration(GroupExpiryTime) * time.Minute}, pendingPoolTimer)
Naveen Sampath04696f72022-06-13 15:19:14 +0530670 InitEventFuncMapper()
671 db = database.GetDatabase()
672 return &va
673}
674
vinokuma926cb3e2023-03-29 11:41:06 +0530675// GetFlowEventRegister - returs the register based on flow mod type
Naveen Sampath04696f72022-06-13 15:19:14 +0530676func (d *VoltDevice) GetFlowEventRegister(flowModType of.Command) (*util.ConcurrentMap, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530677 switch flowModType {
678 case of.CommandDel:
679 return d.FlowDelEventMap, nil
680 case of.CommandAdd:
681 return d.FlowAddEventMap, nil
682 default:
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530683 logger.Warnw(ctx, "Unknown Flow Mod received", log.Fields{"flowModtype": flowModType})
Naveen Sampath04696f72022-06-13 15:19:14 +0530684 }
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530685 return util.NewConcurrentMap(), errors.New("unknown flow mod")
Naveen Sampath04696f72022-06-13 15:19:14 +0530686}
687
688// RegisterFlowAddEvent to register a flow event.
689func (d *VoltDevice) RegisterFlowAddEvent(cookie string, event *FlowEvent) {
690 logger.Debugw(ctx, "Registered Flow Add Event", log.Fields{"Cookie": cookie, "Event": event})
691 d.FlowAddEventMap.MapLock.Lock()
692 defer d.FlowAddEventMap.MapLock.Unlock()
693 d.FlowAddEventMap.Set(cookie, event)
694}
695
696// RegisterFlowDelEvent to register a flow event.
697func (d *VoltDevice) RegisterFlowDelEvent(cookie string, event *FlowEvent) {
698 logger.Debugw(ctx, "Registered Flow Del Event", log.Fields{"Cookie": cookie, "Event": event})
699 d.FlowDelEventMap.MapLock.Lock()
700 defer d.FlowDelEventMap.MapLock.Unlock()
701 d.FlowDelEventMap.Set(cookie, event)
702}
703
704// UnRegisterFlowEvent to unregister a flow event.
705func (d *VoltDevice) UnRegisterFlowEvent(cookie string, flowModType of.Command) {
706 logger.Debugw(ctx, "UnRegistered Flow Add Event", log.Fields{"Cookie": cookie, "Type": flowModType})
707 flowEventMap, err := d.GetFlowEventRegister(flowModType)
708 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530709 logger.Warnw(ctx, "Flow event map does not exists", log.Fields{"flowMod": flowModType, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530710 return
711 }
712 flowEventMap.MapLock.Lock()
713 defer flowEventMap.MapLock.Unlock()
714 flowEventMap.Remove(cookie)
715}
716
717// AddIgmpGroups to add Igmp groups.
718func (va *VoltApplication) AddIgmpGroups(numOfGroups uint32) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530719 logger.Debugw(ctx, "AddIgmpGroups", log.Fields{"NumOfGroups": numOfGroups})
Naveen Sampath04696f72022-06-13 15:19:14 +0530720 //TODO: Temp change to resolve group id issue in pOLT
721 //for i := 1; uint32(i) <= numOfGroups; i++ {
722 for i := 2; uint32(i) <= (numOfGroups + 1); i++ {
723 ig := IgmpGroup{}
724 ig.GroupID = uint32(i)
725 va.IgmpGroupIds = append(va.IgmpGroupIds, &ig)
726 }
727}
728
729// GetAvailIgmpGroupID to get id of available igmp group.
730func (va *VoltApplication) GetAvailIgmpGroupID() *IgmpGroup {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530731 logger.Info(ctx, "GetAvailIgmpGroupID")
Naveen Sampath04696f72022-06-13 15:19:14 +0530732 var ig *IgmpGroup
733 if len(va.IgmpGroupIds) > 0 {
734 ig, va.IgmpGroupIds = va.IgmpGroupIds[0], va.IgmpGroupIds[1:]
735 return ig
736 }
737 return nil
738}
739
740// GetIgmpGroupID to get id of igmp group.
741func (va *VoltApplication) GetIgmpGroupID(gid uint32) (*IgmpGroup, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530742 logger.Info(ctx, "GetIgmpGroupID")
Naveen Sampath04696f72022-06-13 15:19:14 +0530743 for id, ig := range va.IgmpGroupIds {
744 if ig.GroupID == gid {
745 va.IgmpGroupIds = append(va.IgmpGroupIds[0:id], va.IgmpGroupIds[id+1:]...)
746 return ig, nil
747 }
748 }
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530749 return nil, errors.New("group id missing")
Naveen Sampath04696f72022-06-13 15:19:14 +0530750}
751
752// PutIgmpGroupID to add id of igmp group.
753func (va *VoltApplication) PutIgmpGroupID(ig *IgmpGroup) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530754 logger.Debugw(ctx, "GetIgmpGroupID", log.Fields{"GroupID": ig.GroupID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530755 va.IgmpGroupIds = append([]*IgmpGroup{ig}, va.IgmpGroupIds[0:]...)
756}
757
vinokuma926cb3e2023-03-29 11:41:06 +0530758// RestoreUpgradeStatus - gets upgrade/migration status from DB and updates local flags
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530759func (va *VoltApplication) RestoreUpgradeStatus(cntx context.Context) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530760 logger.Info(ctx, "Received Restore Upgrade Status")
Naveen Sampath04696f72022-06-13 15:19:14 +0530761 Migrate := new(DataMigration)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530762 if err := GetMigrationInfo(cntx, Migrate); err == nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530763 if Migrate.Status == MigrationInProgress {
764 isUpgradeComplete = false
765 return
766 }
767 }
768 isUpgradeComplete = true
769
770 logger.Infow(ctx, "Upgrade Status Restored", log.Fields{"Upgrade Completed": isUpgradeComplete})
771}
772
773// ReadAllFromDb : If we are restarted, learn from the database the current execution
774// stage
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530775func (va *VoltApplication) ReadAllFromDb(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530776 logger.Info(ctx, "Reading the meters from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530777 va.RestoreMetersFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530778 logger.Info(ctx, "Reading the VNETs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530779 va.RestoreVnetsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530780 logger.Info(ctx, "Reading the VPVs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530781 va.RestoreVpvsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530782 logger.Info(ctx, "Reading the Services from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530783 va.RestoreSvcsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530784 logger.Info(ctx, "Reading the MVLANs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530785 va.RestoreMvlansFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530786 logger.Info(ctx, "Reading the IGMP profiles from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530787 va.RestoreIGMPProfilesFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530788 logger.Info(ctx, "Reading the Mcast configs from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530789 va.RestoreMcastConfigsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530790 logger.Info(ctx, "Reading the IGMP groups for DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530791 va.RestoreIgmpGroupsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530792 logger.Info(ctx, "Reading Upgrade status from DB")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530793 va.RestoreUpgradeStatus(cntx)
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530794 logger.Info(ctx, "Reading OltFlowService from DB")
795 va.RestoreOltFlowService(cntx)
Akash Sonia8246972023-01-03 10:37:08 +0530796 logger.Info(ctx, "Reading device config from DB")
797 va.RestoreDeviceConfigFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530798 logger.Info(ctx, "Reconciled from DB")
799}
800
vinokuma926cb3e2023-03-29 11:41:06 +0530801// InitStaticConfig to initialize static config.
Naveen Sampath04696f72022-06-13 15:19:14 +0530802func (va *VoltApplication) InitStaticConfig() {
803 va.InitIgmpSrcMac()
804}
805
806// SetVendorID to set vendor id
807func (va *VoltApplication) SetVendorID(vendorID string) {
808 va.vendorID = vendorID
809}
810
811// GetVendorID to get vendor id
812func (va *VoltApplication) GetVendorID() string {
813 return va.vendorID
814}
815
816// SetRebootFlag to set reboot flag
817func (va *VoltApplication) SetRebootFlag(flag bool) {
818 vgcRebooted = flag
819}
820
821// GetUpgradeFlag to get reboot status
822func (va *VoltApplication) GetUpgradeFlag() bool {
823 return isUpgradeComplete
824}
825
826// SetUpgradeFlag to set reboot status
827func (va *VoltApplication) SetUpgradeFlag(flag bool) {
828 isUpgradeComplete = flag
829}
830
831// ------------------------------------------------------------
832// Device related functions
833
834// AddDevice : Add a device and typically the device stores the NNI port on the device
835// The NNI port is used when the packets are emitted towards the network.
836// The outport is selected as the NNI port of the device. Today, we support
837// a single NNI port per OLT. This is true whether the network uses any
838// protection mechanism (LAG, ERPS, etc.). The aggregate of the such protection
839// is represented by a single NNI port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530840func (va *VoltApplication) AddDevice(cntx context.Context, device string, slno, southBoundID string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530841 logger.Debugw(ctx, "Received Device Ind: Add", log.Fields{"Device": device, "SrNo": slno, "southBoundID": southBoundID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530842 if _, ok := va.DevicesDisc.Load(device); ok {
843 logger.Warnw(ctx, "Device Exists", log.Fields{"Device": device})
844 }
845 d := NewVoltDevice(device, slno, southBoundID)
846
847 addPort := func(key, value interface{}) bool {
848 portID := key.(uint32)
849 port := value.(*PonPortCfg)
850 va.AggActiveChannelsCountForPonPort(device, portID, port)
851 d.ActiveChannelsPerPon.Store(portID, port)
852 return true
853 }
854 if nbDevice, exists := va.NbDevice.Load(southBoundID); exists {
855 // Pon Ports added before OLT activate.
856 nbDevice.(*NbDevice).PonPorts.Range(addPort)
857 } else {
858 // Check if NbPort exists in DB. VGC restart case.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530859 nbd := va.RestoreNbDeviceFromDb(cntx, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530860 nbd.PonPorts.Range(addPort)
861 }
862 va.DevicesDisc.Store(device, d)
863}
864
865// GetDevice to get a device.
866func (va *VoltApplication) GetDevice(device string) *VoltDevice {
867 if d, ok := va.DevicesDisc.Load(device); ok {
868 return d.(*VoltDevice)
869 }
870 return nil
871}
872
873// DelDevice to delete a device.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530874func (va *VoltApplication) DelDevice(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530875 logger.Debugw(ctx, "Received Device Ind: Delete", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +0530876 if vdIntf, ok := va.DevicesDisc.Load(device); ok {
877 vd := vdIntf.(*VoltDevice)
878 va.DevicesDisc.Delete(device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530879 _ = db.DelAllRoutesForDevice(cntx, device)
880 va.HandleFlowClearFlag(cntx, device, vd.SerialNum, vd.SouthBoundID)
881 _ = db.DelAllGroup(cntx, device)
882 _ = db.DelAllMeter(cntx, device)
883 _ = db.DelAllPorts(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530884 logger.Debugw(ctx, "Device deleted", log.Fields{"Device": device})
885 } else {
886 logger.Warnw(ctx, "Device Doesn't Exist", log.Fields{"Device": device})
887 }
888}
889
890// GetDeviceBySerialNo to get a device by serial number.
891// TODO - Transform this into a MAP instead
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530892func (va *VoltApplication) GetDeviceBySerialNo(slno string) (*VoltDevice, string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530893 logger.Debugw(ctx, "Received Device Ind: Get", log.Fields{"Serial Num": slno})
Naveen Sampath04696f72022-06-13 15:19:14 +0530894 var device *VoltDevice
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530895 var deviceID string
Naveen Sampath04696f72022-06-13 15:19:14 +0530896 getserial := func(key interface{}, value interface{}) bool {
897 device = value.(*VoltDevice)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530898 deviceID = key.(string)
Naveen Sampath04696f72022-06-13 15:19:14 +0530899 return device.SerialNum != slno
900 }
901 va.DevicesDisc.Range(getserial)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530902 return device, deviceID
Naveen Sampath04696f72022-06-13 15:19:14 +0530903}
904
905// PortAddInd : This is a PORT add indication coming from the VPAgent, which is essentially
906// a request coming from VOLTHA. The device and identity of the port is provided
907// in this request. Add them to the application for further use
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530908func (va *VoltApplication) PortAddInd(cntx context.Context, device string, id uint32, portName string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530909 logger.Debugw(ctx, "Received Port Ind: Add", log.Fields{"Device": device, "ID": id, "Port": portName})
Naveen Sampath04696f72022-06-13 15:19:14 +0530910 va.portLock.Lock()
911 if d := va.GetDevice(device); d != nil {
912 p := d.AddPort(portName, id)
913 va.PortsDisc.Store(portName, p)
914 va.portLock.Unlock()
915 nni, _ := va.GetNniPort(device)
916 if nni == portName {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530917 d.pushFlowsForUnis(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530918 }
919 } else {
920 va.portLock.Unlock()
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530921 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Add", log.Fields{"Device": device, "ID": id, "Port": portName})
Naveen Sampath04696f72022-06-13 15:19:14 +0530922 }
923}
924
925// PortDelInd : Only the NNI ports are recorded in the device for now. When port delete
926// arrives, only the NNI ports need adjustments.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530927func (va *VoltApplication) PortDelInd(cntx context.Context, device string, port string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530928 logger.Debugw(ctx, "Received Port Ind: Delete", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530929 if d := va.GetDevice(device); d != nil {
930 p := d.GetPort(port)
931 if p != nil && p.State == PortStateUp {
932 logger.Infow(ctx, "Port state is UP. Trigerring Port Down Ind before deleting", log.Fields{"Port": p})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530933 va.PortDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530934 }
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530935 // if RemoveFlowsOnDisable is flase, then flows will be existing till port delete. Remove the flows now
936 if !va.OltFlowServiceConfig.RemoveFlowsOnDisable {
Akash Sonia8246972023-01-03 10:37:08 +0530937 vpvs, ok := va.VnetsByPort.Load(port)
938 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530939 logger.Infow(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
940 } else {
941 for _, vpv := range vpvs.([]*VoltPortVnet) {
942 vpv.VpvLock.Lock()
Sridhar Ravindra03aa0bf2023-09-12 17:46:40 +0530943 // Set delFlowsInDevice to true to delete flows only in DB/device during Port Delete.
944 vpv.PortDownInd(cntx, device, port, true, true)
Tinoj Joseph4ead4e02023-01-30 03:12:44 +0530945 vpv.VpvLock.Unlock()
946 }
947 }
948 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530949 va.portLock.Lock()
950 defer va.portLock.Unlock()
951 d.DelPort(port)
952 if _, ok := va.PortsDisc.Load(port); ok {
953 va.PortsDisc.Delete(port)
954 }
955 } else {
956 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Delete", log.Fields{"Device": device, "Port": port})
957 }
958}
959
vinokuma926cb3e2023-03-29 11:41:06 +0530960// PortUpdateInd Updates port Id incase of ONU movement
Naveen Sampath04696f72022-06-13 15:19:14 +0530961func (va *VoltApplication) PortUpdateInd(device string, portName string, id uint32) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530962 logger.Debugw(ctx, "Received Port Ind: Update", log.Fields{"Device": device, "Port": portName, "ID": id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530963 va.portLock.Lock()
964 defer va.portLock.Unlock()
965 if d := va.GetDevice(device); d != nil {
966 vp := d.GetPort(portName)
967 vp.ID = id
968 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530969 logger.Warnw(ctx, "Device Not Found", log.Fields{"Device": device, "Port": portName, "ID": id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530970 }
971}
972
973// AddNbPonPort Add pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530974func (va *VoltApplication) AddNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530975 enableMulticastKPI bool, portAlarmProfileID string) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530976 logger.Debugw(ctx, "Received NbPonPort Ind: Add", log.Fields{"oltSbID": oltSbID, "portID": portID, "maxAllowedChannels": maxAllowedChannels, "enableMulticastKPI": enableMulticastKPI, "portAlarmProfileID": portAlarmProfileID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530977 var nbd *NbDevice
978 nbDevice, ok := va.NbDevice.Load(oltSbID)
979
980 if !ok {
981 nbd = NewNbDevice()
982 nbd.SouthBoundID = oltSbID
983 } else {
984 nbd = nbDevice.(*NbDevice)
985 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530986 port := nbd.AddPortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530987 logger.Debugw(ctx, "Added Port To NbDevice", log.Fields{"port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530988 // Add this port to voltDevice
989 addPort := func(key, value interface{}) bool {
990 voltDevice := value.(*VoltDevice)
991 if oltSbID == voltDevice.SouthBoundID {
992 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); !exists {
993 voltDevice.ActiveChannelsPerPon.Store(portID, port)
994 }
995 return false
996 }
997 return true
998 }
999 va.DevicesDisc.Range(addPort)
1000 va.NbDevice.Store(oltSbID, nbd)
1001
1002 return nil
1003}
1004
1005// UpdateNbPonPort update pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301006func (va *VoltApplication) UpdateNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301007 logger.Debugw(ctx, "Received NbPonPort Ind: Update", log.Fields{"oltSbID": oltSbID, "portID": portID, "maxAllowedChannels": maxAllowedChannels, "enableMulticastKPI": enableMulticastKPI, "portAlarmProfileID": portAlarmProfileID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301008 var nbd *NbDevice
1009 nbDevice, ok := va.NbDevice.Load(oltSbID)
1010
1011 if !ok {
1012 logger.Errorw(ctx, "Device-doesn't-exists", log.Fields{"deviceID": oltSbID})
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301013 return fmt.Errorf("device-doesn't-exists - %s", oltSbID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301014 }
1015 nbd = nbDevice.(*NbDevice)
1016
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301017 port := nbd.UpdatePortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301018 if port == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301019 return fmt.Errorf("port-doesn't-exists-%d", portID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301020 }
1021 va.NbDevice.Store(oltSbID, nbd)
1022
1023 // Add this port to voltDevice
1024 updPort := func(key, value interface{}) bool {
1025 voltDevice := value.(*VoltDevice)
1026 if oltSbID == voltDevice.SouthBoundID {
1027 voltDevice.ActiveChannelCountLock.Lock()
1028 if p, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
1029 oldPort := p.(*PonPortCfg)
1030 if port.MaxActiveChannels != 0 {
1031 oldPort.MaxActiveChannels = port.MaxActiveChannels
1032 oldPort.EnableMulticastKPI = port.EnableMulticastKPI
1033 voltDevice.ActiveChannelsPerPon.Store(portID, oldPort)
1034 }
1035 }
1036 voltDevice.ActiveChannelCountLock.Unlock()
1037 return false
1038 }
1039 return true
1040 }
1041 va.DevicesDisc.Range(updPort)
1042
1043 return nil
1044}
1045
1046// DeleteNbPonPort Delete pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301047func (va *VoltApplication) DeleteNbPonPort(cntx context.Context, oltSbID string, portID uint32) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301048 logger.Debugw(ctx, "Received NbPonPort Ind: Delete", log.Fields{"oltSbID": oltSbID, "portID": portID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301049 nbDevice, ok := va.NbDevice.Load(oltSbID)
1050 if ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301051 nbDevice.(*NbDevice).DeletePortFromNbDevice(cntx, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301052 va.NbDevice.Store(oltSbID, nbDevice.(*NbDevice))
1053 } else {
1054 logger.Warnw(ctx, "Delete pon received for unknown device", log.Fields{"oltSbID": oltSbID})
1055 return nil
1056 }
1057 // Delete this port from voltDevice
1058 delPort := func(key, value interface{}) bool {
1059 voltDevice := value.(*VoltDevice)
1060 if oltSbID == voltDevice.SouthBoundID {
1061 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
1062 voltDevice.ActiveChannelsPerPon.Delete(portID)
1063 }
1064 return false
1065 }
1066 return true
1067 }
1068 va.DevicesDisc.Range(delPort)
1069 return nil
1070}
1071
1072// GetNniPort : Get the NNI port for a device. Called from different other applications
1073// as a port to match or destination for a packet out. The VOLT application
1074// is written with the assumption that there is a single NNI port. The OLT
1075// device is responsible for translating the combination of VLAN and the
1076// NNI port ID to identify possibly a single physical port or a logical
1077// port which is a result of protection methods applied.
1078func (va *VoltApplication) GetNniPort(device string) (string, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301079 logger.Debugw(ctx, "NNI Get Ind", log.Fields{"device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301080 va.portLock.Lock()
1081 defer va.portLock.Unlock()
1082 d, ok := va.DevicesDisc.Load(device)
1083 if !ok {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301084 return "", errors.New("device doesn't exist")
Naveen Sampath04696f72022-06-13 15:19:14 +05301085 }
1086 return d.(*VoltDevice).NniPort, nil
1087}
1088
1089// NniDownInd process for Nni down indication.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301090func (va *VoltApplication) NniDownInd(cntx context.Context, deviceID string, devSrNo string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301091 logger.Debugw(ctx, "NNI Down Ind", log.Fields{"DeviceID": deviceID, "Device SrNo": devSrNo})
Naveen Sampath04696f72022-06-13 15:19:14 +05301092
1093 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1094 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301095 mvProfile.removeIgmpMcastFlows(cntx, devSrNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301096 return true
1097 }
1098 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1099
1100 //Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301101 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301102}
1103
1104// DeviceUpInd changes device state to up.
1105func (va *VoltApplication) DeviceUpInd(device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301106 logger.Infow(ctx, "Received Device Ind: UP", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301107 if d := va.GetDevice(device); d != nil {
1108 d.State = controller.DeviceStateUP
1109 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301110 logger.Warnw(ctx, "Ignoring Device indication: UP. Device Missing", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301111 }
1112}
1113
1114// DeviceDownInd changes device state to down.
1115func (va *VoltApplication) DeviceDownInd(device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301116 logger.Infow(ctx, "Received Device Ind: DOWN", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301117 if d := va.GetDevice(device); d != nil {
1118 d.State = controller.DeviceStateDOWN
1119 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301120 logger.Warnw(ctx, "Ignoring Device indication: DOWN. Device Missing", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301121 }
1122}
1123
1124// DeviceRebootInd process for handling flow clear flag for device reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301125func (va *VoltApplication) DeviceRebootInd(cntx context.Context, device string, serialNum string, southBoundID string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301126 logger.Infow(ctx, "Received Device Ind: Reboot", log.Fields{"Device": device, "SerialNumber": serialNum, "SouthBoundID": southBoundID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301127
1128 if d := va.GetDevice(device); d != nil {
1129 if d.State == controller.DeviceStateREBOOTED {
1130 logger.Warnw(ctx, "Ignoring Device Ind: Reboot, Device already in Reboot state", log.Fields{"Device": device, "SerialNumber": serialNum, "State": d.State})
1131 return
1132 }
1133 d.State = controller.DeviceStateREBOOTED
1134 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301135 va.HandleFlowClearFlag(cntx, device, serialNum, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301136}
1137
1138// DeviceDisableInd handles device deactivation process
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301139func (va *VoltApplication) DeviceDisableInd(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301140 logger.Infow(ctx, "Received Device Ind: Disable", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301141
1142 d := va.GetDevice(device)
1143 if d == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301144 logger.Warnw(ctx, "Ignoring Device indication: DISABLED. Device Missing", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301145 return
1146 }
1147
1148 d.State = controller.DeviceStateDISABLED
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301149 va.HandleFlowClearFlag(cntx, device, d.SerialNum, d.SouthBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301150}
1151
1152// ProcessIgmpDSFlowForMvlan for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301153func (va *VoltApplication) ProcessIgmpDSFlowForMvlan(cntx context.Context, d *VoltDevice, mvp *MvlanProfile, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301154 logger.Debugw(ctx, "Process IGMP DS Flows for MVlan", log.Fields{"device": d.Name, "Mvlan": mvp.Mvlan, "addFlow": addFlow})
1155 portState := false
1156 p := d.GetPort(d.NniPort)
1157 if p != nil && p.State == PortStateUp {
1158 portState = true
1159 }
1160
1161 if addFlow {
1162 if portState {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301163 mvp.pushIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301164 }
1165 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301166 mvp.removeIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301167 }
1168}
1169
1170// ProcessIgmpDSFlowForDevice for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301171func (va *VoltApplication) ProcessIgmpDSFlowForDevice(cntx context.Context, d *VoltDevice, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301172 logger.Debugw(ctx, "Process IGMP DS Flows for device", log.Fields{"device": d.Name, "addFlow": addFlow})
1173
1174 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1175 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301176 va.ProcessIgmpDSFlowForMvlan(cntx, d, mvProfile, addFlow)
Naveen Sampath04696f72022-06-13 15:19:14 +05301177 return true
1178 }
1179 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1180}
1181
1182// GetDeviceFromPort : This is suitable only for access ports as their naming convention
1183// makes them unique across all the OLTs. This must be called with
1184// port name that is an access port. Currently called from VNETs, attached
1185// only to access ports, and the services which are also attached only
1186// to access ports
1187func (va *VoltApplication) GetDeviceFromPort(port string) (*VoltDevice, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301188 logger.Debugw(ctx, "Received Get Device From Port", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301189 va.portLock.Lock()
1190 defer va.portLock.Unlock()
1191 var err error
1192 err = nil
1193 p, ok := va.PortsDisc.Load(port)
1194 if !ok {
1195 return nil, errorCodes.ErrPortNotFound
1196 }
1197 d := va.GetDevice(p.(*VoltPort).Device)
1198 if d == nil {
1199 err = errorCodes.ErrDeviceNotFound
1200 }
1201 return d, err
1202}
1203
1204// GetPortID : This too applies only to access ports. The ports can be indexed
1205// purely by their names without the device forming part of the key
1206func (va *VoltApplication) GetPortID(port string) (uint32, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301207 logger.Debugw(ctx, "Received Get Port ID", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301208 va.portLock.Lock()
1209 defer va.portLock.Unlock()
1210 p, ok := va.PortsDisc.Load(port)
1211 if !ok {
1212 return 0, errorCodes.ErrPortNotFound
1213 }
1214 return p.(*VoltPort).ID, nil
1215}
1216
1217// GetPortName : This too applies only to access ports. The ports can be indexed
1218// purely by their names without the device forming part of the key
1219func (va *VoltApplication) GetPortName(port uint32) (string, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301220 logger.Debugw(ctx, "Received Get Port Name", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301221 va.portLock.Lock()
1222 defer va.portLock.Unlock()
1223 var portName string
1224 va.PortsDisc.Range(func(key interface{}, value interface{}) bool {
1225 portInfo := value.(*VoltPort)
1226 if portInfo.ID == port {
1227 portName = portInfo.Name
1228 return false
1229 }
1230 return true
1231 })
1232 return portName, nil
1233}
1234
1235// GetPonFromUniPort to get Pon info from UniPort
1236func (va *VoltApplication) GetPonFromUniPort(port string) (string, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301237 logger.Debugw(ctx, "Received Get Pon From UniPort", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301238 uniPortID, err := va.GetPortID(port)
1239 if err == nil {
1240 ponPortID := (uniPortID & 0x0FF00000) >> 20 //pon(8) + onu(8) + uni(12)
1241 return strconv.FormatUint(uint64(ponPortID), 10), nil
1242 }
1243 return "", err
1244}
1245
1246// GetPortState : This too applies only to access ports. The ports can be indexed
1247// purely by their names without the device forming part of the key
1248func (va *VoltApplication) GetPortState(port string) (PortState, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301249 logger.Debugw(ctx, "Received Get Port State", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301250 va.portLock.Lock()
1251 defer va.portLock.Unlock()
1252 p, ok := va.PortsDisc.Load(port)
1253 if !ok {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301254 return 0, errors.New("port not configured")
Naveen Sampath04696f72022-06-13 15:19:14 +05301255 }
1256 return p.(*VoltPort).State, nil
1257}
1258
1259// GetIcmpv6Receivers to get Icmp v6 receivers
1260func (va *VoltApplication) GetIcmpv6Receivers(device string) []uint32 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301261 logger.Debugw(ctx, "Get Icmpv6 Receivers", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301262 var receiverList []uint32
1263 receivers, _ := va.Icmpv6Receivers.Load(device)
1264 if receivers != nil {
1265 receiverList = receivers.([]uint32)
1266 }
1267 return receiverList
1268}
1269
1270// AddIcmpv6Receivers to add Icmp v6 receivers
1271func (va *VoltApplication) AddIcmpv6Receivers(device string, portID uint32) []uint32 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301272 logger.Debugw(ctx, "Received Add Icmpv6 Receivers", log.Fields{"device": device, "portID": portID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301273 var receiverList []uint32
1274 receivers, _ := va.Icmpv6Receivers.Load(device)
1275 if receivers != nil {
1276 receiverList = receivers.([]uint32)
1277 }
1278 receiverList = append(receiverList, portID)
1279 va.Icmpv6Receivers.Store(device, receiverList)
1280 logger.Debugw(ctx, "Receivers after addition", log.Fields{"Receivers": receiverList})
1281 return receiverList
1282}
1283
1284// DelIcmpv6Receivers to delete Icmp v6 receievers
1285func (va *VoltApplication) DelIcmpv6Receivers(device string, portID uint32) []uint32 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301286 logger.Debugw(ctx, "Received Add Icmpv6 Receivers", log.Fields{"device": device, "portID": portID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301287 var receiverList []uint32
1288 receivers, _ := va.Icmpv6Receivers.Load(device)
1289 if receivers != nil {
1290 receiverList = receivers.([]uint32)
1291 }
1292 for i, port := range receiverList {
1293 if port == portID {
1294 receiverList = append(receiverList[0:i], receiverList[i+1:]...)
1295 va.Icmpv6Receivers.Store(device, receiverList)
1296 break
1297 }
1298 }
1299 logger.Debugw(ctx, "Receivers After deletion", log.Fields{"Receivers": receiverList})
1300 return receiverList
1301}
1302
1303// ProcessDevFlowForDevice - Process DS ICMPv6 & ARP flow for provided device and vnet profile
1304// device - Device Obj
1305// vnet - vnet profile name
1306// enabled - vlan enabled/disabled - based on the status, the flow shall be added/removed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301307func (va *VoltApplication) ProcessDevFlowForDevice(cntx context.Context, device *VoltDevice, vnet *VoltVnet, enabled bool) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301308 logger.Debugw(ctx, "Process Dev Flow For Device", log.Fields{"Device": device, "VnetName": vnet.Name, "Enabled": enabled})
Naveen Sampath04696f72022-06-13 15:19:14 +05301309 _, applied := device.ConfiguredVlanForDeviceFlows.Get(VnetKey(vnet.SVlan, vnet.CVlan, 0))
1310 if enabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301311 va.PushDevFlowForVlan(cntx, vnet)
Naveen Sampath04696f72022-06-13 15:19:14 +05301312 } else if !enabled && applied {
1313 //va.DeleteDevFlowForVlan(vnet)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301314 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, device.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301315 }
1316}
1317
vinokuma926cb3e2023-03-29 11:41:06 +05301318// NniVlanIndToIgmp - Trigger receiver up indication to all ports with igmp enabled
1319// and has the provided mvlan
Naveen Sampath04696f72022-06-13 15:19:14 +05301320func (va *VoltApplication) NniVlanIndToIgmp(device *VoltDevice, mvp *MvlanProfile) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301321 logger.Infow(ctx, "Received Nni Vlan Ind To Igmp", log.Fields{"Vlan": mvp.Mvlan})
Naveen Sampath04696f72022-06-13 15:19:14 +05301322
vinokuma926cb3e2023-03-29 11:41:06 +05301323 // Trigger nni indication for receiver only for first time
Naveen Sampath04696f72022-06-13 15:19:14 +05301324 if device.IgmpDsFlowAppliedForMvlan[uint16(mvp.Mvlan)] {
1325 return
1326 }
1327 device.Ports.Range(func(key, value interface{}) bool {
1328 port := key.(string)
1329
1330 if state, _ := va.GetPortState(port); state == PortStateUp {
1331 vpvs, _ := va.VnetsByPort.Load(port)
1332 if vpvs == nil {
1333 return true
1334 }
1335 for _, vpv := range vpvs.([]*VoltPortVnet) {
vinokuma926cb3e2023-03-29 11:41:06 +05301336 // Send indication only for subscribers with the received mvlan profile
Naveen Sampath04696f72022-06-13 15:19:14 +05301337 if vpv.IgmpEnabled && vpv.MvlanProfileName == mvp.Name {
1338 vpv.services.Range(ReceiverUpInd)
1339 }
1340 }
1341 }
1342 return true
1343 })
1344}
1345
1346// PortUpInd :
1347// -----------------------------------------------------------------------
1348// Port status change handling
1349// ----------------------------------------------------------------------
1350// Port UP indication is passed to all services associated with the port
1351// so that the services can configure flows applicable when the port goes
1352// up from down state
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301353func (va *VoltApplication) PortUpInd(cntx context.Context, device string, port string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301354 logger.Infow(ctx, "Received Southbound Port Ind: UP", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301355 d := va.GetDevice(device)
1356
1357 if d == nil {
1358 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: UP", log.Fields{"Device": device, "Port": port})
1359 return
1360 }
1361
vinokuma926cb3e2023-03-29 11:41:06 +05301362 // Fixme: If Port Update Comes in large numbers, this will result in slow update per device
Naveen Sampath04696f72022-06-13 15:19:14 +05301363 va.portLock.Lock()
1364 // Do not defer the port mutex unlock here
1365 // Some of the following func calls needs the port lock, so defering the lock here
1366 // may lead to dead-lock
1367 p := d.GetPort(port)
1368
1369 if p == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301370 logger.Warnw(ctx, "Ignoring Port Ind: UP, Port doesnt exist", log.Fields{"Device": device, "PortName": port, "PortId": p})
Naveen Sampath04696f72022-06-13 15:19:14 +05301371 va.portLock.Unlock()
1372 return
1373 }
1374 p.State = PortStateUp
1375 va.portLock.Unlock()
1376
Naveen Sampath04696f72022-06-13 15:19:14 +05301377 if p.Type == VoltPortTypeNni {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301378 logger.Debugw(ctx, "Received NNI Port Ind: UP", log.Fields{"Device": device, "PortName": port, "PortId": p.ID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301379 //va.PushDevFlowForDevice(d)
1380 //Build Igmp TrapFlowRule
1381 //va.ProcessIgmpDSFlowForDevice(d, true)
1382 }
1383 vpvs, ok := va.VnetsByPort.Load(port)
1384 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301385 logger.Warnw(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301386 //msgbus.ProcessPortInd(msgbus.PortUp, d.SerialNum, p.Name, false, getServiceList(port))
1387 return
1388 }
1389
vinokuma926cb3e2023-03-29 11:41:06 +05301390 // If NNI port is not UP, do not push Flows
Naveen Sampath04696f72022-06-13 15:19:14 +05301391 if d.NniPort == "" {
1392 logger.Warnw(ctx, "NNI port not UP. Not sending Port UP Ind for VPVs", log.Fields{"NNI": d.NniPort})
1393 return
1394 }
1395
Naveen Sampath04696f72022-06-13 15:19:14 +05301396 for _, vpv := range vpvs.([]*VoltPortVnet) {
1397 vpv.VpvLock.Lock()
vinokuma926cb3e2023-03-29 11:41:06 +05301398 // If no service is activated drop the portUpInd
Tinoj Josephec742f62022-09-29 19:11:10 +05301399 if vpv.IsServiceActivated(cntx) {
vinokuma926cb3e2023-03-29 11:41:06 +05301400 // Do not trigger indication for the vpv which is already removed from vpv list as
Tinoj Josephec742f62022-09-29 19:11:10 +05301401 // part of service delete (during the lock wait duration)
1402 // In that case, the services associated wil be zero
1403 if vpv.servicesCount.Load() != 0 {
1404 vpv.PortUpInd(cntx, d, port)
1405 }
1406 } else {
1407 // Service not activated, still attach device to service
1408 vpv.setDevice(d.Name)
Naveen Sampath04696f72022-06-13 15:19:14 +05301409 }
1410 vpv.VpvLock.Unlock()
1411 }
1412 // At the end of processing inform the other entities that
1413 // are interested in the events
1414}
1415
1416/*
1417func getServiceList(port string) map[string]bool {
1418 serviceList := make(map[string]bool)
1419
1420 getServiceNames := func(key interface{}, value interface{}) bool {
1421 serviceList[key.(string)] = value.(*VoltService).DsHSIAFlowsApplied
1422 return true
1423 }
1424
1425 if vpvs, _ := GetApplication().VnetsByPort.Load(port); vpvs != nil {
1426 vpvList := vpvs.([]*VoltPortVnet)
1427 for _, vpv := range vpvList {
1428 vpv.services.Range(getServiceNames)
1429 }
1430 }
1431 return serviceList
1432
1433}*/
1434
vinokuma926cb3e2023-03-29 11:41:06 +05301435// ReceiverUpInd - Send receiver up indication for service with Igmp enabled
Naveen Sampath04696f72022-06-13 15:19:14 +05301436func ReceiverUpInd(key, value interface{}) bool {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301437 logger.Info(ctx, "Receiver Indication: UP")
Naveen Sampath04696f72022-06-13 15:19:14 +05301438 svc := value.(*VoltService)
1439 var vlan of.VlanType
1440
1441 if !svc.IPAssigned() {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301442 logger.Warnw(ctx, "IP Not assigned, skipping general query", log.Fields{"Service": svc})
Naveen Sampath04696f72022-06-13 15:19:14 +05301443 return false
1444 }
1445
vinokuma926cb3e2023-03-29 11:41:06 +05301446 // Send port up indication to igmp only for service with igmp enabled
Naveen Sampath04696f72022-06-13 15:19:14 +05301447 if svc.IgmpEnabled {
1448 if svc.VlanControl == ONUCVlan || svc.VlanControl == ONUCVlanOLTSVlan {
1449 vlan = svc.CVlan
1450 } else {
1451 vlan = svc.UniVlan
1452 }
1453 if device, _ := GetApplication().GetDeviceFromPort(svc.Port); device != nil {
1454 GetApplication().ReceiverUpInd(device.Name, svc.Port, svc.MvlanProfileName, vlan, svc.Pbits)
1455 }
1456 return false
1457 }
1458 return true
1459}
1460
1461// PortDownInd : Port down indication is passed on to the services so that the services
1462// can make changes at this transition.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301463func (va *VoltApplication) PortDownInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301464 logger.Infow(ctx, "Received SouthBound Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1465 d := va.GetDevice(device)
1466
1467 if d == nil {
1468 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1469 return
1470 }
vinokuma926cb3e2023-03-29 11:41:06 +05301471 // Fixme: If Port Update Comes in large numbers, this will result in slow update per device
Naveen Sampath04696f72022-06-13 15:19:14 +05301472 va.portLock.Lock()
1473 // Do not defer the port mutex unlock here
1474 // Some of the following func calls needs the port lock, so defering the lock here
1475 // may lead to dead-lock
1476 p := d.GetPort(port)
1477 if p == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301478 logger.Warnw(ctx, "Ignoring Port Ind: Down, Port doesnt exist", log.Fields{"Device": device, "PortName": port, "PortId": p})
Naveen Sampath04696f72022-06-13 15:19:14 +05301479 va.portLock.Unlock()
1480 return
1481 }
1482 p.State = PortStateDown
1483 va.portLock.Unlock()
1484
1485 if d.State == controller.DeviceStateREBOOTED {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301486 logger.Warnw(ctx, "Ignoring Port Ind: Down, Device has been Rebooted", log.Fields{"Device": device, "PortName": port, "PortId": p})
Naveen Sampath04696f72022-06-13 15:19:14 +05301487 return
1488 }
1489
1490 if p.Type == VoltPortTypeNni {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301491 logger.Debugw(ctx, "Received NNI Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301492 va.DeleteDevFlowForDevice(cntx, d)
1493 va.NniDownInd(cntx, device, d.SerialNum)
1494 va.RemovePendingGroups(cntx, device, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301495 }
1496 vpvs, ok := va.VnetsByPort.Load(port)
1497 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301498 logger.Warnw(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301499 //msgbus.ProcessPortInd(msgbus.PortDown, d.SerialNum, p.Name, false, getServiceList(port))
1500 return
1501 }
Akash Sonia8246972023-01-03 10:37:08 +05301502
Naveen Sampath04696f72022-06-13 15:19:14 +05301503 for _, vpv := range vpvs.([]*VoltPortVnet) {
1504 vpv.VpvLock.Lock()
Sridhar Ravindra03aa0bf2023-09-12 17:46:40 +05301505 vpv.PortDownInd(cntx, device, port, false, false)
Naveen Sampath04696f72022-06-13 15:19:14 +05301506 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301507 va.ReceiverDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +05301508 }
1509 vpv.VpvLock.Unlock()
1510 }
1511}
1512
1513// PacketInInd :
1514// -----------------------------------------------------------------------
1515// PacketIn Processing
1516// Packet In Indication processing. It arrives with the identities of
1517// the device and port on which the packet is received. At first, the
1518// packet is decoded and the right processor is called. Currently, we
1519// plan to support only DHCP and IGMP. In future, we can add more
1520// capabilities as needed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301521func (va *VoltApplication) PacketInInd(cntx context.Context, device string, port string, pkt []byte) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301522 logger.Infow(ctx, "Received a Packet-In Indication", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301523 // Decode the incoming packet
1524 packetSide := US
1525 if strings.Contains(port, NNI) {
1526 packetSide = DS
1527 }
1528
Naveen Sampath04696f72022-06-13 15:19:14 +05301529 gopkt := gopacket.NewPacket(pkt, layers.LayerTypeEthernet, gopacket.Default)
1530
1531 var dot1qFound = false
1532 for _, l := range gopkt.Layers() {
1533 if l.LayerType() == layers.LayerTypeDot1Q {
1534 dot1qFound = true
1535 break
1536 }
1537 }
1538
1539 if !dot1qFound {
1540 logger.Debugw(ctx, "Ignoring Received Packet-In Indication without Dot1Q Header",
1541 log.Fields{"Device": device, "Port": port})
1542 return
1543 }
1544
1545 logger.Debugw(ctx, "Received Southbound Packet In", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1546
1547 // Classify the packet into packet types that we support
1548 // The supported types are DHCP and IGMP. The DHCP packet is
1549 // identified by matching the L4 protocol to UDP. The IGMP packet
1550 // is identified by matching L3 protocol to IGMP
1551 arpl := gopkt.Layer(layers.LayerTypeARP)
1552 if arpl != nil {
1553 if callBack, ok := PacketHandlers[ARP]; 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, "ARP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1557 }
1558 return
1559 }
1560 ipv4l := gopkt.Layer(layers.LayerTypeIPv4)
1561 if ipv4l != nil {
1562 ip := ipv4l.(*layers.IPv4)
1563
1564 if ip.Protocol == layers.IPProtocolUDP {
1565 logger.Debugw(ctx, "Received Southbound UDP ipv4 packet in", log.Fields{"StreamSide": packetSide})
1566 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv4)
1567 if dhcpl != nil {
1568 if callBack, ok := PacketHandlers[DHCPv4]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301569 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301570 } else {
1571 logger.Debugw(ctx, "DHCPv4 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1572 }
1573 }
1574 } else if ip.Protocol == layers.IPProtocolIGMP {
1575 logger.Debugw(ctx, "Received Southbound IGMP packet in", log.Fields{"StreamSide": packetSide})
1576 if callBack, ok := PacketHandlers[IGMP]; 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, "IGMP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1580 }
1581 }
1582 return
1583 }
1584 ipv6l := gopkt.Layer(layers.LayerTypeIPv6)
1585 if ipv6l != nil {
1586 ip := ipv6l.(*layers.IPv6)
1587 if ip.NextHeader == layers.IPProtocolUDP {
1588 logger.Debug(ctx, "Received Southbound UDP ipv6 packet in")
1589 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv6)
1590 if dhcpl != nil {
1591 if callBack, ok := PacketHandlers[DHCPv6]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301592 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301593 } else {
1594 logger.Debugw(ctx, "DHCPv6 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1595 }
1596 }
1597 }
1598 return
1599 }
1600
1601 pppoel := gopkt.Layer(layers.LayerTypePPPoE)
1602 if pppoel != nil {
1603 logger.Debugw(ctx, "Received Southbound PPPoE packet in", log.Fields{"StreamSide": packetSide})
1604 if callBack, ok := PacketHandlers[PPPOE]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301605 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301606 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301607 logger.Warnw(ctx, "PPPoE handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
Naveen Sampath04696f72022-06-13 15:19:14 +05301608 }
1609 }
1610}
1611
1612// GetVlans : This utility gets the VLANs from the packet. The VLANs are
1613// used to identify the right service that must process the incoming
1614// packet
1615func GetVlans(pkt gopacket.Packet) []of.VlanType {
1616 var vlans []of.VlanType
1617 for _, l := range pkt.Layers() {
1618 if l.LayerType() == layers.LayerTypeDot1Q {
1619 q, ok := l.(*layers.Dot1Q)
1620 if ok {
1621 vlans = append(vlans, of.VlanType(q.VLANIdentifier))
1622 }
1623 }
1624 }
1625 return vlans
1626}
1627
1628// GetPriority to get priority
1629func GetPriority(pkt gopacket.Packet) uint8 {
1630 for _, l := range pkt.Layers() {
1631 if l.LayerType() == layers.LayerTypeDot1Q {
1632 q, ok := l.(*layers.Dot1Q)
1633 if ok {
1634 return q.Priority
1635 }
1636 }
1637 }
1638 return PriorityNone
1639}
1640
1641// HandleFlowClearFlag to handle flow clear flag during reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301642func (va *VoltApplication) HandleFlowClearFlag(cntx context.Context, deviceID string, serialNum, southBoundID string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301643 logger.Infow(ctx, "Clear All flags for Device", log.Fields{"Device": deviceID, "SerialNum": serialNum, "SBID": southBoundID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301644 dev, ok := va.DevicesDisc.Load(deviceID)
1645 if ok && dev != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301646 logger.Debugw(ctx, "Clear Flags for device", log.Fields{"voltDevice": dev.(*VoltDevice).Name})
Naveen Sampath04696f72022-06-13 15:19:14 +05301647 dev.(*VoltDevice).icmpv6GroupAdded = false
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301648 logger.Debugw(ctx, "Clearing DS Icmpv6 Map",
Naveen Sampath04696f72022-06-13 15:19:14 +05301649 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1650 dev.(*VoltDevice).ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301651 logger.Debugw(ctx, "Clearing DS IGMP Map",
Naveen Sampath04696f72022-06-13 15:19:14 +05301652 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1653 for k := range dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan {
1654 delete(dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan, k)
1655 }
vinokuma926cb3e2023-03-29 11:41:06 +05301656 // Delete group 1 - ICMPv6/ARP group
Naveen Sampath04696f72022-06-13 15:19:14 +05301657 if err := ProcessIcmpv6McGroup(deviceID, true); err != nil {
1658 logger.Errorw(ctx, "ProcessIcmpv6McGroup failed", log.Fields{"Device": deviceID, "Error": err})
1659 }
1660 } else {
1661 logger.Warnw(ctx, "VoltDevice not found for device ", log.Fields{"deviceID": deviceID})
1662 }
1663
1664 getVpvs := func(key interface{}, value interface{}) bool {
1665 vpvs := value.([]*VoltPortVnet)
1666 for _, vpv := range vpvs {
1667 if vpv.Device == deviceID {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301668 logger.Debugw(ctx, "Clear Flags for vpv",
Naveen Sampath04696f72022-06-13 15:19:14 +05301669 log.Fields{"device": vpv.Device, "port": vpv.Port,
1670 "svlan": vpv.SVlan, "cvlan": vpv.CVlan, "univlan": vpv.UniVlan})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301671 vpv.ClearAllServiceFlags(cntx)
1672 vpv.ClearAllVpvFlags(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301673
1674 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301675 va.ReceiverDownInd(cntx, vpv.Device, vpv.Port)
vinokuma926cb3e2023-03-29 11:41:06 +05301676 // Also clear service igmp stats
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301677 vpv.ClearServiceCounters(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301678 }
1679 }
1680 }
1681 return true
1682 }
1683 va.VnetsByPort.Range(getVpvs)
1684
vinokuma926cb3e2023-03-29 11:41:06 +05301685 // Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301686 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301687
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301688 logger.Infow(ctx, "All flags cleared for device", log.Fields{"Device": deviceID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301689
vinokuma926cb3e2023-03-29 11:41:06 +05301690 // Reset pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301691 va.RemovePendingGroups(cntx, deviceID, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301692
vinokuma926cb3e2023-03-29 11:41:06 +05301693 // Process all Migrate Service Request - force udpate all profiles since resources are already cleaned up
Naveen Sampath04696f72022-06-13 15:19:14 +05301694 if dev != nil {
1695 triggerForceUpdate := func(key, value interface{}) bool {
1696 msrList := value.(*util.ConcurrentMap)
1697 forceUpdateServices := func(key, value interface{}) bool {
1698 msr := value.(*MigrateServicesRequest)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301699 forceUpdateAllServices(cntx, msr)
Naveen Sampath04696f72022-06-13 15:19:14 +05301700 return true
1701 }
1702 msrList.Range(forceUpdateServices)
1703 return true
1704 }
1705 dev.(*VoltDevice).MigratingServices.Range(triggerForceUpdate)
1706 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301707 va.FetchAndProcessAllMigrateServicesReq(cntx, deviceID, forceUpdateAllServices)
Naveen Sampath04696f72022-06-13 15:19:14 +05301708 }
1709}
1710
vinokuma926cb3e2023-03-29 11:41:06 +05301711// GetPonPortIDFromUNIPort to get pon port id from uni port
Naveen Sampath04696f72022-06-13 15:19:14 +05301712func GetPonPortIDFromUNIPort(uniPortID uint32) uint32 {
1713 ponPortID := (uniPortID & 0x0FF00000) >> 20
1714 return ponPortID
1715}
1716
vinokuma926cb3e2023-03-29 11:41:06 +05301717// ProcessFlowModResultIndication - Processes Flow mod operation indications from controller
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301718func (va *VoltApplication) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301719 logger.Debugw(ctx, "Received Flow Mod Result Indication.", log.Fields{"Cookie": flowStatus.Cookie, "Device": flowStatus.Device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301720 d := va.GetDevice(flowStatus.Device)
1721 if d == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301722 logger.Warnw(ctx, "Dropping Flow Mod Indication. Device not found", log.Fields{"Cookie": flowStatus.Cookie, "Device": flowStatus.Device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301723 return
1724 }
1725
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301726 cookieExists := ExecuteFlowEvent(cntx, d, flowStatus.Cookie, flowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +05301727
1728 if flowStatus.Flow != nil {
1729 flowAdd := (flowStatus.FlowModType == of.CommandAdd)
1730 if !cookieExists && !isFlowStatusSuccess(flowStatus.Status, flowAdd) {
1731 pushFlowFailureNotif(flowStatus)
1732 }
1733 }
1734}
1735
1736func pushFlowFailureNotif(flowStatus intf.FlowStatus) {
1737 subFlow := flowStatus.Flow
1738 cookie := subFlow.Cookie
1739 uniPort := cookie >> 16 & 0xFFFFFFFF
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301740 logger.Warnw(ctx, "Flow Failure Notification", log.Fields{"uniPort": uniPort, "Cookie": cookie})
Naveen Sampath04696f72022-06-13 15:19:14 +05301741}
1742
vinokuma926cb3e2023-03-29 11:41:06 +05301743// UpdateMvlanProfilesForDevice to update mvlan profile for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301744func (va *VoltApplication) UpdateMvlanProfilesForDevice(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301745 logger.Debugw(ctx, "Received Update Mvlan Profiles For Device", log.Fields{"device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301746 checkAndAddMvlanUpdateTask := func(key, value interface{}) bool {
1747 mvp := value.(*MvlanProfile)
1748 if mvp.IsUpdateInProgressForDevice(device) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301749 mvp.UpdateProfile(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301750 }
1751 return true
1752 }
1753 va.MvlanProfilesByName.Range(checkAndAddMvlanUpdateTask)
1754}
1755
1756// TaskInfo structure that is used to store the task Info.
1757type TaskInfo struct {
1758 ID string
1759 Name string
1760 Timestamp string
1761}
1762
1763// GetTaskList to get task list information.
1764func (va *VoltApplication) GetTaskList(device string) map[int]*TaskInfo {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301765 logger.Debugw(ctx, "Received Get Task List", log.Fields{"device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301766 taskList := cntlr.GetController().GetTaskList(device)
1767 taskMap := make(map[int]*TaskInfo)
1768 for i, task := range taskList {
1769 taskID := strconv.Itoa(int(task.TaskID()))
1770 name := task.Name()
1771 timestamp := task.Timestamp()
1772 taskInfo := &TaskInfo{ID: taskID, Name: name, Timestamp: timestamp}
1773 taskMap[i] = taskInfo
1774 }
1775 return taskMap
1776}
1777
1778// UpdateDeviceSerialNumberList to update the device serial number list after device serial number is updated for vnet and mvlan
1779func (va *VoltApplication) UpdateDeviceSerialNumberList(oldOltSlNo string, newOltSlNo string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301780 logger.Debugw(ctx, "Update Device Serial Number List", log.Fields{"oldOltSlNo": oldOltSlNo, "newOltSlNo": newOltSlNo})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05301781 voltDevice, _ := va.GetDeviceBySerialNo(oldOltSlNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301782
1783 if voltDevice != nil {
1784 // Device is present with old serial number ID
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301785 logger.Warnw(ctx, "OLT Migration cannot be completed as there are dangling devices", log.Fields{"Serial Number": oldOltSlNo})
Naveen Sampath04696f72022-06-13 15:19:14 +05301786 } else {
1787 logger.Infow(ctx, "No device present with old serial number", log.Fields{"Serial Number": oldOltSlNo})
Naveen Sampath04696f72022-06-13 15:19:14 +05301788 // Add Serial Number to Blocked Devices List.
1789 cntlr.GetController().AddBlockedDevices(oldOltSlNo)
1790 cntlr.GetController().AddBlockedDevices(newOltSlNo)
1791
1792 updateSlNoForVnet := func(key, value interface{}) bool {
1793 vnet := value.(*VoltVnet)
1794 for i, deviceSlNo := range vnet.VnetConfig.DevicesList {
1795 if deviceSlNo == oldOltSlNo {
1796 vnet.VnetConfig.DevicesList[i] = newOltSlNo
1797 logger.Infow(ctx, "device serial number updated for vnet profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1798 break
1799 }
1800 }
1801 return true
1802 }
1803
1804 updateSlNoforMvlan := func(key interface{}, value interface{}) bool {
1805 mvProfile := value.(*MvlanProfile)
1806 for deviceSlNo := range mvProfile.DevicesList {
1807 if deviceSlNo == oldOltSlNo {
1808 mvProfile.DevicesList[newOltSlNo] = mvProfile.DevicesList[oldOltSlNo]
1809 delete(mvProfile.DevicesList, oldOltSlNo)
1810 logger.Infow(ctx, "device serial number updated for mvlan profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1811 break
1812 }
1813 }
1814 return true
1815 }
1816
1817 va.VnetsByName.Range(updateSlNoForVnet)
1818 va.MvlanProfilesByName.Range(updateSlNoforMvlan)
1819
1820 // Clear the serial number from Blocked Devices List
1821 cntlr.GetController().DelBlockedDevices(oldOltSlNo)
1822 cntlr.GetController().DelBlockedDevices(newOltSlNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301823 }
1824}
1825
1826// GetVpvsForDsPkt to get vpv for downstream packets
1827func (va *VoltApplication) GetVpvsForDsPkt(cvlan of.VlanType, svlan of.VlanType, clientMAC net.HardwareAddr,
1828 pbit uint8) ([]*VoltPortVnet, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301829 logger.Debugw(ctx, "Received Get Vpvs For Ds Pkt", log.Fields{"Cvlan": cvlan, "Svlan": svlan, "Mac": clientMAC})
Naveen Sampath04696f72022-06-13 15:19:14 +05301830 var matchVPVs []*VoltPortVnet
1831 findVpv := func(key, value interface{}) bool {
1832 vpvs := value.([]*VoltPortVnet)
1833 for _, vpv := range vpvs {
1834 if vpv.isVlanMatching(cvlan, svlan) && vpv.MatchesPriority(pbit) != nil {
1835 var subMac net.HardwareAddr
1836 if NonZeroMacAddress(vpv.MacAddr) {
1837 subMac = vpv.MacAddr
1838 } else if vpv.LearntMacAddr != nil && NonZeroMacAddress(vpv.LearntMacAddr) {
1839 subMac = vpv.LearntMacAddr
1840 } else {
1841 matchVPVs = append(matchVPVs, vpv)
1842 continue
1843 }
1844 if util.MacAddrsMatch(subMac, clientMAC) {
1845 matchVPVs = append([]*VoltPortVnet{}, vpv)
1846 logger.Infow(ctx, "Matching VPV found", log.Fields{"Port": vpv.Port, "SVLAN": vpv.SVlan, "CVLAN": vpv.CVlan, "UNIVlan": vpv.UniVlan, "MAC": clientMAC})
1847 return false
1848 }
1849 }
1850 }
1851 return true
1852 }
1853 va.VnetsByPort.Range(findVpv)
1854
1855 if len(matchVPVs) != 1 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301856 logger.Errorw(ctx, "No matching VPV found or multiple vpvs found", log.Fields{"Match VPVs": matchVPVs, "MAC": clientMAC})
1857 return nil, errors.New("no matching VPV found or multiple vpvs found")
Naveen Sampath04696f72022-06-13 15:19:14 +05301858 }
1859 return matchVPVs, nil
1860}
1861
1862// GetMacInPortMap to get PORT value based on MAC key
1863func (va *VoltApplication) GetMacInPortMap(macAddr net.HardwareAddr) string {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301864 logger.Debugw(ctx, "Received Get PORT value based on MAC key", log.Fields{"MacAddr": macAddr.String()})
Naveen Sampath04696f72022-06-13 15:19:14 +05301865 if NonZeroMacAddress(macAddr) {
1866 va.macPortLock.Lock()
1867 defer va.macPortLock.Unlock()
1868 if port, ok := va.macPortMap[macAddr.String()]; ok {
1869 logger.Debugw(ctx, "found-entry-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1870 return port
1871 }
1872 }
1873 logger.Infow(ctx, "entry-not-found-macportmap", log.Fields{"MacAddr": macAddr.String()})
1874 return ""
1875}
1876
1877// UpdateMacInPortMap to update MAC PORT (key value) information in MacPortMap
1878func (va *VoltApplication) UpdateMacInPortMap(macAddr net.HardwareAddr, port string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301879 logger.Debugw(ctx, "Update Macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301880 if NonZeroMacAddress(macAddr) {
1881 va.macPortLock.Lock()
1882 va.macPortMap[macAddr.String()] = port
1883 va.macPortLock.Unlock()
1884 logger.Debugw(ctx, "updated-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1885 }
1886}
1887
1888// DeleteMacInPortMap to remove MAC key from MacPortMap
1889func (va *VoltApplication) DeleteMacInPortMap(macAddr net.HardwareAddr) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301890 logger.Debugw(ctx, "Delete Mac from Macportmap", log.Fields{"MacAddr": macAddr.String()})
Naveen Sampath04696f72022-06-13 15:19:14 +05301891 if NonZeroMacAddress(macAddr) {
1892 port := va.GetMacInPortMap(macAddr)
1893 va.macPortLock.Lock()
1894 delete(va.macPortMap, macAddr.String())
1895 va.macPortLock.Unlock()
1896 logger.Debugw(ctx, "deleted-from-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1897 }
1898}
1899
vinokuma926cb3e2023-03-29 11:41:06 +05301900// AddGroupToPendingPool - adds the IgmpGroup with active group table entry to global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301901func (va *VoltApplication) AddGroupToPendingPool(ig *IgmpGroup) {
1902 var grpMap map[*IgmpGroup]bool
1903 var ok bool
1904
1905 va.PendingPoolLock.Lock()
1906 defer va.PendingPoolLock.Unlock()
1907
1908 logger.Infow(ctx, "Adding IgmpGroup to Global Pending Pool", log.Fields{"GroupID": ig.GroupID, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr, "PendingDevices": len(ig.Devices)})
1909 // Do Not Reset any current profile info since group table entry tied to mvlan profile
1910 // The PonVlan is part of set field in group installed
1911 // Hence, Group created is always tied to the same mvlan profile until deleted
1912
1913 for device := range ig.Devices {
1914 key := getPendingPoolKey(ig.Mvlan, device)
1915
1916 if grpMap, ok = va.IgmpPendingPool[key]; !ok {
1917 grpMap = make(map[*IgmpGroup]bool)
1918 }
1919 grpMap[ig] = true
1920
1921 //Add grpObj reference to all associated devices
1922 va.IgmpPendingPool[key] = grpMap
1923 }
1924}
1925
vinokuma926cb3e2023-03-29 11:41:06 +05301926// RemoveGroupFromPendingPool - removes the group from global pending group pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301927func (va *VoltApplication) RemoveGroupFromPendingPool(device string, ig *IgmpGroup) bool {
1928 GetApplication().PendingPoolLock.Lock()
1929 defer GetApplication().PendingPoolLock.Unlock()
1930
1931 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)})
1932
1933 key := getPendingPoolKey(ig.Mvlan, device)
1934 if _, ok := va.IgmpPendingPool[key]; ok {
1935 delete(va.IgmpPendingPool[key], ig)
1936 return true
1937 }
1938 return false
1939}
1940
vinokuma926cb3e2023-03-29 11:41:06 +05301941// RemoveGroupsFromPendingPool - removes the group from global pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301942func (va *VoltApplication) RemoveGroupsFromPendingPool(cntx context.Context, device string, mvlan of.VlanType) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301943 GetApplication().PendingPoolLock.Lock()
1944 defer GetApplication().PendingPoolLock.Unlock()
1945
1946 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool for given Deivce & Mvlan", log.Fields{"Device": device, "Mvlan": mvlan.String()})
1947
1948 key := getPendingPoolKey(mvlan, device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301949 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301950}
1951
vinokuma926cb3e2023-03-29 11:41:06 +05301952// RemoveGroupListFromPendingPool - removes the groups for provided key
Naveen Sampath04696f72022-06-13 15:19:14 +05301953// 1. Deletes the group from device
1954// 2. Delete the IgmpGroup obj and release the group ID to pool
1955// Note: Make sure to obtain PendingPoolLock lock before calling this func
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301956func (va *VoltApplication) RemoveGroupListFromPendingPool(cntx context.Context, key string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301957 logger.Infow(ctx, "Remove GroupList from Pending Pool for ", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301958 if grpMap, ok := va.IgmpPendingPool[key]; ok {
1959 delete(va.IgmpPendingPool, key)
1960 for ig := range grpMap {
1961 for device := range ig.Devices {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301962 ig.DeleteIgmpGroupDevice(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301963 }
1964 }
1965 }
1966}
1967
vinokuma926cb3e2023-03-29 11:41:06 +05301968// RemoveGroupDevicesFromPendingPool - removes the group from global pending group pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301969func (va *VoltApplication) RemoveGroupDevicesFromPendingPool(ig *IgmpGroup) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301970 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)})
1971 for device := range ig.PendingGroupForDevice {
1972 va.RemoveGroupFromPendingPool(device, ig)
1973 }
1974}
1975
vinokuma926cb3e2023-03-29 11:41:06 +05301976// GetGroupFromPendingPool - Returns IgmpGroup obj from global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301977func (va *VoltApplication) GetGroupFromPendingPool(mvlan of.VlanType, device string) *IgmpGroup {
Naveen Sampath04696f72022-06-13 15:19:14 +05301978 var ig *IgmpGroup
1979
1980 va.PendingPoolLock.Lock()
1981 defer va.PendingPoolLock.Unlock()
1982
1983 key := getPendingPoolKey(mvlan, device)
1984 logger.Infow(ctx, "Getting IgmpGroup from Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String(), "Key": key})
1985
vinokuma926cb3e2023-03-29 11:41:06 +05301986 // Gets all IgmpGrp Obj for the device
Naveen Sampath04696f72022-06-13 15:19:14 +05301987 grpMap, ok := va.IgmpPendingPool[key]
1988 if !ok || len(grpMap) == 0 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301989 logger.Warnw(ctx, "Matching IgmpGroup not found in Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String()})
Naveen Sampath04696f72022-06-13 15:19:14 +05301990 return nil
1991 }
1992
vinokuma926cb3e2023-03-29 11:41:06 +05301993 // Gets a random obj from available grps
Naveen Sampath04696f72022-06-13 15:19:14 +05301994 for ig = range grpMap {
vinokuma926cb3e2023-03-29 11:41:06 +05301995 // Remove grp obj reference from all devices associated in pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301996 for dev := range ig.Devices {
1997 key := getPendingPoolKey(mvlan, dev)
1998 delete(va.IgmpPendingPool[key], ig)
1999 }
2000
vinokuma926cb3e2023-03-29 11:41:06 +05302001 // Safety check to avoid re-allocating group already in use
Naveen Sampath04696f72022-06-13 15:19:14 +05302002 if ig.NumDevicesActive() == 0 {
2003 return ig
2004 }
2005
vinokuma926cb3e2023-03-29 11:41:06 +05302006 // Iteration will continue only if IG is not allocated
Naveen Sampath04696f72022-06-13 15:19:14 +05302007 }
2008 return nil
2009}
2010
vinokuma926cb3e2023-03-29 11:41:06 +05302011// RemovePendingGroups - removes all pending groups for provided reference from global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05302012// reference - mvlan/device ID
2013// isRefDevice - true - Device as reference
vinokuma926cb3e2023-03-29 11:41:06 +05302014// false - Mvlan as reference
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302015func (va *VoltApplication) RemovePendingGroups(cntx context.Context, reference string, isRefDevice bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302016 va.PendingPoolLock.Lock()
2017 defer va.PendingPoolLock.Unlock()
2018
2019 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool", log.Fields{"Reference": reference, "isRefDevice": isRefDevice})
2020
vinokuma926cb3e2023-03-29 11:41:06 +05302021 // Pending Pool key: "<mvlan>_<DeviceID>""
Naveen Sampath04696f72022-06-13 15:19:14 +05302022 paramPosition := 0
2023 if isRefDevice {
2024 paramPosition = 1
2025 }
2026
2027 // 1.Remove the Entry from pending pool
2028 // 2.Deletes the group from device
2029 // 3.Delete the IgmpGroup obj and release the group ID to pool
2030 for key := range va.IgmpPendingPool {
2031 keyParams := strings.Split(key, "_")
2032 if keyParams[paramPosition] == reference {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302033 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05302034 }
2035 }
2036}
2037
2038func getPendingPoolKey(mvlan of.VlanType, device string) string {
2039 return mvlan.String() + "_" + device
2040}
2041
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302042func (va *VoltApplication) removeExpiredGroups(cntx context.Context) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302043 logger.Info(ctx, "Remove expired Igmp Groups")
Naveen Sampath04696f72022-06-13 15:19:14 +05302044 removeExpiredGroups := func(key interface{}, value interface{}) bool {
2045 ig := value.(*IgmpGroup)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302046 ig.removeExpiredGroupFromDevice(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302047 return true
2048 }
2049 va.IgmpGroups.Range(removeExpiredGroups)
2050}
2051
vinokuma926cb3e2023-03-29 11:41:06 +05302052// TriggerPendingProfileDeleteReq - trigger pending profile delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302053func (va *VoltApplication) TriggerPendingProfileDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302054 logger.Infow(ctx, "Trigger Pending Profile Delete for device", log.Fields{"Device": device})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302055 va.TriggerPendingServiceDeactivateReq(cntx, device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302056 va.TriggerPendingServiceDeleteReq(cntx, device)
2057 va.TriggerPendingVpvDeleteReq(cntx, device)
2058 va.TriggerPendingVnetDeleteReq(cntx, device)
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302059 logger.Infow(ctx, "All Pending Profile Delete triggered for device", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05302060}
2061
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302062// TriggerPendingServiceDeactivateReq - trigger pending service deactivate request
2063func (va *VoltApplication) TriggerPendingServiceDeactivateReq(cntx context.Context, device string) {
2064 logger.Infow(ctx, "Pending Services to be deactivated", log.Fields{"Count": len(va.ServicesToDeactivate)})
2065 for serviceName := range va.ServicesToDeactivate {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302066 logger.Debugw(ctx, "Trigger Service Deactivate", log.Fields{"Service": serviceName})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302067 if vs := va.GetService(serviceName); vs != nil {
2068 if vs.Device == device {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302069 logger.Infow(ctx, "Triggering Pending Service Deactivate", log.Fields{"Service": vs.Name})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302070 vpv := va.GetVnetByPort(vs.Port, vs.SVlan, vs.CVlan, vs.UniVlan)
2071 if vpv == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302072 logger.Warnw(ctx, "Vpv Not found for Service", log.Fields{"vs": vs.Name, "port": vs.Port, "Vnet": vs.VnetID})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302073 continue
2074 }
2075
2076 vpv.DelTrapFlows(cntx)
2077 vs.DelHsiaFlows(cntx)
2078 vs.WriteToDb(cntx)
2079 vpv.ClearServiceCounters(cntx)
2080 }
2081 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302082 logger.Warnw(ctx, "Pending Service Not found", log.Fields{"Service": serviceName})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302083 }
2084 }
2085}
2086
vinokuma926cb3e2023-03-29 11:41:06 +05302087// TriggerPendingServiceDeleteReq - trigger pending service delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302088func (va *VoltApplication) TriggerPendingServiceDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302089 logger.Infow(ctx, "Pending Services to be deleted", log.Fields{"Count": len(va.ServicesToDelete)})
Naveen Sampath04696f72022-06-13 15:19:14 +05302090 for serviceName := range va.ServicesToDelete {
2091 logger.Debugw(ctx, "Trigger Service Delete", log.Fields{"Service": serviceName})
2092 if vs := va.GetService(serviceName); vs != nil {
2093 if vs.Device == device {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302094 logger.Infow(ctx, "Triggering Pending Service delete", log.Fields{"Service": vs.Name})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302095 vs.DelHsiaFlows(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302096 if vs.ForceDelete {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302097 vs.DelFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302098 }
2099 }
2100 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302101 logger.Warnw(ctx, "Pending Service Not found", log.Fields{"Service": serviceName})
Naveen Sampath04696f72022-06-13 15:19:14 +05302102 }
2103 }
2104}
2105
vinokuma926cb3e2023-03-29 11:41:06 +05302106// TriggerPendingVpvDeleteReq - trigger pending VPV delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302107func (va *VoltApplication) TriggerPendingVpvDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302108 logger.Infow(ctx, "Pending VPVs to be deleted", log.Fields{"Count": len(va.VoltPortVnetsToDelete)})
Naveen Sampath04696f72022-06-13 15:19:14 +05302109 for vpv := range va.VoltPortVnetsToDelete {
2110 if vpv.Device == device {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302111 logger.Debugw(ctx, "Triggering Pending VPv flow delete", log.Fields{"Port": vpv.Port, "Device": vpv.Device, "Vnet": vpv.VnetName})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302112 va.DelVnetFromPort(cntx, vpv.Port, vpv)
Naveen Sampath04696f72022-06-13 15:19:14 +05302113 }
2114 }
2115}
2116
vinokuma926cb3e2023-03-29 11:41:06 +05302117// TriggerPendingVnetDeleteReq - trigger pending vnet delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302118func (va *VoltApplication) TriggerPendingVnetDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302119 logger.Infow(ctx, "Pending Vnets to be deleted", log.Fields{"Count": len(va.VnetsToDelete)})
Naveen Sampath04696f72022-06-13 15:19:14 +05302120 for vnetName := range va.VnetsToDelete {
2121 if vnetIntf, _ := va.VnetsByName.Load(vnetName); vnetIntf != nil {
2122 vnet := vnetIntf.(*VoltVnet)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302123 if d, _ := va.GetDeviceBySerialNo(vnet.PendingDeviceToDelete); d != nil && d.SerialNum == vnet.PendingDeviceToDelete {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302124 logger.Infow(ctx, "Triggering Pending Vnet flows delete", log.Fields{"Vnet": vnet.Name, "Device": vnet.PendingDeviceToDelete})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302125 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, vnet.PendingDeviceToDelete)
Naveen Sampath04696f72022-06-13 15:19:14 +05302126 va.deleteVnetConfig(vnet)
2127 } else {
Tinoj Joseph1d108322022-07-13 10:07:39 +05302128 logger.Warnw(ctx, "Vnet Delete Failed : Device Not Found", log.Fields{"Vnet": vnet.Name, "Device": vnet.PendingDeviceToDelete})
Naveen Sampath04696f72022-06-13 15:19:14 +05302129 }
2130 }
2131 }
2132}
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302133
2134type OltFlowService struct {
vinokuma926cb3e2023-03-29 11:41:06 +05302135 DefaultTechProfileID int `json:"defaultTechProfileId"`
Akash Sonia8246972023-01-03 10:37:08 +05302136 EnableDhcpOnNni bool `json:"enableDhcpOnNni"`
Akash Sonia8246972023-01-03 10:37:08 +05302137 EnableIgmpOnNni bool `json:"enableIgmpOnNni"`
2138 EnableEapol bool `json:"enableEapol"`
2139 EnableDhcpV6 bool `json:"enableDhcpV6"`
2140 EnableDhcpV4 bool `json:"enableDhcpV4"`
2141 RemoveFlowsOnDisable bool `json:"removeFlowsOnDisable"`
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302142}
2143
2144func (va *VoltApplication) UpdateOltFlowService(cntx context.Context, oltFlowService OltFlowService) {
2145 logger.Infow(ctx, "UpdateOltFlowService", log.Fields{"oldValue": va.OltFlowServiceConfig, "newValue": oltFlowService})
2146 va.OltFlowServiceConfig = oltFlowService
2147 b, err := json.Marshal(va.OltFlowServiceConfig)
2148 if err != nil {
2149 logger.Warnw(ctx, "Failed to Marshal OltFlowServiceConfig", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2150 return
2151 }
2152 _ = db.PutOltFlowService(cntx, string(b))
2153}
Akash Sonia8246972023-01-03 10:37:08 +05302154
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302155// RestoreOltFlowService to read from the DB and restore olt flow service config
2156func (va *VoltApplication) RestoreOltFlowService(cntx context.Context) {
2157 oltflowService, err := db.GetOltFlowService(cntx)
2158 if err != nil {
2159 logger.Warnw(ctx, "Failed to Get OltFlowServiceConfig from DB", log.Fields{"Error": err})
2160 return
2161 }
2162 err = json.Unmarshal([]byte(oltflowService), &va.OltFlowServiceConfig)
2163 if err != nil {
2164 logger.Warn(ctx, "Unmarshal of oltflowService failed")
2165 return
2166 }
2167 logger.Infow(ctx, "updated OltFlowServiceConfig from DB", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2168}
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302169
Akash Soni87a19072023-02-28 00:46:59 +05302170func (va *VoltApplication) UpdateDeviceConfig(cntx context.Context, deviceConfig *DeviceConfig) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302171 logger.Infow(ctx, "Received UpdateDeviceConfig", log.Fields{"DeviceInfo": deviceConfig})
Akash Soni87a19072023-02-28 00:46:59 +05302172 var dc *DeviceConfig
2173 va.DevicesConfig.Store(deviceConfig.SerialNumber, deviceConfig)
2174 err := dc.WriteDeviceConfigToDb(cntx, deviceConfig.SerialNumber, deviceConfig)
2175 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302176 logger.Warnw(ctx, "DB update for device config failed", log.Fields{"err": err})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302177 }
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302178 logger.Debugw(ctx, "Added OLT configurations", log.Fields{"DeviceInfo": deviceConfig})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302179 // If device is already discovered update the VoltDevice structure
Akash Soni87a19072023-02-28 00:46:59 +05302180 device, id := va.GetDeviceBySerialNo(deviceConfig.SerialNumber)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302181 if device != nil {
Akash Soni87a19072023-02-28 00:46:59 +05302182 device.NniDhcpTrapVid = of.VlanType(deviceConfig.NniDhcpTrapVid)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302183 va.DevicesDisc.Store(id, device)
2184 }
2185}