blob: 0117cd74444da0fa3fa077ec9464c44369214374 [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()
943 vpv.PortDownInd(cntx, device, port, true)
944 vpv.VpvLock.Unlock()
945 }
946 }
947 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530948 va.portLock.Lock()
949 defer va.portLock.Unlock()
950 d.DelPort(port)
951 if _, ok := va.PortsDisc.Load(port); ok {
952 va.PortsDisc.Delete(port)
953 }
954 } else {
955 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: Delete", log.Fields{"Device": device, "Port": port})
956 }
957}
958
vinokuma926cb3e2023-03-29 11:41:06 +0530959// PortUpdateInd Updates port Id incase of ONU movement
Naveen Sampath04696f72022-06-13 15:19:14 +0530960func (va *VoltApplication) PortUpdateInd(device string, portName string, id uint32) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530961 logger.Debugw(ctx, "Received Port Ind: Update", log.Fields{"Device": device, "Port": portName, "ID": id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530962 va.portLock.Lock()
963 defer va.portLock.Unlock()
964 if d := va.GetDevice(device); d != nil {
965 vp := d.GetPort(portName)
966 vp.ID = id
967 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530968 logger.Warnw(ctx, "Device Not Found", log.Fields{"Device": device, "Port": portName, "ID": id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530969 }
970}
971
972// AddNbPonPort Add pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530973func (va *VoltApplication) AddNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32,
Naveen Sampath04696f72022-06-13 15:19:14 +0530974 enableMulticastKPI bool, portAlarmProfileID string) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530975 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 +0530976 var nbd *NbDevice
977 nbDevice, ok := va.NbDevice.Load(oltSbID)
978
979 if !ok {
980 nbd = NewNbDevice()
981 nbd.SouthBoundID = oltSbID
982 } else {
983 nbd = nbDevice.(*NbDevice)
984 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530985 port := nbd.AddPortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530986 logger.Debugw(ctx, "Added Port To NbDevice", log.Fields{"port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530987 // Add this port to voltDevice
988 addPort := func(key, value interface{}) bool {
989 voltDevice := value.(*VoltDevice)
990 if oltSbID == voltDevice.SouthBoundID {
991 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); !exists {
992 voltDevice.ActiveChannelsPerPon.Store(portID, port)
993 }
994 return false
995 }
996 return true
997 }
998 va.DevicesDisc.Range(addPort)
999 va.NbDevice.Store(oltSbID, nbd)
1000
1001 return nil
1002}
1003
1004// UpdateNbPonPort update pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301005func (va *VoltApplication) UpdateNbPonPort(cntx context.Context, oltSbID string, portID, maxAllowedChannels uint32, enableMulticastKPI bool, portAlarmProfileID string) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301006 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 +05301007 var nbd *NbDevice
1008 nbDevice, ok := va.NbDevice.Load(oltSbID)
1009
1010 if !ok {
1011 logger.Errorw(ctx, "Device-doesn't-exists", log.Fields{"deviceID": oltSbID})
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301012 return fmt.Errorf("device-doesn't-exists - %s", oltSbID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301013 }
1014 nbd = nbDevice.(*NbDevice)
1015
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301016 port := nbd.UpdatePortToNbDevice(cntx, portID, maxAllowedChannels, enableMulticastKPI, portAlarmProfileID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301017 if port == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301018 return fmt.Errorf("port-doesn't-exists-%d", portID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301019 }
1020 va.NbDevice.Store(oltSbID, nbd)
1021
1022 // Add this port to voltDevice
1023 updPort := func(key, value interface{}) bool {
1024 voltDevice := value.(*VoltDevice)
1025 if oltSbID == voltDevice.SouthBoundID {
1026 voltDevice.ActiveChannelCountLock.Lock()
1027 if p, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
1028 oldPort := p.(*PonPortCfg)
1029 if port.MaxActiveChannels != 0 {
1030 oldPort.MaxActiveChannels = port.MaxActiveChannels
1031 oldPort.EnableMulticastKPI = port.EnableMulticastKPI
1032 voltDevice.ActiveChannelsPerPon.Store(portID, oldPort)
1033 }
1034 }
1035 voltDevice.ActiveChannelCountLock.Unlock()
1036 return false
1037 }
1038 return true
1039 }
1040 va.DevicesDisc.Range(updPort)
1041
1042 return nil
1043}
1044
1045// DeleteNbPonPort Delete pon port to nbDevice
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301046func (va *VoltApplication) DeleteNbPonPort(cntx context.Context, oltSbID string, portID uint32) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301047 logger.Debugw(ctx, "Received NbPonPort Ind: Delete", log.Fields{"oltSbID": oltSbID, "portID": portID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301048 nbDevice, ok := va.NbDevice.Load(oltSbID)
1049 if ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301050 nbDevice.(*NbDevice).DeletePortFromNbDevice(cntx, portID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301051 va.NbDevice.Store(oltSbID, nbDevice.(*NbDevice))
1052 } else {
1053 logger.Warnw(ctx, "Delete pon received for unknown device", log.Fields{"oltSbID": oltSbID})
1054 return nil
1055 }
1056 // Delete this port from voltDevice
1057 delPort := func(key, value interface{}) bool {
1058 voltDevice := value.(*VoltDevice)
1059 if oltSbID == voltDevice.SouthBoundID {
1060 if _, exists := voltDevice.ActiveChannelsPerPon.Load(portID); exists {
1061 voltDevice.ActiveChannelsPerPon.Delete(portID)
1062 }
1063 return false
1064 }
1065 return true
1066 }
1067 va.DevicesDisc.Range(delPort)
1068 return nil
1069}
1070
1071// GetNniPort : Get the NNI port for a device. Called from different other applications
1072// as a port to match or destination for a packet out. The VOLT application
1073// is written with the assumption that there is a single NNI port. The OLT
1074// device is responsible for translating the combination of VLAN and the
1075// NNI port ID to identify possibly a single physical port or a logical
1076// port which is a result of protection methods applied.
1077func (va *VoltApplication) GetNniPort(device string) (string, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301078 logger.Debugw(ctx, "NNI Get Ind", log.Fields{"device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301079 va.portLock.Lock()
1080 defer va.portLock.Unlock()
1081 d, ok := va.DevicesDisc.Load(device)
1082 if !ok {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301083 return "", errors.New("device doesn't exist")
Naveen Sampath04696f72022-06-13 15:19:14 +05301084 }
1085 return d.(*VoltDevice).NniPort, nil
1086}
1087
1088// NniDownInd process for Nni down indication.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301089func (va *VoltApplication) NniDownInd(cntx context.Context, deviceID string, devSrNo string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301090 logger.Debugw(ctx, "NNI Down Ind", log.Fields{"DeviceID": deviceID, "Device SrNo": devSrNo})
Naveen Sampath04696f72022-06-13 15:19:14 +05301091
1092 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1093 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301094 mvProfile.removeIgmpMcastFlows(cntx, devSrNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301095 return true
1096 }
1097 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1098
1099 //Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301100 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301101}
1102
1103// DeviceUpInd changes device state to up.
1104func (va *VoltApplication) DeviceUpInd(device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301105 logger.Infow(ctx, "Received Device Ind: UP", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301106 if d := va.GetDevice(device); d != nil {
1107 d.State = controller.DeviceStateUP
1108 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301109 logger.Warnw(ctx, "Ignoring Device indication: UP. Device Missing", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301110 }
1111}
1112
1113// DeviceDownInd changes device state to down.
1114func (va *VoltApplication) DeviceDownInd(device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301115 logger.Infow(ctx, "Received Device Ind: DOWN", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301116 if d := va.GetDevice(device); d != nil {
1117 d.State = controller.DeviceStateDOWN
1118 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301119 logger.Warnw(ctx, "Ignoring Device indication: DOWN. Device Missing", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301120 }
1121}
1122
1123// DeviceRebootInd process for handling flow clear flag for device reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301124func (va *VoltApplication) DeviceRebootInd(cntx context.Context, device string, serialNum string, southBoundID string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301125 logger.Infow(ctx, "Received Device Ind: Reboot", log.Fields{"Device": device, "SerialNumber": serialNum, "SouthBoundID": southBoundID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301126
1127 if d := va.GetDevice(device); d != nil {
1128 if d.State == controller.DeviceStateREBOOTED {
1129 logger.Warnw(ctx, "Ignoring Device Ind: Reboot, Device already in Reboot state", log.Fields{"Device": device, "SerialNumber": serialNum, "State": d.State})
1130 return
1131 }
1132 d.State = controller.DeviceStateREBOOTED
1133 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301134 va.HandleFlowClearFlag(cntx, device, serialNum, southBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301135}
1136
1137// DeviceDisableInd handles device deactivation process
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301138func (va *VoltApplication) DeviceDisableInd(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301139 logger.Infow(ctx, "Received Device Ind: Disable", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301140
1141 d := va.GetDevice(device)
1142 if d == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301143 logger.Warnw(ctx, "Ignoring Device indication: DISABLED. Device Missing", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301144 return
1145 }
1146
1147 d.State = controller.DeviceStateDISABLED
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301148 va.HandleFlowClearFlag(cntx, device, d.SerialNum, d.SouthBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301149}
1150
1151// ProcessIgmpDSFlowForMvlan for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301152func (va *VoltApplication) ProcessIgmpDSFlowForMvlan(cntx context.Context, d *VoltDevice, mvp *MvlanProfile, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301153 logger.Debugw(ctx, "Process IGMP DS Flows for MVlan", log.Fields{"device": d.Name, "Mvlan": mvp.Mvlan, "addFlow": addFlow})
1154 portState := false
1155 p := d.GetPort(d.NniPort)
1156 if p != nil && p.State == PortStateUp {
1157 portState = true
1158 }
1159
1160 if addFlow {
1161 if portState {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301162 mvp.pushIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301163 }
1164 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301165 mvp.removeIgmpMcastFlows(cntx, d.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301166 }
1167}
1168
1169// ProcessIgmpDSFlowForDevice for processing Igmp DS flow for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301170func (va *VoltApplication) ProcessIgmpDSFlowForDevice(cntx context.Context, d *VoltDevice, addFlow bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301171 logger.Debugw(ctx, "Process IGMP DS Flows for device", log.Fields{"device": d.Name, "addFlow": addFlow})
1172
1173 handleIgmpDsFlows := func(key interface{}, value interface{}) bool {
1174 mvProfile := value.(*MvlanProfile)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301175 va.ProcessIgmpDSFlowForMvlan(cntx, d, mvProfile, addFlow)
Naveen Sampath04696f72022-06-13 15:19:14 +05301176 return true
1177 }
1178 va.MvlanProfilesByName.Range(handleIgmpDsFlows)
1179}
1180
1181// GetDeviceFromPort : This is suitable only for access ports as their naming convention
1182// makes them unique across all the OLTs. This must be called with
1183// port name that is an access port. Currently called from VNETs, attached
1184// only to access ports, and the services which are also attached only
1185// to access ports
1186func (va *VoltApplication) GetDeviceFromPort(port string) (*VoltDevice, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301187 logger.Debugw(ctx, "Received Get Device From Port", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301188 va.portLock.Lock()
1189 defer va.portLock.Unlock()
1190 var err error
1191 err = nil
1192 p, ok := va.PortsDisc.Load(port)
1193 if !ok {
1194 return nil, errorCodes.ErrPortNotFound
1195 }
1196 d := va.GetDevice(p.(*VoltPort).Device)
1197 if d == nil {
1198 err = errorCodes.ErrDeviceNotFound
1199 }
1200 return d, err
1201}
1202
1203// GetPortID : This too applies only to access ports. The ports can be indexed
1204// purely by their names without the device forming part of the key
1205func (va *VoltApplication) GetPortID(port string) (uint32, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301206 logger.Debugw(ctx, "Received Get Port ID", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301207 va.portLock.Lock()
1208 defer va.portLock.Unlock()
1209 p, ok := va.PortsDisc.Load(port)
1210 if !ok {
1211 return 0, errorCodes.ErrPortNotFound
1212 }
1213 return p.(*VoltPort).ID, nil
1214}
1215
1216// GetPortName : This too applies only to access ports. The ports can be indexed
1217// purely by their names without the device forming part of the key
1218func (va *VoltApplication) GetPortName(port uint32) (string, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301219 logger.Debugw(ctx, "Received Get Port Name", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301220 va.portLock.Lock()
1221 defer va.portLock.Unlock()
1222 var portName string
1223 va.PortsDisc.Range(func(key interface{}, value interface{}) bool {
1224 portInfo := value.(*VoltPort)
1225 if portInfo.ID == port {
1226 portName = portInfo.Name
1227 return false
1228 }
1229 return true
1230 })
1231 return portName, nil
1232}
1233
1234// GetPonFromUniPort to get Pon info from UniPort
1235func (va *VoltApplication) GetPonFromUniPort(port string) (string, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301236 logger.Debugw(ctx, "Received Get Pon From UniPort", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301237 uniPortID, err := va.GetPortID(port)
1238 if err == nil {
1239 ponPortID := (uniPortID & 0x0FF00000) >> 20 //pon(8) + onu(8) + uni(12)
1240 return strconv.FormatUint(uint64(ponPortID), 10), nil
1241 }
1242 return "", err
1243}
1244
1245// GetPortState : This too applies only to access ports. The ports can be indexed
1246// purely by their names without the device forming part of the key
1247func (va *VoltApplication) GetPortState(port string) (PortState, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301248 logger.Debugw(ctx, "Received Get Port State", log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301249 va.portLock.Lock()
1250 defer va.portLock.Unlock()
1251 p, ok := va.PortsDisc.Load(port)
1252 if !ok {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301253 return 0, errors.New("port not configured")
Naveen Sampath04696f72022-06-13 15:19:14 +05301254 }
1255 return p.(*VoltPort).State, nil
1256}
1257
1258// GetIcmpv6Receivers to get Icmp v6 receivers
1259func (va *VoltApplication) GetIcmpv6Receivers(device string) []uint32 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301260 logger.Debugw(ctx, "Get Icmpv6 Receivers", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301261 var receiverList []uint32
1262 receivers, _ := va.Icmpv6Receivers.Load(device)
1263 if receivers != nil {
1264 receiverList = receivers.([]uint32)
1265 }
1266 return receiverList
1267}
1268
1269// AddIcmpv6Receivers to add Icmp v6 receivers
1270func (va *VoltApplication) AddIcmpv6Receivers(device string, portID uint32) []uint32 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301271 logger.Debugw(ctx, "Received Add Icmpv6 Receivers", log.Fields{"device": device, "portID": portID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301272 var receiverList []uint32
1273 receivers, _ := va.Icmpv6Receivers.Load(device)
1274 if receivers != nil {
1275 receiverList = receivers.([]uint32)
1276 }
1277 receiverList = append(receiverList, portID)
1278 va.Icmpv6Receivers.Store(device, receiverList)
1279 logger.Debugw(ctx, "Receivers after addition", log.Fields{"Receivers": receiverList})
1280 return receiverList
1281}
1282
1283// DelIcmpv6Receivers to delete Icmp v6 receievers
1284func (va *VoltApplication) DelIcmpv6Receivers(device string, portID uint32) []uint32 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301285 logger.Debugw(ctx, "Received Add Icmpv6 Receivers", log.Fields{"device": device, "portID": portID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301286 var receiverList []uint32
1287 receivers, _ := va.Icmpv6Receivers.Load(device)
1288 if receivers != nil {
1289 receiverList = receivers.([]uint32)
1290 }
1291 for i, port := range receiverList {
1292 if port == portID {
1293 receiverList = append(receiverList[0:i], receiverList[i+1:]...)
1294 va.Icmpv6Receivers.Store(device, receiverList)
1295 break
1296 }
1297 }
1298 logger.Debugw(ctx, "Receivers After deletion", log.Fields{"Receivers": receiverList})
1299 return receiverList
1300}
1301
1302// ProcessDevFlowForDevice - Process DS ICMPv6 & ARP flow for provided device and vnet profile
1303// device - Device Obj
1304// vnet - vnet profile name
1305// enabled - vlan enabled/disabled - based on the status, the flow shall be added/removed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301306func (va *VoltApplication) ProcessDevFlowForDevice(cntx context.Context, device *VoltDevice, vnet *VoltVnet, enabled bool) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301307 logger.Debugw(ctx, "Process Dev Flow For Device", log.Fields{"Device": device, "VnetName": vnet.Name, "Enabled": enabled})
Naveen Sampath04696f72022-06-13 15:19:14 +05301308 _, applied := device.ConfiguredVlanForDeviceFlows.Get(VnetKey(vnet.SVlan, vnet.CVlan, 0))
1309 if enabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301310 va.PushDevFlowForVlan(cntx, vnet)
Naveen Sampath04696f72022-06-13 15:19:14 +05301311 } else if !enabled && applied {
1312 //va.DeleteDevFlowForVlan(vnet)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301313 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, device.SerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301314 }
1315}
1316
vinokuma926cb3e2023-03-29 11:41:06 +05301317// NniVlanIndToIgmp - Trigger receiver up indication to all ports with igmp enabled
1318// and has the provided mvlan
Naveen Sampath04696f72022-06-13 15:19:14 +05301319func (va *VoltApplication) NniVlanIndToIgmp(device *VoltDevice, mvp *MvlanProfile) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301320 logger.Infow(ctx, "Received Nni Vlan Ind To Igmp", log.Fields{"Vlan": mvp.Mvlan})
Naveen Sampath04696f72022-06-13 15:19:14 +05301321
vinokuma926cb3e2023-03-29 11:41:06 +05301322 // Trigger nni indication for receiver only for first time
Naveen Sampath04696f72022-06-13 15:19:14 +05301323 if device.IgmpDsFlowAppliedForMvlan[uint16(mvp.Mvlan)] {
1324 return
1325 }
1326 device.Ports.Range(func(key, value interface{}) bool {
1327 port := key.(string)
1328
1329 if state, _ := va.GetPortState(port); state == PortStateUp {
1330 vpvs, _ := va.VnetsByPort.Load(port)
1331 if vpvs == nil {
1332 return true
1333 }
1334 for _, vpv := range vpvs.([]*VoltPortVnet) {
vinokuma926cb3e2023-03-29 11:41:06 +05301335 // Send indication only for subscribers with the received mvlan profile
Naveen Sampath04696f72022-06-13 15:19:14 +05301336 if vpv.IgmpEnabled && vpv.MvlanProfileName == mvp.Name {
1337 vpv.services.Range(ReceiverUpInd)
1338 }
1339 }
1340 }
1341 return true
1342 })
1343}
1344
1345// PortUpInd :
1346// -----------------------------------------------------------------------
1347// Port status change handling
1348// ----------------------------------------------------------------------
1349// Port UP indication is passed to all services associated with the port
1350// so that the services can configure flows applicable when the port goes
1351// up from down state
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301352func (va *VoltApplication) PortUpInd(cntx context.Context, device string, port string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301353 logger.Infow(ctx, "Received Southbound Port Ind: UP", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301354 d := va.GetDevice(device)
1355
1356 if d == nil {
1357 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: UP", log.Fields{"Device": device, "Port": port})
1358 return
1359 }
1360
vinokuma926cb3e2023-03-29 11:41:06 +05301361 // Fixme: If Port Update Comes in large numbers, this will result in slow update per device
Naveen Sampath04696f72022-06-13 15:19:14 +05301362 va.portLock.Lock()
1363 // Do not defer the port mutex unlock here
1364 // Some of the following func calls needs the port lock, so defering the lock here
1365 // may lead to dead-lock
1366 p := d.GetPort(port)
1367
1368 if p == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301369 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 +05301370 va.portLock.Unlock()
1371 return
1372 }
1373 p.State = PortStateUp
1374 va.portLock.Unlock()
1375
Naveen Sampath04696f72022-06-13 15:19:14 +05301376 if p.Type == VoltPortTypeNni {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301377 logger.Debugw(ctx, "Received NNI Port Ind: UP", log.Fields{"Device": device, "PortName": port, "PortId": p.ID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301378 //va.PushDevFlowForDevice(d)
1379 //Build Igmp TrapFlowRule
1380 //va.ProcessIgmpDSFlowForDevice(d, true)
1381 }
1382 vpvs, ok := va.VnetsByPort.Load(port)
1383 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301384 logger.Warnw(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301385 //msgbus.ProcessPortInd(msgbus.PortUp, d.SerialNum, p.Name, false, getServiceList(port))
1386 return
1387 }
1388
vinokuma926cb3e2023-03-29 11:41:06 +05301389 // If NNI port is not UP, do not push Flows
Naveen Sampath04696f72022-06-13 15:19:14 +05301390 if d.NniPort == "" {
1391 logger.Warnw(ctx, "NNI port not UP. Not sending Port UP Ind for VPVs", log.Fields{"NNI": d.NniPort})
1392 return
1393 }
1394
Naveen Sampath04696f72022-06-13 15:19:14 +05301395 for _, vpv := range vpvs.([]*VoltPortVnet) {
1396 vpv.VpvLock.Lock()
vinokuma926cb3e2023-03-29 11:41:06 +05301397 // If no service is activated drop the portUpInd
Tinoj Josephec742f62022-09-29 19:11:10 +05301398 if vpv.IsServiceActivated(cntx) {
vinokuma926cb3e2023-03-29 11:41:06 +05301399 // Do not trigger indication for the vpv which is already removed from vpv list as
Tinoj Josephec742f62022-09-29 19:11:10 +05301400 // part of service delete (during the lock wait duration)
1401 // In that case, the services associated wil be zero
1402 if vpv.servicesCount.Load() != 0 {
1403 vpv.PortUpInd(cntx, d, port)
1404 }
1405 } else {
1406 // Service not activated, still attach device to service
1407 vpv.setDevice(d.Name)
Naveen Sampath04696f72022-06-13 15:19:14 +05301408 }
1409 vpv.VpvLock.Unlock()
1410 }
1411 // At the end of processing inform the other entities that
1412 // are interested in the events
1413}
1414
1415/*
1416func getServiceList(port string) map[string]bool {
1417 serviceList := make(map[string]bool)
1418
1419 getServiceNames := func(key interface{}, value interface{}) bool {
1420 serviceList[key.(string)] = value.(*VoltService).DsHSIAFlowsApplied
1421 return true
1422 }
1423
1424 if vpvs, _ := GetApplication().VnetsByPort.Load(port); vpvs != nil {
1425 vpvList := vpvs.([]*VoltPortVnet)
1426 for _, vpv := range vpvList {
1427 vpv.services.Range(getServiceNames)
1428 }
1429 }
1430 return serviceList
1431
1432}*/
1433
vinokuma926cb3e2023-03-29 11:41:06 +05301434// ReceiverUpInd - Send receiver up indication for service with Igmp enabled
Naveen Sampath04696f72022-06-13 15:19:14 +05301435func ReceiverUpInd(key, value interface{}) bool {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301436 logger.Info(ctx, "Receiver Indication: UP")
Naveen Sampath04696f72022-06-13 15:19:14 +05301437 svc := value.(*VoltService)
1438 var vlan of.VlanType
1439
1440 if !svc.IPAssigned() {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301441 logger.Warnw(ctx, "IP Not assigned, skipping general query", log.Fields{"Service": svc})
Naveen Sampath04696f72022-06-13 15:19:14 +05301442 return false
1443 }
1444
vinokuma926cb3e2023-03-29 11:41:06 +05301445 // Send port up indication to igmp only for service with igmp enabled
Naveen Sampath04696f72022-06-13 15:19:14 +05301446 if svc.IgmpEnabled {
1447 if svc.VlanControl == ONUCVlan || svc.VlanControl == ONUCVlanOLTSVlan {
1448 vlan = svc.CVlan
1449 } else {
1450 vlan = svc.UniVlan
1451 }
1452 if device, _ := GetApplication().GetDeviceFromPort(svc.Port); device != nil {
1453 GetApplication().ReceiverUpInd(device.Name, svc.Port, svc.MvlanProfileName, vlan, svc.Pbits)
1454 }
1455 return false
1456 }
1457 return true
1458}
1459
1460// PortDownInd : Port down indication is passed on to the services so that the services
1461// can make changes at this transition.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301462func (va *VoltApplication) PortDownInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301463 logger.Infow(ctx, "Received SouthBound Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1464 d := va.GetDevice(device)
1465
1466 if d == nil {
1467 logger.Warnw(ctx, "Device Not Found - Dropping Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
1468 return
1469 }
vinokuma926cb3e2023-03-29 11:41:06 +05301470 // Fixme: If Port Update Comes in large numbers, this will result in slow update per device
Naveen Sampath04696f72022-06-13 15:19:14 +05301471 va.portLock.Lock()
1472 // Do not defer the port mutex unlock here
1473 // Some of the following func calls needs the port lock, so defering the lock here
1474 // may lead to dead-lock
1475 p := d.GetPort(port)
1476 if p == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301477 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 +05301478 va.portLock.Unlock()
1479 return
1480 }
1481 p.State = PortStateDown
1482 va.portLock.Unlock()
1483
1484 if d.State == controller.DeviceStateREBOOTED {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301485 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 +05301486 return
1487 }
1488
1489 if p.Type == VoltPortTypeNni {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301490 logger.Debugw(ctx, "Received NNI Port Ind: DOWN", log.Fields{"Device": device, "Port": port})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301491 va.DeleteDevFlowForDevice(cntx, d)
1492 va.NniDownInd(cntx, device, d.SerialNum)
1493 va.RemovePendingGroups(cntx, device, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301494 }
1495 vpvs, ok := va.VnetsByPort.Load(port)
1496 if !ok || nil == vpvs || len(vpvs.([]*VoltPortVnet)) == 0 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301497 logger.Warnw(ctx, "No VNETs on port", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301498 //msgbus.ProcessPortInd(msgbus.PortDown, d.SerialNum, p.Name, false, getServiceList(port))
1499 return
1500 }
Akash Sonia8246972023-01-03 10:37:08 +05301501
Naveen Sampath04696f72022-06-13 15:19:14 +05301502 for _, vpv := range vpvs.([]*VoltPortVnet) {
1503 vpv.VpvLock.Lock()
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301504 vpv.PortDownInd(cntx, device, port, false)
Naveen Sampath04696f72022-06-13 15:19:14 +05301505 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301506 va.ReceiverDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +05301507 }
1508 vpv.VpvLock.Unlock()
1509 }
1510}
1511
1512// PacketInInd :
1513// -----------------------------------------------------------------------
1514// PacketIn Processing
1515// Packet In Indication processing. It arrives with the identities of
1516// the device and port on which the packet is received. At first, the
1517// packet is decoded and the right processor is called. Currently, we
1518// plan to support only DHCP and IGMP. In future, we can add more
1519// capabilities as needed
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301520func (va *VoltApplication) PacketInInd(cntx context.Context, device string, port string, pkt []byte) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301521 logger.Infow(ctx, "Received a Packet-In Indication", log.Fields{"Device": device, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301522 // Decode the incoming packet
1523 packetSide := US
1524 if strings.Contains(port, NNI) {
1525 packetSide = DS
1526 }
1527
Naveen Sampath04696f72022-06-13 15:19:14 +05301528 gopkt := gopacket.NewPacket(pkt, layers.LayerTypeEthernet, gopacket.Default)
1529
1530 var dot1qFound = false
1531 for _, l := range gopkt.Layers() {
1532 if l.LayerType() == layers.LayerTypeDot1Q {
1533 dot1qFound = true
1534 break
1535 }
1536 }
1537
1538 if !dot1qFound {
1539 logger.Debugw(ctx, "Ignoring Received Packet-In Indication without Dot1Q Header",
1540 log.Fields{"Device": device, "Port": port})
1541 return
1542 }
1543
1544 logger.Debugw(ctx, "Received Southbound Packet In", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1545
1546 // Classify the packet into packet types that we support
1547 // The supported types are DHCP and IGMP. The DHCP packet is
1548 // identified by matching the L4 protocol to UDP. The IGMP packet
1549 // is identified by matching L3 protocol to IGMP
1550 arpl := gopkt.Layer(layers.LayerTypeARP)
1551 if arpl != nil {
1552 if callBack, ok := PacketHandlers[ARP]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301553 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301554 } else {
1555 logger.Debugw(ctx, "ARP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1556 }
1557 return
1558 }
1559 ipv4l := gopkt.Layer(layers.LayerTypeIPv4)
1560 if ipv4l != nil {
1561 ip := ipv4l.(*layers.IPv4)
1562
1563 if ip.Protocol == layers.IPProtocolUDP {
1564 logger.Debugw(ctx, "Received Southbound UDP ipv4 packet in", log.Fields{"StreamSide": packetSide})
1565 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv4)
1566 if dhcpl != nil {
1567 if callBack, ok := PacketHandlers[DHCPv4]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301568 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301569 } else {
1570 logger.Debugw(ctx, "DHCPv4 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1571 }
1572 }
1573 } else if ip.Protocol == layers.IPProtocolIGMP {
1574 logger.Debugw(ctx, "Received Southbound IGMP packet in", log.Fields{"StreamSide": packetSide})
1575 if callBack, ok := PacketHandlers[IGMP]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301576 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301577 } else {
1578 logger.Debugw(ctx, "IGMP handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1579 }
1580 }
1581 return
1582 }
1583 ipv6l := gopkt.Layer(layers.LayerTypeIPv6)
1584 if ipv6l != nil {
1585 ip := ipv6l.(*layers.IPv6)
1586 if ip.NextHeader == layers.IPProtocolUDP {
1587 logger.Debug(ctx, "Received Southbound UDP ipv6 packet in")
1588 dhcpl := gopkt.Layer(layers.LayerTypeDHCPv6)
1589 if dhcpl != nil {
1590 if callBack, ok := PacketHandlers[DHCPv6]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301591 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301592 } else {
1593 logger.Debugw(ctx, "DHCPv6 handler is not registered, dropping the packet", log.Fields{"Pkt": hex.EncodeToString(gopkt.Data())})
1594 }
1595 }
1596 }
1597 return
1598 }
1599
1600 pppoel := gopkt.Layer(layers.LayerTypePPPoE)
1601 if pppoel != nil {
1602 logger.Debugw(ctx, "Received Southbound PPPoE packet in", log.Fields{"StreamSide": packetSide})
1603 if callBack, ok := PacketHandlers[PPPOE]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301604 callBack(cntx, device, port, gopkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301605 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301606 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 +05301607 }
1608 }
1609}
1610
1611// GetVlans : This utility gets the VLANs from the packet. The VLANs are
1612// used to identify the right service that must process the incoming
1613// packet
1614func GetVlans(pkt gopacket.Packet) []of.VlanType {
1615 var vlans []of.VlanType
1616 for _, l := range pkt.Layers() {
1617 if l.LayerType() == layers.LayerTypeDot1Q {
1618 q, ok := l.(*layers.Dot1Q)
1619 if ok {
1620 vlans = append(vlans, of.VlanType(q.VLANIdentifier))
1621 }
1622 }
1623 }
1624 return vlans
1625}
1626
1627// GetPriority to get priority
1628func GetPriority(pkt gopacket.Packet) uint8 {
1629 for _, l := range pkt.Layers() {
1630 if l.LayerType() == layers.LayerTypeDot1Q {
1631 q, ok := l.(*layers.Dot1Q)
1632 if ok {
1633 return q.Priority
1634 }
1635 }
1636 }
1637 return PriorityNone
1638}
1639
1640// HandleFlowClearFlag to handle flow clear flag during reboot
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301641func (va *VoltApplication) HandleFlowClearFlag(cntx context.Context, deviceID string, serialNum, southBoundID string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301642 logger.Infow(ctx, "Clear All flags for Device", log.Fields{"Device": deviceID, "SerialNum": serialNum, "SBID": southBoundID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301643 dev, ok := va.DevicesDisc.Load(deviceID)
1644 if ok && dev != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301645 logger.Debugw(ctx, "Clear Flags for device", log.Fields{"voltDevice": dev.(*VoltDevice).Name})
Naveen Sampath04696f72022-06-13 15:19:14 +05301646 dev.(*VoltDevice).icmpv6GroupAdded = false
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301647 logger.Debugw(ctx, "Clearing DS Icmpv6 Map",
Naveen Sampath04696f72022-06-13 15:19:14 +05301648 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1649 dev.(*VoltDevice).ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301650 logger.Debugw(ctx, "Clearing DS IGMP Map",
Naveen Sampath04696f72022-06-13 15:19:14 +05301651 log.Fields{"voltDevice": dev.(*VoltDevice).Name})
1652 for k := range dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan {
1653 delete(dev.(*VoltDevice).IgmpDsFlowAppliedForMvlan, k)
1654 }
vinokuma926cb3e2023-03-29 11:41:06 +05301655 // Delete group 1 - ICMPv6/ARP group
Naveen Sampath04696f72022-06-13 15:19:14 +05301656 if err := ProcessIcmpv6McGroup(deviceID, true); err != nil {
1657 logger.Errorw(ctx, "ProcessIcmpv6McGroup failed", log.Fields{"Device": deviceID, "Error": err})
1658 }
1659 } else {
1660 logger.Warnw(ctx, "VoltDevice not found for device ", log.Fields{"deviceID": deviceID})
1661 }
1662
1663 getVpvs := func(key interface{}, value interface{}) bool {
1664 vpvs := value.([]*VoltPortVnet)
1665 for _, vpv := range vpvs {
1666 if vpv.Device == deviceID {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301667 logger.Debugw(ctx, "Clear Flags for vpv",
Naveen Sampath04696f72022-06-13 15:19:14 +05301668 log.Fields{"device": vpv.Device, "port": vpv.Port,
1669 "svlan": vpv.SVlan, "cvlan": vpv.CVlan, "univlan": vpv.UniVlan})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301670 vpv.ClearAllServiceFlags(cntx)
1671 vpv.ClearAllVpvFlags(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301672
1673 if vpv.IgmpEnabled {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301674 va.ReceiverDownInd(cntx, vpv.Device, vpv.Port)
vinokuma926cb3e2023-03-29 11:41:06 +05301675 // Also clear service igmp stats
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301676 vpv.ClearServiceCounters(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301677 }
1678 }
1679 }
1680 return true
1681 }
1682 va.VnetsByPort.Range(getVpvs)
1683
vinokuma926cb3e2023-03-29 11:41:06 +05301684 // Clear Static Group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301685 va.ReceiverDownInd(cntx, deviceID, StaticPort)
Naveen Sampath04696f72022-06-13 15:19:14 +05301686
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301687 logger.Infow(ctx, "All flags cleared for device", log.Fields{"Device": deviceID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301688
vinokuma926cb3e2023-03-29 11:41:06 +05301689 // Reset pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301690 va.RemovePendingGroups(cntx, deviceID, true)
Naveen Sampath04696f72022-06-13 15:19:14 +05301691
vinokuma926cb3e2023-03-29 11:41:06 +05301692 // Process all Migrate Service Request - force udpate all profiles since resources are already cleaned up
Naveen Sampath04696f72022-06-13 15:19:14 +05301693 if dev != nil {
1694 triggerForceUpdate := func(key, value interface{}) bool {
1695 msrList := value.(*util.ConcurrentMap)
1696 forceUpdateServices := func(key, value interface{}) bool {
1697 msr := value.(*MigrateServicesRequest)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301698 forceUpdateAllServices(cntx, msr)
Naveen Sampath04696f72022-06-13 15:19:14 +05301699 return true
1700 }
1701 msrList.Range(forceUpdateServices)
1702 return true
1703 }
1704 dev.(*VoltDevice).MigratingServices.Range(triggerForceUpdate)
1705 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301706 va.FetchAndProcessAllMigrateServicesReq(cntx, deviceID, forceUpdateAllServices)
Naveen Sampath04696f72022-06-13 15:19:14 +05301707 }
1708}
1709
vinokuma926cb3e2023-03-29 11:41:06 +05301710// GetPonPortIDFromUNIPort to get pon port id from uni port
Naveen Sampath04696f72022-06-13 15:19:14 +05301711func GetPonPortIDFromUNIPort(uniPortID uint32) uint32 {
1712 ponPortID := (uniPortID & 0x0FF00000) >> 20
1713 return ponPortID
1714}
1715
vinokuma926cb3e2023-03-29 11:41:06 +05301716// ProcessFlowModResultIndication - Processes Flow mod operation indications from controller
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301717func (va *VoltApplication) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301718 logger.Debugw(ctx, "Received Flow Mod Result Indication.", log.Fields{"Cookie": flowStatus.Cookie, "Device": flowStatus.Device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301719 d := va.GetDevice(flowStatus.Device)
1720 if d == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301721 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 +05301722 return
1723 }
1724
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301725 cookieExists := ExecuteFlowEvent(cntx, d, flowStatus.Cookie, flowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +05301726
1727 if flowStatus.Flow != nil {
1728 flowAdd := (flowStatus.FlowModType == of.CommandAdd)
1729 if !cookieExists && !isFlowStatusSuccess(flowStatus.Status, flowAdd) {
1730 pushFlowFailureNotif(flowStatus)
1731 }
1732 }
1733}
1734
1735func pushFlowFailureNotif(flowStatus intf.FlowStatus) {
1736 subFlow := flowStatus.Flow
1737 cookie := subFlow.Cookie
1738 uniPort := cookie >> 16 & 0xFFFFFFFF
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301739 logger.Warnw(ctx, "Flow Failure Notification", log.Fields{"uniPort": uniPort, "Cookie": cookie})
Naveen Sampath04696f72022-06-13 15:19:14 +05301740}
1741
vinokuma926cb3e2023-03-29 11:41:06 +05301742// UpdateMvlanProfilesForDevice to update mvlan profile for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301743func (va *VoltApplication) UpdateMvlanProfilesForDevice(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301744 logger.Debugw(ctx, "Received Update Mvlan Profiles For Device", log.Fields{"device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301745 checkAndAddMvlanUpdateTask := func(key, value interface{}) bool {
1746 mvp := value.(*MvlanProfile)
1747 if mvp.IsUpdateInProgressForDevice(device) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301748 mvp.UpdateProfile(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301749 }
1750 return true
1751 }
1752 va.MvlanProfilesByName.Range(checkAndAddMvlanUpdateTask)
1753}
1754
1755// TaskInfo structure that is used to store the task Info.
1756type TaskInfo struct {
1757 ID string
1758 Name string
1759 Timestamp string
1760}
1761
1762// GetTaskList to get task list information.
1763func (va *VoltApplication) GetTaskList(device string) map[int]*TaskInfo {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301764 logger.Debugw(ctx, "Received Get Task List", log.Fields{"device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05301765 taskList := cntlr.GetController().GetTaskList(device)
1766 taskMap := make(map[int]*TaskInfo)
1767 for i, task := range taskList {
1768 taskID := strconv.Itoa(int(task.TaskID()))
1769 name := task.Name()
1770 timestamp := task.Timestamp()
1771 taskInfo := &TaskInfo{ID: taskID, Name: name, Timestamp: timestamp}
1772 taskMap[i] = taskInfo
1773 }
1774 return taskMap
1775}
1776
1777// UpdateDeviceSerialNumberList to update the device serial number list after device serial number is updated for vnet and mvlan
1778func (va *VoltApplication) UpdateDeviceSerialNumberList(oldOltSlNo string, newOltSlNo string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301779 logger.Debugw(ctx, "Update Device Serial Number List", log.Fields{"oldOltSlNo": oldOltSlNo, "newOltSlNo": newOltSlNo})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05301780 voltDevice, _ := va.GetDeviceBySerialNo(oldOltSlNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301781
1782 if voltDevice != nil {
1783 // Device is present with old serial number ID
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301784 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 +05301785 } else {
1786 logger.Infow(ctx, "No device present with old serial number", log.Fields{"Serial Number": oldOltSlNo})
Naveen Sampath04696f72022-06-13 15:19:14 +05301787 // Add Serial Number to Blocked Devices List.
1788 cntlr.GetController().AddBlockedDevices(oldOltSlNo)
1789 cntlr.GetController().AddBlockedDevices(newOltSlNo)
1790
1791 updateSlNoForVnet := func(key, value interface{}) bool {
1792 vnet := value.(*VoltVnet)
1793 for i, deviceSlNo := range vnet.VnetConfig.DevicesList {
1794 if deviceSlNo == oldOltSlNo {
1795 vnet.VnetConfig.DevicesList[i] = newOltSlNo
1796 logger.Infow(ctx, "device serial number updated for vnet profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1797 break
1798 }
1799 }
1800 return true
1801 }
1802
1803 updateSlNoforMvlan := func(key interface{}, value interface{}) bool {
1804 mvProfile := value.(*MvlanProfile)
1805 for deviceSlNo := range mvProfile.DevicesList {
1806 if deviceSlNo == oldOltSlNo {
1807 mvProfile.DevicesList[newOltSlNo] = mvProfile.DevicesList[oldOltSlNo]
1808 delete(mvProfile.DevicesList, oldOltSlNo)
1809 logger.Infow(ctx, "device serial number updated for mvlan profile", log.Fields{"Updated Serial Number": deviceSlNo, "Previous Serial Number": oldOltSlNo})
1810 break
1811 }
1812 }
1813 return true
1814 }
1815
1816 va.VnetsByName.Range(updateSlNoForVnet)
1817 va.MvlanProfilesByName.Range(updateSlNoforMvlan)
1818
1819 // Clear the serial number from Blocked Devices List
1820 cntlr.GetController().DelBlockedDevices(oldOltSlNo)
1821 cntlr.GetController().DelBlockedDevices(newOltSlNo)
Naveen Sampath04696f72022-06-13 15:19:14 +05301822 }
1823}
1824
1825// GetVpvsForDsPkt to get vpv for downstream packets
1826func (va *VoltApplication) GetVpvsForDsPkt(cvlan of.VlanType, svlan of.VlanType, clientMAC net.HardwareAddr,
1827 pbit uint8) ([]*VoltPortVnet, error) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301828 logger.Debugw(ctx, "Received Get Vpvs For Ds Pkt", log.Fields{"Cvlan": cvlan, "Svlan": svlan, "Mac": clientMAC})
Naveen Sampath04696f72022-06-13 15:19:14 +05301829 var matchVPVs []*VoltPortVnet
1830 findVpv := func(key, value interface{}) bool {
1831 vpvs := value.([]*VoltPortVnet)
1832 for _, vpv := range vpvs {
1833 if vpv.isVlanMatching(cvlan, svlan) && vpv.MatchesPriority(pbit) != nil {
1834 var subMac net.HardwareAddr
1835 if NonZeroMacAddress(vpv.MacAddr) {
1836 subMac = vpv.MacAddr
1837 } else if vpv.LearntMacAddr != nil && NonZeroMacAddress(vpv.LearntMacAddr) {
1838 subMac = vpv.LearntMacAddr
1839 } else {
1840 matchVPVs = append(matchVPVs, vpv)
1841 continue
1842 }
1843 if util.MacAddrsMatch(subMac, clientMAC) {
1844 matchVPVs = append([]*VoltPortVnet{}, vpv)
1845 logger.Infow(ctx, "Matching VPV found", log.Fields{"Port": vpv.Port, "SVLAN": vpv.SVlan, "CVLAN": vpv.CVlan, "UNIVlan": vpv.UniVlan, "MAC": clientMAC})
1846 return false
1847 }
1848 }
1849 }
1850 return true
1851 }
1852 va.VnetsByPort.Range(findVpv)
1853
1854 if len(matchVPVs) != 1 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301855 logger.Errorw(ctx, "No matching VPV found or multiple vpvs found", log.Fields{"Match VPVs": matchVPVs, "MAC": clientMAC})
1856 return nil, errors.New("no matching VPV found or multiple vpvs found")
Naveen Sampath04696f72022-06-13 15:19:14 +05301857 }
1858 return matchVPVs, nil
1859}
1860
1861// GetMacInPortMap to get PORT value based on MAC key
1862func (va *VoltApplication) GetMacInPortMap(macAddr net.HardwareAddr) string {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301863 logger.Debugw(ctx, "Received Get PORT value based on MAC key", log.Fields{"MacAddr": macAddr.String()})
Naveen Sampath04696f72022-06-13 15:19:14 +05301864 if NonZeroMacAddress(macAddr) {
1865 va.macPortLock.Lock()
1866 defer va.macPortLock.Unlock()
1867 if port, ok := va.macPortMap[macAddr.String()]; ok {
1868 logger.Debugw(ctx, "found-entry-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1869 return port
1870 }
1871 }
1872 logger.Infow(ctx, "entry-not-found-macportmap", log.Fields{"MacAddr": macAddr.String()})
1873 return ""
1874}
1875
1876// UpdateMacInPortMap to update MAC PORT (key value) information in MacPortMap
1877func (va *VoltApplication) UpdateMacInPortMap(macAddr net.HardwareAddr, port string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301878 logger.Debugw(ctx, "Update Macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301879 if NonZeroMacAddress(macAddr) {
1880 va.macPortLock.Lock()
1881 va.macPortMap[macAddr.String()] = port
1882 va.macPortLock.Unlock()
1883 logger.Debugw(ctx, "updated-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1884 }
1885}
1886
1887// DeleteMacInPortMap to remove MAC key from MacPortMap
1888func (va *VoltApplication) DeleteMacInPortMap(macAddr net.HardwareAddr) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301889 logger.Debugw(ctx, "Delete Mac from Macportmap", log.Fields{"MacAddr": macAddr.String()})
Naveen Sampath04696f72022-06-13 15:19:14 +05301890 if NonZeroMacAddress(macAddr) {
1891 port := va.GetMacInPortMap(macAddr)
1892 va.macPortLock.Lock()
1893 delete(va.macPortMap, macAddr.String())
1894 va.macPortLock.Unlock()
1895 logger.Debugw(ctx, "deleted-from-macportmap", log.Fields{"MacAddr": macAddr.String(), "Port": port})
1896 }
1897}
1898
vinokuma926cb3e2023-03-29 11:41:06 +05301899// AddGroupToPendingPool - adds the IgmpGroup with active group table entry to global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301900func (va *VoltApplication) AddGroupToPendingPool(ig *IgmpGroup) {
1901 var grpMap map[*IgmpGroup]bool
1902 var ok bool
1903
1904 va.PendingPoolLock.Lock()
1905 defer va.PendingPoolLock.Unlock()
1906
1907 logger.Infow(ctx, "Adding IgmpGroup to Global Pending Pool", log.Fields{"GroupID": ig.GroupID, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr, "PendingDevices": len(ig.Devices)})
1908 // Do Not Reset any current profile info since group table entry tied to mvlan profile
1909 // The PonVlan is part of set field in group installed
1910 // Hence, Group created is always tied to the same mvlan profile until deleted
1911
1912 for device := range ig.Devices {
1913 key := getPendingPoolKey(ig.Mvlan, device)
1914
1915 if grpMap, ok = va.IgmpPendingPool[key]; !ok {
1916 grpMap = make(map[*IgmpGroup]bool)
1917 }
1918 grpMap[ig] = true
1919
1920 //Add grpObj reference to all associated devices
1921 va.IgmpPendingPool[key] = grpMap
1922 }
1923}
1924
vinokuma926cb3e2023-03-29 11:41:06 +05301925// RemoveGroupFromPendingPool - removes the group from global pending group pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301926func (va *VoltApplication) RemoveGroupFromPendingPool(device string, ig *IgmpGroup) bool {
1927 GetApplication().PendingPoolLock.Lock()
1928 defer GetApplication().PendingPoolLock.Unlock()
1929
1930 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)})
1931
1932 key := getPendingPoolKey(ig.Mvlan, device)
1933 if _, ok := va.IgmpPendingPool[key]; ok {
1934 delete(va.IgmpPendingPool[key], ig)
1935 return true
1936 }
1937 return false
1938}
1939
vinokuma926cb3e2023-03-29 11:41:06 +05301940// RemoveGroupsFromPendingPool - removes the group from global pending group pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301941func (va *VoltApplication) RemoveGroupsFromPendingPool(cntx context.Context, device string, mvlan of.VlanType) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301942 GetApplication().PendingPoolLock.Lock()
1943 defer GetApplication().PendingPoolLock.Unlock()
1944
1945 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool for given Deivce & Mvlan", log.Fields{"Device": device, "Mvlan": mvlan.String()})
1946
1947 key := getPendingPoolKey(mvlan, device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301948 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301949}
1950
vinokuma926cb3e2023-03-29 11:41:06 +05301951// RemoveGroupListFromPendingPool - removes the groups for provided key
Naveen Sampath04696f72022-06-13 15:19:14 +05301952// 1. Deletes the group from device
1953// 2. Delete the IgmpGroup obj and release the group ID to pool
1954// Note: Make sure to obtain PendingPoolLock lock before calling this func
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301955func (va *VoltApplication) RemoveGroupListFromPendingPool(cntx context.Context, key string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301956 logger.Infow(ctx, "Remove GroupList from Pending Pool for ", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301957 if grpMap, ok := va.IgmpPendingPool[key]; ok {
1958 delete(va.IgmpPendingPool, key)
1959 for ig := range grpMap {
1960 for device := range ig.Devices {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301961 ig.DeleteIgmpGroupDevice(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +05301962 }
1963 }
1964 }
1965}
1966
vinokuma926cb3e2023-03-29 11:41:06 +05301967// RemoveGroupDevicesFromPendingPool - removes the group from global pending group pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301968func (va *VoltApplication) RemoveGroupDevicesFromPendingPool(ig *IgmpGroup) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301969 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)})
1970 for device := range ig.PendingGroupForDevice {
1971 va.RemoveGroupFromPendingPool(device, ig)
1972 }
1973}
1974
vinokuma926cb3e2023-03-29 11:41:06 +05301975// GetGroupFromPendingPool - Returns IgmpGroup obj from global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301976func (va *VoltApplication) GetGroupFromPendingPool(mvlan of.VlanType, device string) *IgmpGroup {
Naveen Sampath04696f72022-06-13 15:19:14 +05301977 var ig *IgmpGroup
1978
1979 va.PendingPoolLock.Lock()
1980 defer va.PendingPoolLock.Unlock()
1981
1982 key := getPendingPoolKey(mvlan, device)
1983 logger.Infow(ctx, "Getting IgmpGroup from Global Pending Pool", log.Fields{"Device": device, "Mvlan": mvlan.String(), "Key": key})
1984
vinokuma926cb3e2023-03-29 11:41:06 +05301985 // Gets all IgmpGrp Obj for the device
Naveen Sampath04696f72022-06-13 15:19:14 +05301986 grpMap, ok := va.IgmpPendingPool[key]
1987 if !ok || len(grpMap) == 0 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05301988 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 +05301989 return nil
1990 }
1991
vinokuma926cb3e2023-03-29 11:41:06 +05301992 // Gets a random obj from available grps
Naveen Sampath04696f72022-06-13 15:19:14 +05301993 for ig = range grpMap {
vinokuma926cb3e2023-03-29 11:41:06 +05301994 // Remove grp obj reference from all devices associated in pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05301995 for dev := range ig.Devices {
1996 key := getPendingPoolKey(mvlan, dev)
1997 delete(va.IgmpPendingPool[key], ig)
1998 }
1999
vinokuma926cb3e2023-03-29 11:41:06 +05302000 // Safety check to avoid re-allocating group already in use
Naveen Sampath04696f72022-06-13 15:19:14 +05302001 if ig.NumDevicesActive() == 0 {
2002 return ig
2003 }
2004
vinokuma926cb3e2023-03-29 11:41:06 +05302005 // Iteration will continue only if IG is not allocated
Naveen Sampath04696f72022-06-13 15:19:14 +05302006 }
2007 return nil
2008}
2009
vinokuma926cb3e2023-03-29 11:41:06 +05302010// RemovePendingGroups - removes all pending groups for provided reference from global pending pool
Naveen Sampath04696f72022-06-13 15:19:14 +05302011// reference - mvlan/device ID
2012// isRefDevice - true - Device as reference
vinokuma926cb3e2023-03-29 11:41:06 +05302013// false - Mvlan as reference
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302014func (va *VoltApplication) RemovePendingGroups(cntx context.Context, reference string, isRefDevice bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +05302015 va.PendingPoolLock.Lock()
2016 defer va.PendingPoolLock.Unlock()
2017
2018 logger.Infow(ctx, "Removing IgmpGroups from Global Pending Pool", log.Fields{"Reference": reference, "isRefDevice": isRefDevice})
2019
vinokuma926cb3e2023-03-29 11:41:06 +05302020 // Pending Pool key: "<mvlan>_<DeviceID>""
Naveen Sampath04696f72022-06-13 15:19:14 +05302021 paramPosition := 0
2022 if isRefDevice {
2023 paramPosition = 1
2024 }
2025
2026 // 1.Remove the Entry from pending pool
2027 // 2.Deletes the group from device
2028 // 3.Delete the IgmpGroup obj and release the group ID to pool
2029 for key := range va.IgmpPendingPool {
2030 keyParams := strings.Split(key, "_")
2031 if keyParams[paramPosition] == reference {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302032 va.RemoveGroupListFromPendingPool(cntx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05302033 }
2034 }
2035}
2036
2037func getPendingPoolKey(mvlan of.VlanType, device string) string {
2038 return mvlan.String() + "_" + device
2039}
2040
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302041func (va *VoltApplication) removeExpiredGroups(cntx context.Context) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302042 logger.Info(ctx, "Remove expired Igmp Groups")
Naveen Sampath04696f72022-06-13 15:19:14 +05302043 removeExpiredGroups := func(key interface{}, value interface{}) bool {
2044 ig := value.(*IgmpGroup)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302045 ig.removeExpiredGroupFromDevice(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302046 return true
2047 }
2048 va.IgmpGroups.Range(removeExpiredGroups)
2049}
2050
vinokuma926cb3e2023-03-29 11:41:06 +05302051// TriggerPendingProfileDeleteReq - trigger pending profile delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302052func (va *VoltApplication) TriggerPendingProfileDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302053 logger.Infow(ctx, "Trigger Pending Profile Delete for device", log.Fields{"Device": device})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302054 va.TriggerPendingServiceDeactivateReq(cntx, device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302055 va.TriggerPendingServiceDeleteReq(cntx, device)
2056 va.TriggerPendingVpvDeleteReq(cntx, device)
2057 va.TriggerPendingVnetDeleteReq(cntx, device)
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302058 logger.Infow(ctx, "All Pending Profile Delete triggered for device", log.Fields{"Device": device})
Naveen Sampath04696f72022-06-13 15:19:14 +05302059}
2060
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302061// TriggerPendingServiceDeactivateReq - trigger pending service deactivate request
2062func (va *VoltApplication) TriggerPendingServiceDeactivateReq(cntx context.Context, device string) {
2063 logger.Infow(ctx, "Pending Services to be deactivated", log.Fields{"Count": len(va.ServicesToDeactivate)})
2064 for serviceName := range va.ServicesToDeactivate {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302065 logger.Debugw(ctx, "Trigger Service Deactivate", log.Fields{"Service": serviceName})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302066 if vs := va.GetService(serviceName); vs != nil {
2067 if vs.Device == device {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302068 logger.Infow(ctx, "Triggering Pending Service Deactivate", log.Fields{"Service": vs.Name})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302069 vpv := va.GetVnetByPort(vs.Port, vs.SVlan, vs.CVlan, vs.UniVlan)
2070 if vpv == nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302071 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 +05302072 continue
2073 }
2074
2075 vpv.DelTrapFlows(cntx)
2076 vs.DelHsiaFlows(cntx)
2077 vs.WriteToDb(cntx)
2078 vpv.ClearServiceCounters(cntx)
2079 }
2080 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302081 logger.Warnw(ctx, "Pending Service Not found", log.Fields{"Service": serviceName})
Hitesh Chhabra64be2442023-06-21 17:06:34 +05302082 }
2083 }
2084}
2085
vinokuma926cb3e2023-03-29 11:41:06 +05302086// TriggerPendingServiceDeleteReq - trigger pending service delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302087func (va *VoltApplication) TriggerPendingServiceDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302088 logger.Infow(ctx, "Pending Services to be deleted", log.Fields{"Count": len(va.ServicesToDelete)})
Naveen Sampath04696f72022-06-13 15:19:14 +05302089 for serviceName := range va.ServicesToDelete {
2090 logger.Debugw(ctx, "Trigger Service Delete", log.Fields{"Service": serviceName})
2091 if vs := va.GetService(serviceName); vs != nil {
2092 if vs.Device == device {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302093 logger.Infow(ctx, "Triggering Pending Service delete", log.Fields{"Service": vs.Name})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302094 vs.DelHsiaFlows(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302095 if vs.ForceDelete {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302096 vs.DelFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05302097 }
2098 }
2099 } else {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302100 logger.Warnw(ctx, "Pending Service Not found", log.Fields{"Service": serviceName})
Naveen Sampath04696f72022-06-13 15:19:14 +05302101 }
2102 }
2103}
2104
vinokuma926cb3e2023-03-29 11:41:06 +05302105// TriggerPendingVpvDeleteReq - trigger pending VPV delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302106func (va *VoltApplication) TriggerPendingVpvDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302107 logger.Infow(ctx, "Pending VPVs to be deleted", log.Fields{"Count": len(va.VoltPortVnetsToDelete)})
Naveen Sampath04696f72022-06-13 15:19:14 +05302108 for vpv := range va.VoltPortVnetsToDelete {
2109 if vpv.Device == device {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302110 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 +05302111 va.DelVnetFromPort(cntx, vpv.Port, vpv)
Naveen Sampath04696f72022-06-13 15:19:14 +05302112 }
2113 }
2114}
2115
vinokuma926cb3e2023-03-29 11:41:06 +05302116// TriggerPendingVnetDeleteReq - trigger pending vnet delete request
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302117func (va *VoltApplication) TriggerPendingVnetDeleteReq(cntx context.Context, device string) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302118 logger.Infow(ctx, "Pending Vnets to be deleted", log.Fields{"Count": len(va.VnetsToDelete)})
Naveen Sampath04696f72022-06-13 15:19:14 +05302119 for vnetName := range va.VnetsToDelete {
2120 if vnetIntf, _ := va.VnetsByName.Load(vnetName); vnetIntf != nil {
2121 vnet := vnetIntf.(*VoltVnet)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302122 if d, _ := va.GetDeviceBySerialNo(vnet.PendingDeviceToDelete); d != nil && d.SerialNum == vnet.PendingDeviceToDelete {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302123 logger.Infow(ctx, "Triggering Pending Vnet flows delete", log.Fields{"Vnet": vnet.Name, "Device": vnet.PendingDeviceToDelete})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05302124 va.DeleteDevFlowForVlanFromDevice(cntx, vnet, vnet.PendingDeviceToDelete)
Naveen Sampath04696f72022-06-13 15:19:14 +05302125 va.deleteVnetConfig(vnet)
2126 } else {
Tinoj Joseph1d108322022-07-13 10:07:39 +05302127 logger.Warnw(ctx, "Vnet Delete Failed : Device Not Found", log.Fields{"Vnet": vnet.Name, "Device": vnet.PendingDeviceToDelete})
Naveen Sampath04696f72022-06-13 15:19:14 +05302128 }
2129 }
2130 }
2131}
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302132
2133type OltFlowService struct {
vinokuma926cb3e2023-03-29 11:41:06 +05302134 DefaultTechProfileID int `json:"defaultTechProfileId"`
Akash Sonia8246972023-01-03 10:37:08 +05302135 EnableDhcpOnNni bool `json:"enableDhcpOnNni"`
Akash Sonia8246972023-01-03 10:37:08 +05302136 EnableIgmpOnNni bool `json:"enableIgmpOnNni"`
2137 EnableEapol bool `json:"enableEapol"`
2138 EnableDhcpV6 bool `json:"enableDhcpV6"`
2139 EnableDhcpV4 bool `json:"enableDhcpV4"`
2140 RemoveFlowsOnDisable bool `json:"removeFlowsOnDisable"`
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302141}
2142
2143func (va *VoltApplication) UpdateOltFlowService(cntx context.Context, oltFlowService OltFlowService) {
2144 logger.Infow(ctx, "UpdateOltFlowService", log.Fields{"oldValue": va.OltFlowServiceConfig, "newValue": oltFlowService})
2145 va.OltFlowServiceConfig = oltFlowService
2146 b, err := json.Marshal(va.OltFlowServiceConfig)
2147 if err != nil {
2148 logger.Warnw(ctx, "Failed to Marshal OltFlowServiceConfig", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2149 return
2150 }
2151 _ = db.PutOltFlowService(cntx, string(b))
2152}
Akash Sonia8246972023-01-03 10:37:08 +05302153
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05302154// RestoreOltFlowService to read from the DB and restore olt flow service config
2155func (va *VoltApplication) RestoreOltFlowService(cntx context.Context) {
2156 oltflowService, err := db.GetOltFlowService(cntx)
2157 if err != nil {
2158 logger.Warnw(ctx, "Failed to Get OltFlowServiceConfig from DB", log.Fields{"Error": err})
2159 return
2160 }
2161 err = json.Unmarshal([]byte(oltflowService), &va.OltFlowServiceConfig)
2162 if err != nil {
2163 logger.Warn(ctx, "Unmarshal of oltflowService failed")
2164 return
2165 }
2166 logger.Infow(ctx, "updated OltFlowServiceConfig from DB", log.Fields{"OltFlowServiceConfig": va.OltFlowServiceConfig})
2167}
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302168
Akash Soni87a19072023-02-28 00:46:59 +05302169func (va *VoltApplication) UpdateDeviceConfig(cntx context.Context, deviceConfig *DeviceConfig) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302170 logger.Infow(ctx, "Received UpdateDeviceConfig", log.Fields{"DeviceInfo": deviceConfig})
Akash Soni87a19072023-02-28 00:46:59 +05302171 var dc *DeviceConfig
2172 va.DevicesConfig.Store(deviceConfig.SerialNumber, deviceConfig)
2173 err := dc.WriteDeviceConfigToDb(cntx, deviceConfig.SerialNumber, deviceConfig)
2174 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302175 logger.Warnw(ctx, "DB update for device config failed", log.Fields{"err": err})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302176 }
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +05302177 logger.Debugw(ctx, "Added OLT configurations", log.Fields{"DeviceInfo": deviceConfig})
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302178 // If device is already discovered update the VoltDevice structure
Akash Soni87a19072023-02-28 00:46:59 +05302179 device, id := va.GetDeviceBySerialNo(deviceConfig.SerialNumber)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302180 if device != nil {
Akash Soni87a19072023-02-28 00:46:59 +05302181 device.NniDhcpTrapVid = of.VlanType(deviceConfig.NniDhcpTrapVid)
Tinoj Joseph50d722c2022-12-06 22:53:22 +05302182 va.DevicesDisc.Store(id, device)
2183 }
2184}