blob: d04e00e240c9329384eded0f2b8829c0638931d8 [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 controller
17
18import (
19 "context"
20 "errors"
21 "sync"
22 "time"
23
24 "encoding/hex"
25
26 "voltha-go-controller/database"
27 errorCodes "voltha-go-controller/internal/pkg/errorcodes"
28 "voltha-go-controller/internal/pkg/intf"
29 "voltha-go-controller/internal/pkg/of"
30 "voltha-go-controller/internal/pkg/tasks"
31 "voltha-go-controller/internal/pkg/util"
32 "voltha-go-controller/internal/pkg/vpagent"
33
Tinoj Joseph1d108322022-07-13 10:07:39 +053034 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053035)
36
37var logger log.CLogger
38var ctx = context.TODO()
39
40func init() {
41 // Setup this package so that it's log level can be modified at run time
42 var err error
Tinoj Joseph1d108322022-07-13 10:07:39 +053043 logger, err = log.AddPackageWithDefaultParam()
Naveen Sampath04696f72022-06-13 15:19:14 +053044 if err != nil {
45 panic(err)
46 }
47}
48
49var db database.DBIntf
50
Akash Soni6f369452023-09-19 11:18:28 +053051type VoltControllerInterface interface {
52 GetDevice(id string) (*Device, error)
53 GetAllPendingFlows() ([]*of.VoltSubFlow, error)
54 GetAllFlows() ([]*of.VoltSubFlow, error)
55 GetFlows(deviceID string) ([]*of.VoltSubFlow, error)
56 GetFlow(deviceID string, cookie uint64) (*of.VoltSubFlow, error)
57 GetGroups(cntx context.Context, id uint32) (*of.Group, error)
58 GetGroupList() ([]*of.Group, error)
59 GetMeterInfo(cntx context.Context, id uint32) (map[string]*of.Meter, error)
60 GetAllMeterInfo() (map[string][]*of.Meter, error)
61 GetTaskList(device string) []tasks.Task
62}
63
Naveen Sampath04696f72022-06-13 15:19:14 +053064// VoltController structure
65type VoltController struct {
Naveen Sampath04696f72022-06-13 15:19:14 +053066 ctx context.Context
67 app intf.App
Naveen Sampath04696f72022-06-13 15:19:14 +053068 BlockedDeviceList *util.ConcurrentMap
69 deviceTaskQueue *util.ConcurrentMap
vinokuma926cb3e2023-03-29 11:41:06 +053070 vagent map[string]*vpagent.VPAgent
bseenivaa8cb94c2024-12-16 13:37:17 +053071 Devices sync.Map
vinokuma926cb3e2023-03-29 11:41:06 +053072 rebootInProgressDevices map[string]string
73 deviceLock sync.RWMutex
74 rebootLock sync.Mutex
Sridhar Ravindra3ec14232024-01-01 19:11:48 +053075 deviceTableSyncDuration time.Duration // Time interval between each cycle of audit task
76 maxFlowRetryDuration time.Duration // Maximum duration for which flows will be retried upon failures
77 maxFlowRetryAttempts uint32 // maxFlowRetryAttempt = maxFlowRetryDuration / deviceTableSyncDuration
vinokuma926cb3e2023-03-29 11:41:06 +053078 RebootFlow bool
Naveen Sampath04696f72022-06-13 15:19:14 +053079}
80
81var vcontroller *VoltController
82
83// NewController is the constructor for VoltController
84func NewController(ctx context.Context, app intf.App) intf.IVPClientAgent {
85 var controller VoltController
86
87 controller.rebootInProgressDevices = make(map[string]string)
Naveen Sampath04696f72022-06-13 15:19:14 +053088 controller.deviceLock = sync.RWMutex{}
89 controller.ctx = ctx
90 controller.app = app
91 controller.BlockedDeviceList = util.NewConcurrentMap()
92 controller.deviceTaskQueue = util.NewConcurrentMap()
93 db = database.GetDatabase()
94 vcontroller = &controller
95 return &controller
96}
97
vinokuma926cb3e2023-03-29 11:41:06 +053098// SetDeviceTableSyncDuration - sets interval between device table sync up activity
99// duration - in minutes
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530100func (v *VoltController) SetDeviceTableSyncDuration(duration int) {
101 v.deviceTableSyncDuration = time.Duration(duration) * time.Second
102}
103
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530104// SetMaxFlowRetryDuration - sets max flow retry interval
105func (v *VoltController) SetMaxFlowRetryDuration(duration int) {
106 v.maxFlowRetryDuration = time.Duration(duration) * time.Second
107}
108
109// SetMaxFlowRetryAttempts - sets max flow retry attempts
110func (v *VoltController) SetMaxFlowRetryAttempts() {
111 v.maxFlowRetryAttempts = uint32((v.maxFlowRetryDuration / v.deviceTableSyncDuration))
112}
113
vinokuma926cb3e2023-03-29 11:41:06 +0530114// GetDeviceTableSyncDuration - returns configured device table sync duration
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530115func (v *VoltController) GetDeviceTableSyncDuration() time.Duration {
116 return v.deviceTableSyncDuration
117}
118
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530119// GetMaxFlowRetryAttempt - returns max flow retry attempst
120func (v *VoltController) GetMaxFlowRetryAttempt() uint32 {
121 return v.maxFlowRetryAttempts
122}
123
Naveen Sampath04696f72022-06-13 15:19:14 +0530124// AddDevice to add device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530125func (v *VoltController) AddDevice(cntx context.Context, config *intf.VPClientCfg) intf.IVPClient {
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530126 d := NewDevice(cntx, config.DeviceID, config.SerialNum, config.VolthaClient, config.SouthBoundID, config.MfrDesc, config.HwDesc, config.SwDesc)
bseenivaa8cb94c2024-12-16 13:37:17 +0530127 v.Devices.Store(config.DeviceID, d)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530128 v.app.AddDevice(cntx, d.ID, d.SerialNum, config.SouthBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530129
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530130 d.RestoreMetersFromDb(cntx)
131 d.RestoreGroupsFromDb(cntx)
132 d.RestoreFlowsFromDb(cntx)
133 d.RestorePortsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530134 d.ConnectInd(context.TODO(), intf.DeviceDisc)
135 d.packetOutChannel = config.PacketOutChannel
136
Akash Soni6168f312023-05-18 20:57:33 +0530137 logger.Debugw(ctx, "Added device", log.Fields{"Device": config.DeviceID, "SerialNo": d.SerialNum, "State": d.State})
Naveen Sampath04696f72022-06-13 15:19:14 +0530138
139 return d
140}
141
142// DelDevice to delete device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530143func (v *VoltController) DelDevice(cntx context.Context, id string) {
bseenivaa8cb94c2024-12-16 13:37:17 +0530144 var device *Device
145 d, ok := v.Devices.Load(id)
Naveen Sampath04696f72022-06-13 15:19:14 +0530146 if ok {
bseenivaa8cb94c2024-12-16 13:37:17 +0530147 v.Devices.Delete(id)
148 device, ok = d.(*Device)
149 if ok {
150 device.Delete()
151 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530152 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530153 v.app.DelDevice(cntx, id)
bseenivaa8cb94c2024-12-16 13:37:17 +0530154 device.cancel() // To stop the device tables sync routine
Akash Soni6168f312023-05-18 20:57:33 +0530155 logger.Debugw(ctx, "Deleted device", log.Fields{"Device": id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530156}
157
vinokuma926cb3e2023-03-29 11:41:06 +0530158// AddControllerTask - add task to controller queue
Naveen Sampath04696f72022-06-13 15:19:14 +0530159func (v *VoltController) AddControllerTask(device string, task tasks.Task) {
160 var taskQueueIntf interface{}
161 var taskQueue *tasks.Tasks
162 var found bool
163 if taskQueueIntf, found = v.deviceTaskQueue.Get(device); !found {
164 taskQueue = tasks.NewTasks(context.TODO())
165 v.deviceTaskQueue.Set(device, taskQueue)
166 } else {
167 taskQueue = taskQueueIntf.(*tasks.Tasks)
168 }
169 taskQueue.AddTask(task)
170 logger.Warnw(ctx, "Task Added to Controller Task List", log.Fields{"Len": taskQueue.NumPendingTasks(), "Total": taskQueue.TotalTasks()})
171}
172
vinokuma926cb3e2023-03-29 11:41:06 +0530173// AddNewDevice - called when new device is discovered. This will be
174// processed as part of controller queue
Naveen Sampath04696f72022-06-13 15:19:14 +0530175func (v *VoltController) AddNewDevice(config *intf.VPClientCfg) {
176 adt := NewAddDeviceTask(config)
177 v.AddControllerTask(config.DeviceID, adt)
178}
179
180// GetDevice to get device info
181func (v *VoltController) GetDevice(id string) (*Device, error) {
bseenivaa8cb94c2024-12-16 13:37:17 +0530182 var device *Device
183 d, ok := v.Devices.Load(id)
184 if !ok {
185 return nil, errorCodes.ErrDeviceNotFound
186 }
187 device, ok = d.(*Device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530188 if ok {
bseenivaa8cb94c2024-12-16 13:37:17 +0530189 return device, nil
Naveen Sampath04696f72022-06-13 15:19:14 +0530190 }
191 return nil, errorCodes.ErrDeviceNotFound
192}
193
194// IsRebootInProgressForDevice to check if reboot is in progress for the device
195func (v *VoltController) IsRebootInProgressForDevice(device string) bool {
196 v.rebootLock.Lock()
197 defer v.rebootLock.Unlock()
198 _, ok := v.rebootInProgressDevices[device]
199 return ok
200}
201
202// SetRebootInProgressForDevice to set reboot in progress for the device
203func (v *VoltController) SetRebootInProgressForDevice(device string) bool {
204 v.rebootLock.Lock()
205 defer v.rebootLock.Unlock()
206 _, ok := v.rebootInProgressDevices[device]
207 if ok {
208 return true
209 }
210 v.rebootInProgressDevices[device] = device
211 logger.Warnw(ctx, "Setted Reboot-In-Progress flag", log.Fields{"Device": device})
212
213 d, err := v.GetDevice(device)
214 if err == nil {
215 d.ResetCache()
216 } else {
217 logger.Errorw(ctx, "Failed to get device", log.Fields{"Device": device, "Error": err})
218 }
219
220 return true
221}
222
223// ReSetRebootInProgressForDevice to reset reboot in progress for the device
224func (v *VoltController) ReSetRebootInProgressForDevice(device string) bool {
225 v.rebootLock.Lock()
226 defer v.rebootLock.Unlock()
227 _, ok := v.rebootInProgressDevices[device]
228 if !ok {
229 return true
230 }
231 delete(v.rebootInProgressDevices, device)
232 logger.Warnw(ctx, "Resetted Reboot-In-Progress flag", log.Fields{"Device": device})
233 return true
234}
235
236// DeviceRebootInd is device reboot indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530237func (v *VoltController) DeviceRebootInd(cntx context.Context, dID string, srNo string, sbID string) {
238 v.app.DeviceRebootInd(cntx, dID, srNo, sbID)
239 _ = db.DelAllRoutesForDevice(cntx, dID)
240 _ = db.DelAllGroup(cntx, dID)
241 _ = db.DelAllMeter(cntx, dID)
242 _ = db.DelAllPONCounters(cntx, dID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530243}
244
245// DeviceDisableInd is device deactivation indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530246func (v *VoltController) DeviceDisableInd(cntx context.Context, dID string) {
247 v.app.DeviceDisableInd(cntx, dID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530248}
249
vinokuma926cb3e2023-03-29 11:41:06 +0530250// TriggerPendingProfileDeleteReq - trigger pending profile delete requests
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530251func (v *VoltController) TriggerPendingProfileDeleteReq(cntx context.Context, device string) {
252 v.app.TriggerPendingProfileDeleteReq(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530253}
254
vinokuma926cb3e2023-03-29 11:41:06 +0530255// TriggerPendingMigrateServicesReq - trigger pending services migration requests
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530256func (v *VoltController) TriggerPendingMigrateServicesReq(cntx context.Context, device string) {
257 v.app.TriggerPendingMigrateServicesReq(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530258}
259
260// SetAuditFlags to set the audit flags
261func (v *VoltController) SetAuditFlags(device *Device) {
262 v.app.SetRebootFlag(true)
263 device.auditInProgress = true
264}
265
266// ResetAuditFlags to reset the audit flags
267func (v *VoltController) ResetAuditFlags(device *Device) {
268 v.app.SetRebootFlag(false)
269 device.auditInProgress = false
270}
271
vinokuma926cb3e2023-03-29 11:41:06 +0530272// ProcessFlowModResultIndication - send flow mod result notification
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530273func (v *VoltController) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) {
274 v.app.ProcessFlowModResultIndication(cntx, flowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +0530275}
276
Akash Sonief452f12024-12-12 18:20:28 +0530277func (v *VoltController) CheckAndDeactivateService(ctx context.Context, flow *of.VoltSubFlow, devSerialNum string, devID string) {
278 v.app.CheckAndDeactivateService(ctx, flow, devSerialNum, devID)
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530279}
280
Naveen Sampath04696f72022-06-13 15:19:14 +0530281// AddVPAgent to add the vpagent
282func (v *VoltController) AddVPAgent(vep string, vpa *vpagent.VPAgent) {
283 v.vagent[vep] = vpa
284}
285
286// VPAgent to get vpagent info
287func (v *VoltController) VPAgent(vep string) (*vpagent.VPAgent, error) {
288 vpa, ok := v.vagent[vep]
289 if ok {
290 return vpa, nil
291 }
292 return nil, errors.New("VPA Not Registered")
293}
294
295// PacketOutReq for packet out request
296func (v *VoltController) PacketOutReq(device string, inport string, outport string, pkt []byte, isCustomPkt bool) error {
297 logger.Debugw(ctx, "Packet Out Req", log.Fields{"Device": device, "OutPort": outport})
298 d, err := v.GetDevice(device)
299 if err != nil {
300 return err
301 }
302 logger.Debugw(ctx, "Packet Out Pkt", log.Fields{"Pkt": hex.EncodeToString(pkt)})
303 return d.PacketOutReq(inport, outport, pkt, isCustomPkt)
304}
305
306// AddFlows to add flows
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530307func (v *VoltController) AddFlows(cntx context.Context, port string, device string, flow *of.VoltFlow) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530308 d, err := v.GetDevice(device)
309 if err != nil {
310 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
311 return err
312 }
313 devPort := d.GetPortByName(port)
314 if devPort == nil {
315 logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device})
316 return errorCodes.ErrPortNotFound
317 }
318 if d.ctx == nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530319 // FIXME: Application should know the context before it could submit task. Handle at application level
Naveen Sampath04696f72022-06-13 15:19:14 +0530320 logger.Errorw(ctx, "Context is missing. AddFlow Operation Not added to Task", log.Fields{"Device": device})
321 return errorCodes.ErrInvalidParamInRequest
322 }
323
324 var isMigrationRequired bool
325 if flow.MigrateCookie {
326 // flow migration to new cookie must be done only during the audit. Migration for all subflows must be done if
327 // atlease one subflow with old cookie found in the device.
328 for _, subFlow := range flow.SubFlows {
329 if isMigrationRequired = d.IsFlowPresentWithOldCookie(subFlow); isMigrationRequired {
330 break
331 }
332 }
333 }
334
335 if isMigrationRequired {
336 // In this case, the flow is updated in local cache and db here.
337 // Actual flow deletion and addition at voltha will happen during flow tables audit.
338 for _, subFlow := range flow.SubFlows {
339 logger.Debugw(ctx, "Cookie Migration Required", log.Fields{"OldCookie": subFlow.OldCookie, "NewCookie": subFlow.Cookie})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530340 if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530341 logger.Errorw(ctx, "Delete flow with old cookie failed", log.Fields{"Error": err, "OldCookie": subFlow.OldCookie})
342 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530343 if err := d.AddFlow(cntx, subFlow); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530344 logger.Errorw(ctx, "Flow Add Failed", log.Fields{"Error": err, "Cookie": subFlow.Cookie})
345 }
346 }
347 } else {
348 flow.Command = of.CommandAdd
349 d.UpdateFlows(flow, devPort)
350 for cookie := range flow.SubFlows {
351 logger.Debugw(ctx, "Flow Add added to queue", log.Fields{"Cookie": cookie, "Device": device, "Port": port})
352 }
353 }
354 return nil
355}
356
357// DelFlows to delete flows
Sridhar Ravindra03aa0bf2023-09-12 17:46:40 +0530358// delFlowsOnlyInDevice flag indicates that flows should be deleted only in DB/device and should not be forwarded to core
359func (v *VoltController) DelFlows(cntx context.Context, port string, device string, flow *of.VoltFlow, delFlowsOnlyInDevice bool) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530360 d, err := v.GetDevice(device)
361 if err != nil {
362 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
363 return err
364 }
365 devPort := d.GetPortByName(port)
366 if devPort == nil {
367 logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device})
368 return errorCodes.ErrPortNotFound
369 }
370 if d.ctx == nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530371 // FIXME: Application should know the context before it could submit task. Handle at application level
Naveen Sampath04696f72022-06-13 15:19:14 +0530372 logger.Errorw(ctx, "Context is missing. DelFlow Operation Not added to Task", log.Fields{"Device": device})
373 return errorCodes.ErrInvalidParamInRequest
374 }
375
376 var isMigrationRequired bool
377 if flow.MigrateCookie {
378 // flow migration to new cookie must be done only during the audit. Migration for all subflows must be done if
379 // atlease one subflow with old cookie found in the device.
380 for _, subFlow := range flow.SubFlows {
381 if isMigrationRequired = d.IsFlowPresentWithOldCookie(subFlow); isMigrationRequired {
382 break
383 }
384 }
385 }
386
387 if isMigrationRequired {
388 // In this case, the flow is deleted from local cache and db here.
389 // Actual flow deletion at voltha will happen during flow tables audit.
390 for _, subFlow := range flow.SubFlows {
391 logger.Debugw(ctx, "Old Cookie delete Required", log.Fields{"OldCookie": subFlow.OldCookie})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530392 if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530393 logger.Errorw(ctx, "DelFlowWithOldCookie failed", log.Fields{"OldCookie": subFlow.OldCookie, "Error": err})
394 }
395 }
396 } else {
Sridhar Ravindra03aa0bf2023-09-12 17:46:40 +0530397 // Delete flows only in DB/device when Port Delete has come. Do not send flows to core during Port Delete
398 if delFlowsOnlyInDevice {
399 for cookie, subFlow := range flow.SubFlows {
400 err := d.DelFlow(ctx, subFlow)
401 logger.Infow(ctx, "Flow Deleted from device/DB", log.Fields{"Cookie": cookie, "Device": device, "Port": port, "Error": err})
402 }
403 } else {
404 flow.Command = of.CommandDel
405 d.UpdateFlows(flow, devPort)
406 for cookie := range flow.SubFlows {
407 logger.Debugw(ctx, "Flow Del added to queue", log.Fields{"Cookie": cookie, "Device": device, "Port": port})
408 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530409 }
410 }
411 return nil
412}
413
414// GroupUpdate for group update
415func (v *VoltController) GroupUpdate(port string, device string, group *of.Group) error {
416 d, err := v.GetDevice(device)
417 if err != nil {
418 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
419 return err
420 }
421
422 devPort := d.GetPortByName(port)
423 if devPort == nil {
424 logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device})
425 return errorCodes.ErrPortNotFound
426 }
427
428 if d.ctx == nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530429 // FIXME: Application should know the context before it could submit task. Handle at application level
Naveen Sampath04696f72022-06-13 15:19:14 +0530430 logger.Errorw(ctx, "Context is missing. GroupMod Operation Not added to task", log.Fields{"Device": device})
431 return errorCodes.ErrInvalidParamInRequest
432 }
433
434 d.UpdateGroup(group, devPort)
435 return nil
436}
437
438// ModMeter to get mod meter info
439func (v *VoltController) ModMeter(port string, device string, command of.MeterCommand, meter *of.Meter) error {
440 d, err := v.GetDevice(device)
441 if err != nil {
442 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
443 return err
444 }
445
446 devPort := d.GetPortByName(port)
447 if devPort == nil {
448 logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device})
449 return errorCodes.ErrPortNotFound
450 }
451
452 d.ModMeter(command, meter, devPort)
453 return nil
454}
455
456// PortAddInd for port add indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530457func (v *VoltController) PortAddInd(cntx context.Context, device string, id uint32, name string) {
458 v.app.PortAddInd(cntx, device, id, name)
Naveen Sampath04696f72022-06-13 15:19:14 +0530459}
460
461// PortDelInd for port delete indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530462func (v *VoltController) PortDelInd(cntx context.Context, device string, port string) {
463 v.app.PortDelInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530464}
465
466// PortUpdateInd for port update indication
467func (v *VoltController) PortUpdateInd(device string, name string, id uint32) {
468 v.app.PortUpdateInd(device, name, id)
469}
470
471// PortUpInd for port up indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530472func (v *VoltController) PortUpInd(cntx context.Context, device string, port string) {
473 v.app.PortUpInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530474}
475
476// PortDownInd for port down indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530477func (v *VoltController) PortDownInd(cntx context.Context, device string, port string) {
478 v.app.PortDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530479}
480
481// DeviceUpInd for device up indication
482func (v *VoltController) DeviceUpInd(device string) {
483 v.app.DeviceUpInd(device)
484}
485
486// DeviceDownInd for device down indication
487func (v *VoltController) DeviceDownInd(device string) {
488 v.app.DeviceDownInd(device)
489}
490
491// PacketInInd for packet in indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530492func (v *VoltController) PacketInInd(cntx context.Context, device string, port string, data []byte) {
493 v.app.PacketInInd(cntx, device, port, data)
Naveen Sampath04696f72022-06-13 15:19:14 +0530494}
495
496// GetPortState to get port status
497func (v *VoltController) GetPortState(device string, name string) (PortState, error) {
498 d, err := v.GetDevice(device)
499 if err != nil {
500 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
501 return PortStateDown, err
502 }
503 return d.GetPortState(name)
504}
505
506// UpdateMvlanProfiles for update mvlan profiles
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530507func (v *VoltController) UpdateMvlanProfiles(cntx context.Context, device string) {
508 v.app.UpdateMvlanProfilesForDevice(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530509}
510
511// GetController to get controller
512func GetController() *VoltController {
513 return vcontroller
514}
515
516/*
517// PostIndication to post indication
518func (v *VoltController) PostIndication(device string, task interface{}) error {
519 var srvTask AddServiceIndTask
520 var portTask AddPortIndTask
521 var taskCommon tasks.Task
522 var isSvcTask bool
523
524 switch data := task.(type) {
525 case *AddServiceIndTask:
526 srvTask = *data
527 taskCommon = data
528 isSvcTask = true
529 case *AddPortIndTask:
530 portTask = *data
531 taskCommon = data
532 }
533
534 d, err := v.GetDevice(device)
535 if err != nil {
536 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
537 //It means device itself it not present so just post the indication directly
538 if isSvcTask {
539 msgbus.PostAccessConfigInd(srvTask.result, d.SerialNum, srvTask.indicationType, srvTask.serviceName, 0, srvTask.reason, srvTask.trigger, srvTask.portState)
540 } else {
541 msgbus.ProcessPortInd(portTask.indicationType, d.SerialNum, portTask.portName, portTask.accessConfig, portTask.serviceList)
542 }
543 return err
544 }
545 if taskCommon != nil {
546 d.AddTask(taskCommon)
547 }
548 return nil
549}
550*/
551
552// GetTaskList to get the task list
553func (v *VoltController) GetTaskList(device string) []tasks.Task {
554 d, err := v.GetDevice(device)
555 if err != nil || d.ctx == nil {
556 logger.Errorw(ctx, "Device Not Connected/Found", log.Fields{"Device": device, "Dev Obj": d})
557 return []tasks.Task{}
558 }
559 return d.GetTaskList()
Naveen Sampath04696f72022-06-13 15:19:14 +0530560}
561
Akash Soni6f369452023-09-19 11:18:28 +0530562// AddBlockedDevices to add Devices to blocked Devices list
Naveen Sampath04696f72022-06-13 15:19:14 +0530563func (v *VoltController) AddBlockedDevices(deviceSerialNumber string) {
564 v.BlockedDeviceList.Set(deviceSerialNumber, deviceSerialNumber)
565}
566
567// DelBlockedDevices to remove device from blocked device list
568func (v *VoltController) DelBlockedDevices(deviceSerialNumber string) {
569 v.BlockedDeviceList.Remove(deviceSerialNumber)
570}
571
572// IsBlockedDevice to check if device is blocked
573func (v *VoltController) IsBlockedDevice(deviceSerialNumber string) bool {
574 _, ifPresent := v.BlockedDeviceList.Get(deviceSerialNumber)
575 return ifPresent
576}
Tinoj Josephec742f62022-09-29 19:11:10 +0530577
578// GetFlows returns flow specific to device and flowID
579func (v *VoltController) GetFlow(deviceID string, cookie uint64) (*of.VoltSubFlow, error) {
580 d, err := v.GetDevice(deviceID)
581 if err != nil {
582 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": deviceID, "Error": err})
583 return nil, err
584 }
585 if flow, ok := d.GetFlow(cookie); ok {
586 return flow, nil
587 }
588 return nil, nil
589}
590
591// GetFlows returns list of flows for a particular device
592func (v *VoltController) GetFlows(deviceID string) ([]*of.VoltSubFlow, error) {
593 d, err := v.GetDevice(deviceID)
594 if err != nil {
595 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": deviceID, "Error": err})
Akash Sonia8246972023-01-03 10:37:08 +0530596 return nil, nil
Tinoj Josephec742f62022-09-29 19:11:10 +0530597 }
598 return d.GetAllFlows(), nil
599}
600
601// GetAllFlows returns list of all flows
602func (v *VoltController) GetAllFlows() ([]*of.VoltSubFlow, error) {
603 var flows []*of.VoltSubFlow
bseenivaa8cb94c2024-12-16 13:37:17 +0530604 v.Devices.Range(func(_, value interface{}) bool {
605 d, ok := value.(*Device)
606 if ok {
607 flows = append(flows, d.GetAllFlows()...)
608 }
609 return true
610 })
Tinoj Josephec742f62022-09-29 19:11:10 +0530611 return flows, nil
612}
613
614// GetAllPendingFlows returns list of all flows
615func (v *VoltController) GetAllPendingFlows() ([]*of.VoltSubFlow, error) {
616 var flows []*of.VoltSubFlow
bseenivaa8cb94c2024-12-16 13:37:17 +0530617 v.Devices.Range(func(_, value interface{}) bool {
618 d, ok := value.(*Device)
619 if ok {
620 flows = append(flows, d.GetAllPendingFlows()...)
621 }
622 return true
623 })
Tinoj Josephec742f62022-09-29 19:11:10 +0530624 return flows, nil
625}
Akash Sonib3abf522022-12-19 13:20:02 +0530626func (v *VoltController) GetAllMeterInfo() (map[string][]*of.Meter, error) {
627 logger.Info(ctx, "Entering into GetAllMeterInfo method")
628 meters := map[string][]*of.Meter{}
bseenivaa8cb94c2024-12-16 13:37:17 +0530629 v.Devices.Range(func(_, value interface{}) bool {
630 device, ok := value.(*Device)
631 if ok {
632 logger.Debugw(ctx, "Inside GetAllMeterInfo method", log.Fields{"deviceId": device.ID, "southbound": device.SouthBoundID, "serial no": device.SerialNum})
633 for _, meter := range device.meters {
634 meters[device.ID] = append(meters[device.ID], meter)
635 }
636 logger.Debugw(ctx, "Inside GetAllMeterInfo method", log.Fields{"meters": meters})
Akash Sonib3abf522022-12-19 13:20:02 +0530637 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530638 return true
639 })
Akash Sonib3abf522022-12-19 13:20:02 +0530640 return meters, nil
641}
642
643func (v *VoltController) GetMeterInfo(cntx context.Context, id uint32) (map[string]*of.Meter, error) {
644 logger.Info(ctx, "Entering into GetMeterInfo method")
645 meters := map[string]*of.Meter{}
bseenivaa8cb94c2024-12-16 13:37:17 +0530646 var errResult error
647 v.Devices.Range(func(_, value interface{}) bool {
648 device, ok := value.(*Device)
649 if ok {
650 logger.Debugw(ctx, "Inside GetMeterInfo method", log.Fields{"deviceId": device.ID})
651 meter, err := device.GetMeter(id)
652 if err != nil {
653 logger.Errorw(ctx, "Failed to fetch the meter", log.Fields{"Reason": err.Error()})
654 errResult = err
655 return false
656 }
657 meters[device.ID] = meter
658 logger.Debugw(ctx, "meters", log.Fields{"Meter": meters})
Akash Sonib3abf522022-12-19 13:20:02 +0530659 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530660 return true
661 })
662 if errResult != nil {
663 return nil, errResult
Akash Sonib3abf522022-12-19 13:20:02 +0530664 }
665 return meters, nil
666}
667
668func (v *VoltController) GetGroupList() ([]*of.Group, error) {
669 logger.Info(ctx, "Entering into GetGroupList method")
670 groups := []*of.Group{}
bseenivaa8cb94c2024-12-16 13:37:17 +0530671 v.Devices.Range(func(_, value interface{}) bool {
672 device, ok := value.(*Device)
673 if ok {
674 device.groups.Range(func(key, value interface{}) bool {
675 groupID := key.(uint32)
676 logger.Debugw(ctx, "Inside GetGroupList method", log.Fields{"groupID": groupID})
677 //Obtain all groups associated with the device
678 grps, ok := device.groups.Load(groupID)
679 if !ok {
680 return true
681 }
682 grp := grps.(*of.Group)
683 groups = append(groups, grp)
Akash Sonib3abf522022-12-19 13:20:02 +0530684 return true
bseenivaa8cb94c2024-12-16 13:37:17 +0530685 })
686 }
687 return true
688 })
Akash Sonib3abf522022-12-19 13:20:02 +0530689 logger.Debugw(ctx, "Groups", log.Fields{"groups": groups})
690 return groups, nil
691}
692
693func (v *VoltController) GetGroups(cntx context.Context, id uint32) (*of.Group, error) {
Akash Sonib3abf522022-12-19 13:20:02 +0530694 logger.Info(ctx, "Entering into GetGroupList method")
695 var groups *of.Group
bseenivaa8cb94c2024-12-16 13:37:17 +0530696 var err error
697 v.Devices.Range(func(_, value interface{}) bool {
698 device, ok := value.(*Device)
699 if ok {
700 logger.Debugw(ctx, "Inside GetGroupList method", log.Fields{"groupID": id})
701 grps, ok := device.groups.Load(id)
702 if !ok {
703 err = errors.New("group not found")
704 return false
705 }
706 groups = grps.(*of.Group)
707 logger.Debugw(ctx, "Groups", log.Fields{"groups": groups})
Akash Sonib3abf522022-12-19 13:20:02 +0530708 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530709 return true
710 })
711 if err != nil {
712 return nil, err
Akash Sonib3abf522022-12-19 13:20:02 +0530713 }
714 return groups, nil
715}