blob: c3ad13f4e45c360a5fe6982c79393edbad565d16 [file] [log] [blame]
Joey Armstrongaca03cf2024-04-23 09:29:52 -04001/* -----------------------------------------------------------------------
2 * Copyright 2022-2024 Open Networking Foundation Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * -----------------------------------------------------------------------
16 * SPDX-FileCopyrightText: 2022-2024 Open Networking Foundation Contributors
17 * SPDX-License-Identifier: Apache-2.0
18 * -----------------------------------------------------------------------
Akash Sonia8246972023-01-03 10:37:08 +053019 */
Joey Armstrongaca03cf2024-04-23 09:29:52 -040020
Naveen Sampath04696f72022-06-13 15:19:14 +053021// This implementation of database assumes that it is working for
22// Open ONU adapter. Thus, it assumes some base path for all the
23// database operations. For all database operations, the key passed is
24// added to the database base path.
25
26package database
27
28import (
29 "context"
30 "errors"
Akash Sonia8246972023-01-03 10:37:08 +053031 "fmt"
Naveen Sampath04696f72022-06-13 15:19:14 +053032 "net"
33 "strconv"
34 "time"
Naveen Sampath04696f72022-06-13 15:19:14 +053035
Akash Sonia8246972023-01-03 10:37:08 +053036 "voltha-go-controller/internal/pkg/errorcodes"
Naveen Sampath04696f72022-06-13 15:19:14 +053037 "voltha-go-controller/internal/pkg/of"
Tinoj Joseph1d108322022-07-13 10:07:39 +053038 "voltha-go-controller/log"
Akash Sonia8246972023-01-03 10:37:08 +053039
40 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
Naveen Sampath04696f72022-06-13 15:19:14 +053041)
42
Akash Sonief452f12024-12-12 18:20:28 +053043const (
44 PonPort = "/pon-port/"
45)
46
Naveen Sampath04696f72022-06-13 15:19:14 +053047var logger log.CLogger
Naveen Sampath04696f72022-06-13 15:19:14 +053048
49// Database structure
50type Database struct {
vinokuma926cb3e2023-03-29 11:41:06 +053051 kvc kvstore.Client
Naveen Sampath04696f72022-06-13 15:19:14 +053052 storeType string
53 address string
54 //timeout uint32
Naveen Sampath04696f72022-06-13 15:19:14 +053055}
56
57// Initialize the database module. The database module runs as a singleton
58// object and is initialized when the adapter is created.
Tinoj Joseph07cc5372022-07-18 22:53:51 +053059func Initialize(ctx context.Context, storeType string, address string, timeout int) (*Database, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +053060 var err error
61 var database Database
62 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
63 database.address = address
64 database.storeType = storeType
65 switch storeType {
66 case "redis":
67 database.kvc, err = kvstore.NewRedisClient(address, time.Duration(timeout), false)
68 return &database, err
Abhay Kumar6040f382024-07-12 09:29:23 +053069 case "redis-sentinel":
70 database.kvc, err = kvstore.NewRedisClient(address, time.Duration(timeout), true)
71 return &database, err
Akash Sonidedc8ee2023-03-03 11:51:49 +053072 case "etcd":
73 database.kvc, err = kvstore.NewEtcdClient(ctx, address, time.Duration(timeout), log.ErrorLevel)
74 return &database, err
Naveen Sampath04696f72022-06-13 15:19:14 +053075 }
76 return &database, errors.New("unsupported-kv-store")
77}
78
79// Utility function that retrieves value for a key. It is assumed that
80// the information is always a string and the data retrieved is returned
81// as a string
82
83// Put to add value to database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053084func (db *Database) Put(ctx context.Context, fullKeyPath, value string) error {
85 return db.kvc.Put(ctx, fullKeyPath, value)
Naveen Sampath04696f72022-06-13 15:19:14 +053086}
87
88// Get to retrieve value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053089func (db *Database) Get(ctx context.Context, key string) (string, error) {
90 kv, err := db.kvc.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +053091 if err != nil {
92 return "", err
93 }
94 if kv != nil {
95 return string(kv.Value.([]byte)), nil
96 }
97 return "", errors.New("Value not found")
98}
99
100// Del to delete value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530101func (db *Database) Del(ctx context.Context, fullPath string) error {
102 if err := db.kvc.Delete(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530103 logger.Errorw(ctx, "The path doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530104 return err
105 }
106 return nil
107}
108
109// DeleteAll to delete all value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530110func (db *Database) DeleteAll(ctx context.Context, fullPath string) error {
111 if err := db.kvc.DeleteWithPrefix(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530112 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530113 return err
114 }
115 return nil
116}
117
118// DeleteAllUnderHashKey to delete all values under hash key
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530119func (db *Database) DeleteAllUnderHashKey(ctx context.Context, hashKeyPrefix string) error {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530120 kv, err := db.kvc.List(ctx, hashKeyPrefix)
121 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530122 logger.Errorw(ctx, "The key path doesn't exist", log.Fields{"key": hashKeyPrefix, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530123 return err
124 }
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530125 for key := range kv {
126 if err := db.kvc.Delete(ctx, key); err != nil {
127 logger.Errorw(ctx, "Delete key from DB Failed", log.Fields{"key": key, "Error": err})
128 }
129 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530130 return nil
131}
132
133// List to list the values
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530134func (db *Database) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
135 kv, err := db.kvc.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530136 if err != nil {
137 return nil, err
138 }
139 if kv != nil {
140 return kv, nil
141 }
142 return nil, errors.New("Value not found")
143}
144
145// OLT specific database items
146
147// GetOlt to get olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530148func (db *Database) GetOlt(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530149 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530150 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530151}
152
153// PutOlt to add olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530154func (db *Database) PutOlt(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530155 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530156 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530157}
158
159// DelOlt to delete olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530160func (db *Database) DelOlt(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530161 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530162 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530163 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530164 return err
165 }
166 return nil
167}
168
169// Flows specific database actions
170
171// PutFlow to add flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530172func (db *Database) PutFlow(ctx context.Context, deviceID string, flowID uint64, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530173 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530174 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530175}
176
177// GetFlow to get flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530178func (db *Database) GetFlow(ctx context.Context, deviceID string, flowID uint64) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530179 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530180 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530181}
182
183// GetFlows to get multiple flows
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530184func (db *Database) GetFlows(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530185 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530186 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530187}
188
189// DelFlow to delete flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530190func (db *Database) DelFlow(ctx context.Context, deviceID string, flowID uint64) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530191 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530192 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530193 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530194 return err
195 }
196 return nil
197}
198
199// Group specific database actions
200
201// PutGroup to add group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530202func (db *Database) PutGroup(ctx context.Context, deviceID string, groupID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530203 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530204 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530205}
206
207// GetGroup to get group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530208func (db *Database) GetGroup(ctx context.Context, deviceID string, groupID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530209 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530210 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530211}
212
213// GetGroups to get multiple group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530214func (db *Database) GetGroups(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530215 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
216 logger.Infow(ctx, "key", log.Fields{"Key": key})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530217 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530218}
219
220// DelGroup to delete group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530221func (db *Database) DelGroup(ctx context.Context, deviceID string, groupID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530222 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530223 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530224 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530225 return err
226 }
227 return nil
228}
229
230// DelAllGroup to delete all group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530231func (db *Database) DelAllGroup(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530232 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530233 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530234 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530235 return err
236 }
237 logger.Infow(ctx, "Deleting all the groups for device", log.Fields{"device": deviceID})
238 return nil
239}
240
241// DelAllPorts to delete all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530242func (db *Database) DelAllPorts(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530243 key := fmt.Sprintf(GetKeyPath(DevicePortPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530244 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530245 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530246 return err
247 }
248 logger.Infow(ctx, "Deleting all the ports for device", log.Fields{"device": device})
249 return nil
250}
251
252// Ports specific database actions
253
254// PutPort to add port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530255func (db *Database) PutPort(ctx context.Context, deviceID string, portID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530256 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530257 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530258}
259
260// GetPort to get port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530261func (db *Database) GetPort(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530262 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530263 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530264}
265
266// GetPorts to get multiple ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530267func (db *Database) GetPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530268 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530269 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530270}
271
272// DelPort to delete port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530273func (db *Database) DelPort(ctx context.Context, deviceID string, portID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530274 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530275 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530276 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530277 return err
278 }
279 return nil
280}
281
282// Device meter specific database actions
283
284// PutDeviceMeter to add device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530285func (db *Database) PutDeviceMeter(ctx context.Context, deviceID string, meterID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530286 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530287 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530288}
289
290// GetDeviceMeter to get device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530291func (db *Database) GetDeviceMeter(ctx context.Context, deviceID string, meterID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530292 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530293 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530294}
295
296// GetDeviceMeters to get multiple device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530297func (db *Database) GetDeviceMeters(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530298 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530299 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530300}
301
302// DelDeviceMeter to delete device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530303func (db *Database) DelDeviceMeter(ctx context.Context, deviceID string, meterID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530304 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530305 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530306 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530307 return err
308 }
309 return nil
310}
311
312// Service specific database actions
313
314// GetServices to get multiple services info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530315func (db *Database) GetServices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530316 key := GetKeyPath(ServicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530317 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530318}
319
320// GetService to get service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530321func (db *Database) GetService(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530322 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530323 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530324}
325
326// PutService to add service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530327func (db *Database) PutService(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530328 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530329 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530330}
331
332// DelService to delete service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530333func (db *Database) DelService(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530334 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530335 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530336 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530337 return err
338 }
339 return nil
340}
341
342// Virtual networks specific database actions
343
344// GetVnets to get multiple vnets info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530345func (db *Database) GetVnets(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530346 key := GetKeyPath(VnetPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530347 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530348}
349
350// GetVnet to get vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530351func (db *Database) GetVnet(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530352 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530353 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530354}
355
356// PutVnet to add vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530357func (db *Database) PutVnet(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530358 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530359 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530360}
361
362// DelVnet to delete vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530363func (db *Database) DelVnet(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530364 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530365 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530366 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530367 return err
368 }
369 return nil
370}
371
372// Virtual networks on ports specific database actions
373
374// GetVpvs to get multiple vpvs info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530375func (db *Database) GetVpvs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530376 key := GetKeyPath(VpvPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530377 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530378}
379
380// GetVpv to get vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530381func (db *Database) GetVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530382 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
383 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530384 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530385}
386
387// PutVpv to add vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530388func (db *Database) PutVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530389 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
390 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530391 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530392}
393
394// DelVpv to delete vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530395func (db *Database) DelVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530396 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
397 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530398 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530399 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530400 return err
401 }
402 return nil
403}
404
405// Virtual networks on ports specific database actions
406
407// GetMvlans to get multiple mvlans info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530408func (db *Database) GetMvlans(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530409 key := GetKeyPath(MvlanPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530410 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530411}
412
413// GetMvlan to get mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530414func (db *Database) GetMvlan(ctx context.Context, mvlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530415 name := strconv.FormatInt(int64(mvlan), 10)
416 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530417 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530418}
419
420// PutMvlan to add mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530421func (db *Database) PutMvlan(ctx context.Context, mvlan uint16, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530422 name := strconv.FormatInt(int64(mvlan), 10)
423 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530424 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530425}
426
427// DelMvlan to delete mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530428func (db *Database) DelMvlan(ctx context.Context, mvlan uint16) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530429 name := strconv.FormatInt(int64(mvlan), 10)
430 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530431 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530432 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530433 return err
434 }
435 return nil
436}
437
438// database specific actions on IGMP config
439
440// DelIGMPCfg to delete icmp config
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530441func (db *Database) DelIGMPCfg(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530442 key := GetKeyPath(IgmpConfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530443 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530444 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530445 return err
446 }
447 return nil
448}
449
450// database specific actions on IGMP Profile
451
452// GetIgmpProfiles to get multiple igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530453func (db *Database) GetIgmpProfiles(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530454 key := GetKeyPath(IgmpProfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530455 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530456}
457
458// GetIgmpProfile to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530459func (db *Database) GetIgmpProfile(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530460 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530461 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530462}
463
464// PutIgmpProfile to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530465func (db *Database) PutIgmpProfile(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530466 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530467 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530468}
469
470// DelIgmpProfile to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530471func (db *Database) DelIgmpProfile(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530472 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530473 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530474 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530475 return err
476 }
477 return nil
478}
479
480// database specific actions on Mcast config Info
481
482// GetMcastConfigs to get multiple mcast config info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530483func (db *Database) GetMcastConfigs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530484 key := GetKeyPath(McastConfigPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530485 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530486}
487
488// GetMcastConfig to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530489func (db *Database) GetMcastConfig(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530490 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530491 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530492}
493
494// PutMcastConfig to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530495func (db *Database) PutMcastConfig(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530496 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530497 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530498}
499
500// DelMcastConfig to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530501func (db *Database) DelMcastConfig(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530502 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530503 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530504 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530505 return err
506 }
507 return nil
508}
509
510// database specific actions on health
511
512// GetHealth to get health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530513func (db *Database) GetHealth(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530514 key := GetKeyPath(HealthPath)
Akash Sonia8246972023-01-03 10:37:08 +0530515 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530516}
517
518// PutHealth to add health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530519func (db *Database) PutHealth(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530520 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530521 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530522}
523
524// DelHealth to delete health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530525func (db *Database) DelHealth(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530526 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530527 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530528 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530529 return err
530 }
531 return nil
532}
533
534// Meters
535
536// GetMeters to get multiple meters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530537func (db *Database) GetMeters(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530538 key := GetKeyPath(MeterPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530539 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530540}
541
542// GetMeter to get meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530543func (db *Database) GetMeter(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530544 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530545 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530546}
547
548// PutMeter to add meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530549func (db *Database) PutMeter(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530550 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530551 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530552}
553
554// DelMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530555func (db *Database) DelMeter(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530556 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530557 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530558 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530559 return err
560 }
561 return nil
562}
563
564// DelAllMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530565func (db *Database) DelAllMeter(ctx context.Context, device string) error {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530566 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530567 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530568 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530569 return err
570 }
571 logger.Infow(ctx, "Deleting all the meters for device", log.Fields{"device": device})
572 return nil
573}
574
575// IGMP groups
576
577// GetIgmpGroups to get multiple igmp groups info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530578func (db *Database) GetIgmpGroups(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530579 key := GetKeyPath(IgmpGroupPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530580 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530581}
582
583// GetIgmpGroup to get igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530584func (db *Database) GetIgmpGroup(ctx context.Context, id string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530585 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530586 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530587}
588
589// PutIgmpGroup to add igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530590func (db *Database) PutIgmpGroup(ctx context.Context, id string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530591 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530592 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530593}
594
595// DelIgmpGroup to delete igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530596func (db *Database) DelIgmpGroup(ctx context.Context, id string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530597 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530598 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530599 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530600 return err
601 }
602 return nil
603}
604
605// IGMP group devices
606
607// GetAllIgmpDevices to get multiple igmp devices info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530608func (db *Database) GetAllIgmpDevices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530609 key := GetKeyPath(IgmpDevicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530610 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530611}
612
613// GetPrevIgmpDevices to get previous igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530614func (db *Database) GetPrevIgmpDevices(ctx context.Context, mvlan of.VlanType, gid string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530615 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530616 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530617}
618
619// GetIgmpDevices to get igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530620func (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 +0530621 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530622 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530623}
624
625// GetIgmpDevice to get igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530626func (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 +0530627 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530628 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530629}
630
631// PutIgmpDevice to add igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530632func (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 +0530633 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530634 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530635}
636
637// DelIgmpDevice to delete igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530638func (db *Database) DelIgmpDevice(ctx context.Context, mvlan of.VlanType, gid string, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530639 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530640 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530641 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530642 return err
643 }
644 return nil
645}
646
647// IGMP group channels
648
649// GetAllIgmpChannels to get all igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530650func (db *Database) GetAllIgmpChannels(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530651 key := GetKeyPath(IgmpChannelPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530652 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530653}
654
655// GetPrevIgmpChannels to get previous igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530656func (db *Database) GetPrevIgmpChannels(ctx context.Context, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530657 key := GetKeyPath(IgmpChannelPath) + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530658 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530659}
660
661// GetIgmpChannels to get multiple igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530662func (db *Database) GetIgmpChannels(ctx context.Context, mvlan of.VlanType, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530663 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530664 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530665}
666
667// GetIgmpChannel to get igmp channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530668func (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 +0530669 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530670 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530671}
672
673// PutIgmpChannel to add igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530674func (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 +0530675 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530676 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530677}
678
679// DelIgmpChannel to delete igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530680func (db *Database) DelIgmpChannel(ctx context.Context, mvlan of.VlanType, gName string, device string, gip net.IP) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530681 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530682 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530683 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530684 return err
685 }
686 return nil
687}
688
689// IGMP group receivers
690
691// GetAllIgmpRcvrs to get all igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530692func (db *Database) GetAllIgmpRcvrs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530693 key := GetKeyPath(IgmpPortPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530694 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530695}
696
697// GetPrevIgmpRcvrs to get previous igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530698func (db *Database) GetPrevIgmpRcvrs(ctx context.Context, gip net.IP, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530699 key := GetKeyPath(IgmpPortPath) + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530700 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530701}
702
703// GetIgmpRcvrs to get multiple igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530704func (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 +0530705 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530706 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530707}
708
709// GetIgmpRcvr to get igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530710func (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 +0530711 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530712 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530713}
714
715// PutIgmpRcvr to add igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530716func (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 +0530717 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530718 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530719}
720
721// DelIgmpRcvr to delete igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530722func (db *Database) DelIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string, rcvr string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530723 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530724 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530725 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530726 return err
727 }
728 return nil
729}
730
731// DelAllIgmpRcvr to delete all igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530732func (db *Database) DelAllIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530733 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530734 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530735 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530736 return err
737 }
738 return nil
739}
740
741// DelAllRoutesForDevice to delete all routes for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530742func (db *Database) DelAllRoutesForDevice(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530743 /* service/vgc/v1/devices/<deviceID>/flows/ */
744 logger.Infow(ctx, "Deleting all the flows for device", log.Fields{"device": device})
745 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530746 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530747 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530748 return err
749 }
750 return nil
751}
752
753// PutNbDevicePort to add device port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530754func (db *Database) PutNbDevicePort(ctx context.Context, device string, ponPortID uint32, value string) {
Akash Sonief452f12024-12-12 18:20:28 +0530755 key := GetKeyPath(NbDevicePath) + device + PonPort + fmt.Sprintf("%v", ponPortID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530756
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530757 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530758 logger.Warnw(ctx, "Put Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530759 }
760}
761
Akash Sonia8246972023-01-03 10:37:08 +0530762// GetServices to get multiple services info
763func (db *Database) GetDeviceConfig(ctx context.Context) (map[string]*kvstore.KVPair, error) {
764 key := GetKeyPath(DeviceConfigPath)
765 return db.List(ctx, key)
766}
767
768// PutSBDeviceConfig to add device info
769func (db *Database) PutDeviceConfig(ctx context.Context, serialNum string, value string) error {
770 key := GetKeyPath(DeviceConfigPath) + serialNum
771
772 if err := db.kvc.Put(ctx, key, value); err != nil {
773 logger.Warnw(ctx, "Put Device Config failed", log.Fields{"key": key})
774 return errorcodes.ErrFailedToUpdateDB
775 }
776 return nil
777}
778
Naveen Sampath04696f72022-06-13 15:19:14 +0530779// DelNbDevicePort to delete device port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530780func (db *Database) DelNbDevicePort(ctx context.Context, device string, ponPortID uint32) {
Akash Sonief452f12024-12-12 18:20:28 +0530781 key := GetKeyPath(NbDevicePath) + device + PonPort + fmt.Sprintf("%v", ponPortID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530782
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530783 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530784 logger.Warnw(ctx, "Delete Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530785 }
786}
787
788// GetAllNbPorts to get all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530789func (db *Database) GetAllNbPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Akash Sonief452f12024-12-12 18:20:28 +0530790 key := GetKeyPath(NbDevicePath) + deviceID + PonPort
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530791 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530792}
793
794//Functions for migration database
795
796// GetMigrationInfo to get migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530797func (db *Database) GetMigrationInfo(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530798 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530799 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530800}
801
802// PutMigrationInfo to add migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530803func (db *Database) PutMigrationInfo(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530804 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530805 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530806}
807
808// DelMigrationInfo to delete migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530809func (db *Database) DelMigrationInfo(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530810 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530811 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530812 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530813 return err
814 }
815 return nil
816}
817
818//PON counters
819
820// GetAllPonCounters to get all pon counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530821func (db *Database) GetAllPonCounters(ctx context.Context, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530822 key := GetKeyPath(PonCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530823 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530824}
825
826// GetPonCounter to get pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530827func (db *Database) GetPonCounter(ctx context.Context, device, ponID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530828 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530829 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530830}
831
832// PutPonCounter to add pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530833func (db *Database) PutPonCounter(ctx context.Context, device, ponID, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530834 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530835 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530836}
837
838// DelPonCounter to delete pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530839func (db *Database) DelPonCounter(ctx context.Context, device, ponID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530840 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530841 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530842 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530843 return err
844 }
845 return nil
846}
847
848//PON Channel counters
849
850// GetAllPonChannelCounters to get all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530851func (db *Database) GetAllPonChannelCounters(ctx context.Context, device, ponID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530852 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530853 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530854}
855
856// GetPonChannelCounter to get pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530857func (db *Database) GetPonChannelCounter(ctx context.Context, device, ponID, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530858 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530859 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530860}
861
862// PutPonChannelCounter to add pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530863func (db *Database) PutPonChannelCounter(ctx context.Context, device, ponID, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530864 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530865 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530866}
867
868// DelPonChannelCounter to delete pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530869func (db *Database) DelPonChannelCounter(ctx context.Context, device, ponID, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530870 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530871 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530872 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530873 return err
874 }
875 return nil
876}
877
878// DelAllPONCounters to delete all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530879func (db *Database) DelAllPONCounters(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530880 key := GetKeyPath(PonCounterPath) + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530881 return db.DeleteAll(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530882}
883
884// DelPONCounters to delete pon counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530885func (db *Database) DelPONCounters(ctx context.Context, device string, ponID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530886 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530887 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530888 logger.Warnw(ctx, "Delete Pon counters failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530889 }
890 //DeletePonCounter(device, ponID)
891}
892
893// PutOltIgmpCounters to add Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530894func (db *Database) PutOltIgmpCounters(ctx context.Context, device, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530895 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530896 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530897}
898
899// GetOltIgmpCounter to get Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530900func (db *Database) GetOltIgmpCounter(ctx context.Context, device string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530901 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530902 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530903}
904
905//Service Channel counters
906
907// GetAllServiceChannelCounters to get all service channel counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530908func (db *Database) GetAllServiceChannelCounters(ctx context.Context, serviceName string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530909 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530910 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530911}
912
913// GetServiceChannelCounter to get service channel counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530914func (db *Database) GetServiceChannelCounter(ctx context.Context, serviceName, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530915 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530916 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530917}
918
919// PutServiceChannelCounter to add service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530920func (db *Database) PutServiceChannelCounter(ctx context.Context, serviceName, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530921 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530922 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530923}
924
925// DelServiceChannelCounter to delete service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530926func (db *Database) DelServiceChannelCounter(ctx context.Context, serviceName, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530927 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530928 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530929 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530930 return err
931 }
932 return nil
933}
934
935// DelAllServiceChannelCounter to delete all service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530936func (db *Database) DelAllServiceChannelCounter(ctx context.Context, serviceName string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530937 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530938 return db.DeleteAllUnderHashKey(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530939}
940
941// OltExists to know if the ONU is added to the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530942func (db *Database) OltExists(ctx context.Context, deviceID string) bool {
943 if _, err := db.GetOlt(ctx, deviceID); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530944 return false
945 }
946 return true
Naveen Sampath04696f72022-06-13 15:19:14 +0530947}
948
949// PutFlowHash to add flowhash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530950func (db *Database) PutFlowHash(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530951 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530952 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530953}
954
955// GetFlowHash gets the flow hash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530956func (db *Database) GetFlowHash(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530957 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530958 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530959}
960
961// PutPortAlarmProfile to add port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530962func (db *Database) PutPortAlarmProfile(ctx context.Context, portAlarmProfileID string, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530963 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530964 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530965 logger.Warnw(ctx, "Put PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530966 }
967}
968
969// DelPortAlarmProfile to delete port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530970func (db *Database) DelPortAlarmProfile(ctx context.Context, portAlarmProfileID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530971 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530972 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530973 logger.Warnw(ctx, "Delete PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530974 }
975}
976
977// GetPortAlarmProfile to get port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530978func (db *Database) GetPortAlarmProfile(ctx context.Context, portAlarmProfileID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530979 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530980 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530981}
982
983// PutPortAlarmData to add port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530984func (db *Database) PutPortAlarmData(ctx context.Context, deviceID string, portID uint32, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530985 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530986 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530987 logger.Warnw(ctx, "Put PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530988 }
989}
990
991// DelPortAlarmData to delete port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530992func (db *Database) DelPortAlarmData(ctx context.Context, deviceID string, portID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530993 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530994 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530995 logger.Warnw(ctx, "Delete PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530996 }
997}
998
999// GetPortAlarmData to get port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301000func (db *Database) GetPortAlarmData(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301001 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301002 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301003}
1004
1005// GetAllPortAlarmData to get port alarm data for all ports
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301006func (db *Database) GetAllPortAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301007 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301008 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301009}
1010
1011// PutSubAlarmData to add subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301012func (db *Database) PutSubAlarmData(ctx context.Context, deviceID string, portName string, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301013 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301014 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301015 logger.Warnw(ctx, "Put Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301016 }
1017}
1018
1019// DelSubAlarmData to delete subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301020func (db *Database) DelSubAlarmData(ctx context.Context, deviceID string, portName string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301021 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301022 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301023 logger.Warnw(ctx, "Delete Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301024 }
1025}
1026
1027// GetSubAlarmData to get subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301028func (db *Database) GetSubAlarmData(ctx context.Context, deviceID string, portName string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301029 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301030 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301031}
1032
1033// GetAllSubAlarmData to get sub alarm data for all subscribers
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301034func (db *Database) GetAllSubAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301035 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301036 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301037}
1038
1039// Migrate Service req specific database actions
1040
1041// PutMigrateServicesReq to add MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301042func (db *Database) PutMigrateServicesReq(ctx context.Context, deviceID string, vnet string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301043 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301044 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +05301045}
1046
1047// GetMigrateServicesReq to get MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301048func (db *Database) GetMigrateServicesReq(ctx context.Context, deviceID string, vnet string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301049 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301050 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301051}
1052
1053// GetAllMigrateServicesReq to get multiple MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301054func (db *Database) GetAllMigrateServicesReq(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301055 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301056 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301057}
1058
1059// DelMigrateServicesReq to delete MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301060func (db *Database) DelMigrateServicesReq(ctx context.Context, deviceID string, vnet string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301061 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301062 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301063 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301064 return err
1065 }
1066 return nil
1067}
1068
1069// DelAllMigrateServicesReq to delete all MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301070func (db *Database) DelAllMigrateServicesReq(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301071 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301072 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301073 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301074 return err
1075 }
1076 logger.Infow(ctx, "Deleting all the Update Vnet Requests for device", log.Fields{"device": deviceID})
1077 return nil
1078}
Akash Sonidedc8ee2023-03-03 11:51:49 +05301079
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301080// PutOltFlowService to add OltFlowService info
1081func (db *Database) PutOltFlowService(ctx context.Context, value string) error {
1082 key := GetKeyPath(OltFlowServicePath)
Naveen Sampath04696f72022-06-13 15:19:14 +05301083
Akash Sonidedc8ee2023-03-03 11:51:49 +05301084 if err := db.kvc.Put(ctx, key, value); err != nil {
1085 logger.Warnw(ctx, "Put OltFlowService failed", log.Fields{"key": key})
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301086 return err
Akash Sonidedc8ee2023-03-03 11:51:49 +05301087 }
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301088 return nil
1089}
1090
1091// GetOltFlowService to get OltFlowService info
1092func (db *Database) GetOltFlowService(ctx context.Context) (string, error) {
1093 key := GetKeyPath(OltFlowServicePath)
1094 return db.Get(ctx, key)
1095}
Naveen Sampath04696f72022-06-13 15:19:14 +05301096func init() {
Akash Sonia8246972023-01-03 10:37:08 +05301097 // Setup this package so that it's log level can be modified at run time
1098 var err error
1099 logger, err = log.AddPackageWithDefaultParam()
1100 if err != nil {
1101 panic(err)
1102 }
Naveen Sampath04696f72022-06-13 15:19:14 +05301103}