Initial version
[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 var (
29         // XApp is an application instance
30         Rmr      *RMRClient
31         Sdl      *SDLClient
32         UeNib    *UENIBClient
33         Rnib     *RNIBClient
34         Resource *Router
35         Metric   *Metrics
36         Logger   Log
37         Config   Configurator
38 )
39
40 func init() {
41         // Load xapp configuration
42         Logger = LoadConfig()
43
44         Logger.SetLevel(viper.GetInt("logger.level"))
45         Rmr = NewRMRClient()
46         Resource = NewRouter()
47         Config = Configurator{}
48         UeNib = NewUENIBClient()
49         Metric = NewMetrics(viper.GetString("metrics.url"), viper.GetString("metrics.namespace"), Resource.router)
50
51         if viper.IsSet("db.namespaces") {
52                 namespaces := viper.GetStringSlice("db.namespaces")
53                 if namespaces[0] != "" {
54                         Sdl = NewSDLClient(viper.GetStringSlice("db.namespaces")[0])
55                 }
56                 if namespaces[1] != "" {
57                         Rnib = NewRNIBClient(viper.GetStringSlice("db.namespaces")[1])
58                 }
59         } else {
60                 Sdl = NewSDLClient(viper.GetString("db.namespace"))
61         }
62 }
63
64 func Run(c MessageConsumer) {
65         go http.ListenAndServe(viper.GetString("local.host"), Resource.router)
66
67         Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", viper.GetString("local.host")))
68
69         Sdl.TestConnection()
70         Rmr.Start(c)
71 }