Merge "API update"
[ric-plt/xapp-frame.git] / pkg / xapp / subscription.go
index 1cdce82..6ceabf9 100755 (executable)
 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"
-       apicontrol "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/control"
+       apicommon "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/common"
        apipolicy "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/policy"
        apireport "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/report"
        apimodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"
@@ -35,12 +39,15 @@ import (
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi"
        "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/common"
        "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.SubscriptionResponse, error)
+type SubscriptionQueryHandler func() (models.SubscriptionList, error)
+type SubscriptionDeleteHandler func(string) error
 
 type Subscriber struct {
        localAddr  string
@@ -71,7 +78,7 @@ func NewSubscriber(host string, timo int) *Subscriber {
 }
 
 // Server interface: listen and receive subscription requests
-func (r *Subscriber) Listen(handler SubscriptionReportHandler) error {
+func (r *Subscriber) Listen(add SubscriptionHandler, get SubscriptionQueryHandler, del SubscriptionDeleteHandler) error {
        swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
        if err != nil {
                return err
@@ -79,33 +86,42 @@ func (r *Subscriber) Listen(handler SubscriptionReportHandler) error {
 
        api := operations.NewXappFrameworkAPI(swaggerSpec)
 
+       // Subscription: query
+       api.QueryGetAllSubscriptionsHandler = query.GetAllSubscriptionsHandlerFunc(
+               func(p query.GetAllSubscriptionsParams) middleware.Responder {
+                       if resp, err := get(); 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 := add(models.SubscriptionTypeReport, p.ReportParams); err == nil {
                                return report.NewSubscribeReportCreated().WithPayload(resp)
                        }
                        return report.NewSubscribeReportInternalServerError()
                })
 
-       // SubscriptionType: Control
-       api.ControlSubscribeControlHandler = control.SubscribeControlHandlerFunc(
-               func(p control.SubscribeControlParams) middleware.Responder {
-                       if resp, err := handler(models.SubscriptionTypeControl, p.ControlParams); err == nil {
-                               return control.NewSubscribeControlCreated().WithPayload(resp)
-                       }
-                       return control.NewSubscribeControlInternalServerError()
-               })
-
        // 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 := add(models.SubscriptionTypePolicy, p.PolicyParams); err == nil {
                                return policy.NewSubscribePolicyCreated().WithPayload(resp)
                        }
                        return policy.NewSubscribePolicyInternalServerError()
                })
 
+       // SubscriptionType: delete
+       api.CommonUnsubscribeHandler = common.UnsubscribeHandlerFunc(
+               func(p common.UnsubscribeParams) middleware.Responder {
+                       if err := del(p.SubscriptionID); err == nil {
+                               return common.NewUnsubscribeNoContent()
+                       }
+                       return common.NewUnsubscribeInternalServerError()
+               })
+
        server := restapi.NewServer(api)
        defer server.Shutdown()
        server.Host = r.localAddr
@@ -119,38 +135,58 @@ func (r *Subscriber) Listen(handler SubscriptionReportHandler) error {
 }
 
 // Subscription interface for xApp: REPORT
-func (r *Subscriber) SubscribeReport(p *apimodel.ReportParams) (apimodel.SubscriptionResult, error) {
+func (r *Subscriber) SubscribeReport(p *apimodel.ReportParams) (*apimodel.SubscriptionResponse, error) {
        params := apireport.NewSubscribeReportParamsWithTimeout(r.timeout).WithReportParams(p)
        result, err := r.CreateTransport().Report.SubscribeReport(params)
        if err != nil {
-               return apimodel.SubscriptionResult{}, err
+               return &apimodel.SubscriptionResponse{}, err
        }
 
        return result.Payload, err
 }
 
-// Subscription interface for xApp: CONTROL
-func (r *Subscriber) SubscribeControl(p *apimodel.ControlParams) (apimodel.SubscriptionResult, error) {
-       params := apicontrol.NewSubscribeControlParamsWithTimeout(r.timeout).WithControlParams(p)
-       result, err := r.CreateTransport().Control.SubscribeControl(params)
+// Subscription interface for xApp: POLICY
+func (r *Subscriber) SubscribePolicy(p *apimodel.PolicyParams) (*apimodel.SubscriptionResponse, error) {
+       params := apipolicy.NewSubscribePolicyParamsWithTimeout(r.timeout).WithPolicyParams(p)
+       result, err := r.CreateTransport().Policy.SubscribePolicy(params)
        if err != nil {
-               return apimodel.SubscriptionResult{}, err
+               return &apimodel.SubscriptionResponse{}, err
        }
 
        return result.Payload, err
 }
 
-// Subscription interface for xApp: POLICY
-func (r *Subscriber) SubscribePolicy(p *apimodel.PolicyParams) (apimodel.SubscriptionResult, error) {
-       params := apipolicy.NewSubscribePolicyParamsWithTimeout(r.timeout).WithPolicyParams(p)
-       result, err := r.CreateTransport().Policy.SubscribePolicy(params)
+// Subscription interface for xApp: DELETE
+func (r *Subscriber) UnSubscribe(subId string) error {
+       params := apicommon.NewUnsubscribeParamsWithTimeout(r.timeout).WithSubscriptionID(subId)
+       _, err := r.CreateTransport().Common.Unsubscribe(params)
+
+       return err
+}
+
+// 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 apimodel.SubscriptionResult{}, err
+               return models.SubscriptionList{}, err
        }
 
-       return result.Payload, 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 (s *Subscriber) CreateTransport() *apiclient.RICSubscription {
-       return apiclient.New(httptransport.New(s.remoteHost, s.remoteUrl, s.remoteProt), strfmt.Default)
+func (r *Subscriber) CreateTransport() *apiclient.RICSubscription {
+       return apiclient.New(httptransport.New(r.remoteHost, r.remoteUrl, r.remoteProt), strfmt.Default)
 }