Get interface for subscriptions

Change-Id: I11d351c6e3de9fcdc1f4e1a7937054c091bd870e
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
diff --git a/pkg/xapp/subscription.go b/pkg/xapp/subscription.go
index 1cdce82..2090123 100755
--- a/pkg/xapp/subscription.go
+++ b/pkg/xapp/subscription.go
@@ -20,10 +20,14 @@
 package xapp
 
 import (
+	"encoding/json"
+	"fmt"
 	"github.com/go-openapi/loads"
 	httptransport "github.com/go-openapi/runtime/client"
 	"github.com/go-openapi/runtime/middleware"
 	"github.com/go-openapi/strfmt"
+	"io/ioutil"
+	"net/http"
 	"time"
 
 	apiclient "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi"
@@ -37,10 +41,12 @@
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations"
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/control"
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/policy"
+	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/query"
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/report"
 )
 
-type SubscriptionReportHandler func(models.SubscriptionType, interface{}) (models.SubscriptionResult, error)
+type SubscriptionHandler func(models.SubscriptionType, interface{}) (models.SubscriptionResult, error)
+type SubscriptionQueryHandler func() (models.SubscriptionList, error)
 
 type Subscriber struct {
 	localAddr  string
@@ -71,7 +77,7 @@
 }
 
 // Server interface: listen and receive subscription requests
-func (r *Subscriber) Listen(handler SubscriptionReportHandler) error {
+func (r *Subscriber) Listen(subHandler SubscriptionHandler, queryHandler SubscriptionQueryHandler) error {
 	swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
 	if err != nil {
 		return err
@@ -79,10 +85,19 @@
 
 	api := operations.NewXappFrameworkAPI(swaggerSpec)
 
+	// Subscription: query
+	api.QueryGetAllSubscriptionsHandler = query.GetAllSubscriptionsHandlerFunc(
+		func(p query.GetAllSubscriptionsParams) middleware.Responder {
+			if resp, err := queryHandler(); err == nil {
+				return query.NewGetAllSubscriptionsOK().WithPayload(resp)
+			}
+			return query.NewGetAllSubscriptionsInternalServerError()
+		})
+
 	// SubscriptionType: Report
 	api.ReportSubscribeReportHandler = report.SubscribeReportHandlerFunc(
 		func(p report.SubscribeReportParams) middleware.Responder {
-			if resp, err := handler(models.SubscriptionTypeReport, p.ReportParams); err == nil {
+			if resp, err := subHandler(models.SubscriptionTypeReport, p.ReportParams); err == nil {
 				return report.NewSubscribeReportCreated().WithPayload(resp)
 			}
 			return report.NewSubscribeReportInternalServerError()
@@ -91,7 +106,7 @@
 	// SubscriptionType: Control
 	api.ControlSubscribeControlHandler = control.SubscribeControlHandlerFunc(
 		func(p control.SubscribeControlParams) middleware.Responder {
-			if resp, err := handler(models.SubscriptionTypeControl, p.ControlParams); err == nil {
+			if resp, err := subHandler(models.SubscriptionTypeControl, p.ControlParams); err == nil {
 				return control.NewSubscribeControlCreated().WithPayload(resp)
 			}
 			return control.NewSubscribeControlInternalServerError()
@@ -100,7 +115,7 @@
 	// SubscriptionType: policy
 	api.PolicySubscribePolicyHandler = policy.SubscribePolicyHandlerFunc(
 		func(p policy.SubscribePolicyParams) middleware.Responder {
-			if resp, err := handler(models.SubscriptionTypePolicy, p.PolicyParams); err == nil {
+			if resp, err := subHandler(models.SubscriptionTypePolicy, p.PolicyParams); err == nil {
 				return policy.NewSubscribePolicyCreated().WithPayload(resp)
 			}
 			return policy.NewSubscribePolicyInternalServerError()
@@ -151,6 +166,29 @@
 	return result.Payload, err
 }
 
-func (s *Subscriber) CreateTransport() *apiclient.RICSubscription {
-	return apiclient.New(httptransport.New(s.remoteHost, s.remoteUrl, s.remoteProt), strfmt.Default)
+// Subscription interface for xApp: QUERY
+func (r *Subscriber) QuerySubscriptions() (models.SubscriptionList, error) {
+	resp, err := http.Get(fmt.Sprintf("http://%s/%s/subscriptions", r.remoteHost, r.remoteUrl))
+	if err != nil {
+		return models.SubscriptionList{}, err
+	}
+
+	defer resp.Body.Close()
+
+	contents, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return models.SubscriptionList{}, err
+	}
+
+	subscriptions := models.SubscriptionList{}
+	err = json.Unmarshal([]byte(string(contents)), &subscriptions)
+	if err != nil {
+		return models.SubscriptionList{}, err
+	}
+
+	return subscriptions, nil
+}
+
+func (r *Subscriber) CreateTransport() *apiclient.RICSubscription {
+	return apiclient.New(httptransport.New(r.remoteHost, r.remoteUrl, r.remoteProt), strfmt.Default)
 }
diff --git a/pkg/xapp/subscription_test.go b/pkg/xapp/subscription_test.go
index 28549e7..5ebb9c2 100755
--- a/pkg/xapp/subscription_test.go
+++ b/pkg/xapp/subscription_test.go
@@ -59,14 +59,35 @@
 	return models.SubscriptionResult{11, 22, 33}, nil
 }
 
+func queryHandler() (models.SubscriptionList, error) {
+	resp := models.SubscriptionList{
+		&models.SubscriptionData{
+			SubscriptionID: 11,
+			Meid: "Test-Gnb",
+			Endpoint: []string{"127.0.0.1:4056"},
+		},
+	}
+
+	return resp, nil
+}
+
 func TestSetup(t *testing.T) {
 	suite = t
 
 	// Start the server to simulate SubManager
-	go Subscription.Listen(subscriptionHandler)
+	go Subscription.Listen(subscriptionHandler, queryHandler)
 	time.Sleep(time.Duration(2) * time.Second)
 }
 
+func TestSubscriptionQueryHandling(t *testing.T) {
+	resp, err := Subscription.QuerySubscriptions()
+
+	assert.Equal(t, err, nil)
+	assert.Equal(t, resp[0].SubscriptionID, int64(11))
+	assert.Equal(t, resp[0].Meid, "Test-Gnb")
+	assert.Equal(t, resp[0].Endpoint, []string{"127.0.0.1:4056"})
+}
+
 func TestSubscriptionReportHandling(t *testing.T) {
 	result, err := Subscription.SubscribeReport(&reportParams)
 
diff --git a/pkg/xapp/xapp_test.go b/pkg/xapp/xapp_test.go
index 27fb445..f0e3070 100755
--- a/pkg/xapp/xapp_test.go
+++ b/pkg/xapp/xapp_test.go
@@ -102,6 +102,7 @@
 }
 
 func TestMessagesReceivedSuccessfully(t *testing.T) {
+	time.Sleep(time.Duration(5) * time.Second)
 	for i := 0; i < 100; i++ {
 		params := &RMRParams{}
 		params.Mtype = 10004
@@ -118,11 +119,11 @@
 	waitForSdl := viper.GetBool("db.waitForSdl")
 	stats := getMetrics(t)
 	if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
-		t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect")
+		t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
 	}
 
 	if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
-		t.Errorf("Error: ricxapp_RMR_Received value incorrect")
+		t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
 	}
 
 	if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {