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===================================
28 log "github.com/sirupsen/logrus"
29 "oransc.org/nonrtric/dmaapmediatorproducer/internal/config"
30 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
31 "oransc.org/nonrtric/dmaapmediatorproducer/internal/server"
34 var configuration *config.Config
35 var callbackAddress string
38 configuration = config.New()
39 if loglevel, err := log.ParseLevel(configuration.LogLevel); err == nil {
40 log.SetLevel(loglevel)
42 log.Warnf("Invalid log level: %v. Log level will be Info!", configuration.LogLevel)
45 log.Debug("Initializing DMaaP Mediator Producer")
46 if configuration.InfoProducerHost == "" {
47 log.Fatal("Missing INFO_PRODUCER_SUPERVISION_CALLBACK_HOST")
49 callbackAddress = fmt.Sprintf("%v:%v", configuration.InfoProducerHost, configuration.InfoProducerPort)
51 registrator := config.NewRegistratorImpl(configuration.InfoCoordinatorAddress)
52 if types, err := jobs.GetTypes(); err == nil {
53 if regErr := registrator.RegisterTypes(types); regErr != nil {
54 log.Fatalf("Unable to register all types due to: %v", regErr)
57 log.Fatalf("Unable to get types to register due to: %v", err)
59 producer := config.ProducerRegistrationInfo{
60 InfoProducerSupervisionCallbackUrl: callbackAddress + server.StatusCallbackPath,
61 SupportedInfoTypes: jobs.GetSupportedTypes(),
62 InfoJobCallbackUrl: callbackAddress + server.JobsCallbackPath,
64 if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil {
65 log.Fatalf("Unable to register producer due to: %v", err)
70 log.Debug("Starting DMaaP Mediator Producer")
71 wg := new(sync.WaitGroup)
73 // add two goroutines to `wg` WaitGroup, one for each running go routine
76 log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort)
78 http.HandleFunc(server.StatusCallbackPath, server.StatusHandler)
79 http.HandleFunc(server.JobsCallbackPath, server.CreateInfoJobHandler)
80 log.Warn(http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), nil))
85 jobs.RunJobs(fmt.Sprintf("%v:%v", configuration.MRHost, configuration.MRPort))
89 // wait until WaitGroup is done
91 log.Debug("Stopping DMaaP Mediator Producer")