blob: acbef44109a5096d99f5ed4306f8272c5b8cfc12 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16package controller
17
18import (
19 "context"
20 "time"
21
22 "voltha-go-controller/internal/pkg/of"
Tinoj Joseph1d108322022-07-13 10:07:39 +053023 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053024)
25
26// ModMeterTask structure
27type ModMeterTask struct {
28 taskID uint8
29 ctx context.Context
30 command of.MeterCommand
31 meter *of.Meter
32 device *Device
33 timestamp string
34}
35
36// NewModMeterTask is the constructor for ModMeterTask
37func 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
49func (mmt *ModMeterTask) Name() string {
50 return "Add Flows Task"
51}
52
53// TaskID returns task Id of the task
54func (mmt *ModMeterTask) TaskID() uint8 {
55 return mmt.taskID
56}
57
58// Timestamp returns time stamp for the task
59func (mmt *ModMeterTask) Timestamp() string {
60 return mmt.timestamp
61}
62
63// Stop to stop the task
64func (mmt *ModMeterTask) Stop() {
65}
66
67// Start to start the task
68func (mmt *ModMeterTask) Start(ctx context.Context, taskID uint8) error {
69 mmt.taskID = taskID
70 mmt.ctx = ctx
71
72 //Temp commenting Sync response handling
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 Joseph1d108322022-07-13 10:07:39 +053085 // logger.Infow("Meter Mod Result", log.Fields{"meterID": mmt.meter.ID, "Error Code": statusCode})
Naveen Sampath04696f72022-06-13 15:19:14 +053086 // }
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 Joseph07cc5372022-07-18 22:53:51 +053092 if err := mmt.device.AddMeter(ctx, mmt.meter); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053093 // Meter already exists so we dont have to do anything here
94 return nil
95 }
Sridhar Ravindra2d2ef4e2023-02-08 16:43:38 +053096 logger.Infow(ctx, "Updated meter state to pending", log.Fields{"Meter": mmt.meter.ID})
Naveen Sampath04696f72022-06-13 15:19:14 +053097 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053098 if !mmt.device.DelMeter(ctx, mmt.meter) {
Naveen Sampath04696f72022-06-13 15:19:14 +053099 // 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 {
115
116 if _, err = vc.UpdateLogicalDeviceMeterTable(mmt.ctx, meterMod); err != nil {
117 logger.Errorw(ctx, "Update Meter Table Failed", log.Fields{"Reason": err.Error()})
Sridhar Ravindra2d2ef4e2023-02-08 16:43:38 +0530118 } else {
119 mmt.meter.State = of.MeterOperSuccess
120 if err := mmt.device.UpdateMeter(ctx, mmt.meter); err != nil {
121 // Meter does not exist, update failed
122 logger.Error(ctx, "Update meter to DB failed")
123 }
124 logger.Infow(ctx, "Updated meter state to success", log.Fields{"Meter": mmt.meter.ID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530125 }
126 //triggerMeterNotification(err)
127 return err
128 }
129
130 logger.Error(ctx, "Update Meter Table Failed: Voltha Client Unavailable")
131 return nil
132}