2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
28 "oransc.org/usecase/oduclosedloop/messages"
37 type SliceAssuranceMeas struct {
38 Metrics map[MapKey]*SliceMetric
39 Policies map[string]*PolicyRatio
42 func NewSliceAssuranceMeas() *SliceAssuranceMeas {
43 s := SliceAssuranceMeas{}
44 s.Metrics = make(map[MapKey]*SliceMetric)
45 s.Policies = make(map[string]*PolicyRatio)
49 func (sa *SliceAssuranceMeas) AddNewPolicy(duid string, rrmPolicyRatio messages.RRMPolicyRatio) {
50 for _, policyMember := range rrmPolicyRatio.RRMPolicyMembers {
51 metric := sa.GetSliceMetric(duid, policyMember.SliceDifferentiator, policyMember.SliceServiceType)
53 pr := NewPolicyRatio(rrmPolicyRatio.Id, rrmPolicyRatio.RRMPolicyMaxRatio, rrmPolicyRatio.RRMPolicyMinRatio, rrmPolicyRatio.RRMPolicyDedicatedRatio)
54 sa.Policies[pr.PolicyRatioId] = pr
55 metric.RRMPolicyRatioId = rrmPolicyRatio.Id
60 func (sa *SliceAssuranceMeas) GetSliceMetric(duid string, sd int, sst int) *SliceMetric {
61 key := MapKey{duid, sd, sst}
62 value, check := sa.Metrics[key]
71 func (sa *SliceAssuranceMeas) AddOrUpdateMetric(meas messages.Measurement) (string, error) {
76 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+)\]`)
77 res := regex.FindAllStringSubmatch(meas.MeasurementTypeInstanceReference, -1)
79 if res != nil && len(res[0]) == 6 {
82 sst = toInt(res[0][5])
84 key := MapKey{duid, sd, sst}
85 value, check := sa.Metrics[key]
88 sa.updateMetric(key, value, res[0][3], meas.Value)
90 // Only add new one if value exceeds threshold
91 sa.addMetric(res, meas.Value)
94 return duid, fmt.Errorf(" wrong format for MeasurementTypeInstanceReference")
99 func (sa *SliceAssuranceMeas) addMetric(res [][]string, metricValue int) {
100 if metricValue > 700 {
101 metric := NewSliceMetric(res[0][1], res[0][2], toInt(res[0][4]), toInt(res[0][5]))
102 metric.PM[res[0][3]] = metricValue
103 key := MapKey{res[0][1], toInt(res[0][4]), toInt(res[0][5])}
104 sa.Metrics[key] = metric
108 func (sa *SliceAssuranceMeas) updateMetric(key MapKey, value *SliceMetric, metricName string, metricValue int) {
109 if metricValue < 700 {
110 delete(sa.Metrics, key)
112 value.PM[metricName] = metricValue
116 func toInt(num string) int {
117 res, err := strconv.Atoi(num)
124 func (sa *SliceAssuranceMeas) PrintStructures() {
125 fmt.Printf("SliceAssurance Metrics: \n")
126 for key, metric := range sa.Metrics {
127 fmt.Printf("Key: %+v\n", key)
128 fmt.Printf("Metric: %+v\n", metric)
130 fmt.Printf("SliceAssurance Policies: \n")
131 for key, metric := range sa.Policies {
132 fmt.Printf("Key: %+v\n", key)
133 fmt.Printf("Metric: %+v\n", metric)