Enhance config handling

Change-Id: Id63d2cc4461daaab4d0b6a2990a5f108bbe97e73
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
diff --git a/pkg/cm/cm.go b/pkg/cm/cm.go
index a073c0a..fb80276 100755
--- a/pkg/cm/cm.go
+++ b/pkg/cm/cm.go
@@ -23,16 +23,15 @@
 	"encoding/json"
 	"errors"
 	"fmt"
-	"github.com/spf13/viper"
-	"github.com/valyala/fastjson"
-	"github.com/xeipuuv/gojsonschema"
 	"io/ioutil"
 	"os"
 	"path"
 	"regexp"
 	"strings"
 	"strconv"
-	"time"
+	"github.com/spf13/viper"
+	"github.com/valyala/fastjson"
+	"github.com/xeipuuv/gojsonschema"
 
 	"gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/appmgr"
 	"gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/models"
@@ -45,44 +44,54 @@
 	return &CM{}
 }
 
-func (cm *CM) UploadConfig() (cfg models.AllXappConfig) {
-	ns := cm.GetNamespace("")
+func (cm *CM) UploadConfigAll() (configList models.AllXappConfig) {
+	return cm.UploadConfigElement("")
+}
+
+func (cm *CM) UploadConfigElement(Element string) (configList models.AllXappConfig) {
+	namespace := cm.GetNamespace("")
 	for _, name := range cm.GetNamesFromHelmRepo() {
-		if name == "appmgr" {
+		var activeConfig interface{}
+		xAppName := name
+		if err := cm.GetConfigmap(xAppName, namespace, &activeConfig); err != nil {
+			appmgr.Logger.Info("No active configMap found for '%s', ignoring ...", xAppName)
 			continue
 		}
 
+		if Element != "" {
+			m := activeConfig.(map[string]interface{})
+			if m[Element] == nil {
+				appmgr.Logger.Info("xApp '%s' doesn't have requested element '%s' in config", name, Element)
+				continue
+			}
+			activeConfig = m[Element]
+		}
+
 		c := models.XAppConfig{
-			Metadata: &models.ConfigMetadata{Name: &name, Namespace: ns, ConfigName: cm.GetConfigMapName(name, ns)},
+			Metadata: &models.ConfigMetadata{XappName: &xAppName, Namespace: &namespace},
+			Config: activeConfig,
 		}
-
-		err := cm.ReadSchema(name, &c)
-		if err != nil {
-			continue
-		}
-
-		err = cm.ReadConfigMap(c.Metadata.ConfigName, ns, &c.Config)
-		if err != nil {
-			appmgr.Logger.Info("No active configMap found, using default!")
-		}
-
-		cfg = append(cfg, &c)
+		configList = append(configList, &c)
 	}
 	return
 }
 
-func (cm *CM) ReadSchema(name string, c *models.XAppConfig) (err error) {
+func (cm *CM) GetConfigmap(name, namespace string, c *interface{}) (err error) {
+	cmJson, err := cm.ReadConfigmap(name, namespace)
+	if err != nil {
+		return err
+	}
+
+	return json.Unmarshal([]byte(cmJson), &c)
+}
+
+func (cm *CM) ReadSchema(name string, desc *interface{}) (err error) {
 	if err = cm.FetchChart(name); err != nil {
 		return
 	}
 
 	tarDir := viper.GetString("xapp.tarDir")
-	err = cm.ReadFile(path.Join(tarDir, name, viper.GetString("xapp.schema")), &c.Descriptor)
-	if err != nil {
-		return
-	}
-
-	err = cm.ReadFile(path.Join(tarDir, name, viper.GetString("xapp.config")), &c.Config)
+	err = cm.ReadFile(path.Join(tarDir, name, viper.GetString("xapp.schema")), desc)
 	if err != nil {
 		return
 	}
@@ -94,148 +103,71 @@
 	return
 }
 
-func (cm *CM) ReadConfigMap(ConfigName string, ns string, c *interface{}) (err error) {
-	args := fmt.Sprintf("get configmap -o jsonpath='{.data.config-file\\.json}' -n %s %s", ns, ConfigName)
-	configMapJson, err := util.KubectlExec(args)
-	if err != nil {
-		return
+func (cm *CM) UpdateConfigMap(r models.XAppConfig) (models.ConfigValidationErrors, error) {
+	fmt.Printf("Configmap update: xappName=%s namespace=%s config: %v", *r.Metadata.XappName, *r.Metadata.Namespace, r.Config)
+	if validationErrors, err := cm.Validate(r); err != nil {
+		return validationErrors, err
 	}
 
-	err = json.Unmarshal([]byte(configMapJson), &c)
+	cmContent, err := cm.BuildConfigMap(r)
 	if err != nil {
-		return
+		return nil, err
 	}
 
-	return
+	if err := cm.GenerateJSONFile(cmContent); err != nil {
+		return nil, err
+	}
+	err = cm.ReplaceConfigMap(*r.Metadata.XappName, *r.Metadata.Namespace)
+
+	return nil, err
 }
 
-func (cm *CM) ApplyConfigMap(r models.XAppConfig, action string) (err error) {
-	c := appmgr.ConfigMap{
-		Kind:       "ConfigMap",
-		ApiVersion: "v1",
-		Metadata:   appmgr.CMMetadata{Name: *r.Metadata.Name, Namespace: r.Metadata.Namespace},
-		Data:       r.Config,
-	}
-
-	cmJson, err := json.Marshal(c.Data)
+func (cm *CM) BuildConfigMap(r models.XAppConfig) (string, error) {
+	configJson, err := json.Marshal(r.Config)
 	if err != nil {
 		appmgr.Logger.Info("Config marshalling failed: %v", err)
-		return
+		return "", err
 	}
 
-	cmFile := viper.GetString("xapp.tmpConfig")
-	err = ioutil.WriteFile(cmFile, cmJson, 0644)
+	cmContent, err := cm.ReadConfigmap(*r.Metadata.XappName, *r.Metadata.Namespace)
 	if err != nil {
-		appmgr.Logger.Info("WriteFile failed: %v", err)
-		return
+		return "", err
 	}
 
-	cmd := " create configmap -n %s %s --from-file=%s -o json --dry-run | kubectl %s -f -"
-	args := fmt.Sprintf(cmd, r.Metadata.Namespace, r.Metadata.ConfigName, cmFile, action)
-	_, err = util.KubectlExec(args)
-	if err != nil {
-		return
-	}
-	appmgr.Logger.Info("Configmap changes done!")
-
-	return
-}
-
-func (cm *CM) GetConfigMap(m models.XappDescriptor, c *interface{}) (err error) {
-	return cm.ReadConfigMap(cm.GetConfigMapName(*m.XappName, m.Namespace), m.Namespace, c)
-}
-
-func (cm *CM) CreateConfigMap(r models.XAppConfig) (errList models.ConfigValidationErrors, err error) {
-	if errList, err = cm.Validate(r); err != nil {
-		return
-	}
-	err = cm.ApplyConfigMap(r, "create")
-	return
-}
-
-func (cm *CM) UpdateConfigMap(r models.XAppConfig) (errList models.ConfigValidationErrors, err error) {
-	if errList, err = cm.Validate(r); err != nil {
-		return
-	}
-
-	// Re-create the configmap with the new parameters
-	err = cm.ApplyConfigMap(r, "apply")
-	return
-}
-
-func (cm *CM) DeleteConfigMap(r models.ConfigMetadata) (c interface{}, err error) {
-	err = cm.ReadConfigMap(r.ConfigName, r.Namespace, &c)
+	v, err := cm.ParseJson(cmContent)
 	if err == nil {
-		args := fmt.Sprintf(" delete configmap --namespace=%s %s", r.Namespace, r.ConfigName)
-		_, err = util.KubectlExec(args)
+		v.Set("controls", fastjson.MustParse(string(configJson)))
+		fmt.Println(v.String())
+		return v.String(), nil
 	}
-	return
+
+	return "", err
 }
 
-func (cm *CM) PurgeConfigMap(m models.XappDescriptor) (c interface{}, err error) {
-	md := models.ConfigMetadata{Name: m.XappName, Namespace: m.Namespace, ConfigName: cm.GetConfigMapName(*m.XappName, m.Namespace)}
-
-	return cm.DeleteConfigMap(md)
-}
-
-func (cm *CM) RestoreConfigMap(m models.XappDescriptor, c interface{}) (err error) {
-	md := &models.ConfigMetadata{Name: m.XappName, Namespace: m.Namespace, ConfigName: cm.GetConfigMapName(*m.XappName, m.Namespace)}
-	time.Sleep(time.Duration(10 * time.Second))
-
-	return cm.ApplyConfigMap(models.XAppConfig{Metadata: md, Config: c}, "create")
-}
-
-func (cm *CM) GetNamesFromHelmRepo() (names []string) {
-	rname := viper.GetString("helm.repo-name")
-
-	cmdArgs := strings.Join([]string{"search ", rname}, "")
-	out, err := util.HelmExec(cmdArgs)
+func (cm *CM) ParseJson(dsContent string) (*fastjson.Value, error) {
+	var p fastjson.Parser
+	v, err := p.Parse(dsContent)
 	if err != nil {
-		return
+		appmgr.Logger.Info("fastjson.Parser failed: %v", err)
 	}
-
-	re := regexp.MustCompile(rname + `/.*`)
-	result := re.FindAllStringSubmatch(string(out), -1)
-	if result != nil {
-		var tmp string
-		for _, v := range result {
-			fmt.Sscanf(v[0], "%s", &tmp)
-			names = append(names, strings.Split(tmp, "/")[1])
-		}
-	}
-	return names
+	return v, err
 }
 
-func (cm *CM) Validate(req models.XAppConfig) (errList models.ConfigValidationErrors, err error) {
-	c := models.XAppConfig{}
-	err = cm.ReadSchema(*req.Metadata.Name, &c)
+
+func (cm *CM) GenerateJSONFile(jsonString string) error {
+	cmJson, err := json.RawMessage(jsonString).MarshalJSON()
 	if err != nil {
-		appmgr.Logger.Info("No schema file found for '%s', aborting ...", *req.Metadata.Name)
-		return
+		appmgr.Logger.Error("Config marshalling failed: %v", err)
+		return err
 	}
-	return cm.doValidate(c.Descriptor, req.Config)
-}
 
-func (cm *CM) doValidate(schema, cfg interface{}) (errList models.ConfigValidationErrors, err error) {
-	schemaLoader := gojsonschema.NewGoLoader(schema)
-	documentLoader := gojsonschema.NewGoLoader(cfg)
-
-	result, err := gojsonschema.Validate(schemaLoader, documentLoader)
+	err = ioutil.WriteFile(viper.GetString("xapp.tmpConfig"), cmJson, 0644)
 	if err != nil {
-		appmgr.Logger.Info("Validation failed: %v", err)
-		return
+		appmgr.Logger.Error("WriteFile failed: %v", err)
+		return err
 	}
 
-	if result.Valid() == false {
-		appmgr.Logger.Info("The document is not valid, Errors: %v", result.Errors())
-		for _, desc := range result.Errors() {
-			field := desc.Field()
-			validationError := desc.Description()
-			errList = append(errList, &models.ConfigValidationError{Field: &field, Error: &validationError})
-		}
-		return errList, errors.New("Validation failed!")
-	}
-	return
+	return nil
 }
 
 func (cm *CM) ReadFile(name string, data interface{}) (err error) {
@@ -254,6 +186,19 @@
 	return
 }
 
+func (cm *CM) ReadConfigmap(name string, ns string) (string, error) {
+	args := fmt.Sprintf("get configmap -o jsonpath='{.data.config-file\\.json}' -n %s %s", ns, cm.GetConfigMapName(name, ns))
+	out, err := util.KubectlExec(args)
+	return string(out), err
+}
+
+func (cm *CM) ReplaceConfigMap(name, ns string) (error) {
+	cmd := " create configmap -n %s %s --from-file=%s -o json --dry-run | kubectl replace -f -"
+	args := fmt.Sprintf(cmd, ns, cm.GetConfigMapName(name, ns), viper.GetString("xapp.tmpConfig"))
+	_, err := util.KubectlExec(args)
+	return err
+}
+
 func (cm *CM) FetchChart(name string) (err error) {
 	tarDir := viper.GetString("xapp.tarDir")
 	repo := viper.GetString("helm.repo-name")
@@ -283,9 +228,11 @@
 	for _, m := range v.GetArray("rmr", "txMessages") {
 		msgs.TxMessages = append(msgs.TxMessages, strings.Trim(m.String(), `"`))
 	}
+
 	for _, m := range v.GetArray("rmr", "rxMessages") {
 		msgs.RxMessages = append(msgs.RxMessages, strings.Trim(m.String(), `"`))
 	}
+
 	for _, m := range v.GetArray("rmr", "policies") {
 		if val, err := strconv.Atoi(strings.Trim(m.String(), `"`)); err == nil {
 			msgs.Policies = append(msgs.Policies, int64(val))
@@ -310,3 +257,58 @@
 	}
 	return ns
 }
+
+func (cm *CM) GetNamesFromHelmRepo() (names []string) {
+	rname := viper.GetString("helm.repo-name")
+
+	cmdArgs := strings.Join([]string{"search ", rname}, "")
+	out, err := util.HelmExec(cmdArgs)
+	if err != nil {
+		return
+	}
+
+	re := regexp.MustCompile(rname + `/.*`)
+	result := re.FindAllStringSubmatch(string(out), -1)
+	if result != nil {
+		var tmp string
+		for _, v := range result {
+			fmt.Sscanf(v[0], "%s", &tmp)
+			names = append(names, strings.Split(tmp, "/")[1])
+		}
+	}
+	return names
+}
+
+func (cm *CM) Validate(req models.XAppConfig) (errList models.ConfigValidationErrors, err error) {
+	var desc interface{}
+	err = cm.ReadSchema(*req.Metadata.XappName, &desc)
+	if err != nil {
+		appmgr.Logger.Info("No schema file found for '%s', aborting ...", *req.Metadata.XappName)
+		return
+	}
+	return cm.doValidate(desc, req.Config)
+}
+
+func (cm *CM) doValidate(schema, cfg interface{}) (errList models.ConfigValidationErrors, err error) {
+	schemaLoader := gojsonschema.NewGoLoader(schema)
+	documentLoader := gojsonschema.NewGoLoader(cfg)
+
+	result, err := gojsonschema.Validate(schemaLoader, documentLoader)
+	if err != nil {
+		appmgr.Logger.Info("Validation failed: %v", err)
+		return
+	}
+
+	if result.Valid() == false {
+		appmgr.Logger.Info("The document is not valid, Errors: %v", result.Errors())
+		for _, desc := range result.Errors() {
+			field := desc.Field()
+			validationError := desc.Description()
+			errList = append(errList, &models.ConfigValidationError{Field: &field, Error: &validationError})
+		}
+		return errList, errors.New("Validation failed!")
+	}
+	appmgr.Logger.Info("Config validation successful!")
+
+	return
+}
\ No newline at end of file