blob: 0344e7d7f3abdc50ce8d435867300f0c98a4f764 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15// This implementation of database assumes that it is working for
16// Open ONU adapter. Thus, it assumes some base path for all the
17// database operations. For all database operations, the key passed is
18// added to the database base path.
19
20package database
21
22import (
23 "context"
24 "errors"
25 "net"
26 "strconv"
27 "time"
28 "fmt"
29
30 "voltha-go-controller/internal/pkg/of"
31 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
Tinoj Joseph1d108322022-07-13 10:07:39 +053032 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053033)
34
35var logger log.CLogger
Naveen Sampath04696f72022-06-13 15:19:14 +053036
37// Database structure
38type Database struct {
39 storeType string
40 address string
41 //timeout uint32
42 kvc kvstore.Client
43}
44
45// Initialize the database module. The database module runs as a singleton
46// object and is initialized when the adapter is created.
Tinoj Joseph07cc5372022-07-18 22:53:51 +053047func Initialize(ctx context.Context, storeType string, address string, timeout int) (*Database, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +053048 var err error
49 var database Database
50 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
51 database.address = address
52 database.storeType = storeType
53 switch storeType {
54 case "redis":
55 database.kvc, err = kvstore.NewRedisClient(address, time.Duration(timeout), false)
56 return &database, err
57 }
58 return &database, errors.New("unsupported-kv-store")
59}
60
61// Utility function that retrieves value for a key. It is assumed that
62// the information is always a string and the data retrieved is returned
63// as a string
64
65// Put to add value to database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053066func (db *Database) Put(ctx context.Context, fullKeyPath, value string) error {
67 return db.kvc.Put(ctx, fullKeyPath, value)
Naveen Sampath04696f72022-06-13 15:19:14 +053068}
69
70// Get to retrieve value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053071func (db *Database) Get(ctx context.Context, key string) (string, error) {
72 kv, err := db.kvc.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +053073 if err != nil {
74 return "", err
75 }
76 if kv != nil {
77 return string(kv.Value.([]byte)), nil
78 }
79 return "", errors.New("Value not found")
80}
81
82// Del to delete value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053083func (db *Database) Del(ctx context.Context, fullPath string) error {
84 if err := db.kvc.Delete(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +053085 logger.Errorw(ctx, "The path doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +053086 return err
87 }
88 return nil
89}
90
91// DeleteAll to delete all value from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +053092func (db *Database) DeleteAll(ctx context.Context, fullPath string) error {
93 if err := db.kvc.DeleteWithPrefix(ctx, fullPath); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +053094 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": fullPath, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +053095 return err
96 }
97 return nil
98}
99
100// DeleteAllUnderHashKey to delete all values under hash key
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530101func (db *Database) DeleteAllUnderHashKey(ctx context.Context, hashKeyPrefix string) error {
102 if err := db.kvc.Delete(ctx, hashKeyPrefix); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530103 logger.Errorw(ctx, "The key path doesn't exist", log.Fields{"key": hashKeyPrefix, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530104 return err
105 }
106 return nil
107}
108
109// List to list the values
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530110func (db *Database) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
111 kv, err := db.kvc.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530112 if err != nil {
113 return nil, err
114 }
115 if kv != nil {
116 return kv, nil
117 }
118 return nil, errors.New("Value not found")
119}
120
121// OLT specific database items
122
123// GetOlt to get olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530124func (db *Database) GetOlt(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530125 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530126 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530127}
128
129// PutOlt to add olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530130func (db *Database) PutOlt(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530131 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530132 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530133}
134
135// DelOlt to delete olt info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530136func (db *Database) DelOlt(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530137 key := fmt.Sprintf(GetKeyPath(DevicePath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530138 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530139 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530140 return err
141 }
142 return nil
143}
144
145// Flows specific database actions
146
147// PutFlow to add flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530148func (db *Database) PutFlow(ctx context.Context, deviceID string, flowID uint64, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530149 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530150 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530151}
152
153// GetFlow to get flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530154func (db *Database) GetFlow(ctx context.Context, deviceID string, flowID uint64) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530155 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530156 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530157}
158
159// GetFlows to get multiple flows
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530160func (db *Database) GetFlows(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530161 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530162 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530163}
164
165// DelFlow to delete flow
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530166func (db *Database) DelFlow(ctx context.Context, deviceID string, flowID uint64) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530167 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID) + strconv.FormatUint(flowID, 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530168 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530169 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530170 return err
171 }
172 return nil
173}
174
175// Group specific database actions
176
177// PutGroup to add group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530178func (db *Database) PutGroup(ctx context.Context, deviceID string, groupID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530179 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530180 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530181}
182
183// GetGroup to get group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530184func (db *Database) GetGroup(ctx context.Context, deviceID string, groupID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530185 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530186 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530187}
188
189// GetGroups to get multiple group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530190func (db *Database) GetGroups(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530191 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
192 logger.Infow(ctx, "key", log.Fields{"Key": key})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530193 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530194}
195
196// DelGroup to delete group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530197func (db *Database) DelGroup(ctx context.Context, deviceID string, groupID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530198 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID) + strconv.FormatUint(uint64(groupID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530199 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530200 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530201 return err
202 }
203 return nil
204}
205
206// DelAllGroup to delete all group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530207func (db *Database) DelAllGroup(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530208 key := fmt.Sprintf(GetKeyPath(DeviceGroupPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530209 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530210 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530211 return err
212 }
213 logger.Infow(ctx, "Deleting all the groups for device", log.Fields{"device": deviceID})
214 return nil
215}
216
217// DelAllPorts to delete all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530218func (db *Database) DelAllPorts(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530219 key := fmt.Sprintf(GetKeyPath(DevicePortPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530220 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530221 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530222 return err
223 }
224 logger.Infow(ctx, "Deleting all the ports for device", log.Fields{"device": device})
225 return nil
226}
227
228// Ports specific database actions
229
230// PutPort to add port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530231func (db *Database) PutPort(ctx context.Context, deviceID string, portID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530232 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530233 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530234}
235
236// GetPort to get port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530237func (db *Database) GetPort(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530238 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530239 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530240}
241
242// GetPorts to get multiple ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530243func (db *Database) GetPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530244 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530245 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530246}
247
248// DelPort to delete port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530249func (db *Database) DelPort(ctx context.Context, deviceID string, portID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530250 key := fmt.Sprintf(GetKeyPath(DevicePortPath), deviceID) + strconv.FormatUint(uint64(portID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530251 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530252 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530253 return err
254 }
255 return nil
256}
257
258// Device meter specific database actions
259
260// PutDeviceMeter to add device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530261func (db *Database) PutDeviceMeter(ctx context.Context, deviceID string, meterID uint32, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530262 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530263 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530264}
265
266// GetDeviceMeter to get device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530267func (db *Database) GetDeviceMeter(ctx context.Context, deviceID string, meterID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530268 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530269 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530270}
271
272// GetDeviceMeters to get multiple device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530273func (db *Database) GetDeviceMeters(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530274 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530275 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530276}
277
278// DelDeviceMeter to delete device meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530279func (db *Database) DelDeviceMeter(ctx context.Context, deviceID string, meterID uint32) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530280 key := fmt.Sprintf(GetKeyPath(DeviceMeterPath), deviceID) + strconv.FormatUint(uint64(meterID), 10)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530281 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530282 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530283 return err
284 }
285 return nil
286}
287
288// Service specific database actions
289
290// GetServices to get multiple services info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530291func (db *Database) GetServices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530292 key := GetKeyPath(ServicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530293 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530294}
295
296// GetService to get service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530297func (db *Database) GetService(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530298 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530299 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530300}
301
302// PutService to add service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530303func (db *Database) PutService(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530304 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530305 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530306}
307
308// DelService to delete service info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530309func (db *Database) DelService(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530310 key := GetKeyPath(ServicePath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530311 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530312 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530313 return err
314 }
315 return nil
316}
317
318// Virtual networks specific database actions
319
320// GetVnets to get multiple vnets info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530321func (db *Database) GetVnets(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530322 key := GetKeyPath(VnetPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530323 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530324}
325
326// GetVnet to get vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530327func (db *Database) GetVnet(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530328 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530329 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530330}
331
332// PutVnet to add vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530333func (db *Database) PutVnet(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530334 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530335 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530336}
337
338// DelVnet to delete vnet info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530339func (db *Database) DelVnet(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530340 key := GetKeyPath(VnetPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530341 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530342 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530343 return err
344 }
345 return nil
346}
347
348// Virtual networks on ports specific database actions
349
350// GetVpvs to get multiple vpvs info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530351func (db *Database) GetVpvs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530352 key := GetKeyPath(VpvPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530353 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530354}
355
356// GetVpv to get vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530357func (db *Database) GetVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530358 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
359 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530360 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530361}
362
363// PutVpv to add vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530364func (db *Database) PutVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530365 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
366 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530367 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530368}
369
370// DelVpv to delete vpv info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530371func (db *Database) DelVpv(ctx context.Context, port string, SVlan uint16, CVlan uint16, UniVlan uint16) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530372 name := port + fmt.Sprintf("-%v-%v-%v", SVlan, CVlan, UniVlan)
373 key := GetKeyPath(VpvPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530374 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530375 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530376 return err
377 }
378 return nil
379}
380
381// Virtual networks on ports specific database actions
382
383// GetMvlans to get multiple mvlans info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530384func (db *Database) GetMvlans(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530385 key := GetKeyPath(MvlanPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530386 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530387}
388
389// GetMvlan to get mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530390func (db *Database) GetMvlan(ctx context.Context, mvlan uint16) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530391 name := strconv.FormatInt(int64(mvlan), 10)
392 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530393 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530394}
395
396// PutMvlan to add mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530397func (db *Database) PutMvlan(ctx context.Context, mvlan uint16, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530398 name := strconv.FormatInt(int64(mvlan), 10)
399 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530400 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530401}
402
403// DelMvlan to delete mvlan info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530404func (db *Database) DelMvlan(ctx context.Context, mvlan uint16) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530405 name := strconv.FormatInt(int64(mvlan), 10)
406 key := GetKeyPath(MvlanPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530407 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530408 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530409 return err
410 }
411 return nil
412}
413
414// database specific actions on IGMP config
415
416// DelIGMPCfg to delete icmp config
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530417func (db *Database) DelIGMPCfg(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530418 key := GetKeyPath(IgmpConfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530419 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530420 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530421 return err
422 }
423 return nil
424}
425
426// database specific actions on IGMP Profile
427
428// GetIgmpProfiles to get multiple igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530429func (db *Database) GetIgmpProfiles(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530430 key := GetKeyPath(IgmpProfPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530431 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530432}
433
434// GetIgmpProfile to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530435func (db *Database) GetIgmpProfile(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530436 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530437 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530438}
439
440// PutIgmpProfile to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530441func (db *Database) PutIgmpProfile(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530442 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530443 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530444}
445
446// DelIgmpProfile to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530447func (db *Database) DelIgmpProfile(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530448 key := GetKeyPath(IgmpProfPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530449 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530450 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530451 return err
452 }
453 return nil
454}
455
456// database specific actions on Mcast config Info
457
458// GetMcastConfigs to get multiple mcast config info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530459func (db *Database) GetMcastConfigs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530460 key := GetKeyPath(McastConfigPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530461 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530462}
463
464// GetMcastConfig to get igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530465func (db *Database) GetMcastConfig(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530466 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530467 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530468}
469
470// PutMcastConfig to put igmp profile info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530471func (db *Database) PutMcastConfig(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530472 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530473 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530474}
475
476// DelMcastConfig to delete igmp profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530477func (db *Database) DelMcastConfig(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530478 key := GetKeyPath(McastConfigPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530479 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530480 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530481 return err
482 }
483 return nil
484}
485
486// database specific actions on health
487
488// GetHealth to get health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530489func (db *Database) GetHealth(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530490 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530491 return db.Get(ctx,key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530492}
493
494// PutHealth to add health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530495func (db *Database) PutHealth(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530496 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530497 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530498}
499
500// DelHealth to delete health info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530501func (db *Database) DelHealth(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530502 key := GetKeyPath(HealthPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530503 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530504 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530505 return err
506 }
507 return nil
508}
509
510// Meters
511
512// GetMeters to get multiple meters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530513func (db *Database) GetMeters(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530514 key := GetKeyPath(MeterPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530515 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530516}
517
518// GetMeter to get meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530519func (db *Database) GetMeter(ctx context.Context, name string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530520 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530521 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530522}
523
524// PutMeter to add meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530525func (db *Database) PutMeter(ctx context.Context, name string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530526 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530527 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530528}
529
530// DelMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530531func (db *Database) DelMeter(ctx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530532 key := GetKeyPath(MeterPath) + name
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530533 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530534 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530535 return err
536 }
537 return nil
538}
539
540// DelAllMeter to delete meter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530541func (db *Database) DelAllMeter(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530542 key := GetKeyPath(DevicePath) + device + "/" + MeterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530543 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530544 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530545 return err
546 }
547 logger.Infow(ctx, "Deleting all the meters for device", log.Fields{"device": device})
548 return nil
549}
550
551// IGMP groups
552
553// GetIgmpGroups to get multiple igmp groups info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530554func (db *Database) GetIgmpGroups(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530555 key := GetKeyPath(IgmpGroupPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530556 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530557}
558
559// GetIgmpGroup to get igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530560func (db *Database) GetIgmpGroup(ctx context.Context, id string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530561 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530562 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530563}
564
565// PutIgmpGroup to add igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530566func (db *Database) PutIgmpGroup(ctx context.Context, id string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530567 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530568 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530569}
570
571// DelIgmpGroup to delete igmp group info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530572func (db *Database) DelIgmpGroup(ctx context.Context, id string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530573 key := GetKeyPath(IgmpGroupPath) + id
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530574 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530575 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530576 return err
577 }
578 return nil
579}
580
581// IGMP group devices
582
583// GetAllIgmpDevices to get multiple igmp devices info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530584func (db *Database) GetAllIgmpDevices(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530585 key := GetKeyPath(IgmpDevicePath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530586 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530587}
588
589// GetPrevIgmpDevices to get previous igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530590func (db *Database) GetPrevIgmpDevices(ctx context.Context, mvlan of.VlanType, gid string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530591 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530592 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530593}
594
595// GetIgmpDevices to get igmp devices
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530596func (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 +0530597 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530598 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530599}
600
601// GetIgmpDevice to get igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530602func (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 +0530603 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530604 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530605}
606
607// PutIgmpDevice to add igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530608func (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 +0530609 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530610 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530611}
612
613// DelIgmpDevice to delete igmp device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530614func (db *Database) DelIgmpDevice(ctx context.Context, mvlan of.VlanType, gid string, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530615 key := GetKeyPath(IgmpDevicePath) + mvlan.String() + "/" + gid + "/" + gip.String() + "/" + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530616 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530617 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530618 return err
619 }
620 return nil
621}
622
623// IGMP group channels
624
625// GetAllIgmpChannels to get all igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530626func (db *Database) GetAllIgmpChannels(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530627 key := GetKeyPath(IgmpChannelPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530628 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530629}
630
631// GetPrevIgmpChannels to get previous igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530632func (db *Database) GetPrevIgmpChannels(ctx context.Context, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530633 key := GetKeyPath(IgmpChannelPath) + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530634 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530635}
636
637// GetIgmpChannels to get multiple igmp channels
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530638func (db *Database) GetIgmpChannels(ctx context.Context, mvlan of.VlanType, gName, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530639 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530640 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530641}
642
643// GetIgmpChannel to get igmp channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530644func (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 +0530645 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530646 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530647}
648
649// PutIgmpChannel to add igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530650func (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 +0530651 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530652 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530653}
654
655// DelIgmpChannel to delete igmp channel info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530656func (db *Database) DelIgmpChannel(ctx context.Context, mvlan of.VlanType, gName string, device string, gip net.IP) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530657 key := GetKeyPath(IgmpChannelPath) + mvlan.String() + "/" + gName + "/" + device + "/" + gip.String()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530658 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530659 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530660 return err
661 }
662 return nil
663}
664
665// IGMP group receivers
666
667// GetAllIgmpRcvrs to get all igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530668func (db *Database) GetAllIgmpRcvrs(ctx context.Context) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530669 key := GetKeyPath(IgmpPortPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530670 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530671}
672
673// GetPrevIgmpRcvrs to get previous igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530674func (db *Database) GetPrevIgmpRcvrs(ctx context.Context, gip net.IP, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530675 key := GetKeyPath(IgmpPortPath) + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530676 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530677}
678
679// GetIgmpRcvrs to get multiple igmp receivers info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530680func (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 +0530681 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530682 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530683}
684
685// GetIgmpRcvr to get igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530686func (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 +0530687 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530688 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530689}
690
691// PutIgmpRcvr to add igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530692func (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 +0530693 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530694 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530695}
696
697// DelIgmpRcvr to delete igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530698func (db *Database) DelIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string, rcvr string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530699 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/" + rcvr
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530700 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530701 logger.Warnw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530702 return err
703 }
704 return nil
705}
706
707// DelAllIgmpRcvr to delete all igmp receiver info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530708func (db *Database) DelAllIgmpRcvr(ctx context.Context, mvlan of.VlanType, gip net.IP, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530709 key := GetKeyPath(IgmpPortPath) + mvlan.String() + "/" + gip.String() + "/" + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530710 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530711 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530712 return err
713 }
714 return nil
715}
716
717// DelAllRoutesForDevice to delete all routes for device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530718func (db *Database) DelAllRoutesForDevice(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530719 /* service/vgc/v1/devices/<deviceID>/flows/ */
720 logger.Infow(ctx, "Deleting all the flows for device", log.Fields{"device": device})
721 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), device)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530722 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530723 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530724 return err
725 }
726 return nil
727}
728
729// PutNbDevicePort to add device port info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530730func (db *Database) PutNbDevicePort(ctx context.Context, device string, ponPortID uint32, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530731 key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID)
732
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530733 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530734 logger.Warnw(ctx, "Put Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530735 }
736}
737
738// DelNbDevicePort to delete device port
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530739func (db *Database) DelNbDevicePort(ctx context.Context, device string, ponPortID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530740 key := GetKeyPath(NbDevicePath) + device + "/pon-port/" + fmt.Sprintf("%v", ponPortID)
741
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530742 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530743 logger.Warnw(ctx, "Delete Device Port failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530744 }
745}
746
747// GetAllNbPorts to get all ports info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530748func (db *Database) GetAllNbPorts(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530749 key := GetKeyPath(NbDevicePath) + deviceID + "/pon-port/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530750 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530751}
752
753//Functions for migration database
754
755// GetMigrationInfo to get migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530756func (db *Database) GetMigrationInfo(ctx context.Context) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530757 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530758 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530759}
760
761// PutMigrationInfo to add migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530762func (db *Database) PutMigrationInfo(ctx context.Context, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530763 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530764 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530765}
766
767// DelMigrationInfo to delete migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530768func (db *Database) DelMigrationInfo(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530769 key := GetKeyPath(MigrationInfoPath)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530770 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530771 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530772 return err
773 }
774 return nil
775}
776
777//PON counters
778
779// GetAllPonCounters to get all pon counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530780func (db *Database) GetAllPonCounters(ctx context.Context, device string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530781 key := GetKeyPath(PonCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530782 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530783}
784
785// GetPonCounter to get pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530786func (db *Database) GetPonCounter(ctx context.Context, device, ponID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530787 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530788 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530789}
790
791// PutPonCounter to add pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530792func (db *Database) PutPonCounter(ctx context.Context, device, ponID, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530793 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530794 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530795}
796
797// DelPonCounter to delete pon counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530798func (db *Database) DelPonCounter(ctx context.Context, device, ponID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530799 key := GetKeyPath(PonCounterPath) + device + "/" + ponID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530800 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530801 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530802 return err
803 }
804 return nil
805}
806
807//PON Channel counters
808
809// GetAllPonChannelCounters to get all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530810func (db *Database) GetAllPonChannelCounters(ctx context.Context, device, ponID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530811 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530812 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530813}
814
815// GetPonChannelCounter to get pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530816func (db *Database) GetPonChannelCounter(ctx context.Context, device, ponID, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530817 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530818 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530819}
820
821// PutPonChannelCounter to add pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530822func (db *Database) PutPonChannelCounter(ctx context.Context, device, ponID, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530823 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530824 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530825}
826
827// DelPonChannelCounter to delete pon channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530828func (db *Database) DelPonChannelCounter(ctx context.Context, device, ponID, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530829 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530830 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530831 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530832 return err
833 }
834 return nil
835}
836
837// DelAllPONCounters to delete all pon channel counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530838func (db *Database) DelAllPONCounters(ctx context.Context, device string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530839 key := GetKeyPath(PonCounterPath) + device + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530840 return db.DeleteAll(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530841}
842
843// DelPONCounters to delete pon counters
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530844func (db *Database) DelPONCounters(ctx context.Context, device string, ponID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530845 key := GetKeyPath(PonCounterPath) + device + "/" + ponID + "/"
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530846 if err := db.DeleteAll(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530847 logger.Warnw(ctx, "Delete Pon counters failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530848 }
849 //DeletePonCounter(device, ponID)
850}
851
852// PutOltIgmpCounters to add Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530853func (db *Database) PutOltIgmpCounters(ctx context.Context, device, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530854 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530855 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530856}
857
858// GetOltIgmpCounter to get Olt Igmp counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530859func (db *Database) GetOltIgmpCounter(ctx context.Context, device string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530860 key := GetKeyPath(OltIgmpCounterPath) + device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530861 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530862}
863
864//Service Channel counters
865
866// GetAllServiceChannelCounters to get all service channel counters info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530867func (db *Database) GetAllServiceChannelCounters(ctx context.Context, serviceName string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530868 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530869 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530870}
871
872// GetServiceChannelCounter to get service channel counter info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530873func (db *Database) GetServiceChannelCounter(ctx context.Context, serviceName, channel string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530874 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530875 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530876}
877
878// PutServiceChannelCounter to add service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530879func (db *Database) PutServiceChannelCounter(ctx context.Context, serviceName, channel, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530880 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530881 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530882}
883
884// DelServiceChannelCounter to delete service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530885func (db *Database) DelServiceChannelCounter(ctx context.Context, serviceName, channel string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530886 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath + channel
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530887 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530888 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530889 return err
890 }
891 return nil
892}
893
894// DelAllServiceChannelCounter to delete all service channel counter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530895func (db *Database) DelAllServiceChannelCounter(ctx context.Context, serviceName string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530896 key := GetKeyPath(ServiceCounterPath) + serviceName + "/" + ChannelCounterPath
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530897 return db.DeleteAllUnderHashKey(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530898}
899
900// OltExists to know if the ONU is added to the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530901func (db *Database) OltExists(ctx context.Context, deviceID string) bool {
902 if _, err := db.GetOlt(ctx, deviceID); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530903 return false
904 }
905 return true
906
907}
908
909// PutFlowHash to add flowhash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530910func (db *Database) PutFlowHash(ctx context.Context, deviceID string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530911 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530912 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +0530913}
914
915// GetFlowHash gets the flow hash for the device
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530916func (db *Database) GetFlowHash(ctx context.Context, deviceID string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530917 key := fmt.Sprintf(GetKeyPath(DeviceFlowPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530918 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530919}
920
921// PutPortAlarmProfile to add port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530922func (db *Database) PutPortAlarmProfile(ctx context.Context, portAlarmProfileID string, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530923 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530924 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530925 logger.Warnw(ctx, "Put PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530926 }
927}
928
929// DelPortAlarmProfile to delete port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530930func (db *Database) DelPortAlarmProfile(ctx context.Context, portAlarmProfileID string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530931 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530932 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530933 logger.Warnw(ctx, "Delete PortAlarmProfile failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530934 }
935}
936
937// GetPortAlarmProfile to get port alarm profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530938func (db *Database) GetPortAlarmProfile(ctx context.Context, portAlarmProfileID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530939 key := GetKeyPath(PortAlarmProfilePath) + fmt.Sprintf("%v", portAlarmProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530940 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530941}
942
943// PutPortAlarmData to add port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530944func (db *Database) PutPortAlarmData(ctx context.Context, deviceID string, portID uint32, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530945 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530946 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530947 logger.Warnw(ctx, "Put PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530948 }
949}
950
951// DelPortAlarmData to delete port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530952func (db *Database) DelPortAlarmData(ctx context.Context, deviceID string, portID uint32) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530953 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530954 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530955 logger.Warnw(ctx, "Delete PortAlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530956 }
957}
958
959// GetPortAlarmData to get port alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530960func (db *Database) GetPortAlarmData(ctx context.Context, deviceID string, portID uint32) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530961 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID) + fmt.Sprintf("%v", portID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530962 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530963}
964
965// GetAllPortAlarmData to get port alarm data for all ports
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530966func (db *Database) GetAllPortAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530967 key := fmt.Sprintf(GetKeyPath(PortAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530968 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530969}
970
971// PutSubAlarmData to add subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530972func (db *Database) PutSubAlarmData(ctx context.Context, deviceID string, portName string, value string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530973 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530974 if err := db.kvc.Put(ctx, key, value); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530975 logger.Warnw(ctx, "Put Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530976 }
977}
978
979// DelSubAlarmData to delete subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530980func (db *Database) DelSubAlarmData(ctx context.Context, deviceID string, portName string) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530981 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530982 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530983 logger.Warnw(ctx, "Delete Subscriber AlarmData failed", log.Fields{"key": key})
Naveen Sampath04696f72022-06-13 15:19:14 +0530984 }
985}
986
987// GetSubAlarmData to get subscriber alarm data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530988func (db *Database) GetSubAlarmData(ctx context.Context, deviceID string, portName string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530989 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID) + fmt.Sprintf("%v", portName)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530990 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530991}
992
993// GetAllSubAlarmData to get sub alarm data for all subscribers
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530994func (db *Database) GetAllSubAlarmData(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530995 key := fmt.Sprintf(GetKeyPath(SubAlarmDataPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530996 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +0530997}
998
999// Migrate Service req specific database actions
1000
1001// PutMigrateServicesReq to add MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301002func (db *Database) PutMigrateServicesReq(ctx context.Context, deviceID string, vnet string, value string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301003 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301004 return db.kvc.Put(ctx, key, value)
Naveen Sampath04696f72022-06-13 15:19:14 +05301005}
1006
1007// GetMigrateServicesReq to get MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301008func (db *Database) GetMigrateServicesReq(ctx context.Context, deviceID string, vnet string) (string, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301009 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301010 return db.Get(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301011}
1012
1013// GetAllMigrateServicesReq to get multiple MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301014func (db *Database) GetAllMigrateServicesReq(ctx context.Context, deviceID string) (map[string]*kvstore.KVPair, error) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301015 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301016 return db.List(ctx, key)
Naveen Sampath04696f72022-06-13 15:19:14 +05301017}
1018
1019// DelMigrateServicesReq to delete MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301020func (db *Database) DelMigrateServicesReq(ctx context.Context, deviceID string, vnet string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301021 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID) + vnet
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301022 if err := db.kvc.Delete(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301023 logger.Errorw(ctx, "The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301024 return err
1025 }
1026 return nil
1027}
1028
1029// DelAllMigrateServicesReq to delete all MigrateServicesReq info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301030func (db *Database) DelAllMigrateServicesReq(ctx context.Context, deviceID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301031 key := fmt.Sprintf(GetKeyPath(ServicesMigrateReqPath), deviceID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301032 if err := db.DeleteAllUnderHashKey(ctx, key); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301033 logger.Warnw(ctx, "Delete All failed: The key doesn't exist", log.Fields{"key": key, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +05301034 return err
1035 }
1036 logger.Infow(ctx, "Deleting all the Update Vnet Requests for device", log.Fields{"device": deviceID})
1037 return nil
1038}
1039
1040func init() {
Tinoj Joseph1d108322022-07-13 10:07:39 +05301041 // Setup this package so that it's log level can be modified at run time
1042 var err error
1043 logger, err = log.AddPackageWithDefaultParam()
1044 if err != nil {
1045 panic(err)
1046 }
Naveen Sampath04696f72022-06-13 15:19:14 +05301047}