Merge "Binding interface IP address for REST Server" v0.9.2
authorJuha Hyttinen <juha.hyttinen@nokia.com>
Mon, 27 Sep 2021 04:42:19 +0000 (04:42 +0000)
committerGerrit Code Review <gerrit@o-ran-sc.org>
Mon, 27 Sep 2021 04:42:19 +0000 (04:42 +0000)
1  2 
pkg/xapp/xapp.go

diff --combined pkg/xapp/xapp.go
@@@ -23,6 -23,7 +23,7 @@@ import 
        "bytes"
        "encoding/json"
        "fmt"
+       "net"
        "net/http"
        "os"
        "os/signal"
@@@ -48,7 -49,6 +49,7 @@@ var 
        // XApp is an application instance
        Rmr           *RMRClient
        Sdl           *SDLClient
 +      SdlStorage    *SDLStorage
        Rnib          *RNIBClient
        Resource      *Router
        Metric        *Metrics
@@@ -65,7 -65,7 +66,7 @@@
  )
  
  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{}) {
@@@ -263,25 -263,52 +264,53 @@@ 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 getIpAdress() string {
+       var ip net.IP
+       itf, err := net.InterfaceByName(os.Getenv("INTERFACE_NAME"))
+       if err != nil {
+               Logger.Info("Interface name is not able to resolve " + err.Error())
+               return ip.String()
+       }
+       item, err := itf.Addrs()
+       if err != nil {
+               Logger.Info("IP address is not able to resolve " + err.Error())
+               return ip.String()
+       }
+       for _, addr := range item {
+               switch v := addr.(type) {
+               case *net.IPNet:
+                       if !v.IP.IsLinkLocalUnicast() {
+                               ip = v.IP
+                       }
+               }
+       }
+       return ip.String()
+ }
  func RunWithParams(c MessageConsumer, sdlcheck bool) {
        Rmr = NewRMRClient()
  
        Rmr.SetReadyCB(XappReadyCb, nil)
-       host := fmt.Sprintf(":%d", GetPortData("http").Port)
+       ipString := getIpAdress()
+       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()