getAllSubscriptions API (RM -> SM) during restart of routing manager handled

Change-Id: I36a60cb06e2231881b63e85675bcb4c7df8c086a
Signed-off-by: rangajal <ranjit.angajala@nokia.com>
diff --git a/Dockerfile b/Dockerfile
index 1dc624c..c8f7d6b 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -62,6 +62,8 @@
 COPY pkg pkg
 COPY cmd cmd
 COPY run_rtmgr.sh /run_rtmgr.sh
+RUN mkdir manifests
+COPY manifests/ /go/src/routing-manager/manifests
 #RUN go mod download 
 #RUN /usr/local/go/bin/go mod tidy
 ENV GOPATH /go
@@ -72,7 +74,7 @@
 # UT intermediate container
 FROM rtmgrbuild as rtmgrut
 RUN ldconfig
-RUN go test ./pkg/sbi ./pkg/rpe ./pkg/nbi ./pkg/sdl -f "./manifests/rtmgr/rtmgr-cfg.yaml" -cover -race
+RUN go test ./pkg/sbi ./pkg/rpe ./pkg/nbi ./pkg/sdl -f "/go/src/routing-manager/manifests/rtmgr/rtmgr-cfg.yaml" -cover -race
 
 # Final, executable container
 FROM ubuntu:16.04
diff --git a/RELNOTES b/RELNOTES
index b497112..3401229 100644
--- a/RELNOTES
+++ b/RELNOTES
@@ -1,3 +1,6 @@
+### v0.4.16
+* getAllSubscriptions API (RM -> SM) during restart of routing manager handled
+
 ### v0.4.15
 * Retained (E2M->E2T issue) - retrying when is_Ready flag in socket handle is false 
 
diff --git a/container-tag.yaml b/container-tag.yaml
index c754d3c..d1f21e4 100644
--- a/container-tag.yaml
+++ b/container-tag.yaml
@@ -2,4 +2,4 @@
 # By default this file is in the docker build directory,
 # but the location can configured in the JJB template.
 ---
-tag: 0.4.15
+tag: 0.4.16
diff --git a/go.mod b/go.mod
index e133c89..89a5158 100644
--- a/go.mod
+++ b/go.mod
@@ -3,13 +3,13 @@
 go 1.12.1
 
 require (
-	gerrit.o-ran-sc.org/r/ric-plt/xapp-frame v0.0.24
+	gerrit.o-ran-sc.org/r/ric-plt/xapp-frame v0.0.27
 	nanomsg.org/go/mangos/v2 v2.0.5
 )
 
 replace gerrit.o-ran-sc.org/r/ric-plt/sdlgo => gerrit.o-ran-sc.org/r/ric-plt/sdlgo.git v0.2.0
 
-replace gerrit.o-ran-sc.org/r/ric-plt/xapp-frame => gerrit.o-ran-sc.org/r/ric-plt/xapp-frame.git v0.0.24
+replace gerrit.o-ran-sc.org/r/ric-plt/xapp-frame => gerrit.o-ran-sc.org/r/ric-plt/xapp-frame.git v0.0.27
 
 replace gerrit.o-ran-sc.org/r/com/golog => gerrit.o-ran-sc.org/r/com/golog.git v0.0.1
 
diff --git a/manifests/rtmgr/rtmgr-cfg.yaml b/manifests/rtmgr/rtmgr-cfg.yaml
index 5fbc273..55aab19 100644
--- a/manifests/rtmgr/rtmgr-cfg.yaml
+++ b/manifests/rtmgr/rtmgr-cfg.yaml
@@ -65,3 +65,5 @@
       "maxSize": 2072
       "numWorkers": 1
     }
+subscription:
+ host: "127.0.0.1:8089"
diff --git a/pkg/nbi/httprestful.go b/pkg/nbi/httprestful.go
index b07a8db..f47034e 100644
--- a/pkg/nbi/httprestful.go
+++ b/pkg/nbi/httprestful.go
@@ -36,6 +36,7 @@
 	"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"
@@ -579,8 +580,8 @@
 
 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 maxRetries = 10
+    var readErr error
+    var maxRetries = 10
 	var xappData *[]rtmgr.XApp
 	xappData = new([]rtmgr.XApp)
 	xapp.Logger.Info("Trying to fetch XApps data from XAPP manager")
@@ -607,7 +608,6 @@
 	e2ts := make(map[string]rtmgr.E2TInstance)
 	xapp.Logger.Info("Trying to fetch E2T data from E2manager")
         for i := 1; i <= maxRetries; i++ {
-                time.Sleep(2 * time.Second)
 
 		readErr = nil
 		e2tDataList, err := httpGetE2TList(e2murl)
@@ -620,6 +620,7 @@
 			xapp.Logger.Warn("cannot get E2T data from E2M due to: " + err.Error())
 			readErr = err
 		}
+       time.Sleep(2 * time.Second)
 	}
 
 	if ( readErr != nil) {
@@ -627,22 +628,42 @@
 	}
 
 	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
+    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())
+    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())
         }
-        // post subscription req to appmgr
-        readErr = PostSubReq(xmurl, nbiif)
-        if readErr == nil {
-                return nil
-        }
+		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
 }
 
@@ -841,3 +862,21 @@
 	}
 
 }
+
+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)
+		}
+	}
+}
diff --git a/pkg/nbi/httprestful_test.go b/pkg/nbi/httprestful_test.go
index 3419dcd..f42a835 100644
--- a/pkg/nbi/httprestful_test.go
+++ b/pkg/nbi/httprestful_test.go
@@ -68,6 +68,8 @@
 
 var E2TListResp = []byte(`[{"e2tAddress":"127.0.0.1:0","ranNames":["RanM0","RanN0"]},{"e2tAddress":"127.0.0.1:1","ranNames":["RanM1","RanN1"]},{"e2tAddress":"127.0.0.1:2","ranNames":["RanM2","RanN2"]},{"e2tAddress":"127.0.0.1:3","ranNames":["RanM3","RanN3"]}]`)
 
+var SubscriptionList = []byte(`[{"SubscriptionId":11,"Meid":"Test-Gnb","Endpoint":["127.0.0.1:4056"]}]`)
+
 var InvalidSubResp = []byte(`{"Version":0, "EventType":all}`)
 
 func TestValidateXappCallbackData_1(t *testing.T) {
@@ -549,6 +551,25 @@
 	return ts
 }
 
+func createMockSubmgrWithData(url string, t []byte) *httptest.Server {
+	l, err := net.Listen("tcp", url)
+	if err != nil {
+		fmt.Println("Failed to create listener: " + err.Error())
+	}
+	ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+
+		if r.Method == "GET" && r.URL.String() == "//ric/v1/subscriptions" {
+			w.Header().Add("Content-Type", "application/json")
+			w.WriteHeader(http.StatusOK)
+			w.Write(t)
+		}
+
+	}))
+	ts.Listener.Close()
+	ts.Listener = l
+	return ts
+}
+
 func createMockPlatformComponents() {
 	var filename = "config.json"
 	file, _ := json.MarshalIndent(stub.ValidPlatformComponents, "", "")
@@ -682,6 +703,10 @@
 	ts1.Start()
 	defer ts1.Close()
 
+	ts2 := createMockSubmgrWithData("127.0.0.1:8089", SubscriptionList)
+	ts2.Start()
+	defer ts2.Close()
+
 	sdlEngine, _ := sdl.GetSdl("file")
 	var httpRestful, _ = GetNbi("httpRESTful")
 	createMockPlatformComponents()