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===================================
30 "github.com/gorilla/mux"
31 log "github.com/sirupsen/logrus"
32 _ "oransc.org/nonrtric/dmaapmediatorproducer/api"
33 "oransc.org/nonrtric/dmaapmediatorproducer/internal/config"
34 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
35 "oransc.org/nonrtric/dmaapmediatorproducer/internal/kafkaclient"
36 "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
37 "oransc.org/nonrtric/dmaapmediatorproducer/internal/server"
39 httpSwagger "github.com/swaggo/http-swagger"
42 var configuration *config.Config
46 configuration = config.New()
49 // @title DMaaP Mediator Producer
52 // @license.name Apache 2.0
53 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
56 log.SetLevel(configuration.LogLevel)
57 log.Debug("Initializing DMaaP Mediator Producer")
58 log.Debug("Using configuration: ", configuration)
59 if err := validateConfiguration(configuration); err != nil {
60 log.Fatalf("Stopping producer due to error: %v", err)
62 callbackAddress := fmt.Sprintf("%v:%v", configuration.InfoProducerHost, configuration.InfoProducerPort)
64 var cert tls.Certificate
65 if c, err := restclient.CreateClientCertificate(configuration.ProducerCertPath, configuration.ProducerKeyPath); err == nil {
68 log.Fatalf("Stopping producer due to error: %v", err)
71 retryClient := restclient.CreateRetryClient(cert)
72 kafkaFactory := kafkaclient.KafkaFactoryImpl{BootstrapServer: configuration.KafkaBootstrapServers}
73 distributionClient := restclient.CreateClientWithoutRetry(cert, 10*time.Second)
75 jobsManager := jobs.NewJobsManagerImpl(retryClient, configuration.DMaaPMRAddress, kafkaFactory, distributionClient)
76 go startCallbackServer(jobsManager, callbackAddress)
78 if err := registerTypesAndProducer(jobsManager, configuration.InfoCoordinatorAddress, callbackAddress, retryClient); err != nil {
79 log.Fatalf("Stopping producer due to: %v", err)
82 jobsManager.StartJobsForAllTypes()
84 log.Debug("Starting DMaaP Mediator Producer")
89 func validateConfiguration(configuration *config.Config) error {
90 if configuration.InfoProducerHost == "" {
91 return fmt.Errorf("missing INFO_PRODUCER_HOST")
93 if configuration.ProducerCertPath == "" || configuration.ProducerKeyPath == "" {
94 return fmt.Errorf("missing PRODUCER_CERT and/or PRODUCER_KEY")
96 if configuration.DMaaPMRAddress == "" && configuration.KafkaBootstrapServers == "" {
97 return fmt.Errorf("at least one of DMAAP_MR_ADDR or KAFKA_BOOTSRAP_SERVERS must be provided")
101 func registerTypesAndProducer(jobTypesManager jobs.JobTypesManager, infoCoordinatorAddress string, callbackAddress string, client restclient.HTTPClient) error {
102 registrator := config.NewRegistratorImpl(infoCoordinatorAddress, client)
103 configTypes, err := config.GetJobTypesFromConfiguration("configs")
105 return fmt.Errorf("unable to register all types due to: %v", err)
107 regErr := registrator.RegisterTypes(jobTypesManager.LoadTypesFromConfiguration(configTypes))
109 return fmt.Errorf("unable to register all types due to: %v", regErr)
112 producer := config.ProducerRegistrationInfo{
113 InfoProducerSupervisionCallbackUrl: callbackAddress + server.HealthCheckPath,
114 SupportedInfoTypes: jobTypesManager.GetSupportedTypes(),
115 InfoJobCallbackUrl: callbackAddress + server.AddJobPath,
117 if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil {
118 return fmt.Errorf("unable to register producer due to: %v", err)
123 func startCallbackServer(jobsManager jobs.JobsManager, callbackAddress string) {
124 log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort)
125 r := server.NewRouter(jobsManager, statusHandler)
127 if restclient.IsUrlSecure(callbackAddress) {
128 log.Fatalf("Server stopped: %v", http.ListenAndServeTLS(fmt.Sprintf(":%v", configuration.InfoProducerPort), configuration.ProducerCertPath, configuration.ProducerKeyPath, r))
130 log.Fatalf("Server stopped: %v", http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), r))
134 type ProducerStatus struct {
135 // The registration status of the producer in Information Coordinator Service. Either `registered` or `not registered`
136 RegisteredStatus string `json:"registeredStatus" swaggertype:"string" example:"registered"`
137 } // @name ProducerStatus
139 // @Summary Get status
140 // @Description Get the status of the producer. Will show if the producer has registered in ICS.
141 // @Tags Data producer (callbacks)
143 // @Success 200 {object} ProducerStatus
144 // @Router /health_check [get]
145 func statusHandler(w http.ResponseWriter, r *http.Request) {
146 status := ProducerStatus{
147 RegisteredStatus: "not registered",
150 status.RegisteredStatus = "registered"
152 json.NewEncoder(w).Encode(status)
155 // @Summary Get Swagger Documentation
156 // @Description Get the Swagger API documentation for the producer.
159 // @Router /swagger [get]
160 func addSwaggerHandler(r *mux.Router) {
161 r.PathPrefix("/swagger").Handler(httpSwagger.WrapHandler)
164 func keepProducerAlive() {
165 forever := make(chan int)