elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +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 ( |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 24 | "crypto/tls" |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 25 | "fmt" |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 26 | "net/http" |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 27 | "time" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 28 | |
| 29 | log "github.com/sirupsen/logrus" |
| 30 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/config" |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 31 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs" |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 32 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient" |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 33 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/server" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | var configuration *config.Config |
| 37 | |
| 38 | func init() { |
| 39 | configuration = config.New() |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | func main() { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 43 | log.SetLevel(configuration.LogLevel) |
| 44 | log.Debug("Initializing DMaaP Mediator Producer") |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame] | 45 | log.Debug("Using configuration: ", configuration) |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 46 | if err := validateConfiguration(configuration); err != nil { |
| 47 | log.Fatalf("Stopping producer due to error: %v", err) |
| 48 | } |
| 49 | callbackAddress := fmt.Sprintf("%v:%v", configuration.InfoProducerHost, configuration.InfoProducerPort) |
| 50 | |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame] | 51 | var cert tls.Certificate |
| 52 | if c, err := restclient.CreateClientCertificate(configuration.ProducerCertPath, configuration.ProducerKeyPath); err == nil { |
| 53 | cert = c |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 54 | } else { |
| 55 | log.Fatalf("Stopping producer due to error: %v", err) |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 56 | } |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame] | 57 | retryClient := restclient.CreateRetryClient(cert) |
elinuxhenrik | 803e81a | 2021-10-26 13:22:55 +0200 | [diff] [blame] | 58 | |
elinuxhenrik | 4ce4914 | 2021-11-23 17:01:07 +0100 | [diff] [blame] | 59 | jobsManager := jobs.NewJobsManagerImpl("configs/type_config.json", retryClient, configuration.DMaaPMRAddress, restclient.CreateClientWithoutRetry(cert, 10*time.Second)) |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 60 | if err := registerTypesAndProducer(jobsManager, configuration.InfoCoordinatorAddress, callbackAddress, retryClient); err != nil { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 61 | log.Fatalf("Stopping producer due to: %v", err) |
| 62 | } |
elinuxhenrik | 036962c | 2021-11-17 15:46:07 +0100 | [diff] [blame] | 63 | jobsManager.StartJobsForAllTypes() |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 64 | |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 65 | log.Debug("Starting DMaaP Mediator Producer") |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 66 | go func() { |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 67 | log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort) |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 68 | r := server.NewRouter(jobsManager) |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame] | 69 | if restclient.IsUrlSecure(callbackAddress) { |
| 70 | log.Fatalf("Server stopped: %v", http.ListenAndServeTLS(fmt.Sprintf(":%v", configuration.InfoProducerPort), configuration.ProducerCertPath, configuration.ProducerKeyPath, r)) |
| 71 | } else { |
| 72 | log.Fatalf("Server stopped: %v", http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), r)) |
| 73 | } |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 74 | }() |
| 75 | |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 76 | keepProducerAlive() |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 77 | } |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 78 | |
| 79 | func validateConfiguration(configuration *config.Config) error { |
| 80 | if configuration.InfoProducerHost == "" { |
| 81 | return fmt.Errorf("missing INFO_PRODUCER_HOST") |
| 82 | } |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 83 | if configuration.ProducerCertPath == "" || configuration.ProducerKeyPath == "" { |
| 84 | return fmt.Errorf("missing PRODUCER_CERT and/or PRODUCER_KEY") |
| 85 | } |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 86 | return nil |
| 87 | } |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame] | 88 | func registerTypesAndProducer(jobTypesHandler jobs.JobTypesManager, infoCoordinatorAddress string, callbackAddress string, client restclient.HTTPClient) error { |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 89 | registrator := config.NewRegistratorImpl(infoCoordinatorAddress, client) |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame] | 90 | if types, err := jobTypesHandler.LoadTypesFromConfiguration(); err == nil { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 91 | if regErr := registrator.RegisterTypes(types); regErr != nil { |
| 92 | return fmt.Errorf("unable to register all types due to: %v", regErr) |
| 93 | } |
| 94 | } else { |
| 95 | return fmt.Errorf("unable to get types to register due to: %v", err) |
| 96 | } |
| 97 | producer := config.ProducerRegistrationInfo{ |
| 98 | InfoProducerSupervisionCallbackUrl: callbackAddress + server.StatusPath, |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame] | 99 | SupportedInfoTypes: jobTypesHandler.GetSupportedTypes(), |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 100 | InfoJobCallbackUrl: callbackAddress + server.AddJobPath, |
| 101 | } |
| 102 | if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil { |
| 103 | return fmt.Errorf("unable to register producer due to: %v", err) |
| 104 | } |
| 105 | return nil |
| 106 | } |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 107 | |
| 108 | func keepProducerAlive() { |
| 109 | forever := make(chan int) |
| 110 | <-forever |
| 111 | } |