2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
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"
36 var consumerConfig linkfailure.Configuration
37 var lookupService repository.LookupService
41 const jobId = "14e7bb84-a44d-44c1-90b7-6995a92ad43c"
44 configuration := config.New()
46 log.SetLevel(configuration.LogLevel)
48 if configuration.ConsumerHost == "" || configuration.ConsumerPort == 0 {
49 log.Fatal("Consumer host and port must be provided!")
51 host = configuration.ConsumerHost
52 port = fmt.Sprint(configuration.ConsumerPort)
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)
59 consumerConfig = linkfailure.Configuration{
60 InfoCoordAddress: configuration.InfoCoordinatorAddress,
61 SDNRAddress: configuration.SDNRHost + ":" + fmt.Sprint(configuration.SDNRPort),
62 SDNRUser: configuration.SDNRUser,
63 SDNRPassword: configuration.SDNPassword,
69 messageHandler := linkfailure.NewLinkFailureHandler(lookupService, consumerConfig)
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))
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"`
84 InfoTypeId: "STD_Fault_Messages",
85 JobResultUri: host + ":" + port,
86 JobOwner: "O-RU Closed Loop Usecase",
89 body, _ := json.Marshal(jobRegistrationInfo)
90 putErr := restclient.PutWithoutAuth(consumerConfig.InfoCoordAddress+"/data-consumer/v1/info-jobs/"+jobId, body)
92 http.Error(w, fmt.Sprintf("Unable to register consumer job: %v", putErr), http.StatusBadRequest)
95 log.Debug("Registered job.")
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)
104 log.Debug("Deleted job.")
107 func deleteJob() error {
108 return restclient.Delete(consumerConfig.InfoCoordAddress + "/data-consumer/v1/info-jobs/" + jobId)