69d29649179c322b551bd03283ecddf0165b0f25
[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         UeNib    *UENIBClient
35         Rnib     *RNIBClient
36         Resource *Router
37         Metric   *Metrics
38         Logger   *Log
39         Config   Configurator
40 )
41
42 func IsReady() bool {
43         return Rmr.IsReady() && Sdl.IsReady()
44 }
45
46 func SetReadyCB(cb ReadyCB, params interface{}) {
47         Rmr.SetReadyCB(cb, params)
48 }
49
50 func init() {
51         // Load xapp configuration
52         Logger = LoadConfig()
53
54         Logger.SetLevel(viper.GetInt("logger.level"))
55         Resource = NewRouter()
56         Config = Configurator{}
57         Metric = NewMetrics(viper.GetString("metrics.url"), viper.GetString("metrics.namespace"), Resource.router)
58         Rmr = NewRMRClient()
59         UeNib = NewUENIBClient()
60
61         if viper.IsSet("db.namespaces") {
62                 namespaces := viper.GetStringSlice("db.namespaces")
63                 if namespaces[0] != "" {
64                         Sdl = NewSDLClient(viper.GetStringSlice("db.namespaces")[0])
65                 }
66                 if namespaces[1] != "" {
67                         Rnib = NewRNIBClient(viper.GetStringSlice("db.namespaces")[1])
68                 }
69         } else {
70                 Sdl = NewSDLClient(viper.GetString("db.namespace"))
71         }
72 }
73
74 func Run(c MessageConsumer) {
75         go http.ListenAndServe(viper.GetString("local.host"), Resource.router)
76
77         Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", viper.GetString("local.host")))
78
79         Sdl.TestConnection()
80         Rmr.Start(c)
81 }