[VOL-2782] Update go.mod of voltha-go,open-olt-adapter,ofagent-go and voltctl to be compatible with latest version of voltha-lig-go

Change-Id: Ib377da4604a18c2e1505e3f3423703fc1d16f9d4
diff --git a/go.mod b/go.mod
index c6583b6..a0a9e8e 100755
--- a/go.mod
+++ b/go.mod
@@ -6,7 +6,7 @@
 	github.com/gogo/protobuf v1.3.0
 	github.com/golang/protobuf v1.3.2
 	github.com/google/uuid v1.1.1
-	github.com/opencord/voltha-lib-go/v3 v3.0.20
+	github.com/opencord/voltha-lib-go/v3 v3.0.23
 	github.com/opencord/voltha-protos/v3 v3.2.8
 	github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
 	github.com/stretchr/testify v1.4.0
diff --git a/go.sum b/go.sum
index 2a8899f..d3e752e 100644
--- a/go.sum
+++ b/go.sum
@@ -190,8 +190,8 @@
 github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
 github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
 github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
-github.com/opencord/voltha-lib-go/v3 v3.0.20 h1:b3r4tKOREX63jOxDDioipPP/hkCcVwmRu8MXLx/LNMk=
-github.com/opencord/voltha-lib-go/v3 v3.0.20/go.mod h1:QuAohPQ+InSw+8XgCFxnp4cpHWcxO2efVTtiBFUmuOY=
+github.com/opencord/voltha-lib-go/v3 v3.0.23 h1:QJkosPdVm85AaLzVEk4umzcL7p2MmwukU9f83rZTIOU=
+github.com/opencord/voltha-lib-go/v3 v3.0.23/go.mod h1:QuAohPQ+InSw+8XgCFxnp4cpHWcxO2efVTtiBFUmuOY=
 github.com/opencord/voltha-protos/v3 v3.2.3 h1:Wv73mw1Ye0bCfyhOk5svgrlE2tLizHq6tQluoDq9Vg8=
 github.com/opencord/voltha-protos/v3 v3.2.3/go.mod h1:RIGHt7b80BHpHh3ceodknh0DxUjUHCWSbYbZqRx7Og0=
 github.com/opencord/voltha-protos/v3 v3.2.8 h1:FuMEmUsW4WRpIsNCcELM9QBZZwBLRvuw85DVvfpZ1do=
diff --git a/rw_core/main.go b/rw_core/main.go
index 3068c98..2cc5240 100644
--- a/rw_core/main.go
+++ b/rw_core/main.go
@@ -115,7 +115,7 @@
 		log.Fatal(err)
 	}
 	cm := conf.NewConfigManager(rw.kvClient, rw.config.KVStoreType, rw.config.KVStoreHost, rw.config.KVStorePort, rw.config.KVStoreTimeout)
-	go conf.ProcessLogConfigChange(cm, ctx)
+	go conf.StartLogLevelConfigProcessing(cm, ctx)
 
 	// Setup KV transaction context
 	if err := c.SetTransactionContext(instanceID,
diff --git a/vendor/github.com/opencord/voltha-lib-go/v3/pkg/config/configmanager.go b/vendor/github.com/opencord/voltha-lib-go/v3/pkg/config/configmanager.go
index 462b743..441c488 100644
--- a/vendor/github.com/opencord/voltha-lib-go/v3/pkg/config/configmanager.go
+++ b/vendor/github.com/opencord/voltha-lib-go/v3/pkg/config/configmanager.go
@@ -33,7 +33,7 @@
 
 const (
 	defaultkvStoreConfigPath = "config"
-	kvStoreDataPathPrefix    = "/service/voltha"
+	kvStoreDataPathPrefix    = "service/voltha"
 	kvStorePathSeparator     = "/"
 )
 
@@ -59,6 +59,10 @@
 	Delete
 )
 
