blob: 75f48021870cde7b22a7f91fe655fae7d1ab7356 [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.
Akash Sonia8246972023-01-03 10:37:08 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015// 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
20package database
21
22import (
23 "context"
24 "errors"
Akash Sonia8246972023-01-03 10:37:08 +053025 "fmt"
Naveen Sampath04696f72022-06-13 15:19:14 +053026 "net"
27 "strconv"
28 "time"
Naveen Sampath04696f72022-06-13 15:19:14 +053029
Akash Sonia8246972023-01-03 10:37:08 +053030 "voltha-go-controller/internal/pkg/errorcodes"
Naveen Sampath04696f72022-06-13 15:19:14 +053031 "voltha-go-controller/internal/pkg/of"
Tinoj Joseph1d108322022-07-13 10:07:39 +053032 "voltha-go-controller/log"
Akash Sonia8246972023-01-03 10:37:08 +053033
34 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
Naveen Sampath04696f72022-06-13 15:19:14 +053035)
36
37var logger log.CLogger
Naveen Sampath04696f72022-06-13 15:19:14 +053038
39// Database structure
40type Database struct {
41 storeType string
42 address string
43 //timeout uint32
Akash Sonia8246972023-01-03 10:37:08 +053044 kvc kvstore.Client
Naveen Sampath04696f72022-06-13 15:19:14 +053045}
46
47// Initialize the database module. The database module runs as a singleton
48// object and is initialized when the adapter is created.
Tinoj Joseph07cc5372022-07-18 22:53:51 +053049func Initialize(ctx context.Context, storeType string, address string, timeout int) (*Database, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +053050 var err error
51 var database Database
52 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
53 database.address = address
54 database.storeType = storeType
55 switch storeType {
56 case "redis":
57 database.kvc, err = kvstore.NewRedisClient(address, time.Duration(timeout), false)
58 return &database, err
59 }
60 return &database, errors.New("unsupported-kv-store")
61}
62
63// Utility function that retrieves value for a key. It is assumed that
64// the information is always a string and the data retrieved is returned
65// as a string
66
67// Put to add value to database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053068func (db *Database) Put(ctx context.Context, fullKeyPath, value string) error {
69 return db.kvc.Put(ctx, fullKeyPath, value)
Naveen Sampath04696f72022-06-13 15:19:14 +053070}
71
72// Get to retrieve value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053073func (db *Database) Get(ctx context.Context, key string) (string, error) {
74 kv, err := db.kvc.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +053075 if err != nil {
76 return "", err
77 }
78 if kv != nil {
79 return string(kv.Value.([]byte)), nil
80 }
81 return "", errors.New("Value not found")
82}
83
84// Del to delete value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053085func (db *Database) Del(ctx context.Context, fullPath string) error {
86 if err := db.kvc.Delete(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +053087 logger.Errorw(ctx, "The path doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +053088 return err
89 }
90 return nil
91}
92
93// DeleteAll to delete all value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053094func (db *Database) DeleteAll(ctx context.Context, fullPath string) error {
95 if err := db.kvc.DeleteWithPrefix(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +053096 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +053097 return err
98 }
99 return nil
100}
101
102// DeleteAllUnderHashKey to delete all values under hash key
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530103func (db *Database) DeleteAllUnderHashKey(ctx context.Context, hashKeyPrefix string) error {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530104 kv, err := db.kvc.List(ctx, hashKeyPrefix)
105 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530106 logger.Errorw(ctx, "The key path doesn't exist", log.Fields{"key": hashKeyPrefix, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530107 return err
108 }
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530109 for key := range kv {
110 if err := db.kvc.Delete(ctx, key); err != nil {
111 logger.Errorw(ctx, "Delete key from DB Failed", log.Fields{"key": key, "Error": err})
112 }
113 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530114 return nil
115}
116
117// List to list the values
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530118func (db *Database) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
119 kv, err := db.kvc.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530120 if err != nil {
121 return nil, err
122 }
123 if kv != nil {
124 return kv, nil
125 }
126 return nil, errors.New("Value not found")
127}
128
129// OLT specific database items
130
131// GetOlt to get olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530132func (db *Database) GetOlt(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530133 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530134 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530135}
136
137// PutOlt to add olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530138func (db *Database) PutOlt(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530139 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530140 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530141}
142
143// DelOlt to delete olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530144func (db *Database) DelOlt(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530145 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530146 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530147 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530148 return err
149 }
150 return nil
151}
152
153// Flows specific database actions
154
155// PutFlow to add flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530156func (db *Database) PutFlow(ctx context.Context, deviceID string, flowID uint64, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530157 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530158 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530159}
160
161// GetFlow to get flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530162func (db *Database) GetFlow(ctx context.Context, deviceID string, flowID uint64) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530163 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530164 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530165}
166
167// GetFlows to get multiple flows
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530168func (db *Database) GetFlows(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530169 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530170 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530171}
172
173// DelFlow to delete flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530174func (db *Database) DelFlow(ctx context.Context, deviceID string, flowID uint64) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530175 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530176 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530177 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530178 return err
179 }
180 return nil
181}
182
183// Group specific database actions
184
185// PutGroup to add group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530186func (db *Database) PutGroup(ctx context.Context, deviceID string, groupID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530187 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530188 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530189}
190
191// GetGroup to get group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530192func (db *Database) GetGroup(ctx context.Context, deviceID string, groupID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530193 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530194 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530195}
196
197// GetGroups to get multiple group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530198func (db *Database) GetGroups(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530199 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
200 logger.Infow(ctx, "key", log.Fields{"Key": key})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530201 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530202}
203
204// DelGroup to delete group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530205func (db *Database) DelGroup(ctx context.Context, deviceID string, groupID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530206 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530207 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530208 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530209 return err
210 }
211 return nil
212}
213
214// DelAllGroup to delete all group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530215func (db *Database) DelAllGroup(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530216 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530217 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530218 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530219 return err
220 }
221 logger.Infow(ctx, "Deleting all the groups for device", log.Fields{"device": deviceID})
222 return nil
223}
224
225// DelAllPorts to delete all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530226func (db *Database) DelAllPorts(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530227 key := fmt.Sprintf(GetKeyPath(DevicePortPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530228 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530229 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530230 return err
231 }
232 logger.Infow(ctx, "Deleting all the ports for device", log.Fields{"device": device})
233 return nil
234}
235
236// Ports specific database actions
237
238// PutPort to add port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530239func (db *Database) PutPort(ctx context.Context, deviceID string, portID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530240 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530241 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530242}
243
244// GetPort to get port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530245func (db *Database) GetPort(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530246 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530247 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530248}
249
250// GetPorts to get multiple ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530251func (db *Database) GetPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530252 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530253 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530254}
255
256// DelPort to delete port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530257func (db *Database) DelPort(ctx context.Context, deviceID string, portID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530258 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530259 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530260 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530261 return err
262 }
263 return nil
264}
265
266// Device meter specific database actions
267
268// PutDeviceMeter to add device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530269func (db *Database) PutDeviceMeter(ctx context.Context, deviceID string, meterID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530270 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530271 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530272}
273
274// GetDeviceMeter to get device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530275func (db *Database) GetDeviceMeter(ctx context.Context, deviceID string, meterID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530276 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530277 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530278}
279
280// GetDeviceMeters to get multiple device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530281func (db *Database) GetDeviceMeters(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530282 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530283 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530284}
285
286// DelDeviceMeter to delete device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530287func (db *Database) DelDeviceMeter(ctx context.Context, deviceID string, meterID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530288 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530289 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530290 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530291 return err
292 }
293 return nil
294}
295
296// Service specific database actions
297
298// GetServices to get multiple services info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530299func (db *Database) GetServices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530300 key := GetKeyPath(ServicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530301 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530302}
303
304// GetService to get service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530305func (db *Database) GetService(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530306 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530307 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530308}
309
310// PutService to add service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530311func (db *Database) PutService(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530312 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530313 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530314}
315
316// DelService to delete service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530317func (db *Database) DelService(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530318 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530319 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530320 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530321 return err
322 }
323 return nil
324}
325
326// Virtual networks specific database actions
327
328// GetVnets to get multiple vnets info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530329func (db *Database) GetVnets(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530330 key := GetKeyPath(VnetPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530331 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530332}
333
334// GetVnet to get vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530335func (db *Database) GetVnet(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530336 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530337 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530338}
339
340// PutVnet to add vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530341func (db *Database) PutVnet(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530342 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530343 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530344}
345
346// DelVnet to delete vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530347func (db *Database) DelVnet(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530348 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530349 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530350 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530351 return err
352 }
353 return nil
354}
355
356// Virtual networks on ports specific database actions
357
358// GetVpvs to get multiple vpvs info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530359func (db *Database) GetVpvs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530360 key := GetKeyPath(VpvPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530361 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530362}
363
364// GetVpv to get vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530365func (db *Database) GetVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530366 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
367 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530368 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530369}
370
371// PutVpv to add vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530372func (db *Database) PutVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530373 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
374 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530375 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530376}
377
378// DelVpv to delete vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530379func (db *Database) DelVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530380 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
381 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530382 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530383 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530384 return err
385 }
386 return nil
387}
388
389// Virtual networks on ports specific database actions
390
391// GetMvlans to get multiple mvlans info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530392func (db *Database) GetMvlans(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530393 key := GetKeyPath(MvlanPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530394 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530395}
396
397// GetMvlan to get mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530398func (db *Database) GetMvlan(ctx context.Context, mvlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530399 name := strconv.FormatInt(int64(mvlan), 10)
400 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530401 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530402}
403
404// PutMvlan to add mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530405func (db *Database) PutMvlan(ctx context.Context, mvlan uint16, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530406 name := strconv.FormatInt(int64(mvlan), 10)
407 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530408 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530409}
410
411// DelMvlan to delete mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530412func (db *Database) DelMvlan(ctx context.Context, mvlan uint16) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530413 name := strconv.FormatInt(int64(mvlan), 10)
414 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530415 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530416 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530417 return err
418 }
419 return nil
420}
421
422// database specific actions on IGMP config
423
424// DelIGMPCfg to delete icmp config
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530425func (db *Database) DelIGMPCfg(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530426 key := GetKeyPath(IgmpConfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530427 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530428 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530429 return err
430 }
431 return nil
432}
433
434// database specific actions on IGMP Profile
435
436// GetIgmpProfiles to get multiple igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530437func (db *Database) GetIgmpProfiles(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530438 key := GetKeyPath(IgmpProfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530439 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530440}
441
442// GetIgmpProfile to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530443func (db *Database) GetIgmpProfile(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530444 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530445 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530446}
447
448// PutIgmpProfile to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530449func (db *Database) PutIgmpProfile(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530450 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530451 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530452}
453
454// DelIgmpProfile to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530455func (db *Database) DelIgmpProfile(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530456 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530457 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530458 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530459 return err
460 }
461 return nil
462}
463
464// database specific actions on Mcast config Info
465
466// GetMcastConfigs to get multiple mcast config info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530467func (db *Database) GetMcastConfigs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530468 key := GetKeyPath(McastConfigPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530469 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530470}
471
472// GetMcastConfig to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530473func (db *Database) GetMcastConfig(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530474 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530475 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530476}
477
478// PutMcastConfig to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530479func (db *Database) PutMcastConfig(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530480 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530481 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530482}
483
484// DelMcastConfig to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530485func (db *Database) DelMcastConfig(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530486 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530487 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530488 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530489 return err
490 }
491 return nil
492}
493
494// database specific actions on health
495
496// GetHealth to get health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530497func (db *Database) GetHealth(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530498 key := GetKeyPath(HealthPath)
Akash Sonia8246972023-01-03 10:37:08 +0530499 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530500}
501
502// PutHealth to add health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530503func (db *Database) PutHealth(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530504 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530505 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530506}
507
508// DelHealth to delete health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530509func (db *Database) DelHealth(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530510 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530511 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530512 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530513 return err
514 }
515 return nil
516}
517
518// Meters
519
520// GetMeters to get multiple meters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530521func (db *Database) GetMeters(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530522 key := GetKeyPath(MeterPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530523 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530524}
525
526// GetMeter to get meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530527func (db *Database) GetMeter(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530528 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530529 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530530}
531
532// PutMeter to add meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530533func (db *Database) PutMeter(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530534 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530535 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530536}
537
538// DelMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530539func (db *Database) DelMeter(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530540 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530541 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530542 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530543 return err
544 }
545 return nil
546}
547
548// DelAllMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530549func (db *Database) DelAllMeter(ctx context.Context, device string) error {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530550 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530551 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530552 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530553 return err
554 }
555 logger.Infow(ctx, "Deleting all the meters for device", log.Fields{"device": device})
556 return nil
557}
558
559// IGMP groups
560
561// GetIgmpGroups to get multiple igmp groups info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530562func (db *Database) GetIgmpGroups(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530563 key := GetKeyPath(IgmpGroupPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530564 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530565}
566
567// GetIgmpGroup to get igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530568func (db *Database) GetIgmpGroup(ctx context.Context, id string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530569 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530570 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530571}
572
573// PutIgmpGroup to add igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530574func (db *Database) PutIgmpGroup(ctx context.Context, id string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530575 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530576 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530577}
578
579// DelIgmpGroup to delete igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530580func (db *Database) DelIgmpGroup(ctx context.Context, id string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530581 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530582 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530583 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530584 return err
585 }
586 return nil
587}
588
589// IGMP group devices
590
591// GetAllIgmpDevices to get multiple igmp devices info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530592func (db *Database) GetAllIgmpDevices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530593 key := GetKeyPath(IgmpDevicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530594 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530595}
596
597// GetPrevIgmpDevices to get previous igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530598func (db *Database) GetPrevIgmpDevices(ctx context.Context, mvlan of.VlanType, gid string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530599 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530600 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530601}
602
603// GetIgmpDevices to get igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530604func (db *Database) GetIgmpDevices(ctx context.Context, mvlan of.VlanType, gid string, gip net.IP) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530605 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530606 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530607}
608
609// GetIgmpDevice to get igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530610func (db *Database) GetIgmpDevice(ctx context.Context, mvlan of.VlanType, gid string, gip net.IP, device string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530611 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530612 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530613}
614
615// PutIgmpDevice to add igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530616func (db *Database) PutIgmpDevice(ctx context.Context, mvlan of.VlanType, gid string, gip net.IP, device string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530617 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530618 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530619}
620
621// DelIgmpDevice to delete igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530622func (db *Database) DelIgmpDevice(ctx context.Context, mvlan of.VlanType, gid string, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530623 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530624 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530625 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530626 return err
627 }
628 return nil
629}
630
631// IGMP group channels
632
633// GetAllIgmpChannels to get all igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530634func (db *Database) GetAllIgmpChannels(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530635 key := GetKeyPath(IgmpChannelPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530636 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530637}
638
639// GetPrevIgmpChannels to get previous igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530640func (db *Database) GetPrevIgmpChannels(ctx context.Context, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530641 key := GetKeyPath(IgmpChannelPath) + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530642 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530643}
644
645// GetIgmpChannels to get multiple igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530646func (db *Database) GetIgmpChannels(ctx context.Context, mvlan of.VlanType, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530647 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530648 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530649}
650
651// GetIgmpChannel to get igmp channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530652func (db *Database) GetIgmpChannel(ctx context.Context, mvlan of.VlanType, gName string, device string, gip net.IP) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530653 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530654 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530655}
656
657// PutIgmpChannel to add igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530658func (db *Database) PutIgmpChannel(ctx context.Context, mvlan of.VlanType, gName string, device string, gip net.IP, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530659 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530660 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530661}
662
663// DelIgmpChannel to delete igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530664func (db *Database) DelIgmpChannel(ctx context.Context, mvlan of.VlanType, gName string, device string, gip net.IP) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530665 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530666 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530667 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530668 return err
669 }
670 return nil
671}
672
673// IGMP group receivers
674
675// GetAllIgmpRcvrs to get all igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530676func (db *Database) GetAllIgmpRcvrs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530677 key := GetKeyPath(IgmpPortPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530678 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530679}
680
681// GetPrevIgmpRcvrs to get previous igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530682func (db *Database) GetPrevIgmpRcvrs(ctx context.Context, gip net.IP, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530683 key := GetKeyPath(IgmpPortPath) + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530684 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530685}
686
687// GetIgmpRcvrs to get multiple igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530688func (db *Database) GetIgmpRcvrs(ctx context.Context, mvlan of.VlanType, gip net.IP, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530689 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530690 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530691}
692
693// GetIgmpRcvr to get igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530694func (db *Database) GetIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string, rcvr string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530695 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530696 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530697}
698
699// PutIgmpRcvr to add igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530700func (db *Database) PutIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string, rcvr string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530701 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530702 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530703}
704
705// DelIgmpRcvr to delete igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530706func (db *Database) DelIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string, rcvr string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530707 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530708 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530709 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530710 return err
711 }
712 return nil
713}
714
715// DelAllIgmpRcvr to delete all igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530716func (db *Database) DelAllIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530717 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530718 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530719 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530720 return err
721 }
722 return nil
723}
724
725// DelAllRoutesForDevice to delete all routes for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530726func (db *Database) DelAllRoutesForDevice(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530727 /* service/vgc/v1/devices/<deviceID>/flows/ */
728 logger.Infow(ctx, "Deleting all the flows for device", log.Fields{"device": device})
729 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530730 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530731 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530732 return err
733 }
734 return nil
735}
736
737// PutNbDevicePort to add device port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530738func (db *Database) PutNbDevicePort(ctx context.Context, device string, ponPortID uint32, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530739 key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID)
740
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530741 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530742 logger.Warnw(ctx, "Put Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530743 }
744}
745
Akash Sonia8246972023-01-03 10:37:08 +0530746// GetServices to get multiple services info
747func (db *Database) GetDeviceConfig(ctx context.Context) (map[string]*kvstore.KVPair, error) {
748 key := GetKeyPath(DeviceConfigPath)
749 return db.List(ctx, key)
750}
751
752// PutSBDeviceConfig to add device info
753func (db *Database) PutDeviceConfig(ctx context.Context, serialNum string, value string) error {
754 key := GetKeyPath(DeviceConfigPath) + serialNum
755
756 if err := db.kvc.Put(ctx, key, value); err != nil {
757 logger.Warnw(ctx, "Put Device Config failed", log.Fields{"key": key})
758 return errorcodes.ErrFailedToUpdateDB
759 }
760 return nil
761}
762
Naveen Sampath04696f72022-06-13 15:19:14 +0530763// DelNbDevicePort to delete device port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530764func (db *Database) DelNbDevicePort(ctx context.Context, device string, ponPortID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530765 key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID)
766
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530767 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530768 logger.Warnw(ctx, "Delete Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530769 }
770}
771
772// GetAllNbPorts to get all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530773func (db *Database) GetAllNbPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530774 key := GetKeyPath(NbDevicePath) + deviceID + "/pon-port/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530775 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530776}
777
778//Functions for migration database
779
780// GetMigrationInfo to get migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530781func (db *Database) GetMigrationInfo(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530782 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530783 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530784}
785
786// PutMigrationInfo to add migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530787func (db *Database) PutMigrationInfo(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530788 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530789 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530790}
791
792// DelMigrationInfo to delete migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530793func (db *Database) DelMigrationInfo(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530794 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530795 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530796 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530797 return err
798 }
799 return nil
800}
801
802//PON counters
803
804// GetAllPonCounters to get all pon counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530805func (db *Database) GetAllPonCounters(ctx context.Context, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530806 key := GetKeyPath(PonCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530807 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530808}
809
810// GetPonCounter to get pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530811func (db *Database) GetPonCounter(ctx context.Context, device, ponID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530812 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530813 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530814}
815
816// PutPonCounter to add pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530817func (db *Database) PutPonCounter(ctx context.Context, device, ponID, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530818 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530819 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530820}
821
822// DelPonCounter to delete pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530823func (db *Database) DelPonCounter(ctx context.Context, device, ponID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530824 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530825 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530826 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530827 return err
828 }
829 return nil
830}
831
832//PON Channel counters
833
834// GetAllPonChannelCounters to get all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530835func (db *Database) GetAllPonChannelCounters(ctx context.Context, device, ponID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530836 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530837 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530838}
839
840// GetPonChannelCounter to get pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530841func (db *Database) GetPonChannelCounter(ctx context.Context, device, ponID, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530842 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530843 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530844}
845
846// PutPonChannelCounter to add pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530847func (db *Database) PutPonChannelCounter(ctx context.Context, device, ponID, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530848 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530849 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530850}
851
852// DelPonChannelCounter to delete pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530853func (db *Database) DelPonChannelCounter(ctx context.Context, device, ponID, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530854 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530855 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530856 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530857 return err
858 }
859 return nil
860}
861
862// DelAllPONCounters to delete all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530863func (db *Database) DelAllPONCounters(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530864 key := GetKeyPath(PonCounterPath) + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530865 return db.DeleteAll(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530866}
867
868// DelPONCounters to delete pon counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530869func (db *Database) DelPONCounters(ctx context.Context, device string, ponID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530870 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530871 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530872 logger.Warnw(ctx, "Delete Pon counters failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530873 }
874 //DeletePonCounter(device, ponID)
875}
876
877// PutOltIgmpCounters to add Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530878func (db *Database) PutOltIgmpCounters(ctx context.Context, device, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530879 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530880 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530881}
882
883// GetOltIgmpCounter to get Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530884func (db *Database) GetOltIgmpCounter(ctx context.Context, device string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530885 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530886 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530887}
888
889//Service Channel counters
890
891// GetAllServiceChannelCounters to get all service channel counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530892func (db *Database) GetAllServiceChannelCounters(ctx context.Context, serviceName string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530893 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530894 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530895}
896
897// GetServiceChannelCounter to get service channel counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530898func (db *Database) GetServiceChannelCounter(ctx context.Context, serviceName, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530899 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530900 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530901}
902
903// PutServiceChannelCounter to add service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530904func (db *Database) PutServiceChannelCounter(ctx context.Context, serviceName, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530905 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530906 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530907}
908
909// DelServiceChannelCounter to delete service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530910func (db *Database) DelServiceChannelCounter(ctx context.Context, serviceName, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530911 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530912 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530913 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530914 return err
915 }
916 return nil
917}
918
919// DelAllServiceChannelCounter to delete all service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530920func (db *Database) DelAllServiceChannelCounter(ctx context.Context, serviceName string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530921 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530922 return db.DeleteAllUnderHashKey(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530923}
924
925// OltExists to know if the ONU is added to the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530926func (db *Database) OltExists(ctx context.Context, deviceID string) bool {
927 if _, err := db.GetOlt(ctx, deviceID); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530928 return false
929 }
930 return true
931
932}
933
934// PutFlowHash to add flowhash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530935func (db *Database) PutFlowHash(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530936 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530937 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530938}
939
940// GetFlowHash gets the flow hash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530941func (db *Database) GetFlowHash(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530942 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530943 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530944}
945
946// PutPortAlarmProfile to add port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530947func (db *Database) PutPortAlarmProfile(ctx context.Context, portAlarmProfileID string, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530948 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530949 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530950 logger.Warnw(ctx, "Put PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530951 }
952}
953
954// DelPortAlarmProfile to delete port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530955func (db *Database) DelPortAlarmProfile(ctx context.Context, portAlarmProfileID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530956 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530957 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530958 logger.Warnw(ctx, "Delete PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530959 }
960}
961
962// GetPortAlarmProfile to get port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530963func (db *Database) GetPortAlarmProfile(ctx context.Context, portAlarmProfileID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530964 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530965 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530966}
967
968// PutPortAlarmData to add port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530969func (db *Database) PutPortAlarmData(ctx context.Context, deviceID string, portID uint32, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530970 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530971 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530972 logger.Warnw(ctx, "Put PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530973 }
974}
975
976// DelPortAlarmData to delete port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530977func (db *Database) DelPortAlarmData(ctx context.Context, deviceID string, portID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530978 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530979 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530980 logger.Warnw(ctx, "Delete PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530981 }
982}
983
984// GetPortAlarmData to get port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530985func (db *Database) GetPortAlarmData(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530986 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530987 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530988}
989
990// GetAllPortAlarmData to get port alarm data for all ports
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530991func (db *Database) GetAllPortAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530992 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530993 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530994}
995
996// PutSubAlarmData to add subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530997func (db *Database) PutSubAlarmData(ctx context.Context, deviceID string, portName string, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530998 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530999 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301000 logger.Warnw(ctx, "Put Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301001 }
1002}
1003
1004// DelSubAlarmData to delete subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301005func (db *Database) DelSubAlarmData(ctx context.Context, deviceID string, portName string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301006 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301007 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301008 logger.Warnw(ctx, "Delete Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301009 }
1010}
1011
1012// GetSubAlarmData to get subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301013func (db *Database) GetSubAlarmData(ctx context.Context, deviceID string, portName string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301014 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301015 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301016}
1017
1018// GetAllSubAlarmData to get sub alarm data for all subscribers
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301019func (db *Database) GetAllSubAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301020 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301021 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301022}
1023
1024// Migrate Service req specific database actions
1025
1026// PutMigrateServicesReq to add MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301027func (db *Database) PutMigrateServicesReq(ctx context.Context, deviceID string, vnet string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301028 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301029 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +05301030}
1031
1032// GetMigrateServicesReq to get MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301033func (db *Database) GetMigrateServicesReq(ctx context.Context, deviceID string, vnet string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301034 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301035 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301036}
1037
1038// GetAllMigrateServicesReq to get multiple MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301039func (db *Database) GetAllMigrateServicesReq(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301040 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301041 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301042}
1043
1044// DelMigrateServicesReq to delete MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301045func (db *Database) DelMigrateServicesReq(ctx context.Context, deviceID string, vnet string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301046 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301047 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301048 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301049 return err
1050 }
1051 return nil
1052}
1053
1054// DelAllMigrateServicesReq to delete all MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301055func (db *Database) DelAllMigrateServicesReq(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301056 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301057 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301058 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301059 return err
1060 }
1061 logger.Infow(ctx, "Deleting all the Update Vnet Requests for device", log.Fields{"device": deviceID})
1062 return nil
1063}
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301064// PutOltFlowService to add OltFlowService info
1065func (db *Database) PutOltFlowService(ctx context.Context, value string) error {
1066 key := GetKeyPath(OltFlowServicePath)
Naveen Sampath04696f72022-06-13 15:19:14 +05301067
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301068 if err := db.kvc.Put(ctx, key, value); err != nil {
1069 logger.Warnw(ctx, "Put OltFlowService failed", log.Fields{"key": key})
1070 return err
1071 }
1072 return nil
1073}
1074
1075// GetOltFlowService to get OltFlowService info
1076func (db *Database) GetOltFlowService(ctx context.Context) (string, error) {
1077 key := GetKeyPath(OltFlowServicePath)
1078 return db.Get(ctx, key)
1079}
Naveen Sampath04696f72022-06-13 15:19:14 +05301080func init() {
Akash Sonia8246972023-01-03 10:37:08 +05301081 // Setup this package so that it's log level can be modified at run time
1082 var err error
1083 logger, err = log.AddPackageWithDefaultParam()
1084 if err != nil {
1085 panic(err)
1086 }
Naveen Sampath04696f72022-06-13 15:19:14 +05301087}