Consumer O-DU slice assurance rApp
[nonrtric/rapp/ransliceassurance.git] / icsversion / internal / odusliceassurance / sdnrHandler.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: 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
27         log "github.com/sirupsen/logrus"
28         "oransc.org/usecase/oduclosedloop/icsversion/internal/restclient"
29         "oransc.org/usecase/oduclosedloop/icsversion/internal/structures"
30         "oransc.org/usecase/oduclosedloop/icsversion/messages"
31 )
32
33 type SdnrConfiguration struct {
34         SDNRAddress  string
35         SDNRUser     string
36         SDNRPassword string
37 }
38
39 type SdnrHandler struct {
40         config SdnrConfiguration
41         client *restclient.Client
42         data   *structures.SliceAssuranceMeas
43 }
44
45 func NewSdnrHandler(conf SdnrConfiguration, client *restclient.Client, data *structures.SliceAssuranceMeas) *SdnrHandler {
46         return &SdnrHandler{
47                 config: conf,
48                 client: client,
49                 data:   data,
50         }
51 }
52
53 func (handler SdnrHandler) getRRMInformation(duid string) {
54         var duRRMPolicyRatio messages.ORanDuRestConf
55
56         log.Infof("Get RRM Information from SDNR url: %v", handler.config.SDNRAddress)
57         if error := handler.client.Get(getUrlForDistributedUnitFunctions(handler.config.SDNRAddress, duid), &duRRMPolicyRatio, handler.config.SDNRUser, handler.config.SDNRPassword); error == nil {
58                 prettyPrint(duRRMPolicyRatio.DistributedUnitFunction)
59         } else {
60                 log.Warn("Send of Get RRM Information failed! ", error)
61         }
62
63         for _, odu := range duRRMPolicyRatio.DistributedUnitFunction {
64                 for _, policy := range odu.RRMPolicyRatio {
65                         log.Infof("Add or Update policy: %+v from DU id: %v", policy.Id, duid)
66                         handler.data.AddNewPolicy(duid, policy)
67                 }
68         }
69 }
70
71 func (handler SdnrHandler) updateDedicatedRatio() {
72         for _, metric := range handler.data.Metrics {
73                 policy, check := handler.data.Policies[metric.RRMPolicyRatioId]
74                 //TODO What happened if dedicated ratio is already higher that default and threshold is exceed?
75                 if check && policy.PolicyDedicatedRatio <= DEFAULT_DEDICATED_RATIO {
76                         log.Infof("Send Request to update DedicatedRatio for DU id: %v Policy id: %v", metric.DUId, policy.PolicyRatioId)
77                         path := getUrlUpdatePolicyDedicatedRatio(handler.config.SDNRAddress, metric.DUId, policy.PolicyRatioId)
78                         updatePolicyMessage := policy.GetUpdateDedicatedRatioMessage(metric.SliceDiff, metric.SliceServiceType, NEW_DEDICATED_RATIO)
79                         prettyPrint(updatePolicyMessage)
80                         if error := handler.client.Put(path, updatePolicyMessage, nil, handler.config.SDNRUser, handler.config.SDNRPassword); error == nil {
81                                 log.Infof("Policy Dedicated Ratio for PolicyId: %v was updated to %v", policy.PolicyRatioId, NEW_DEDICATED_RATIO)
82                         } else {
83                                 log.Warn("Send of Put Request to update DedicatedRatio failed! ", error)
84                         }
85                 }
86         }
87 }
88
89 func getUrlForDistributedUnitFunctions(host string, duid string) string {
90         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
91 }
92
93 func getUrlUpdatePolicyDedicatedRatio(host string, duid string, policyid string) string {
94         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
95 }
96
97 func prettyPrint(jsonStruct interface{}) {
98         b, err := json.MarshalIndent(jsonStruct, "", "  ")
99         if err != nil {
100                 fmt.Println("error:", err)
101         }
102         fmt.Print(string(b))
103 }