blob: 0178a077b67a900487236d7e454e12455c09654c [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
Abhay Kumar6040f382024-07-12 09:29:23 +053065 case "redis-sentinel":
66 database.kvc, err = kvstore.NewRedisClient(address, time.Duration(timeout), true)
67 return &database, err
Akash Sonidedc8ee2023-03-03 11:51:49 +053068 case "etcd":
69 database.kvc, err = kvstore.NewEtcdClient(ctx, address, time.Duration(timeout), log.ErrorLevel)
70 return &database, err
Naveen Sampath04696f72022-06-13 15:19:14 +053071 }
72 return &database, errors.New("unsupported-kv-store")
73}
74
75// Utility function that retrieves value for a key. It is assumed that
76// the information is always a string and the data retrieved is returned
77// as a string
78
79// Put to add value to database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053080func (db *Database) Put(ctx context.Context, fullKeyPath, value string) error {
81 return db.kvc.Put(ctx, fullKeyPath, value)
Naveen Sampath04696f72022-06-13 15:19:14 +053082}
83
84// Get to retrieve value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053085func (db *Database) Get(ctx context.Context, key string) (string, error) {
86 kv, err := db.kvc.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +053087 if err != nil {
88 return "", err
89 }
90 if kv != nil {
91 return string(kv.Value.([]byte)), nil
92 }
93 return "", errors.New("Value not found")
94}
95
96// Del to delete value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053097func (db *Database) Del(ctx context.Context, fullPath string) error {
98 if err := db.kvc.Delete(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +053099 logger.Errorw(ctx, "The path doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530100 return err
101 }
102 return nil
103}
104
105// DeleteAll to delete all value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530106func (db *Database) DeleteAll(ctx context.Context, fullPath string) error {
107 if err := db.kvc.DeleteWithPrefix(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530108 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530109 return err
110 }
111 return nil
112}
113
114// DeleteAllUnderHashKey to delete all values under hash key
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530115func (db *Database) DeleteAllUnderHashKey(ctx context.Context, hashKeyPrefix string) error {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530116 kv, err := db.kvc.List(ctx, hashKeyPrefix)
117 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530118 logger.Errorw(ctx, "The key path doesn't exist", log.Fields{"key": hashKeyPrefix, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530119 return err
120 }
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530121 for key := range kv {
122 if err := db.kvc.Delete(ctx, key); err != nil {
123 logger.Errorw(ctx, "Delete key from DB Failed", log.Fields{"key": key, "Error": err})
124 }
125 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530126 return nil
127}
128
129// List to list the values
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530130func (db *Database) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
131 kv, err := db.kvc.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530132 if err != nil {
133 return nil, err
134 }
135 if kv != nil {
136 return kv, nil
137 }
138 return nil, errors.New("Value not found")
139}
140
141// OLT specific database items
142
143// GetOlt to get olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530144func (db *Database) GetOlt(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530145 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530146 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530147}
148
149// PutOlt to add olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530150func (db *Database) PutOlt(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530151 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530152 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530153}
154
155// DelOlt to delete olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530156func (db *Database) DelOlt(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530157 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530158 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530159 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530160 return err
161 }
162 return nil
163}
164
165// Flows specific database actions
166
167// PutFlow to add flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530168func (db *Database) PutFlow(ctx context.Context, deviceID string, flowID uint64, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530169 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530170 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530171}
172
173// GetFlow to get flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530174func (db *Database) GetFlow(ctx context.Context, deviceID string, flowID uint64) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530175 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530176 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530177}
178
179// GetFlows to get multiple flows
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530180func (db *Database) GetFlows(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530181 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530182 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530183}
184
185// DelFlow to delete flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530186func (db *Database) DelFlow(ctx context.Context, deviceID string, flowID uint64) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530187 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530188 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530189 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530190 return err
191 }
192 return nil
193}
194
195// Group specific database actions
196
197// PutGroup to add group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530198func (db *Database) PutGroup(ctx context.Context, deviceID string, groupID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530199 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530200 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530201}
202
203// GetGroup to get group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530204func (db *Database) GetGroup(ctx context.Context, deviceID string, groupID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530205 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530206 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530207}
208
209// GetGroups to get multiple group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530210func (db *Database) GetGroups(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530211 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
212 logger.Infow(ctx, "key", log.Fields{"Key": key})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530213 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530214}
215
216// DelGroup to delete group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530217func (db *Database) DelGroup(ctx context.Context, deviceID string, groupID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530218 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530219 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530220 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530221 return err
222 }
223 return nil
224}
225
226// DelAllGroup to delete all group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530227func (db *Database) DelAllGroup(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530228 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530229 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530230 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530231 return err
232 }
233 logger.Infow(ctx, "Deleting all the groups for device", log.Fields{"device": deviceID})
234 return nil
235}
236
237// DelAllPorts to delete all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530238func (db *Database) DelAllPorts(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530239 key := fmt.Sprintf(GetKeyPath(DevicePortPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530240 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530241 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530242 return err
243 }
244 logger.Infow(ctx, "Deleting all the ports for device", log.Fields{"device": device})
245 return nil
246}
247
248// Ports specific database actions
249
250// PutPort to add port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530251func (db *Database) PutPort(ctx context.Context, deviceID string, portID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530252 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530253 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530254}
255
256// GetPort to get port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530257func (db *Database) GetPort(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530258 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530259 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530260}
261
262// GetPorts to get multiple ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530263func (db *Database) GetPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530264 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530265 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530266}
267
268// DelPort to delete port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530269func (db *Database) DelPort(ctx context.Context, deviceID string, portID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530270 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530271 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530272 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530273 return err
274 }
275 return nil
276}
277
278// Device meter specific database actions
279
280// PutDeviceMeter to add device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530281func (db *Database) PutDeviceMeter(ctx context.Context, deviceID string, meterID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530282 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530283 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530284}
285
286// GetDeviceMeter to get device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530287func (db *Database) GetDeviceMeter(ctx context.Context, deviceID string, meterID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530288 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530289 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530290}
291
292// GetDeviceMeters to get multiple device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530293func (db *Database) GetDeviceMeters(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530294 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530295 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530296}
297
298// DelDeviceMeter to delete device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530299func (db *Database) DelDeviceMeter(ctx context.Context, deviceID string, meterID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530300 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530301 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530302 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530303 return err
304 }
305 return nil
306}
307
308// Service specific database actions
309
310// GetServices to get multiple services info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530311func (db *Database) GetServices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530312 key := GetKeyPath(ServicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530313 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530314}
315
316// GetService to get service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530317func (db *Database) GetService(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530318 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530319 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530320}
321
322// PutService to add service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530323func (db *Database) PutService(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530324 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530325 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530326}
327
328// DelService to delete service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530329func (db *Database) DelService(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530330 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530331 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530332 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530333 return err
334 }
335 return nil
336}
337
338// Virtual networks specific database actions
339
340// GetVnets to get multiple vnets info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530341func (db *Database) GetVnets(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530342 key := GetKeyPath(VnetPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530343 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530344}
345
346// GetVnet to get vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530347func (db *Database) GetVnet(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530348 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530349 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530350}
351
352// PutVnet to add vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530353func (db *Database) PutVnet(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530354 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530355 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530356}
357
358// DelVnet to delete vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530359func (db *Database) DelVnet(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530360 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530361 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530362 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530363 return err
364 }
365 return nil
366}
367
368// Virtual networks on ports specific database actions
369
370// GetVpvs to get multiple vpvs info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530371func (db *Database) GetVpvs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530372 key := GetKeyPath(VpvPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530373 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530374}
375
376// GetVpv to get vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530377func (db *Database) GetVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530378 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
379 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530380 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530381}
382
383// PutVpv to add vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530384func (db *Database) PutVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530385 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
386 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530387 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530388}
389
390// DelVpv to delete vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530391func (db *Database) DelVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530392 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
393 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530394 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530395 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530396 return err
397 }
398 return nil
399}
400
401// Virtual networks on ports specific database actions
402
403// GetMvlans to get multiple mvlans info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530404func (db *Database) GetMvlans(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530405 key := GetKeyPath(MvlanPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530406 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530407}
408
409// GetMvlan to get mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530410func (db *Database) GetMvlan(ctx context.Context, mvlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530411 name := strconv.FormatInt(int64(mvlan), 10)
412 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530413 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530414}
415
416// PutMvlan to add mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530417func (db *Database) PutMvlan(ctx context.Context, mvlan uint16, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530418 name := strconv.FormatInt(int64(mvlan), 10)
419 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530420 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530421}
422
423// DelMvlan to delete mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530424func (db *Database) DelMvlan(ctx context.Context, mvlan uint16) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530425 name := strconv.FormatInt(int64(mvlan), 10)
426 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530427 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530428 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530429 return err
430 }
431 return nil
432}
433
434// database specific actions on IGMP config
435
436// DelIGMPCfg to delete icmp config
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530437func (db *Database) DelIGMPCfg(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530438 key := GetKeyPath(IgmpConfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530439 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530440 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530441 return err
442 }
443 return nil
444}
445
446// database specific actions on IGMP Profile
447
448// GetIgmpProfiles to get multiple igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530449func (db *Database) GetIgmpProfiles(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530450 key := GetKeyPath(IgmpProfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530451 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530452}
453
454// GetIgmpProfile to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530455func (db *Database) GetIgmpProfile(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530456 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530457 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530458}
459
460// PutIgmpProfile to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530461func (db *Database) PutIgmpProfile(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530462 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530463 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530464}
465
466// DelIgmpProfile to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530467func (db *Database) DelIgmpProfile(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530468 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530469 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530470 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530471 return err
472 }
473 return nil
474}
475
476// database specific actions on Mcast config Info
477
478// GetMcastConfigs to get multiple mcast config info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530479func (db *Database) GetMcastConfigs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530480 key := GetKeyPath(McastConfigPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530481 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530482}
483
484// GetMcastConfig to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530485func (db *Database) GetMcastConfig(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530486 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530487 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530488}
489
490// PutMcastConfig to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530491func (db *Database) PutMcastConfig(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530492 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530493 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530494}
495
496// DelMcastConfig to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530497func (db *Database) DelMcastConfig(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530498 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530499 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530500 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530501 return err
502 }
503 return nil
504}
505
506// database specific actions on health
507
508// GetHealth to get health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530509func (db *Database) GetHealth(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530510 key := GetKeyPath(HealthPath)
Akash Sonia8246972023-01-03 10:37:08 +0530511 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530512}
513
514// PutHealth to add health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530515func (db *Database) PutHealth(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530516 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530517 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530518}
519
520// DelHealth to delete health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530521func (db *Database) DelHealth(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530522 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530523 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530524 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530525 return err
526 }
527 return nil
528}
529
530// Meters
531
532// GetMeters to get multiple meters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530533func (db *Database) GetMeters(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530534 key := GetKeyPath(MeterPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530535 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530536}
537
538// GetMeter to get meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530539func (db *Database) GetMeter(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530540 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530541 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530542}
543
544// PutMeter to add meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530545func (db *Database) PutMeter(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530546 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530547 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530548}
549
550// DelMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530551func (db *Database) DelMeter(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530552 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530553 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530554 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530555 return err
556 }
557 return nil
558}
559
560// DelAllMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530561func (db *Database) DelAllMeter(ctx context.Context, device string) error {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530562 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530563 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530564 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530565 return err
566 }
567 logger.Infow(ctx, "Deleting all the meters for device", log.Fields{"device": device})
568 return nil
569}
570
571// IGMP groups
572
573// GetIgmpGroups to get multiple igmp groups info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530574func (db *Database) GetIgmpGroups(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530575 key := GetKeyPath(IgmpGroupPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530576 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530577}
578
579// GetIgmpGroup to get igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530580func (db *Database) GetIgmpGroup(ctx context.Context, id string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530581 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530582 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530583}
584
585// PutIgmpGroup to add igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530586func (db *Database) PutIgmpGroup(ctx context.Context, id string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530587 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530588 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530589}
590
591// DelIgmpGroup to delete igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530592func (db *Database) DelIgmpGroup(ctx context.Context, id string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530593 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530594 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530595 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530596 return err
597 }
598 return nil
599}
600
601// IGMP group devices
602
603// GetAllIgmpDevices to get multiple igmp devices info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530604func (db *Database) GetAllIgmpDevices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530605 key := GetKeyPath(IgmpDevicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530606 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530607}
608
609// GetPrevIgmpDevices to get previous igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530610func (db *Database) GetPrevIgmpDevices(ctx context.Context, mvlan of.VlanType, gid string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530611 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530612 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530613}
614
615// GetIgmpDevices to get igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530616func (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 +0530617 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530618 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530619}
620
621// GetIgmpDevice to get igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530622func (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 +0530623 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530624 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530625}
626
627// PutIgmpDevice to add igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530628func (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 +0530629 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530630 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530631}
632
633// DelIgmpDevice to delete igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530634func (db *Database) DelIgmpDevice(ctx context.Context, mvlan of.VlanType, gid string, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530635 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530636 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530637 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530638 return err
639 }
640 return nil
641}
642
643// IGMP group channels
644
645// GetAllIgmpChannels to get all igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530646func (db *Database) GetAllIgmpChannels(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530647 key := GetKeyPath(IgmpChannelPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530648 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530649}
650
651// GetPrevIgmpChannels to get previous igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530652func (db *Database) GetPrevIgmpChannels(ctx context.Context, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530653 key := GetKeyPath(IgmpChannelPath) + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530654 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530655}
656
657// GetIgmpChannels to get multiple igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530658func (db *Database) GetIgmpChannels(ctx context.Context, mvlan of.VlanType, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530659 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530660 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530661}
662
663// GetIgmpChannel to get igmp channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530664func (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 +0530665 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530666 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530667}
668
669// PutIgmpChannel to add igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530670func (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 +0530671 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530672 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530673}
674
675// DelIgmpChannel to delete igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530676func (db *Database) DelIgmpChannel(ctx context.Context, mvlan of.VlanType, gName string, device string, gip net.IP) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530677 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530678 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530679 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530680 return err
681 }
682 return nil
683}
684
685// IGMP group receivers
686
687// GetAllIgmpRcvrs to get all igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530688func (db *Database) GetAllIgmpRcvrs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530689 key := GetKeyPath(IgmpPortPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530690 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530691}
692
693// GetPrevIgmpRcvrs to get previous igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530694func (db *Database) GetPrevIgmpRcvrs(ctx context.Context, gip net.IP, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530695 key := GetKeyPath(IgmpPortPath) + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530696 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530697}
698
699// GetIgmpRcvrs to get multiple igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530700func (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 +0530701 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530702 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530703}
704
705// GetIgmpRcvr to get igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530706func (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 +0530707 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530708 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530709}
710
711// PutIgmpRcvr to add igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530712func (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 +0530713 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530714 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530715}
716
717// DelIgmpRcvr to delete igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530718func (db *Database) DelIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string, rcvr string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530719 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530720 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530721 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530722 return err
723 }
724 return nil
725}
726
727// DelAllIgmpRcvr to delete all igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530728func (db *Database) DelAllIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530729 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530730 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530731 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530732 return err
733 }
734 return nil
735}
736
737// DelAllRoutesForDevice to delete all routes for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530738func (db *Database) DelAllRoutesForDevice(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530739 /* service/vgc/v1/devices/<deviceID>/flows/ */
740 logger.Infow(ctx, "Deleting all the flows for device", log.Fields{"device": device})
741 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530742 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530743 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530744 return err
745 }
746 return nil
747}
748
749// PutNbDevicePort to add device port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530750func (db *Database) PutNbDevicePort(ctx context.Context, device string, ponPortID uint32, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530751 key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID)
752
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530753 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530754 logger.Warnw(ctx, "Put Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530755 }
756}
757
Akash Sonia8246972023-01-03 10:37:08 +0530758// GetServices to get multiple services info
759func (db *Database) GetDeviceConfig(ctx context.Context) (map[string]*kvstore.KVPair, error) {
760 key := GetKeyPath(DeviceConfigPath)
761 return db.List(ctx, key)
762}
763
764// PutSBDeviceConfig to add device info
765func (db *Database) PutDeviceConfig(ctx context.Context, serialNum string, value string) error {
766 key := GetKeyPath(DeviceConfigPath) + serialNum
767
768 if err := db.kvc.Put(ctx, key, value); err != nil {
769 logger.Warnw(ctx, "Put Device Config failed", log.Fields{"key": key})
770 return errorcodes.ErrFailedToUpdateDB
771 }
772 return nil
773}
774
Naveen Sampath04696f72022-06-13 15:19:14 +0530775// DelNbDevicePort to delete device port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530776func (db *Database) DelNbDevicePort(ctx context.Context, device string, ponPortID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530777 key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID)
778
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530779 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530780 logger.Warnw(ctx, "Delete Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530781 }
782}
783
784// GetAllNbPorts to get all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530785func (db *Database) GetAllNbPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530786 key := GetKeyPath(NbDevicePath) + deviceID + "/pon-port/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530787 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530788}
789
790//Functions for migration database
791
792// GetMigrationInfo to get migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530793func (db *Database) GetMigrationInfo(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530794 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530795 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530796}
797
798// PutMigrationInfo to add migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530799func (db *Database) PutMigrationInfo(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530800 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530801 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530802}
803
804// DelMigrationInfo to delete migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530805func (db *Database) DelMigrationInfo(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530806 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530807 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530808 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530809 return err
810 }
811 return nil
812}
813
814//PON counters
815
816// GetAllPonCounters to get all pon counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530817func (db *Database) GetAllPonCounters(ctx context.Context, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530818 key := GetKeyPath(PonCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530819 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530820}
821
822// GetPonCounter to get pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530823func (db *Database) GetPonCounter(ctx context.Context, device, ponID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530824 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530825 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530826}
827
828// PutPonCounter to add pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530829func (db *Database) PutPonCounter(ctx context.Context, device, ponID, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530830 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530831 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530832}
833
834// DelPonCounter to delete pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530835func (db *Database) DelPonCounter(ctx context.Context, device, ponID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530836 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530837 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530838 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530839 return err
840 }
841 return nil
842}
843
844//PON Channel counters
845
846// GetAllPonChannelCounters to get all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530847func (db *Database) GetAllPonChannelCounters(ctx context.Context, device, ponID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530848 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530849 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530850}
851
852// GetPonChannelCounter to get pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530853func (db *Database) GetPonChannelCounter(ctx context.Context, device, ponID, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530854 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530855 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530856}
857
858// PutPonChannelCounter to add pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530859func (db *Database) PutPonChannelCounter(ctx context.Context, device, ponID, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530860 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530861 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530862}
863
864// DelPonChannelCounter to delete pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530865func (db *Database) DelPonChannelCounter(ctx context.Context, device, ponID, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530866 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530867 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530868 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530869 return err
870 }
871 return nil
872}
873
874// DelAllPONCounters to delete all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530875func (db *Database) DelAllPONCounters(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530876 key := GetKeyPath(PonCounterPath) + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530877 return db.DeleteAll(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530878}
879
880// DelPONCounters to delete pon counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530881func (db *Database) DelPONCounters(ctx context.Context, device string, ponID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530882 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530883 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530884 logger.Warnw(ctx, "Delete Pon counters failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530885 }
886 //DeletePonCounter(device, ponID)
887}
888
889// PutOltIgmpCounters to add Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530890func (db *Database) PutOltIgmpCounters(ctx context.Context, device, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530891 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530892 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530893}
894
895// GetOltIgmpCounter to get Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530896func (db *Database) GetOltIgmpCounter(ctx context.Context, device string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530897 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530898 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530899}
900
901//Service Channel counters
902
903// GetAllServiceChannelCounters to get all service channel counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530904func (db *Database) GetAllServiceChannelCounters(ctx context.Context, serviceName string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530905 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530906 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530907}
908
909// GetServiceChannelCounter to get service channel counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530910func (db *Database) GetServiceChannelCounter(ctx context.Context, serviceName, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530911 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530912 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530913}
914
915// PutServiceChannelCounter to add service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530916func (db *Database) PutServiceChannelCounter(ctx context.Context, serviceName, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530917 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530918 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530919}
920
921// DelServiceChannelCounter to delete service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530922func (db *Database) DelServiceChannelCounter(ctx context.Context, serviceName, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530923 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530924 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530925 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530926 return err
927 }
928 return nil
929}
930
931// DelAllServiceChannelCounter to delete all service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530932func (db *Database) DelAllServiceChannelCounter(ctx context.Context, serviceName string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530933 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530934 return db.DeleteAllUnderHashKey(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530935}
936
937// OltExists to know if the ONU is added to the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530938func (db *Database) OltExists(ctx context.Context, deviceID string) bool {
939 if _, err := db.GetOlt(ctx, deviceID); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530940 return false
941 }
942 return true
Naveen Sampath04696f72022-06-13 15:19:14 +0530943}
944
945// PutFlowHash to add flowhash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530946func (db *Database) PutFlowHash(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530947 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530948 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530949}
950
951// GetFlowHash gets the flow hash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530952func (db *Database) GetFlowHash(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530953 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530954 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530955}
956
957// PutPortAlarmProfile to add port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530958func (db *Database) PutPortAlarmProfile(ctx context.Context, portAlarmProfileID string, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530959 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530960 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530961 logger.Warnw(ctx, "Put PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530962 }
963}
964
965// DelPortAlarmProfile to delete port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530966func (db *Database) DelPortAlarmProfile(ctx context.Context, portAlarmProfileID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530967 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530968 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530969 logger.Warnw(ctx, "Delete PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530970 }
971}
972
973// GetPortAlarmProfile to get port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530974func (db *Database) GetPortAlarmProfile(ctx context.Context, portAlarmProfileID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530975 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530976 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530977}
978
979// PutPortAlarmData to add port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530980func (db *Database) PutPortAlarmData(ctx context.Context, deviceID string, portID uint32, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530981 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530982 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530983 logger.Warnw(ctx, "Put PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530984 }
985}
986
987// DelPortAlarmData to delete port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530988func (db *Database) DelPortAlarmData(ctx context.Context, deviceID string, portID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530989 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530990 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530991 logger.Warnw(ctx, "Delete PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530992 }
993}
994
995// GetPortAlarmData to get port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530996func (db *Database) GetPortAlarmData(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530997 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530998 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530999}
1000
1001// GetAllPortAlarmData to get port alarm data for all ports
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301002func (db *Database) GetAllPortAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301003 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301004 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301005}
1006
1007// PutSubAlarmData to add subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301008func (db *Database) PutSubAlarmData(ctx context.Context, deviceID string, portName string, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301009 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301010 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301011 logger.Warnw(ctx, "Put Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301012 }
1013}
1014
1015// DelSubAlarmData to delete subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301016func (db *Database) DelSubAlarmData(ctx context.Context, deviceID string, portName string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301017 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301018 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301019 logger.Warnw(ctx, "Delete Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +05301020 }
1021}
1022
1023// GetSubAlarmData to get subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301024func (db *Database) GetSubAlarmData(ctx context.Context, deviceID string, portName string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301025 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301026 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301027}
1028
1029// GetAllSubAlarmData to get sub alarm data for all subscribers
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301030func (db *Database) GetAllSubAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301031 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301032 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301033}
1034
1035// Migrate Service req specific database actions
1036
1037// PutMigrateServicesReq to add MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301038func (db *Database) PutMigrateServicesReq(ctx context.Context, deviceID string, vnet string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301039 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301040 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +05301041}
1042
1043// GetMigrateServicesReq to get MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301044func (db *Database) GetMigrateServicesReq(ctx context.Context, deviceID string, vnet string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301045 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301046 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301047}
1048
1049// GetAllMigrateServicesReq to get multiple MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301050func (db *Database) GetAllMigrateServicesReq(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301051 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301052 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301053}
1054
1055// DelMigrateServicesReq to delete MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301056func (db *Database) DelMigrateServicesReq(ctx context.Context, deviceID string, vnet string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301057 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301058 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301059 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301060 return err
1061 }
1062 return nil
1063}
1064
1065// DelAllMigrateServicesReq to delete all MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301066func (db *Database) DelAllMigrateServicesReq(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301067 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301068 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301069 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301070 return err
1071 }
1072 logger.Infow(ctx, "Deleting all the Update Vnet Requests for device", log.Fields{"device": deviceID})
1073 return nil
1074}
Akash Sonidedc8ee2023-03-03 11:51:49 +05301075
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301076// PutOltFlowService to add OltFlowService info
1077func (db *Database) PutOltFlowService(ctx context.Context, value string) error {
1078 key := GetKeyPath(OltFlowServicePath)
Naveen Sampath04696f72022-06-13 15:19:14 +05301079
Akash Sonidedc8ee2023-03-03 11:51:49 +05301080 if err := db.kvc.Put(ctx, key, value); err != nil {
1081 logger.Warnw(ctx, "Put OltFlowService failed", log.Fields{"key": key})
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301082 return err
Akash Sonidedc8ee2023-03-03 11:51:49 +05301083 }
Tinoj Joseph4ead4e02023-01-30 03:12:44 +05301084 return nil
1085}
1086
1087// GetOltFlowService to get OltFlowService info
1088func (db *Database) GetOltFlowService(ctx context.Context) (string, error) {
1089 key := GetKeyPath(OltFlowServicePath)
1090 return db.Get(ctx, key)
1091}
Naveen Sampath04696f72022-06-13 15:19:14 +05301092func init() {
Akash Sonia8246972023-01-03 10:37:08 +05301093 // Setup this package so that it's log level can be modified at run time
1094 var err error
1095 logger, err = log.AddPackageWithDefaultParam()
1096 if err != nil {
1097 panic(err)
1098 }
Naveen Sampath04696f72022-06-13 15:19:14 +05301099}