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 |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 71 | Devices map[string]*Device |
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) |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 88 | controller.Devices = make(map[string]*Device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 89 | 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 99 | // SetDeviceTableSyncDuration - sets interval between device table sync up activity |
| 100 | // duration - in minutes |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 101 | func (v *VoltController) SetDeviceTableSyncDuration(duration int) { |
| 102 | v.deviceTableSyncDuration = time.Duration(duration) * time.Second |
| 103 | } |
| 104 | |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 105 | // SetMaxFlowRetryDuration - sets max flow retry interval |
| 106 | func (v *VoltController) SetMaxFlowRetryDuration(duration int) { |
| 107 | v.maxFlowRetryDuration = time.Duration(duration) * time.Second |
| 108 | } |
| 109 | |
| 110 | // SetMaxFlowRetryAttempts - sets max flow retry attempts |
| 111 | func (v *VoltController) SetMaxFlowRetryAttempts() { |
| 112 | v.maxFlowRetryAttempts = uint32((v.maxFlowRetryDuration / v.deviceTableSyncDuration)) |
| 113 | } |
| 114 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 115 | // GetDeviceTableSyncDuration - returns configured device table sync duration |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 116 | func (v *VoltController) GetDeviceTableSyncDuration() time.Duration { |
| 117 | return v.deviceTableSyncDuration |
| 118 | } |
| 119 | |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 120 | // GetMaxFlowRetryAttempt - returns max flow retry attempst |
| 121 | func (v *VoltController) GetMaxFlowRetryAttempt() uint32 { |
| 122 | return v.maxFlowRetryAttempts |
| 123 | } |
| 124 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 125 | // AddDevice to add device |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 126 | func (v *VoltController) AddDevice(cntx context.Context, config *intf.VPClientCfg) intf.IVPClient { |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 127 | d := NewDevice(cntx, config.DeviceID, config.SerialNum, config.VolthaClient, config.SouthBoundID, config.MfrDesc, config.HwDesc, config.SwDesc) |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 128 | v.Devices[config.DeviceID] = d |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 129 | v.app.AddDevice(cntx, d.ID, d.SerialNum, config.SouthBoundID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 130 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 131 | d.RestoreMetersFromDb(cntx) |
| 132 | d.RestoreGroupsFromDb(cntx) |
| 133 | d.RestoreFlowsFromDb(cntx) |
| 134 | d.RestorePortsFromDb(cntx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 135 | d.ConnectInd(context.TODO(), intf.DeviceDisc) |
| 136 | d.packetOutChannel = config.PacketOutChannel |
| 137 | |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 138 | 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] | 139 | |
| 140 | return d |
| 141 | } |
| 142 | |
| 143 | // DelDevice to delete device |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 144 | func (v *VoltController) DelDevice(cntx context.Context, id string) { |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 145 | d, ok := v.Devices[id] |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 146 | if ok { |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 147 | delete(v.Devices, id) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 148 | d.Delete() |
| 149 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 150 | v.app.DelDevice(cntx, id) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 151 | d.cancel() // To stop the device tables sync routine |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 152 | logger.Debugw(ctx, "Deleted device", log.Fields{"Device": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 153 | } |
| 154 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 155 | // AddControllerTask - add task to controller queue |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 156 | func (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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 170 | // AddNewDevice - called when new device is discovered. This will be |
| 171 | // processed as part of controller queue |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 172 | func (v *VoltController) AddNewDevice(config *intf.VPClientCfg) { |
| 173 | adt := NewAddDeviceTask(config) |
| 174 | v.AddControllerTask(config.DeviceID, adt) |
| 175 | } |
| 176 | |
| 177 | // GetDevice to get device info |
| 178 | func (v *VoltController) GetDevice(id string) (*Device, error) { |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 179 | d, ok := v.Devices[id] |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 180 | 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 |
| 187 | func (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 |
| 195 | func (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 |
| 216 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 229 | func (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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // DeviceDisableInd is device deactivation indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 238 | func (v *VoltController) DeviceDisableInd(cntx context.Context, dID string) { |
| 239 | v.app.DeviceDisableInd(cntx, dID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 240 | } |
| 241 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 242 | // TriggerPendingProfileDeleteReq - trigger pending profile delete requests |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 243 | func (v *VoltController) TriggerPendingProfileDeleteReq(cntx context.Context, device string) { |
| 244 | v.app.TriggerPendingProfileDeleteReq(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 245 | } |
| 246 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 247 | // TriggerPendingMigrateServicesReq - trigger pending services migration requests |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 248 | func (v *VoltController) TriggerPendingMigrateServicesReq(cntx context.Context, device string) { |
| 249 | v.app.TriggerPendingMigrateServicesReq(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // SetAuditFlags to set the audit flags |
| 253 | func (v *VoltController) SetAuditFlags(device *Device) { |
| 254 | v.app.SetRebootFlag(true) |
| 255 | device.auditInProgress = true |
| 256 | } |
| 257 | |
| 258 | // ResetAuditFlags to reset the audit flags |
| 259 | func (v *VoltController) ResetAuditFlags(device *Device) { |
| 260 | v.app.SetRebootFlag(false) |
| 261 | device.auditInProgress = false |
| 262 | } |
| 263 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 264 | // ProcessFlowModResultIndication - send flow mod result notification |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 265 | func (v *VoltController) ProcessFlowModResultIndication(cntx context.Context, flowStatus intf.FlowStatus) { |
| 266 | v.app.ProcessFlowModResultIndication(cntx, flowStatus) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 267 | } |
| 268 | |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 269 | // IsFlowDelThresholdReached - check if the attempts for flow delete has reached threshold or not |
| 270 | func (v *VoltController) IsFlowDelThresholdReached(cntx context.Context, cookie string, device string) bool { |
| 271 | return v.app.IsFlowDelThresholdReached(cntx, cookie, device) |
| 272 | } |
| 273 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 274 | // AddVPAgent to add the vpagent |
| 275 | func (v *VoltController) AddVPAgent(vep string, vpa *vpagent.VPAgent) { |
| 276 | v.vagent[vep] = vpa |
| 277 | } |
| 278 | |
| 279 | // VPAgent to get vpagent info |
| 280 | func (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 |
| 289 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 300 | 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] | 301 | 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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 312 | // 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] | 313 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 333 | if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 334 | logger.Errorw(ctx, "Delete flow with old cookie failed", log.Fields{"Error": err, "OldCookie": subFlow.OldCookie}) |
| 335 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 336 | if err := d.AddFlow(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 337 | 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 Ravindra | 03aa0bf | 2023-09-12 17:46:40 +0530 | [diff] [blame] | 351 | // delFlowsOnlyInDevice flag indicates that flows should be deleted only in DB/device and should not be forwarded to core |
| 352 | 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] | 353 | 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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 364 | // 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] | 365 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 385 | if err := d.DelFlowWithOldCookie(cntx, subFlow); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 386 | logger.Errorw(ctx, "DelFlowWithOldCookie failed", log.Fields{"OldCookie": subFlow.OldCookie, "Error": err}) |
| 387 | } |
| 388 | } |
| 389 | } else { |
Sridhar Ravindra | 03aa0bf | 2023-09-12 17:46:40 +0530 | [diff] [blame] | 390 | // 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | return nil |
| 405 | } |
| 406 | |
| 407 | // GroupUpdate for group update |
| 408 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 422 | // 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] | 423 | 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 |
| 432 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 450 | func (v *VoltController) PortAddInd(cntx context.Context, device string, id uint32, name string) { |
| 451 | v.app.PortAddInd(cntx, device, id, name) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // PortDelInd for port delete indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 455 | func (v *VoltController) PortDelInd(cntx context.Context, device string, port string) { |
| 456 | v.app.PortDelInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // PortUpdateInd for port update indication |
| 460 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 465 | func (v *VoltController) PortUpInd(cntx context.Context, device string, port string) { |
| 466 | v.app.PortUpInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // PortDownInd for port down indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 470 | func (v *VoltController) PortDownInd(cntx context.Context, device string, port string) { |
| 471 | v.app.PortDownInd(cntx, device, port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // DeviceUpInd for device up indication |
| 475 | func (v *VoltController) DeviceUpInd(device string) { |
| 476 | v.app.DeviceUpInd(device) |
| 477 | } |
| 478 | |
| 479 | // DeviceDownInd for device down indication |
| 480 | func (v *VoltController) DeviceDownInd(device string) { |
| 481 | v.app.DeviceDownInd(device) |
| 482 | } |
| 483 | |
| 484 | // PacketInInd for packet in indication |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 485 | func (v *VoltController) PacketInInd(cntx context.Context, device string, port string, data []byte) { |
| 486 | v.app.PacketInInd(cntx, device, port, data) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // GetPortState to get port status |
| 490 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 500 | func (v *VoltController) UpdateMvlanProfiles(cntx context.Context, device string) { |
| 501 | v.app.UpdateMvlanProfilesForDevice(cntx, device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // GetController to get controller |
| 505 | func GetController() *VoltController { |
| 506 | return vcontroller |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | // PostIndication to post indication |
| 511 | func (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 |
| 546 | func (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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 553 | } |
| 554 | |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 555 | // AddBlockedDevices to add Devices to blocked Devices list |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 556 | func (v *VoltController) AddBlockedDevices(deviceSerialNumber string) { |
| 557 | v.BlockedDeviceList.Set(deviceSerialNumber, deviceSerialNumber) |
| 558 | } |
| 559 | |
| 560 | // DelBlockedDevices to remove device from blocked device list |
| 561 | func (v *VoltController) DelBlockedDevices(deviceSerialNumber string) { |
| 562 | v.BlockedDeviceList.Remove(deviceSerialNumber) |
| 563 | } |
| 564 | |
| 565 | // IsBlockedDevice to check if device is blocked |
| 566 | func (v *VoltController) IsBlockedDevice(deviceSerialNumber string) bool { |
| 567 | _, ifPresent := v.BlockedDeviceList.Get(deviceSerialNumber) |
| 568 | return ifPresent |
| 569 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 570 | |
| 571 | // GetFlows returns flow specific to device and flowID |
| 572 | func (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 |
| 585 | func (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 Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 589 | return nil, nil |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 590 | } |
| 591 | return d.GetAllFlows(), nil |
| 592 | } |
| 593 | |
| 594 | // GetAllFlows returns list of all flows |
| 595 | func (v *VoltController) GetAllFlows() ([]*of.VoltSubFlow, error) { |
| 596 | var flows []*of.VoltSubFlow |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 597 | for _, d := range v.Devices { |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 598 | flows = append(flows, d.GetAllFlows()...) |
| 599 | } |
| 600 | return flows, nil |
| 601 | } |
| 602 | |
| 603 | // GetAllPendingFlows returns list of all flows |
| 604 | func (v *VoltController) GetAllPendingFlows() ([]*of.VoltSubFlow, error) { |
| 605 | var flows []*of.VoltSubFlow |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 606 | for _, d := range v.Devices { |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 607 | flows = append(flows, d.GetAllPendingFlows()...) |
| 608 | } |
| 609 | return flows, nil |
| 610 | } |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 611 | func (v *VoltController) GetAllMeterInfo() (map[string][]*of.Meter, error) { |
| 612 | logger.Info(ctx, "Entering into GetAllMeterInfo method") |
| 613 | meters := map[string][]*of.Meter{} |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 614 | for _, device := range v.Devices { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 615 | 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] | 616 | for _, meter := range device.meters { |
| 617 | meters[device.ID] = append(meters[device.ID], meter) |
| 618 | } |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 619 | logger.Debugw(ctx, "Inside GetAllMeterInfo method", log.Fields{"meters": meters}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 620 | } |
| 621 | return meters, nil |
| 622 | } |
| 623 | |
| 624 | func (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 Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 627 | for _, device := range v.Devices { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 628 | logger.Debugw(ctx, "Inside GetMeterInfo method", log.Fields{"deviceId": device.ID}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 629 | 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 Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 635 | logger.Debugw(ctx, "meters", log.Fields{"Meter": meters}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 636 | } |
| 637 | return meters, nil |
| 638 | } |
| 639 | |
| 640 | func (v *VoltController) GetGroupList() ([]*of.Group, error) { |
| 641 | logger.Info(ctx, "Entering into GetGroupList method") |
| 642 | groups := []*of.Group{} |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 643 | for _, device := range v.Devices { |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 644 | device.groups.Range(func(key, value interface{}) bool { |
| 645 | groupID := key.(uint32) |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 646 | logger.Debugw(ctx, "Inside GetGroupList method", log.Fields{"groupID": groupID}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 647 | //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 | |
| 661 | func (v *VoltController) GetGroups(cntx context.Context, id uint32) (*of.Group, error) { |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 662 | logger.Info(ctx, "Entering into GetGroupList method") |
| 663 | var groups *of.Group |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 664 | for _, device := range v.Devices { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 665 | logger.Debugw(ctx, "Inside GetGroupList method", log.Fields{"groupID": id}) |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 666 | grps, ok := device.groups.Load(id) |
| 667 | if !ok { |
Akash Soni | d36d23b | 2023-08-18 12:51:40 +0530 | [diff] [blame] | 668 | return nil, errors.New("group not found") |
Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame] | 669 | } |
| 670 | groups = grps.(*of.Group) |
| 671 | logger.Debugw(ctx, "Groups", log.Fields{"groups": groups}) |
| 672 | } |
| 673 | return groups, nil |
| 674 | } |