Improve documentation
[nonrtric.git] / test / usecases / oruclosedlooprecovery / goversion / internal / linkfailure / linkfailurehandler.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 linkfailure
22
23 import (
24         "net/http"
25         "strings"
26
27         log "github.com/sirupsen/logrus"
28
29         "oransc.org/usecase/oruclosedloop/internal/repository"
30         "oransc.org/usecase/oruclosedloop/internal/restclient"
31         "oransc.org/usecase/oruclosedloop/internal/ves"
32 )
33
34 type Configuration struct {
35         SDNRAddress  string
36         SDNRUser     string
37         SDNRPassword string
38 }
39
40 const rawSdnrPath = "/rests/data/network-topology:network-topology/topology=topology-netconf/node=[O-DU-ID]/yang-ext:mount/o-ran-sc-du-hello-world:network-function/distributed-unit-functions=[O-DU-ID]/radio-resource-management-policy-ratio=rrm-pol-1"
41 const unlockMessage = `{"o-ran-sc-du-hello-world:radio-resource-management-policy-ratio":[{"id":"rrm-pol-1","radio-resource-management-policy-max-ratio":25,"radio-resource-management-policy-members":[{"mobile-country-code":"310","mobile-network-code":"150","slice-differentiator":1,"slice-service-type":1}],"radio-resource-management-policy-min-ratio":15,"user-label":"rrm-pol-1","resource-type":"prb","radio-resource-management-policy-dedicated-ratio":20,"administrative-state":"unlocked"}]}`
42
43 type LinkFailureHandler struct {
44         lookupService repository.LookupService
45         config        Configuration
46         client        restclient.HTTPClient
47 }
48
49 func NewLinkFailureHandler(ls repository.LookupService, conf Configuration, client restclient.HTTPClient) *LinkFailureHandler {
50         return &LinkFailureHandler{
51                 lookupService: ls,
52                 config:        conf,
53                 client:        client,
54         }
55 }
56
57 func (lfh LinkFailureHandler) MessagesHandler(w http.ResponseWriter, r *http.Request) {
58         log.Debug("Handling messages")
59         if messages := ves.GetVesMessages(r.Body); messages != nil {
60                 faultMessages := ves.GetFaultMessages(messages)
61
62                 for _, message := range faultMessages {
63                         if message.IsLinkFailure() {
64                                 lfh.sendUnlockMessage(message.GetORuId())
65                         } else if message.IsClearLinkFailure() {
66                                 log.Debugf("Cleared Link failure for O-RU ID: %v", message.GetORuId())
67                         }
68                 }
69         }
70 }
71
72 func (lfh LinkFailureHandler) sendUnlockMessage(oRuId string) {
73         if oDuId, err := lfh.lookupService.GetODuID(oRuId); err == nil {
74                 sdnrPath := getSdnrPath(oDuId)
75                 if error := restclient.Put(lfh.config.SDNRAddress+sdnrPath, unlockMessage, lfh.client, lfh.config.SDNRUser, lfh.config.SDNRPassword); error == nil {
76                         log.Debugf("Sent unlock message for O-RU: %v to O-DU: %v.", oRuId, oDuId)
77                 } else {
78                         log.Warn("Send of unlock message failed due to ", error)
79                 }
80         } else {
81                 log.Warn("Send of unlock message failed due to ", err)
82         }
83
84 }
85
86 func getSdnrPath(oDuId string) string {
87         sdnrPath := strings.Replace(rawSdnrPath, "[O-DU-ID]", oDuId, -1)
88         return sdnrPath
89 }