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"
30 "oransc.org/nonrtric/capifcore/internal/keycloak"
32 "oransc.org/nonrtric/capifcore/internal/common29122"
33 invokerapi "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
35 "oransc.org/nonrtric/capifcore/internal/publishservice"
37 "github.com/labstack/echo/v4"
40 //go:generate mockery --name InvokerRegister
41 type InvokerRegister interface {
42 // Checks if the invoker is registered.
43 // Returns true of the provided invoker is registered, false otherwise.
44 IsInvokerRegistered(invokerId string) bool
45 // Verifies that the provided secret is the invoker's registered secret.
46 // Returns true if the provided secret is the registered invoker's secret, false otherwise.
47 VerifyInvokerSecret(invokerId, secret string) bool
48 // Gets the provided invoker's registered APIs.
49 // Returns a list of all the invoker's registered APIs.
50 GetInvokerApiList(invokerId string) *invokerapi.APIList
53 type InvokerManager struct {
54 onboardedInvokers map[string]invokerapi.APIInvokerEnrolmentDetails
55 publishRegister publishservice.PublishRegister
57 keycloak keycloak.AccessManagement
58 eventChannel chan<- eventsapi.EventNotification
62 // Creates a manager that implements both the InvokerRegister and the invokermanagementapi.ServerInterface interfaces.
63 func NewInvokerManager(publishRegister publishservice.PublishRegister, km keycloak.AccessManagement, eventChannel chan<- eventsapi.EventNotification) *InvokerManager {
64 return &InvokerManager{
65 onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails),
66 publishRegister: publishRegister,
69 eventChannel: eventChannel,
73 func (im *InvokerManager) IsInvokerRegistered(invokerId string) bool {
75 defer im.lock.Unlock()
77 _, registered := im.onboardedInvokers[invokerId]
81 func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
83 defer im.lock.Unlock()
86 if invoker, registered := im.onboardedInvokers[invokerId]; registered {
87 verified = *invoker.OnboardingInformation.OnboardingSecret == secret
92 func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APIList {
93 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
95 defer im.lock.Unlock()
96 invoker, ok := im.onboardedInvokers[invokerId]
98 invoker.ApiList = &apiList
104 // Creates a new individual API Invoker profile.
105 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
106 var newInvoker invokerapi.APIInvokerEnrolmentDetails
107 errMsg := "Unable to onboard invoker due to %s"
108 if err := ctx.Bind(&newInvoker); err != nil {
109 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
112 if err := im.isInvokerOnboarded(newInvoker); err != nil {
113 return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err))
116 if err := im.validateInvoker(newInvoker, ctx); err != nil {
117 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
120 im.prepareNewInvoker(&newInvoker)
122 go im.sendEvent(*newInvoker.ApiInvokerId, eventsapi.CAPIFEventAPIINVOKERONBOARDED)
124 uri := ctx.Request().Host + ctx.Request().URL.String()
125 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
126 err := ctx.JSON(http.StatusCreated, newInvoker)
128 // Something really bad happened, tell Echo that our handler failed
135 func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnrolmentDetails) error {
136 for _, invoker := range im.onboardedInvokers {
137 if err := invoker.ValidateAlreadyOnboarded(newInvoker); err != nil {
144 func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnrolmentDetails) {
145 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
146 newInvoker.ApiList = &apiList
149 defer im.lock.Unlock()
151 newInvoker.PrepareNewInvoker()
153 im.addClientInKeycloak(newInvoker)
155 im.onboardedInvokers[*newInvoker.ApiInvokerId] = *newInvoker
158 func (im *InvokerManager) addClientInKeycloak(newInvoker *invokerapi.APIInvokerEnrolmentDetails) error {
159 if err := im.keycloak.AddClient(*newInvoker.ApiInvokerId, "invokerrealm"); err != nil {
163 if body, err := im.keycloak.GetClientRepresentation(*newInvoker.ApiInvokerId, "invokerrealm"); err != nil {
166 newInvoker.OnboardingInformation.OnboardingSecret = body.Secret
171 // Deletes an individual API Invoker.
172 func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
173 if _, ok := im.onboardedInvokers[onboardingId]; ok {
174 im.deleteInvoker(onboardingId)
177 go im.sendEvent(onboardingId, eventsapi.CAPIFEventAPIINVOKEROFFBOARDED)
179 return ctx.NoContent(http.StatusNoContent)
182 func (im *InvokerManager) deleteInvoker(onboardingId string) {
184 defer im.lock.Unlock()
185 delete(im.onboardedInvokers, onboardingId)
188 // Updates an individual API invoker details.
189 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
190 var invoker invokerapi.APIInvokerEnrolmentDetails
191 errMsg := "Unable to update invoker due to %s"
192 if err := ctx.Bind(&invoker); err != nil {
193 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
196 if onboardingId != *invoker.ApiInvokerId {
197 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "ApiInvokerId not matching"))
200 if err := im.validateInvoker(invoker, ctx); err != nil {
201 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
204 if _, ok := im.onboardedInvokers[onboardingId]; ok {
205 im.updateInvoker(invoker)
207 return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
210 err := ctx.JSON(http.StatusOK, invoker)
212 // Something really bad happened, tell Echo that our handler failed
219 func (im *InvokerManager) updateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails) {
221 defer im.lock.Unlock()
222 im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
225 func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
226 return ctx.NoContent(http.StatusNotImplemented)
229 func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) error {
230 if err := invoker.Validate(); err != nil {
237 func (im *InvokerManager) sendEvent(invokerId string, eventType eventsapi.CAPIFEvent) {
238 invokerIds := []string{invokerId}
239 event := eventsapi.EventNotification{
240 EventDetail: &eventsapi.CAPIFEventDetail{
241 ApiInvokerIds: &invokerIds,
245 im.eventChannel <- event
248 // This function wraps sending of an error in the Error format, and
249 // handling the failure to marshal that.
250 func sendCoreError(ctx echo.Context, code int, message string) error {
251 pd := common29122.ProblemDetails{
255 err := ctx.JSON(code, pd)