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 of |
| 17 | |
| 18 | import ( |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 19 | // "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 20 | 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 |
| 25 | type MeterCommand uint32 |
| 26 | |
| 27 | const ( |
| 28 | // MeterCommandAdd constant |
| 29 | MeterCommandAdd MeterCommand = 1 |
| 30 | // MeterCommandDel constant |
| 31 | MeterCommandDel MeterCommand = 2 |
| 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | // MeterOperSuccess constant |
| 36 | MeterOperSuccess = 0 |
| 37 | // MeterOperFailure constant |
| 38 | MeterOperFailure = 1 |
| 39 | // MeterOperPending constant |
| 40 | MeterOperPending = 2 |
| 41 | ) |
| 42 | |
| 43 | // Band structure |
| 44 | type Band struct { |
| 45 | Type uint32 |
| 46 | Rate uint32 |
| 47 | BurstSize uint32 |
| 48 | } |
| 49 | |
| 50 | // Meter structure |
| 51 | type Meter struct { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 52 | ErrorReason string |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 53 | Bands []Band |
| 54 | ID uint32 |
| 55 | State uint8 |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // NewMeter is constructor for Meter |
| 59 | func 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 |
| 66 | func (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 |
| 72 | func 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 |
| 90 | func 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 | } |