First go version of o-ru-closed-loop
[nonrtric.git] / test / usecases / oruclosedlooprecovery / goversion / main.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 main
22
23 import (
24         "encoding/json"
25         "fmt"
26         "net/http"
27
28         "github.com/gorilla/mux"
29         log "github.com/sirupsen/logrus"
30         "oransc.org/usecase/oruclosedloop/internal/config"
31         "oransc.org/usecase/oruclosedloop/internal/linkfailure"
32         "oransc.org/usecase/oruclosedloop/internal/repository"
33         "oransc.org/usecase/oruclosedloop/internal/restclient"
34 )
35
36 var consumerConfig linkfailure.Configuration
37 var lookupService repository.LookupService
38 var host string
39 var port string
40
41 const jobId = "14e7bb84-a44d-44c1-90b7-6995a92ad43c"
42
43 func init() {
44         configuration := config.New()
45
46         log.SetLevel(configuration.LogLevel)
47
48         if configuration.ConsumerHost == "" || configuration.ConsumerPort == 0 {
49                 log.Fatal("Consumer host and port must be provided!")
50         }
51         host = configuration.ConsumerHost
52         port = fmt.Sprint(configuration.ConsumerPort)
53
54         csvFileHelper := repository.NewCsvFileHelper()
55         lookupService = repository.NewLookupServiceImpl(&csvFileHelper, configuration.ORUToODUMapFile)
56         if initErr := lookupService.Init(); initErr != nil {
57                 log.Fatalf("Unable to create LookupService due to inability to get O-RU-ID to O-DU-ID map. Cause: %v", initErr)
58         }
59         consumerConfig = linkfailure.Configuration{
60                 InfoCoordAddress: configuration.InfoCoordinatorAddress,
61                 SDNRAddress:      configuration.SDNRHost + ":" + fmt.Sprint(configuration.SDNRPort),
62                 SDNRUser:         configuration.SDNRUser,
63                 SDNRPassword:     configuration.SDNPassword,
64         }
65 }
66
67 func main() {
68         defer deleteJob()
69         messageHandler := linkfailure.NewLinkFailureHandler(lookupService, consumerConfig)
70         r := mux.NewRouter()
71         r.HandleFunc("/", messageHandler.MessagesHandler).Methods(http.MethodPost)
72         r.HandleFunc("/admin/start", startHandler).Methods(http.MethodPost)
73         r.HandleFunc("/admin/stop", stopHandler).Methods(http.MethodPost)
74         log.Error(http.ListenAndServe(":"+port, r))
75 }
76
77 func startHandler(w http.ResponseWriter, r *http.Request) {
78         jobRegistrationInfo := struct {
79                 InfoTypeId    string      `json:"info_type_id"`
80                 JobResultUri  string      `json:"job_result_uri"`
81                 JobOwner      string      `json:"job_owner"`
82                 JobDefinition interface{} `json:"job_definition"`
83         }{
84                 InfoTypeId:    "STD_Fault_Messages",
85                 JobResultUri:  host + ":" + port,
86                 JobOwner:      "O-RU Closed Loop Usecase",
87                 JobDefinition: "{}",
88         }
89         body, _ := json.Marshal(jobRegistrationInfo)
90         putErr := restclient.PutWithoutAuth(consumerConfig.InfoCoordAddress+"/data-consumer/v1/info-jobs/"+jobId, body)
91         if putErr != nil {
92                 http.Error(w, fmt.Sprintf("Unable to register consumer job: %v", putErr), http.StatusBadRequest)
93                 return
94         }
95         log.Debug("Registered job.")
96 }
97
98 func stopHandler(w http.ResponseWriter, r *http.Request) {
99         deleteErr := deleteJob()
100         if deleteErr != nil {
101                 http.Error(w, fmt.Sprintf("Unable to delete consumer job: %v", deleteErr), http.StatusBadRequest)
102                 return
103         }
104         log.Debug("Deleted job.")
105 }
106
107 func deleteJob() error {
108         return restclient.Delete(consumerConfig.InfoCoordAddress + "/data-consumer/v1/info-jobs/" + jobId)
109 }