+func (ce ChangeEvent) String() string {
+	return [...]string{"Put", "Delete"}[ce]
+}
+
 // ConfigChangeEvent represents config for the events recieved from watch
 // For example,ChangeType is Put ,ConfigAttribute default
 type ConfigChangeEvent struct {
@@ -207,6 +211,25 @@
 	}
 }
 
+// Retrieves value of a specific config key. Value of key is returned in String format
+func (c *ComponentConfig) Retrieve(ctx context.Context, configKey string) (string, error) {
+	key := c.makeConfigPath() + "/" + configKey
+
+	log.Debugw("retrieving-config", log.Fields{"key": key})
+
+	if kvpair, err := c.cManager.backend.Get(ctx, key); err != nil {
+		return "", err
+	} else {
+		if kvpair == nil {
+			return "", fmt.Errorf("config-key-does-not-exist : %s", key)
+		}
+
+		value := strings.Trim(fmt.Sprintf("%s", kvpair.Value), "\"")
+		log.Debugw("retrieved-config", log.Fields{"key": key, "value": value})
+		return value, nil
+	}
+}
+
 func (c *ComponentConfig) RetrieveAll(ctx context.Context) (map[string]string, error) {
 	key := c.makeConfigPath()
 
@@ -233,7 +256,7 @@
 func (c *ComponentConfig) Save(ctx context.Context, configKey string, configValue string) error {
 	key := c.makeConfigPath() + "/" + configKey
 
-	log.Debugw("saving-key", log.Fields{"key": key, "value": configValue})
+	log.Debugw("saving-config", log.Fields{"key": key, "value": configValue})
 
 	//save the data for update config
 	if err := c.cManager.backend.Put(ctx, key, configValue); err != nil {
@@ -246,7 +269,7 @@
 	//construct key using makeConfigPath
 	key := c.makeConfigPath() + "/" + configKey
 
-	log.Debugw("deleting-key", log.Fields{"key": key})
+	log.Debugw("deleting-config", log.Fields{"key": key})
 	//delete the config
 	if err := c.cManager.backend.Delete(ctx, key); err != nil {
 		return err
diff --git a/vendor/github.com/opencord/voltha-lib-go/v3/pkg/config/logcontroller.go b/vendor/github.com/opencord/voltha-lib-go/v3/pkg/config/logcontroller.go
index b45c2c8..65927e6 100644
--- a/vendor/github.com/opencord/voltha-lib-go/v3/pkg/config/logcontroller.go
+++ b/vendor/github.com/opencord/voltha-lib-go/v3/pkg/config/logcontroller.go
@@ -14,8 +14,10 @@
  * limitations under the License.
  */
 
-// Package Config provides dynamic logging configuration for specific Voltha component type implemented using backend.The package can be used in following manner
-// Any Voltha component type can start dynamic logging by starting goroutine of ProcessLogConfigChange after starting kvClient for the component.
+// Package Config provides dynamic logging configuration for specific Voltha component with loglevel lookup
+// from etcd kvstore implemented using backend.
+// Any Voltha component can start utilizing dynamic logging by starting goroutine of StartLogLevelConfigProcessing after
+// starting kvClient for the component.
 
 package config
 
@@ -29,6 +31,12 @@
 	"strings"
 )
 
+const (
+	defaultLogLevelKey                = "default" // kvstore key containing default loglevel
+	globalConfigRootNode              = "global"  // Root Node in kvstore containing global config
+	initialGlobalDefaultLogLevelValue = "WARN"    // Hard-coded Global Default loglevel pushed at PoD startup
+)
+
 // ComponentLogController represents a Configuration for Logging Config of specific Voltha component type
 // It stores ComponentConfig and GlobalConfig of loglevel config of specific Voltha component type
 // For example,ComponentLogController instance will be created for rw-core component
@@ -38,6 +46,7 @@
 	GlobalConfig        *ComponentConfig
 	configManager       *ConfigManager
 	logHash             [16]byte
+	initialLogLevel     string // Initial default log level set by helm chart
 }
 
 func NewComponentLogController(cm *ConfigManager) (*ComponentLogController, error) {
@@ -48,40 +57,85 @@
 		return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env")
 	}
 
+	var defaultLogLevel string
+	var err error
+	// Retrieve and save default log level; used for fallback if all loglevel config is cleared in etcd
+	if defaultLogLevel, err = log.LogLevelToString(log.GetDefaultLogLevel()); err != nil {
+		defaultLogLevel = "DEBUG"
+	}
+
 	return &ComponentLogController{
 		ComponentName:       componentName,
 		componentNameConfig: nil,
 		GlobalConfig:        nil,
 		configManager:       cm,
+		initialLogLevel:     defaultLogLevel,
 	}, nil
 
 }
 
-// ProcessLogConfigChange initialize component config and global config
-func ProcessLogConfigChange(cm *ConfigManager, ctx context.Context) {
+// StartLogLevelConfigProcessing initialize component config and global config
+// Then, it persists initial default Loglevels into Config Store before
+// starting the loading and processing of all Log Configuration
+func StartLogLevelConfigProcessing(cm *ConfigManager, ctx context.Context) {
 	cc, err := NewComponentLogController(cm)
 	if err != nil {
 		log.Errorw("unable-to-construct-component-log-controller-instance-for-log-config-monitoring", log.Fields{"error": err})
 		return
 	}
 
-	log.Debugw("processing-log-config-change", log.Fields{"cc": cc})
-
-	cc.GlobalConfig = cm.InitComponentConfig("global", ConfigTypeLogLevel)
+	cc.GlobalConfig = cm.InitComponentConfig(globalConfigRootNode, ConfigTypeLogLevel)
 	log.Debugw("global-log-config", log.Fields{"cc-global-config": cc.GlobalConfig})
 
 	cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogLevel)
 	log.Debugw("component-log-config", log.Fields{"cc-component-name-config": cc.componentNameConfig})
 
+	cc.persistInitialDefaultLogConfigs(ctx)
+
 	cc.processLogConfig(ctx)
 }
 
-// ProcessLogConfig wait on componentn config and global config channel for any changes
-// Event channel will be recieved from backend for valid change type
+// Method to persist Global default loglevel into etcd, if not set yet
+// It also checks and set Component default loglevel into etcd with initial loglevel set from command line
+func (c *ComponentLogController) persistInitialDefaultLogConfigs(ctx context.Context) {
+
+	_, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
+	if err != nil {
+		log.Debugw("failed-to-retrieve-global-default-log-config-at-startup", log.Fields{"error": err})
+
+		err = c.GlobalConfig.Save(ctx, defaultLogLevelKey, initialGlobalDefaultLogLevelValue)
+		if err != nil {
+			log.Errorw("failed-to-persist-global-default-log-config-at-startup", log.Fields{"error": err, "loglevel": initialGlobalDefaultLogLevelValue})
+		}
+	}
+
+	_, err = c.componentNameConfig.Retrieve(ctx, defaultLogLevelKey)
+	if err != nil {
+		log.Debugw("failed-to-retrieve-component-default-log-config-at-startup", log.Fields{"error": err})
+
+		err = c.componentNameConfig.Save(ctx, defaultLogLevelKey, c.initialLogLevel)
+		if err != nil {
+			log.Errorw("failed-to-persist-component-default-log-config-at-startup", log.Fields{"error": err, "loglevel": c.initialLogLevel})
+		}
+	}
+}
+
+// ProcessLogConfig will first load and apply log config and then start waiting on component config and global config
+// channels for any changes. Event channel will be recieved from backend for valid change type
 // Then data for componentn log config and global log config will be retrieved from backend and stored in updatedLogConfig in precedence order
 // If any changes in updatedLogConfig will be applied on component
 func (c *ComponentLogController) processLogConfig(ctx context.Context) {
 
+	// Load and apply Log Config for first time
+	initialLogConfig, err := c.buildUpdatedLogConfig(ctx)
+	if err != nil {
+		log.Warnw("unable-to-load-log-config-at-startup", log.Fields{"error": err})
+	} else {
+		if err := c.loadAndApplyLogConfig(initialLogConfig); err != nil {
+			log.Warnw("unable-to-apply-log-config-at-startup", log.Fields{"error": err})
+		}
+	}
+
 	componentConfigEventChan := c.componentNameConfig.MonitorForConfigChange(ctx)
 
 	globalConfigEventChan := c.GlobalConfig.MonitorForConfigChange(ctx)
@@ -94,7 +148,7 @@
 		case configEvent = <-componentConfigEventChan:
 
 		}
-		log.Debugw("processing-log-config-change", log.Fields{"config-event": configEvent})
+		log.Debugw("processing-log-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute})
 
 		updatedLogConfig, err := c.buildUpdatedLogConfig(ctx)
 		if err != nil {
@@ -112,12 +166,12 @@
 }
 
 // get active loglevel from the zap logger
-func getActiveLogLevel() map[string]string {
-	loglevel := make(map[string]string)
+func getActiveLogLevels() map[string]string {
+	loglevels := make(map[string]string)
 
 	// now do the default log level
 	if level, err := log.LogLevelToString(log.GetDefaultLogLevel()); err == nil {
-		loglevel["default"] = level
+		loglevels[defaultLogLevelKey] = level
 	}
 
 	// do the per-package log levels
@@ -125,61 +179,69 @@
 		level, err := log.GetPackageLogLevel(packageName)
 		if err != nil {
 			log.Warnw("unable-to-fetch-current-active-loglevel-for-package-name", log.Fields{"package-name": packageName, "error": err})
+			continue
 		}
 
-		packagename := strings.ReplaceAll(packageName, "/", "#")
 		if l, err := log.LogLevelToString(level); err == nil {
-			loglevel[packagename] = l
+			loglevels[packageName] = l
 		}
-
 	}
-	log.Debugw("getting-log-levels-from-zap-logger", log.Fields{"log-level": loglevel})
 
-	return loglevel
+	log.Debugw("retreived-log-levels-from-zap-logger", log.Fields{"loglevels": loglevels})
+
+	return loglevels
 }
 
 func (c *ComponentLogController) getGlobalLogConfig(ctx context.Context) (string, error) {
 
-	globalDefaultLogLevel := ""
-	globalLogConfig, err := c.GlobalConfig.RetrieveAll(ctx)
+	globalDefaultLogLevel, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
 	if err != nil {
 		return "", err
 	}
 
-	if globalLevel, ok := globalLogConfig["default"]; ok {
-		if _, err := log.StringToLogLevel(globalLevel); err != nil {
-			log.Warnw("unsupported-loglevel-config-defined-at-global-context-pacakge-name", log.Fields{"log-level": globalLevel})
-		} else {
-			globalDefaultLogLevel = globalLevel
-		}
+	// Handle edge cases when global default loglevel is deleted directly from etcd or set to a invalid value
+	// We should use hard-coded initial default value in such cases
+	if globalDefaultLogLevel == "" {
+		log.Warn("global-default-loglevel-not-found-in-config-store")
+		globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
 	}
-	log.Debugw("retrieved-global-log-config", log.Fields{"global-log-config": globalLogConfig})
+
+	if _, err := log.StringToLogLevel(globalDefaultLogLevel); err != nil {
+		log.Warnw("unsupported-loglevel-config-defined-at-global-default", log.Fields{"log-level": globalDefaultLogLevel})
+		globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
+	}
+
+	log.Debugw("retrieved-global-default-loglevel", log.Fields{"level": globalDefaultLogLevel})
 
 	return globalDefaultLogLevel, nil
 }
 
-func (c *ComponentLogController) getComponentLogConfig(globalDefaultLogLevel string, ctx context.Context) (map[string]string, error) {
-	var defaultPresent bool
+func (c *ComponentLogController) getComponentLogConfig(ctx context.Context, globalDefaultLogLevel string) (map[string]string, error) {
 	componentLogConfig, err := c.componentNameConfig.RetrieveAll(ctx)
 	if err != nil {
 		return nil, err
 	}
 
-	for componentKey, componentLevel := range componentLogConfig {
-		if _, err := log.StringToLogLevel(componentLevel); err != nil || componentKey == "" {
-			log.Warnw("unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": componentKey, "log-level": componentLevel})
-			delete(componentLogConfig, componentKey)
+	effectiveDefaultLogLevel := ""
+	for logConfigKey, logConfigValue := range componentLogConfig {
+		if _, err := log.StringToLogLevel(logConfigValue); err != nil || logConfigKey == "" {
+			log.Warnw("unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": logConfigKey, "log-level": logConfigValue})
+			delete(componentLogConfig, logConfigKey)
 		} else {
-			if componentKey == "default" {
-				defaultPresent = true
+			if logConfigKey == defaultLogLevelKey {
+				effectiveDefaultLogLevel = componentLogConfig[defaultLogLevelKey]
 			}
 		}
 	}
-	if !defaultPresent {
-		if globalDefaultLogLevel != "" {
-			componentLogConfig["default"] = globalDefaultLogLevel
-		}
+
+	// if default loglevel is not configured for the component, component should use
+	// default loglevel configured at global level
+	if effectiveDefaultLogLevel == "" {
+		effectiveDefaultLogLevel = globalDefaultLogLevel
 	}
+
+	componentLogConfig[defaultLogLevelKey] = effectiveDefaultLogLevel
+
 	log.Debugw("retrieved-component-log-config", log.Fields{"component-log-level": componentLogConfig})
 
 	return componentLogConfig, nil
@@ -194,16 +256,20 @@
 func (c *ComponentLogController) buildUpdatedLogConfig(ctx context.Context) (map[string]string, error) {
 	globalLogLevel, err := c.getGlobalLogConfig(ctx)
 	if err != nil {
-		return nil, err
+		log.Errorw("unable-to-retrieve-global-log-config", log.Fields{"err": err})
 	}
 
-	componentLogConfig, err := c.getComponentLogConfig(globalLogLevel, ctx)
+	componentLogConfig, err := c.getComponentLogConfig(ctx, globalLogLevel)
 	if err != nil {
 		return nil, err
 	}
 
-	log.Debugw("building-and-updating-log-config", log.Fields{"component-log-config": componentLogConfig})
-	return componentLogConfig, nil
+	finalLogConfig := make(map[string]string)
+	for packageName, logLevel := range componentLogConfig {
+		finalLogConfig[strings.ReplaceAll(packageName, "#", "/")] = logLevel
+	}
+
+	return finalLogConfig, nil
 }
 
 // load and apply the current configuration for component name
@@ -216,64 +282,70 @@
 		return err
 	}
 
-	log.Debugw("loading-and-applying-log-config", log.Fields{"log-config": logConfig})
 	if c.logHash != currentLogHash {
 		UpdateLogLevels(logConfig)
 		c.logHash = currentLogHash
+	} else {
+		log.Debug("effective-loglevel-config-same-as-currently-active")
 	}
+
 	return nil
 }
 
-// getDefaultLogLevel to return active default log level
-func getDefaultLogLevel(logConfig map[string]string) string {
+// createModifiedLogLevels loops through the activeLogLevels recieved from zap logger and updatedLogLevels recieved from buildUpdatedLogConfig
+// to identify and create map of modified Log Levels of 2 types:
+// - Packages for which log level has been changed
+// - Packages for which log level config has been cleared - set to default log level
+func createModifiedLogLevels(activeLogLevels, updatedLogLevels map[string]string) map[string]string {
+	defaultLevel := updatedLogLevels[defaultLogLevelKey]
 
-	for key, level := range logConfig {
-		if key == "default" {
-			return level
-		}
-	}
-	return ""
-}
-
-// createCurrentLogLevel loop through the activeLogLevels recieved from zap logger and updatedLogLevels recieved from buildUpdatedLogConfig
-// The packageName is present or not will be checked in updatedLogLevels ,if the package name is not present then updatedLogLevels will be updated with
-// the packageName and loglevel with  default log level
-func createCurrentLogLevel(activeLogLevels, updatedLogLevels map[string]string) map[string]string {
-	level := getDefaultLogLevel(updatedLogLevels)
+	modifiedLogLevels := make(map[string]string)
 	for activeKey, activeLevel := range activeLogLevels {
 		if _, exist := updatedLogLevels[activeKey]; !exist {
-			if level != "" {
-				activeLevel = level
+			if activeLevel != defaultLevel {
+				modifiedLogLevels[activeKey] = defaultLevel
 			}
-			updatedLogLevels[activeKey] = activeLevel
+		} else if activeLevel != updatedLogLevels[activeKey] {
+			modifiedLogLevels[activeKey] = updatedLogLevels[activeKey]
 		}
 	}
-	return updatedLogLevels
+
+	// Log warnings for all invalid packages for which log config has been set
+	for key, value := range updatedLogLevels {
+		if _, exist := activeLogLevels[key]; !exist {
+			log.Warnw("ignoring-loglevel-set-for-invalid-package", log.Fields{"package": key, "log-level": value})
+		}
+	}
+
+	return modifiedLogLevels
 }
 
 // updateLogLevels update the loglevels for the component
 // retrieve active confguration from logger
 // compare with entries one by one and apply
-func UpdateLogLevels(logLevel map[string]string) {
+func UpdateLogLevels(updatedLogConfig map[string]string) {
 
-	activeLogLevels := getActiveLogLevel()
-	currentLogLevel := createCurrentLogLevel(activeLogLevels, logLevel)
-	for key, level := range currentLogLevel {
-		if key == "default" {
+	activeLogLevels := getActiveLogLevels()
+	changedLogLevels := createModifiedLogLevels(activeLogLevels, updatedLogConfig)
+
+	// If no changed log levels are found, just return. It may happen on configuration of a invalid package
+	if len(changedLogLevels) == 0 {
+		log.Debug("no-change-in-effective-loglevel-config")
+		return
+	}
+
+	log.Debugw("applying-log-level-for-modified-packages", log.Fields{"changed-log-levels": changedLogLevels})
+	for key, level := range changedLogLevels {
+		if key == defaultLogLevelKey {
 			if l, err := log.StringToLogLevel(level); err == nil {
 				log.SetDefaultLogLevel(l)
 			}
 		} else {
-			pname := strings.ReplaceAll(key, "#", "/")
-			if _, err := log.AddPackage(log.JSON, log.DebugLevel, nil, pname); err != nil {
-				log.Warnw("unable-to-add-log-package", log.Fields{"package-name": pname, "error": err})
-			}
 			if l, err := log.StringToLogLevel(level); err == nil {
-				log.SetPackageLogLevel(pname, l)
+				log.SetPackageLogLevel(key, l)
 			}
 		}
 	}
-	log.Debugw("updated-log-level", log.Fields{"current-log-level": currentLogLevel})
 }
 
 // generate md5 hash of key value pairs appended into a single string
diff --git a/vendor/modules.txt b/vendor/modules.txt
index ccccabc..5130080 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -96,7 +96,7 @@
 github.com/modern-go/concurrent
 # github.com/modern-go/reflect2 v1.0.1
 github.com/modern-go/reflect2
-# github.com/opencord/voltha-lib-go/v3 v3.0.20
+# github.com/opencord/voltha-lib-go/v3 v3.0.23
 github.com/opencord/voltha-lib-go/v3/pkg/adapters
 github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif
 github.com/opencord/voltha-lib-go/v3/pkg/adapters/common