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
45 client *restclient.Client
46 metricsPolicies *structures.SliceAssuranceMeas
51 var sDNRUsername string
52 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
60 a.client = restclient.New(&http.Client{}, false)
61 a.metricsPolicies = structures.NewSliceAssuranceMeas()
64 func (a *App) Run(topic string, pollTime int) {
66 a.getMessagesFromDmaap(dmaapMRUrl + topic)
68 for key := range a.metricsPolicies.Metrics {
69 a.getRRMInformation(key.Duid)
71 a.updateDedicatedRatio()
73 time.Sleep(time.Second * time.Duration(pollTime))
77 func (a *App) getMessagesFromDmaap(path string) {
78 log.Infof("Polling new messages from DmaapMR %v", path)
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)
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)
102 func (a *App) getRRMInformation(duid string) {
103 var duRRMPolicyRatio messages.ORanDuRestConf
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)
109 log.Warn("Send of Get RRM Information failed! ", error)
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)
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)
132 log.Warn("Send of Put Request to update DedicatedRatio failed! ", error)
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
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
146 func prettyPrint(jsonStruct interface{}) {
147 b, err := json.MarshalIndent(jsonStruct, "", " ")
149 fmt.Println("error:", err)