First version of ODU slice assurance usecase
[nonrtric.git] / test / usecases / odusliceassurance / goversion / messages / stdVesMessage.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package messages
22
23 import (
24         "fmt"
25         "regexp"
26         "strconv"
27         "strings"
28
29         "oransc.org/usecase/oduclosedloop/internal/structures"
30 )
31
32 type StdDefinedMessage struct {
33         Event Event `json:"event"`
34 }
35
36 type Event struct {
37         CommonEventHeader CommonEventHeader `json:"commonEventHeader"`
38         StndDefinedFields StndDefinedFields `json:"stndDefinedFields"`
39 }
40
41 type CommonEventHeader struct {
42         Domain               string `json:"domain"`
43         StndDefinedNamespace string `json:"stndDefinedNamespace"`
44 }
45
46 type StndDefinedFields struct {
47         StndDefinedFieldsVersion string `json:"stndDefinedFieldsVersion"`
48         SchemaReference          string `json:"schemaReference"`
49         Data                     Data   `json:"data"`
50 }
51
52 type Data struct {
53         DataId       string        `json:"id"`
54         Measurements []Measurement `json:"measurements"`
55 }
56
57 type Measurement struct {
58         MeasurementTypeInstanceReference string `json:"measurement-type-instance-reference"`
59         Value                            int    `json:"value"`
60         Unit                             string `json:"unit"`
61 }
62
63 func (message StdDefinedMessage) GetMeasurements() []Measurement {
64         return message.Event.StndDefinedFields.Data.Measurements
65 }
66
67 func (meas Measurement) CreateSliceMetric() *structures.SliceMetric {
68         var pmName string
69         var duid, cellid string
70         var sd, sst int
71
72         typeParts := strings.Split(meas.MeasurementTypeInstanceReference, "/")
73         for _, part := range typeParts {
74                 if strings.Contains(part, "distributed-unit-functions") {
75                         duid = getValueInsideQuotes(part)
76
77                 } else if strings.Contains(part, "cell[") {
78                         cellid = getValueInsideQuotes(part)
79
80                 } else if strings.Contains(part, "performance-measurement-type") {
81                         pmName = getValueInsideQuotes(part)
82
83                 } else if strings.Contains(part, "slice-differentiator") {
84                         sd = getPropertyNumber(part)
85
86                 } else if strings.Contains(part, "slice-differentiator") {
87                         res, err := strconv.Atoi(getValueInsideQuotes(part))
88                         if err != nil {
89                                 sst = -1
90                         }
91                         sst = res
92                 }
93         }
94
95         sm := structures.NewSliceMetric(duid, cellid, sd, sst)
96         sm.PM[pmName] = meas.Value
97         return sm
98 }
99
100 func getValueInsideQuotes(text string) string {
101         re := regexp.MustCompile(`\'(.*?)\'`)
102
103         match := re.FindAllString(text, -1)
104         var res string
105         if len(match) == 1 {
106                 res = strings.Trim(match[0], "'")
107         }
108         return res
109 }
110
111 func getPropertyNumber(text string) int {
112         re := regexp.MustCompile("[0-9]+")
113         match := re.FindAllString(text, -1)
114         var res int
115         var err error
116         if len(match) == 1 {
117                 res, err = strconv.Atoi(match[0])
118                 if err != nil {
119                         fmt.Println(err)
120                         return -1
121                 }
122         }
123         return res
124 }