Defer alarm sysrem init
[ric-plt/xapp-frame.git] / pkg / xapp / xapp.go
index c51e0ac..0ae9b02 100755 (executable)
@@ -25,25 +25,38 @@ import (
        "net/http"
 )
 
-type ReadyCB func()
+type ReadyCB func(interface{})
 
 var (
        // XApp is an application instance
-       Rmr      *RMRClient
-       Sdl      *SDLClient
-       Rnib     *RNIBClient
-       Resource *Router
-       Metric   *Metrics
-       Logger   *Log
-       Config   Configurator
+       Rmr           *RMRClient
+       Sdl           *SDLClient
+       Rnib          *RNIBClient
+       Resource      *Router
+       Metric        *Metrics
+       Logger        *Log
+       Config        Configurator
+       Subscription  *Subscriber
+       Alarm         *AlarmClient
+       readyCb       ReadyCB
+       readyCbParams interface{}
 )
 
 func IsReady() bool {
-       return Rmr.IsReady() && Sdl.IsReady()
+       return Rmr != nil && Rmr.IsReady() && Sdl != nil && Sdl.IsReady()
 }
 
-func SetReadyCB(cb ReadyCB) {
-       Rmr.SetReadyCB(cb)
+func SetReadyCB(cb ReadyCB, params interface{}) {
+       readyCb = cb
+       readyCbParams = params
+}
+
+func xappReadyCb(params interface{}) {
+       Alarm = NewAlarmClient(viper.GetString("alarm.MOId"), viper.GetString("alarm.APPId"))
+
+       if readyCb != nil {
+               readyCb(readyCbParams)
+       }
 }
 
 func init() {
@@ -54,14 +67,14 @@ func init() {
        Resource = NewRouter()
        Config = Configurator{}
        Metric = NewMetrics(viper.GetString("metrics.url"), viper.GetString("metrics.namespace"), Resource.router)
-       Rmr = NewRMRClient()
+       Subscription = NewSubscriber(viper.GetString("subscription.host"), viper.GetInt("subscription.timeout"))
 
        if viper.IsSet("db.namespaces") {
                namespaces := viper.GetStringSlice("db.namespaces")
-               if namespaces[0] != "" {
+               if len(namespaces) > 0 && namespaces[0] != "" {
                        Sdl = NewSDLClient(viper.GetStringSlice("db.namespaces")[0])
                }
-               if namespaces[1] != "" {
+               if len(namespaces) > 1 && namespaces[1] != "" {
                        Rnib = NewRNIBClient(viper.GetStringSlice("db.namespaces")[1])
                }
        } else {
@@ -69,11 +82,17 @@ func init() {
        }
 }
 
-func Run(c MessageConsumer) {
+func RunWithParams(c MessageConsumer, sdlcheck bool) {
+       Rmr = NewRMRClient()
+       Rmr.SetReadyCB(xappReadyCb, nil)
        go http.ListenAndServe(viper.GetString("local.host"), Resource.router)
-
        Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", viper.GetString("local.host")))
-
-       Sdl.TestConnection()
+       if sdlcheck {
+               Sdl.TestConnection()
+       }
        Rmr.Start(c)
 }
+
+func Run(c MessageConsumer) {
+       RunWithParams(c, true)
+}