blob: 5be86b560f9a9a420dc36fc0fe349a86c32d2e3e [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
43var logger log.CLogger
Naveen Sampath04696f72022-06-13 15:19:14 +053044
45// Database structure
46type Database struct {
vinokuma926cb3e2023-03-29 11:41:06 +053047 kvc kvstore.Client
Naveen Sampath04696f72022-06-13 15:19:14 +053048 storeType string
49 address string
50 //timeout uint32
Naveen Sampath04696f72022-06-13 15:19:14 +053051}
52
53// Initialize the database module. The database module runs as a singleton
54// object and is initialized when the adapter is created.
Tinoj Joseph07cc5372022-07-18 22:53:51 +053055func Initialize(ctx context.Context, storeType string, address string, timeout int) (*Database, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +053056 var err error
57 var database Database
58 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
59 database.address = address
60 database.storeType = storeType
61 switch storeType {
62 case "redis":
63 database.kvc, err = kvstore.NewRedisClient(address, time.Duration(timeout), false)
64 return &database, err
Akash Sonidedc8ee2023-03-03 11:51:49 +053065 case "etcd":
66 database.kvc, err = kvstore.NewEtcdClient(ctx, address, time.Duration(timeout), log.ErrorLevel)
67 return &database, err
Naveen Sampath04696f72022-06-13 15:19:14 +053068 }
69 return &database, errors.New("unsupported-kv-store")
70}
71
72// Utility function that retrieves value for a key. It is assumed that
73// the information is always a string and the data retrieved is returned
74// as a string
75
76// Put to add value to database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053077func (db *Database) Put(ctx context.Context, fullKeyPath, value string) error {
78 return db.kvc.Put(ctx, fullKeyPath, value)
Naveen Sampath04696f72022-06-13 15:19:14 +053079}
80
81// Get to retrieve value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053082func (db *Database) Get(ctx context.Context, key string) (string, error) {
83 kv, err := db.kvc.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +053084 if err != nil {
85 return "", err
86 }
87 if kv != nil {
88 return string(kv.Value.([]byte)), nil
89 }
90 return "", errors.New("Value not found")
91}
92
93// Del to delete value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053094func (db *Database) Del(ctx context.Context, fullPath string) error {
95 if err := db.kvc.Delete(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +053096 logger.Errorw(ctx, "The path 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// DeleteAll to delete all value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530103func (db *Database) DeleteAll(ctx context.Context, fullPath string) error {
104 if err := db.kvc.DeleteWithPrefix(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530105 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530106 return err
107 }
108 return nil
109}
110
111// DeleteAllUnderHashKey to delete all values under hash key
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530112func (db *Database) DeleteAllUnderHashKey(ctx context.Context, hashKeyPrefix string) error {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530113 kv, err := db.kvc.List(ctx, hashKeyPrefix)
114 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530115 logger.Errorw(ctx, "The key path doesn't exist", log.Fields{"key": hashKeyPrefix, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530116 return err
117 }
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530118 for key := range kv {
119 if err := db.kvc.Delete(ctx, key); err != nil {
120 logger.Errorw(ctx, "Delete key from DB Failed", log.Fields{"key": key, "Error": err})
121 }
122 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530123 return nil
124}
125
126// List to list the values
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530127func (db *Database) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
128 kv, err := db.kvc.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530129 if err != nil {
130 return nil, err
131 }
132 if kv != nil {
133 return kv, nil
134 }
135 return nil, errors.New("Value not found")
136}
137
138// OLT specific database items
139
140// GetOlt to get olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530141func (db *Database) GetOlt(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530142 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530143 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530144}
145
146// PutOlt to add olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530147func (db *Database) PutOlt(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530148 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530149 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530150}
151
152// DelOlt to delete olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530153func (db *Database) DelOlt(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530154 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530155 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530156 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530157 return err
158 }
159 return nil
160}
161
162// Flows specific database actions
163
164// PutFlow to add flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530165func (db *Database) PutFlow(ctx context.Context, deviceID string, flowID uint64, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530166 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530167 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530168}
169
170// GetFlow to get flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530171func (db *Database) GetFlow(ctx context.Context, deviceID string, flowID uint64) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530172 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530173 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530174}
175
176// GetFlows to get multiple flows
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530177func (db *Database) GetFlows(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530178 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530179 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530180}
181
182// DelFlow to delete flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530183func (db *Database) DelFlow(ctx context.Context, deviceID string, flowID uint64) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530184 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530185 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530186 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530187 return err
188 }
189 return nil
190}
191
192// Group specific database actions
193
194// PutGroup to add group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530195func (db *Database) PutGroup(ctx context.Context, deviceID string, groupID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530196 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530197 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530198}
199
200// GetGroup to get group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530201func (db *Database) GetGroup(ctx context.Context, deviceID string, groupID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530202 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530203 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530204}
205
206// GetGroups to get multiple group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530207func (db *Database) GetGroups(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530208 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
209 logger.Infow(ctx, "key", log.Fields{"Key": key})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530210 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530211}
212
213// DelGroup to delete group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530214func (db *Database) DelGroup(ctx context.Context, deviceID string, groupID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530215 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530216 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530217 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530218 return err
219 }
220 return nil
221}
222
223// DelAllGroup to delete all group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530224func (db *Database) DelAllGroup(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530225 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530226 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530227 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530228 return err
229 }
230 logger.Infow(ctx, "Deleting all the groups for device", log.Fields{"device": deviceID})
231 return nil
232}
233
234// DelAllPorts to delete all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530235func (db *Database) DelAllPorts(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530236 key := fmt.Sprintf(GetKeyPath(DevicePortPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530237 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530238 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530239 return err
240 }
241 logger.Infow(ctx, "Deleting all the ports for device", log.Fields{"device": device})
242 return nil
243}
244
245// Ports specific database actions
246
247// PutPort to add port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530248func (db *Database) PutPort(ctx context.Context, deviceID string, portID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530249 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530250 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530251}
252
253// GetPort to get port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530254func (db *Database) GetPort(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530255 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530256 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530257}
258
259// GetPorts to get multiple ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530260func (db *Database) GetPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530261 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530262 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530263}
264
265// DelPort to delete port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530266func (db *Database) DelPort(ctx context.Context, deviceID string, portID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530267 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530268 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530269 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530270 return err
271 }
272 return nil
273}
274
275// Device meter specific database actions
276
277// PutDeviceMeter to add device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530278func (db *Database) PutDeviceMeter(ctx context.Context, deviceID string, meterID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530279 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530280 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530281}
282
283// GetDeviceMeter to get device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530284func (db *Database) GetDeviceMeter(ctx context.Context, deviceID string, meterID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530285 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530286 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530287}
288
289// GetDeviceMeters to get multiple device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530290func (db *Database) GetDeviceMeters(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530291 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530292 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530293}
294
295// DelDeviceMeter to delete device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530296func (db *Database) DelDeviceMeter(ctx context.Context, deviceID string, meterID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530297 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530298 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530299 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530300 return err
301 }
302 return nil
303}
304
305// Service specific database actions
306
307// GetServices to get multiple services info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530308func (db *Database) GetServices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530309 key := GetKeyPath(ServicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530310 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530311}
312
313// GetService to get service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530314func (db *Database) GetService(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530315 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530316 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530317}
318
319// PutService to add service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530320func (db *Database) PutService(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530321 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530322 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530323}
324
325// DelService to delete service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530326func (db *Database) DelService(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530327 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530328 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530329 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530330 return err
331 }
332 return nil
333}
334
335// Virtual networks specific database actions
336
337// GetVnets to get multiple vnets info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530338func (db *Database) GetVnets(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530339 key := GetKeyPath(VnetPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530340 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530341}
342
343// GetVnet to get vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530344func (db *Database) GetVnet(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530345 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530346 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530347}
348
349// PutVnet to add vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530350func (db *Database) PutVnet(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530351 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530352 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530353}
354
355// DelVnet to delete vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530356func (db *Database) DelVnet(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530357 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530358 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530359 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530360 return err
361 }
362 return nil
363}
364
365// Virtual networks on ports specific database actions
366
367// GetVpvs to get multiple vpvs info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530368func (db *Database) GetVpvs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530369 key := GetKeyPath(VpvPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530370 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530371}
372
373// GetVpv to get vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530374func (db *Database) GetVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530375 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
376 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530377 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530378}
379
380// PutVpv to add vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530381func (db *Database) PutVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16, value 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.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530385}
386
387// DelVpv to delete vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530388func (db *Database) DelVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) 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 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530392 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530393 return err
394 }
395 return nil
396}
397
398// Virtual networks on ports specific database actions
399
400// GetMvlans to get multiple mvlans info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530401func (db *Database) GetMvlans(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530402 key := GetKeyPath(MvlanPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530403 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530404}
405
406// GetMvlan to get mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530407func (db *Database) GetMvlan(ctx context.Context, mvlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530408 name := strconv.FormatInt(int64(mvlan), 10)
409 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530410 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530411}
412
413// PutMvlan to add mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530414func (db *Database) PutMvlan(ctx context.Context, mvlan uint16, value 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.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530418}
419
420// DelMvlan to delete mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530421func (db *Database) DelMvlan(ctx context.Context, mvlan uint16) 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 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530425 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530426 return err
427 }
428 return nil
429}
430
431// database specific actions on IGMP config
432
433// DelIGMPCfg to delete icmp config
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530434func (db *Database) DelIGMPCfg(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530435 key := GetKeyPath(IgmpConfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530436 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530437 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530438 return err
439 }
440 return nil
441}
442
443// database specific actions on IGMP Profile
444
445// GetIgmpProfiles to get multiple igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530446func (db *Database) GetIgmpProfiles(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530447 key := GetKeyPath(IgmpProfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530448 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530449}
450
451// GetIgmpProfile to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530452func (db *Database) GetIgmpProfile(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530453 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530454 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530455}
456
457// PutIgmpProfile to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530458func (db *Database) PutIgmpProfile(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530459 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530460 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530461}
462
463// DelIgmpProfile to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530464func (db *Database) DelIgmpProfile(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530465 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530466 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530467 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530468 return err
469 }
470 return nil
471}
472
473// database specific actions on Mcast config Info
474
475// GetMcastConfigs to get multiple mcast config info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530476func (db *Database) GetMcastConfigs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530477 key := GetKeyPath(McastConfigPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530478 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530479}
480
481// GetMcastConfig to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530482func (db *Database) GetMcastConfig(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530483 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530484 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530485}
486
487// PutMcastConfig to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530488func (db *Database) PutMcastConfig(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530489 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530490 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530491}
492
493// DelMcastConfig to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530494func (db *Database) DelMcastConfig(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530495 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530496 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530497 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530498 return err
499 }
500 return nil
501}
502
503// database specific actions on health
504
505// GetHealth to get health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530506func (db *Database) GetHealth(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530507 key := GetKeyPath(HealthPath)
Akash Sonia8246972023-01-03 10:37:08 +0530508 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530509}
510
511// PutHealth to add health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530512func (db *Database) PutHealth(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530513 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530514 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530515}
516
517// DelHealth to delete health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530518func (db *Database) DelHealth(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530519 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530520 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530521 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530522 return err
523 }
524 return nil
525}
526
527// Meters
528
529// GetMeters to get multiple meters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530530func (db *Database) GetMeters(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530531 key := GetKeyPath(MeterPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530532 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530533}
534
535// GetMeter to get meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530536func (db *Database) GetMeter(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530537 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530538 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530539}
540
541// PutMeter to add meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530542func (db *Database) PutMeter(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530543 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530544 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530545}
546
547// DelMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530548func (db *Database) DelMeter(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530549 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530550 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530551 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530552 return err
553 }
554 return nil
555}
556
557// DelAllMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530558func (db *Database) DelAllMeter(ctx context.Context, device string) error {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530559 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530560 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530561 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530562 return err
563 }
564 logger.Infow(ctx, "Deleting all the meters for device", log.Fields{"device": device})
565 return nil
566}
567
568// IGMP groups
569
570// GetIgmpGroups to get multiple igmp groups info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530571func (db *Database) GetIgmpGroups(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530572 key := GetKeyPath(IgmpGroupPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530573 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530574}
575
576// GetIgmpGroup to get igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530577func (db *Database) GetIgmpGroup(ctx context.Context, id string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530578 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530579 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530580}
581
582// PutIgmpGroup to add igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530583func (db *Database) PutIgmpGroup(ctx context.Context, id string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530584 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530585 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530586}
587
588// DelIgmpGroup to delete igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530589func (db *Database) DelIgmpGroup(ctx context.Context, id string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530590 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530591 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530592 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530593 return err
594 }
595 return nil
596}
597
598// IGMP group devices
599
600// GetAllIgmpDevices to get multiple igmp devices info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530601func (db *Database) GetAllIgmpDevices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530602 key := GetKeyPath(IgmpDevicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530603 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530604}
605
606// GetPrevIgmpDevices to get previous igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530607func (db *Database) GetPrevIgmpDevices(ctx context.Context, mvlan of.VlanType, gid string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530608 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530609 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530610}
611
612// GetIgmpDevices to get igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530613func (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 +0530614 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530615 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530616}
617
618// GetIgmpDevice to get igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530619func (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 +0530620 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530621 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530622}
623
624// PutIgmpDevice to add igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530625func (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 +0530626 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530627 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530628}
629
630// DelIgmpDevice to delete igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530631func (db *Database) DelIgmpDevice(ctx context.Context, mvlan of.VlanType, gid string, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530632 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530633 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530634 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530635 return err
636 }
637 return nil
638}
639
640// IGMP group channels
641
642// GetAllIgmpChannels to get all igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530643func (db *Database) GetAllIgmpChannels(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530644 key := GetKeyPath(IgmpChannelPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530645 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530646}
647
648// GetPrevIgmpChannels to get previous igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530649func (db *Database) GetPrevIgmpChannels(ctx context.Context, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530650 key := GetKeyPath(IgmpChannelPath) + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530651 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530652}
653
654// GetIgmpChannels to get multiple igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530655func (db *Database) GetIgmpChannels(ctx context.Context, mvlan of.VlanType, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530656 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530657 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530658}
659
660// GetIgmpChannel to get igmp channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530661func (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 +0530662 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530663 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530664}
665
666// PutIgmpChannel to add igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530667func (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 +0530668 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530669 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530670}
671
672// DelIgmpChannel to delete igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530673func (db *Database) DelIgmpChannel(ctx context.Context, mvlan of.VlanType, gName string, device string, gip net.IP) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530674 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530675 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530676 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530677 return err
678 }
679 return nil
680}
681
682// IGMP group receivers
683
684// GetAllIgmpRcvrs to get all igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530685func (db *Database) GetAllIgmpRcvrs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530686 key := GetKeyPath(IgmpPortPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530687 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530688}
689
690// GetPrevIgmpRcvrs to get previous igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530691func (db *Database) GetPrevIgmpRcvrs(ctx context.Context, gip net.IP, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530692 key := GetKeyPath(IgmpPortPath) + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530693 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530694}
695
696// GetIgmpRcvrs to get multiple igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530697func (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 +0530698 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530699 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530700}
701
702// GetIgmpRcvr to get igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530703func (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 +0530704 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530705 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530706}
707
708// PutIgmpRcvr to add igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530709func (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 +0530710 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530711 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530712}
713
714// DelIgmpRcvr to delete igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530715func (db *Database) DelIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string, rcvr string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530716 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530717 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530718 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530719 return err
720 }
721 return nil
722}
723
724// DelAllIgmpRcvr to delete all igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530725func (db *Database) DelAllIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530726 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530727 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530728 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530729 return err
730 }
731 return nil
732}
733
734// DelAllRoutesForDevice to delete all routes for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530735func (db *Database) DelAllRoutesForDevice(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530736 /* service/vgc/v1/devices/<deviceID>/flows/ */
737 logger.Infow(ctx, "Deleting all the flows for device", log.Fields{"device": device})
738 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530739 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530740 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530741 return err
742 }
743 return nil
744}
745
746// PutNbDevicePort to add device port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530747func (db *Database) PutNbDevicePort(ctx context.Context, device string, ponPortID uint32, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530748 key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID)
749
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530750 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530751 logger.Warnw(ctx, "Put Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530752 }
753}
754
Akash Sonia8246972023-01-03 10:37:08 +0530755// GetServices to get multiple services info
756func (db *Database) GetDeviceConfig(ctx context.Context) (map[string]*kvstore.KVPair, error) {
757 key := GetKeyPath(DeviceConfigPath)
758 return db.List(ctx, key)
759}
760
761// PutSBDeviceConfig to add device info
762func (db *Database) PutDeviceConfig(ctx context.Context, serialNum string, value string) error {
763 key := GetKeyPath(DeviceConfigPath) + serialNum
764
765 if err := db.kvc.Put(ctx, key, value); err != nil {
766 logger.Warnw(ctx, "Put Device Config failed", log.Fields{"key": key})
767 return errorcodes.ErrFailedToUpdateDB
768 }
769 return nil
770}
771
Naveen Sampath04696f72022-06-13 15:19:14 +0530772// DelNbDevicePort to delete device port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530773func (db *Database) DelNbDevicePort(ctx context.Context, device string, ponPortID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530774 key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID)
775
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530776 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530777 logger.Warnw(ctx, "Delete Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530778 }
779}
780
781// GetAllNbPorts to get all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530782func (db *Database) GetAllNbPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530783 key := GetKeyPath(NbDevicePath) + deviceID + "/pon-port/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530784 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530785}
786
787//Functions for migration database
788
789// GetMigrationInfo to get migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530790func (db *Database) GetMigrationInfo(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530791 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530792 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530793}
794
795// PutMigrationInfo to add migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530796func (db *Database) PutMigrationInfo(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530797 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530798 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530799}
800
801// DelMigrationInfo to delete migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530802func (db *Database) DelMigrationInfo(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530803 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530804 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530805 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530806 return err
807 }
808 return nil
809}
810
811//PON counters
812
813// GetAllPonCounters to get all pon counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530814func (db *Database) GetAllPonCounters(ctx context.Context, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530815 key := GetKeyPath(PonCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530816 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530817}
818
819// GetPonCounter to get pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530820func (db *Database) GetPonCounter(ctx context.Context, device, ponID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530821 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530822 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530823}
824
825// PutPonCounter to add pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530826func (db *Database) PutPonCounter(ctx context.Context, device, ponID, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530827 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530828 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530829}
830
831// DelPonCounter to delete pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530832func (db *Database) DelPonCounter(ctx context.Context, device, ponID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530833 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530834 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530835 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530836 return err
837 }
838 return nil
839}
840
841//PON Channel counters
842
843// GetAllPonChannelCounters to get all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530844func (db *Database) GetAllPonChannelCounters(ctx context.Context, device, ponID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530845 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530846 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530847}
848
849// GetPonChannelCounter to get pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530850func (db *Database) GetPonChannelCounter(ctx context.Context, device, ponID, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530851 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530852 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530853}
854
855// PutPonChannelCounter to add pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530856func (db *Database) PutPonChannelCounter(ctx context.Context, device, ponID, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530857 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530858 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530859}
860
861// DelPonChannelCounter to delete pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530862func (db *Database) DelPonChannelCounter(ctx context.Context, device, ponID, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530863 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530864 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530865 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530866 return err
867 }
868 return nil
869}
870
871// DelAllPONCounters to delete all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530872func (db *Database) DelAllPONCounters(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530873 key := GetKeyPath(PonCounterPath) + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530874 return db.DeleteAll(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530875}
876
877// DelPONCounters to delete pon counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530878func (db *Database) DelPONCounters(ctx context.Context, device string, ponID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530879 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530880 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530881 logger.Warnw(ctx, "Delete Pon counters failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530882 }
883 //DeletePonCounter(device, ponID)
884}
885
886// PutOltIgmpCounters to add Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530887func (db *Database) PutOltIgmpCounters(ctx context.Context, device, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530888 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530889 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530890}
891
892// GetOltIgmpCounter to get Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530893func (db *Database) GetOltIgmpCounter(ctx context.Context, device string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530894 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530895 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530896}
897
898//Service Channel counters
899
900// GetAllServiceChannelCounters to get all service channel counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530901func (db *Database) GetAllServiceChannelCounters(ctx context.Context, serviceName string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530902 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530903 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530904}
905
906// GetServiceChannelCounter to get service channel counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530907func (db *Database) GetServiceChannelCounter(ctx context.Context, serviceName, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530908 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530909 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530910}
911
912// PutServiceChannelCounter to add service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530913func (db *Database) PutServiceChannelCounter(ctx context.Context, serviceName, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530914 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530915 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530916}
917
918// DelServiceChannelCounter to delete service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530919func (db *Database) DelServiceChannelCounter(ctx context.Context, serviceName, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530920 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530921 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530922 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530923 return err
924 }
925 return nil
926}
927
928// DelAllServiceChannelCounter to delete all service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530929func (db *Database) DelAllServiceChannelCounter(ctx context.Context, serviceName string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530930 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530931 return db.DeleteAllUnderHashKey(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530932}
933
934// OltExists to know if the ONU is added to the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530935func (db *Database) OltExists(ctx context.Context, deviceID string) bool {
936 if _, err := db.GetOlt(ctx, deviceID); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530937 return false
938 }
939 return true
Naveen Sampath04696f72022-06-13 15:19:14 +0530940}
941
942// PutFlowHash to add flowhash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530943func (db *Database) PutFlowHash(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530944 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530945 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530946}
947
948// GetFlowHash gets the flow hash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530949func (db *Database) GetFlowHash(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530950 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530951 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530952}
953
954// PutPortAlarmProfile to add port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530955func (db *Database) PutPortAlarmProfile(ctx context.Context, portAlarmProfileID string, value 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.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530958 logger.Warnw(ctx, "Put PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530959 }
960}
961
962// DelPortAlarmProfile to delete port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530963func (db *Database) DelPortAlarmProfile(ctx context.Context, portAlarmProfileID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530964 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530965 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530966 logger.Warnw(ctx, "Delete PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530967 }
968}
969
970// GetPortAlarmProfile to get port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530971func (db *Database) GetPortAlarmProfile(ctx context.Context, portAlarmProfileID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530972 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530973 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530974}
975
976// PutPortAlarmData to add port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530977func (db *Database) PutPortAlarmData(ctx context.Context, deviceID string, portID uint32, value string) {
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.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530980 logger.Warnw(ctx, "Put PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530981 }
982}
983
984// DelPortAlarmData to delete port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530985func (db *Database) DelPortAlarmData(ctx context.Context, deviceID string, portID uint32) {
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 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530988 logger.Warnw(ctx, "Delete PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530989 }
990}
991
992// GetPortAlarmData to get port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530993func (db *Database) GetPortAlarmData(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530994 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530995 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530996}
997
998// GetAllPortAlarmData to get port alarm data for all ports
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530999func (db *Database) GetAllPortAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301000 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301001 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301002}
1003
1004// PutSubAlarmData to add subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301005func (db *Database) PutSubAlarmData(ctx context.Context, deviceID string, portName string, value 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.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301008 logger.Warnw(ctx, "Put Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301009 }
1010}
1011
1012// DelSubAlarmData to delete subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301013func (db *Database) DelSubAlarmData(ctx context.Context, deviceID string, portName string) {
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 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301016 logger.Warnw(ctx, "Delete Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301017 }
1018}
1019
1020// GetSubAlarmData to get subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301021func (db *Database) GetSubAlarmData(ctx context.Context, deviceID string, portName string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301022 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301023 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301024}
1025
1026// GetAllSubAlarmData to get sub alarm data for all subscribers
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301027func (db *Database) GetAllSubAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301028 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301029 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301030}
1031
1032// Migrate Service req specific database actions
1033
1034// PutMigrateServicesReq to add MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301035func (db *Database) PutMigrateServicesReq(ctx context.Context, deviceID string, vnet string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301036 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301037 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +05301038}
1039
1040// GetMigrateServicesReq to get MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301041func (db *Database) GetMigrateServicesReq(ctx context.Context, deviceID string, vnet string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301042 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301043 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301044}
1045
1046// GetAllMigrateServicesReq to get multiple MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301047func (db *Database) GetAllMigrateServicesReq(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301048 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301049 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301050}
1051
1052// DelMigrateServicesReq to delete MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301053func (db *Database) DelMigrateServicesReq(ctx context.Context, deviceID string, vnet string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301054 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301055 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301056 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301057 return err
1058 }
1059 return nil
1060}
1061
1062// DelAllMigrateServicesReq to delete all MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301063func (db *Database) DelAllMigrateServicesReq(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301064 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301065 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301066 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301067 return err
1068 }
1069 logger.Infow(ctx, "Deleting all the Update Vnet Requests for device", log.Fields{"device": deviceID})
1070 return nil
1071}
Akash Sonidedc8ee2023-03-03 11:51:49 +05301072
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301073// PutOltFlowService to add OltFlowService info
1074func (db *Database) PutOltFlowService(ctx context.Context, value string) error {
1075 key := GetKeyPath(OltFlowServicePath)
Naveen Sampath04696f72022-06-13 15:19:14 +05301076
Akash Sonidedc8ee2023-03-03 11:51:49 +05301077 if err := db.kvc.Put(ctx, key, value); err != nil {
1078 logger.Warnw(ctx, "Put OltFlowService failed", log.Fields{"key": key})
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301079 return err
Akash Sonidedc8ee2023-03-03 11:51:49 +05301080 }
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301081 return nil
1082}
1083
1084// GetOltFlowService to get OltFlowService info
1085func (db *Database) GetOltFlowService(ctx context.Context) (string, error) {
1086 key := GetKeyPath(OltFlowServicePath)
1087 return db.Get(ctx, key)
1088}
Naveen Sampath04696f72022-06-13 15:19:14 +05301089func init() {
Akash Sonia8246972023-01-03 10:37:08 +05301090 // Setup this package so that it's log level can be modified at run time
1091 var err error
1092 logger, err = log.AddPackageWithDefaultParam()
1093 if err != nil {
1094 panic(err)
1095 }
Naveen Sampath04696f72022-06-13 15:19:14 +05301096}