Merge "First version of ODU slice assurance usecase"
[nonrtric.git] / test / usecases / odusliceassurance / goversion / internal / sliceassurance / app.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 sliceassurance
22
23 import (
24         "fmt"
25         "net/http"
26         "strconv"
27         "time"
28
29         "oransc.org/usecase/oduclosedloop/internal/restclient"
30         "oransc.org/usecase/oduclosedloop/internal/structures"
31         "oransc.org/usecase/oduclosedloop/messages"
32 )
33
34 const (
35         THRESHOLD_TPUT          = 700
36         DEFAULT_DEDICATED_RATIO = 40
37         NEW_DEDICATED_RATIO     = 50
38         NODE_ID                 = "O-DU-1211"
39 )
40
41 type App struct {
42         Client          restclient.HTTPClient
43         MetricsPolicies *structures.SliceAssuranceMeas
44 }
45
46 var dmaapMRUrl string
47 var SDNRUrl string
48
49 func (a *App) Initialize(dmaapUrl string, sdnrUrl string) {
50         dmaapMRUrl = dmaapUrl
51         SDNRUrl = sdnrUrl
52
53         a.Client = restclient.New(&http.Client{})
54         a.MetricsPolicies = structures.NewSliceAssuranceMeas()
55 }
56
57 func (a *App) Run(topic string, pollTime int) {
58         for {
59                 fmt.Printf("Polling new messages from DmaapMR\n")
60                 var stdMessage messages.StdDefinedMessage
61
62                 a.Client.Get(dmaapMRUrl+topic, &stdMessage)
63
64                 a.processMessages(stdMessage)
65
66                 exceedsThMetrics := a.checkIfThresholdIsExceed()
67                 if len(exceedsThMetrics) > 0 {
68                         a.updateDedicatedRatio(exceedsThMetrics)
69                 }
70
71                 time.Sleep(time.Second * time.Duration(pollTime))
72         }
73 }
74
75 func (a *App) processMessages(stdMessage messages.StdDefinedMessage) {
76
77         for _, meas := range stdMessage.GetMeasurements() {
78
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)
83
84                 //Fetch policy ratio metrics from SDNR
85                 var duRRMPolicyRatio messages.ORanDuRestConf
86                 a.Client.Get(getUrlForDistributedUnitFunctions(SDNRUrl, tmpSm.DUId), &duRRMPolicyRatio)
87
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 {
92                 members:
93                         for _, member := range policy.RRMPolicyMembers {
94                                 metric := a.MetricsPolicies.GetSliceMetric(policyRatioDuId, member.SliceDifferentiator, member.SliceServiceType)
95                                 if metric != nil {
96                                         a.MetricsPolicies.AddNewPolicy(addOrUpdatePolicyRatio(metric, policy))
97                                         break members
98                                 }
99                         }
100                 }
101         }
102 }
103
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 {
108
109                         if (value) > THRESHOLD_TPUT {
110                                 fmt.Printf("PM: [%v, %v] exceeds threshold value!\n", key, value)
111                                 exceedsThMetrics = append(exceedsThMetrics, metric)
112                         }
113                 }
114         }
115         return exceedsThMetrics
116 }
117
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]
122
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)
127                 }
128         }
129 }
130
131 func addOrUpdatePolicyRatio(metric *structures.SliceMetric, policy messages.RRMPolicyRatio) *structures.PolicyRatio {
132         if metric.RRMPolicyRatioId == "" {
133                 metric.RRMPolicyRatioId = policy.Id
134         }
135         return &structures.PolicyRatio{
136                 PolicyRatioId:        policy.Id,
137                 PolicyMaxRatio:       policy.RRMPolicyMaxRatio,
138                 PolicyMinRatio:       policy.RRMPolicyMinRatio,
139                 PolicyDedicatedRatio: toInt(policy.RRMPolicyDedicatedRatio),
140         }
141 }
142
143 func toInt(num string) int {
144         res, err := strconv.Atoi(num)
145         if err != nil {
146                 return -1
147         }
148         return res
149 }
150
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
153 }
154
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
157 }