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 | |
elinuxhenrik | 803e81a | 2021-10-26 13:22:55 +0200 | [diff] [blame] | 29 | "github.com/hashicorp/go-retryablehttp" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 30 | log "github.com/sirupsen/logrus" |
| 31 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/config" |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 32 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs" |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 33 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient" |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 34 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/server" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | var configuration *config.Config |
| 38 | |
| 39 | func init() { |
| 40 | configuration = config.New() |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | func main() { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 44 | log.SetLevel(configuration.LogLevel) |
| 45 | log.Debug("Initializing DMaaP Mediator Producer") |
| 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 | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 51 | var retryClient restclient.HTTPClient |
| 52 | if cert, err := createClientCertificate(); err == nil { |
| 53 | retryClient = createRetryClient(cert) |
| 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 | 803e81a | 2021-10-26 13:22:55 +0200 | [diff] [blame] | 57 | |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 58 | jobsManager := jobs.NewJobsManagerImpl("configs/type_config.json", retryClient, configuration.DMaaPMRAddress, &http.Client{ |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 59 | Timeout: time.Second * 5, |
| 60 | }) |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 61 | if err := registerTypesAndProducer(jobsManager, configuration.InfoCoordinatorAddress, callbackAddress, retryClient); err != nil { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 62 | log.Fatalf("Stopping producer due to: %v", err) |
| 63 | } |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 64 | jobsManager.StartJobs() |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 65 | |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 66 | log.Debug("Starting DMaaP Mediator Producer") |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 67 | go func() { |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 68 | log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort) |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 69 | r := server.NewRouter(jobsManager) |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 70 | log.Fatalf("Server stopped: %v", http.ListenAndServeTLS(fmt.Sprintf(":%v", configuration.InfoProducerPort), configuration.ProducerCertPath, configuration.ProducerKeyPath, r)) |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 71 | }() |
| 72 | |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 73 | keepProducerAlive() |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 74 | } |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 75 | |
| 76 | func validateConfiguration(configuration *config.Config) error { |
| 77 | if configuration.InfoProducerHost == "" { |
| 78 | return fmt.Errorf("missing INFO_PRODUCER_HOST") |
| 79 | } |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 80 | if configuration.ProducerCertPath == "" || configuration.ProducerKeyPath == "" { |
| 81 | return fmt.Errorf("missing PRODUCER_CERT and/or PRODUCER_KEY") |
| 82 | } |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 83 | return nil |
| 84 | } |
| 85 | |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 86 | func createClientCertificate() (*tls.Certificate, error) { |
| 87 | if cert, err := tls.LoadX509KeyPair(configuration.ProducerCertPath, configuration.ProducerKeyPath); err == nil { |
| 88 | return &cert, nil |
| 89 | } else { |
elinuxhenrik | 11b500e | 2021-11-02 09:19:26 +0100 | [diff] [blame] | 90 | return nil, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", configuration.ProducerCertPath, configuration.ProducerKeyPath, err) |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | func createRetryClient(cert *tls.Certificate) *http.Client { |
| 95 | rawRetryClient := retryablehttp.NewClient() |
| 96 | rawRetryClient.RetryWaitMax = time.Minute |
| 97 | rawRetryClient.RetryMax = int(^uint(0) >> 1) |
| 98 | rawRetryClient.HTTPClient.Transport = &http.Transport{ |
| 99 | TLSClientConfig: &tls.Config{ |
| 100 | Certificates: []tls.Certificate{ |
| 101 | *cert, |
| 102 | }, |
| 103 | InsecureSkipVerify: true, |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | return rawRetryClient.StandardClient() |
| 108 | } |
| 109 | |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 110 | func registerTypesAndProducer(jobHandler jobs.JobTypesManager, infoCoordinatorAddress string, callbackAddress string, client restclient.HTTPClient) error { |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 111 | registrator := config.NewRegistratorImpl(infoCoordinatorAddress, client) |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 112 | if types, err := jobHandler.LoadTypesFromConfiguration(); err == nil { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 113 | if regErr := registrator.RegisterTypes(types); regErr != nil { |
| 114 | return fmt.Errorf("unable to register all types due to: %v", regErr) |
| 115 | } |
| 116 | } else { |
| 117 | return fmt.Errorf("unable to get types to register due to: %v", err) |
| 118 | } |
| 119 | producer := config.ProducerRegistrationInfo{ |
| 120 | InfoProducerSupervisionCallbackUrl: callbackAddress + server.StatusPath, |
| 121 | SupportedInfoTypes: jobHandler.GetSupportedTypes(), |
| 122 | InfoJobCallbackUrl: callbackAddress + server.AddJobPath, |
| 123 | } |
| 124 | if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil { |
| 125 | return fmt.Errorf("unable to register producer due to: %v", err) |
| 126 | } |
| 127 | return nil |
| 128 | } |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 129 | |
| 130 | func keepProducerAlive() { |
| 131 | forever := make(chan int) |
| 132 | <-forever |
| 133 | } |