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" |
| 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 | |
| 27 | const ( |
| 28 | defaultkvStoreConfigPath = "config" |
Girish Kumar | a8ef633 | 2020-03-18 18:35:43 +0000 | [diff] [blame] | 29 | kvStoreDataPathPrefix = "service/voltha" |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 30 | kvStorePathSeparator = "/" |
| 31 | ) |
| 32 | |
| 33 | // ConfigType represents the type for which config is created inside the kvstore |
| 34 | // For example, loglevel |
| 35 | type ConfigType int |
| 36 | |
| 37 | const ( |
| 38 | ConfigTypeLogLevel ConfigType = iota |
Girish Kumar | 472a5c9 | 2020-04-14 09:14:18 +0000 | [diff] [blame] | 39 | ConfigTypeMetadata |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 40 | ConfigTypeKafka |
| 41 | ) |
| 42 | |
| 43 | func (c ConfigType) String() string { |
Girish Kumar | 472a5c9 | 2020-04-14 09:14:18 +0000 | [diff] [blame] | 44 | return [...]string{"loglevel", "metadata", "kafka"}[c] |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // ChangeEvent represents the event recieved from watch |
| 48 | // For example, Put Event |
| 49 | type ChangeEvent int |
| 50 | |
| 51 | const ( |
| 52 | Put ChangeEvent = iota |
| 53 | Delete |
| 54 | ) |
| 55 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 56 | func (ce ChangeEvent) String() string { |
| 57 | return [...]string{"Put", "Delete"}[ce] |
| 58 | } |
| 59 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 60 | // ConfigChangeEvent represents config for the events recieved from watch |
| 61 | // For example,ChangeType is Put ,ConfigAttribute default |
| 62 | type ConfigChangeEvent struct { |
| 63 | ChangeType ChangeEvent |
| 64 | ConfigAttribute string |
| 65 | } |
| 66 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 67 | // ConfigManager is a wrapper over Backend to maintain Configuration of voltha components |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 68 | // in kvstore based persistent storage |
| 69 | type ConfigManager struct { |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 70 | Backend *db.Backend |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 71 | 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/ |
| 87 | type ComponentConfig struct { |
| 88 | cManager *ConfigManager |
| 89 | componentLabel string |
| 90 | configType ConfigType |
| 91 | changeEventChan chan *ConfigChangeEvent |
| 92 | kvStoreEventChan chan *kvstore.Event |
| 93 | } |
| 94 | |
| 95 | func NewConfigManager(kvClient kvstore.Client, kvStoreType, kvStoreHost string, kvStorePort, kvStoreTimeout int) *ConfigManager { |
| 96 | |
| 97 | return &ConfigManager{ |
| 98 | KvStoreConfigPrefix: defaultkvStoreConfigPath, |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 99 | Backend: &db.Backend{ |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 100 | Client: kvClient, |
| 101 | StoreType: kvStoreType, |
| 102 | Host: kvStoreHost, |
| 103 | Port: kvStorePort, |
| 104 | Timeout: kvStoreTimeout, |
| 105 | PathPrefix: kvStoreDataPathPrefix, |
| 106 | }, |
| 107 | } |
| 108 | } |
| 109 | |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 110 | // RetrieveComponentList list the component Names for which loglevel is stored in kvstore |
| 111 | func (c *ConfigManager) RetrieveComponentList(ctx context.Context, configType ConfigType) ([]string, error) { |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 112 | data, err := c.Backend.List(ctx, c.KvStoreConfigPrefix) |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 117 | // Looping through the data recieved from the Backend for config |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 118 | // 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 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 139 | // Initialize the component config |
| 140 | func (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 | |
| 152 | func (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. |
| 165 | func (c *ComponentConfig) MonitorForConfigChange(ctx context.Context) chan *ConfigChangeEvent { |
| 166 | key := c.makeConfigPath() |
| 167 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 168 | logger.Debugw("monitoring-for-config-change", log.Fields{"key": key}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 169 | |
| 170 | c.changeEventChan = make(chan *ConfigChangeEvent, 1) |
| 171 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 172 | c.kvStoreEventChan = c.cManager.Backend.CreateWatch(ctx, key, true) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 173 | |
| 174 | go c.processKVStoreWatchEvents() |
| 175 | |
| 176 | return c.changeEventChan |
| 177 | } |
| 178 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 179 | // processKVStoreWatchEvents process event channel recieved from the Backend for any ChangeType |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 180 | // It checks for the EventType is valid or not.For the valid EventTypes creates ConfigChangeEvent and send it on channel |
| 181 | func (c *ComponentConfig) processKVStoreWatchEvents() { |
| 182 | |
| 183 | ccKeyPrefix := c.makeConfigPath() |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 184 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 185 | logger.Debugw("processing-kvstore-event-change", log.Fields{"key-prefix": ccKeyPrefix}) |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 186 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 187 | ccPathPrefix := c.cManager.Backend.PathPrefix + ccKeyPrefix + kvStorePathSeparator |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 188 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 189 | for watchResp := range c.kvStoreEventChan { |
| 190 | |
| 191 | if watchResp.EventType == kvstore.CONNECTIONDOWN || watchResp.EventType == kvstore.UNKNOWN { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 192 | 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] | 193 | 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 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 208 | // Retrieves value of a specific config key. Value of key is returned in String format |
| 209 | func (c *ComponentConfig) Retrieve(ctx context.Context, configKey string) (string, error) { |
| 210 | key := c.makeConfigPath() + "/" + configKey |
| 211 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 212 | logger.Debugw("retrieving-config", log.Fields{"key": key}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 213 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 214 | if kvpair, err := c.cManager.Backend.Get(ctx, key); err != nil { |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 215 | 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), "\"") |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 222 | logger.Debugw("retrieved-config", log.Fields{"key": key, "value": value}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 223 | return value, nil |
| 224 | } |
| 225 | } |
| 226 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 227 | func (c *ComponentConfig) RetrieveAll(ctx context.Context) (map[string]string, error) { |
| 228 | key := c.makeConfigPath() |
| 229 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 230 | logger.Debugw("retreiving-list", log.Fields{"key": key}) |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 231 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 232 | data, err := c.cManager.Backend.List(ctx, key) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 233 | if err != nil { |
| 234 | return nil, err |
| 235 | } |
| 236 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 237 | // Looping through the data recieved from the Backend for the given key |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 238 | // 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) |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 242 | ccPathPrefix := c.cManager.Backend.PathPrefix + kvStorePathSeparator + key + kvStorePathSeparator |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 243 | 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 | |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 250 | func (c *ComponentConfig) Save(ctx context.Context, configKey string, configValue string) error { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 251 | key := c.makeConfigPath() + "/" + configKey |
| 252 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 253 | logger.Debugw("saving-config", log.Fields{"key": key, "value": configValue}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 254 | |
| 255 | //save the data for update config |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 256 | if err := c.cManager.Backend.Put(ctx, key, configValue); err != nil { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 257 | return err |
| 258 | } |
| 259 | return nil |
| 260 | } |
| 261 | |
divyadesai | 42bcb67 | 2020-03-04 11:40:53 +0000 | [diff] [blame] | 262 | func (c *ComponentConfig) Delete(ctx context.Context, configKey string) error { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 263 | //construct key using makeConfigPath |
| 264 | key := c.makeConfigPath() + "/" + configKey |
| 265 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 266 | logger.Debugw("deleting-config", log.Fields{"key": key}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 267 | //delete the config |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 268 | if err := c.cManager.Backend.Delete(ctx, key); err != nil { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 269 | return err |
| 270 | } |
| 271 | return nil |
| 272 | } |