blob: 76e45581a9d7dd2b04af32f529614c2858cad8bb [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
Akash Soni230e6212023-10-16 10:46:07 +053071 Devices map[string]*Device
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)
Akash Soni230e6212023-10-16 10:46:07 +053088 controller.Devices = make(map[string]*Device)
Naveen Sampath04696f72022-06-13 15:19:14 +053089 controller.deviceLock = sync.RWMutex{}
90 controller.ctx = ctx
91 controller.app = app
92 controller.BlockedDeviceList = util.NewConcurrentMap()
93 controller.deviceTaskQueue = util.NewConcurrentMap()
94 db = database.GetDatabase()
95 vcontroller = &controller
96 return &controller
97}
98
vinokuma926cb3e2023-03-29 11:41:06 +053099// SetDeviceTableSyncDuration - sets interval between device table sync up activity
100// duration - in minutes
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530101func (v *VoltController) SetDeviceTableSyncDuration(duration int) {
102 v.deviceTableSyncDuration = time.Duration(duration) * time.Second
103}
104
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530105// SetMaxFlowRetryDuration - sets max flow retry interval
106func (v *VoltController) SetMaxFlowRetryDuration(duration int) {
107 v.maxFlowRetryDuration = time.Duration(duration) * time.Second
108}
109
110// SetMaxFlowRetryAttempts - sets max flow retry attempts
111func (v *VoltController) SetMaxFlowRetryAttempts() {
112 v.maxFlowRetryAttempts = uint32((v.maxFlowRetryDuration / v.deviceTableSyncDuration))
113}
114
vinokuma926cb3e2023-03-29 11:41:06 +0530115// GetDeviceTableSyncDuration - returns configured device table sync duration
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530116func (v *VoltController) GetDeviceTableSyncDuration() time.Duration {
117 return v.deviceTableSyncDuration
118}
119
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530120// GetMaxFlowRetryAttempt - returns max flow retry attempst
121func (v *VoltController) GetMaxFlowRetryAttempt() uint32 {
122 return v.maxFlowRetryAttempts
123}
124
Naveen Sampath04696f72022-06-13 15:19:14 +0530125// AddDevice to add device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530126func (v *VoltController) AddDevice(cntx context.Context, config *intf.VPClientCfg) intf.IVPClient {
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530127 d := NewDevice(cntx, config.DeviceID, config.SerialNum, config.VolthaClient, config.SouthBoundID, config.MfrDesc, config.HwDesc, config.SwDesc)
Akash Soni230e6212023-10-16 10:46:07 +0530128 v.Devices[config.DeviceID] = d
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530129 v.app.AddDevice(cntx, d.ID, d.SerialNum, config.SouthBoundID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530130
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530131 d.RestoreMetersFromDb(cntx)
132 d.RestoreGroupsFromDb(cntx)
133 d.RestoreFlowsFromDb(cntx)
134 d.RestorePortsFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530135 d.ConnectInd(context.TODO(), intf.DeviceDisc)
136 d.packetOutChannel = config.PacketOutChannel
137
Akash Soni6168f312023-05-18 20:57:33 +0530138 logger.Debugw(ctx, "Added device", log.Fields{"Device": config.DeviceID, "SerialNo": d.SerialNum, "State": d.State})
Naveen Sampath04696f72022-06-13 15:19:14 +0530139
140 return d
141}
142
143// DelDevice to delete device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530144func (v *VoltController) DelDevice(cntx context.Context, id string) {
Akash Soni230e6212023-10-16 10:46:07 +0530145 d, ok := v.Devices[id]
Naveen Sampath04696f72022-06-13 15:19:14 +0530146 if ok {
Akash Soni230e6212023-10-16 10:46:07 +0530147 delete(v.Devices, id)
Naveen Sampath04696f72022-06-13 15:19:14 +0530148 d.Delete()
149 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530150 v.app.DelDevice(cntx, id)
Naveen Sampath04696f72022-06-13 15:19:14 +0530151 d.cancel() // To stop the device tables sync routine
Akash Soni6168f312023-05-18 20:57:33 +0530152 logger.Debugw(ctx, "Deleted device", log.Fields{"Device": id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530153}
154
vinokuma926cb3e2023-03-29 11:41:06 +0530155// AddControllerTask - add task to controller queue
Naveen Sampath04696f72022-06-13 15:19:14 +0530156func (v *VoltController) AddControllerTask(device string, task tasks.Task) {
157 var taskQueueIntf interface{}
158 var taskQueue *tasks.Tasks
159 var found bool
160 if taskQueueIntf, found = v.deviceTaskQueue.Get(device); !found {
161 taskQueue = tasks.NewTasks(context.TODO())
162 v.deviceTaskQueue.Set(device, taskQueue)
163 } else {
164 taskQueue = taskQueueIntf.(*tasks.Tasks)
165 }
166 taskQueue.AddTask(task)
167 logger.Warnw(ctx, "Task Added to Controller Task List", log.Fields{"Len": taskQueue.NumPendingTasks(), "Total": taskQueue.TotalTasks()})
168}
169
vinokuma926cb3e2023-03-29 11:41:06 +0530170// AddNewDevice - called when new device is discovered. This will be
171// processed as part of controller queue
Naveen Sampath04696f72022-06-13 15:19:14 +0530172func (v *VoltController) AddNewDevice(config *intf.VPClientCfg) {
173 adt := NewAddDeviceTask(config)
174 v.AddControllerTask(config.DeviceID, adt)
175}
176
177// GetDevice to get device info
178func (v *VoltController) GetDevice(id string) (*Device, error) {
Akash Soni230e6212023-10-16 10:46:07 +0530179 d, ok := v.Devices[id]
Naveen Sampath04696f72022-06-13 15:19:14 +0530180 if ok {
181 return d, nil
182 }
183 return nil, errorCodes.ErrDeviceNotFound
184}
185
186// IsRebootInProgressForDevice to check if reboot is in progress for the device
187func (v *VoltController) IsRebootInProgressForDevice(device string) bool {
188 v.rebootLock.Lock()
189 defer v.rebootLock.Unlock()
190 _, ok := v.rebootInProgressDevices[device]
191 return ok
192}
193
194// SetRebootInProgressForDevice to set reboot in progress for the device
195func (v *VoltController) SetRebootInProgressForDevice(device string) bool {
196 v.rebootLock.Lock()
197 defer v.rebootLock.Unlock()
198 _, ok := v.rebootInProgressDevices[device]
199 if ok {
200 return true
201 }
202 v.rebootInProgressDevices[device] = device
203 logger.Warnw(ctx, "Setted Reboot-In-Progress flag", log.Fields{"Device": device})
204
205 d, err := v.GetDevice(device)
206 if err == nil {
207 d.ResetCache()
208 } else {
209 logger.Errorw(ctx, "Failed to get device", log.Fields{"Device": device, "Error": err})
210 }
211
212 return true
213}
214
215// ReSetRebootInProgressForDevice to reset reboot in progress for the device
216func (v *VoltController) ReSetRebootInProgressForDevice(device string) bool {
217 v.rebootLock.Lock()
218 defer v.rebootLock.Unlock()
219 _, ok := v.rebootInProgressDevices[device]
220 if !ok {
221 return true
222 }
223 delete(v.rebootInProgressDevices, device)
224 logger.Warnw(ctx, "Resetted Reboot-In-Progress flag", log.Fields{"Device": device})
225 return true
226}
227
228// DeviceRebootInd is device reboot indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530229func (v *VoltController) DeviceRebootInd(cntx context.Context, dID string, srNo string, sbID string) {
230 v.app.DeviceRebootInd(cntx, dID, srNo, sbID)
231 _ = db.DelAllRoutesForDevice(cntx, dID)
232 _ = db.DelAllGroup(cntx, dID)
233 _ = db.DelAllMeter(cntx, dID)
234 _ = db.DelAllPONCounters(cntx, dID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530235}
236
237// DeviceDisableInd is device deactivation indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530238func (v *VoltController) DeviceDisableInd(cntx context.Context, dID string) {
239 v.app.DeviceDisableInd(cntx, dID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530240}
241
vinokuma926cb3e2023-03-29 11:41:06 +0530242// TriggerPendingProfileDeleteReq - trigger pending profile delete requests
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530243func (v *VoltController) TriggerPendingProfileDeleteReq(cntx context.Context, device string) {
244 v.app.TriggerPendingProfileDeleteReq(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530245}
246
vinokuma926cb3e2023-03-29 11:41:06 +0530247// TriggerPendingMigrateServicesReq - trigger pending services migration requests
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530248func (v *VoltController) TriggerPendingMigrateServicesReq(cntx context.Context, device string) {
249 v.app.TriggerPendingMigrateServicesReq(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530250}
251
252// SetAuditFlags to set the audit flags
253func (v *VoltController) SetAuditFlags(device *Device) {
254 v.app.SetRebootFlag(true)
255 device.auditInProgress = true
256}
257
258// ResetAuditFlags to reset the audit flags
259func (v *VoltController) ResetAuditFlags(device *Device) {
260 v.app.SetRebootFlag(false)
261 device.auditInProgress = false
262}
263
vinokuma926cb3e2023-03-29 11:41:06 +0530264// ProcessFlowModResultIndication - send flow mod result notification
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530265func (v *VoltController) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) {
266 v.app.ProcessFlowModResultIndication(cntx, flowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +0530267}
268
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530269// IsFlowDelThresholdReached - check if the attempts for flow delete has reached threshold or not
270func (v *VoltController) IsFlowDelThresholdReached(cntx context.Context, cookie string, device string) bool {
271 return v.app.IsFlowDelThresholdReached(cntx, cookie, device)
272}
273
Naveen Sampath04696f72022-06-13 15:19:14 +0530274// AddVPAgent to add the vpagent
275func (v *VoltController) AddVPAgent(vep string, vpa *vpagent.VPAgent) {
276 v.vagent[vep] = vpa
277}
278
279// VPAgent to get vpagent info
280func (v *VoltController) VPAgent(vep string) (*vpagent.VPAgent, error) {
281 vpa, ok := v.vagent[vep]
282 if ok {
283 return vpa, nil
284 }
285 return nil, errors.New("VPA Not Registered")
286}
287
288// PacketOutReq for packet out request
289func (v *VoltController) PacketOutReq(device string, inport string, outport string, pkt []byte, isCustomPkt bool) error {
290 logger.Debugw(ctx, "Packet Out Req", log.Fields{"Device": device, "OutPort": outport})
291 d, err := v.GetDevice(device)
292 if err != nil {
293 return err
294 }
295 logger.Debugw(ctx, "Packet Out Pkt", log.Fields{"Pkt": hex.EncodeToString(pkt)})
296 return d.PacketOutReq(inport, outport, pkt, isCustomPkt)
297}
298
299// AddFlows to add flows
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530300func (v *VoltController) AddFlows(cntx context.Context, port string, device string, flow *of.VoltFlow) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530301 d, err := v.GetDevice(device)
302 if err != nil {
303 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
304 return err
305 }
306 devPort := d.GetPortByName(port)
307 if devPort == nil {
308 logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device})
309 return errorCodes.ErrPortNotFound
310 }
311 if d.ctx == nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530312 // FIXME: Application should know the context before it could submit task. Handle at application level
Naveen Sampath04696f72022-06-13 15:19:14 +0530313 logger.Errorw(ctx, "Context is missing. AddFlow Operation Not added to Task", log.Fields{"Device": device})
314 return errorCodes.ErrInvalidParamInRequest
315 }
316
317 var isMigrationRequired bool
318 if flow.MigrateCookie {
319 // flow migration to new cookie must be done only during the audit. Migration for all subflows must be done if
320 // atlease one subflow with old cookie found in the device.
321 for _, subFlow := range flow.SubFlows {
322 if isMigrationRequired = d.IsFlowPresentWithOldCookie(subFlow); isMigrationRequired {
323 break
324 }
325 }
326 }
327
328 if isMigrationRequired {
329 // In this case, the flow is updated in local cache and db here.
330 // Actual flow deletion and addition at voltha will happen during flow tables audit.
331 for _, subFlow := range flow.SubFlows {
332 logger.Debugw(ctx, "Cookie Migration Required", log.Fields{"OldCookie": subFlow.OldCookie, "NewCookie": subFlow.Cookie})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530333 if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530334 logger.Errorw(ctx, "Delete flow with old cookie failed", log.Fields{"Error": err, "OldCookie": subFlow.OldCookie})
335 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530336 if err := d.AddFlow(cntx, subFlow); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530337 logger.Errorw(ctx, "Flow Add Failed", log.Fields{"Error": err, "Cookie": subFlow.Cookie})
338 }
339 }
340 } else {
341 flow.Command = of.CommandAdd
342 d.UpdateFlows(flow, devPort)
343 for cookie := range flow.SubFlows {
344 logger.Debugw(ctx, "Flow Add added to queue", log.Fields{"Cookie": cookie, "Device": device, "Port": port})
345 }
346 }
347 return nil
348}
349
350// DelFlows to delete flows
Sridhar Ravindra03aa0bf2023-09-12 17:46:40 +0530351// delFlowsOnlyInDevice flag indicates that flows should be deleted only in DB/device and should not be forwarded to core
352func (v *VoltController) DelFlows(cntx context.Context, port string, device string, flow *of.VoltFlow, delFlowsOnlyInDevice bool) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530353 d, err := v.GetDevice(device)
354 if err != nil {
355 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
356 return err
357 }
358 devPort := d.GetPortByName(port)
359 if devPort == nil {
360 logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device})
361 return errorCodes.ErrPortNotFound
362 }
363 if d.ctx == nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530364 // FIXME: Application should know the context before it could submit task. Handle at application level
Naveen Sampath04696f72022-06-13 15:19:14 +0530365 logger.Errorw(ctx, "Context is missing. DelFlow Operation Not added to Task", log.Fields{"Device": device})
366 return errorCodes.ErrInvalidParamInRequest
367 }
368
369 var isMigrationRequired bool
370 if flow.MigrateCookie {
371 // flow migration to new cookie must be done only during the audit. Migration for all subflows must be done if
372 // atlease one subflow with old cookie found in the device.
373 for _, subFlow := range flow.SubFlows {
374 if isMigrationRequired = d.IsFlowPresentWithOldCookie(subFlow); isMigrationRequired {
375 break
376 }
377 }
378 }
379
380 if isMigrationRequired {
381 // In this case, the flow is deleted from local cache and db here.
382 // Actual flow deletion at voltha will happen during flow tables audit.
383 for _, subFlow := range flow.SubFlows {
384 logger.Debugw(ctx, "Old Cookie delete Required", log.Fields{"OldCookie": subFlow.OldCookie})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530385 if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530386 logger.Errorw(ctx, "DelFlowWithOldCookie failed", log.Fields{"OldCookie": subFlow.OldCookie, "Error": err})
387 }
388 }
389 } else {
Sridhar Ravindra03aa0bf2023-09-12 17:46:40 +0530390 // Delete flows only in DB/device when Port Delete has come. Do not send flows to core during Port Delete
391 if delFlowsOnlyInDevice {
392 for cookie, subFlow := range flow.SubFlows {
393 err := d.DelFlow(ctx, subFlow)
394 logger.Infow(ctx, "Flow Deleted from device/DB", log.Fields{"Cookie": cookie, "Device": device, "Port": port, "Error": err})
395 }
396 } else {
397 flow.Command = of.CommandDel
398 d.UpdateFlows(flow, devPort)
399 for cookie := range flow.SubFlows {
400 logger.Debugw(ctx, "Flow Del added to queue", log.Fields{"Cookie": cookie, "Device": device, "Port": port})
401 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530402 }
403 }
404 return nil
405}
406
407// GroupUpdate for group update
408func (v *VoltController) GroupUpdate(port string, device string, group *of.Group) error {
409 d, err := v.GetDevice(device)
410 if err != nil {
411 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
412 return err
413 }
414
415 devPort := d.GetPortByName(port)
416 if devPort == nil {
417 logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device})
418 return errorCodes.ErrPortNotFound
419 }
420
421 if d.ctx == nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530422 // FIXME: Application should know the context before it could submit task. Handle at application level
Naveen Sampath04696f72022-06-13 15:19:14 +0530423 logger.Errorw(ctx, "Context is missing. GroupMod Operation Not added to task", log.Fields{"Device": device})
424 return errorCodes.ErrInvalidParamInRequest
425 }
426
427 d.UpdateGroup(group, devPort)
428 return nil
429}
430
431// ModMeter to get mod meter info
432func (v *VoltController) ModMeter(port string, device string, command of.MeterCommand, meter *of.Meter) error {
433 d, err := v.GetDevice(device)
434 if err != nil {
435 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
436 return err
437 }
438
439 devPort := d.GetPortByName(port)
440 if devPort == nil {
441 logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device})
442 return errorCodes.ErrPortNotFound
443 }
444
445 d.ModMeter(command, meter, devPort)
446 return nil
447}
448
449// PortAddInd for port add indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530450func (v *VoltController) PortAddInd(cntx context.Context, device string, id uint32, name string) {
451 v.app.PortAddInd(cntx, device, id, name)
Naveen Sampath04696f72022-06-13 15:19:14 +0530452}
453
454// PortDelInd for port delete indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530455func (v *VoltController) PortDelInd(cntx context.Context, device string, port string) {
456 v.app.PortDelInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530457}
458
459// PortUpdateInd for port update indication
460func (v *VoltController) PortUpdateInd(device string, name string, id uint32) {
461 v.app.PortUpdateInd(device, name, id)
462}
463
464// PortUpInd for port up indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530465func (v *VoltController) PortUpInd(cntx context.Context, device string, port string) {
466 v.app.PortUpInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530467}
468
469// PortDownInd for port down indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530470func (v *VoltController) PortDownInd(cntx context.Context, device string, port string) {
471 v.app.PortDownInd(cntx, device, port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530472}
473
474// DeviceUpInd for device up indication
475func (v *VoltController) DeviceUpInd(device string) {
476 v.app.DeviceUpInd(device)
477}
478
479// DeviceDownInd for device down indication
480func (v *VoltController) DeviceDownInd(device string) {
481 v.app.DeviceDownInd(device)
482}
483
484// PacketInInd for packet in indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530485func (v *VoltController) PacketInInd(cntx context.Context, device string, port string, data []byte) {
486 v.app.PacketInInd(cntx, device, port, data)
Naveen Sampath04696f72022-06-13 15:19:14 +0530487}
488
489// GetPortState to get port status
490func (v *VoltController) GetPortState(device string, name string) (PortState, error) {
491 d, err := v.GetDevice(device)
492 if err != nil {
493 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
494 return PortStateDown, err
495 }
496 return d.GetPortState(name)
497}
498
499// UpdateMvlanProfiles for update mvlan profiles
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530500func (v *VoltController) UpdateMvlanProfiles(cntx context.Context, device string) {
501 v.app.UpdateMvlanProfilesForDevice(cntx, device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530502}
503
504// GetController to get controller
505func GetController() *VoltController {
506 return vcontroller
507}
508
509/*
510// PostIndication to post indication
511func (v *VoltController) PostIndication(device string, task interface{}) error {
512 var srvTask AddServiceIndTask
513 var portTask AddPortIndTask
514 var taskCommon tasks.Task
515 var isSvcTask bool
516
517 switch data := task.(type) {
518 case *AddServiceIndTask:
519 srvTask = *data
520 taskCommon = data
521 isSvcTask = true
522 case *AddPortIndTask:
523 portTask = *data
524 taskCommon = data
525 }
526
527 d, err := v.GetDevice(device)
528 if err != nil {
529 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
530 //It means device itself it not present so just post the indication directly
531 if isSvcTask {
532 msgbus.PostAccessConfigInd(srvTask.result, d.SerialNum, srvTask.indicationType, srvTask.serviceName, 0, srvTask.reason, srvTask.trigger, srvTask.portState)
533 } else {
534 msgbus.ProcessPortInd(portTask.indicationType, d.SerialNum, portTask.portName, portTask.accessConfig, portTask.serviceList)
535 }
536 return err
537 }
538 if taskCommon != nil {
539 d.AddTask(taskCommon)
540 }
541 return nil
542}
543*/
544
545// GetTaskList to get the task list
546func (v *VoltController) GetTaskList(device string) []tasks.Task {
547 d, err := v.GetDevice(device)
548 if err != nil || d.ctx == nil {
549 logger.Errorw(ctx, "Device Not Connected/Found", log.Fields{"Device": device, "Dev Obj": d})
550 return []tasks.Task{}
551 }
552 return d.GetTaskList()
Naveen Sampath04696f72022-06-13 15:19:14 +0530553}
554
Akash Soni6f369452023-09-19 11:18:28 +0530555// AddBlockedDevices to add Devices to blocked Devices list
Naveen Sampath04696f72022-06-13 15:19:14 +0530556func (v *VoltController) AddBlockedDevices(deviceSerialNumber string) {
557 v.BlockedDeviceList.Set(deviceSerialNumber, deviceSerialNumber)
558}
559
560// DelBlockedDevices to remove device from blocked device list
561func (v *VoltController) DelBlockedDevices(deviceSerialNumber string) {
562 v.BlockedDeviceList.Remove(deviceSerialNumber)
563}
564
565// IsBlockedDevice to check if device is blocked
566func (v *VoltController) IsBlockedDevice(deviceSerialNumber string) bool {
567 _, ifPresent := v.BlockedDeviceList.Get(deviceSerialNumber)
568 return ifPresent
569}
Tinoj Josephec742f62022-09-29 19:11:10 +0530570
571// GetFlows returns flow specific to device and flowID
572func (v *VoltController) GetFlow(deviceID string, cookie uint64) (*of.VoltSubFlow, error) {
573 d, err := v.GetDevice(deviceID)
574 if err != nil {
575 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": deviceID, "Error": err})
576 return nil, err
577 }
578 if flow, ok := d.GetFlow(cookie); ok {
579 return flow, nil
580 }
581 return nil, nil
582}
583
584// GetFlows returns list of flows for a particular device
585func (v *VoltController) GetFlows(deviceID string) ([]*of.VoltSubFlow, error) {
586 d, err := v.GetDevice(deviceID)
587 if err != nil {
588 logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": deviceID, "Error": err})
Akash Sonia8246972023-01-03 10:37:08 +0530589 return nil, nil
Tinoj Josephec742f62022-09-29 19:11:10 +0530590 }
591 return d.GetAllFlows(), nil
592}
593
594// GetAllFlows returns list of all flows
595func (v *VoltController) GetAllFlows() ([]*of.VoltSubFlow, error) {
596 var flows []*of.VoltSubFlow
Akash Soni230e6212023-10-16 10:46:07 +0530597 for _, d := range v.Devices {
Tinoj Josephec742f62022-09-29 19:11:10 +0530598 flows = append(flows, d.GetAllFlows()...)
599 }
600 return flows, nil
601}
602
603// GetAllPendingFlows returns list of all flows
604func (v *VoltController) GetAllPendingFlows() ([]*of.VoltSubFlow, error) {
605 var flows []*of.VoltSubFlow
Akash Soni230e6212023-10-16 10:46:07 +0530606 for _, d := range v.Devices {
Tinoj Josephec742f62022-09-29 19:11:10 +0530607 flows = append(flows, d.GetAllPendingFlows()...)
608 }
609 return flows, nil
610}
Akash Sonib3abf522022-12-19 13:20:02 +0530611func (v *VoltController) GetAllMeterInfo() (map[string][]*of.Meter, error) {
612 logger.Info(ctx, "Entering into GetAllMeterInfo method")
613 meters := map[string][]*of.Meter{}
Akash Soni230e6212023-10-16 10:46:07 +0530614 for _, device := range v.Devices {
Akash Soni6168f312023-05-18 20:57:33 +0530615 logger.Debugw(ctx, "Inside GetAllMeterInfo method", log.Fields{"deviceId": device.ID, "southbound": device.SouthBoundID, "serial no": device.SerialNum})
Akash Sonib3abf522022-12-19 13:20:02 +0530616 for _, meter := range device.meters {
617 meters[device.ID] = append(meters[device.ID], meter)
618 }
Akash Soni6168f312023-05-18 20:57:33 +0530619 logger.Debugw(ctx, "Inside GetAllMeterInfo method", log.Fields{"meters": meters})
Akash Sonib3abf522022-12-19 13:20:02 +0530620 }
621 return meters, nil
622}
623
624func (v *VoltController) GetMeterInfo(cntx context.Context, id uint32) (map[string]*of.Meter, error) {
625 logger.Info(ctx, "Entering into GetMeterInfo method")
626 meters := map[string]*of.Meter{}
Akash Soni230e6212023-10-16 10:46:07 +0530627 for _, device := range v.Devices {
Akash Soni6168f312023-05-18 20:57:33 +0530628 logger.Debugw(ctx, "Inside GetMeterInfo method", log.Fields{"deviceId": device.ID})
Akash Sonib3abf522022-12-19 13:20:02 +0530629 meter, err := device.GetMeter(id)
630 if err != nil {
631 logger.Errorw(ctx, "Failed to fetch the meter", log.Fields{"Reason": err.Error()})
632 return nil, err
633 }
634 meters[device.ID] = meter
Akash Soni6168f312023-05-18 20:57:33 +0530635 logger.Debugw(ctx, "meters", log.Fields{"Meter": meters})
Akash Sonib3abf522022-12-19 13:20:02 +0530636 }
637 return meters, nil
638}
639
640func (v *VoltController) GetGroupList() ([]*of.Group, error) {
641 logger.Info(ctx, "Entering into GetGroupList method")
642 groups := []*of.Group{}
Akash Soni230e6212023-10-16 10:46:07 +0530643 for _, device := range v.Devices {
Akash Sonib3abf522022-12-19 13:20:02 +0530644 device.groups.Range(func(key, value interface{}) bool {
645 groupID := key.(uint32)
Akash Soni6168f312023-05-18 20:57:33 +0530646 logger.Debugw(ctx, "Inside GetGroupList method", log.Fields{"groupID": groupID})
Akash Sonib3abf522022-12-19 13:20:02 +0530647 //Obtain all groups associated with the device
648 grps, ok := device.groups.Load(groupID)
649 if !ok {
650 return true
651 }
652 grp := grps.(*of.Group)
653 groups = append(groups, grp)
654 return true
655 })
656 }
657 logger.Debugw(ctx, "Groups", log.Fields{"groups": groups})
658 return groups, nil
659}
660
661func (v *VoltController) GetGroups(cntx context.Context, id uint32) (*of.Group, error) {
Akash Sonib3abf522022-12-19 13:20:02 +0530662 logger.Info(ctx, "Entering into GetGroupList method")
663 var groups *of.Group
Akash Soni230e6212023-10-16 10:46:07 +0530664 for _, device := range v.Devices {
Akash Soni6168f312023-05-18 20:57:33 +0530665 logger.Debugw(ctx, "Inside GetGroupList method", log.Fields{"groupID": id})
Akash Sonib3abf522022-12-19 13:20:02 +0530666 grps, ok := device.groups.Load(id)
667 if !ok {
Akash Sonid36d23b2023-08-18 12:51:40 +0530668 return nil, errors.New("group not found")
Akash Sonib3abf522022-12-19 13:20:02 +0530669 }
670 groups = grps.(*of.Group)
671 logger.Debugw(ctx, "Groups", log.Fields{"groups": groups})
672 }
673 return groups, nil
674}