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