elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 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" |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 27 | "os" |
| 28 | "os/signal" |
| 29 | "syscall" |
elinuxhenrik | c60a390 | 2021-10-14 07:56:15 +0200 | [diff] [blame] | 30 | "time" |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 31 | |
| 32 | "github.com/gorilla/mux" |
| 33 | log "github.com/sirupsen/logrus" |
| 34 | "oransc.org/usecase/oruclosedloop/internal/config" |
| 35 | "oransc.org/usecase/oruclosedloop/internal/linkfailure" |
| 36 | "oransc.org/usecase/oruclosedloop/internal/repository" |
| 37 | "oransc.org/usecase/oruclosedloop/internal/restclient" |
| 38 | ) |
| 39 | |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 40 | type Server interface { |
| 41 | ListenAndServe() error |
| 42 | } |
| 43 | |
elinuxhenrik | c60a390 | 2021-10-14 07:56:15 +0200 | [diff] [blame] | 44 | const timeoutHTTPClient = time.Second * 5 |
| 45 | const jobId = "14e7bb84-a44d-44c1-90b7-6995a92ad43c" |
| 46 | |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 47 | var jobRegistrationInfo = struct { |
| 48 | InfoTypeId string `json:"info_type_id"` |
| 49 | JobResultUri string `json:"job_result_uri"` |
| 50 | JobOwner string `json:"job_owner"` |
| 51 | JobDefinition interface{} `json:"job_definition"` |
| 52 | }{ |
| 53 | InfoTypeId: "STD_Fault_Messages", |
| 54 | JobResultUri: "", |
| 55 | JobOwner: "O-RU Closed Loop Usecase", |
| 56 | JobDefinition: "{}", |
| 57 | } |
| 58 | |
| 59 | var client restclient.HTTPClient |
| 60 | var configuration *config.Config |
elinuxhenrik | c60a390 | 2021-10-14 07:56:15 +0200 | [diff] [blame] | 61 | var linkfailureConfig linkfailure.Configuration |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 62 | var lookupService repository.LookupService |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 63 | var consumerPort string |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 64 | |
| 65 | func init() { |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 66 | doInit() |
| 67 | } |
| 68 | |
| 69 | func doInit() { |
| 70 | configuration = config.New() |
| 71 | |
| 72 | log.SetLevel(configuration.LogLevel) |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 73 | |
elinuxhenrik | c60a390 | 2021-10-14 07:56:15 +0200 | [diff] [blame] | 74 | client = &http.Client{ |
| 75 | Timeout: timeoutHTTPClient, |
| 76 | } |
| 77 | |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 78 | consumerPort = fmt.Sprint(configuration.ConsumerPort) |
| 79 | jobRegistrationInfo.JobResultUri = configuration.ConsumerHost + ":" + consumerPort |
elinuxhenrik | c60a390 | 2021-10-14 07:56:15 +0200 | [diff] [blame] | 80 | |
| 81 | linkfailureConfig = linkfailure.Configuration{ |
| 82 | SDNRAddress: configuration.SDNRHost + ":" + fmt.Sprint(configuration.SDNRPort), |
| 83 | SDNRUser: configuration.SDNRUser, |
| 84 | SDNRPassword: configuration.SDNPassword, |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 88 | func main() { |
| 89 | if err := validateConfiguration(configuration); err != nil { |
| 90 | log.Fatalf("Unable to start consumer due to configuration error: %v", err) |
| 91 | } |
| 92 | |
| 93 | csvFileHelper := repository.NewCsvFileHelperImpl() |
| 94 | if initErr := initializeLookupService(csvFileHelper, configuration.ORUToODUMapFile); initErr != nil { |
| 95 | log.Fatalf("Unable to create LookupService due to inability to get O-RU-ID to O-DU-ID map. Cause: %v", initErr) |
| 96 | } |
| 97 | |
| 98 | go func() { |
| 99 | startServer(&http.Server{ |
| 100 | Addr: ":" + consumerPort, |
| 101 | Handler: getRouter(), |
| 102 | }) |
elinuxhenrik | c7ff638 | 2021-10-27 13:11:50 +0200 | [diff] [blame] | 103 | deleteJob() |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 104 | os.Exit(1) // If the startServer function exits, it is because there has been a failure in the server, so we exit. |
| 105 | }() |
| 106 | |
| 107 | go func() { |
| 108 | deleteOnShutdown(make(chan os.Signal, 1)) |
| 109 | os.Exit(0) |
| 110 | }() |
| 111 | |
| 112 | keepConsumerAlive() |
| 113 | } |
| 114 | |
elinuxhenrik | c60a390 | 2021-10-14 07:56:15 +0200 | [diff] [blame] | 115 | func validateConfiguration(configuration *config.Config) error { |
| 116 | if configuration.ConsumerHost == "" || configuration.ConsumerPort == 0 { |
| 117 | return fmt.Errorf("consumer host and port must be provided") |
| 118 | } |
| 119 | return nil |
| 120 | } |
| 121 | |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 122 | func initializeLookupService(csvFileHelper repository.CsvFileHelper, csvFile string) error { |
| 123 | lookupService = repository.NewLookupServiceImpl(csvFileHelper, csvFile) |
| 124 | return lookupService.Init() |
elinuxhenrik | c60a390 | 2021-10-14 07:56:15 +0200 | [diff] [blame] | 125 | } |
| 126 | |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 127 | func getRouter() *mux.Router { |
elinuxhenrik | c60a390 | 2021-10-14 07:56:15 +0200 | [diff] [blame] | 128 | messageHandler := linkfailure.NewLinkFailureHandler(lookupService, linkfailureConfig, client) |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 129 | |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 130 | r := mux.NewRouter() |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 131 | r.HandleFunc("/", messageHandler.MessagesHandler).Methods(http.MethodPost).Name("messageHandler") |
| 132 | r.HandleFunc("/admin/start", startHandler).Methods(http.MethodPost).Name("start") |
| 133 | r.HandleFunc("/admin/stop", stopHandler).Methods(http.MethodPost).Name("stop") |
| 134 | |
| 135 | return r |
| 136 | } |
| 137 | |
| 138 | func startServer(server Server) { |
| 139 | if err := server.ListenAndServe(); err != nil { |
| 140 | log.Errorf("Server stopped unintentionally due to: %v. Deleteing job.", err) |
| 141 | if deleteErr := deleteJob(); deleteErr != nil { |
| 142 | log.Error(fmt.Sprintf("Unable to delete consumer job due to: %v. Please remove job %v manually.", deleteErr, jobId)) |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func keepConsumerAlive() { |
| 148 | forever := make(chan int) |
| 149 | <-forever |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | func startHandler(w http.ResponseWriter, r *http.Request) { |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 153 | body, _ := json.Marshal(jobRegistrationInfo) |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 154 | putErr := restclient.PutWithoutAuth(configuration.InfoCoordinatorAddress+"/data-consumer/v1/info-jobs/"+jobId, body, client) |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 155 | if putErr != nil { |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 156 | http.Error(w, fmt.Sprintf("Unable to register consumer job due to: %v.", putErr), http.StatusBadRequest) |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 157 | return |
| 158 | } |
| 159 | log.Debug("Registered job.") |
| 160 | } |
| 161 | |
| 162 | func stopHandler(w http.ResponseWriter, r *http.Request) { |
| 163 | deleteErr := deleteJob() |
| 164 | if deleteErr != nil { |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 165 | http.Error(w, fmt.Sprintf("Unable to delete consumer job due to: %v. Please remove job %v manually.", deleteErr, jobId), http.StatusBadRequest) |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 166 | return |
| 167 | } |
| 168 | log.Debug("Deleted job.") |
| 169 | } |
| 170 | |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 171 | func deleteOnShutdown(s chan os.Signal) { |
| 172 | signal.Notify(s, os.Interrupt) |
| 173 | signal.Notify(s, syscall.SIGTERM) |
| 174 | <-s |
| 175 | log.Info("Shutting down gracefully.") |
| 176 | if err := deleteJob(); err != nil { |
| 177 | log.Error(fmt.Sprintf("Unable to delete job on shutdown due to: %v. Please remove job %v manually.", err, jobId)) |
| 178 | } |
| 179 | } |
| 180 | |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 181 | func deleteJob() error { |
elinuxhenrik | c658d38 | 2021-10-22 13:40:15 +0200 | [diff] [blame] | 182 | return restclient.Delete(configuration.InfoCoordinatorAddress+"/data-consumer/v1/info-jobs/"+jobId, client) |
elinuxhenrik | 856d55d | 2021-08-24 17:01:24 +0200 | [diff] [blame] | 183 | } |