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===================================
29 log "github.com/sirupsen/logrus"
30 "oransc.org/nonrtric/dmaapmediatorproducer/internal/config"
31 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
32 "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
33 "oransc.org/nonrtric/dmaapmediatorproducer/internal/server"
36 var configuration *config.Config
39 configuration = config.New()
43 log.SetLevel(configuration.LogLevel)
44 log.Debug("Initializing DMaaP Mediator Producer")
45 log.Debug("Using configuration: ", configuration)
46 if err := validateConfiguration(configuration); err != nil {
47 log.Fatalf("Stopping producer due to error: %v", err)
49 callbackAddress := fmt.Sprintf("%v:%v", configuration.InfoProducerHost, configuration.InfoProducerPort)
51 var cert tls.Certificate
52 if c, err := restclient.CreateClientCertificate(configuration.ProducerCertPath, configuration.ProducerKeyPath); err == nil {
55 log.Fatalf("Stopping producer due to error: %v", err)
57 retryClient := restclient.CreateRetryClient(cert)
59 jobsManager := jobs.NewJobsManagerImpl(retryClient, configuration.DMaaPMRAddress, restclient.CreateClientWithoutRetry(cert, 10*time.Second))
60 if err := registerTypesAndProducer(jobsManager, configuration.InfoCoordinatorAddress, callbackAddress, retryClient); err != nil {
61 log.Fatalf("Stopping producer due to: %v", err)
63 jobsManager.StartJobsForAllTypes()
65 log.Debug("Starting DMaaP Mediator Producer")
67 log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort)
68 r := server.NewRouter(jobsManager)
69 if restclient.IsUrlSecure(callbackAddress) {
70 log.Fatalf("Server stopped: %v", http.ListenAndServeTLS(fmt.Sprintf(":%v", configuration.InfoProducerPort), configuration.ProducerCertPath, configuration.ProducerKeyPath, r))
72 log.Fatalf("Server stopped: %v", http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), r))
79 func validateConfiguration(configuration *config.Config) error {
80 if configuration.InfoProducerHost == "" {
81 return fmt.Errorf("missing INFO_PRODUCER_HOST")
83 if configuration.ProducerCertPath == "" || configuration.ProducerKeyPath == "" {
84 return fmt.Errorf("missing PRODUCER_CERT and/or PRODUCER_KEY")
88 func registerTypesAndProducer(jobTypesHandler jobs.JobTypesManager, infoCoordinatorAddress string, callbackAddress string, client restclient.HTTPClient) error {
89 registrator := config.NewRegistratorImpl(infoCoordinatorAddress, client)
90 configTypes, err := config.GetJobTypesFromConfiguration("configs/type_config.json")
92 return fmt.Errorf("unable to register all types due to: %v", err)
94 regErr := registrator.RegisterTypes(jobTypesHandler.LoadTypesFromConfiguration(configTypes))
96 return fmt.Errorf("unable to register all types due to: %v", regErr)
99 producer := config.ProducerRegistrationInfo{
100 InfoProducerSupervisionCallbackUrl: callbackAddress + server.StatusPath,
101 SupportedInfoTypes: jobTypesHandler.GetSupportedTypes(),
102 InfoJobCallbackUrl: callbackAddress + server.AddJobPath,
104 if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil {
105 return fmt.Errorf("unable to register producer due to: %v", err)
110 func keepProducerAlive() {
111 forever := make(chan int)