Fixing bug in O-DU slice assurance app

Issue-ID: NONRTRIC-637
Signed-off-by: ychacon <yennifer.chacon@est.tech>
Change-Id: I59bbb44679f1bc97351cc43e5b80ffa8d58ab810
diff --git a/test/usecases/odusliceassurance/goversion/internal/sliceassurance/app.go b/test/usecases/odusliceassurance/goversion/internal/sliceassurance/app.go
index 2cef277..4266508 100644
--- a/test/usecases/odusliceassurance/goversion/internal/sliceassurance/app.go
+++ b/test/usecases/odusliceassurance/goversion/internal/sliceassurance/app.go
@@ -69,9 +69,8 @@
 
 func (a *App) getMessagesFromDmaap(url string) {
 	var stdMessage messages.StdDefinedMessage
-
 	a.client.Get(url, &stdMessage)
-	log.Infof("Polling new messages from DmaapMR: %v", stdMessage)
+	log.Infof("Polling new messages from DmaapMR")
 	for _, meas := range stdMessage.GetMeasurements() {
 		//Create sliceMetric and check if metric exist and update existing one or create new one
 		if _, err := a.metricsPolicies.AddOrUpdateMetric(meas); err != nil {
diff --git a/test/usecases/odusliceassurance/goversion/internal/structures/sliceassurance.go b/test/usecases/odusliceassurance/goversion/internal/structures/sliceassurance.go
index 7cefcaf..1f9c860 100644
--- a/test/usecases/odusliceassurance/goversion/internal/structures/sliceassurance.go
+++ b/test/usecases/odusliceassurance/goversion/internal/structures/sliceassurance.go
@@ -25,6 +25,7 @@
 	"regexp"
 	"strconv"
 
+	log "github.com/sirupsen/logrus"
 	"oransc.org/usecase/oduclosedloop/messages"
 )
 
@@ -51,8 +52,13 @@
 		metric := sa.GetSliceMetric(duid, policyMember.SliceDifferentiator, policyMember.SliceServiceType)
 		if metric != nil {
 			pr := NewPolicyRatio(rrmPolicyRatio.Id, rrmPolicyRatio.RRMPolicyMaxRatio, rrmPolicyRatio.RRMPolicyMinRatio, rrmPolicyRatio.RRMPolicyDedicatedRatio)
+			_, check := sa.Policies[pr.PolicyRatioId]
+			if !check {
+				log.Infof(" new policy has been added %+v", *pr)
+			}
 			sa.Policies[pr.PolicyRatioId] = pr
 			metric.RRMPolicyRatioId = rrmPolicyRatio.Id
+
 		}
 	}
 }
@@ -73,7 +79,7 @@
 	var duid string
 	var sd, sst int
 
-	regex := *regexp.MustCompile(`\/(.*)network-function\/distributed-unit-functions\[id=\'(.*)\'\]\/cell\[id=\'(.*)\'\]\/supported-measurements\[performance-measurement-type=\'(.*)\'\]\/supported-snssai-subcounter-instances\[slice-differentiator=\'(\d+)\'\]\[slice-service-type=\'(\d+)\'\]`)
+	regex := *regexp.MustCompile(`\/(.*)network-function\/distributed-unit-functions\[id=\'(.*)\'\]\/cell\[id=\'(.*)\'\]\/supported-measurements\/performance-measurement-type=\'(.*)\'\]\/supported-snssai-subcounter-instances\[slice-differentiator=\'(\d+)\'\]\[slice-service-type=\'(\d+)\'\]`)
 	res := regex.FindAllStringSubmatch(meas.MeasurementTypeInstanceReference, -1)
 
 	if res != nil && len(res[0]) == 7 {
@@ -102,14 +108,17 @@
 		metric.PM[res[0][3]] = metricValue
 		key := MapKey{res[0][2], toInt(res[0][5]), toInt(res[0][6])}
 		sa.Metrics[key] = metric
+		log.Infof(" new metric has been added %+v", *metric)
 	}
 }
 
 func (sa *SliceAssuranceMeas) updateMetric(key MapKey, value *SliceMetric, metricName string, metricValue int) {
 	if metricValue < 700 {
 		delete(sa.Metrics, key)
+		log.Infof(" metric with key %+v has been deleted", key)
 	} else {
 		value.PM[metricName] = metricValue
+		log.Infof(" metric value has been updated, new value: %v", metricValue)
 	}
 }
 
