blob: 724ad32a4da3ded2662962a95f20df734c23c39b [file] [log] [blame]
divyadesaia37f78b2020-02-07 12:41:22 +00001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package config
17
18import (
19 "context"
20 "fmt"
21 "github.com/opencord/voltha-lib-go/v3/pkg/db"
22 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
23 "github.com/opencord/voltha-lib-go/v3/pkg/log"
24 "strings"
25)
26
27const (
28 defaultkvStoreConfigPath = "config"
divyadesaid26f6b12020-03-19 06:30:28 +000029 kvStoreDataPathPrefix = "service/voltha"
divyadesaia37f78b2020-02-07 12:41:22 +000030 kvStorePathSeparator = "/"
31)
32
33// ConfigType represents the type for which config is created inside the kvstore
34// For example, loglevel
35type ConfigType int
36
37const (
38 ConfigTypeLogLevel ConfigType = iota
39 ConfigTypeKafka
40)
41
42func (c ConfigType) String() string {
43 return [...]string{"loglevel", "kafka"}[c]
44}
45
46// ChangeEvent represents the event recieved from watch
47// For example, Put Event
48type ChangeEvent int
49
50const (
51 Put ChangeEvent = iota
52 Delete
53)
54
divyadesaid26f6b12020-03-19 06:30:28 +000055func (ce ChangeEvent) String() string {
56 return [...]string{"Put", "Delete"}[ce]
57}
58
divyadesaia37f78b2020-02-07 12:41:22 +000059// ConfigChangeEvent represents config for the events recieved from watch
60// For example,ChangeType is Put ,ConfigAttribute default
61type ConfigChangeEvent struct {
62 ChangeType ChangeEvent
63 ConfigAttribute string
64}
65
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070066// ConfigManager is a wrapper over Backend to maintain Configuration of voltha components
divyadesaia37f78b2020-02-07 12:41:22 +000067// in kvstore based persistent storage
68type ConfigManager struct {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070069 Backend *db.Backend
divyadesaia37f78b2020-02-07 12:41:22 +000070 KvStoreConfigPrefix string
71}
72
73// ComponentConfig represents a category of configuration for a specific VOLTHA component type
74// stored in a persistent storage pointed to by Config Manager
75// For example, one ComponentConfig instance will be created for loglevel config type for rw-core
76// component while another ComponentConfig instance will refer to connection config type for same
77// rw-core component. So, there can be multiple ComponentConfig instance created per component
78// pointing to different category of configuration.
79//
80// Configuration pointed to be by ComponentConfig is stored in kvstore as a list of key/value pairs
81// under the hierarchical tree with following base path
82// <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/
83//
84// For example, rw-core ComponentConfig for loglevel config entries will be stored under following path
85// /voltha/service/config/rw-core/loglevel/
86type ComponentConfig struct {
87 cManager *ConfigManager
88 componentLabel string
89 configType ConfigType
90 changeEventChan chan *ConfigChangeEvent
91 kvStoreEventChan chan *kvstore.Event
92}
93
94func NewConfigManager(kvClient kvstore.Client, kvStoreType, kvStoreHost string, kvStorePort, kvStoreTimeout int) *ConfigManager {
95
96 return &ConfigManager{
97 KvStoreConfigPrefix: defaultkvStoreConfigPath,
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070098 Backend: &db.Backend{
divyadesaia37f78b2020-02-07 12:41:22 +000099 Client: kvClient,
100 StoreType: kvStoreType,
101 Host: kvStoreHost,
102 Port: kvStorePort,
103 Timeout: kvStoreTimeout,
104 PathPrefix: kvStoreDataPathPrefix,
105 },
106 }
107}
108
divyadesaid26f6b12020-03-19 06:30:28 +0000109// RetrieveComponentList list the component Names for which loglevel is stored in kvstore
110func (c *ConfigManager) RetrieveComponentList(ctx context.Context, configType ConfigType) ([]string, error) {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700111 data, err := c.Backend.List(ctx, c.KvStoreConfigPrefix)
divyadesaid26f6b12020-03-19 06:30:28 +0000112 if err != nil {
113 return nil, err
114 }
115
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700116 // Looping through the data recieved from the Backend for config
divyadesaid26f6b12020-03-19 06:30:28 +0000117 // Trimming and Splitting the required key and value from data and storing as componentName,PackageName and Level
118 // For Example, recieved key would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default and value \"DEBUG\"
119 // Then in default will be stored as PackageName,componentName as <Component Name> and DEBUG will be stored as value in List struct
120 ccPathPrefix := kvStorePathSeparator + configType.String() + kvStorePathSeparator
121 pathPrefix := kvStoreDataPathPrefix + kvStorePathSeparator + c.KvStoreConfigPrefix + kvStorePathSeparator
122 var list []string
123 keys := make(map[string]interface{})
124 for attr := range data {
125 cname := strings.TrimPrefix(attr, pathPrefix)
126 cName := strings.SplitN(cname, ccPathPrefix, 2)
127 if len(cName) != 2 {
128 continue
129 }
130 if _, exist := keys[cName[0]]; !exist {
131 keys[cName[0]] = nil
132 list = append(list, cName[0])
133 }
134 }
135 return list, nil
136}
137
divyadesaia37f78b2020-02-07 12:41:22 +0000138// Initialize the component config
139func (cm *ConfigManager) InitComponentConfig(componentLabel string, configType ConfigType) *ComponentConfig {
140
141 return &ComponentConfig{
142 componentLabel: componentLabel,
143 configType: configType,
144 cManager: cm,
145 changeEventChan: nil,
146 kvStoreEventChan: nil,
147 }
148
149}
150
151func (c *ComponentConfig) makeConfigPath() string {
152
153 cType := c.configType.String()
154 return c.cManager.KvStoreConfigPrefix + kvStorePathSeparator +
155 c.componentLabel + kvStorePathSeparator + cType
156}
157
158// MonitorForConfigChange watch on the subkeys for the given key
159// Any changes to the subkeys for the given key will return an event channel
160// Then Event channel will be processed and new event channel with required values will be created and return
161// For example, rw-core will be watching on <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/
162// will return an event channel for PUT,DELETE eventType.
163// Then values from event channel will be processed and stored in kvStoreEventChan.
164func (c *ComponentConfig) MonitorForConfigChange(ctx context.Context) chan *ConfigChangeEvent {
165 key := c.makeConfigPath()
166
Scott Baker24f83e22020-03-30 16:14:28 -0700167 logger.Debugw("monitoring-for-config-change", log.Fields{"key": key})
divyadesaia37f78b2020-02-07 12:41:22 +0000168
169 c.changeEventChan = make(chan *ConfigChangeEvent, 1)
170
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700171 c.kvStoreEventChan = c.cManager.Backend.CreateWatch(ctx, key, true)
divyadesaia37f78b2020-02-07 12:41:22 +0000172
173 go c.processKVStoreWatchEvents()
174
175 return c.changeEventChan
176}
177
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700178// processKVStoreWatchEvents process event channel recieved from the Backend for any ChangeType
divyadesaia37f78b2020-02-07 12:41:22 +0000179// It checks for the EventType is valid or not.For the valid EventTypes creates ConfigChangeEvent and send it on channel
180func (c *ComponentConfig) processKVStoreWatchEvents() {
181
182 ccKeyPrefix := c.makeConfigPath()
divyadesaid26f6b12020-03-19 06:30:28 +0000183
Scott Baker24f83e22020-03-30 16:14:28 -0700184 logger.Debugw("processing-kvstore-event-change", log.Fields{"key-prefix": ccKeyPrefix})
divyadesaid26f6b12020-03-19 06:30:28 +0000185
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700186 ccPathPrefix := c.cManager.Backend.PathPrefix + ccKeyPrefix + kvStorePathSeparator
divyadesaid26f6b12020-03-19 06:30:28 +0000187
divyadesaia37f78b2020-02-07 12:41:22 +0000188 for watchResp := range c.kvStoreEventChan {
189
190 if watchResp.EventType == kvstore.CONNECTIONDOWN || watchResp.EventType == kvstore.UNKNOWN {
Scott Baker24f83e22020-03-30 16:14:28 -0700191 logger.Warnw("received-invalid-change-type-in-watch-channel-from-kvstore", log.Fields{"change-type": watchResp.EventType})
divyadesaia37f78b2020-02-07 12:41:22 +0000192 continue
193 }
194
195 // populating the configAttribute from the received Key
196 // For Example, Key received would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default
197 // Storing default in configAttribute variable
198 ky := fmt.Sprintf("%s", watchResp.Key)
199
200 c.changeEventChan <- &ConfigChangeEvent{
201 ChangeType: ChangeEvent(watchResp.EventType),
202 ConfigAttribute: strings.TrimPrefix(ky, ccPathPrefix),
203 }
204 }
205}
206
divyadesaid26f6b12020-03-19 06:30:28 +0000207// Retrieves value of a specific config key. Value of key is returned in String format
208func (c *ComponentConfig) Retrieve(ctx context.Context, configKey string) (string, error) {
209 key := c.makeConfigPath() + "/" + configKey
210
Scott Baker24f83e22020-03-30 16:14:28 -0700211 logger.Debugw("retrieving-config", log.Fields{"key": key})
divyadesaid26f6b12020-03-19 06:30:28 +0000212
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700213 if kvpair, err := c.cManager.Backend.Get(ctx, key); err != nil {
divyadesaid26f6b12020-03-19 06:30:28 +0000214 return "", err
215 } else {
216 if kvpair == nil {
217 return "", fmt.Errorf("config-key-does-not-exist : %s", key)
218 }
219
220 value := strings.Trim(fmt.Sprintf("%s", kvpair.Value), "\"")
Scott Baker24f83e22020-03-30 16:14:28 -0700221 logger.Debugw("retrieved-config", log.Fields{"key": key, "value": value})
divyadesaid26f6b12020-03-19 06:30:28 +0000222 return value, nil
223 }
224}
225
divyadesaia37f78b2020-02-07 12:41:22 +0000226func (c *ComponentConfig) RetrieveAll(ctx context.Context) (map[string]string, error) {
227 key := c.makeConfigPath()
228
Scott Baker24f83e22020-03-30 16:14:28 -0700229 logger.Debugw("retreiving-list", log.Fields{"key": key})
divyadesaid26f6b12020-03-19 06:30:28 +0000230
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700231 data, err := c.cManager.Backend.List(ctx, key)
divyadesaia37f78b2020-02-07 12:41:22 +0000232 if err != nil {
233 return nil, err
234 }
235
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700236 // Looping through the data recieved from the Backend for the given key
divyadesaia37f78b2020-02-07 12:41:22 +0000237 // Trimming the required key and value from data and storing as key/value pair
238 // For Example, recieved key would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default and value \"DEBUG\"
239 // Then in default will be stored as key and DEBUG will be stored as value in map[string]string
240 res := make(map[string]string)
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700241 ccPathPrefix := c.cManager.Backend.PathPrefix + kvStorePathSeparator + key + kvStorePathSeparator
divyadesaia37f78b2020-02-07 12:41:22 +0000242 for attr, val := range data {
243 res[strings.TrimPrefix(attr, ccPathPrefix)] = strings.Trim(fmt.Sprintf("%s", val.Value), "\"")
244 }
245
246 return res, nil
247}
248
divyadesaid26f6b12020-03-19 06:30:28 +0000249func (c *ComponentConfig) Save(ctx context.Context, configKey string, configValue string) error {
divyadesaia37f78b2020-02-07 12:41:22 +0000250 key := c.makeConfigPath() + "/" + configKey
251
Scott Baker24f83e22020-03-30 16:14:28 -0700252 logger.Debugw("saving-config", log.Fields{"key": key, "value": configValue})
divyadesaia37f78b2020-02-07 12:41:22 +0000253
254 //save the data for update config
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700255 if err := c.cManager.Backend.Put(ctx, key, configValue); err != nil {
divyadesaia37f78b2020-02-07 12:41:22 +0000256 return err
257 }
258 return nil
259}
260
divyadesaid26f6b12020-03-19 06:30:28 +0000261func (c *ComponentConfig) Delete(ctx context.Context, configKey string) error {
divyadesaia37f78b2020-02-07 12:41:22 +0000262 //construct key using makeConfigPath
263 key := c.makeConfigPath() + "/" + configKey
264
Scott Baker24f83e22020-03-30 16:14:28 -0700265 logger.Debugw("deleting-config", log.Fields{"key": key})
divyadesaia37f78b2020-02-07 12:41:22 +0000266 //delete the config
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700267 if err := c.cManager.Backend.Delete(ctx, key); err != nil {
divyadesaia37f78b2020-02-07 12:41:22 +0000268 return err
269 }
270 return nil
271}