blob: 0dafc7a46784125f1ee5731767ab25a446e79cc6 [file] [log] [blame]
divyadesai81bb7ba2020-03-11 11:45:23 +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
divyadesai81bb7ba2020-03-11 11:45:23 +000027const (
28 defaultkvStoreConfigPath = "config"
divyadesai52dc0882020-03-19 06:38:11 +000029 kvStoreDataPathPrefix = "service/voltha"
divyadesai81bb7ba2020-03-11 11:45:23 +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
Rohan Agrawal00d3a412020-04-22 10:51:39 +000039 ConfigTypeMetadata
divyadesai81bb7ba2020-03-11 11:45:23 +000040 ConfigTypeKafka
41)
42
43func (c ConfigType) String() string {
Rohan Agrawal00d3a412020-04-22 10:51:39 +000044 return [...]string{"loglevel", "metadata", "kafka"}[c]
divyadesai81bb7ba2020-03-11 11:45:23 +000045}
46
47// ChangeEvent represents the event recieved from watch
48// For example, Put Event
49type ChangeEvent int
50
51const (
52 Put ChangeEvent = iota
53 Delete
54)
55
divyadesai52dc0882020-03-19 06:38:11 +000056func (ce ChangeEvent) String() string {
57 return [...]string{"Put", "Delete"}[ce]
58}
59
divyadesai81bb7ba2020-03-11 11:45:23 +000060// ConfigChangeEvent represents config for the events recieved from watch
61// For example,ChangeType is Put ,ConfigAttribute default
62type ConfigChangeEvent struct {
63 ChangeType ChangeEvent
64 ConfigAttribute string
65}
66
Rohan Agrawal00d3a412020-04-22 10:51:39 +000067// ConfigManager is a wrapper over Backend to maintain Configuration of voltha components
divyadesai81bb7ba2020-03-11 11:45:23 +000068// in kvstore based persistent storage
69type ConfigManager struct {
Rohan Agrawal00d3a412020-04-22 10:51:39 +000070 Backend *db.Backend
divyadesai81bb7ba2020-03-11 11:45:23 +000071 KvStoreConfigPrefix string
72}
73
74// ComponentConfig represents a category of configuration for a specific VOLTHA component type
75// stored in a persistent storage pointed to by Config Manager
76// For example, one ComponentConfig instance will be created for loglevel config type for rw-core
77// component while another ComponentConfig instance will refer to connection config type for same
78// rw-core component. So, there can be multiple ComponentConfig instance created per component
79// pointing to different category of configuration.
80//
81// Configuration pointed to be by ComponentConfig is stored in kvstore as a list of key/value pairs
82// under the hierarchical tree with following base path
83// <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/
84//
85// For example, rw-core ComponentConfig for loglevel config entries will be stored under following path
86// /voltha/service/config/rw-core/loglevel/
87type ComponentConfig struct {
88 cManager *ConfigManager
89 componentLabel string
90 configType ConfigType
91 changeEventChan chan *ConfigChangeEvent
92 kvStoreEventChan chan *kvstore.Event
93}
94
95func NewConfigManager(kvClient kvstore.Client, kvStoreType, kvStoreHost string, kvStorePort, kvStoreTimeout int) *ConfigManager {
96
97 return &ConfigManager{
98 KvStoreConfigPrefix: defaultkvStoreConfigPath,
Rohan Agrawal00d3a412020-04-22 10:51:39 +000099 Backend: &db.Backend{
divyadesai81bb7ba2020-03-11 11:45:23 +0000100 Client: kvClient,
101 StoreType: kvStoreType,
102 Host: kvStoreHost,
103 Port: kvStorePort,
104 Timeout: kvStoreTimeout,
105 PathPrefix: kvStoreDataPathPrefix,
106 },
107 }
108}
109
110// RetrieveComponentList list the component Names for which loglevel is stored in kvstore
111func (c *ConfigManager) RetrieveComponentList(ctx context.Context, configType ConfigType) ([]string, error) {
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000112 data, err := c.Backend.List(ctx, c.KvStoreConfigPrefix)
divyadesai81bb7ba2020-03-11 11:45:23 +0000113 if err != nil {
114 return nil, err
115 }
116
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000117 // Looping through the data recieved from the Backend for config
divyadesai81bb7ba2020-03-11 11:45:23 +0000118 // Trimming and Splitting the required key and value from data and storing as componentName,PackageName and Level
119 // For Example, recieved key would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default and value \"DEBUG\"
120 // Then in default will be stored as PackageName,componentName as <Component Name> and DEBUG will be stored as value in List struct
121 ccPathPrefix := kvStorePathSeparator + configType.String() + kvStorePathSeparator
122 pathPrefix := kvStoreDataPathPrefix + kvStorePathSeparator + c.KvStoreConfigPrefix + kvStorePathSeparator
123 var list []string
124 keys := make(map[string]interface{})
125 for attr := range data {
126 cname := strings.TrimPrefix(attr, pathPrefix)
127 cName := strings.SplitN(cname, ccPathPrefix, 2)
128 if len(cName) != 2 {
129 continue
130 }
131 if _, exist := keys[cName[0]]; !exist {
132 keys[cName[0]] = nil
133 list = append(list, cName[0])
134 }
135 }
136 return list, nil
137}
138
139// Initialize the component config
140func (cm *ConfigManager) InitComponentConfig(componentLabel string, configType ConfigType) *ComponentConfig {
141
142 return &ComponentConfig{
143 componentLabel: componentLabel,
144 configType: configType,
145 cManager: cm,
146 changeEventChan: nil,
147 kvStoreEventChan: nil,
148 }
149
150}
151
152func (c *ComponentConfig) makeConfigPath() string {
153
154 cType := c.configType.String()
155 return c.cManager.KvStoreConfigPrefix + kvStorePathSeparator +
156 c.componentLabel + kvStorePathSeparator + cType
157}
158
159// MonitorForConfigChange watch on the subkeys for the given key
160// Any changes to the subkeys for the given key will return an event channel
161// Then Event channel will be processed and new event channel with required values will be created and return
162// For example, rw-core will be watching on <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/
163// will return an event channel for PUT,DELETE eventType.
164// Then values from event channel will be processed and stored in kvStoreEventChan.
165func (c *ComponentConfig) MonitorForConfigChange(ctx context.Context) chan *ConfigChangeEvent {
166 key := c.makeConfigPath()
167
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000168 logger.Debugw("monitoring-for-config-change", log.Fields{"key": key})
divyadesai81bb7ba2020-03-11 11:45:23 +0000169
170 c.changeEventChan = make(chan *ConfigChangeEvent, 1)
171
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000172 c.kvStoreEventChan = c.cManager.Backend.CreateWatch(ctx, key, true)
divyadesai81bb7ba2020-03-11 11:45:23 +0000173
174 go c.processKVStoreWatchEvents()
175
176 return c.changeEventChan
177}
178
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000179// processKVStoreWatchEvents process event channel recieved from the Backend for any ChangeType
divyadesai81bb7ba2020-03-11 11:45:23 +0000180// It checks for the EventType is valid or not.For the valid EventTypes creates ConfigChangeEvent and send it on channel
181func (c *ComponentConfig) processKVStoreWatchEvents() {
182
183 ccKeyPrefix := c.makeConfigPath()
184
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000185 logger.Debugw("processing-kvstore-event-change", log.Fields{"key-prefix": ccKeyPrefix})
divyadesai81bb7ba2020-03-11 11:45:23 +0000186
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000187 ccPathPrefix := c.cManager.Backend.PathPrefix + ccKeyPrefix + kvStorePathSeparator
divyadesai81bb7ba2020-03-11 11:45:23 +0000188
189 for watchResp := range c.kvStoreEventChan {
190
191 if watchResp.EventType == kvstore.CONNECTIONDOWN || watchResp.EventType == kvstore.UNKNOWN {
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000192 logger.Warnw("received-invalid-change-type-in-watch-channel-from-kvstore", log.Fields{"change-type": watchResp.EventType})
divyadesai81bb7ba2020-03-11 11:45:23 +0000193 continue
194 }
195
196 // populating the configAttribute from the received Key
197 // For Example, Key received would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default
198 // Storing default in configAttribute variable
199 ky := fmt.Sprintf("%s", watchResp.Key)
200
201 c.changeEventChan <- &ConfigChangeEvent{
202 ChangeType: ChangeEvent(watchResp.EventType),
203 ConfigAttribute: strings.TrimPrefix(ky, ccPathPrefix),
204 }
205 }
206}
207
divyadesai52dc0882020-03-19 06:38:11 +0000208// Retrieves value of a specific config key. Value of key is returned in String format
209func (c *ComponentConfig) Retrieve(ctx context.Context, configKey string) (string, error) {
210 key := c.makeConfigPath() + "/" + configKey
211
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000212 logger.Debugw("retrieving-config", log.Fields{"key": key})
divyadesai52dc0882020-03-19 06:38:11 +0000213
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000214 if kvpair, err := c.cManager.Backend.Get(ctx, key); err != nil {
divyadesai52dc0882020-03-19 06:38:11 +0000215 return "", err
216 } else {
217 if kvpair == nil {
218 return "", fmt.Errorf("config-key-does-not-exist : %s", key)
219 }
220
221 value := strings.Trim(fmt.Sprintf("%s", kvpair.Value), "\"")
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000222 logger.Debugw("retrieved-config", log.Fields{"key": key, "value": value})
divyadesai52dc0882020-03-19 06:38:11 +0000223 return value, nil
224 }
225}
226
divyadesai81bb7ba2020-03-11 11:45:23 +0000227func (c *ComponentConfig) RetrieveAll(ctx context.Context) (map[string]string, error) {
228 key := c.makeConfigPath()
229
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000230 logger.Debugw("retreiving-list", log.Fields{"key": key})
divyadesai81bb7ba2020-03-11 11:45:23 +0000231
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000232 data, err := c.cManager.Backend.List(ctx, key)
divyadesai81bb7ba2020-03-11 11:45:23 +0000233 if err != nil {
234 return nil, err
235 }
236
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000237 // Looping through the data recieved from the Backend for the given key
divyadesai81bb7ba2020-03-11 11:45:23 +0000238 // Trimming the required key and value from data and storing as key/value pair
239 // For Example, recieved key would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default and value \"DEBUG\"
240 // Then in default will be stored as key and DEBUG will be stored as value in map[string]string
241 res := make(map[string]string)
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000242 ccPathPrefix := c.cManager.Backend.PathPrefix + kvStorePathSeparator + key + kvStorePathSeparator
divyadesai81bb7ba2020-03-11 11:45:23 +0000243 for attr, val := range data {
244 res[strings.TrimPrefix(attr, ccPathPrefix)] = strings.Trim(fmt.Sprintf("%s", val.Value), "\"")
245 }
246
247 return res, nil
248}
249
250func (c *ComponentConfig) Save(ctx context.Context, configKey string, configValue string) error {
251 key := c.makeConfigPath() + "/" + configKey
252
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000253 logger.Debugw("saving-config", log.Fields{"key": key, "value": configValue})
divyadesai81bb7ba2020-03-11 11:45:23 +0000254
255 //save the data for update config
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000256 if err := c.cManager.Backend.Put(ctx, key, configValue); err != nil {
divyadesai81bb7ba2020-03-11 11:45:23 +0000257 return err
258 }
259 return nil
260}
261
262func (c *ComponentConfig) Delete(ctx context.Context, configKey string) error {
263 //construct key using makeConfigPath
264 key := c.makeConfigPath() + "/" + configKey
265
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000266 logger.Debugw("deleting-config", log.Fields{"key": key})
divyadesai81bb7ba2020-03-11 11:45:23 +0000267 //delete the config
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000268 if err := c.cManager.Backend.Delete(ctx, key); err != nil {
divyadesai81bb7ba2020-03-11 11:45:23 +0000269 return err
270 }
271 return nil
272}