Add optional subscription ID
[ric-plt/xapp-frame.git] / pkg / restapi / operations / xapp_framework_api.go
index e16707e..83f3bb5 100644 (file)
@@ -10,13 +10,13 @@ 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"
@@ -32,26 +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(),
-               XMLProducer:         runtime.XMLProducer(),
+
+               JSONConsumer: runtime.JSONConsumer(),
+
+               JSONProducer: runtime.JSONProducer(),
+               XMLProducer:  runtime.XMLProducer(),
+
                CommonSubscribeHandler: common.SubscribeHandlerFunc(func(params common.SubscribeParams) middleware.Responder {
-                       return middleware.NotImplemented("operation CommonSubscribe has not yet been implemented")
+                       return middleware.NotImplemented("operation common.Subscribe has not yet been implemented")
                }),
                CommonUnsubscribeHandler: common.UnsubscribeHandlerFunc(func(params common.UnsubscribeParams) middleware.Responder {
-                       return middleware.NotImplemented("operation CommonUnsubscribe has not yet been implemented")
+                       return middleware.NotImplemented("operation common.Unsubscribe has not yet been implemented")
                }),
                CommonGetAllSubscriptionsHandler: common.GetAllSubscriptionsHandlerFunc(func(params common.GetAllSubscriptionsParams) middleware.Responder {
-                       return middleware.NotImplemented("operation CommonGetAllSubscriptions has not yet been implemented")
+                       return middleware.NotImplemented("operation common.GetAllSubscriptions has not yet been implemented")
                }),
                XappGetXappConfigListHandler: xapp.GetXappConfigListHandlerFunc(func(params xapp.GetXappConfigListParams) middleware.Responder {
-                       return middleware.NotImplemented("operation XappGetXappConfigList has not yet been implemented")
+                       return middleware.NotImplemented("operation xapp.GetXappConfigList has not yet been implemented")
                }),
        }
 }
@@ -78,12 +82,15 @@ 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 a "application/xml" mime type
+       // XMLProducer registers a producer for the following mime types:
+       //   - application/xml
        XMLProducer runtime.Producer
 
        // CommonSubscribeHandler sets the operation handler for the subscribe operation
@@ -94,11 +101,14 @@ type XappFrameworkAPI struct {
        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()
@@ -156,7 +166,6 @@ func (o *XappFrameworkAPI) Validate() error {
        if o.JSONProducer == nil {
                unregistered = append(unregistered, "JSONProducer")
        }
-
        if o.XMLProducer == nil {
                unregistered = append(unregistered, "XMLProducer")
        }
@@ -164,15 +173,12 @@ func (o *XappFrameworkAPI) Validate() error {
        if o.CommonSubscribeHandler == nil {
                unregistered = append(unregistered, "common.SubscribeHandler")
        }
-
        if o.CommonUnsubscribeHandler == nil {
                unregistered = append(unregistered, "common.UnsubscribeHandler")
        }
-
        if o.CommonGetAllSubscriptionsHandler == nil {
                unregistered = append(unregistered, "common.GetAllSubscriptionsHandler")
        }
-
        if o.XappGetXappConfigListHandler == nil {
                unregistered = append(unregistered, "xapp.GetXappConfigListHandler")
        }
@@ -191,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 {
@@ -220,22 +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 {
@@ -243,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
@@ -273,7 +268,6 @@ 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)
        }
@@ -282,22 +276,18 @@ func (o *XappFrameworkAPI) initHandlerCache() {
                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["GET"] == nil {
                o.handlers["GET"] = 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["GET"]["/config"] = xapp.NewGetXappConfigList(o.context, o.XappGetXappConfigListHandler)
-
 }
 
 // Serve creates a http handler to serve the API over HTTP
@@ -327,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)
+       }
+}