ebcf31253fc7a5100a360a75bd6c6f1c2c84ca85
[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         "encoding/json"
25         "io/ioutil"
26         "net/http"
27         "strings"
28
29         log "github.com/sirupsen/logrus"
30
31         "oransc.org/usecase/oruclosedloop/internal/repository"
32         "oransc.org/usecase/oruclosedloop/internal/restclient"
33         "oransc.org/usecase/oruclosedloop/internal/ves"
34 )
35
36 type Configuration struct {
37         ConsumerAddress  string
38         InfoCoordAddress string
39         SDNRAddress      string
40         SDNRUser         string
41         SDNRPassword     string
42 }
43
44 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/du-to-ru-connection=[O-RU-ID]"
45
46 const unlockMessage = `{"o-ran-sc-du-hello-world:du-to-ru-connection": [{"name":"[O-RU-ID]","administrative-state":"UNLOCKED"}]}`
47
48 type LinkFailureHandler struct {
49         lookupService repository.LookupService
50         config        Configuration
51 }
52
53 func NewLinkFailureHandler(ls repository.LookupService, conf Configuration) *LinkFailureHandler {
54         return &LinkFailureHandler{
55                 lookupService: ls,
56                 config:        conf,
57         }
58 }
59
60 func (lfh LinkFailureHandler) MessagesHandler(w http.ResponseWriter, r *http.Request) {
61         log.Debug("Handling messages")
62         if messages := lfh.getVesMessages(r); messages != nil {
63                 faultMessages := ves.GetFaultMessages(messages)
64
65                 for _, message := range faultMessages {
66                         if message.IsLinkFailure() {
67                                 lfh.sendUnlockMessage(message.GetORuId())
68                         } else if message.IsClearLinkFailure() {
69                                 log.Debugf("Cleared Link failure for O-RU ID: %v", message.GetORuId())
70                         }
71                 }
72         }
73 }
74
75 func (lfh LinkFailureHandler) sendUnlockMessage(oRuId string) {
76         if oDuId, err := lfh.lookupService.GetODuID(oRuId); err == nil {
77                 sdnrPath := getSdnrPath(oRuId, oDuId)
78                 unlockMessage := lfh.getUnlockMessage(oRuId)
79                 if error := restclient.Put(lfh.config.SDNRAddress+sdnrPath, unlockMessage, lfh.config.SDNRUser, lfh.config.SDNRPassword); error == nil {
80                         log.Debugf("Sent unlock message for O-RU: %v to O-DU: %v.", oRuId, oDuId)
81                 } else {
82                         log.Warn(error)
83                 }
84         } else {
85                 log.Warn(err)
86         }
87
88 }
89
90 func getSdnrPath(oRuId string, oDuId string) string {
91         sdnrPath := strings.Replace(rawSdnrPath, "[O-DU-ID]", oDuId, 1)
92         sdnrPath = strings.Replace(sdnrPath, "[O-RU-ID]", oRuId, 1)
93         return sdnrPath
94 }
95
96 func (lfh LinkFailureHandler) getUnlockMessage(oRuId string) string {
97         return strings.Replace(unlockMessage, "[O-RU-ID]", oRuId, 1)
98 }
99
100 func (lfh LinkFailureHandler) getVesMessages(r *http.Request) *[]string {
101         var messages []string
102         body, err := ioutil.ReadAll(r.Body)
103         if err != nil {
104                 log.Warn(err)
105                 return nil
106         }
107         err = json.Unmarshal(body, &messages)
108         if err != nil {
109                 log.Warn(err)
110                 return nil
111         }
112         return &messages
113 }