Merge "Minor bugs fix in ORU-app simulator" into e-release
[nonrtric.git] / dmaap-mediator-producer / main.go
index 194ed75..65a84a2 100644 (file)
@@ -22,23 +22,36 @@ package main
 
 import (
        "crypto/tls"
+       "encoding/json"
        "fmt"
        "net/http"
        "time"
 
+       "github.com/gorilla/mux"
        log "github.com/sirupsen/logrus"
+       _ "oransc.org/nonrtric/dmaapmediatorproducer/api"
        "oransc.org/nonrtric/dmaapmediatorproducer/internal/config"
        "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
+       "oransc.org/nonrtric/dmaapmediatorproducer/internal/kafkaclient"
        "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
        "oransc.org/nonrtric/dmaapmediatorproducer/internal/server"
+
+       httpSwagger "github.com/swaggo/http-swagger"
 )
 
 var configuration *config.Config
+var registered bool
 
 func init() {
        configuration = config.New()
 }
 
+// @title    DMaaP Mediator Producer
+// @version  1.1.0
+
+// @license.name  Apache 2.0
+// @license.url   http://www.apache.org/licenses/LICENSE-2.0.html
+
 func main() {
        log.SetLevel(configuration.LogLevel)
        log.Debug("Initializing DMaaP Mediator Producer")
@@ -54,24 +67,21 @@ func main() {
        } else {
                log.Fatalf("Stopping producer due to error: %v", err)
        }
+
        retryClient := restclient.CreateRetryClient(cert)
+       kafkaFactory := kafkaclient.KafkaFactoryImpl{BootstrapServer: configuration.KafkaBootstrapServers}
+       distributionClient := restclient.CreateClientWithoutRetry(cert, 10*time.Second)
+
+       jobsManager := jobs.NewJobsManagerImpl(retryClient, configuration.DMaaPMRAddress, kafkaFactory, distributionClient)
+       go startCallbackServer(jobsManager, callbackAddress)
 
-       jobsManager := jobs.NewJobsManagerImpl("configs/type_config.json", retryClient, configuration.DMaaPMRAddress, restclient.CreateClientWithoutRetry(cert, 5*time.Second))
        if err := registerTypesAndProducer(jobsManager, configuration.InfoCoordinatorAddress, callbackAddress, retryClient); err != nil {
                log.Fatalf("Stopping producer due to: %v", err)
        }
+       registered = true
        jobsManager.StartJobsForAllTypes()
 
        log.Debug("Starting DMaaP Mediator Producer")
-       go func() {
-               log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort)
-               r := server.NewRouter(jobsManager)
-               if restclient.IsUrlSecure(callbackAddress) {
-                       log.Fatalf("Server stopped: %v", http.ListenAndServeTLS(fmt.Sprintf(":%v", configuration.InfoProducerPort), configuration.ProducerCertPath, configuration.ProducerKeyPath, r))
-               } else {
-                       log.Fatalf("Server stopped: %v", http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), r))
-               }
-       }()
 
        keepProducerAlive()
 }
@@ -83,20 +93,25 @@ func validateConfiguration(configuration *config.Config) error {
        if configuration.ProducerCertPath == "" || configuration.ProducerKeyPath == "" {
                return fmt.Errorf("missing PRODUCER_CERT and/or PRODUCER_KEY")
        }
+       if configuration.DMaaPMRAddress == "" && configuration.KafkaBootstrapServers == "" {
+               return fmt.Errorf("at least one of DMAAP_MR_ADDR or KAFKA_BOOTSRAP_SERVERS must be provided")
+       }
        return nil
 }
-func registerTypesAndProducer(jobTypesHandler jobs.JobTypesManager, infoCoordinatorAddress string, callbackAddress string, client restclient.HTTPClient) error {
+func registerTypesAndProducer(jobTypesManager jobs.JobTypesManager, infoCoordinatorAddress string, callbackAddress string, client restclient.HTTPClient) error {
        registrator := config.NewRegistratorImpl(infoCoordinatorAddress, client)
-       if types, err := jobTypesHandler.LoadTypesFromConfiguration(); err == nil {
-               if regErr := registrator.RegisterTypes(types); regErr != nil {
-                       return fmt.Errorf("unable to register all types due to: %v", regErr)
-               }
-       } else {
-               return fmt.Errorf("unable to get types to register due to: %v", err)
+       configTypes, err := config.GetJobTypesFromConfiguration("configs")
+       if err != nil {
+               return fmt.Errorf("unable to register all types due to: %v", err)
+       }
+       regErr := registrator.RegisterTypes(jobTypesManager.LoadTypesFromConfiguration(configTypes))
+       if regErr != nil {
+               return fmt.Errorf("unable to register all types due to: %v", regErr)
        }
+
        producer := config.ProducerRegistrationInfo{
-               InfoProducerSupervisionCallbackUrl: callbackAddress + server.StatusPath,
-               SupportedInfoTypes:                 jobTypesHandler.GetSupportedTypes(),
+               InfoProducerSupervisionCallbackUrl: callbackAddress + server.HealthCheckPath,
+               SupportedInfoTypes:                 jobTypesManager.GetSupportedTypes(),
                InfoJobCallbackUrl:                 callbackAddress + server.AddJobPath,
        }
        if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil {
@@ -105,6 +120,47 @@ func registerTypesAndProducer(jobTypesHandler jobs.JobTypesManager, infoCoordina
        return nil
 }
 
+func startCallbackServer(jobsManager jobs.JobsManager, callbackAddress string) {
+       log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort)
+       r := server.NewRouter(jobsManager, statusHandler)
+       addSwaggerHandler(r)
+       if restclient.IsUrlSecure(callbackAddress) {
+               log.Fatalf("Server stopped: %v", http.ListenAndServeTLS(fmt.Sprintf(":%v", configuration.InfoProducerPort), configuration.ProducerCertPath, configuration.ProducerKeyPath, r))
+       } else {
+               log.Fatalf("Server stopped: %v", http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), r))
+       }
+}
+
+type ProducerStatus struct {
+       // The registration status of the producer in Information Coordinator Service. Either `registered` or `not registered`
+       RegisteredStatus string `json:"registeredStatus" swaggertype:"string" example:"registered"`
+} // @name  ProducerStatus
+
+// @Summary      Get status
+// @Description  Get the status of the producer. Will show if the producer has registered in ICS.
+// @Tags         Data producer (callbacks)
+// @Produce      json
+// @Success      200  {object}  ProducerStatus
+// @Router       /health_check [get]
+func statusHandler(w http.ResponseWriter, r *http.Request) {
+       status := ProducerStatus{
+               RegisteredStatus: "not registered",
+       }
+       if registered {
+               status.RegisteredStatus = "registered"
+       }
+       json.NewEncoder(w).Encode(status)
+}
+
+// @Summary      Get Swagger Documentation
+// @Description  Get the Swagger API documentation for the producer.
+// @Tags         Admin
+// @Success      200
+// @Router       /swagger [get]
+func addSwaggerHandler(r *mux.Router) {
+       r.PathPrefix("/swagger").Handler(httpSwagger.WrapHandler)
+}
+
 func keepProducerAlive() {
        forever := make(chan int)
        <-forever