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 "github.com/gorilla/mux"
30 log "github.com/sirupsen/logrus"
31 _ "oransc.org/nonrtric/dmaapmediatorproducer/docs"
32 "oransc.org/nonrtric/dmaapmediatorproducer/internal/config"
33 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
34 "oransc.org/nonrtric/dmaapmediatorproducer/internal/kafkaclient"
35 "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
36 "oransc.org/nonrtric/dmaapmediatorproducer/internal/server"
38 httpSwagger "github.com/swaggo/http-swagger"
41 var configuration *config.Config
45 configuration = config.New()
48 // @title DMaaP Mediator Producer
51 // @license.name Apache 2.0
52 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
55 log.SetLevel(configuration.LogLevel)
56 log.Debug("Initializing DMaaP Mediator Producer")
57 log.Debug("Using configuration: ", configuration)
58 if err := validateConfiguration(configuration); err != nil {
59 log.Fatalf("Stopping producer due to error: %v", err)
61 callbackAddress := fmt.Sprintf("%v:%v", configuration.InfoProducerHost, configuration.InfoProducerPort)
63 var cert tls.Certificate
64 if c, err := restclient.CreateClientCertificate(configuration.ProducerCertPath, configuration.ProducerKeyPath); err == nil {
67 log.Fatalf("Stopping producer due to error: %v", err)
70 retryClient := restclient.CreateRetryClient(cert)
71 kafkaFactory := kafkaclient.KafkaFactoryImpl{BootstrapServer: configuration.KafkaBootstrapServers}
72 distributionClient := restclient.CreateClientWithoutRetry(cert, 10*time.Second)
74 jobsManager := jobs.NewJobsManagerImpl(retryClient, configuration.DMaaPMRAddress, kafkaFactory, distributionClient)
75 go startCallbackServer(jobsManager, callbackAddress)
77 if err := registerTypesAndProducer(jobsManager, configuration.InfoCoordinatorAddress, callbackAddress, retryClient); err != nil {
78 log.Fatalf("Stopping producer due to: %v", err)
81 jobsManager.StartJobsForAllTypes()
83 log.Debug("Starting DMaaP Mediator Producer")
88 func validateConfiguration(configuration *config.Config) error {
89 if configuration.InfoProducerHost == "" {
90 return fmt.Errorf("missing INFO_PRODUCER_HOST")
92 if configuration.ProducerCertPath == "" || configuration.ProducerKeyPath == "" {
93 return fmt.Errorf("missing PRODUCER_CERT and/or PRODUCER_KEY")
95 if configuration.DMaaPMRAddress == "" && configuration.KafkaBootstrapServers == "" {
96 return fmt.Errorf("at least one of DMAAP_MR_ADDR or KAFKA_BOOTSRAP_SERVERS must be provided")
100 func registerTypesAndProducer(jobTypesManager jobs.JobTypesManager, infoCoordinatorAddress string, callbackAddress string, client restclient.HTTPClient) error {
101 registrator := config.NewRegistratorImpl(infoCoordinatorAddress, client)
102 configTypes, err := config.GetJobTypesFromConfiguration("configs")
104 return fmt.Errorf("unable to register all types due to: %v", err)
106 regErr := registrator.RegisterTypes(jobTypesManager.LoadTypesFromConfiguration(configTypes))
108 return fmt.Errorf("unable to register all types due to: %v", regErr)
111 producer := config.ProducerRegistrationInfo{
112 InfoProducerSupervisionCallbackUrl: callbackAddress + server.HealthCheckPath,
113 SupportedInfoTypes: jobTypesManager.GetSupportedTypes(),
114 InfoJobCallbackUrl: callbackAddress + server.AddJobPath,
116 if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil {
117 return fmt.Errorf("unable to register producer due to: %v", err)
122 func startCallbackServer(jobsManager jobs.JobsManager, callbackAddress string) {
123 log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort)
124 r := server.NewRouter(jobsManager, statusHandler)
126 if restclient.IsUrlSecure(callbackAddress) {
127 log.Fatalf("Server stopped: %v", http.ListenAndServeTLS(fmt.Sprintf(":%v", configuration.InfoProducerPort), configuration.ProducerCertPath, configuration.ProducerKeyPath, r))
129 log.Fatalf("Server stopped: %v", http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), r))
133 // @Summary Get status
134 // @Description Get the status of the producer. Will show if the producer has registered in ICS.
135 // @Tags Data producer (callbacks)
137 // @Router /health_check [get]
138 func statusHandler(w http.ResponseWriter, r *http.Request) {
139 registeredStatus := "not registered"
141 registeredStatus = "registered"
143 fmt.Fprintf(w, `{"status": "%v"}`, registeredStatus)
146 // @Summary Get Swagger Documentation
147 // @Description Get the Swagger API documentation for the producer.
150 // @Router /swagger [get]
151 func addSwaggerHandler(r *mux.Router) {
152 r.PathPrefix("/swagger").Handler(httpSwagger.WrapHandler)
155 func keepProducerAlive() {
156 forever := make(chan int)