Merge "Binding interface IP address for REST Server"
[ric-plt/xapp-frame.git] / pkg / restapi / operations / xapp_framework_api.go
index 28f71f2..83f3bb5 100644 (file)
@@ -10,19 +10,17 @@ import (
        "net/http"
        "strings"
 
-       errors "github.com/go-openapi/errors"
-       loads "github.com/go-openapi/loads"
-       runtime "github.com/go-openapi/runtime"
-       middleware "github.com/go-openapi/runtime/middleware"
-       security "github.com/go-openapi/runtime/security"
-       spec "github.com/go-openapi/spec"
-       strfmt "github.com/go-openapi/strfmt"
+       "github.com/go-openapi/errors"
+       "github.com/go-openapi/loads"
+       "github.com/go-openapi/runtime"
+       "github.com/go-openapi/runtime/middleware"
+       "github.com/go-openapi/runtime/security"
+       "github.com/go-openapi/spec"
+       "github.com/go-openapi/strfmt"
        "github.com/go-openapi/swag"
 
        "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/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/report"
+       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/xapp"
 )
 
 // NewXappFrameworkAPI creates a new XappFramework instance
@@ -34,25 +32,30 @@ func NewXappFrameworkAPI(spec *loads.Document) *XappFrameworkAPI {
                defaultProduces:     "application/json",
                customConsumers:     make(map[string]runtime.Consumer),
                customProducers:     make(map[string]runtime.Producer),
+               PreServerShutdown:   func() {},
                ServerShutdown:      func() {},
                spec:                spec,
                ServeError:          errors.ServeError,
                BasicAuthenticator:  security.BasicAuth,
                APIKeyAuthenticator: security.APIKeyAuth,
                BearerAuthenticator: security.BearerAuth,
-               JSONConsumer:        runtime.JSONConsumer(),
-               JSONProducer:        runtime.JSONProducer(),
-               CommonUnsubscribeHandler: common.UnsubscribeHandlerFunc(func(params common.UnsubscribeParams) middleware.Responder {
-                       return middleware.NotImplemented("operation CommonUnsubscribe has not yet been implemented")
+
+               JSONConsumer: runtime.JSONConsumer(),
+
+               JSONProducer: runtime.JSONProducer(),
+               XMLProducer:  runtime.XMLProducer(),
+
+               CommonSubscribeHandler: common.SubscribeHandlerFunc(func(params common.SubscribeParams) middleware.Responder {
+                       return middleware.NotImplemented("operation common.Subscribe has not yet been implemented")
                }),
-               ControlSubscribeControlHandler: control.SubscribeControlHandlerFunc(func(params control.SubscribeControlParams) middleware.Responder {
-                       return middleware.NotImplemented("operation ControlSubscribeControl has not yet been implemented")
+               CommonUnsubscribeHandler: common.UnsubscribeHandlerFunc(func(params common.UnsubscribeParams) middleware.Responder {
+                       return middleware.NotImplemented("operation common.Unsubscribe has not yet been implemented")
                }),
-               PolicySubscribePolicyHandler: policy.SubscribePolicyHandlerFunc(func(params policy.SubscribePolicyParams) middleware.Responder {
-                       return middleware.NotImplemented("operation PolicySubscribePolicy has not yet been implemented")
+               CommonGetAllSubscriptionsHandler: common.GetAllSubscriptionsHandlerFunc(func(params common.GetAllSubscriptionsParams) middleware.Responder {
+                       return middleware.NotImplemented("operation common.GetAllSubscriptions has not yet been implemented")
                }),
-               ReportSubscribeReportHandler: report.SubscribeReportHandlerFunc(func(params report.SubscribeReportParams) middleware.Responder {
-                       return middleware.NotImplemented("operation ReportSubscribeReport has not yet been implemented")
+               XappGetXappConfigListHandler: xapp.GetXappConfigListHandlerFunc(func(params xapp.GetXappConfigListParams) middleware.Responder {
+                       return middleware.NotImplemented("operation xapp.GetXappConfigList has not yet been implemented")
                }),
        }
 }
@@ -79,25 +82,33 @@ type XappFrameworkAPI struct {
        // It has a default implementation in the security package, however you can replace it for your particular usage.
        BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator
 
-       // JSONConsumer registers a consumer for a "application/json" mime type
+       // JSONConsumer registers a consumer for the following mime types:
+       //   - application/json
        JSONConsumer runtime.Consumer
 
-       // JSONProducer registers a producer for a "application/json" mime type
+       // JSONProducer registers a producer for the following mime types:
+       //   - application/json
        JSONProducer runtime.Producer
+       // XMLProducer registers a producer for the following mime types:
+       //   - application/xml
+       XMLProducer runtime.Producer
 
+       // CommonSubscribeHandler sets the operation handler for the subscribe operation
+       CommonSubscribeHandler common.SubscribeHandler
        // CommonUnsubscribeHandler sets the operation handler for the unsubscribe operation
        CommonUnsubscribeHandler common.UnsubscribeHandler
-       // ControlSubscribeControlHandler sets the operation handler for the subscribe control operation
-       ControlSubscribeControlHandler control.SubscribeControlHandler
-       // PolicySubscribePolicyHandler sets the operation handler for the subscribe policy operation
-       PolicySubscribePolicyHandler policy.SubscribePolicyHandler
-       // ReportSubscribeReportHandler sets the operation handler for the subscribe report operation
-       ReportSubscribeReportHandler report.SubscribeReportHandler
-
+       // CommonGetAllSubscriptionsHandler sets the operation handler for the get all subscriptions operation
+       CommonGetAllSubscriptionsHandler common.GetAllSubscriptionsHandler
+       // XappGetXappConfigListHandler sets the operation handler for the get xapp config list operation
+       XappGetXappConfigListHandler xapp.GetXappConfigListHandler
        // ServeError is called when an error is received, there is a default handler
        // but you can set your own with this
        ServeError func(http.ResponseWriter, *http.Request, error)
 
+       // PreServerShutdown is called before the HTTP(S) server is shutdown
+       // This allows for custom functions to get executed before the HTTP(S) server stops accepting traffic
+       PreServerShutdown func()
+
        // ServerShutdown is called when the HTTP(S) server is shut down and done
        // handling all active connections and does not accept connections any more
        ServerShutdown func()
@@ -155,21 +166,21 @@ func (o *XappFrameworkAPI) Validate() error {
        if o.JSONProducer == nil {
                unregistered = append(unregistered, "JSONProducer")
        }
+       if o.XMLProducer == nil {
+               unregistered = append(unregistered, "XMLProducer")
+       }
 
+       if o.CommonSubscribeHandler == nil {
+               unregistered = append(unregistered, "common.SubscribeHandler")
+       }
        if o.CommonUnsubscribeHandler == nil {
                unregistered = append(unregistered, "common.UnsubscribeHandler")
        }
-
-       if o.ControlSubscribeControlHandler == nil {
-               unregistered = append(unregistered, "control.SubscribeControlHandler")
-       }
-
-       if o.PolicySubscribePolicyHandler == nil {
-               unregistered = append(unregistered, "policy.SubscribePolicyHandler")
+       if o.CommonGetAllSubscriptionsHandler == nil {
+               unregistered = append(unregistered, "common.GetAllSubscriptionsHandler")
        }
-
-       if o.ReportSubscribeReportHandler == nil {
-               unregistered = append(unregistered, "report.SubscribeReportHandler")
+       if o.XappGetXappConfigListHandler == nil {
+               unregistered = append(unregistered, "xapp.GetXappConfigListHandler")
        }
 
        if len(unregistered) > 0 {
@@ -186,28 +197,22 @@ func (o *XappFrameworkAPI) ServeErrorFor(operationID string) func(http.ResponseW
 
 // AuthenticatorsFor gets the authenticators for the specified security schemes
 func (o *XappFrameworkAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator {
-
        return nil
-
 }
 
 // Authorizer returns the registered authorizer
 func (o *XappFrameworkAPI) Authorizer() runtime.Authorizer {
-
        return nil
-
 }
 
-// ConsumersFor gets the consumers for the specified media types
+// ConsumersFor gets the consumers for the specified media types.
+// MIME type parameters are ignored here.
 func (o *XappFrameworkAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer {
-
-       result := make(map[string]runtime.Consumer)
+       result := make(map[string]runtime.Consumer, len(mediaTypes))
        for _, mt := range mediaTypes {
                switch mt {
-
                case "application/json":
                        result["application/json"] = o.JSONConsumer
-
                }
 
                if c, ok := o.customConsumers[mt]; ok {
@@ -215,19 +220,18 @@ func (o *XappFrameworkAPI) ConsumersFor(mediaTypes []string) map[string]runtime.
                }
        }
        return result
-
 }
 
-// ProducersFor gets the producers for the specified media types
+// ProducersFor gets the producers for the specified media types.
+// MIME type parameters are ignored here.
 func (o *XappFrameworkAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer {
-
-       result := make(map[string]runtime.Producer)
+       result := make(map[string]runtime.Producer, len(mediaTypes))
        for _, mt := range mediaTypes {
                switch mt {
-
                case "application/json":
                        result["application/json"] = o.JSONProducer
-
+               case "application/xml":
+                       result["application/xml"] = o.XMLProducer
                }
 
                if p, ok := o.customProducers[mt]; ok {
@@ -235,7 +239,6 @@ func (o *XappFrameworkAPI) ProducersFor(mediaTypes []string) map[string]runtime.
                }
        }
        return result
-
 }
 
 // HandlerFor gets a http.Handler for the provided operation method and path
@@ -265,31 +268,26 @@ func (o *XappFrameworkAPI) Context() *middleware.Context {
 
 func (o *XappFrameworkAPI) initHandlerCache() {
        o.Context() // don't care about the result, just that the initialization happened
-
        if o.handlers == nil {
                o.handlers = make(map[string]map[string]http.Handler)
        }
 
+       if o.handlers["POST"] == nil {
+               o.handlers["POST"] = make(map[string]http.Handler)
+       }
+       o.handlers["POST"]["/subscriptions"] = common.NewSubscribe(o.context, o.CommonSubscribeHandler)
        if o.handlers["DELETE"] == nil {
                o.handlers["DELETE"] = make(map[string]http.Handler)
        }
        o.handlers["DELETE"]["/subscriptions/{subscriptionId}"] = common.NewUnsubscribe(o.context, o.CommonUnsubscribeHandler)
-
-       if o.handlers["POST"] == nil {
-               o.handlers["POST"] = make(map[string]http.Handler)
-       }
-       o.handlers["POST"]["/subscriptions/control"] = control.NewSubscribeControl(o.context, o.ControlSubscribeControlHandler)
-
-       if o.handlers["POST"] == nil {
-               o.handlers["POST"] = make(map[string]http.Handler)
+       if o.handlers["GET"] == nil {
+               o.handlers["GET"] = make(map[string]http.Handler)
        }
-       o.handlers["POST"]["/subscriptions/policy"] = policy.NewSubscribePolicy(o.context, o.PolicySubscribePolicyHandler)
-
-       if o.handlers["POST"] == nil {
-               o.handlers["POST"] = make(map[string]http.Handler)
+       o.handlers["GET"]["/subscriptions"] = common.NewGetAllSubscriptions(o.context, o.CommonGetAllSubscriptionsHandler)
+       if o.handlers["GET"] == nil {
+               o.handlers["GET"] = make(map[string]http.Handler)
        }
-       o.handlers["POST"]["/subscriptions/report"] = report.NewSubscribeReport(o.context, o.ReportSubscribeReportHandler)
-
+       o.handlers["GET"]["/config"] = xapp.NewGetXappConfigList(o.context, o.XappGetXappConfigListHandler)
 }
 
 // Serve creates a http handler to serve the API over HTTP
@@ -319,3 +317,15 @@ func (o *XappFrameworkAPI) RegisterConsumer(mediaType string, consumer runtime.C
 func (o *XappFrameworkAPI) RegisterProducer(mediaType string, producer runtime.Producer) {
        o.customProducers[mediaType] = producer
 }
+
+// AddMiddlewareFor adds a http middleware to existing handler
+func (o *XappFrameworkAPI) AddMiddlewareFor(method, path string, builder middleware.Builder) {
+       um := strings.ToUpper(method)
+       if path == "/" {
+               path = ""
+       }
+       o.Init()
+       if h, ok := o.handlers[um][path]; ok {
+               o.handlers[method][path] = builder(h)
+       }
+}