divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package config |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 21 | "os" |
| 22 | "strings" |
| 23 | "time" |
| 24 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | const ( |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 31 | defaultkvStoreConfigPath = "config" |
| 32 | defaultkvStoreDataPathPrefix = "service/voltha" |
| 33 | kvStorePathSeparator = "/" |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | // ConfigType represents the type for which config is created inside the kvstore |
| 37 | // For example, loglevel |
| 38 | type ConfigType int |
| 39 | |
| 40 | const ( |
| 41 | ConfigTypeLogLevel ConfigType = iota |
Girish Kumar | 472a5c9 | 2020-04-14 09:14:18 +0000 | [diff] [blame] | 42 | ConfigTypeMetadata |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 43 | ConfigTypeKafka |
| 44 | ) |
| 45 | |
| 46 | func (c ConfigType) String() string { |
Girish Kumar | 472a5c9 | 2020-04-14 09:14:18 +0000 | [diff] [blame] | 47 | return [...]string{"loglevel", "metadata", "kafka"}[c] |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // ChangeEvent represents the event recieved from watch |
| 51 | // For example, Put Event |
| 52 | type ChangeEvent int |
| 53 | |
| 54 | const ( |
| 55 | Put ChangeEvent = iota |
| 56 | Delete |
| 57 | ) |
| 58 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 59 | func (ce ChangeEvent) String() string { |
| 60 | return [...]string{"Put", "Delete"}[ce] |
| 61 | } |
| 62 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 63 | // ConfigChangeEvent represents config for the events recieved from watch |
| 64 | // For example,ChangeType is Put ,ConfigAttribute default |
| 65 | type ConfigChangeEvent struct { |
| 66 | ChangeType ChangeEvent |
| 67 | ConfigAttribute string |
| 68 | } |
| 69 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 70 | // ConfigManager is a wrapper over Backend to maintain Configuration of voltha components |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 71 | // in kvstore based persistent storage |
| 72 | type ConfigManager struct { |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 73 | Backend *db.Backend |
| 74 | KVStoreConfigPrefix string |
| 75 | KVStoreDataPathPrefix string |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // ComponentConfig represents a category of configuration for a specific VOLTHA component type |
| 79 | // stored in a persistent storage pointed to by Config Manager |
| 80 | // For example, one ComponentConfig instance will be created for loglevel config type for rw-core |
| 81 | // component while another ComponentConfig instance will refer to connection config type for same |
| 82 | // rw-core component. So, there can be multiple ComponentConfig instance created per component |
| 83 | // pointing to different category of configuration. |
| 84 | // |
| 85 | // Configuration pointed to be by ComponentConfig is stored in kvstore as a list of key/value pairs |
| 86 | // under the hierarchical tree with following base path |
| 87 | // <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/ |
| 88 | // |
| 89 | // For example, rw-core ComponentConfig for loglevel config entries will be stored under following path |
| 90 | // /voltha/service/config/rw-core/loglevel/ |
| 91 | type ComponentConfig struct { |
| 92 | cManager *ConfigManager |
| 93 | componentLabel string |
| 94 | configType ConfigType |
| 95 | changeEventChan chan *ConfigChangeEvent |
| 96 | kvStoreEventChan chan *kvstore.Event |
| 97 | } |
| 98 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 99 | func NewConfigManager(kvClient kvstore.Client, kvStoreType, kvStoreAddress string, kvStoreTimeout time.Duration) *ConfigManager { |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 100 | var kvStorePrefix string |
| 101 | if prefix, present := os.LookupEnv("KV_STORE_DATAPATH_PREFIX"); present { |
| 102 | kvStorePrefix = prefix |
| 103 | logger.Infow("KV_STORE_DATAPATH_PREFIX env variable is set, ", log.Fields{"kvStoreDataPathPrefix": kvStorePrefix}) |
| 104 | } else { |
| 105 | kvStorePrefix = defaultkvStoreDataPathPrefix |
| 106 | logger.Infow("KV_STORE_DATAPATH_PREFIX env variable is not set, using default", log.Fields{"kvStoreDataPathPrefix": defaultkvStoreDataPathPrefix}) |
| 107 | } |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 108 | return &ConfigManager{ |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 109 | KVStoreConfigPrefix: defaultkvStoreConfigPath, |
| 110 | KVStoreDataPathPrefix: kvStorePrefix, |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 111 | Backend: &db.Backend{ |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 112 | Client: kvClient, |
| 113 | StoreType: kvStoreType, |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 114 | Address: kvStoreAddress, |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 115 | Timeout: kvStoreTimeout, |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 116 | PathPrefix: kvStorePrefix, |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 117 | }, |
| 118 | } |
| 119 | } |
| 120 | |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 121 | // RetrieveComponentList list the component Names for which loglevel is stored in kvstore |
| 122 | func (c *ConfigManager) RetrieveComponentList(ctx context.Context, configType ConfigType) ([]string, error) { |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 123 | data, err := c.Backend.List(ctx, c.KVStoreConfigPrefix) |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 128 | // Looping through the data recieved from the Backend for config |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 129 | // Trimming and Splitting the required key and value from data and storing as componentName,PackageName and Level |
| 130 | // For Example, recieved key would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default and value \"DEBUG\" |
| 131 | // Then in default will be stored as PackageName,componentName as <Component Name> and DEBUG will be stored as value in List struct |
| 132 | ccPathPrefix := kvStorePathSeparator + configType.String() + kvStorePathSeparator |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 133 | pathPrefix := c.KVStoreDataPathPrefix + kvStorePathSeparator + c.KVStoreConfigPrefix + kvStorePathSeparator |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 134 | var list []string |
| 135 | keys := make(map[string]interface{}) |
| 136 | for attr := range data { |
| 137 | cname := strings.TrimPrefix(attr, pathPrefix) |
| 138 | cName := strings.SplitN(cname, ccPathPrefix, 2) |
| 139 | if len(cName) != 2 { |
| 140 | continue |
| 141 | } |
| 142 | if _, exist := keys[cName[0]]; !exist { |
| 143 | keys[cName[0]] = nil |
| 144 | list = append(list, cName[0]) |
| 145 | } |
| 146 | } |
| 147 | return list, nil |
| 148 | } |
| 149 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 150 | // Initialize the component config |
| 151 | func (cm *ConfigManager) InitComponentConfig(componentLabel string, configType ConfigType) *ComponentConfig { |
| 152 | |
| 153 | return &ComponentConfig{ |
| 154 | componentLabel: componentLabel, |
| 155 | configType: configType, |
| 156 | cManager: cm, |
| 157 | changeEventChan: nil, |
| 158 | kvStoreEventChan: nil, |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | func (c *ComponentConfig) makeConfigPath() string { |
| 164 | |
| 165 | cType := c.configType.String() |
serkant.uluderya | e1e2473 | 2020-05-20 00:35:21 -0700 | [diff] [blame] | 166 | return c.cManager.KVStoreConfigPrefix + kvStorePathSeparator + |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 167 | c.componentLabel + kvStorePathSeparator + cType |
| 168 | } |
| 169 | |
| 170 | // MonitorForConfigChange watch on the subkeys for the given key |
| 171 | // Any changes to the subkeys for the given key will return an event channel |
| 172 | // Then Event channel will be processed and new event channel with required values will be created and return |
| 173 | // For example, rw-core will be watching on <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/ |
| 174 | // will return an event channel for PUT,DELETE eventType. |
| 175 | // Then values from event channel will be processed and stored in kvStoreEventChan. |
| 176 | func (c *ComponentConfig) MonitorForConfigChange(ctx context.Context) chan *ConfigChangeEvent { |
| 177 | key := c.makeConfigPath() |
| 178 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 179 | logger.Debugw("monitoring-for-config-change", log.Fields{"key": key}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 180 | |
| 181 | c.changeEventChan = make(chan *ConfigChangeEvent, 1) |
| 182 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 183 | c.kvStoreEventChan = c.cManager.Backend.CreateWatch(ctx, key, true) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 184 | |
| 185 | go c.processKVStoreWatchEvents() |
| 186 | |
| 187 | return c.changeEventChan |
| 188 | } |
| 189 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 190 | // processKVStoreWatchEvents process event channel recieved from the Backend for any ChangeType |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 191 | // It checks for the EventType is valid or not.For the valid EventTypes creates ConfigChangeEvent and send it on channel |
| 192 | func (c *ComponentConfig) processKVStoreWatchEvents() { |
| 193 | |
| 194 | ccKeyPrefix := c.makeConfigPath() |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 195 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 196 | logger.Debugw("processing-kvstore-event-change", log.Fields{"key-prefix": ccKeyPrefix}) |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 197 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 198 | ccPathPrefix := c.cManager.Backend.PathPrefix + ccKeyPrefix + kvStorePathSeparator |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 199 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 200 | for watchResp := range c.kvStoreEventChan { |
| 201 | |
| 202 | if watchResp.EventType == kvstore.CONNECTIONDOWN || watchResp.EventType == kvstore.UNKNOWN { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 203 | logger.Warnw("received-invalid-change-type-in-watch-channel-from-kvstore", log.Fields{"change-type": watchResp.EventType}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 204 | continue |
| 205 | } |
| 206 | |
| 207 | // populating the configAttribute from the received Key |
| 208 | // For Example, Key received would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default |
| 209 | // Storing default in configAttribute variable |
| 210 | ky := fmt.Sprintf("%s", watchResp.Key) |
| 211 | |
| 212 | c.changeEventChan <- &ConfigChangeEvent{ |
| 213 | ChangeType: ChangeEvent(watchResp.EventType), |
| 214 | ConfigAttribute: strings.TrimPrefix(ky, ccPathPrefix), |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 219 | // Retrieves value of a specific config key. Value of key is returned in String format |
| 220 | func (c *ComponentConfig) Retrieve(ctx context.Context, configKey string) (string, error) { |
| 221 | key := c.makeConfigPath() + "/" + configKey |
| 222 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 223 | logger.Debugw("retrieving-config", log.Fields{"key": key}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 224 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 225 | if kvpair, err := c.cManager.Backend.Get(ctx, key); err != nil { |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 226 | return "", err |
| 227 | } else { |
| 228 | if kvpair == nil { |
| 229 | return "", fmt.Errorf("config-key-does-not-exist : %s", key) |
| 230 | } |
| 231 | |
| 232 | value := strings.Trim(fmt.Sprintf("%s", kvpair.Value), "\"") |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 233 | logger.Debugw("retrieved-config", log.Fields{"key": key, "value": value}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 234 | return value, nil |
| 235 | } |
| 236 | } |
| 237 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 238 | func (c *ComponentConfig) RetrieveAll(ctx context.Context) (map[string]string, error) { |
| 239 | key := c.makeConfigPath() |
| 240 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 241 | logger.Debugw("retreiving-list", log.Fields{"key": key}) |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 242 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 243 | data, err := c.cManager.Backend.List(ctx, key) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 244 | if err != nil { |
| 245 | return nil, err |
| 246 | } |
| 247 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 248 | // Looping through the data recieved from the Backend for the given key |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 249 | // Trimming the required key and value from data and storing as key/value pair |
| 250 | // For Example, recieved key would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default and value \"DEBUG\" |
| 251 | // Then in default will be stored as key and DEBUG will be stored as value in map[string]string |
| 252 | res := make(map[string]string) |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 253 | ccPathPrefix := c.cManager.Backend.PathPrefix + kvStorePathSeparator + key + kvStorePathSeparator |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 254 | for attr, val := range data { |
| 255 | res[strings.TrimPrefix(attr, ccPathPrefix)] = strings.Trim(fmt.Sprintf("%s", val.Value), "\"") |
| 256 | } |
| 257 | |
| 258 | return res, nil |
| 259 | } |
| 260 | |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 261 | func (c *ComponentConfig) Save(ctx context.Context, configKey string, configValue string) error { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 262 | key := c.makeConfigPath() + "/" + configKey |
| 263 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 264 | logger.Debugw("saving-config", log.Fields{"key": key, "value": configValue}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 265 | |
| 266 | //save the data for update config |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 267 | if err := c.cManager.Backend.Put(ctx, key, configValue); err != nil { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 268 | return err |
| 269 | } |
| 270 | return nil |
| 271 | } |
| 272 | |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 273 | func (c *ComponentConfig) Delete(ctx context.Context, configKey string) error { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 274 | //construct key using makeConfigPath |
| 275 | key := c.makeConfigPath() + "/" + configKey |
| 276 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 277 | logger.Debugw("deleting-config", log.Fields{"key": key}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 278 | //delete the config |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 279 | if err := c.cManager.Backend.Delete(ctx, key); err != nil { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 280 | return err |
| 281 | } |
| 282 | return nil |
| 283 | } |