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 const timeoutHTTPClient = time.Second * 5
37 const timeoutPollClient = time.Second * 15
39 var configuration *config.Config
40 var httpClient restclient.HTTPClient
41 var jobHandler *jobs.JobHandlerImpl
44 configuration = config.New()
48 log.SetLevel(configuration.LogLevel)
49 log.Debug("Initializing DMaaP Mediator Producer")
50 if err := validateConfiguration(configuration); err != nil {
51 log.Fatalf("Stopping producer due to error: %v", err)
53 callbackAddress := fmt.Sprintf("%v:%v", configuration.InfoProducerHost, configuration.InfoProducerPort)
55 httpClient = &http.Client{
56 Timeout: timeoutHTTPClient,
58 pollClient := &http.Client{
59 Timeout: timeoutPollClient,
61 jobHandler = jobs.NewJobHandlerImpl("configs/type_config.json", pollClient, httpClient)
62 if err := registerTypesAndProducer(jobHandler, configuration.InfoCoordinatorAddress, callbackAddress); err != nil {
63 log.Fatalf("Stopping producer due to: %v", err)
66 log.Debug("Starting DMaaP Mediator Producer")
67 wg := new(sync.WaitGroup)
69 // add two goroutines to `wg` WaitGroup, one for each running go routine
72 log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort)
74 r := server.NewRouter(jobHandler)
75 log.Warn(http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), r))
80 jobHandler.RunJobs(fmt.Sprintf("%v:%v", configuration.MRHost, configuration.MRPort))
84 // wait until WaitGroup is done
86 log.Debug("Stopping DMaaP Mediator Producer")
89 func validateConfiguration(configuration *config.Config) error {
90 if configuration.InfoProducerHost == "" {
91 return fmt.Errorf("missing INFO_PRODUCER_HOST")
96 func registerTypesAndProducer(jobHandler jobs.JobTypeHandler, infoCoordinatorAddress string, callbackAddress string) error {
97 registrator := config.NewRegistratorImpl(infoCoordinatorAddress, httpClient)
98 if types, err := jobHandler.GetTypes(); err == nil {
99 if regErr := registrator.RegisterTypes(types); regErr != nil {
100 return fmt.Errorf("unable to register all types due to: %v", regErr)
103 return fmt.Errorf("unable to get types to register due to: %v", err)
105 producer := config.ProducerRegistrationInfo{
106 InfoProducerSupervisionCallbackUrl: callbackAddress + server.StatusPath,
107 SupportedInfoTypes: jobHandler.GetSupportedTypes(),
108 InfoJobCallbackUrl: callbackAddress + server.AddJobPath,
110 if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil {
111 return fmt.Errorf("unable to register producer due to: %v", err)