blob: 79fcb6b5881a9abbd77eb0d8929e0c75dbea4f22 [file] [log] [blame]
elinuxhenrikcce95ff2021-09-05 17:27:02 +02001// -
2// ========================LICENSE_START=================================
3// O-RAN-SC
4// %%
5// Copyright (C) 2021: Nordix Foundation
6// %%
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
10//
11// http://www.apache.org/licenses/LICENSE-2.0
12//
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===================================
19//
20
21package main
22
23import (
elinuxhenrikba96d842021-09-06 16:05:01 +020024 "fmt"
elinuxhenrik382870d2021-09-23 11:09:09 +020025 "net/http"
elinuxhenrik63a42ca2021-09-06 22:16:24 +020026 "sync"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020027
28 log "github.com/sirupsen/logrus"
29 "oransc.org/nonrtric/dmaapmediatorproducer/internal/config"
elinuxhenrik63a42ca2021-09-06 22:16:24 +020030 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
elinuxhenrikba96d842021-09-06 16:05:01 +020031 "oransc.org/nonrtric/dmaapmediatorproducer/internal/server"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020032)
33
34var configuration *config.Config
elinuxhenrik382870d2021-09-23 11:09:09 +020035var callbackAddress string
elinuxhenrikcce95ff2021-09-05 17:27:02 +020036
37func init() {
38 configuration = config.New()
39 if loglevel, err := log.ParseLevel(configuration.LogLevel); err == nil {
40 log.SetLevel(loglevel)
41 } else {
42 log.Warnf("Invalid log level: %v. Log level will be Info!", configuration.LogLevel)
43 }
44
45 log.Debug("Initializing DMaaP Mediator Producer")
elinuxhenrik382870d2021-09-23 11:09:09 +020046 if configuration.InfoProducerHost == "" {
elinuxhenrikba96d842021-09-06 16:05:01 +020047 log.Fatal("Missing INFO_PRODUCER_SUPERVISION_CALLBACK_HOST")
elinuxhenrika77cd652021-09-06 10:56:21 +020048 }
elinuxhenrik382870d2021-09-23 11:09:09 +020049 callbackAddress = fmt.Sprintf("%v:%v", configuration.InfoProducerHost, configuration.InfoProducerPort)
elinuxhenrikcce95ff2021-09-05 17:27:02 +020050
51 registrator := config.NewRegistratorImpl(configuration.InfoCoordinatorAddress)
elinuxhenrik63a42ca2021-09-06 22:16:24 +020052 if types, err := jobs.GetTypes(); err == nil {
elinuxhenrikcce95ff2021-09-05 17:27:02 +020053 if regErr := registrator.RegisterTypes(types); regErr != nil {
54 log.Fatalf("Unable to register all types due to: %v", regErr)
55 }
56 } else {
57 log.Fatalf("Unable to get types to register due to: %v", err)
58 }
elinuxhenrika77cd652021-09-06 10:56:21 +020059 producer := config.ProducerRegistrationInfo{
elinuxhenrik382870d2021-09-23 11:09:09 +020060 InfoProducerSupervisionCallbackUrl: callbackAddress + server.StatusCallbackPath,
elinuxhenrik63a42ca2021-09-06 22:16:24 +020061 SupportedInfoTypes: jobs.GetSupportedTypes(),
elinuxhenrik382870d2021-09-23 11:09:09 +020062 InfoJobCallbackUrl: callbackAddress + server.JobsCallbackPath,
elinuxhenrika77cd652021-09-06 10:56:21 +020063 }
64 if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil {
65 log.Fatalf("Unable to register producer due to: %v", err)
66 }
elinuxhenrikcce95ff2021-09-05 17:27:02 +020067}
68
69func main() {
70 log.Debug("Starting DMaaP Mediator Producer")
elinuxhenrik63a42ca2021-09-06 22:16:24 +020071 wg := new(sync.WaitGroup)
elinuxhenrikba96d842021-09-06 16:05:01 +020072
elinuxhenrik382870d2021-09-23 11:09:09 +020073 // add two goroutines to `wg` WaitGroup, one for each running go routine
74 wg.Add(2)
elinuxhenrik63a42ca2021-09-06 22:16:24 +020075
elinuxhenrik382870d2021-09-23 11:09:09 +020076 log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort)
elinuxhenrik63a42ca2021-09-06 22:16:24 +020077 go func() {
elinuxhenrik382870d2021-09-23 11:09:09 +020078 http.HandleFunc(server.StatusCallbackPath, server.StatusHandler)
79 http.HandleFunc(server.JobsCallbackPath, server.CreateInfoJobHandler)
80 log.Warn(http.ListenAndServe(fmt.Sprintf(":%v", configuration.InfoProducerPort), nil))
elinuxhenrik63a42ca2021-09-06 22:16:24 +020081 wg.Done()
82 }()
83
elinuxhenrik28038562021-09-21 15:43:11 +020084 go func() {
85 jobs.RunJobs(fmt.Sprintf("%v:%v", configuration.MRHost, configuration.MRPort))
86 wg.Done()
87 }()
88
elinuxhenrik63a42ca2021-09-06 22:16:24 +020089 // wait until WaitGroup is done
90 wg.Wait()
elinuxhenrikba96d842021-09-06 16:05:01 +020091 log.Debug("Stopping DMaaP Mediator Producer")
elinuxhenrikcce95ff2021-09-05 17:27:02 +020092}