blob: 62d2f4023315d6d5b64f7f9820c0702a649eef95 [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 }
96 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053097 if !mmt.device.DelMeter(ctx, mmt.meter) {
Naveen Sampath04696f72022-06-13 15:19:14 +053098 // Meter doesn't exist so we dont have to do anything here
99 return nil
100 }
101 }
102
103 if mmt.device.State != DeviceStateUP {
104 logger.Errorw(ctx, "Update Meter Table Failed: Device State DOWN", log.Fields{"Reason": "Device State DOWN", "Meter": mmt.meter.ID})
105 return nil
106 }
107 meterMod, err := of.MeterUpdate(mmt.device.ID, mmt.command, mmt.meter)
108 if err != nil {
109 logger.Errorw(ctx, "Update Meter Table Failed", log.Fields{"Reason": err.Error()})
110 return err
111 }
112
113 if vc := mmt.device.VolthaClient(); vc != nil {
114
115 if _, err = vc.UpdateLogicalDeviceMeterTable(mmt.ctx, meterMod); err != nil {
116 logger.Errorw(ctx, "Update Meter Table Failed", log.Fields{"Reason": err.Error()})
117 }
118 //triggerMeterNotification(err)
119 return err
120 }
121
122 logger.Error(ctx, "Update Meter Table Failed: Voltha Client Unavailable")
123 return nil
124}