3aecf45cd69941b70277f8908c56aaaa4683fb99
[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/du-to-ru-connection=[O-RU-ID]"
41 const unlockMessage = `{"o-ran-sc-du-hello-world:du-to-ru-connection": [{"name":"[O-RU-ID]","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(oRuId, oDuId)
75                 unlockMessage := lfh.getUnlockMessage(oRuId)
76                 if error := restclient.Put(lfh.config.SDNRAddress+sdnrPath, unlockMessage, lfh.client, lfh.config.SDNRUser, lfh.config.SDNRPassword); error == nil {
77                         log.Debugf("Sent unlock message for O-RU: %v to O-DU: %v.", oRuId, oDuId)
78                 } else {
79                         log.Warn("Send of unlock message failed due to ", error)
80                 }
81         } else {
82                 log.Warn("Send of unlock message failed due to ", err)
83         }
84
85 }
86
87 func getSdnrPath(oRuId string, oDuId string) string {
88         sdnrPath := strings.Replace(rawSdnrPath, "[O-DU-ID]", oDuId, 1)
89         sdnrPath = strings.Replace(sdnrPath, "[O-RU-ID]", oRuId, 1)
90         return sdnrPath
91 }
92
93 func (lfh LinkFailureHandler) getUnlockMessage(oRuId string) string {
94         return strings.Replace(unlockMessage, "[O-RU-ID]", oRuId, 1)
95 }