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