blob: 625b1a7134bb0fc8197f5aecbddf634721a27df1 [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 of
17
18import (
Tinoj Joseph1d108322022-07-13 10:07:39 +053019 // "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053020 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
21 // "github.com/opencord/voltha-protos/v5/go/voltha"
22)
23
24// MeterCommand : Meters and bands as stored by VOLT application
25type MeterCommand uint32
26
27const (
28 // MeterCommandAdd constant
29 MeterCommandAdd MeterCommand = 1
30 // MeterCommandDel constant
31 MeterCommandDel MeterCommand = 2
32)
33
34const (
35 // MeterOperSuccess constant
36 MeterOperSuccess = 0
37 // MeterOperFailure constant
38 MeterOperFailure = 1
39 // MeterOperPending constant
40 MeterOperPending = 2
41)
42
43// Band structure
44type Band struct {
45 Type uint32
46 Rate uint32
47 BurstSize uint32
48}
49
50// Meter structure
51type Meter struct {
52 ID uint32
53 Bands []Band
54 State uint8
55 ErrorReason string
56}
57
58// NewMeter is constructor for Meter
59func NewMeter(id uint32) *Meter {
60 var vm Meter
61 vm.ID = id
62 return &vm
63}
64
65// AddBand to add band info to meter
66func (vm *Meter) AddBand(rate uint32, bs uint32) {
67 vb := Band{Rate: rate, BurstSize: bs}
68 vm.Bands = append(vm.Bands, vb)
69}
70
71// MeterUpdate for conversion of VOLT to OF for meters and bands
72func MeterUpdate(deviceID string, c MeterCommand, m *Meter) (*ofp.MeterModUpdate, error) {
73 mmu := &ofp.MeterModUpdate{Id: deviceID}
74 mmu.MeterMod = &ofp.OfpMeterMod{
75 MeterId: m.ID,
76 }
77 if c == MeterCommandAdd {
78 mmu.MeterMod.Command = ofp.OfpMeterModCommand_OFPMC_ADD
79 mmu.MeterMod.Flags = 5
80 for _, b := range m.Bands {
81 AddBand(mmu, b)
82 }
83 } else {
84 mmu.MeterMod.Command = ofp.OfpMeterModCommand_OFPMC_DELETE
85 }
86 return mmu, nil
87}
88
89// AddBand to add band info
90func AddBand(mmu *ofp.MeterModUpdate, b Band) {
91 band := &ofp.OfpMeterBandHeader{}
92 band.Type = ofp.OfpMeterBandType_OFPMBT_DROP
93 band.Rate = b.Rate
94 band.BurstSize = b.BurstSize
95 // band.Data = &ofp.OfpMeterBandHeader_Drop{
96 // Drop: &ofp.OfpMeterBandDrop{},
97 // }
98 mmu.MeterMod.Bands = append(mmu.MeterMod.Bands, band)
99}