2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: 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 log "github.com/sirupsen/logrus"
29 "oransc.org/usecase/oduclosedloop/icsversion/messages"
38 type SliceAssuranceMeas struct {
39 Metrics map[MapKey]*SliceMetric
40 Policies map[string]*PolicyRatio
43 func NewSliceAssuranceMeas() *SliceAssuranceMeas {
44 s := SliceAssuranceMeas{}
45 s.Metrics = make(map[MapKey]*SliceMetric)
46 s.Policies = make(map[string]*PolicyRatio)
50 func (sa *SliceAssuranceMeas) AddNewPolicy(duid string, rrmPolicyRatio messages.RRMPolicyRatio) {
51 for _, policyMember := range rrmPolicyRatio.RRMPolicyMembers {
52 metric := sa.GetSliceMetric(duid, policyMember.SliceDifferentiator, policyMember.SliceServiceType)
54 pr := NewPolicyRatio(rrmPolicyRatio.Id, rrmPolicyRatio.RRMPolicyMaxRatio, rrmPolicyRatio.RRMPolicyMinRatio, rrmPolicyRatio.RRMPolicyDedicatedRatio)
55 _, check := sa.Policies[pr.PolicyRatioId]
57 log.Infof(" new policy has been added %+v", *pr)
59 sa.Policies[pr.PolicyRatioId] = pr
60 metric.RRMPolicyRatioId = rrmPolicyRatio.Id
66 func (sa *SliceAssuranceMeas) GetSliceMetric(duid string, sd int, sst int) *SliceMetric {
67 key := MapKey{duid, sd, sst}
68 value, check := sa.Metrics[key]
77 func (sa *SliceAssuranceMeas) AddOrUpdateMetric(meas messages.Measurement) (string, error) {
82 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+)\'\]`)
83 res := regex.FindAllStringSubmatch(meas.MeasurementTypeInstanceReference, -1)
85 if res != nil && len(res[0]) == 7 {
88 sst = toInt(res[0][6])
90 key := MapKey{duid, sd, sst}
91 value, check := sa.Metrics[key]
94 sa.updateMetric(key, value, res[0][4], meas.Value)
96 // Only add new one if value exceeds threshold
97 sa.addMetric(res, meas.Value)
100 return duid, fmt.Errorf(" wrong format for MeasurementTypeInstanceReference")
105 func (sa *SliceAssuranceMeas) addMetric(res [][]string, metricValue int) {
106 if metricValue > 700 {
107 metric := NewSliceMetric(res[0][2], res[0][3], toInt(res[0][5]), toInt(res[0][6]))
108 metric.PM[res[0][3]] = metricValue
109 key := MapKey{res[0][2], toInt(res[0][5]), toInt(res[0][6])}
110 sa.Metrics[key] = metric
111 log.Infof(" new metric has been added %+v", *metric)
115 func (sa *SliceAssuranceMeas) updateMetric(key MapKey, value *SliceMetric, metricName string, metricValue int) {
116 if metricValue < 700 {
117 delete(sa.Metrics, key)
118 log.Infof(" metric with key %+v has been deleted", key)
120 value.PM[metricName] = metricValue
121 log.Infof(" metric value has been updated, new value: %v", metricValue)
125 func toInt(num string) int {
126 res, err := strconv.Atoi(num)