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===================================
21 package sliceassurance
29 "oransc.org/usecase/oduclosedloop/internal/restclient"
30 "oransc.org/usecase/oduclosedloop/internal/structures"
31 "oransc.org/usecase/oduclosedloop/messages"
36 DEFAULT_DEDICATED_RATIO = 40
37 NEW_DEDICATED_RATIO = 50
42 Client restclient.HTTPClient
43 MetricsPolicies *structures.SliceAssuranceMeas
49 func (a *App) Initialize(dmaapUrl string, sdnrUrl string) {
53 a.Client = restclient.New(&http.Client{})
54 a.MetricsPolicies = structures.NewSliceAssuranceMeas()
57 func (a *App) Run(topic string, pollTime int) {
59 fmt.Printf("Polling new messages from DmaapMR\n")
60 var stdMessage messages.StdDefinedMessage
62 a.Client.Get(dmaapMRUrl+topic, &stdMessage)
64 a.processMessages(stdMessage)
66 exceedsThMetrics := a.checkIfThresholdIsExceed()
67 if len(exceedsThMetrics) > 0 {
68 a.updateDedicatedRatio(exceedsThMetrics)
71 time.Sleep(time.Second * time.Duration(pollTime))
75 func (a *App) processMessages(stdMessage messages.StdDefinedMessage) {
77 for _, meas := range stdMessage.GetMeasurements() {
79 fmt.Printf("New measurement: %+v\n", meas)
80 //Create sliceMetric and check if metric exist and update existing one or create new one
81 tmpSm := meas.CreateSliceMetric()
82 a.MetricsPolicies.AddOrUpdateMetric(tmpSm)
84 //Fetch policy ratio metrics from SDNR
85 var duRRMPolicyRatio messages.ORanDuRestConf
86 a.Client.Get(getUrlForDistributedUnitFunctions(SDNRUrl, tmpSm.DUId), &duRRMPolicyRatio)
88 //Get DuId and check if we have metrics for it
89 policyRatioDuId := duRRMPolicyRatio.DistributedUnitFunction.Id
90 policies := duRRMPolicyRatio.DistributedUnitFunction.RRMPolicyRatio
91 for _, policy := range policies {
93 for _, member := range policy.RRMPolicyMembers {
94 metric := a.MetricsPolicies.GetSliceMetric(policyRatioDuId, member.SliceDifferentiator, member.SliceServiceType)
96 a.MetricsPolicies.AddNewPolicy(addOrUpdatePolicyRatio(metric, policy))
104 func (a *App) checkIfThresholdIsExceed() []*structures.SliceMetric {
105 exceedsThMetrics := make([]*structures.SliceMetric, 0)
106 for _, metric := range a.MetricsPolicies.Metrics {
107 for key, value := range metric.PM {
109 if (value) > THRESHOLD_TPUT {
110 fmt.Printf("PM: [%v, %v] exceeds threshold value!\n", key, value)
111 exceedsThMetrics = append(exceedsThMetrics, metric)
115 return exceedsThMetrics
118 func (a *App) updateDedicatedRatio(exceedsThMetrics []*structures.SliceMetric) {
119 for _, m := range exceedsThMetrics {
120 //Check if RRMPolicyDedicatedRatio is higher than default value
121 policy := a.MetricsPolicies.Policies[m.RRMPolicyRatioId]
123 if policy != nil && policy.PolicyDedicatedRatio <= DEFAULT_DEDICATED_RATIO {
124 //Send PostRequest to update DedicatedRatio
125 url := getUrlUpdatePolicyDedicatedRatio(SDNRUrl, m.DUId, policy.PolicyRatioId)
126 a.Client.Post(url, messages.GetDedicatedRatioUpdateMessage(*m, *policy, NEW_DEDICATED_RATIO), nil)
131 func addOrUpdatePolicyRatio(metric *structures.SliceMetric, policy messages.RRMPolicyRatio) *structures.PolicyRatio {
132 if metric.RRMPolicyRatioId == "" {
133 metric.RRMPolicyRatioId = policy.Id
135 return &structures.PolicyRatio{
136 PolicyRatioId: policy.Id,
137 PolicyMaxRatio: policy.RRMPolicyMaxRatio,
138 PolicyMinRatio: policy.RRMPolicyMinRatio,
139 PolicyDedicatedRatio: toInt(policy.RRMPolicyDedicatedRatio),
143 func toInt(num string) int {
144 res, err := strconv.Atoi(num)
151 func getUrlForDistributedUnitFunctions(host string, duid string) string {
152 return host + "/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=" + duid
155 func getUrlUpdatePolicyDedicatedRatio(host string, duid string, policyid string) string {
156 return host + "/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=" + duid + "/radio-resource-management-policy-ratio=" + policyid