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. |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 14 | */ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 15 | |
| 16 | package controller |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "time" |
| 21 | |
| 22 | "voltha-go-controller/internal/pkg/of" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 23 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // ModMeterTask structure |
| 27 | type ModMeterTask struct { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 28 | ctx context.Context |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 29 | meter *of.Meter |
| 30 | device *Device |
| 31 | timestamp string |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 32 | command of.MeterCommand |
| 33 | taskID uint8 |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // NewModMeterTask is the constructor for ModMeterTask |
| 37 | func NewModMeterTask(ctx context.Context, command of.MeterCommand, meter *of.Meter, device *Device) *ModMeterTask { |
| 38 | var mmt ModMeterTask |
| 39 | mmt.device = device |
| 40 | mmt.meter = meter |
| 41 | mmt.ctx = ctx |
| 42 | mmt.command = command |
| 43 | tstamp := (time.Now()).Format(time.RFC3339Nano) |
| 44 | mmt.timestamp = tstamp |
| 45 | return &mmt |
| 46 | } |
| 47 | |
| 48 | // Name returns name of the task |
| 49 | func (mmt *ModMeterTask) Name() string { |
| 50 | return "Add Flows Task" |
| 51 | } |
| 52 | |
| 53 | // TaskID returns task Id of the task |
| 54 | func (mmt *ModMeterTask) TaskID() uint8 { |
| 55 | return mmt.taskID |
| 56 | } |
| 57 | |
| 58 | // Timestamp returns time stamp for the task |
| 59 | func (mmt *ModMeterTask) Timestamp() string { |
| 60 | return mmt.timestamp |
| 61 | } |
| 62 | |
| 63 | // Stop to stop the task |
| 64 | func (mmt *ModMeterTask) Stop() { |
| 65 | } |
| 66 | |
| 67 | // Start to start the task |
| 68 | func (mmt *ModMeterTask) Start(ctx context.Context, taskID uint8) error { |
| 69 | mmt.taskID = taskID |
| 70 | mmt.ctx = ctx |
| 71 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 72 | // Temp commenting Sync response handling |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 73 | //triggerMeterNotification := func(err error) { |
| 74 | |
| 75 | // statusCode, statusMsg := infraerror.GetErrorInfo(err) |
| 76 | |
| 77 | // if mmt.command == of.MeterCommandAdd && infraerrorcode.ErrorCode(statusCode) != infraerrorcode.ErrOk { |
| 78 | // mmt.meter.State = of.MeterOperFailure |
| 79 | // mmt.meter.ErrorReason = statusMsg |
| 80 | |
| 81 | // logger.Errorw(ctx, "Update Meter Table Failed", |
| 82 | // log.Fields{"meterId": mmt.meter.ID, "meterOp": mmt.command, "Status": statusCode, "errorReason": statusMsg}) |
| 83 | // go mmt.device.AddMeterToDb(mmt.meter) |
| 84 | // } else { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 85 | // logger.Infow("Meter Mod Result", log.Fields{"meterID": mmt.meter.ID, "Error Code": statusCode}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 86 | // } |
| 87 | // } |
| 88 | |
| 89 | // First add/delete the flows first locally before passing them to actual device |
| 90 | if mmt.command == of.MeterCommandAdd { |
| 91 | mmt.meter.State = of.MeterOperPending |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 92 | if err := mmt.device.AddMeter(ctx, mmt.meter); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 93 | // Meter already exists so we dont have to do anything here |
| 94 | return nil |
| 95 | } |
Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame^] | 96 | logger.Debugw(ctx, "Updated meter state to pending", log.Fields{"Meter": mmt.meter.ID}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 97 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 98 | if !mmt.device.DelMeter(ctx, mmt.meter) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 99 | // Meter doesn't exist so we dont have to do anything here |
| 100 | return nil |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if mmt.device.State != DeviceStateUP { |
| 105 | logger.Errorw(ctx, "Update Meter Table Failed: Device State DOWN", log.Fields{"Reason": "Device State DOWN", "Meter": mmt.meter.ID}) |
| 106 | return nil |
| 107 | } |
| 108 | meterMod, err := of.MeterUpdate(mmt.device.ID, mmt.command, mmt.meter) |
| 109 | if err != nil { |
| 110 | logger.Errorw(ctx, "Update Meter Table Failed", log.Fields{"Reason": err.Error()}) |
| 111 | return err |
| 112 | } |
| 113 | |
| 114 | if vc := mmt.device.VolthaClient(); vc != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 115 | if _, err = vc.UpdateLogicalDeviceMeterTable(mmt.ctx, meterMod); err != nil { |
| 116 | logger.Errorw(ctx, "Update Meter Table Failed", log.Fields{"Reason": err.Error()}) |
Sridhar Ravindra | 2d2ef4e | 2023-02-08 16:43:38 +0530 | [diff] [blame] | 117 | } else { |
| 118 | mmt.meter.State = of.MeterOperSuccess |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 119 | if err = mmt.device.UpdateMeter(ctx, mmt.meter); err != nil { |
Sridhar Ravindra | 2d2ef4e | 2023-02-08 16:43:38 +0530 | [diff] [blame] | 120 | // Meter does not exist, update failed |
| 121 | logger.Error(ctx, "Update meter to DB failed") |
| 122 | } |
Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame^] | 123 | logger.Debugw(ctx, "Updated meter state to success", log.Fields{"Meter": mmt.meter.ID}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 124 | } |
| 125 | //triggerMeterNotification(err) |
| 126 | return err |
| 127 | } |
| 128 | |
| 129 | logger.Error(ctx, "Update Meter Table Failed: Voltha Client Unavailable") |
| 130 | return nil |
| 131 | } |