0ae9b02c0e51a8c017bda4f0d1124b0e5818bdb7
[ric-plt/xapp-frame.git] / pkg / xapp / xapp.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package xapp
21
22 import (
23         "fmt"
24         "github.com/spf13/viper"
25         "net/http"
26 )
27
28 type ReadyCB func(interface{})
29
30 var (
31         // XApp is an application instance
32         Rmr           *RMRClient
33         Sdl           *SDLClient
34         Rnib          *RNIBClient
35         Resource      *Router
36         Metric        *Metrics
37         Logger        *Log
38         Config        Configurator
39         Subscription  *Subscriber
40         Alarm         *AlarmClient
41         readyCb       ReadyCB
42         readyCbParams interface{}
43 )
44
45 func IsReady() bool {
46         return Rmr != nil && Rmr.IsReady() && Sdl != nil && Sdl.IsReady()
47 }
48
49 func SetReadyCB(cb ReadyCB, params interface{}) {
50         readyCb = cb
51         readyCbParams = params
52 }
53
54 func xappReadyCb(params interface{}) {
55         Alarm = NewAlarmClient(viper.GetString("alarm.MOId"), viper.GetString("alarm.APPId"))
56
57         if readyCb != nil {
58                 readyCb(readyCbParams)
59         }
60 }
61
62 func init() {
63         // Load xapp configuration
64         Logger = LoadConfig()
65
66         Logger.SetLevel(viper.GetInt("logger.level"))
67         Resource = NewRouter()
68         Config = Configurator{}
69         Metric = NewMetrics(viper.GetString("metrics.url"), viper.GetString("metrics.namespace"), Resource.router)
70         Subscription = NewSubscriber(viper.GetString("subscription.host"), viper.GetInt("subscription.timeout"))
71
72         if viper.IsSet("db.namespaces") {
73                 namespaces := viper.GetStringSlice("db.namespaces")
74                 if len(namespaces) > 0 && namespaces[0] != "" {
75                         Sdl = NewSDLClient(viper.GetStringSlice("db.namespaces")[0])
76                 }
77                 if len(namespaces) > 1 && namespaces[1] != "" {
78                         Rnib = NewRNIBClient(viper.GetStringSlice("db.namespaces")[1])
79                 }
80         } else {
81                 Sdl = NewSDLClient(viper.GetString("db.namespace"))
82         }
83 }
84
85 func RunWithParams(c MessageConsumer, sdlcheck bool) {
86         Rmr = NewRMRClient()
87         Rmr.SetReadyCB(xappReadyCb, nil)
88         go http.ListenAndServe(viper.GetString("local.host"), Resource.router)
89         Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", viper.GetString("local.host")))
90         if sdlcheck {
91                 Sdl.TestConnection()
92         }
93         Rmr.Start(c)
94 }
95
96 func Run(c MessageConsumer) {
97         RunWithParams(c, true)
98 }