[VOL-2312] Logging - Integrate voltctl with new etcd-based dynamic loglevel mechanism

Change-Id: Iae696fbfcb40c1b82089952d1a95c4553458925a
diff --git a/pkg/config/configmanager.go b/pkg/config/configmanager.go
index 8f96b22..462b743 100644
--- a/pkg/config/configmanager.go
+++ b/pkg/config/configmanager.go
@@ -24,6 +24,13 @@
 	"strings"
 )
 
+func init() {
+	_, err := log.AddPackage(log.JSON, log.FatalLevel, nil)
+	if err != nil {
+		log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err})
+	}
+}
+
 const (
 	defaultkvStoreConfigPath = "config"
 	kvStoreDataPathPrefix    = "/service/voltha"
@@ -102,6 +109,35 @@
 	}
 }
 
+// RetrieveComponentList list the component Names for which loglevel is stored in kvstore
+func (c *ConfigManager) RetrieveComponentList(ctx context.Context, configType ConfigType) ([]string, error) {
+	data, err := c.backend.List(ctx, c.KvStoreConfigPrefix)
+	if err != nil {
+		return nil, err
+	}
+
+	// Looping through the data recieved from the backend for config
+	// Trimming and Splitting the required key and value from data and  storing as componentName,PackageName and Level
+	// For Example, recieved key would be <Backend Prefix Path>/<Config Prefix>/<Component Name>/<Config Type>/default and value \"DEBUG\"
+	// Then in default will be stored as PackageName,componentName as <Component Name> and DEBUG will be stored as value in List struct
+	ccPathPrefix := kvStorePathSeparator + configType.String() + kvStorePathSeparator
+	pathPrefix := kvStoreDataPathPrefix + kvStorePathSeparator + c.KvStoreConfigPrefix + kvStorePathSeparator
+	var list []string
+	keys := make(map[string]interface{})
+	for attr := range data {
+		cname := strings.TrimPrefix(attr, pathPrefix)
+		cName := strings.SplitN(cname, ccPathPrefix, 2)
+		if len(cName) != 2 {
+			continue
+		}
+		if _, exist := keys[cName[0]]; !exist {
+			keys[cName[0]] = nil
+			list = append(list, cName[0])
+		}
+	}
+	return list, nil
+}
+
 // Initialize the component config
 func (cm *ConfigManager) InitComponentConfig(componentLabel string, configType ConfigType) *ComponentConfig {
 
@@ -147,8 +183,11 @@
 func (c *ComponentConfig) processKVStoreWatchEvents() {
 
 	ccKeyPrefix := c.makeConfigPath()
+
 	log.Debugw("processing-kvstore-event-change", log.Fields{"key-prefix": ccKeyPrefix})
+
 	ccPathPrefix := c.cManager.backend.PathPrefix + ccKeyPrefix + kvStorePathSeparator
+
 	for watchResp := range c.kvStoreEventChan {
 
 		if watchResp.EventType == kvstore.CONNECTIONDOWN || watchResp.EventType == kvstore.UNKNOWN {
@@ -172,6 +211,7 @@
 	key := c.makeConfigPath()
 
 	log.Debugw("retreiving-list", log.Fields{"key": key})
+
 	data, err := c.cManager.backend.List(ctx, key)
 	if err != nil {
 		return nil, err
@@ -190,7 +230,7 @@
 	return res, nil
 }
 
-func (c *ComponentConfig) Save(configKey string, configValue string, ctx context.Context) error {
+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})
@@ -202,7 +242,7 @@
 	return nil
 }
 
-func (c *ComponentConfig) Delete(configKey string, ctx context.Context) error {
+func (c *ComponentConfig) Delete(ctx context.Context, configKey string) error {
 	//construct key using makeConfigPath
 	key := c.makeConfigPath() + "/" + configKey
 
diff --git a/pkg/db/backend.go b/pkg/db/backend.go
index 04fe35d..faa86ed 100644
--- a/pkg/db/backend.go
+++ b/pkg/db/backend.go
@@ -222,7 +222,7 @@
 	defer b.Unlock()
 
 	formattedPath := b.makePath(key)
-	logger.Debugw("putting-key", log.Fields{"key": key, "value": string(value.([]byte)), "path": formattedPath})
+	logger.Debugw("putting-key", log.Fields{"key": key, "value": value, "path": formattedPath})
 
 	err := b.Client.Put(ctx, formattedPath, value)