Corrections in UT build script for Sonar
[ric-plt/rtmgr.git] / pkg / nbi / httprestful.go
index ac56aa0..25be854 100644 (file)
@@ -36,6 +36,7 @@ import (
        "errors"
        "fmt"
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+       xfmodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
        "github.com/go-openapi/loads"
        "github.com/go-openapi/runtime/middleware"
        "net"
@@ -44,6 +45,7 @@ import (
        "routing-manager/pkg/models"
        "routing-manager/pkg/restapi"
        "routing-manager/pkg/restapi/operations"
+       "routing-manager/pkg/restapi/operations/debug"
        "routing-manager/pkg/restapi/operations/handle"
        "routing-manager/pkg/rpe"
        "routing-manager/pkg/rtmgr"
@@ -359,6 +361,23 @@ func deleteE2tHandleHandlerImpl(e2tdelchan chan<- *models.E2tDeleteData,
        return nil
 }
 
+func dumpDebugData() (models.Debuginfo, error) {
+       var response models.Debuginfo
+       sdlEngine, _ := sdl.GetSdl("file")
+       rpeEngine, _ := rpe.GetRpe("rmrpush")
+       data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile"))
+       if err != nil || data == nil {
+               xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
+               return response, err
+       }
+       response.RouteTable = *rpeEngine.GeneratePolicies(rtmgr.Eps, data)
+
+       prettyJSON, err := json.MarshalIndent(data, "", "")
+       response.RouteConfigs = string(prettyJSON)
+
+       return response, err
+}
+
 func launchRest(nbiif *string, datach chan<- *models.XappCallbackData, subchan chan<- *models.XappSubscriptionData, subupdatechan chan<- *rtmgr.XappList,
        subdelchan chan<- *models.XappSubscriptionData, e2taddchan chan<- *models.E2tData, assranchan chan<- models.RanE2tMap, disassranchan chan<- models.RanE2tMap, e2tdelchan chan<- *models.E2tDeleteData) {
        swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
@@ -470,6 +489,15 @@ func launchRest(nbiif *string, datach chan<- *models.XappCallbackData, subchan c
                                return handle.NewDeleteE2tHandleCreated()
                        }
                })
+       api.DebugGetDebuginfoHandler = debug.GetDebuginfoHandlerFunc(
+               func(params debug.GetDebuginfoParams) middleware.Responder {
+                       response, err := dumpDebugData()
+                       if err != nil {
+                               return debug.NewGetDebuginfoCreated()
+                       } else {
+                               return debug.NewGetDebuginfoOK().WithPayload(&response)
+                       }
+               })
        // start to serve API
        xapp.Logger.Info("Starting the HTTP Rest service")
        if err := server.Serve(); err != nil {
@@ -500,43 +528,150 @@ func httpGetXApps(xmurl string) (*[]rtmgr.XApp, error) {
        return nil, nil
 }
 
-func retrieveStartupData(xmurl string, nbiif string, fileName string, configfile string, sdlEngine sdl.Engine) error {
-       var readErr error
-       var maxRetries = 10
-       for i := 1; i <= maxRetries; i++ {
-               time.Sleep(2 * time.Second)
-               xappData, err := httpGetXApps(xmurl)
-               if xappData != nil && err == nil {
-                       pcData, confErr := rtmgr.GetPlatformComponents(configfile)
-                       if confErr != nil {
-                               xapp.Logger.Error(confErr.Error())
-                               return confErr
-                       }
-                       xapp.Logger.Info("Recieved intial xapp data and platform data, writing into SDL.")
-                       // Combine the xapps data and platform data before writing to the SDL
-                       ricData := &rtmgr.RicComponents{XApps: *xappData, Pcs: *pcData, E2Ts: make(map[string]rtmgr.E2TInstance)}
-                       writeErr := sdlEngine.WriteAll(fileName, ricData)
-                       if writeErr != nil {
-                               xapp.Logger.Error(writeErr.Error())
-                       }
-                       // post subscription req to appmgr
-                       readErr = PostSubReq(xmurl, nbiif)
-                       if readErr == nil {
-                               return nil
+func httpGetE2TList(e2murl string) (*[]rtmgr.E2tIdentity, error) {
+       xapp.Logger.Info("Invoked httprestful.httpGetE2TList: " + e2murl)
+       r, err := myClient.Get(e2murl)
+       if err != nil {
+               return nil, err
+       }
+       defer r.Body.Close()
+
+       if r.StatusCode == 200 {
+               xapp.Logger.Debug("http client raw response: %v", r)
+               var E2Tlist []rtmgr.E2tIdentity
+               err = json.NewDecoder(r.Body).Decode(&E2Tlist)
+               if err != nil {
+                       xapp.Logger.Warn("Json decode failed: " + err.Error())
+               }
+               xapp.Logger.Info("HTTP GET: OK")
+               xapp.Logger.Debug("httprestful.httpGetXApps returns: %v", E2Tlist)
+               return &E2Tlist, err
+       }
+       xapp.Logger.Warn("httprestful got an unexpected http status code: %v", r.StatusCode)
+       return nil, nil
+}
+
+func PopulateE2TMap(e2tDataList *[]rtmgr.E2tIdentity, e2ts map[string]rtmgr.E2TInstance, meids []string) {
+       xapp.Logger.Info("Invoked httprestful.PopulateE2TMap ")
+
+       for _, e2tData := range *e2tDataList {
+               var str string
+
+               e2tinst := rtmgr.E2TInstance{
+                       Ranlist: make([]string, len(e2tData.Rannames)),
+               }
+
+               e2tinst.Fqdn = e2tData.E2taddress
+               e2tinst.Name = "E2TERMINST"
+               copy(e2tinst.Ranlist, e2tData.Rannames)
+
+               if len(e2tData.Rannames) > 0 {
+                       var meidar string
+                       for _, meid := range e2tData.Rannames {
+                               meidar += meid + " "
                        }
+                       str += "mme_ar|" + e2tData.E2taddress + "|" + strings.TrimSuffix(meidar, " ")
+               }
+
+               e2ts[e2tinst.Fqdn] = e2tinst
+               meids = append(meids,str)
+       }
+}
+
+func retrieveStartupData(xmurl string, nbiif string, fileName string, configfile string, e2murl string, sdlEngine sdl.Engine) error {
+       xapp.Logger.Info("Invoked retrieveStartupData ")
+    var readErr error
+       var err  error
+   var maxRetries = 10
+       var xappData *[]rtmgr.XApp
+       xappData = new([]rtmgr.XApp)
+       xapp.Logger.Info("Trying to fetch XApps data from XAPP manager")
+    for i := 1; i <= maxRetries; i++ {
+       time.Sleep(2 * time.Second)
+
+          readErr = nil
+       xappData, err = httpGetXApps(xmurl)
+       if xappData != nil && err == nil {
+                       break
+       } else if err == nil {
+               readErr = errors.New("unexpected HTTP status code")
+       } else {
+               xapp.Logger.Warn("cannot get xapp data due to: " + err.Error())
+               readErr = err
+       }
+    }
+
+       if ( readErr != nil) {
+               return readErr
+       }
+
+       var meids []string
+       e2ts := make(map[string]rtmgr.E2TInstance)
+       xapp.Logger.Info("Trying to fetch E2T data from E2manager")
+        for i := 1; i <= maxRetries; i++ {
+
+               readErr = nil
+               e2tDataList, err := httpGetE2TList(e2murl)
+               if e2tDataList != nil && err == nil {
+                       PopulateE2TMap(e2tDataList, e2ts, meids[:])
+                       break
                } else if err == nil {
                        readErr = errors.New("unexpected HTTP status code")
                } else {
-                       xapp.Logger.Warn("cannot get xapp data due to: " + err.Error())
+                       xapp.Logger.Warn("cannot get E2T data from E2M due to: " + err.Error())
                        readErr = err
                }
+       time.Sleep(2 * time.Second)
+       }
+
+       if ( readErr != nil) {
+               return readErr
        }
+
+       pcData, confErr := rtmgr.GetPlatformComponents(configfile)
+    if confErr != nil {
+            xapp.Logger.Error(confErr.Error())
+            return confErr
+    }
+    xapp.Logger.Info("Recieved intial xapp data, E2T data and platform data, writing into SDL.")
+    // Combine the xapps data and platform data before writing to the SDL
+       ricData := &rtmgr.RicComponents{XApps: *xappData, Pcs: *pcData, E2Ts: e2ts, MeidMap: meids}
+    writeErr := sdlEngine.WriteAll(fileName, ricData)
+    if writeErr != nil {
+            xapp.Logger.Error(writeErr.Error())
+    }
+
+    xapp.Logger.Info("Trying to fetch Subscriptions data from Subscription manager")
+    for i := 1; i <= maxRetries; i++ {
+               readErr = nil
+               sub_list, err := xapp.Subscription.QuerySubscriptions()
+
+               if sub_list != nil && err == nil {
+                       PopulateSubscription(sub_list)
+                       break
+               } else {
+                       readErr = err
+                       xapp.Logger.Warn("cannot get xapp data due to: " + readErr.Error())
+        }
+               time.Sleep(2 * time.Second)
+       }
+
+       if (readErr != nil) {
+        return readErr
+       }
+
+    // post subscription req to appmgr
+    readErr = PostSubReq(xmurl, nbiif)
+    if readErr == nil {
+            return nil
+    }
+
        return readErr
 }
 
-func (r *HttpRestful) Initialize(xmurl string, nbiif string, fileName string, configfile string,
+func (r *HttpRestful) Initialize(xmurl string, nbiif string, fileName string, configfile string, e2murl string,
        sdlEngine sdl.Engine, rpeEngine rpe.Engine, triggerSBI chan<- bool, m *sync.Mutex) error {
-       err := r.RetrieveStartupData(xmurl, nbiif, fileName, configfile, sdlEngine)
+       err := r.RetrieveStartupData(xmurl, nbiif, fileName, configfile, e2murl, sdlEngine)
        if err != nil {
                xapp.Logger.Error("Exiting as nbi failed to get the initial startup data from the xapp manager: " + err.Error())
                return err
@@ -729,3 +864,21 @@ func updateSubscription(data *rtmgr.XappList) {
        }
 
 }
+
+func PopulateSubscription(sub_list xfmodel.SubscriptionList) {
+       for _, sub_row := range sub_list {
+               var subdata models.XappSubscriptionData
+               id := int32(sub_row.SubscriptionID)
+               subdata.SubscriptionID = &id
+               for _, ep := range sub_row.Endpoint {
+
+                       stringSlice := strings.Split(ep, ":")
+                       subdata.Address = &stringSlice[0]
+                       intportval, _ := strconv.Atoi( stringSlice[1])
+                       value := uint16(intportval)
+                       subdata.Port = &value
+                       xapp.Logger.Debug("Adding Subscription List has Address :%v, port :%v, SubscriptionID :%v ", subdata.Address, subdata.Address, subdata.SubscriptionID)
+                       addSubscription(&rtmgr.Subs, &subdata)
+               }
+       }
+}