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. |
| 14 | */ |
| 15 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 19 | "context" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 20 | "encoding/json" |
| 21 | "errors" |
| 22 | "sync" |
| 23 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 24 | "voltha-go-controller/database" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 25 | cntlr "voltha-go-controller/internal/pkg/controller" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 26 | "voltha-go-controller/internal/pkg/of" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 27 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // VoltShaperConfig is shaper profile configuration structure |
| 31 | type VoltShaperConfig struct { |
| 32 | Name string |
| 33 | BurstSize uint32 |
| 34 | } |
| 35 | |
| 36 | // VoltBwConfig is bandwidth profile configuration structure |
| 37 | type VoltBwConfig struct { |
| 38 | Name string |
| 39 | Fir uint32 |
| 40 | Air uint32 |
| 41 | Eir uint32 |
| 42 | } |
| 43 | |
| 44 | // VoltBandwidthProf is bandwidth profile stored at VGC |
| 45 | type VoltBandwidthProf struct { |
| 46 | VoltBwConfig |
| 47 | } |
| 48 | |
| 49 | // VoltShaperProf is shaper profile stored at VGC |
| 50 | type VoltShaperProf struct { |
| 51 | VoltShaperConfig |
| 52 | } |
| 53 | |
| 54 | // VoltMeterProf is meter profile stored at VGC |
| 55 | type VoltMeterProf struct { |
| 56 | VoltMeter |
| 57 | } |
| 58 | |
| 59 | // MeterMgr structure |
| 60 | type MeterMgr struct { |
| 61 | Meters sync.Map |
| 62 | MetersByID sync.Map |
| 63 | LastMeterID uint32 |
| 64 | } |
| 65 | |
| 66 | // Init to initialize MeterMgr |
| 67 | func (m *MeterMgr) Init() { |
| 68 | m.LastMeterID = 0 |
| 69 | } |
| 70 | |
| 71 | // VoltMeter : A VOLT meter is a combination of BW and shaper profiles |
| 72 | // The ID is generated by the VOLT application |
| 73 | type VoltMeter struct { |
| 74 | Name string |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 75 | Version string |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 76 | ID uint32 |
| 77 | Fir uint32 |
| 78 | Air uint32 |
| 79 | Eir uint32 |
| 80 | BurstSize uint32 |
| 81 | AssociatedServices uint32 |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 82 | Cir uint32 |
| 83 | Cbs uint32 |
| 84 | Pir uint32 |
| 85 | Pbs uint32 |
| 86 | Gir uint32 |
| 87 | Ebs uint32 |
| 88 | } |
| 89 | |
| 90 | // WriteToDb to write a meter profile to DB |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 91 | func (vm *VoltMeter) WriteToDb(cntx context.Context) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 92 | vm.Version = database.PresentVersionMap[database.MeterPath] |
| 93 | b, err := json.Marshal(vm) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 97 | if err1 := db.PutMeter(cntx, vm.Name, string(b)); err1 != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 98 | return err1 |
| 99 | } |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | // DelFromDb to delete a meter profile from DB |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 104 | func (vm *VoltMeter) DelFromDb(cntx context.Context) { |
| 105 | _ = db.DelMeter(cntx, vm.Name) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // GetMeterByName to get meter by name |
| 109 | func (m *MeterMgr) GetMeterByName(name string) (*VoltMeter, bool) { |
| 110 | meter, ok := m.Meters.Load(name) |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 111 | logger.Infow(ctx, "Meter Obtained Name", log.Fields{"Name": name, "Meter": meter}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 112 | if ok { |
| 113 | return meter.(*VoltMeter), ok |
| 114 | } |
| 115 | return nil, ok |
| 116 | } |
| 117 | |
| 118 | // GetMeterByID to get meter by ID |
| 119 | func (m *MeterMgr) GetMeterByID(id uint32) (*VoltMeter, bool) { |
| 120 | meter, ok := m.MetersByID.Load(id) |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 121 | logger.Infow(ctx, "GetMeter Obtained ID", log.Fields{"ID": id, "Meter": meter}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 122 | if ok { |
| 123 | return meter.(*VoltMeter), ok |
| 124 | } |
| 125 | return nil, ok |
| 126 | } |
| 127 | |
| 128 | // AddMeter to add meter |
| 129 | func (m *MeterMgr) AddMeter(meter *VoltMeter) { |
| 130 | m.Meters.Store(meter.Name, meter) |
| 131 | m.MetersByID.Store(meter.ID, meter) |
| 132 | logger.Infow(ctx, "Meter Added/Updated", log.Fields{"Meter": meter, "Name": meter.Name, "Id": meter.ID}) |
| 133 | } |
| 134 | |
| 135 | // DelMeter to delete meter |
| 136 | func (m *MeterMgr) DelMeter(meter *VoltMeter) { |
| 137 | m.Meters.Delete(meter.Name) |
| 138 | m.MetersByID.Delete(meter.ID) |
| 139 | logger.Infow(ctx, "Meter Deleted", log.Fields{"Meter": meter, "Name": meter.Name, "Id": meter.ID}) |
| 140 | } |
| 141 | |
| 142 | // AddToDevice to add meter to the device |
| 143 | func (vm *VoltMeter) AddToDevice(port string, device string, aggVM *VoltMeter) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 144 | logger.Debugw(ctx, "Add To Device", log.Fields{"Id": vm.ID, "Device": device, "Port": port}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 145 | meter := of.NewMeter(vm.ID) |
| 146 | // meter.AddBand(vm.Air, vm.BurstSize) |
| 147 | // meter.AddBand(vm.Eir, vm.BurstSize) |
| 148 | // if aggVM != nil { |
| 149 | // meter.AddBand(aggVM.Air, aggVM.BurstSize) |
| 150 | // meter.AddBand(aggVM.Eir, aggVM.BurstSize) |
| 151 | // } |
| 152 | |
| 153 | //Community VGC Impl |
| 154 | |
| 155 | //Set Cir |
| 156 | if vm.Cir != 0 { |
| 157 | meter.AddBand(vm.Cir, vm.Cbs) |
| 158 | } |
| 159 | |
| 160 | //Set Air to 0 if both air & gir are set |
| 161 | if vm.Air != 0 && vm.Gir != 0 { |
| 162 | vm.Air = 0 |
| 163 | } |
| 164 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 165 | // Set Pir & Pbs |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 166 | var pir uint32 |
| 167 | var pbs uint32 |
| 168 | if vm.Pir != 0 { |
| 169 | pir = vm.Pir |
| 170 | } else { |
| 171 | pir = vm.Eir + vm.Cir + vm.Gir + vm.Air |
| 172 | } |
| 173 | |
| 174 | if vm.Pbs != 0 { |
| 175 | pbs = vm.Pbs |
| 176 | } else { |
| 177 | pbs = vm.Ebs + vm.Cbs |
| 178 | } |
| 179 | meter.AddBand(pir, pbs) |
| 180 | |
| 181 | //Set Gir |
| 182 | if vm.Gir != 0 { |
| 183 | meter.AddBand(vm.Gir, 0) |
| 184 | } |
| 185 | |
| 186 | logger.Infow(ctx, "Meter Config", log.Fields{"Cir": vm.Cir, "Air": vm.Air, "Pir": vm.Pir, "Gir": vm.Gir, "Eir": vm.Eir}) |
| 187 | logger.Infow(ctx, "Meter Burst Config", log.Fields{"Cbs": vm.Cbs, "Pbs": vm.Pbs}) |
| 188 | logger.Infow(ctx, "Meter Burst Oper", log.Fields{"Pir": pir, "Pbs": pbs}) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 189 | // Set Air |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 190 | // Air is used in place of Gir only if Gir is |
| 191 | // not present and Air is not 0 |
| 192 | if vm.Air != 0 { |
| 193 | meter.AddBand(vm.Air, 0) |
| 194 | } |
| 195 | |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 196 | logger.Debugw(ctx, "Total Bands are", log.Fields{"meter": *meter}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 197 | if err := cntlr.GetController().ModMeter(port, device, of.MeterCommandAdd, meter); err != nil { |
| 198 | logger.Warnw(ctx, "Add meter to device Failed", log.Fields{"Id": vm.ID, "meter": *meter, "Error": err}) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // AddMeterToDevice to add meter to the device |
| 203 | func (m *MeterMgr) AddMeterToDevice(port string, device string, meterID uint32, aggMeterID uint32) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 204 | logger.Debugw(ctx, "Adding Meter To Device", log.Fields{"Agg MeterID": aggMeterID, "Device": device, "Port": port, "MeterID": meterID}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 205 | var aggVM *VoltMeter |
| 206 | vm, err := m.GetMeterByProfID(meterID) |
| 207 | if err == nil { |
| 208 | if 0 != aggMeterID { //Assuming valid meter id will never be 0 |
| 209 | if aggVM, err = m.GetMeterByProfID(aggMeterID); err != nil { |
| 210 | logger.Warnw(ctx, "Aggregated Meter not found", log.Fields{"Id": aggMeterID}) |
| 211 | } |
| 212 | } |
| 213 | vm.AddToDevice(port, device, aggVM) |
| 214 | } else { |
| 215 | logger.Warnw(ctx, "Meter not found", log.Fields{"Id": meterID}) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // RestoreMetersFromDb to read from the DB and restore all the services |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 220 | func (m *MeterMgr) RestoreMetersFromDb(cntx context.Context) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 221 | // VNETS must be learnt first |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 222 | logger.Infow(ctx, "Received RestoreMetersFromDb and LastMeterID on restart", log.Fields{"LastMeterID": m.LastMeterID}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 223 | ms, _ := db.GetMeters(cntx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 224 | for _, mt := range ms { |
| 225 | b, ok := mt.Value.([]byte) |
| 226 | if !ok { |
| 227 | logger.Warn(ctx, "The value type is not []byte") |
| 228 | continue |
| 229 | } |
| 230 | var meter VoltMeter |
| 231 | err := json.Unmarshal(b, &meter) |
| 232 | if err != nil { |
| 233 | logger.Warn(ctx, "Unmarshal of meter profile failed") |
| 234 | continue |
| 235 | } |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 236 | logger.Debugw(ctx, "Retrieved Meter", log.Fields{"Meter": meter.Name}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 237 | m.AddMeter(&meter) |
| 238 | if meter.ID > m.LastMeterID { |
| 239 | m.LastMeterID = meter.ID |
| 240 | } |
| 241 | } |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 242 | logger.Debugw(ctx, "LastMeterID on reading DB", log.Fields{"LastMeterID": m.LastMeterID}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // AddMeterProf to add the meter profile name as key |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 246 | func (va *VoltApplication) AddMeterProf(cntx context.Context, cfg VoltMeter) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 247 | logger.Infow(ctx, "Add the meter profile name as key", log.Fields{"MeterConfig": cfg}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 248 | mm := &va.MeterMgr |
| 249 | if _, ok := mm.GetMeterByName(cfg.Name); ok { |
| 250 | logger.Warnw(ctx, "Meter profile exists", log.Fields{"Name": cfg.Name}) |
| 251 | return |
| 252 | } |
| 253 | |
| 254 | mm.LastMeterID++ |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 255 | // FIX-ME: Hardcoded the meter-id temp till meter delete is introduced |
| 256 | // Restriction: Only one meter profile should be used across all services |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 257 | // id := uint32(1) //mm.LastMeterId |
| 258 | id := mm.LastMeterID |
| 259 | cfg.ID = id |
| 260 | mm.AddMeter(&cfg) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 261 | if err := cfg.WriteToDb(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 262 | logger.Warnw(ctx, "MeterProf Write to DB Failed", log.Fields{"MeterConfig": cfg, "Error": err}) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // UpdateMeterProf to update the meter profile |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 267 | func (va *VoltApplication) UpdateMeterProf(cntx context.Context, cfg VoltMeter) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 268 | logger.Debugw(ctx, "Update the meter profile name as key", log.Fields{"MeterConfig": cfg}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 269 | mm := &va.MeterMgr |
| 270 | if _, ok := mm.GetMeterByName(cfg.Name); !ok { |
| 271 | logger.Warnw(ctx, "Meter profile does not exist", log.Fields{"Name": cfg.Name}) |
| 272 | return |
| 273 | } |
| 274 | mm.AddMeter(&cfg) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 275 | if err := cfg.WriteToDb(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 276 | logger.Warnw(ctx, "MeterProf Write to DB Failed", log.Fields{"MeterConfig": cfg, "Error": err}) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // GetMeterByProfID to get a meter based on the identities of bandwidth profile and shaper |
| 281 | // profile names. |
| 282 | func (m *MeterMgr) GetMeterByProfID(id uint32) (*VoltMeter, error) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 283 | logger.Debugw(ctx, "Get Meter by Profile id", log.Fields{"ID": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 284 | if mtr, ok := m.GetMeterByID(id); ok { |
| 285 | return mtr, nil |
| 286 | } |
| 287 | return nil, errors.New("Meter Missing") |
| 288 | } |
| 289 | |
| 290 | // GetMeter to get a meter based on the identities of bandwidth profile and shaper |
| 291 | // profile names. |
| 292 | func (m *MeterMgr) GetMeter(meterID string) (*VoltMeter, error) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 293 | logger.Debugw(ctx, "Get Meter by Meter id", log.Fields{"MeterID": meterID}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 294 | if mt, ok := m.GetMeterByName(meterID); ok { |
| 295 | return mt, nil |
| 296 | } |
| 297 | return nil, errors.New("Meter Missing") |
| 298 | } |
| 299 | |
| 300 | // DeleteFromDevice to delete meter from the device |
| 301 | func (vm *VoltMeter) DeleteFromDevice(port string, device string) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 302 | logger.Debugw(ctx, "Delete meter from device", log.Fields{"Id": vm.ID, "Port": port, "Device": device}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 303 | meter := of.NewMeter(vm.ID) |
| 304 | |
| 305 | logger.Debugw(ctx, "Delete meter from device", log.Fields{"Id": vm.ID, "meter": *meter}) |
| 306 | if err := cntlr.GetController().ModMeter(port, device, of.MeterCommandDel, meter); err != nil { |
| 307 | logger.Warnw(ctx, "Delete meter from device Failed", log.Fields{"Id": vm.ID, "meter": *meter, "Error": err}) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // DelMeterProf to delete meter profile |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 312 | func (va *VoltApplication) DelMeterProf(cntx context.Context, name string) error { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 313 | logger.Debugw(ctx, "Delete meter profile", log.Fields{"Name": name}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 314 | mm := &va.MeterMgr |
| 315 | if _, ok := mm.GetMeterByName(name); !ok { |
| 316 | logger.Warnw(ctx, "Meter profile does not exist", log.Fields{"Name": name}) |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 317 | return errors.New("meter profile doesn't exist") |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 318 | } |
| 319 | cfg, _ := mm.GetMeterByName(name) |
| 320 | if cfg.AssociatedServices != 0 { |
| 321 | logger.Warnw(ctx, "Mismatch in submgr and vgc oeter profile service reference", |
| 322 | log.Fields{"MeterProfile": name, "serviceCount": cfg.AssociatedServices}) |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 323 | return errors.New("service reference is not 0") |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 324 | } |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 325 | // TODO : delete from all devices |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 326 | delmeterFromDevice := func(key interface{}, value interface{}) bool { |
| 327 | device := key.(string) |
| 328 | port, _ := GetApplication().GetNniPort(device) |
| 329 | cfg.DeleteFromDevice(port, device) |
| 330 | return true |
| 331 | } |
| 332 | va.DevicesDisc.Range(delmeterFromDevice) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 333 | cfg.DelFromDb(cntx) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 334 | // Delete meter from device will be invoked by caller separately |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 335 | mm.DelMeter(cfg) |
| 336 | return nil |
| 337 | } |