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 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 51 | // VoltController structure |
| 52 | type VoltController struct { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 53 | ctx context.Context |
| 54 | app intf.App |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 55 | BlockedDeviceList *util.ConcurrentMap |
| 56 | deviceTaskQueue *util.ConcurrentMap |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 57 | vagent map[string]*vpagent.VPAgent |
| 58 | devices map[string]*Device |
| 59 | rebootInProgressDevices map[string]string |
| 60 | deviceLock sync.RWMutex |
| 61 | rebootLock sync.Mutex |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 62 | deviceTableSyncDuration time.Duration |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 63 | RebootFlow bool |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | var vcontroller *VoltController |
| 67 | |
| 68 | // NewController is the constructor for VoltController |
| 69 | func NewController(ctx context.Context, app intf.App) intf.IVPClientAgent { |
| 70 | var controller VoltController |
| 71 | |
| 72 | controller.rebootInProgressDevices = make(map[string]string) |
| 73 | controller.devices = make(map[string]*Device) |
| 74 | controller.deviceLock = sync.RWMutex{} |
| 75 | controller.ctx = ctx |
| 76 | controller.app = app |
| 77 | controller.BlockedDeviceList = util.NewConcurrentMap() |
| 78 | controller.deviceTaskQueue = util.NewConcurrentMap() |
| 79 | db = database.GetDatabase() |
| 80 | vcontroller = &controller |
| 81 | return &controller |
| 82 | } |
| 83 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 84 | // SetDeviceTableSyncDuration - sets interval between device table sync up activity |
| 85 | // duration - in minutes |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 86 | func (v *VoltController) SetDeviceTableSyncDuration(duration int) { |
| 87 | v.deviceTableSyncDuration = time.Duration(duration) * time.Second |
| 88 | } |
| 89 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 90 | // GetDeviceTableSyncDuration - returns configured device table sync duration |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 91 | func (v *VoltController) GetDeviceTableSyncDuration() time.Duration { |
| 92 | return v.deviceTableSyncDuration |
| 93 | } |
| 94 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 95 | // AddDevice to add device |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 96 | func (v *VoltController) AddDevice(cntx context.Context, config *intf.VPClientCfg) intf.IVPClient { |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 97 | d := NewDevice(cntx, config.DeviceID, config.SerialNum, config.VolthaClient, config.SouthBoundID, config.MfrDesc, config.HwDesc, config.SwDesc) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 98 | v.devices[config.DeviceID] = d |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 99 | v.app.AddDevice(cntx, d.ID, d.SerialNum, config.SouthBoundID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 100 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 101 | d.RestoreMetersFromDb(cntx) |
| 102 | d.RestoreGroupsFromDb(cntx) |
| 103 | d.RestoreFlowsFromDb(cntx) |
| 104 | d.RestorePortsFromDb(cntx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 105 | d.ConnectInd(context.TODO(), intf.DeviceDisc) |
| 106 | d.packetOutChannel = config.PacketOutChannel |
| 107 | |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 108 | 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] | 109 | |
| 110 | return d |
| 111 | } |
| 112 | |
| 113 | // DelDevice to delete device |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 114 | func (v *VoltController) DelDevice(cntx context.Context, id string) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 115 | d, ok := v.devices[id] |
| 116 | if ok { |
| 117 | delete(v.devices, id) |
| 118 | d.Delete() |
| 119 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 120 | v.app.DelDevice(cntx, id) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 121 | d.cancel() // To stop the device tables sync routine |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 122 | logger.Debugw(ctx, "Deleted device", log.Fields{"Device": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 123 | } |
| 124 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 125 | // AddControllerTask - add task to controller queue |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 126 | func (v *VoltController) AddControllerTask(device string, task tasks.Task) { |
| 127 | var taskQueueIntf interface{} |
| 128 | var taskQueue *tasks.Tasks |
| 129 | var found bool |
| 130 | if taskQueueIntf, found = v.deviceTaskQueue.Get(device); !found { |
| 131 | taskQueue = tasks.NewTasks(context.TODO()) |
| 132 | v.deviceTaskQueue.Set(device, taskQueue) |
| 133 | } else { |
| 134 | taskQueue = taskQueueIntf.(*tasks.Tasks) |
| 135 | } |
| 136 | taskQueue.AddTask(task) |
| 137 | logger.Warnw(ctx, "Task Added to Controller Task List", log.Fields{"Len": taskQueue.NumPendingTasks(), "Total": taskQueue.TotalTasks()}) |
| 138 | } |
| 139 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 140 | // AddNewDevice - called when new device is discovered. This will be |
| 141 | // processed as part of controller queue |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 142 | func (v *VoltController) AddNewDevice(config *intf.VPClientCfg) { |
| 143 | adt := NewAddDeviceTask(config) |
| 144 | v.AddControllerTask(config.DeviceID, adt) |
| 145 | } |
| 146 | |
| 147 | // GetDevice to get device info |
| 148 | func (v *VoltController) GetDevice(id string) (*Device, error) { |
| 149 | d, ok := v.devices[id] |
| 150 | if ok { |
| 151 | return d, nil |
| 152 | } |
| 153 | return nil, errorCodes.ErrDeviceNotFound |
| 154 | } |
| 155 | |
| 156 | // IsRebootInProgressForDevice to check if reboot is in progress for the device |
| 157 | func (v *VoltController) IsRebootInProgressForDevice(device string) bool { |
| 158 | v.rebootLock.Lock() |
| 159 | defer v.rebootLock.Unlock() |
| 160 | _, ok := v.rebootInProgressDevices[device] |
| 161 | return ok |
| 162 | } |
| 163 | |
| 164 | // SetRebootInProgressForDevice to set reboot in progress for the device |
| 165 | func (v *VoltController) SetRebootInProgressForDevice(device string) bool { |
| 166 | v.rebootLock.Lock() |
| 167 | defer v.rebootLock.Unlock() |
| 168 | _, ok := v.rebootInProgressDevices[device] |
| 169 | if ok { |
| 170 | return true |
| 171 | } |
| 172 | v.rebootInProgressDevices[device] = device |
| 173 | logger.Warnw(ctx, "Setted Reboot-In-Progress flag", log.Fields{"Device": device}) |
| 174 | |
| 175 | d, err := v.GetDevice(device) |
| 176 | if err == nil { |
| 177 | d.ResetCache() |
| 178 | } else { |
| 179 | logger.Errorw(ctx, "Failed to get device", log.Fields{"Device": device, "Error": err}) |
| 180 | } |
| 181 | |
| 182 | return true |
| 183 | } |
| 184 | |
| 185 | // ReSetRebootInProgressForDevice to reset reboot in progress for the device |
| 186 | func (v *VoltController) ReSetRebootInProgressForDevice(device string) bool { |
| 187 | v.rebootLock.Lock() |
| 188 | defer v.rebootLock.Unlock() |
| 189 | _, ok := v.rebootInProgressDevices[device] |
| 190 | if !ok { |
| 191 | return true |
| 192 | } |
| 193 | delete(v.rebootInProgressDevices, device) |
| 194 | logger.Warnw(ctx, "Resetted Reboot-In-Progress flag", log.Fields{"Device": device}) |
| 195 | return true |
| 196 | } |
| 197 | |
| 198 | // DeviceRebootInd is device reboot indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 199 | func (v *VoltController) DeviceRebootInd(cntx context.Context, dID string, srNo string, sbID string) { |
| 200 | v.app.DeviceRebootInd(cntx, dID, srNo, sbID) |
| 201 | _ = db.DelAllRoutesForDevice(cntx, dID) |
| 202 | _ = db.DelAllGroup(cntx, dID) |
| 203 | _ = db.DelAllMeter(cntx, dID) |
| 204 | _ = db.DelAllPONCounters(cntx, dID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // DeviceDisableInd is device deactivation indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 208 | func (v *VoltController) DeviceDisableInd(cntx context.Context, dID string) { |
| 209 | v.app.DeviceDisableInd(cntx, dID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 210 | } |
| 211 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 212 | // TriggerPendingProfileDeleteReq - trigger pending profile delete requests |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 213 | func (v *VoltController) TriggerPendingProfileDeleteReq(cntx context.Context, device string) { |
| 214 | v.app.TriggerPendingProfileDeleteReq(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 215 | } |
| 216 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 217 | // TriggerPendingMigrateServicesReq - trigger pending services migration requests |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 218 | func (v *VoltController) TriggerPendingMigrateServicesReq(cntx context.Context, device string) { |
| 219 | v.app.TriggerPendingMigrateServicesReq(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // SetAuditFlags to set the audit flags |
| 223 | func (v *VoltController) SetAuditFlags(device *Device) { |
| 224 | v.app.SetRebootFlag(true) |
| 225 | device.auditInProgress = true |
| 226 | } |
| 227 | |
| 228 | // ResetAuditFlags to reset the audit flags |
| 229 | func (v *VoltController) ResetAuditFlags(device *Device) { |
| 230 | v.app.SetRebootFlag(false) |
| 231 | device.auditInProgress = false |
| 232 | } |
| 233 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 234 | // ProcessFlowModResultIndication - send flow mod result notification |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 235 | func (v *VoltController) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) { |
| 236 | v.app.ProcessFlowModResultIndication(cntx, flowStatus) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // AddVPAgent to add the vpagent |
| 240 | func (v *VoltController) AddVPAgent(vep string, vpa *vpagent.VPAgent) { |
| 241 | v.vagent[vep] = vpa |
| 242 | } |
| 243 | |
| 244 | // VPAgent to get vpagent info |
| 245 | func (v *VoltController) VPAgent(vep string) (*vpagent.VPAgent, error) { |
| 246 | vpa, ok := v.vagent[vep] |
| 247 | if ok { |
| 248 | return vpa, nil |
| 249 | } |
| 250 | return nil, errors.New("VPA Not Registered") |
| 251 | } |
| 252 | |
| 253 | // PacketOutReq for packet out request |
| 254 | func (v *VoltController) PacketOutReq(device string, inport string, outport string, pkt []byte, isCustomPkt bool) error { |
| 255 | logger.Debugw(ctx, "Packet Out Req", log.Fields{"Device": device, "OutPort": outport}) |
| 256 | d, err := v.GetDevice(device) |
| 257 | if err != nil { |
| 258 | return err |
| 259 | } |
| 260 | logger.Debugw(ctx, "Packet Out Pkt", log.Fields{"Pkt": hex.EncodeToString(pkt)}) |
| 261 | return d.PacketOutReq(inport, outport, pkt, isCustomPkt) |
| 262 | } |
| 263 | |
| 264 | // AddFlows to add flows |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 265 | 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] | 266 | d, err := v.GetDevice(device) |
| 267 | if err != nil { |
| 268 | logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device}) |
| 269 | return err |
| 270 | } |
| 271 | devPort := d.GetPortByName(port) |
| 272 | if devPort == nil { |
| 273 | logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device}) |
| 274 | return errorCodes.ErrPortNotFound |
| 275 | } |
| 276 | if d.ctx == nil { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 277 | // 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] | 278 | logger.Errorw(ctx, "Context is missing. AddFlow Operation Not added to Task", log.Fields{"Device": device}) |
| 279 | return errorCodes.ErrInvalidParamInRequest |
| 280 | } |
| 281 | |
| 282 | var isMigrationRequired bool |
| 283 | if flow.MigrateCookie { |
| 284 | // flow migration to new cookie must be done only during the audit. Migration for all subflows must be done if |
| 285 | // atlease one subflow with old cookie found in the device. |
| 286 | for _, subFlow := range flow.SubFlows { |
| 287 | if isMigrationRequired = d.IsFlowPresentWithOldCookie(subFlow); isMigrationRequired { |
| 288 | break |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if isMigrationRequired { |
| 294 | // In this case, the flow is updated in local cache and db here. |
| 295 | // Actual flow deletion and addition at voltha will happen during flow tables audit. |
| 296 | for _, subFlow := range flow.SubFlows { |
| 297 | 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] | 298 | if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 299 | logger.Errorw(ctx, "Delete flow with old cookie failed", log.Fields{"Error": err, "OldCookie": subFlow.OldCookie}) |
| 300 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 301 | if err := d.AddFlow(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 302 | logger.Errorw(ctx, "Flow Add Failed", log.Fields{"Error": err, "Cookie": subFlow.Cookie}) |
| 303 | } |
| 304 | } |
| 305 | } else { |
| 306 | flow.Command = of.CommandAdd |
| 307 | d.UpdateFlows(flow, devPort) |
| 308 | for cookie := range flow.SubFlows { |
| 309 | logger.Debugw(ctx, "Flow Add added to queue", log.Fields{"Cookie": cookie, "Device": device, "Port": port}) |
| 310 | } |
| 311 | } |
| 312 | return nil |
| 313 | } |
| 314 | |
| 315 | // DelFlows to delete flows |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 316 | func (v *VoltController) DelFlows(cntx context.Context, port string, device string, flow *of.VoltFlow) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 317 | d, err := v.GetDevice(device) |
| 318 | if err != nil { |
| 319 | logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device}) |
| 320 | return err |
| 321 | } |
| 322 | devPort := d.GetPortByName(port) |
| 323 | if devPort == nil { |
| 324 | logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device}) |
| 325 | return errorCodes.ErrPortNotFound |
| 326 | } |
| 327 | if d.ctx == nil { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 328 | // 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] | 329 | logger.Errorw(ctx, "Context is missing. DelFlow Operation Not added to Task", log.Fields{"Device": device}) |
| 330 | return errorCodes.ErrInvalidParamInRequest |
| 331 | } |
| 332 | |
| 333 | var isMigrationRequired bool |
| 334 | if flow.MigrateCookie { |
| 335 | // flow migration to new cookie must be done only during the audit. Migration for all subflows must be done if |
| 336 | // atlease one subflow with old cookie found in the device. |
| 337 | for _, subFlow := range flow.SubFlows { |
| 338 | if isMigrationRequired = d.IsFlowPresentWithOldCookie(subFlow); isMigrationRequired { |
| 339 | break |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if isMigrationRequired { |
| 345 | // In this case, the flow is deleted from local cache and db here. |
| 346 | // Actual flow deletion at voltha will happen during flow tables audit. |
| 347 | for _, subFlow := range flow.SubFlows { |
| 348 | logger.Debugw(ctx, "Old Cookie delete Required", log.Fields{"OldCookie": subFlow.OldCookie}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 349 | if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 350 | logger.Errorw(ctx, "DelFlowWithOldCookie failed", log.Fields{"OldCookie": subFlow.OldCookie, "Error": err}) |
| 351 | } |
| 352 | } |
| 353 | } else { |
| 354 | flow.Command = of.CommandDel |
| 355 | d.UpdateFlows(flow, devPort) |
| 356 | for cookie := range flow.SubFlows { |
| 357 | logger.Debugw(ctx, "Flow Del added to queue", log.Fields{"Cookie": cookie, "Device": device, "Port": port}) |
| 358 | } |
| 359 | } |
| 360 | return nil |
| 361 | } |
| 362 | |
| 363 | // GroupUpdate for group update |
| 364 | func (v *VoltController) GroupUpdate(port string, device string, group *of.Group) error { |
| 365 | d, err := v.GetDevice(device) |
| 366 | if err != nil { |
| 367 | logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device}) |
| 368 | return err |
| 369 | } |
| 370 | |
| 371 | devPort := d.GetPortByName(port) |
| 372 | if devPort == nil { |
| 373 | logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device}) |
| 374 | return errorCodes.ErrPortNotFound |
| 375 | } |
| 376 | |
| 377 | if d.ctx == nil { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 378 | // 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] | 379 | logger.Errorw(ctx, "Context is missing. GroupMod Operation Not added to task", log.Fields{"Device": device}) |
| 380 | return errorCodes.ErrInvalidParamInRequest |
| 381 | } |
| 382 | |
| 383 | d.UpdateGroup(group, devPort) |
| 384 | return nil |
| 385 | } |
| 386 | |
| 387 | // ModMeter to get mod meter info |
| 388 | func (v *VoltController) ModMeter(port string, device string, command of.MeterCommand, meter *of.Meter) error { |
| 389 | d, err := v.GetDevice(device) |
| 390 | if err != nil { |
| 391 | logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device}) |
| 392 | return err |
| 393 | } |
| 394 | |
| 395 | devPort := d.GetPortByName(port) |
| 396 | if devPort == nil { |
| 397 | logger.Errorw(ctx, "Port Not Found", log.Fields{"Device": device}) |
| 398 | return errorCodes.ErrPortNotFound |
| 399 | } |
| 400 | |
| 401 | d.ModMeter(command, meter, devPort) |
| 402 | return nil |
| 403 | } |
| 404 | |
| 405 | // PortAddInd for port add indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 406 | func (v *VoltController) PortAddInd(cntx context.Context, device string, id uint32, name string) { |
| 407 | v.app.PortAddInd(cntx, device, id, name) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // PortDelInd for port delete indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 411 | func (v *VoltController) PortDelInd(cntx context.Context, device string, port string) { |
| 412 | v.app.PortDelInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // PortUpdateInd for port update indication |
| 416 | func (v *VoltController) PortUpdateInd(device string, name string, id uint32) { |
| 417 | v.app.PortUpdateInd(device, name, id) |
| 418 | } |
| 419 | |
| 420 | // PortUpInd for port up indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 421 | func (v *VoltController) PortUpInd(cntx context.Context, device string, port string) { |
| 422 | v.app.PortUpInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | // PortDownInd for port down indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 426 | func (v *VoltController) PortDownInd(cntx context.Context, device string, port string) { |
| 427 | v.app.PortDownInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | // DeviceUpInd for device up indication |
| 431 | func (v *VoltController) DeviceUpInd(device string) { |
| 432 | v.app.DeviceUpInd(device) |
| 433 | } |
| 434 | |
| 435 | // DeviceDownInd for device down indication |
| 436 | func (v *VoltController) DeviceDownInd(device string) { |
| 437 | v.app.DeviceDownInd(device) |
| 438 | } |
| 439 | |
| 440 | // PacketInInd for packet in indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 441 | func (v *VoltController) PacketInInd(cntx context.Context, device string, port string, data []byte) { |
| 442 | v.app.PacketInInd(cntx, device, port, data) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // GetPortState to get port status |
| 446 | func (v *VoltController) GetPortState(device string, name string) (PortState, error) { |
| 447 | d, err := v.GetDevice(device) |
| 448 | if err != nil { |
| 449 | logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device}) |
| 450 | return PortStateDown, err |
| 451 | } |
| 452 | return d.GetPortState(name) |
| 453 | } |
| 454 | |
| 455 | // UpdateMvlanProfiles for update mvlan profiles |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 456 | func (v *VoltController) UpdateMvlanProfiles(cntx context.Context, device string) { |
| 457 | v.app.UpdateMvlanProfilesForDevice(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // GetController to get controller |
| 461 | func GetController() *VoltController { |
| 462 | return vcontroller |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | // PostIndication to post indication |
| 467 | func (v *VoltController) PostIndication(device string, task interface{}) error { |
| 468 | var srvTask AddServiceIndTask |
| 469 | var portTask AddPortIndTask |
| 470 | var taskCommon tasks.Task |
| 471 | var isSvcTask bool |
| 472 | |
| 473 | switch data := task.(type) { |
| 474 | case *AddServiceIndTask: |
| 475 | srvTask = *data |
| 476 | taskCommon = data |
| 477 | isSvcTask = true |
| 478 | case *AddPortIndTask: |
| 479 | portTask = *data |
| 480 | taskCommon = data |
| 481 | } |
| 482 | |
| 483 | d, err := v.GetDevice(device) |
| 484 | if err != nil { |
| 485 | logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device}) |
| 486 | //It means device itself it not present so just post the indication directly |
| 487 | if isSvcTask { |
| 488 | msgbus.PostAccessConfigInd(srvTask.result, d.SerialNum, srvTask.indicationType, srvTask.serviceName, 0, srvTask.reason, srvTask.trigger, srvTask.portState) |
| 489 | } else { |
| 490 | msgbus.ProcessPortInd(portTask.indicationType, d.SerialNum, portTask.portName, portTask.accessConfig, portTask.serviceList) |
| 491 | } |
| 492 | return err |
| 493 | } |
| 494 | if taskCommon != nil { |
| 495 | d.AddTask(taskCommon) |
| 496 | } |
| 497 | return nil |
| 498 | } |
| 499 | */ |
| 500 | |
| 501 | // GetTaskList to get the task list |
| 502 | func (v *VoltController) GetTaskList(device string) []tasks.Task { |
| 503 | d, err := v.GetDevice(device) |
| 504 | if err != nil || d.ctx == nil { |
| 505 | logger.Errorw(ctx, "Device Not Connected/Found", log.Fields{"Device": device, "Dev Obj": d}) |
| 506 | return []tasks.Task{} |
| 507 | } |
| 508 | return d.GetTaskList() |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | // AddBlockedDevices to add devices to blocked devices list |
| 512 | func (v *VoltController) AddBlockedDevices(deviceSerialNumber string) { |
| 513 | v.BlockedDeviceList.Set(deviceSerialNumber, deviceSerialNumber) |
| 514 | } |
| 515 | |
| 516 | // DelBlockedDevices to remove device from blocked device list |
| 517 | func (v *VoltController) DelBlockedDevices(deviceSerialNumber string) { |
| 518 | v.BlockedDeviceList.Remove(deviceSerialNumber) |
| 519 | } |
| 520 | |
| 521 | // IsBlockedDevice to check if device is blocked |
| 522 | func (v *VoltController) IsBlockedDevice(deviceSerialNumber string) bool { |
| 523 | _, ifPresent := v.BlockedDeviceList.Get(deviceSerialNumber) |
| 524 | return ifPresent |
| 525 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 526 | |
| 527 | // GetFlows returns flow specific to device and flowID |
| 528 | func (v *VoltController) GetFlow(deviceID string, cookie uint64) (*of.VoltSubFlow, error) { |
| 529 | d, err := v.GetDevice(deviceID) |
| 530 | if err != nil { |
| 531 | logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": deviceID, "Error": err}) |
| 532 | return nil, err |
| 533 | } |
| 534 | if flow, ok := d.GetFlow(cookie); ok { |
| 535 | return flow, nil |
| 536 | } |
| 537 | return nil, nil |
| 538 | } |
| 539 | |
| 540 | // GetFlows returns list of flows for a particular device |
| 541 | func (v *VoltController) GetFlows(deviceID string) ([]*of.VoltSubFlow, error) { |
| 542 | d, err := v.GetDevice(deviceID) |
| 543 | if err != nil { |
| 544 | logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": deviceID, "Error": err}) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 545 | return nil, nil |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 546 | } |
| 547 | return d.GetAllFlows(), nil |
| 548 | } |
| 549 | |
| 550 | // GetAllFlows returns list of all flows |
| 551 | func (v *VoltController) GetAllFlows() ([]*of.VoltSubFlow, error) { |
| 552 | var flows []*of.VoltSubFlow |
| 553 | for _, d := range v.devices { |
| 554 | flows = append(flows, d.GetAllFlows()...) |
| 555 | } |
| 556 | return flows, nil |
| 557 | } |
| 558 | |
| 559 | // GetAllPendingFlows returns list of all flows |
| 560 | func (v *VoltController) GetAllPendingFlows() ([]*of.VoltSubFlow, error) { |
| 561 | var flows []*of.VoltSubFlow |
| 562 | for _, d := range v.devices { |
| 563 | flows = append(flows, d.GetAllPendingFlows()...) |
| 564 | } |
| 565 | return flows, nil |
| 566 | } |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 567 | func (v *VoltController) GetAllMeterInfo() (map[string][]*of.Meter, error) { |
| 568 | logger.Info(ctx, "Entering into GetAllMeterInfo method") |
| 569 | meters := map[string][]*of.Meter{} |
| 570 | for _, device := range v.devices { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 571 | logger.Debugw(ctx, "Inside GetAllMeterInfo method", log.Fields{"deviceId": device.ID, "southbound": device.SouthBoundID, "serial no": device.SerialNum}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 572 | for _, meter := range device.meters { |
| 573 | meters[device.ID] = append(meters[device.ID], meter) |
| 574 | } |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 575 | logger.Debugw(ctx, "Inside GetAllMeterInfo method", log.Fields{"meters": meters}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 576 | } |
| 577 | return meters, nil |
| 578 | } |
| 579 | |
| 580 | func (v *VoltController) GetMeterInfo(cntx context.Context, id uint32) (map[string]*of.Meter, error) { |
| 581 | logger.Info(ctx, "Entering into GetMeterInfo method") |
| 582 | meters := map[string]*of.Meter{} |
| 583 | for _, device := range v.devices { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 584 | logger.Debugw(ctx, "Inside GetMeterInfo method", log.Fields{"deviceId": device.ID}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 585 | meter, err := device.GetMeter(id) |
| 586 | if err != nil { |
| 587 | logger.Errorw(ctx, "Failed to fetch the meter", log.Fields{"Reason": err.Error()}) |
| 588 | return nil, err |
| 589 | } |
| 590 | meters[device.ID] = meter |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 591 | logger.Debugw(ctx, "meters", log.Fields{"Meter": meters}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 592 | } |
| 593 | return meters, nil |
| 594 | } |
| 595 | |
| 596 | func (v *VoltController) GetGroupList() ([]*of.Group, error) { |
| 597 | logger.Info(ctx, "Entering into GetGroupList method") |
| 598 | groups := []*of.Group{} |
| 599 | for _, device := range v.devices { |
| 600 | device.groups.Range(func(key, value interface{}) bool { |
| 601 | groupID := key.(uint32) |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 602 | logger.Debugw(ctx, "Inside GetGroupList method", log.Fields{"groupID": groupID}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 603 | //Obtain all groups associated with the device |
| 604 | grps, ok := device.groups.Load(groupID) |
| 605 | if !ok { |
| 606 | return true |
| 607 | } |
| 608 | grp := grps.(*of.Group) |
| 609 | groups = append(groups, grp) |
| 610 | return true |
| 611 | }) |
| 612 | } |
| 613 | logger.Debugw(ctx, "Groups", log.Fields{"groups": groups}) |
| 614 | return groups, nil |
| 615 | } |
| 616 | |
| 617 | func (v *VoltController) GetGroups(cntx context.Context, id uint32) (*of.Group, error) { |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 618 | logger.Info(ctx, "Entering into GetGroupList method") |
| 619 | var groups *of.Group |
| 620 | for _, device := range v.devices { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 621 | logger.Debugw(ctx, "Inside GetGroupList method", log.Fields{"groupID": id}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 622 | grps, ok := device.groups.Load(id) |
| 623 | if !ok { |
Akash Soni | d36d23b | 2023-08-18 12:51:40 +0530 | [diff] [blame] | 624 | return nil, errors.New("group not found") |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 625 | } |
| 626 | groups = grps.(*of.Group) |
| 627 | logger.Debugw(ctx, "Groups", log.Fields{"groups": groups}) |
| 628 | } |
| 629 | return groups, nil |
| 630 | } |