diff --git a/test/usecases/odusliceassurance/goversion/internal/structures/sliceassurance_test.go b/test/usecases/odusliceassurance/goversion/internal/structures/sliceassurance_test.go
index 9ed35fb..792f475 100644
--- a/test/usecases/odusliceassurance/goversion/internal/structures/sliceassurance_test.go
+++ b/test/usecases/odusliceassurance/goversion/internal/structures/sliceassurance_test.go
@@ -41,7 +41,7 @@
 			name: "Test adding new metric",
 			args: args{
 				meas: messages.Measurement{
-					MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements[performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
+					MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements/performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
 					Value:                            51232,
 					Unit:                             "kbit/s",
 				},
@@ -51,7 +51,7 @@
 			name: "Test with invalid input",
 			args: args{
 				meas: messages.Measurement{
-					MeasurementTypeInstanceReference: "/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements[performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
+					MeasurementTypeInstanceReference: "/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements/performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
 					Value:                            51232,
 					Unit:                             "kbit/s",
 				},
@@ -83,13 +83,13 @@
 func TestUpdateExistingMetric(t *testing.T) {
 	assertions := require.New(t)
 	meas := messages.Measurement{
-		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements[performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
+		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements/performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
 		Value:                            51232,
 		Unit:                             "kbit/s",
 	}
 
 	updateMeas := messages.Measurement{
-		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements[performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
+		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements/performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
 		Value:                            897,
 		Unit:                             "kbit/s",
 	}
@@ -115,13 +115,13 @@
 func TestDeleteMetricWhenValueLessThanThreshold(t *testing.T) {
 
 	meas := messages.Measurement{
-		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements[performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
+		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements/performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
 		Value:                            51232,
 		Unit:                             "kbit/s",
 	}
 
 	newMeas := messages.Measurement{
-		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements[performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
+		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements/performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
 		Value:                            50,
 		Unit:                             "kbit/s",
 	}
@@ -140,7 +140,7 @@
 func TestAddPolicy(t *testing.T) {
 
 	meas := messages.Measurement{
-		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements[performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
+		MeasurementTypeInstanceReference: "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='O-DU-1211']/cell[id='cell-1']/supported-measurements/performance-measurement-type='user-equipment-average-throughput-uplink']/supported-snssai-subcounter-instances[slice-differentiator='1'][slice-service-type='1']",
 		Value:                            51232,
 		Unit:                             "kbit/s",
 	}
diff --git a/test/usecases/odusliceassurance/goversion/main.go b/test/usecases/odusliceassurance/goversion/main.go
index f1c8d42..4497cfb 100644
--- a/test/usecases/odusliceassurance/goversion/main.go
+++ b/test/usecases/odusliceassurance/goversion/main.go
@@ -29,7 +29,7 @@
 	"oransc.org/usecase/oduclosedloop/internal/sliceassurance"
 )
 
-const TOPIC string = "unauthenticated.VES_O_RAN_SC_HELLO_WORLD_PM_STREAMING_OUTPUT"
+const TOPIC string = "/events/unauthenticated.VES_O_RAN_SC_HELLO_WORLD_PM_STREAMING_OUTPUT/myG/C1"
 
 var configuration *config.Config
 
diff --git a/test/usecases/odusliceassurance/goversion/messages/stdVesMessage.go b/test/usecases/odusliceassurance/goversion/messages/stdVesMessage.go
index c2c5849..5c4a3b0 100644
--- a/test/usecases/odusliceassurance/goversion/messages/stdVesMessage.go
+++ b/test/usecases/odusliceassurance/goversion/messages/stdVesMessage.go
@@ -30,8 +30,24 @@
 }
 
 type CommonEventHeader struct {
-	Domain               string `json:"domain"`
-	StndDefinedNamespace string `json:"stndDefinedNamespace"`
+	Domain                  string `json:"domain"`
+	EventId                 string `json:"eventId"`
+	EventName               string `json:"eventName"`
+	EventType               string `json:"eventType"`
+	Sequence                int    `json:"sequence"`
+	Priority                string `json:"priority"`
+	ReportingEntityId       string `json:"reportingEntityId"`
+	ReportingEntityName     string `json:"reportingEntityName"`
+	SourceId                string `json:"sourceId"`
+	SourceName              string `json:"sourceName"`
+	StartEpochMicrosec      int64  `json:"startEpochMicrosec"`
+	LastEpochMicrosec       int64  `json:"lastEpochMicrosec"`
+	NfNamingCode            string `json:"nfNamingCode"`
+	NfVendorName            string `json:"nfVendorName"`
+	StndDefinedNamespace    string `json:"stndDefinedNamespace"`
+	TimeZoneOffset          string `json:"timeZoneOffset"`
+	Version                 string `json:"version"`
+	VesEventListenerVersion string `json:"vesEventListenerVersion"`
 }
 
 type StndDefinedFields struct {
@@ -41,8 +57,14 @@
 }
 
 type Data struct {
-	DataId       string        `json:"id"`
-	Measurements []Measurement `json:"measurements"`
+	DataId              string        `json:"id"`
+	StartTime           string        `json:"start-time"`
+	AdministrativeState string        `json:"administrative-state"`
+	OperationalState    string        `json:"operational-state"`
+	UserLabel           string        `json:"user-label"`
+	JobTag              string        `json:"job-tag"`
+	GranularityPeriod   int           `json:"granularity-period"`
+	Measurements        []Measurement `json:"measurements"`
 }
 
 type Measurement struct {
diff --git a/test/usecases/odusliceassurance/goversion/stub/simulator.go b/test/usecases/odusliceassurance/goversion/stub/simulator.go
index dcf450c..c8c747a 100644
--- a/test/usecases/odusliceassurance/goversion/stub/simulator.go
+++ b/test/usecases/odusliceassurance/goversion/stub/simulator.go
@@ -118,7 +118,6 @@
 		r := mux.NewRouter()
 		r.HandleFunc("/rests/data/network-topology:network-topology/topology=topology-netconf/node={NODE-ID}/yang-ext:mount/o-ran-sc-du-hello-world:network-function/distributed-unit-functions={O-DU-ID}", getSdnrResponseMessage).Methods(http.MethodGet)
 		r.HandleFunc("/rests/data/network-topology:network-topology/topology=topology-netconf/node={NODE-ID}/yang-ext:mount/o-ran-sc-du-hello-world:network-function/distributed-unit-functions={O-DU-ID}/radio-resource-management-policy-ratio={POLICY-ID}", updateRRMPolicyDedicatedRatio).Methods(http.MethodPost)
-		r.HandleFunc("/events/unauthenticated.PERFORMANCE_MEASUREMENTS", sendDmaapMRMessages).Methods(http.MethodGet)
 
 		fmt.Println("Starting SDNR stub on port: ", *portSdnr)
 
@@ -129,7 +128,7 @@
 	go func() {
 
 		r := mux.NewRouter()
-		r.HandleFunc("/events/unauthenticated.PERFORMANCE_MEASUREMENTS", sendDmaapMRMessages).Methods(http.MethodGet)
+		r.HandleFunc("/events/unauthenticated.VES_O_RAN_SC_HELLO_WORLD_PM_STREAMING_OUTPUT/myG/C1", sendDmaapMRMessages).Methods(http.MethodGet)
 
 		fmt.Println("Starting DmaapMR stub on port: ", *portDmaapMR)
 
@@ -181,7 +180,6 @@
 }
 
 func updateRRMPolicyDedicatedRatio(w http.ResponseWriter, r *http.Request) {
-	log.Info("Post request to update RRMPolicyDedicatedRatio")
 
 	var prMessages []messages.RRMPolicyRatio
 	decoder := json.NewDecoder(r.Body)
@@ -192,6 +190,8 @@
 	}
 	defer r.Body.Close()
 
+	log.Infof("Post request to update RRMPolicyDedicatedRatio %+v", prMessages)
+
 	findAndUpdatePolicy(prMessages)
 	respondWithJSON(w, http.StatusOK, map[string]string{"status": "200"})
 }
@@ -200,8 +200,8 @@
 	for _, policy := range rRMPolicyRatio {
 		for _, entry := range data {
 			if entry.policyRatioId == policy.Id {
+				log.Infof("update Policy Dedicated Ratio: value for policy %+v\n Old value: %v New value: %v ", policy, entry.policyDedicatedRatio, policy.RRMPolicyDedicatedRatio)
 				entry.policyDedicatedRatio = policy.RRMPolicyDedicatedRatio
-				log.Info("New value for Policy dedicated ratio: ", entry.policyDedicatedRatio)
 				if entry.metricValue > THRESHOLD_TPUT {
 					entry.metricValue = rand.Intn(THRESHOLD_TPUT)
 				}
@@ -239,20 +239,44 @@
 	message := messages.StdDefinedMessage{
 		Event: messages.Event{
 			CommonEventHeader: messages.CommonEventHeader{
-				Domain:               "stndDefined",
-				StndDefinedNamespace: "o-ran-sc-du-hello-world-pm-streaming-oas3",
+				Domain:                  "stndDefined",
+				EventId:                 "pm-1_1644252450",
+				EventName:               "stndDefined_performanceMeasurementStreaming",
+				EventType:               "performanceMeasurementStreaming",
+				Sequence:                825,
+				Priority:                "Low",
+				ReportingEntityId:       "",
+				ReportingEntityName:     "O-DU-1122",
+				SourceId:                "",
+				SourceName:              "O-DU-1122",
+				StartEpochMicrosec:      1644252450000000,
+				LastEpochMicrosec:       1644252480000000,
+				NfNamingCode:            "SIM-O-DU",
+				NfVendorName:            "O-RAN-SC SIM Project",
+				StndDefinedNamespace:    "o-ran-sc-du-hello-world-pm-streaming-oas3",
+				TimeZoneOffset:          "+00:00",
+				Version:                 "4.1",
+				VesEventListenerVersion: "7.2.1",
 			},
 			StndDefinedFields: messages.StndDefinedFields{
 				StndDefinedFieldsVersion: "1.0",
 				SchemaReference:          "https://gerrit.o-ran-sc.org/r/gitweb?p=scp/oam/modeling.git;a=blob_plain;f=data-model/oas3/experimental/o-ran-sc-du-hello-world-oas3.json;hb=refs/heads/master",
 				Data: messages.Data{
-					DataId:       "id",
-					Measurements: messagesToSend,
+					DataId:              "pm-1_1644252450",
+					StartTime:           "2022-02-07T16:47:30.0Z",
+					AdministrativeState: "unlocked",
+					OperationalState:    "enabled",
+					UserLabel:           "pm",
+					JobTag:              "my-job-tag",
+					GranularityPeriod:   30,
+					Measurements:        messagesToSend,
 				},
 			},
 		},
 	}
 
+	fmt.Printf("Send Dmaap messages\n %+v\n", message)
+
 	time.Sleep(time.Duration(rand.Intn(3)) * time.Second)
 	respondWithJSON(w, http.StatusOK, message)
 
@@ -261,7 +285,7 @@
 
 func generateMeasurementEntry(entry *SliceAssuranceInformation) messages.Measurement {
 
-	measurementTypeInstanceReference := "/network-function/distributed-unit-functions[id='" + entry.duId + "']/cell[id='" + entry.cellId + "']/supported-measurements/performance-measurement-type[.='" + entry.metricName + "']/supported-snssai-subcounter-instances/slice-differentiator[.=" + strconv.Itoa(entry.sd) + "][slice-service-type=" + strconv.Itoa(entry.sst) + "]"
+	measurementTypeInstanceReference := "/o-ran-sc-du-hello-world:network-function/distributed-unit-functions[id='" + entry.duId + "']/cell[id='" + entry.cellId + "']/supported-measurements/performance-measurement-type='(urn:o-ran-sc:yang:o-ran-sc-du-hello-world?revision=2021-11-23)" + entry.metricName + "']/supported-snssai-subcounter-instances[slice-differentiator='" + strconv.Itoa(entry.sd) + "'][slice-service-type='" + strconv.Itoa(entry.sst) + "']"
 	meas := messages.Measurement{
 
 		MeasurementTypeInstanceReference: measurementTypeInstanceReference,