Measurement struct update

Change-Id: Iaac44be8df2047820f548accec92564605c233c6
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
diff --git a/cmd/vesmgr/config.go b/cmd/vesmgr/config.go
old mode 100644
new mode 100755
index fafb15b..e6e48d9
--- a/cmd/vesmgr/config.go
+++ b/cmd/vesmgr/config.go
@@ -98,6 +98,8 @@
 type AppMetricsStruct struct {
 	ObjectName     string
 	ObjectInstance string
+	MeasId         string
+	MeasObject     string
 }
 
 // AppMetrics contains metrics definitions for all Xapps
@@ -108,11 +110,16 @@
 //
 // { ...
 //   "config" : {
-//     "metrics": [
-//       { "name": "...", "objectName": "...", "objectInstamce": "..." },
+//	   "measurements": [
+//      {
+//     	  "metrics": [
+//          { "name": "...", "objectName": "...", "objectInstamce": "..." },
+//           ...
+//         ]
+//       }
 //       ...
 //     ]
-//   }
+//    }
 // }
 func parseMetricsFromXAppDescriptor(descriptor []byte, appMetrics AppMetrics) AppMetrics {
 	var desc []map[string]interface{}
@@ -120,11 +127,25 @@
 
 	for _, app := range desc {
 		config, configOk := app["config"]
-		if configOk {
-			metrics, metricsOk := config.(map[string]interface{})["metrics"]
-			if metricsOk {
-				parseMetricsRules(metrics.([]interface{}), appMetrics)
+		if !configOk {
+			logger.Info("No xApp config found!")
+			continue
+		}
+		measurements, measurementsOk := config.(map[string]interface{})["measurements"]
+		if !measurementsOk {
+			logger.Info("No xApp metrics found!")
+			continue
+		}
+
+		for _, m := range measurements.([]interface{}) {
+			measId, measIdOk := m.(map[string]interface{})["measId"].(string)
+			measObject, objectOk := m.(map[string]interface{})["object"].(string)
+			metrics, metricsOk := m.(map[string]interface{})["metrics"]
+			if !metricsOk || !measIdOk || !objectOk {
+				logger.Info("No metrics found for measId=%s Object=%s", measId, measObject)
+				continue
 			}
+			parseMetricsRules(metrics.([]interface{}), appMetrics, measId, measObject)
 		}
 	}
 	return appMetrics
@@ -134,7 +155,7 @@
 // of the following format:
 //    { "name": xxx, "objectName": yyy, "objectInstance": zzz }
 // Entries, which do not have all the necessary fields, are ignored.
-func parseMetricsRules(metricsMap []interface{}, appMetrics AppMetrics) AppMetrics {
+func parseMetricsRules(metricsMap []interface{}, appMetrics AppMetrics, measId, measObject string) AppMetrics {
 	for _, element := range metricsMap {
 		name, nameOk := element.(map[string]interface{})["name"].(string)
 		if nameOk {
@@ -142,7 +163,7 @@
 			objectName, objectNameOk := element.(map[string]interface{})["objectName"].(string)
 			objectInstance, objectInstanceOk := element.(map[string]interface{})["objectInstance"].(string)
 			if !alreadyFound && objectNameOk && objectInstanceOk {
-				appMetrics[name] = AppMetricsStruct{objectName, objectInstance}
+				appMetrics[name] = AppMetricsStruct{objectName, objectInstance, measId, measObject}
 				logger.Info("parsed counter %s %s %s", name, objectName, objectInstance)
 			}
 			if alreadyFound {
@@ -154,30 +175,34 @@
 }
 
 func getRules(vespaconf *VESAgentConfiguration, xAppConfig []byte) {
-	appMetrics := make(AppMetrics)
-	parseMetricsFromXAppDescriptor(xAppConfig, appMetrics)
-
-	makeRule := func(expr string, objName string, objInstance string) MetricRule {
+	makeRule := func(expr string, value AppMetricsStruct) MetricRule {
 		return MetricRule{
 			Target:         "AdditionalObjects",
 			Expr:           expr,
-			ObjectInstance: objInstance,
-			ObjectName:     objName,
+			ObjectInstance: value.ObjectInstance,
+			ObjectName:     value.ObjectName,
 			ObjectKeys: []Label{
 				Label{
 					Name: "ricComponentName",
 					Expr: "'{{.labels.kubernetes_name}}'",
 				},
+				Label{
+					Name: "measObject",
+					Expr: value.MeasObject,
+				},
+				Label{
+					Name: "measId",
+					Expr: value.MeasId,
+				},
 			},
 		}
 	}
-	var metricsMap map[string][]interface{}
-	json.Unmarshal(xAppConfig, &metricsMap)
-	metrics := parseMetricsRules(metricsMap["metrics"], appMetrics)
+	appMetrics := make(AppMetrics)
+	metrics := parseMetricsFromXAppDescriptor(xAppConfig, appMetrics)
 
 	vespaconf.Measurement.Prometheus.Rules.Metrics = make([]MetricRule, 0, len(metrics))
 	for key, value := range metrics {
-		vespaconf.Measurement.Prometheus.Rules.Metrics = append(vespaconf.Measurement.Prometheus.Rules.Metrics, makeRule(key, value.ObjectName, value.ObjectInstance))
+		vespaconf.Measurement.Prometheus.Rules.Metrics = append(vespaconf.Measurement.Prometheus.Rules.Metrics, makeRule(key, value))
 	}
 	if len(vespaconf.Measurement.Prometheus.Rules.Metrics) == 0 {
 		logger.Info("vespa config with empty metrics")
diff --git a/cmd/vesmgr/config_test.go b/cmd/vesmgr/config_test.go
old mode 100644
new mode 100755
index e04010f..ac1b7d6
--- a/cmd/vesmgr/config_test.go
+++ b/cmd/vesmgr/config_test.go
@@ -165,7 +165,7 @@
 			{ "name": "ricxapp_SDL_StoreError", "objectName": "ricxappSDLStoreErrorCounter", "objectInstance": "ricxappSDLStoreError" } ]}`
 	appMetrics := make(AppMetrics)
 	m := metricsStringToInterfaceArray(metricsJSON)
-	appMetrics = parseMetricsRules(m, appMetrics)
+	appMetrics = parseMetricsRules(m, appMetrics, "M1234", "App-1")
 	assert.Len(t, appMetrics, 6)
 	assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
 	assert.Equal(t, "ricxappRMRTransmitErrorCounter", appMetrics["ricxapp_RMR_TransmitError"].ObjectName)
@@ -176,7 +176,7 @@
 	appMetrics := make(AppMetrics)
 	metricsJSON := `{"metrics": []`
 	m := metricsStringToInterfaceArray(metricsJSON)
-	appMetrics = parseMetricsRules(m, appMetrics)
+	appMetrics = parseMetricsRules(m, appMetrics, "M1234", "App-1")
 	assert.Empty(t, appMetrics)
 }
 
@@ -185,7 +185,7 @@
 	metricsJSON := `{"metrics": [
 			{ "additionalField": "valueIgnored", "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
 	m := metricsStringToInterfaceArray(metricsJSON)
-	appMetrics = parseMetricsRules(m, appMetrics)
+	appMetrics = parseMetricsRules(m, appMetrics, "M1234", "App-1")
 	assert.Len(t, appMetrics, 1)
 	assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
 	assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
@@ -198,7 +198,7 @@
 			{ "name": "ricxapp_RMR_ReceiveError", "objectInstance": "ricxappRMRReceiveError" },
 			{ "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
 	m := metricsStringToInterfaceArray(metricsJSON)
-	appMetrics = parseMetricsRules(m, appMetrics)
+	appMetrics = parseMetricsRules(m, appMetrics, "M1234", "App-1")
 	assert.Len(t, appMetrics, 2)
 	assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
 	assert.Equal(t, "ricxappRMRTransmittedCounter", appMetrics["ricxapp_RMR_Transmitted"].ObjectName)
@@ -213,7 +213,7 @@
 			{ "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounterXXX", "objectInstance": "ricxappRMRReceivedXXX" },
 			{ "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
 	m := metricsStringToInterfaceArray(metricsJSON)
-	appMetrics = parseMetricsRules(m, appMetrics)
+	appMetrics = parseMetricsRules(m, appMetrics, "M1234", "App-1")
 	assert.Len(t, appMetrics, 2)
 	assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
 	assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
@@ -227,8 +227,8 @@
 			{ "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
 	m1 := metricsStringToInterfaceArray(metricsJSON1)
 	m2 := metricsStringToInterfaceArray(metricsJSON2)
-	appMetrics = parseMetricsRules(m1, appMetrics)
-	appMetrics = parseMetricsRules(m2, appMetrics)
+	appMetrics = parseMetricsRules(m1, appMetrics, "M1234", "App-1")
+	appMetrics = parseMetricsRules(m2, appMetrics,  "M1234", "App-1")
 	assert.Len(t, appMetrics, 2)
 	assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
 	assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
diff --git a/cmd/vesmgr/vesmgr.go b/cmd/vesmgr/vesmgr.go
index 0e775f8..e292307 100755
--- a/cmd/vesmgr/vesmgr.go
+++ b/cmd/vesmgr/vesmgr.go
@@ -116,7 +116,7 @@
 
 	var err error
 	if vesmgr.myIPAddress, err = getMyIP(); err != nil || vesmgr.myIPAddress == "" {
-		logger.Error("Cannot get myIPAddress: IP %s, err %s", vesmgr.myIPAddress, err.Error())
+		logger.Error("Cannot get myIPAddress: IP %s", vesmgr.myIPAddress)
 		panic("Cannot get my IP address")
 	}
 
diff --git a/container-tag.yaml b/container-tag.yaml
index 83cd94a..c28c94d 100644
--- a/container-tag.yaml
+++ b/container-tag.yaml
@@ -1,4 +1,4 @@
 # The Jenkins job uses this string for the tag in the image name
 # for example nexus3.o-ran-sc.org:10004/my-image-name:0.0.1
 ---
-tag: 0.4.0
+tag: 0.5.0
diff --git a/test/xApp_config_test_output.json b/test/xApp_config_test_output.json
old mode 100644
new mode 100755
index fccda3f..c74bac9
--- a/test/xApp_config_test_output.json
+++ b/test/xApp_config_test_output.json
@@ -124,22 +124,28 @@
             "logger": {
                 "level": 5
             },
-            "metrics": [
+            "measurements": [
                 {
-                    "description": "Example counter 1",
-                    "enabled": true,
-                    "name": "App1ExampleCounterOne",
-                    "type": "counter",
-                    "objectName": "App1ExampleCounterOneObject",
-                    "objectInstance": "App1ExampleCounterOneObjectInstance"
-                },
-                {
-                    "description": "Example counter 2",
-                    "enabled": true,
-                    "name": "App1ExampleCounterTwo",
-                    "type": "counter",
-                    "objectName": "App1ExampleCounterTwoObject",
-                    "objectInstance": "App1ExampleCounterTwoObjectInstance"
+                    "measId": "M9001",
+                    "object": "XAPP-xApp1",
+                    "metrics": [
+                        {
+                            "description": "Example counter 1",
+                            "enabled": true,
+                            "name": "App1ExampleCounterOne",
+                            "type": "counter",
+                            "objectName": "App1ExampleCounterOneObject",
+                            "objectInstance": "App1ExampleCounterOneObjectInstance"
+                        },
+                        {
+                            "description": "Example counter 2",
+                            "enabled": true,
+                            "name": "App1ExampleCounterTwo",
+                            "type": "counter",
+                            "objectName": "App1ExampleCounterTwoObject",
+                            "objectInstance": "App1ExampleCounterTwoObjectInstance"
+                        }
+                    ]
                 }
             ]
         }
@@ -269,22 +275,28 @@
             "logger": {
                 "level": 3
             },
-            "metrics": [
+            "measurements": [
                 {
-                    "description": "Example counter",
-                    "enabled": true,
-                    "name": "App2ExampleCounterOne",
-                    "type": "counter",
-                    "objectName": "App2ExampleCounterOneObject",
-                    "objectInstance": "App2ExampleCounterOneObjectInstance"
-                },
-                {
-                    "description": "Another example counter",
-                    "enabled": true,
-                    "name": "App2ExampleCounterTwo",
-                    "type": "counter",
-                    "objectName": "App2ExampleCounterTwoObject",
-                    "objectInstance": "App2ExampleCounterTwoObjectInstance"
+                    "measId": "M9001",
+                    "object": "XAPP-xApp2",
+                    "metrics": [
+                        {
+                            "description": "Example counter",
+                            "enabled": true,
+                            "name": "App2ExampleCounterOne",
+                            "type": "counter",
+                            "objectName": "App2ExampleCounterOneObject",
+                            "objectInstance": "App2ExampleCounterOneObjectInstance"
+                        },
+                        {
+                            "description": "Another example counter",
+                            "enabled": true,
+                            "name": "App2ExampleCounterTwo",
+                            "type": "counter",
+                            "objectName": "App2ExampleCounterTwoObject",
+                            "objectInstance": "App2ExampleCounterTwoObjectInstance"
+                        }
+                    ]
                 }
             ]
         }