Misc improvements in GO code
[nonrtric.git] / test / usecases / oruclosedlooprecovery / goversion / internal / linkfailure / linkfailurehandler.go
index ebcf312..558d0d7 100644 (file)
@@ -21,8 +21,6 @@
 package linkfailure
 
 import (
-       "encoding/json"
-       "io/ioutil"
        "net/http"
        "strings"
 
@@ -34,7 +32,6 @@ import (
 )
 
 type Configuration struct {
-       ConsumerAddress  string
        InfoCoordAddress string
        SDNRAddress      string
        SDNRUser         string
@@ -42,24 +39,25 @@ type Configuration struct {
 }
 
 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]"
-
 const unlockMessage = `{"o-ran-sc-du-hello-world:du-to-ru-connection": [{"name":"[O-RU-ID]","administrative-state":"UNLOCKED"}]}`
 
 type LinkFailureHandler struct {
        lookupService repository.LookupService
        config        Configuration
+       client        restclient.HTTPClient
 }
 
-func NewLinkFailureHandler(ls repository.LookupService, conf Configuration) *LinkFailureHandler {
+func NewLinkFailureHandler(ls repository.LookupService, conf Configuration, client restclient.HTTPClient) *LinkFailureHandler {
        return &LinkFailureHandler{
                lookupService: ls,
                config:        conf,
+               client:        client,
        }
 }
 
 func (lfh LinkFailureHandler) MessagesHandler(w http.ResponseWriter, r *http.Request) {
        log.Debug("Handling messages")
-       if messages := lfh.getVesMessages(r); messages != nil {
+       if messages := ves.GetVesMessages(r.Body); messages != nil {
                faultMessages := ves.GetFaultMessages(messages)
 
                for _, message := range faultMessages {
@@ -76,7 +74,7 @@ func (lfh LinkFailureHandler) sendUnlockMessage(oRuId string) {
        if oDuId, err := lfh.lookupService.GetODuID(oRuId); err == nil {
                sdnrPath := getSdnrPath(oRuId, oDuId)
                unlockMessage := lfh.getUnlockMessage(oRuId)
-               if error := restclient.Put(lfh.config.SDNRAddress+sdnrPath, unlockMessage, lfh.config.SDNRUser, lfh.config.SDNRPassword); error == nil {
+               if error := restclient.Put(lfh.config.SDNRAddress+sdnrPath, unlockMessage, lfh.client, lfh.config.SDNRUser, lfh.config.SDNRPassword); error == nil {
                        log.Debugf("Sent unlock message for O-RU: %v to O-DU: %v.", oRuId, oDuId)
                } else {
                        log.Warn(error)
@@ -96,18 +94,3 @@ func getSdnrPath(oRuId string, oDuId string) string {
 func (lfh LinkFailureHandler) getUnlockMessage(oRuId string) string {
        return strings.Replace(unlockMessage, "[O-RU-ID]", oRuId, 1)
 }
-
-func (lfh LinkFailureHandler) getVesMessages(r *http.Request) *[]string {
-       var messages []string
-       body, err := ioutil.ReadAll(r.Body)
-       if err != nil {
-               log.Warn(err)
-               return nil
-       }
-       err = json.Unmarshal(body, &messages)
-       if err != nil {
-               log.Warn(err)
-               return nil
-       }
-       return &messages
-}