2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package sliceassurance
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"
34 log "github.com/sirupsen/logrus"
39 DEFAULT_DEDICATED_RATIO = 15
40 NEW_DEDICATED_RATIO = 25
44 client *restclient.Client
45 metricsPolicies *structures.SliceAssuranceMeas
50 var sDNRUsername string
51 var sDNRPassword string
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
61 a.client = restclient.New(&http.Client{}, false)
62 a.metricsPolicies = structures.NewSliceAssuranceMeas()
65 func (a *App) Run(topic string, pollTime int) {
67 a.getMessagesFromDmaap(dmaapMRUrl + topic)
69 for key := range a.metricsPolicies.Metrics {
70 a.getRRMInformation(key.Duid)
72 a.updateDedicatedRatio()
74 time.Sleep(time.Second * time.Duration(pollTime))
78 func (a *App) getMessagesFromDmaap(path string) {
79 log.Infof("Polling new messages from DmaapMR %v", path)
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 DmaapMR failed! ", error)
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)
103 func (a *App) getRRMInformation(duid string) {
104 var duRRMPolicyRatio messages.ORanDuRestConf
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)
110 log.Warn("Send of Get RRM Information failed! ", error)
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)
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)
133 log.Warn("Send of Put Request to update DedicatedRatio failed! ", error)
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
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
147 func prettyPrint(jsonStruct interface{}) {
148 b, err := json.MarshalIndent(jsonStruct, "", " ")
150 fmt.Println("error:", err)