2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
21 package invokermanagement
29 "oransc.org/nonrtric/capifcore/internal/eventsapi"
31 "oransc.org/nonrtric/capifcore/internal/common29122"
32 invokerapi "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
34 "oransc.org/nonrtric/capifcore/internal/publishservice"
36 "github.com/labstack/echo/v4"
39 //go:generate mockery --name InvokerRegister
40 type InvokerRegister interface {
41 // Checks if the invoker is registered.
42 // Returns true of the provided invoker is registered, false otherwise.
43 IsInvokerRegistered(invokerId string) bool
44 // Verifies that the provided secret is the invoker's registered secret.
45 // Returns true if the provided secret is the registered invoker's secret, false otherwise.
46 VerifyInvokerSecret(invokerId, secret string) bool
47 // Gets the provided invoker's registered APIs.
48 // Returns a list of all the invoker's registered APIs.
49 GetInvokerApiList(invokerId string) *invokerapi.APIList
52 type InvokerManager struct {
53 onboardedInvokers map[string]invokerapi.APIInvokerEnrolmentDetails
54 publishRegister publishservice.PublishRegister
56 eventChannel chan<- eventsapi.EventNotification
60 // Creates a manager that implements both the InvokerRegister and the invokermanagementapi.ServerInterface interfaces.
61 func NewInvokerManager(publishRegister publishservice.PublishRegister, eventChannel chan<- eventsapi.EventNotification) *InvokerManager {
62 return &InvokerManager{
63 onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails),
64 publishRegister: publishRegister,
66 eventChannel: eventChannel,
70 func (im *InvokerManager) IsInvokerRegistered(invokerId string) bool {
72 defer im.lock.Unlock()
74 _, registered := im.onboardedInvokers[invokerId]
78 func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
80 defer im.lock.Unlock()
83 if invoker, registered := im.onboardedInvokers[invokerId]; registered {
84 verified = *invoker.OnboardingInformation.OnboardingSecret == secret
89 func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APIList {
90 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
92 defer im.lock.Unlock()
93 invoker, ok := im.onboardedInvokers[invokerId]
95 invoker.ApiList = &apiList
101 // Creates a new individual API Invoker profile.
102 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
103 var newInvoker invokerapi.APIInvokerEnrolmentDetails
104 errMsg := "Unable to onboard invoker due to %s"
105 if err := ctx.Bind(&newInvoker); err != nil {
106 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
109 if err := im.isInvokerOnboarded(newInvoker); err != nil {
110 return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err))
113 if err := im.validateInvoker(newInvoker, ctx); err != nil {
114 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
117 im.prepareNewInvoker(&newInvoker)
119 go im.sendEvent(*newInvoker.ApiInvokerId, eventsapi.CAPIFEventAPIINVOKERONBOARDED)
121 uri := ctx.Request().Host + ctx.Request().URL.String()
122 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
123 err := ctx.JSON(http.StatusCreated, newInvoker)
125 // Something really bad happened, tell Echo that our handler failed
132 func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnrolmentDetails) error {
133 for _, invoker := range im.onboardedInvokers {
134 if err := invoker.ValidateAlreadyOnboarded(newInvoker); err != nil {
141 func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnrolmentDetails) {
142 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
143 newInvoker.ApiList = &apiList
146 defer im.lock.Unlock()
148 newInvoker.PrepareNewInvoker()
150 im.onboardedInvokers[*newInvoker.ApiInvokerId] = *newInvoker
153 // Deletes an individual API Invoker.
154 func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
155 if _, ok := im.onboardedInvokers[onboardingId]; ok {
156 im.deleteInvoker(onboardingId)
159 go im.sendEvent(onboardingId, eventsapi.CAPIFEventAPIINVOKEROFFBOARDED)
161 return ctx.NoContent(http.StatusNoContent)
164 func (im *InvokerManager) deleteInvoker(onboardingId string) {
166 defer im.lock.Unlock()
167 delete(im.onboardedInvokers, onboardingId)
170 // Updates an individual API invoker details.
171 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
172 var invoker invokerapi.APIInvokerEnrolmentDetails
173 errMsg := "Unable to update invoker due to %s"
174 if err := ctx.Bind(&invoker); err != nil {
175 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
178 if onboardingId != *invoker.ApiInvokerId {
179 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "ApiInvokerId not matching"))
182 if err := im.validateInvoker(invoker, ctx); err != nil {
183 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
186 if _, ok := im.onboardedInvokers[onboardingId]; ok {
187 im.updateInvoker(invoker)
189 return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
192 err := ctx.JSON(http.StatusOK, invoker)
194 // Something really bad happened, tell Echo that our handler failed
201 func (im *InvokerManager) updateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails) {
203 defer im.lock.Unlock()
204 im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
207 func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
208 return ctx.NoContent(http.StatusNotImplemented)
211 func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) error {
212 if err := invoker.Validate(); err != nil {
219 func (im *InvokerManager) sendEvent(invokerId string, eventType eventsapi.CAPIFEvent) {
220 invokerIds := []string{invokerId}
221 event := eventsapi.EventNotification{
222 EventDetail: &eventsapi.CAPIFEventDetail{
223 ApiInvokerIds: &invokerIds,
227 im.eventChannel <- event
230 // This function wraps sending of an error in the Error format, and
231 // handling the failure to marshal that.
232 func sendCoreError(ctx echo.Context, code int, message string) error {
233 pd := common29122.ProblemDetails{
237 err := ctx.JSON(code, pd)