X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Frestapi%2Foperations%2Fxapp_framework_api.go;h=83f3bb547c9d4db3ee336151965a672af10b2987;hb=8b7008d5777ca84d04c04867ec1a6f0edea4633f;hp=c2ef01b983e427bc28a85fadf4ee05cae8ff8c00;hpb=413abf5f6de7c1808a2dd716ef52ed586c04d8f6;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/restapi/operations/xapp_framework_api.go b/pkg/restapi/operations/xapp_framework_api.go index c2ef01b..83f3bb5 100644 --- a/pkg/restapi/operations/xapp_framework_api.go +++ b/pkg/restapi/operations/xapp_framework_api.go @@ -10,19 +10,16 @@ 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/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" "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/xapp" ) @@ -35,29 +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 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") }), - QueryGetAllSubscriptionsHandler: query.GetAllSubscriptionsHandlerFunc(func(params query.GetAllSubscriptionsParams) middleware.Responder { - return middleware.NotImplemented("operation QueryGetAllSubscriptions 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") }), XappGetXappConfigListHandler: xapp.GetXappConfigListHandlerFunc(func(params xapp.GetXappConfigListParams) middleware.Responder { - return middleware.NotImplemented("operation XappGetXappConfigList has not yet been implemented") - }), - PolicySubscribePolicyHandler: policy.SubscribePolicyHandlerFunc(func(params policy.SubscribePolicyParams) middleware.Responder { - return middleware.NotImplemented("operation PolicySubscribePolicy has not yet been implemented") - }), - ReportSubscribeReportHandler: report.SubscribeReportHandlerFunc(func(params report.SubscribeReportParams) middleware.Responder { - return middleware.NotImplemented("operation ReportSubscribeReport has not yet been implemented") + return middleware.NotImplemented("operation xapp.GetXappConfigList has not yet been implemented") }), } } @@ -84,29 +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 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 + CommonSubscribeHandler common.SubscribeHandler // CommonUnsubscribeHandler sets the operation handler for the unsubscribe operation CommonUnsubscribeHandler common.UnsubscribeHandler - // QueryGetAllSubscriptionsHandler sets the operation handler for the get all subscriptions operation - QueryGetAllSubscriptionsHandler query.GetAllSubscriptionsHandler + // 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 - // 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 - // 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() @@ -164,31 +166,23 @@ 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.QueryGetAllSubscriptionsHandler == nil { - unregistered = append(unregistered, "query.GetAllSubscriptionsHandler") + if o.CommonGetAllSubscriptionsHandler == nil { + unregistered = append(unregistered, "common.GetAllSubscriptionsHandler") } - if o.XappGetXappConfigListHandler == nil { unregistered = append(unregistered, "xapp.GetXappConfigListHandler") } - if o.PolicySubscribePolicyHandler == nil { - unregistered = append(unregistered, "policy.SubscribePolicyHandler") - } - - if o.ReportSubscribeReportHandler == nil { - unregistered = append(unregistered, "report.SubscribeReportHandler") - } - if len(unregistered) > 0 { return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", ")) } @@ -203,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 { @@ -232,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 { @@ -255,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 @@ -285,36 +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["GET"] == nil { o.handlers["GET"] = make(map[string]http.Handler) } - o.handlers["GET"]["/subscriptions"] = query.NewGetAllSubscriptions(o.context, o.QueryGetAllSubscriptionsHandler) - + 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) - - if o.handlers["POST"] == nil { - o.handlers["POST"] = 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["POST"]["/subscriptions/report"] = report.NewSubscribeReport(o.context, o.ReportSubscribeReportHandler) - } // Serve creates a http handler to serve the API over HTTP @@ -344,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) + } +}