Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | package controller |
| 17 | |
| 18 | import ( |
| 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 Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 34 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | var logger log.CLogger |
| 38 | var ctx = context.TODO() |
| 39 | |
| 40 | func init() { |
| 41 | // Setup this package so that it's log level can be modified at run time |
| 42 | var err error |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 43 | logger, err = log.AddPackageWithDefaultParam() |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 44 | if err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | var db database.DBIntf |
| 50 | |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 51 | type 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 64 | // VoltController structure |
| 65 | type VoltController struct { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 66 | ctx context.Context |
| 67 | app intf.App |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 68 | BlockedDeviceList *util.ConcurrentMap |
| 69 | deviceTaskQueue *util.ConcurrentMap |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 70 | vagent map[string]*vpagent.VPAgent |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 71 | Devices sync.Map |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 72 | rebootInProgressDevices map[string]string |
| 73 | deviceLock sync.RWMutex |
| 74 | rebootLock sync.Mutex |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 75 | 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 |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 78 | RebootFlow bool |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | var vcontroller *VoltController |
| 82 | |
| 83 | // NewController is the constructor for VoltController |
| 84 | func NewController(ctx context.Context, app intf.App) intf.IVPClientAgent { |
| 85 | var controller VoltController |
| 86 | |
| 87 | controller.rebootInProgressDevices = make(map[string]string) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 88 | 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 98 | // SetDeviceTableSyncDuration - sets interval between device table sync up activity |
| 99 | // duration - in minutes |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 100 | func (v *VoltController) SetDeviceTableSyncDuration(duration int) { |
| 101 | v.deviceTableSyncDuration = time.Duration(duration) * time.Second |
| 102 | } |
| 103 | |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 104 | // SetMaxFlowRetryDuration - sets max flow retry interval |
| 105 | func (v *VoltController) SetMaxFlowRetryDuration(duration int) { |
| 106 | v.maxFlowRetryDuration = time.Duration(duration) * time.Second |
| 107 | } |
| 108 | |
| 109 | // SetMaxFlowRetryAttempts - sets max flow retry attempts |
| 110 | func (v *VoltController) SetMaxFlowRetryAttempts() { |
| 111 | v.maxFlowRetryAttempts = uint32((v.maxFlowRetryDuration / v.deviceTableSyncDuration)) |
| 112 | } |
| 113 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 114 | // GetDeviceTableSyncDuration - returns configured device table sync duration |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 115 | func (v *VoltController) GetDeviceTableSyncDuration() time.Duration { |
| 116 | return v.deviceTableSyncDuration |
| 117 | } |
| 118 | |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 119 | // GetMaxFlowRetryAttempt - returns max flow retry attempst |
| 120 | func (v *VoltController) GetMaxFlowRetryAttempt() uint32 { |
| 121 | return v.maxFlowRetryAttempts |
| 122 | } |
| 123 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 124 | // AddDevice to add device |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 125 | func (v *VoltController) AddDevice(cntx context.Context, config *intf.VPClientCfg) intf.IVPClient { |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 126 | d := NewDevice(cntx, config.DeviceID, config.SerialNum, config.VolthaClient, config.SouthBoundID, config.MfrDesc, config.HwDesc, config.SwDesc) |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 127 | v.Devices.Store(config.DeviceID, d) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 128 | v.app.AddDevice(cntx, d.ID, d.SerialNum, config.SouthBoundID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 129 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 130 | d.RestoreMetersFromDb(cntx) |
| 131 | d.RestoreGroupsFromDb(cntx) |
| 132 | d.RestoreFlowsFromDb(cntx) |
| 133 | d.RestorePortsFromDb(cntx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 134 | d.ConnectInd(context.TODO(), intf.DeviceDisc) |
| 135 | d.packetOutChannel = config.PacketOutChannel |
| 136 | |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 137 | logger.Debugw(ctx, "Added device", log.Fields{"Device": config.DeviceID, "SerialNo": d.SerialNum, "State": d.State}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 138 | |
| 139 | return d |
| 140 | } |
| 141 | |
| 142 | // DelDevice to delete device |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 143 | func (v *VoltController) DelDevice(cntx context.Context, id string) { |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 144 | var device *Device |
| 145 | d, ok := v.Devices.Load(id) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 146 | if ok { |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 147 | v.Devices.Delete(id) |
| 148 | device, ok = d.(*Device) |
| 149 | if ok { |
| 150 | device.Delete() |
| 151 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 152 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 153 | v.app.DelDevice(cntx, id) |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 154 | device.cancel() // To stop the device tables sync routine |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 155 | logger.Debugw(ctx, "Deleted device", log.Fields{"Device": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 156 | } |
| 157 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 158 | // AddControllerTask - add task to controller queue |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 159 | func (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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 173 | // AddNewDevice - called when new device is discovered. This will be |
| 174 | // processed as part of controller queue |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 175 | func (v *VoltController) AddNewDevice(config *intf.VPClientCfg) { |
| 176 | adt := NewAddDeviceTask(config) |
| 177 | v.AddControllerTask(config.DeviceID, adt) |
| 178 | } |
| 179 | |
| 180 | // GetDevice to get device info |
| 181 | func (v *VoltController) GetDevice(id string) (*Device, error) { |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 182 | 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 188 | if ok { |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 189 | return device, nil |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 190 | } |
| 191 | return nil, errorCodes.ErrDeviceNotFound |
| 192 | } |
| 193 | |
| 194 | // IsRebootInProgressForDevice to check if reboot is in progress for the device |
| 195 | func (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 |
| 203 | func (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 |
| 224 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 237 | func (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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // DeviceDisableInd is device deactivation indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 246 | func (v *VoltController) DeviceDisableInd(cntx context.Context, dID string) { |
| 247 | v.app.DeviceDisableInd(cntx, dID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 248 | } |
| 249 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 250 | // TriggerPendingProfileDeleteReq - trigger pending profile delete requests |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 251 | func (v *VoltController) TriggerPendingProfileDeleteReq(cntx context.Context, device string) { |
| 252 | v.app.TriggerPendingProfileDeleteReq(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 253 | } |
| 254 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 255 | // TriggerPendingMigrateServicesReq - trigger pending services migration requests |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 256 | func (v *VoltController) TriggerPendingMigrateServicesReq(cntx context.Context, device string) { |
| 257 | v.app.TriggerPendingMigrateServicesReq(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // SetAuditFlags to set the audit flags |
| 261 | func (v *VoltController) SetAuditFlags(device *Device) { |
| 262 | v.app.SetRebootFlag(true) |
| 263 | device.auditInProgress = true |
| 264 | } |
| 265 | |
| 266 | // ResetAuditFlags to reset the audit flags |
| 267 | func (v *VoltController) ResetAuditFlags(device *Device) { |
| 268 | v.app.SetRebootFlag(false) |
| 269 | device.auditInProgress = false |
| 270 | } |
| 271 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 272 | // ProcessFlowModResultIndication - send flow mod result notification |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 273 | func (v *VoltController) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) { |
| 274 | v.app.ProcessFlowModResultIndication(cntx, flowStatus) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 275 | } |
| 276 | |
Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 277 | func (v *VoltController) CheckAndDeactivateService(ctx context.Context, flow *of.VoltSubFlow, devSerialNum string, devID string) { |
| 278 | v.app.CheckAndDeactivateService(ctx, flow, devSerialNum, devID) |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 279 | } |
| 280 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 281 | // AddVPAgent to add the vpagent |
| 282 | func (v *VoltController) AddVPAgent(vep string, vpa *vpagent.VPAgent) { |
| 283 | v.vagent[vep] = vpa |
| 284 | } |
| 285 | |
| 286 | // VPAgent to get vpagent info |
| 287 | func (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 |
| 296 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 307 | func (v *VoltController) AddFlows(cntx context.Context, port string, device string, flow *of.VoltFlow) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 308 | 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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 319 | // FIXME: Application should know the context before it could submit task. Handle at application level |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 320 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 340 | if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 341 | logger.Errorw(ctx, "Delete flow with old cookie failed", log.Fields{"Error": err, "OldCookie": subFlow.OldCookie}) |
| 342 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 343 | if err := d.AddFlow(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 344 | 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 Ravindra | 03aa0bf | 2023-09-12 17:46:40 +0530 | [diff] [blame] | 358 | // delFlowsOnlyInDevice flag indicates that flows should be deleted only in DB/device and should not be forwarded to core |
| 359 | func (v *VoltController) DelFlows(cntx context.Context, port string, device string, flow *of.VoltFlow, delFlowsOnlyInDevice bool) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 360 | 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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 371 | // FIXME: Application should know the context before it could submit task. Handle at application level |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 372 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 392 | if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 393 | logger.Errorw(ctx, "DelFlowWithOldCookie failed", log.Fields{"OldCookie": subFlow.OldCookie, "Error": err}) |
| 394 | } |
| 395 | } |
| 396 | } else { |
Sridhar Ravindra | 03aa0bf | 2023-09-12 17:46:40 +0530 | [diff] [blame] | 397 | // 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | return nil |
| 412 | } |
| 413 | |
| 414 | // GroupUpdate for group update |
| 415 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 429 | // FIXME: Application should know the context before it could submit task. Handle at application level |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 430 | 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 |
| 439 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 457 | func (v *VoltController) PortAddInd(cntx context.Context, device string, id uint32, name string) { |
| 458 | v.app.PortAddInd(cntx, device, id, name) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // PortDelInd for port delete indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 462 | func (v *VoltController) PortDelInd(cntx context.Context, device string, port string) { |
| 463 | v.app.PortDelInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // PortUpdateInd for port update indication |
| 467 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 472 | func (v *VoltController) PortUpInd(cntx context.Context, device string, port string) { |
| 473 | v.app.PortUpInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // PortDownInd for port down indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 477 | func (v *VoltController) PortDownInd(cntx context.Context, device string, port string) { |
| 478 | v.app.PortDownInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // DeviceUpInd for device up indication |
| 482 | func (v *VoltController) DeviceUpInd(device string) { |
| 483 | v.app.DeviceUpInd(device) |
| 484 | } |
| 485 | |
| 486 | // DeviceDownInd for device down indication |
| 487 | func (v *VoltController) DeviceDownInd(device string) { |
| 488 | v.app.DeviceDownInd(device) |
| 489 | } |
| 490 | |
| 491 | // PacketInInd for packet in indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 492 | func (v *VoltController) PacketInInd(cntx context.Context, device string, port string, data []byte) { |
| 493 | v.app.PacketInInd(cntx, device, port, data) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // GetPortState to get port status |
| 497 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 507 | func (v *VoltController) UpdateMvlanProfiles(cntx context.Context, device string) { |
| 508 | v.app.UpdateMvlanProfilesForDevice(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | // GetController to get controller |
| 512 | func GetController() *VoltController { |
| 513 | return vcontroller |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | // PostIndication to post indication |
| 518 | func (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 |
| 553 | func (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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 560 | } |
| 561 | |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 562 | // AddBlockedDevices to add Devices to blocked Devices list |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 563 | func (v *VoltController) AddBlockedDevices(deviceSerialNumber string) { |
| 564 | v.BlockedDeviceList.Set(deviceSerialNumber, deviceSerialNumber) |
| 565 | } |
| 566 | |
| 567 | // DelBlockedDevices to remove device from blocked device list |
| 568 | func (v *VoltController) DelBlockedDevices(deviceSerialNumber string) { |
| 569 | v.BlockedDeviceList.Remove(deviceSerialNumber) |
| 570 | } |
| 571 | |
| 572 | // IsBlockedDevice to check if device is blocked |
| 573 | func (v *VoltController) IsBlockedDevice(deviceSerialNumber string) bool { |
| 574 | _, ifPresent := v.BlockedDeviceList.Get(deviceSerialNumber) |
| 575 | return ifPresent |
| 576 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 577 | |
| 578 | // GetFlows returns flow specific to device and flowID |
| 579 | func (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 |
| 592 | func (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 Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 596 | return nil, nil |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 597 | } |
| 598 | return d.GetAllFlows(), nil |
| 599 | } |
| 600 | |
| 601 | // GetAllFlows returns list of all flows |
| 602 | func (v *VoltController) GetAllFlows() ([]*of.VoltSubFlow, error) { |
| 603 | var flows []*of.VoltSubFlow |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 604 | 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 Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 611 | return flows, nil |
| 612 | } |
| 613 | |
| 614 | // GetAllPendingFlows returns list of all flows |
| 615 | func (v *VoltController) GetAllPendingFlows() ([]*of.VoltSubFlow, error) { |
| 616 | var flows []*of.VoltSubFlow |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 617 | 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 Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 624 | return flows, nil |
| 625 | } |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 626 | func (v *VoltController) GetAllMeterInfo() (map[string][]*of.Meter, error) { |
| 627 | logger.Info(ctx, "Entering into GetAllMeterInfo method") |
| 628 | meters := map[string][]*of.Meter{} |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 629 | 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 Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 637 | } |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 638 | return true |
| 639 | }) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 640 | return meters, nil |
| 641 | } |
| 642 | |
| 643 | func (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{} |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 646 | 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 Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 659 | } |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 660 | return true |
| 661 | }) |
| 662 | if errResult != nil { |
| 663 | return nil, errResult |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 664 | } |
| 665 | return meters, nil |
| 666 | } |
| 667 | |
| 668 | func (v *VoltController) GetGroupList() ([]*of.Group, error) { |
| 669 | logger.Info(ctx, "Entering into GetGroupList method") |
| 670 | groups := []*of.Group{} |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 671 | 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 Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 684 | return true |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 685 | }) |
| 686 | } |
| 687 | return true |
| 688 | }) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 689 | logger.Debugw(ctx, "Groups", log.Fields{"groups": groups}) |
| 690 | return groups, nil |
| 691 | } |
| 692 | |
| 693 | func (v *VoltController) GetGroups(cntx context.Context, id uint32) (*of.Group, error) { |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 694 | logger.Info(ctx, "Entering into GetGroupList method") |
| 695 | var groups *of.Group |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 696 | 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 Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 708 | } |
bseeniva | a8cb94c | 2024-12-16 13:37:17 +0530 | [diff] [blame^] | 709 | return true |
| 710 | }) |
| 711 | if err != nil { |
| 712 | return nil, err |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 713 | } |
| 714 | return groups, nil |
| 715 | } |