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 | // This implementation of database assumes that it is working for |
| 16 | // Open ONU adapter. Thus, it assumes some base path for all the |
| 17 | // database operations. For all database operations, the key passed is |
| 18 | // added to the database base path. |
| 19 | |
| 20 | package database |
| 21 | |
| 22 | import ( |
| 23 | "context" |
| 24 | "errors" |
| 25 | "net" |
| 26 | "strconv" |
| 27 | "time" |
| 28 | "fmt" |
| 29 | |
| 30 | "voltha-go-controller/internal/pkg/of" |
| 31 | "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 32 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | var logger log.CLogger |
| 36 | var ctx = context.TODO() |
| 37 | |
| 38 | // Database structure |
| 39 | type Database struct { |
| 40 | storeType string |
| 41 | address string |
| 42 | //timeout uint32 |
| 43 | kvc kvstore.Client |
| 44 | } |
| 45 | |
| 46 | // Initialize the database module. The database module runs as a singleton |
| 47 | // object and is initialized when the adapter is created. |
| 48 | func Initialize(storeType string, address string, timeout int) (*Database, error) { |
| 49 | var err error |
| 50 | var database Database |
| 51 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
| 52 | database.address = address |
| 53 | database.storeType = storeType |
| 54 | switch storeType { |
| 55 | case "redis": |
| 56 | database.kvc, err = kvstore.NewRedisClient(address, time.Duration(timeout), false) |
| 57 | return &database, err |
| 58 | } |
| 59 | return &database, errors.New("unsupported-kv-store") |
| 60 | } |
| 61 | |
| 62 | // Utility function that retrieves value for a key. It is assumed that |
| 63 | // the information is always a string and the data retrieved is returned |
| 64 | // as a string |
| 65 | |
| 66 | // Put to add value to database |
| 67 | func (db *Database) Put(fullKeyPath, value string) error { |
| 68 | return db.kvc.Put(context.Background(), fullKeyPath, value) |
| 69 | } |
| 70 | |
| 71 | // Get to retrieve value from database |
| 72 | func (db *Database) Get(key string) (string, error) { |
| 73 | kv, err := db.kvc.Get(context.Background(), key) |
| 74 | if err != nil { |
| 75 | return "", err |
| 76 | } |
| 77 | if kv != nil { |
| 78 | return string(kv.Value.([]byte)), nil |
| 79 | } |
| 80 | return "", errors.New("Value not found") |
| 81 | } |
| 82 | |
| 83 | // Del to delete value from database |
| 84 | func (db *Database) Del(fullPath string) error { |
| 85 | if err := db.kvc.Delete(context.Background(), fullPath); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 86 | logger.Errorw(ctx, "The path doesn't exist", log.Fields{"key": fullPath, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 87 | return err |
| 88 | } |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | // DeleteAll to delete all value from database |
| 93 | func (db *Database) DeleteAll(fullPath string) error { |
| 94 | if err := db.kvc.DeleteWithPrefix(context.Background(), fullPath); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 95 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": fullPath, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 96 | return err |
| 97 | } |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | // DeleteAllUnderHashKey to delete all values under hash key |
| 102 | func (db *Database) DeleteAllUnderHashKey(hashKeyPrefix string) error { |
| 103 | if err := db.kvc.Delete(context.Background(), hashKeyPrefix); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 104 | logger.Errorw(ctx, "The key path doesn't exist", log.Fields{"key": hashKeyPrefix, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 105 | return err |
| 106 | } |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | // List to list the values |
| 111 | func (db *Database) List(key string) (map[string]*kvstore.KVPair, error) { |
| 112 | kv, err := db.kvc.List(context.Background(), key) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | if kv != nil { |
| 117 | return kv, nil |
| 118 | } |
| 119 | return nil, errors.New("Value not found") |
| 120 | } |
| 121 | |
| 122 | // OLT specific database items |
| 123 | |
| 124 | // GetOlt to get olt info |
| 125 | func (db *Database) GetOlt(deviceID string) (string, error) { |
| 126 | key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID) |
| 127 | return db.Get(key) |
| 128 | } |
| 129 | |
| 130 | // PutOlt to add olt info |
| 131 | func (db *Database) PutOlt(deviceID string, value string) error { |
| 132 | key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID) |
| 133 | return db.kvc.Put(context.Background(), key, value) |
| 134 | } |
| 135 | |
| 136 | // DelOlt to delete olt info |
| 137 | func (db *Database) DelOlt(deviceID string) error { |
| 138 | key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID) |
| 139 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 140 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 141 | return err |
| 142 | } |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | // Flows specific database actions |
| 147 | |
| 148 | // PutFlow to add flow |
| 149 | func (db *Database) PutFlow(deviceID string, flowID uint64, value string) error { |
| 150 | key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10) |
| 151 | return db.kvc.Put(context.Background(), key, value) |
| 152 | } |
| 153 | |
| 154 | // GetFlow to get flow |
| 155 | func (db *Database) GetFlow(deviceID string, flowID uint64) (string, error) { |
| 156 | key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10) |
| 157 | return db.Get(key) |
| 158 | } |
| 159 | |
| 160 | // GetFlows to get multiple flows |
| 161 | func (db *Database) GetFlows(deviceID string) (map[string]*kvstore.KVPair, error) { |
| 162 | key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) |
| 163 | return db.List(key) |
| 164 | } |
| 165 | |
| 166 | // DelFlow to delete flow |
| 167 | func (db *Database) DelFlow(deviceID string, flowID uint64) error { |
| 168 | key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10) |
| 169 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 170 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 171 | return err |
| 172 | } |
| 173 | return nil |
| 174 | } |
| 175 | |
| 176 | // Group specific database actions |
| 177 | |
| 178 | // PutGroup to add group info |
| 179 | func (db *Database) PutGroup(deviceID string, groupID uint32, value string) error { |
| 180 | key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10) |
| 181 | return db.kvc.Put(context.Background(), key, value) |
| 182 | } |
| 183 | |
| 184 | // GetGroup to get group info |
| 185 | func (db *Database) GetGroup(deviceID string, groupID uint32) (string, error) { |
| 186 | key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10) |
| 187 | return db.Get(key) |
| 188 | } |
| 189 | |
| 190 | // GetGroups to get multiple group info |
| 191 | func (db *Database) GetGroups(deviceID string) (map[string]*kvstore.KVPair, error) { |
| 192 | key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) |
| 193 | logger.Infow(ctx, "key", log.Fields{"Key": key}) |
| 194 | return db.List(key) |
| 195 | } |
| 196 | |
| 197 | // DelGroup to delete group info |
| 198 | func (db *Database) DelGroup(deviceID string, groupID uint32) error { |
| 199 | key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10) |
| 200 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 201 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 202 | return err |
| 203 | } |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | // DelAllGroup to delete all group info |
| 208 | func (db *Database) DelAllGroup(deviceID string) error { |
| 209 | key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) |
| 210 | if err := db.DeleteAllUnderHashKey(key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 211 | logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 212 | return err |
| 213 | } |
| 214 | logger.Infow(ctx, "Deleting all the groups for device", log.Fields{"device": deviceID}) |
| 215 | return nil |
| 216 | } |
| 217 | |
| 218 | // DelAllPorts to delete all ports info |
| 219 | func (db *Database) DelAllPorts(device string) error { |
| 220 | key := fmt.Sprintf(GetKeyPath(DevicePortPath), device) |
| 221 | if err := db.DeleteAllUnderHashKey(key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 222 | logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 223 | return err |
| 224 | } |
| 225 | logger.Infow(ctx, "Deleting all the ports for device", log.Fields{"device": device}) |
| 226 | return nil |
| 227 | } |
| 228 | |
| 229 | // Ports specific database actions |
| 230 | |
| 231 | // PutPort to add port info |
| 232 | func (db *Database) PutPort(deviceID string, portID uint32, value string) error { |
| 233 | key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10) |
| 234 | return db.kvc.Put(context.Background(), key, value) |
| 235 | } |
| 236 | |
| 237 | // GetPort to get port info |
| 238 | func (db *Database) GetPort(deviceID string, portID uint32) (string, error) { |
| 239 | key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10) |
| 240 | return db.Get(key) |
| 241 | } |
| 242 | |
| 243 | // GetPorts to get multiple ports info |
| 244 | func (db *Database) GetPorts(deviceID string) (map[string]*kvstore.KVPair, error) { |
| 245 | key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) |
| 246 | return db.List(key) |
| 247 | } |
| 248 | |
| 249 | // DelPort to delete port info |
| 250 | func (db *Database) DelPort(deviceID string, portID uint32) error { |
| 251 | key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10) |
| 252 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 253 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 254 | return err |
| 255 | } |
| 256 | return nil |
| 257 | } |
| 258 | |
| 259 | // Device meter specific database actions |
| 260 | |
| 261 | // PutDeviceMeter to add device meter info |
| 262 | func (db *Database) PutDeviceMeter(deviceID string, meterID uint32, value string) error { |
| 263 | key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10) |
| 264 | return db.kvc.Put(context.Background(), key, value) |
| 265 | } |
| 266 | |
| 267 | // GetDeviceMeter to get device meter info |
| 268 | func (db *Database) GetDeviceMeter(deviceID string, meterID uint32) (string, error) { |
| 269 | key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10) |
| 270 | return db.Get(key) |
| 271 | } |
| 272 | |
| 273 | // GetDeviceMeters to get multiple device meter info |
| 274 | func (db *Database) GetDeviceMeters(deviceID string) (map[string]*kvstore.KVPair, error) { |
| 275 | key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) |
| 276 | return db.List(key) |
| 277 | } |
| 278 | |
| 279 | // DelDeviceMeter to delete device meter info |
| 280 | func (db *Database) DelDeviceMeter(deviceID string, meterID uint32) error { |
| 281 | key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10) |
| 282 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 283 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 284 | return err |
| 285 | } |
| 286 | return nil |
| 287 | } |
| 288 | |
| 289 | // Service specific database actions |
| 290 | |
| 291 | // GetServices to get multiple services info |
| 292 | func (db *Database) GetServices() (map[string]*kvstore.KVPair, error) { |
| 293 | key := GetKeyPath(ServicePath) |
| 294 | return db.List(key) |
| 295 | } |
| 296 | |
| 297 | // GetService to get service info |
| 298 | func (db *Database) GetService(name string) (string, error) { |
| 299 | key := GetKeyPath(ServicePath) + name |
| 300 | return db.Get(key) |
| 301 | } |
| 302 | |
| 303 | // PutService to add service info |
| 304 | func (db *Database) PutService(name string, value string) error { |
| 305 | key := GetKeyPath(ServicePath) + name |
| 306 | return db.kvc.Put(context.Background(), key, value) |
| 307 | } |
| 308 | |
| 309 | // DelService to delete service info |
| 310 | func (db *Database) DelService(name string) error { |
| 311 | key := GetKeyPath(ServicePath) + name |
| 312 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 313 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 314 | return err |
| 315 | } |
| 316 | return nil |
| 317 | } |
| 318 | |
| 319 | // Virtual networks specific database actions |
| 320 | |
| 321 | // GetVnets to get multiple vnets info |
| 322 | func (db *Database) GetVnets() (map[string]*kvstore.KVPair, error) { |
| 323 | key := GetKeyPath(VnetPath) |
| 324 | return db.List(key) |
| 325 | } |
| 326 | |
| 327 | // GetVnet to get vnet info |
| 328 | func (db *Database) GetVnet(name string) (string, error) { |
| 329 | key := GetKeyPath(VnetPath) + name |
| 330 | return db.Get(key) |
| 331 | } |
| 332 | |
| 333 | // PutVnet to add vnet info |
| 334 | func (db *Database) PutVnet(name string, value string) error { |
| 335 | key := GetKeyPath(VnetPath) + name |
| 336 | return db.kvc.Put(context.Background(), key, value) |
| 337 | } |
| 338 | |
| 339 | // DelVnet to delete vnet info |
| 340 | func (db *Database) DelVnet(name string) error { |
| 341 | key := GetKeyPath(VnetPath) + name |
| 342 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 343 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 344 | return err |
| 345 | } |
| 346 | return nil |
| 347 | } |
| 348 | |
| 349 | // Virtual networks on ports specific database actions |
| 350 | |
| 351 | // GetVpvs to get multiple vpvs info |
| 352 | func (db *Database) GetVpvs() (map[string]*kvstore.KVPair, error) { |
| 353 | key := GetKeyPath(VpvPath) |
| 354 | return db.List(key) |
| 355 | } |
| 356 | |
| 357 | // GetVpv to get vpv info |
| 358 | func (db *Database) GetVpv(port string, SVlan uint16, CVlan uint16, UniVlan uint16) (string, error) { |
| 359 | name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan) |
| 360 | key := GetKeyPath(VpvPath) + name |
| 361 | return db.Get(key) |
| 362 | } |
| 363 | |
| 364 | // PutVpv to add vpv info |
| 365 | func (db *Database) PutVpv(port string, SVlan uint16, CVlan uint16, UniVlan uint16, value string) error { |
| 366 | name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan) |
| 367 | key := GetKeyPath(VpvPath) + name |
| 368 | return db.kvc.Put(context.Background(), key, value) |
| 369 | } |
| 370 | |
| 371 | // DelVpv to delete vpv info |
| 372 | func (db *Database) DelVpv(port string, SVlan uint16, CVlan uint16, UniVlan uint16) error { |
| 373 | name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan) |
| 374 | key := GetKeyPath(VpvPath) + name |
| 375 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 376 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 377 | return err |
| 378 | } |
| 379 | return nil |
| 380 | } |
| 381 | |
| 382 | // Virtual networks on ports specific database actions |
| 383 | |
| 384 | // GetMvlans to get multiple mvlans info |
| 385 | func (db *Database) GetMvlans() (map[string]*kvstore.KVPair, error) { |
| 386 | key := GetKeyPath(MvlanPath) |
| 387 | return db.List(key) |
| 388 | } |
| 389 | |
| 390 | // GetMvlan to get mvlan info |
| 391 | func (db *Database) GetMvlan(mvlan uint16) (string, error) { |
| 392 | name := strconv.FormatInt(int64(mvlan), 10) |
| 393 | key := GetKeyPath(MvlanPath) + name |
| 394 | return db.Get(key) |
| 395 | } |
| 396 | |
| 397 | // PutMvlan to add mvlan info |
| 398 | func (db *Database) PutMvlan(mvlan uint16, value string) error { |
| 399 | name := strconv.FormatInt(int64(mvlan), 10) |
| 400 | key := GetKeyPath(MvlanPath) + name |
| 401 | return db.kvc.Put(context.Background(), key, value) |
| 402 | } |
| 403 | |
| 404 | // DelMvlan to delete mvlan info |
| 405 | func (db *Database) DelMvlan(mvlan uint16) error { |
| 406 | name := strconv.FormatInt(int64(mvlan), 10) |
| 407 | key := GetKeyPath(MvlanPath) + name |
| 408 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 409 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 410 | return err |
| 411 | } |
| 412 | return nil |
| 413 | } |
| 414 | |
| 415 | // database specific actions on IGMP config |
| 416 | |
| 417 | // DelIGMPCfg to delete icmp config |
| 418 | func (db *Database) DelIGMPCfg() error { |
| 419 | key := GetKeyPath(IgmpConfPath) |
| 420 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 421 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 422 | return err |
| 423 | } |
| 424 | return nil |
| 425 | } |
| 426 | |
| 427 | // database specific actions on IGMP Profile |
| 428 | |
| 429 | // GetIgmpProfiles to get multiple igmp profile info |
| 430 | func (db *Database) GetIgmpProfiles() (map[string]*kvstore.KVPair, error) { |
| 431 | key := GetKeyPath(IgmpProfPath) |
| 432 | return db.List(key) |
| 433 | } |
| 434 | |
| 435 | // GetIgmpProfile to get igmp profile info |
| 436 | func (db *Database) GetIgmpProfile(name string) (string, error) { |
| 437 | key := GetKeyPath(IgmpProfPath) + name |
| 438 | return db.Get(key) |
| 439 | } |
| 440 | |
| 441 | // PutIgmpProfile to put igmp profile info |
| 442 | func (db *Database) PutIgmpProfile(name string, value string) error { |
| 443 | key := GetKeyPath(IgmpProfPath) + name |
| 444 | return db.kvc.Put(context.Background(), key, value) |
| 445 | } |
| 446 | |
| 447 | // DelIgmpProfile to delete igmp profile |
| 448 | func (db *Database) DelIgmpProfile(name string) error { |
| 449 | key := GetKeyPath(IgmpProfPath) + name |
| 450 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 451 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 452 | return err |
| 453 | } |
| 454 | return nil |
| 455 | } |
| 456 | |
| 457 | // database specific actions on Mcast config Info |
| 458 | |
| 459 | // GetMcastConfigs to get multiple mcast config info |
| 460 | func (db *Database) GetMcastConfigs() (map[string]*kvstore.KVPair, error) { |
| 461 | key := GetKeyPath(McastConfigPath) |
| 462 | return db.List(key) |
| 463 | } |
| 464 | |
| 465 | // GetMcastConfig to get igmp profile info |
| 466 | func (db *Database) GetMcastConfig(name string) (string, error) { |
| 467 | key := GetKeyPath(McastConfigPath) + name |
| 468 | return db.Get(key) |
| 469 | } |
| 470 | |
| 471 | // PutMcastConfig to put igmp profile info |
| 472 | func (db *Database) PutMcastConfig(name string, value string) error { |
| 473 | key := GetKeyPath(McastConfigPath) + name |
| 474 | return db.kvc.Put(context.Background(), key, value) |
| 475 | } |
| 476 | |
| 477 | // DelMcastConfig to delete igmp profile |
| 478 | func (db *Database) DelMcastConfig(name string) error { |
| 479 | key := GetKeyPath(McastConfigPath) + name |
| 480 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 481 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 482 | return err |
| 483 | } |
| 484 | return nil |
| 485 | } |
| 486 | |
| 487 | // database specific actions on health |
| 488 | |
| 489 | // GetHealth to get health info |
| 490 | func (db *Database) GetHealth() (string, error) { |
| 491 | key := GetKeyPath(HealthPath) |
| 492 | return db.Get(key) |
| 493 | } |
| 494 | |
| 495 | // PutHealth to add health info |
| 496 | func (db *Database) PutHealth(value string) error { |
| 497 | key := GetKeyPath(HealthPath) |
| 498 | return db.kvc.Put(context.Background(), key, value) |
| 499 | } |
| 500 | |
| 501 | // DelHealth to delete health info |
| 502 | func (db *Database) DelHealth() error { |
| 503 | key := GetKeyPath(HealthPath) |
| 504 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 505 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 506 | return err |
| 507 | } |
| 508 | return nil |
| 509 | } |
| 510 | |
| 511 | // Meters |
| 512 | |
| 513 | // GetMeters to get multiple meters info |
| 514 | func (db *Database) GetMeters() (map[string]*kvstore.KVPair, error) { |
| 515 | key := GetKeyPath(MeterPath) |
| 516 | return db.List(key) |
| 517 | } |
| 518 | |
| 519 | // GetMeter to get meter info |
| 520 | func (db *Database) GetMeter(name string) (string, error) { |
| 521 | key := GetKeyPath(MeterPath) + name |
| 522 | return db.Get(key) |
| 523 | } |
| 524 | |
| 525 | // PutMeter to add meter info |
| 526 | func (db *Database) PutMeter(name string, value string) error { |
| 527 | key := GetKeyPath(MeterPath) + name |
| 528 | return db.kvc.Put(context.Background(), key, value) |
| 529 | } |
| 530 | |
| 531 | // DelMeter to delete meter info |
| 532 | func (db *Database) DelMeter(name string) error { |
| 533 | key := GetKeyPath(MeterPath) + name |
| 534 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 535 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 536 | return err |
| 537 | } |
| 538 | return nil |
| 539 | } |
| 540 | |
| 541 | // DelAllMeter to delete meter info |
| 542 | func (db *Database) DelAllMeter(device string) error { |
| 543 | key := GetKeyPath(DevicePath) + device + "/" + MeterPath |
| 544 | if err := db.DeleteAllUnderHashKey(key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 545 | logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 546 | return err |
| 547 | } |
| 548 | logger.Infow(ctx, "Deleting all the meters for device", log.Fields{"device": device}) |
| 549 | return nil |
| 550 | } |
| 551 | |
| 552 | // IGMP groups |
| 553 | |
| 554 | // GetIgmpGroups to get multiple igmp groups info |
| 555 | func (db *Database) GetIgmpGroups() (map[string]*kvstore.KVPair, error) { |
| 556 | key := GetKeyPath(IgmpGroupPath) |
| 557 | return db.List(key) |
| 558 | } |
| 559 | |
| 560 | // GetIgmpGroup to get igmp group info |
| 561 | func (db *Database) GetIgmpGroup(id string) (string, error) { |
| 562 | key := GetKeyPath(IgmpGroupPath) + id |
| 563 | return db.Get(key) |
| 564 | } |
| 565 | |
| 566 | // PutIgmpGroup to add igmp group info |
| 567 | func (db *Database) PutIgmpGroup(id string, value string) error { |
| 568 | key := GetKeyPath(IgmpGroupPath) + id |
| 569 | return db.kvc.Put(context.Background(), key, value) |
| 570 | } |
| 571 | |
| 572 | // DelIgmpGroup to delete igmp group info |
| 573 | func (db *Database) DelIgmpGroup(id string) error { |
| 574 | key := GetKeyPath(IgmpGroupPath) + id |
| 575 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 576 | logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 577 | return err |
| 578 | } |
| 579 | return nil |
| 580 | } |
| 581 | |
| 582 | // IGMP group devices |
| 583 | |
| 584 | // GetAllIgmpDevices to get multiple igmp devices info |
| 585 | func (db *Database) GetAllIgmpDevices() (map[string]*kvstore.KVPair, error) { |
| 586 | key := GetKeyPath(IgmpDevicePath) |
| 587 | return db.List(key) |
| 588 | } |
| 589 | |
| 590 | // GetPrevIgmpDevices to get previous igmp devices |
| 591 | func (db *Database) GetPrevIgmpDevices(mvlan of.VlanType, gid string) (map[string]*kvstore.KVPair, error) { |
| 592 | key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" |
| 593 | return db.List(key) |
| 594 | } |
| 595 | |
| 596 | // GetIgmpDevices to get igmp devices |
| 597 | func (db *Database) GetIgmpDevices(mvlan of.VlanType, gid string, gip net.IP) (map[string]*kvstore.KVPair, error) { |
| 598 | key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" |
| 599 | return db.List(key) |
| 600 | } |
| 601 | |
| 602 | // GetIgmpDevice to get igmp device |
| 603 | func (db *Database) GetIgmpDevice(mvlan of.VlanType, gid string, gip net.IP, device string) (string, error) { |
| 604 | key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device |
| 605 | return db.Get(key) |
| 606 | } |
| 607 | |
| 608 | // PutIgmpDevice to add igmp device |
| 609 | func (db *Database) PutIgmpDevice(mvlan of.VlanType, gid string, gip net.IP, device string, value string) error { |
| 610 | key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device |
| 611 | return db.kvc.Put(context.Background(), key, value) |
| 612 | } |
| 613 | |
| 614 | // DelIgmpDevice to delete igmp device |
| 615 | func (db *Database) DelIgmpDevice(mvlan of.VlanType, gid string, gip net.IP, device string) error { |
| 616 | key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device |
| 617 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 618 | logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 619 | return err |
| 620 | } |
| 621 | return nil |
| 622 | } |
| 623 | |
| 624 | // IGMP group channels |
| 625 | |
| 626 | // GetAllIgmpChannels to get all igmp channels |
| 627 | func (db *Database) GetAllIgmpChannels() (map[string]*kvstore.KVPair, error) { |
| 628 | key := GetKeyPath(IgmpChannelPath) |
| 629 | return db.List(key) |
| 630 | } |
| 631 | |
| 632 | // GetPrevIgmpChannels to get previous igmp channels |
| 633 | func (db *Database) GetPrevIgmpChannels(gName, device string) (map[string]*kvstore.KVPair, error) { |
| 634 | key := GetKeyPath(IgmpChannelPath) + gName + "/" + device + "/" |
| 635 | return db.List(key) |
| 636 | } |
| 637 | |
| 638 | // GetIgmpChannels to get multiple igmp channels |
| 639 | func (db *Database) GetIgmpChannels(mvlan of.VlanType, gName, device string) (map[string]*kvstore.KVPair, error) { |
| 640 | key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" |
| 641 | return db.List(key) |
| 642 | } |
| 643 | |
| 644 | // GetIgmpChannel to get igmp channel |
| 645 | func (db *Database) GetIgmpChannel(mvlan of.VlanType, gName string, device string, gip net.IP) (string, error) { |
| 646 | key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String() |
| 647 | return db.Get(key) |
| 648 | } |
| 649 | |
| 650 | // PutIgmpChannel to add igmp channel info |
| 651 | func (db *Database) PutIgmpChannel(mvlan of.VlanType, gName string, device string, gip net.IP, value string) error { |
| 652 | key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String() |
| 653 | return db.kvc.Put(context.Background(), key, value) |
| 654 | } |
| 655 | |
| 656 | // DelIgmpChannel to delete igmp channel info |
| 657 | func (db *Database) DelIgmpChannel(mvlan of.VlanType, gName string, device string, gip net.IP) error { |
| 658 | key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String() |
| 659 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 660 | logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 661 | return err |
| 662 | } |
| 663 | return nil |
| 664 | } |
| 665 | |
| 666 | // IGMP group receivers |
| 667 | |
| 668 | // GetAllIgmpRcvrs to get all igmp receivers info |
| 669 | func (db *Database) GetAllIgmpRcvrs() (map[string]*kvstore.KVPair, error) { |
| 670 | key := GetKeyPath(IgmpPortPath) |
| 671 | return db.List(key) |
| 672 | } |
| 673 | |
| 674 | // GetPrevIgmpRcvrs to get previous igmp receivers info |
| 675 | func (db *Database) GetPrevIgmpRcvrs(gip net.IP, device string) (map[string]*kvstore.KVPair, error) { |
| 676 | key := GetKeyPath(IgmpPortPath) + gip.String() + "/" + device + "/" |
| 677 | return db.List(key) |
| 678 | } |
| 679 | |
| 680 | // GetIgmpRcvrs to get multiple igmp receivers info |
| 681 | func (db *Database) GetIgmpRcvrs(mvlan of.VlanType, gip net.IP, device string) (map[string]*kvstore.KVPair, error) { |
| 682 | key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" |
| 683 | return db.List(key) |
| 684 | } |
| 685 | |
| 686 | // GetIgmpRcvr to get igmp receiver info |
| 687 | func (db *Database) GetIgmpRcvr(mvlan of.VlanType, gip net.IP, device string, rcvr string) (string, error) { |
| 688 | key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr |
| 689 | return db.Get(key) |
| 690 | } |
| 691 | |
| 692 | // PutIgmpRcvr to add igmp receiver info |
| 693 | func (db *Database) PutIgmpRcvr(mvlan of.VlanType, gip net.IP, device string, rcvr string, value string) error { |
| 694 | key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr |
| 695 | return db.kvc.Put(context.Background(), key, value) |
| 696 | } |
| 697 | |
| 698 | // DelIgmpRcvr to delete igmp receiver info |
| 699 | func (db *Database) DelIgmpRcvr(mvlan of.VlanType, gip net.IP, device string, rcvr string) error { |
| 700 | key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr |
| 701 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 702 | logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 703 | return err |
| 704 | } |
| 705 | return nil |
| 706 | } |
| 707 | |
| 708 | // DelAllIgmpRcvr to delete all igmp receiver info |
| 709 | func (db *Database) DelAllIgmpRcvr(mvlan of.VlanType, gip net.IP, device string) error { |
| 710 | key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" |
| 711 | if err := db.DeleteAll(key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 712 | logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 713 | return err |
| 714 | } |
| 715 | return nil |
| 716 | } |
| 717 | |
| 718 | // DelAllRoutesForDevice to delete all routes for device |
| 719 | func (db *Database) DelAllRoutesForDevice(device string) error { |
| 720 | /* service/vgc/v1/devices/<deviceID>/flows/ */ |
| 721 | logger.Infow(ctx, "Deleting all the flows for device", log.Fields{"device": device}) |
| 722 | key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), device) |
| 723 | if err := db.DeleteAllUnderHashKey(key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 724 | logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 725 | return err |
| 726 | } |
| 727 | return nil |
| 728 | } |
| 729 | |
| 730 | // PutNbDevicePort to add device port info |
| 731 | func (db *Database) PutNbDevicePort(device string, ponPortID uint32, value string) { |
| 732 | key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID) |
| 733 | |
| 734 | if err := db.kvc.Put(context.Background(), key, value); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 735 | logger.Warnw(ctx, "Put Device Port failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | // DelNbDevicePort to delete device port |
| 740 | func (db *Database) DelNbDevicePort(device string, ponPortID uint32) { |
| 741 | key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID) |
| 742 | |
| 743 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 744 | logger.Warnw(ctx, "Delete Device Port failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | // GetAllNbPorts to get all ports info |
| 749 | func (db *Database) GetAllNbPorts(deviceID string) (map[string]*kvstore.KVPair, error) { |
| 750 | key := GetKeyPath(NbDevicePath) + deviceID + "/pon-port/" |
| 751 | return db.List(key) |
| 752 | } |
| 753 | |
| 754 | //Functions for migration database |
| 755 | |
| 756 | // GetMigrationInfo to get migration info |
| 757 | func (db *Database) GetMigrationInfo() (string, error) { |
| 758 | key := GetKeyPath(MigrationInfoPath) |
| 759 | return db.Get(key) |
| 760 | } |
| 761 | |
| 762 | // PutMigrationInfo to add migration info |
| 763 | func (db *Database) PutMigrationInfo(value string) error { |
| 764 | key := GetKeyPath(MigrationInfoPath) |
| 765 | return db.kvc.Put(context.Background(), key, value) |
| 766 | } |
| 767 | |
| 768 | // DelMigrationInfo to delete migration info |
| 769 | func (db *Database) DelMigrationInfo() error { |
| 770 | key := GetKeyPath(MigrationInfoPath) |
| 771 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 772 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 773 | return err |
| 774 | } |
| 775 | return nil |
| 776 | } |
| 777 | |
| 778 | //PON counters |
| 779 | |
| 780 | // GetAllPonCounters to get all pon counters info |
| 781 | func (db *Database) GetAllPonCounters(device string) (map[string]*kvstore.KVPair, error) { |
| 782 | key := GetKeyPath(PonCounterPath) + device |
| 783 | return db.List(key) |
| 784 | } |
| 785 | |
| 786 | // GetPonCounter to get pon counter info |
| 787 | func (db *Database) GetPonCounter(device, ponID string) (string, error) { |
| 788 | key := GetKeyPath(PonCounterPath) + device + "/" + ponID |
| 789 | return db.Get(key) |
| 790 | } |
| 791 | |
| 792 | // PutPonCounter to add pon counter info |
| 793 | func (db *Database) PutPonCounter(device, ponID, value string) error { |
| 794 | key := GetKeyPath(PonCounterPath) + device + "/" + ponID |
| 795 | return db.kvc.Put(context.Background(), key, value) |
| 796 | } |
| 797 | |
| 798 | // DelPonCounter to delete pon counter info |
| 799 | func (db *Database) DelPonCounter(device, ponID string) error { |
| 800 | key := GetKeyPath(PonCounterPath) + device + "/" + ponID |
| 801 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 802 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 803 | return err |
| 804 | } |
| 805 | return nil |
| 806 | } |
| 807 | |
| 808 | //PON Channel counters |
| 809 | |
| 810 | // GetAllPonChannelCounters to get all pon channel counters |
| 811 | func (db *Database) GetAllPonChannelCounters(device, ponID string) (map[string]*kvstore.KVPair, error) { |
| 812 | key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath |
| 813 | return db.List(key) |
| 814 | } |
| 815 | |
| 816 | // GetPonChannelCounter to get pon channel counter |
| 817 | func (db *Database) GetPonChannelCounter(device, ponID, channel string) (string, error) { |
| 818 | key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel |
| 819 | return db.Get(key) |
| 820 | } |
| 821 | |
| 822 | // PutPonChannelCounter to add pon channel counter |
| 823 | func (db *Database) PutPonChannelCounter(device, ponID, channel, value string) error { |
| 824 | key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel |
| 825 | return db.kvc.Put(context.Background(), key, value) |
| 826 | } |
| 827 | |
| 828 | // DelPonChannelCounter to delete pon channel counter |
| 829 | func (db *Database) DelPonChannelCounter(device, ponID, channel string) error { |
| 830 | key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel |
| 831 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 832 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 833 | return err |
| 834 | } |
| 835 | return nil |
| 836 | } |
| 837 | |
| 838 | // DelAllPONCounters to delete all pon channel counters |
| 839 | func (db *Database) DelAllPONCounters(device string) error { |
| 840 | key := GetKeyPath(PonCounterPath) + device + "/" |
| 841 | return db.DeleteAll(key) |
| 842 | } |
| 843 | |
| 844 | // DelPONCounters to delete pon counters |
| 845 | func (db *Database) DelPONCounters(device string, ponID string) { |
| 846 | key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" |
| 847 | if err := db.DeleteAll(key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 848 | logger.Warnw(ctx, "Delete Pon counters failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 849 | } |
| 850 | //DeletePonCounter(device, ponID) |
| 851 | } |
| 852 | |
| 853 | // PutOltIgmpCounters to add Olt Igmp counter info |
| 854 | func (db *Database) PutOltIgmpCounters(device, value string) error { |
| 855 | key := GetKeyPath(OltIgmpCounterPath) + device |
| 856 | return db.kvc.Put(context.Background(), key, value) |
| 857 | } |
| 858 | |
| 859 | // GetOltIgmpCounter to get Olt Igmp counter info |
| 860 | func (db *Database) GetOltIgmpCounter(device string) (string, error) { |
| 861 | key := GetKeyPath(OltIgmpCounterPath) + device |
| 862 | return db.Get(key) |
| 863 | } |
| 864 | |
| 865 | //Service Channel counters |
| 866 | |
| 867 | // GetAllServiceChannelCounters to get all service channel counters info |
| 868 | func (db *Database) GetAllServiceChannelCounters(serviceName string) (map[string]*kvstore.KVPair, error) { |
| 869 | key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath |
| 870 | return db.List(key) |
| 871 | } |
| 872 | |
| 873 | // GetServiceChannelCounter to get service channel counter info |
| 874 | func (db *Database) GetServiceChannelCounter(serviceName, channel string) (string, error) { |
| 875 | key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel |
| 876 | return db.Get(key) |
| 877 | } |
| 878 | |
| 879 | // PutServiceChannelCounter to add service channel counter |
| 880 | func (db *Database) PutServiceChannelCounter(serviceName, channel, value string) error { |
| 881 | key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel |
| 882 | return db.kvc.Put(context.Background(), key, value) |
| 883 | } |
| 884 | |
| 885 | // DelServiceChannelCounter to delete service channel counter |
| 886 | func (db *Database) DelServiceChannelCounter(serviceName, channel string) error { |
| 887 | key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel |
| 888 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 889 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 890 | return err |
| 891 | } |
| 892 | return nil |
| 893 | } |
| 894 | |
| 895 | // DelAllServiceChannelCounter to delete all service channel counter |
| 896 | func (db *Database) DelAllServiceChannelCounter(serviceName string) error { |
| 897 | key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath |
| 898 | return db.DeleteAllUnderHashKey(key) |
| 899 | } |
| 900 | |
| 901 | // OltExists to know if the ONU is added to the database |
| 902 | func (db *Database) OltExists(deviceID string) bool { |
| 903 | if _, err := db.GetOlt(deviceID); err != nil { |
| 904 | return false |
| 905 | } |
| 906 | return true |
| 907 | |
| 908 | } |
| 909 | |
| 910 | // PutFlowHash to add flowhash for the device |
| 911 | func (db *Database) PutFlowHash(deviceID string, value string) error { |
| 912 | key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) |
| 913 | return db.kvc.Put(context.Background(), key, value) |
| 914 | } |
| 915 | |
| 916 | // GetFlowHash gets the flow hash for the device |
| 917 | func (db *Database) GetFlowHash(deviceID string) (string, error) { |
| 918 | key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) |
| 919 | return db.Get(key) |
| 920 | } |
| 921 | |
| 922 | // PutPortAlarmProfile to add port alarm profile |
| 923 | func (db *Database) PutPortAlarmProfile(portAlarmProfileID string, value string) { |
| 924 | key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID) |
| 925 | if err := db.kvc.Put(context.Background(), key, value); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 926 | logger.Warnw(ctx, "Put PortAlarmProfile failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
| 930 | // DelPortAlarmProfile to delete port alarm profile |
| 931 | func (db *Database) DelPortAlarmProfile(portAlarmProfileID string) { |
| 932 | key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID) |
| 933 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 934 | logger.Warnw(ctx, "Delete PortAlarmProfile failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
| 938 | // GetPortAlarmProfile to get port alarm profile |
| 939 | func (db *Database) GetPortAlarmProfile(portAlarmProfileID string) (map[string]*kvstore.KVPair, error) { |
| 940 | key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID) |
| 941 | return db.List(key) |
| 942 | } |
| 943 | |
| 944 | // PutPortAlarmData to add port alarm data |
| 945 | func (db *Database) PutPortAlarmData(deviceID string, portID uint32, value string) { |
| 946 | key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID) |
| 947 | if err := db.kvc.Put(context.Background(), key, value); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 948 | logger.Warnw(ctx, "Put PortAlarmData failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
| 952 | // DelPortAlarmData to delete port alarm data |
| 953 | func (db *Database) DelPortAlarmData(deviceID string, portID uint32) { |
| 954 | key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID) |
| 955 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 956 | logger.Warnw(ctx, "Delete PortAlarmData failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
| 960 | // GetPortAlarmData to get port alarm data |
| 961 | func (db *Database) GetPortAlarmData(deviceID string, portID uint32) (string, error) { |
| 962 | key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID) |
| 963 | return db.Get(key) |
| 964 | } |
| 965 | |
| 966 | // GetAllPortAlarmData to get port alarm data for all ports |
| 967 | func (db *Database) GetAllPortAlarmData(deviceID string) (map[string]*kvstore.KVPair, error) { |
| 968 | key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) |
| 969 | return db.List(key) |
| 970 | } |
| 971 | |
| 972 | // PutSubAlarmData to add subscriber alarm data |
| 973 | func (db *Database) PutSubAlarmData(deviceID string, portName string, value string) { |
| 974 | key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName) |
| 975 | if err := db.kvc.Put(context.Background(), key, value); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 976 | logger.Warnw(ctx, "Put Subscriber AlarmData failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | // DelSubAlarmData to delete subscriber alarm data |
| 981 | func (db *Database) DelSubAlarmData(deviceID string, portName string) { |
| 982 | key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName) |
| 983 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 984 | logger.Warnw(ctx, "Delete Subscriber AlarmData failed", log.Fields{"key": key}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
| 988 | // GetSubAlarmData to get subscriber alarm data |
| 989 | func (db *Database) GetSubAlarmData(deviceID string, portName string) (string, error) { |
| 990 | key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName) |
| 991 | return db.Get(key) |
| 992 | } |
| 993 | |
| 994 | // GetAllSubAlarmData to get sub alarm data for all subscribers |
| 995 | func (db *Database) GetAllSubAlarmData(deviceID string) (map[string]*kvstore.KVPair, error) { |
| 996 | key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) |
| 997 | return db.List(key) |
| 998 | } |
| 999 | |
| 1000 | // Migrate Service req specific database actions |
| 1001 | |
| 1002 | // PutMigrateServicesReq to add MigrateServicesReq info |
| 1003 | func (db *Database) PutMigrateServicesReq(deviceID string, vnet string, value string) error { |
| 1004 | key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet |
| 1005 | return db.kvc.Put(context.Background(), key, value) |
| 1006 | } |
| 1007 | |
| 1008 | // GetMigrateServicesReq to get MigrateServicesReq info |
| 1009 | func (db *Database) GetMigrateServicesReq(deviceID string, vnet string) (string, error) { |
| 1010 | key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet |
| 1011 | return db.Get(key) |
| 1012 | } |
| 1013 | |
| 1014 | // GetAllMigrateServicesReq to get multiple MigrateServicesReq info |
| 1015 | func (db *Database) GetAllMigrateServicesReq(deviceID string) (map[string]*kvstore.KVPair, error) { |
| 1016 | key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) |
| 1017 | return db.List(key) |
| 1018 | } |
| 1019 | |
| 1020 | // DelMigrateServicesReq to delete MigrateServicesReq info |
| 1021 | func (db *Database) DelMigrateServicesReq(deviceID string, vnet string) error { |
| 1022 | key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet |
| 1023 | if err := db.kvc.Delete(context.Background(), key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 1024 | logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1025 | return err |
| 1026 | } |
| 1027 | return nil |
| 1028 | } |
| 1029 | |
| 1030 | // DelAllMigrateServicesReq to delete all MigrateServicesReq info |
| 1031 | func (db *Database) DelAllMigrateServicesReq(deviceID string) error { |
| 1032 | key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) |
| 1033 | if err := db.DeleteAllUnderHashKey(key); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 1034 | logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1035 | return err |
| 1036 | } |
| 1037 | logger.Infow(ctx, "Deleting all the Update Vnet Requests for device", log.Fields{"device": deviceID}) |
| 1038 | return nil |
| 1039 | } |
| 1040 | |
| 1041 | func init() { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 1042 | // Setup this package so that it's log level can be modified at run time |
| 1043 | var err error |
| 1044 | logger, err = log.AddPackageWithDefaultParam() |
| 1045 | if err != nil { |
| 1046 | panic(err) |
| 1047 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1048 | } |