elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 1 | // - |
| 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 | |
| 21 | package main |
| 22 | |
| 23 | import ( |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 24 | "fmt" |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 25 | "net/http" |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 26 | "sync" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 27 | |
| 28 | log "github.com/sirupsen/logrus" |
| 29 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/config" |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 30 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs" |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 31 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/server" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var configuration *config.Config |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 35 | var callbackAddress string |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 36 | |
| 37 | func 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") |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 46 | if configuration.InfoProducerHost == "" { |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 47 | log.Fatal("Missing INFO_PRODUCER_SUPERVISION_CALLBACK_HOST") |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 48 | } |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 49 | callbackAddress = fmt.Sprintf("%v:%v", configuration.InfoProducerHost, configuration.InfoProducerPort) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 50 | |
| 51 | registrator := config.NewRegistratorImpl(configuration.InfoCoordinatorAddress) |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 52 | if types, err := jobs.GetTypes(); err == nil { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 53 | 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 | } |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 59 | producer := config.ProducerRegistrationInfo{ |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 60 | InfoProducerSupervisionCallbackUrl: callbackAddress + server.StatusCallbackPath, |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 61 | SupportedInfoTypes: jobs.GetSupportedTypes(), |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 62 | InfoJobCallbackUrl: callbackAddress + server.JobsCallbackPath, |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 63 | } |
| 64 | if err := registrator.RegisterProducer("DMaaP_Mediator_Producer", &producer); err != nil { |
| 65 | log.Fatalf("Unable to register producer due to: %v", err) |
| 66 | } |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func main() { |
| 70 | log.Debug("Starting DMaaP Mediator Producer") |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 71 | wg := new(sync.WaitGroup) |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 72 | |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 73 | // add two goroutines to `wg` WaitGroup, one for each running go routine |
| 74 | wg.Add(2) |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 75 | |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 76 | log.Debugf("Starting callback server at port %v", configuration.InfoProducerPort) |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 77 | go func() { |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 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)) |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 81 | wg.Done() |
| 82 | }() |
| 83 | |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 84 | go func() { |
| 85 | jobs.RunJobs(fmt.Sprintf("%v:%v", configuration.MRHost, configuration.MRPort)) |
| 86 | wg.Done() |
| 87 | }() |
| 88 | |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 89 | // wait until WaitGroup is done |
| 90 | wg.Wait() |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 91 | log.Debug("Stopping DMaaP Mediator Producer") |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 92 | } |