Add some rmr library stats into metrics
[ric-plt/xapp-frame.git] / pkg / xapp / xapp.go
index e8f47ef..12f1628 100755 (executable)
@@ -23,17 +23,26 @@ import (
        "bytes"
        "encoding/json"
        "fmt"
+       "net"
        "net/http"
        "os"
        "os/signal"
        "strings"
        "sync/atomic"
        "syscall"
+       "testing"
        "time"
 
        "github.com/spf13/viper"
 )
 
+// For testing purpose go version 1.13 ->
+
+var _ = func() bool {
+       testing.Init()
+       return true
+}()
+
 type ReadyCB func(interface{})
 type ShutdownCB func()
 
@@ -41,6 +50,7 @@ var (
        // XApp is an application instance
        Rmr           *RMRClient
        Sdl           *SDLClient
+       SdlStorage    *SDLStorage
        Rnib          *RNIBClient
        Resource      *Router
        Metric        *Metrics
@@ -56,8 +66,18 @@ var (
        shutdownCnt   int32
 )
 
+var startTime time.Time
+
+func XappUpTime() time.Duration {
+       return time.Since(startTime)
+}
+
+func init() {
+       startTime = time.Now()
+}
+
 func IsReady() bool {
-       return Rmr != nil && Rmr.IsReady() && Sdl != nil && Sdl.IsReady()
+       return Rmr != nil && Rmr.IsReady() && SdlStorage != nil && SdlStorage.IsReady()
 }
 
 func SetReadyCB(cb ReadyCB, params interface{}) {
@@ -92,7 +112,7 @@ func registerXapp() {
        for {
                time.Sleep(5 * time.Second)
                if !IsHealthProbeReady() {
-                       Logger.Info("Application='%s' is not ready yet, waiting ...", viper.GetString("name"))
+                       Logger.Debug("Application='%s' is not ready yet, waiting ...", viper.GetString("name"))
                        continue
                }
 
@@ -146,6 +166,7 @@ func doRegister() error {
        xappversion := viper.GetString("version")
        pltNs := getPltNamespace("PLT_NAMESPACE", DEFAULT_PLT_NS)
 
+       //httpEp, rmrEp := getService(xappname, SERVICE_HTTP), getService(xappname, SERVICE_RMR)
        httpEp, rmrEp := getService(host, SERVICE_HTTP), getService(host, SERVICE_RMR)
        if httpEp == "" || rmrEp == "" {
                Logger.Warn("Couldn't resolve service endpoints: httpEp=%s rmrEp=%s", httpEp, rmrEp)
@@ -255,24 +276,54 @@ func init() {
        Config = Configurator{}
        Metric = NewMetrics(viper.GetString("metrics.url"), viper.GetString("metrics.namespace"), Resource.router)
        Subscription = NewSubscriber(viper.GetString("controls.subscription.host"), viper.GetInt("controls.subscription.timeout"))
+       SdlStorage = NewSdlStorage()
        Sdl = NewSDLClient(viper.GetString("controls.db.namespace"))
-       Rnib = NewRNIBClient()
+       Rnib = GetNewRnibClient(SdlStorage.db)
        Util = NewUtils()
 
        InstallSignalHandler()
 }
 
+func GetIpAddress() (string, error) {
+       ifname := os.Getenv("INTERFACE_NAME")
+       itf, err := net.InterfaceByName(ifname)
+       if err != nil {
+               return "<nil>", fmt.Errorf("Interface (%s) %w", ifname, err)
+       }
+       item, err := itf.Addrs()
+       if err != nil {
+               return "<nil>", fmt.Errorf("Interface (%s) %w", ifname, err)
+       }
+       for _, addr := range item {
+               switch v := addr.(type) {
+               case *net.IPNet:
+                       if !v.IP.IsLinkLocalUnicast() {
+                               return v.IP.String(), nil
+                       }
+               }
+       }
+       return "<nil>", fmt.Errorf("Interface (%s) couldn't find ip", ifname)
+}
+
 func RunWithParams(c MessageConsumer, sdlcheck bool) {
        Rmr = NewRMRClient()
 
        Rmr.SetReadyCB(XappReadyCb, nil)
-
-       host := fmt.Sprintf(":%d", GetPortData("http").Port)
+       ipString, err := GetIpAddress()
+       if err != nil {
+               Logger.Info("IP address is not able to resolve " + err.Error())
+       }
+       var host string
+       if ipString == "<nil>" {
+               host = fmt.Sprintf(":%d", GetPortData("http").Port)
+       } else {
+               host = fmt.Sprintf("[%s]:%d", ipString, GetPortData("http").Port)
+       }
        go http.ListenAndServe(host, Resource.router)
        Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", host))
 
        if sdlcheck {
-               Sdl.TestConnection()
+               SdlStorage.TestConnection(viper.GetString("controls.db.namespace"))
        }
        go registerXapp()