Seed code
[nonrtric/rapp/orufhrecovery.git] / 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         "crypto/tls"
25         "encoding/json"
26         "fmt"
27         "net/http"
28         "os"
29         "os/signal"
30         "syscall"
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
40 const jobId = "14e7bb84-a44d-44c1-90b7-6995a92ad43c"
41 var job_definition interface{}
42
43 var jobRegistrationInfo = struct {
44         InfoTypeID    string      `json:"info_type_id"`
45         JobResultURI  string      `json:"job_result_uri"`
46         JobOwner      string      `json:"job_owner"`
47         JobDefinition interface{} `json:"job_definition"`
48 }{
49         InfoTypeID:    "STD_Fault_Messages",
50         JobResultURI:  "",
51         JobOwner:      "O-RU Closed Loop Usecase",
52         JobDefinition: job_definition,
53 }
54
55 var client restclient.HTTPClient
56 var configuration *config.Config
57 var linkfailureConfig linkfailure.Configuration
58 var lookupService repository.LookupService
59 var consumerPort string
60 var started bool
61
62 func init() {
63         doInit()
64 }
65
66 func doInit() {
67         configuration = config.New()
68
69         log.SetLevel(configuration.LogLevel)
70         log.Debug("Using configuration: ", configuration)
71
72         consumerPort = fmt.Sprint(configuration.ConsumerPort)
73         jobRegistrationInfo.JobResultURI = configuration.ConsumerHost + ":" + consumerPort
74
75         linkfailureConfig = linkfailure.Configuration{
76                 SDNRAddress:  configuration.SDNRAddress,
77                 SDNRUser:     configuration.SDNRUser,
78                 SDNRPassword: configuration.SDNPassword,
79         }
80 }
81
82 func main() {
83         if err := validateConfiguration(configuration); err != nil {
84                 log.Fatalf("Unable to start consumer due to configuration error: %v", err)
85         }
86
87         csvFileHelper := repository.NewCsvFileHelperImpl()
88         if initErr := initializeLookupService(csvFileHelper, configuration.ORUToODUMapFile); initErr != nil {
89                 log.Fatalf("Unable to create LookupService due to inability to get O-RU-ID to O-DU-ID map. Cause: %v", initErr)
90         }
91
92         var cert tls.Certificate
93         if c, err := restclient.CreateClientCertificate(configuration.ConsumerCertPath, configuration.ConsumerKeyPath); err == nil {
94                 cert = c
95         } else {
96                 log.Fatalf("Stopping producer due to error: %v", err)
97         }
98         client = restclient.CreateRetryClient(cert)
99
100         go func() {
101                 startServer()
102                 os.Exit(1) // If the startServer function exits, it is because there has been a failure in the server, so we exit.
103         }()
104
105         go func() {
106                 deleteOnShutdown(make(chan os.Signal, 1))
107                 os.Exit(0)
108         }()
109
110         keepConsumerAlive()
111 }
112
113 func validateConfiguration(configuration *config.Config) error {
114         if configuration.ConsumerHost == "" || configuration.ConsumerPort == 0 {
115                 return fmt.Errorf("consumer host and port must be provided")
116         }
117
118         if configuration.ConsumerCertPath == "" || configuration.ConsumerKeyPath == "" {
119                 return fmt.Errorf("missing CONSUMER_CERT and/or CONSUMER_KEY")
120         }
121
122         return nil
123 }
124
125 func initializeLookupService(csvFileHelper repository.CsvFileHelper, csvFile string) error {
126         lookupService = repository.NewLookupServiceImpl(csvFileHelper, csvFile)
127         return lookupService.Init()
128 }
129
130 func getRouter() *mux.Router {
131         messageHandler := linkfailure.NewLinkFailureHandler(lookupService, linkfailureConfig, client)
132
133         r := mux.NewRouter()
134         r.HandleFunc("/", messageHandler.MessagesHandler).Methods(http.MethodPost).Name("messageHandler")
135         r.HandleFunc("/status", statusHandler).Methods(http.MethodGet).Name("status")
136         r.HandleFunc("/admin/start", startHandler).Methods(http.MethodPost).Name("start")
137         r.HandleFunc("/admin/stop", stopHandler).Methods(http.MethodPost).Name("stop")
138
139         return r
140 }
141
142 func startServer() {
143         var err error
144         if restclient.IsUrlSecure(configuration.ConsumerHost) {
145                 err = http.ListenAndServeTLS(fmt.Sprintf(":%v", configuration.ConsumerPort), configuration.ConsumerCertPath, configuration.ConsumerKeyPath, getRouter())
146         } else {
147                 err = http.ListenAndServe(fmt.Sprintf(":%v", configuration.ConsumerPort), getRouter())
148         }
149         if err != nil {
150                 log.Errorf("Server stopped unintentionally due to: %v. Deleteing job.", err)
151                 if deleteErr := deleteJob(); deleteErr != nil {
152                         log.Errorf("Unable to delete consumer job due to: %v. Please remove job %v manually.", deleteErr, jobId)
153                 }
154         }
155 }
156
157 func keepConsumerAlive() {
158         forever := make(chan int)
159         <-forever
160 }
161
162 func startHandler(w http.ResponseWriter, r *http.Request) {
163         body, _ := json.Marshal(jobRegistrationInfo)
164         putErr := restclient.PutWithoutAuth(configuration.InfoCoordinatorAddress+"/data-consumer/v1/info-jobs/"+jobId, body, client)
165         if putErr != nil {
166                 http.Error(w, fmt.Sprintf("Unable to register consumer job due to: %v.", putErr), http.StatusBadRequest)
167                 return
168         }
169         log.Debug("Registered job.")
170         started = true
171 }
172
173 func stopHandler(w http.ResponseWriter, r *http.Request) {
174         deleteErr := deleteJob()
175         if deleteErr != nil {
176                 http.Error(w, fmt.Sprintf("Unable to delete consumer job due to: %v. Please remove job %v manually.", deleteErr, jobId), http.StatusBadRequest)
177                 return
178         }
179         log.Debug("Deleted job.")
180         started = false
181 }
182
183 func statusHandler(w http.ResponseWriter, r *http.Request) {
184         runStatus := "started"
185         if !started {
186                 runStatus = "stopped"
187         }
188         fmt.Fprintf(w, `{"status": "%v"}`, runStatus)
189 }
190
191 func deleteOnShutdown(s chan os.Signal) {
192         signal.Notify(s, os.Interrupt)
193         signal.Notify(s, syscall.SIGTERM)
194         <-s
195         log.Info("Shutting down gracefully.")
196         if err := deleteJob(); err != nil {
197                 log.Errorf("Unable to delete job on shutdown due to: %v. Please remove job %v manually.", err, jobId)
198         }
199 }
200
201 func deleteJob() error {
202         return restclient.Delete(configuration.InfoCoordinatorAddress+"/data-consumer/v1/info-jobs/"+jobId, client)
203 }