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