2cef27786a0caf2fb240d3bd5eba1ce871484ac3
[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         "net/http"
25         "time"
26
27         "oransc.org/usecase/oduclosedloop/internal/restclient"
28         "oransc.org/usecase/oduclosedloop/internal/structures"
29         "oransc.org/usecase/oduclosedloop/messages"
30
31         log "github.com/sirupsen/logrus"
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.Client
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                 a.getMessagesFromDmaap(dmaapMRUrl + topic)
60
61                 for key := range a.metricsPolicies.Metrics {
62                         a.getRRMInformation(key.Duid)
63                 }
64                 a.updateDedicatedRatio()
65
66                 time.Sleep(time.Second * time.Duration(pollTime))
67         }
68 }
69
70 func (a *App) getMessagesFromDmaap(url string) {
71         var stdMessage messages.StdDefinedMessage
72
73         a.client.Get(url, &stdMessage)
74         log.Infof("Polling new messages from DmaapMR: %v", stdMessage)
75         for _, meas := range stdMessage.GetMeasurements() {
76                 //Create sliceMetric and check if metric exist and update existing one or create new one
77                 if _, err := a.metricsPolicies.AddOrUpdateMetric(meas); err != nil {
78                         log.Error("Metric could not be added ", err)
79                 }
80         }
81 }
82
83 func (a *App) getRRMInformation(duid string) {
84         var duRRMPolicyRatio messages.ORanDuRestConf
85         a.client.Get(getUrlForDistributedUnitFunctions(sDNRUrl, duid), &duRRMPolicyRatio)
86
87         policies := duRRMPolicyRatio.DistributedUnitFunction.RRMPolicyRatio
88         for _, policy := range policies {
89                 a.metricsPolicies.AddNewPolicy(duid, policy)
90         }
91 }
92
93 func (a *App) updateDedicatedRatio() {
94
95         for _, metric := range a.metricsPolicies.Metrics {
96                 policy, check := a.metricsPolicies.Policies[metric.RRMPolicyRatioId]
97                 //TODO What happened if dedicated ratio is already higher that default and threshold is exceed?
98                 if check && policy.PolicyDedicatedRatio <= DEFAULT_DEDICATED_RATIO {
99                         log.Infof("Send Post Request to update DedicatedRatio for DU id: %v Policy id: %v", metric.DUId, policy.PolicyRatioId)
100                         url := getUrlUpdatePolicyDedicatedRatio(sDNRUrl, metric.DUId, policy.PolicyRatioId)
101                         a.client.Post(url, policy.GetUpdateDedicatedRatioMessage(metric.SliceDiff, metric.SliceServiceType, NEW_DEDICATED_RATIO), nil)
102                 }
103         }
104 }
105
106 func getUrlForDistributedUnitFunctions(host string, duid string) string {
107         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
108 }
109
110 func getUrlUpdatePolicyDedicatedRatio(host string, duid string, policyid string) string {
111         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
112 }