Improve documentation
[nonrtric/rapp/ransliceassurance.git] / smoversion / 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         "encoding/json"
25         "fmt"
26         "net/http"
27         "time"
28
29         "oransc.org/usecase/oduclosedloop/internal/config"
30         "oransc.org/usecase/oduclosedloop/internal/restclient"
31         "oransc.org/usecase/oduclosedloop/internal/structures"
32         "oransc.org/usecase/oduclosedloop/messages"
33
34         log "github.com/sirupsen/logrus"
35 )
36
37 const (
38         THRESHOLD_TPUT          = 7000
39         DEFAULT_DEDICATED_RATIO = 15
40         NEW_DEDICATED_RATIO     = 25
41 )
42
43 type App struct {
44         client          *restclient.Client
45         metricsPolicies *structures.SliceAssuranceMeas
46 }
47
48 var dmaapMRUrl string
49 var sDNRUrl string
50 var sDNRUsername string
51 var sDNRPassword string
52 var nodeId string
53
54 func (a *App) Initialize(config *config.Config) {
55         dmaapMRUrl = config.MRHost + ":" + config.MRPort
56         sDNRUrl = config.SDNRAddress
57         sDNRUsername = config.SDNRUser
58         sDNRPassword = config.SDNPassword
59         nodeId = config.NodeId
60
61         a.client = restclient.New(&http.Client{}, false)
62         a.metricsPolicies = structures.NewSliceAssuranceMeas()
63 }
64
65 func (a *App) Run(topic string, pollTime int) {
66         for {
67                 a.getMessagesFromDmaap(dmaapMRUrl + topic)
68
69                 for key := range a.metricsPolicies.Metrics {
70                         a.getRRMInformation(key.Duid)
71                 }
72                 a.updateDedicatedRatio()
73
74                 time.Sleep(time.Second * time.Duration(pollTime))
75         }
76 }
77
78 func (a *App) getMessagesFromDmaap(path string) {
79         log.Infof("Polling new messages from DmaapMR %v", path)
80
81         //Added to work with onap-Dmaap
82         var messageStrings []string
83         if error := a.client.Get(path, &messageStrings); error != nil {
84                 log.Warn("Send of Get messages from DMaaP MR failed! ", error)
85         }
86
87         for _, msgString := range messageStrings {
88                 var message messages.StdDefinedMessage
89                 if err := json.Unmarshal([]byte(msgString), &message); err == nil {
90                         for _, meas := range message.GetMeasurements() {
91                                 log.Infof("Create sliceMetric and check if metric exist and update existing one or create new one measurement:  %+v\n", meas)
92                                 //Create sliceMetric and check if metric exist and update existing one or create new one
93                                 if _, err := a.metricsPolicies.AddOrUpdateMetric(meas); err != nil {
94                                         log.Error("Metric could not be added ", err)
95                                 }
96                         }
97                 } else {
98                         log.Warn(err)
99                 }
100         }
101 }
102
103 func (a *App) getRRMInformation(duid string) {
104         var duRRMPolicyRatio messages.ORanDuRestConf
105
106         log.Infof("Get RRM Information from SDNR url: %v", sDNRUrl)
107         if error := a.client.Get(getUrlForDistributedUnitFunctions(sDNRUrl, duid, nodeId), &duRRMPolicyRatio, sDNRUsername, sDNRPassword); error == nil {
108                 prettyPrint(duRRMPolicyRatio.DistributedUnitFunction)
109         } else {
110                 log.Warn("Send of Get RRM Information failed! ", error)
111         }
112
113         for _, odu := range duRRMPolicyRatio.DistributedUnitFunction {
114                 for _, policy := range odu.RRMPolicyRatio {
115                         log.Infof("Add or Update policy: %+v from DU id: %v", policy.Id, duid)
116                         a.metricsPolicies.AddNewPolicy(duid, policy)
117                 }
118         }
119 }
120
121 func (a *App) updateDedicatedRatio() {
122         for _, metric := range a.metricsPolicies.Metrics {
123                 policy, check := a.metricsPolicies.Policies[metric.RRMPolicyRatioId]
124                 //TODO What happened if dedicated ratio is already higher that default and threshold is exceed?
125                 if check && policy.PolicyDedicatedRatio <= DEFAULT_DEDICATED_RATIO {
126                         log.Infof("Send Request to update DedicatedRatio for DU id: %v Policy id: %v", metric.DUId, policy.PolicyRatioId)
127                         path := getUrlUpdatePolicyDedicatedRatio(sDNRUrl, metric.DUId, policy.PolicyRatioId, nodeId)
128                         updatePolicyMessage := policy.GetUpdateDedicatedRatioMessage(metric.SliceDiff, metric.SliceServiceType, NEW_DEDICATED_RATIO)
129                         prettyPrint(updatePolicyMessage)
130                         if error := a.client.Put(path, updatePolicyMessage, nil, sDNRUsername, sDNRPassword); error == nil {
131                                 log.Infof("Policy Dedicated Ratio for PolicyId: %v was updated to %v", policy.PolicyRatioId, NEW_DEDICATED_RATIO)
132                         } else {
133                                 log.Warn("Send of Put Request to update DedicatedRatio failed! ", error)
134                         }
135                 }
136         }
137 }
138
139 func getUrlForDistributedUnitFunctions(host string, duid string, nodeid string) string {
140         return host + "/rests/data/network-topology:network-topology/topology=topology-netconf/node=" + nodeid + "/yang-ext:mount/o-ran-sc-du-hello-world:network-function/distributed-unit-functions=" + duid
141 }
142
143 func getUrlUpdatePolicyDedicatedRatio(host string, duid string, policyid string, nodeid string) string {
144         return host + "/rests/data/network-topology:network-topology/topology=topology-netconf/node=" + nodeid + "/yang-ext:mount/o-ran-sc-du-hello-world:network-function/distributed-unit-functions=" + duid + "/radio-resource-management-policy-ratio=" + policyid
145 }
146
147 func prettyPrint(jsonStruct interface{}) {
148         b, err := json.MarshalIndent(jsonStruct, "", "  ")
149         if err != nil {
150                 fmt.Println("error:", err)
151         }
152         fmt.Print(string(b))
153 }