c4a4d65b7af266ea90c44eddb890d3fdb106641e
[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         NODE_ID                 = "O-DU-1122"
42 )
43
44 type App struct {
45         client          *restclient.Client
46         metricsPolicies *structures.SliceAssuranceMeas
47 }
48
49 var dmaapMRUrl string
50 var sDNRUrl string
51 var sDNRUsername string
52 var sDNRPassword 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
60         a.client = restclient.New(&http.Client{}, false)
61         a.metricsPolicies = structures.NewSliceAssuranceMeas()
62 }
63
64 func (a *App) Run(topic string, pollTime int) {
65         for {
66                 a.getMessagesFromDmaap(dmaapMRUrl + topic)
67
68                 for key := range a.metricsPolicies.Metrics {
69                         a.getRRMInformation(key.Duid)
70                 }
71                 a.updateDedicatedRatio()
72
73                 time.Sleep(time.Second * time.Duration(pollTime))
74         }
75 }
76
77 func (a *App) getMessagesFromDmaap(path string) {
78         log.Infof("Polling new messages from DmaapMR %v", path)
79
80         //Added to work with onap-Dmaap
81         var messageStrings []string
82         if error := a.client.Get(path, &messageStrings); error != nil {
83                 log.Warn("Send of Get messages from DmaapMR failed! ", error)
84         }
85
86         for _, msgString := range messageStrings {
87                 var message messages.StdDefinedMessage
88                 if err := json.Unmarshal([]byte(msgString), &message); err == nil {
89                         for _, meas := range message.GetMeasurements() {
90                                 log.Infof("Create sliceMetric and check if metric exist and update existing one or create new one measurement:  %+v\n", meas)
91                                 //Create sliceMetric and check if metric exist and update existing one or create new one
92                                 if _, err := a.metricsPolicies.AddOrUpdateMetric(meas); err != nil {
93                                         log.Error("Metric could not be added ", err)
94                                 }
95                         }
96                 } else {
97                         log.Warn(err)
98                 }
99         }
100 }
101
102 func (a *App) getRRMInformation(duid string) {
103         var duRRMPolicyRatio messages.ORanDuRestConf
104
105         log.Infof("Get RRM Information from SDNR url: %v", sDNRUrl)
106         if error := a.client.Get(getUrlForDistributedUnitFunctions(sDNRUrl, duid), &duRRMPolicyRatio, sDNRUsername, sDNRPassword); error == nil {
107                 prettyPrint(duRRMPolicyRatio.DistributedUnitFunction)
108         } else {
109                 log.Warn("Send of Get RRM Information failed! ", error)
110         }
111
112         for _, odu := range duRRMPolicyRatio.DistributedUnitFunction {
113                 for _, policy := range odu.RRMPolicyRatio {
114                         log.Infof("Add or Update policy: %+v from DU id: %v", policy.Id, duid)
115                         a.metricsPolicies.AddNewPolicy(duid, policy)
116                 }
117         }
118 }
119
120 func (a *App) updateDedicatedRatio() {
121         for _, metric := range a.metricsPolicies.Metrics {
122                 policy, check := a.metricsPolicies.Policies[metric.RRMPolicyRatioId]
123                 //TODO What happened if dedicated ratio is already higher that default and threshold is exceed?
124                 if check && policy.PolicyDedicatedRatio <= DEFAULT_DEDICATED_RATIO {
125                         log.Infof("Send Request to update DedicatedRatio for DU id: %v Policy id: %v", metric.DUId, policy.PolicyRatioId)
126                         path := getUrlUpdatePolicyDedicatedRatio(sDNRUrl, metric.DUId, policy.PolicyRatioId)
127                         updatePolicyMessage := policy.GetUpdateDedicatedRatioMessage(metric.SliceDiff, metric.SliceServiceType, NEW_DEDICATED_RATIO)
128                         prettyPrint(updatePolicyMessage)
129                         if error := a.client.Put(path, updatePolicyMessage, nil, sDNRUsername, sDNRPassword); error == nil {
130                                 log.Infof("Policy Dedicated Ratio for PolicyId: %v was updated to %v", policy.PolicyRatioId, NEW_DEDICATED_RATIO)
131                         } else {
132                                 log.Warn("Send of Put Request to update DedicatedRatio failed! ", error)
133                         }
134                 }
135         }
136 }
137
138 func getUrlForDistributedUnitFunctions(host string, duid string) string {
139         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
140 }
141
142 func getUrlUpdatePolicyDedicatedRatio(host string, duid string, policyid string) string {
143         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
144 }
145
146 func prettyPrint(jsonStruct interface{}) {
147         b, err := json.MarshalIndent(jsonStruct, "", "  ")
148         if err != nil {
149                 fmt.Println("error:", err)
150         }
151         fmt.Print(string(b))
152 }