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
30 "oransc.org/nonrtric/capifcore/internal/eventsapi"
31 publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
33 "oransc.org/nonrtric/capifcore/internal/common29122"
34 invokerapi "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
36 "oransc.org/nonrtric/capifcore/internal/publishservice"
38 "github.com/labstack/echo/v4"
41 //go:generate mockery --name InvokerRegister
42 type InvokerRegister interface {
43 // Checks if the invoker is registered.
44 // Returns true of the provided invoker is registered, false otherwise.
45 IsInvokerRegistered(invokerId string) bool
46 // Verifies that the provided secret is the invoker's registered secret.
47 // Returns true if the provided secret is the registered invoker's secret, false otherwise.
48 VerifyInvokerSecret(invokerId, secret string) bool
49 // Gets the provided invoker's registered APIs.
50 // Returns a list of all the invoker's registered APIs.
51 GetInvokerApiList(invokerId string) *invokerapi.APIList
54 type InvokerManager struct {
55 onboardedInvokers map[string]invokerapi.APIInvokerEnrolmentDetails
56 publishRegister publishservice.PublishRegister
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, eventChannel chan<- eventsapi.EventNotification) *InvokerManager {
64 return &InvokerManager{
65 onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails),
66 publishRegister: publishRegister,
68 eventChannel: eventChannel,
72 func (im *InvokerManager) IsInvokerRegistered(invokerId string) bool {
74 defer im.lock.Unlock()
76 _, registered := im.onboardedInvokers[invokerId]
80 func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
82 defer im.lock.Unlock()
85 if invoker, registered := im.onboardedInvokers[invokerId]; registered {
86 verified = *invoker.OnboardingInformation.OnboardingSecret == secret
91 func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APIList {
92 invoker, ok := im.onboardedInvokers[invokerId]
94 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
96 defer im.lock.Unlock()
97 invoker.ApiList = &apiList
103 // Creates a new individual API Invoker profile.
104 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
105 var newInvoker invokerapi.APIInvokerEnrolmentDetails
106 err := ctx.Bind(&newInvoker)
108 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
111 shouldReturn, coreError := im.validateInvoker(newInvoker, ctx)
116 im.prepareNewInvoker(&newInvoker)
118 go im.sendEvent(*newInvoker.ApiInvokerId, eventsapi.CAPIFEventAPIINVOKERONBOARDED)
120 uri := ctx.Request().Host + ctx.Request().URL.String()
121 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
122 err = ctx.JSON(http.StatusCreated, newInvoker)
124 // Something really bad happened, tell Echo that our handler failed
131 func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnrolmentDetails) {
133 defer im.lock.Unlock()
135 newInvoker.ApiInvokerId = im.getId(newInvoker.ApiInvokerInformation)
136 newInvoker.OnboardingInformation.OnboardingSecret = getOnboardingSecret(*newInvoker)
138 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
139 newInvoker.ApiList = &apiList
141 im.onboardedInvokers[*newInvoker.ApiInvokerId] = *newInvoker
144 func getOnboardingSecret(newInvoker invokerapi.APIInvokerEnrolmentDetails) *string {
145 onboardingSecret := "onboarding_secret_"
146 if newInvoker.ApiInvokerInformation != nil {
147 onboardingSecret = onboardingSecret + strings.ReplaceAll(*newInvoker.ApiInvokerInformation, " ", "_")
149 onboardingSecret = onboardingSecret + *newInvoker.ApiInvokerId
151 return &onboardingSecret
154 // Deletes an individual API Invoker.
155 func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
157 delete(im.onboardedInvokers, onboardingId)
160 go im.sendEvent(onboardingId, eventsapi.CAPIFEventAPIINVOKEROFFBOARDED)
162 return ctx.NoContent(http.StatusNoContent)
165 // Updates an individual API invoker details.
166 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
167 var invoker invokerapi.APIInvokerEnrolmentDetails
168 err := ctx.Bind(&invoker)
170 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
173 if onboardingId != *invoker.ApiInvokerId {
174 return sendCoreError(ctx, http.StatusBadRequest, "Invoker ApiInvokerId not matching")
177 shouldReturn, coreError := im.validateInvoker(invoker, ctx)
182 if _, ok := im.onboardedInvokers[onboardingId]; ok {
184 im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
187 return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
190 err = ctx.JSON(http.StatusOK, invoker)
192 // Something really bad happened, tell Echo that our handler failed
199 func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
200 return ctx.NoContent(http.StatusNotImplemented)
203 func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) (bool, error) {
204 if invoker.NotificationDestination == "" {
205 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
208 if invoker.OnboardingInformation.ApiInvokerPublicKey == "" {
209 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
212 if !im.areAPIsPublished(invoker.ApiList) {
213 return true, sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
219 func (im *InvokerManager) areAPIsPublished(apis *invokerapi.APIList) bool {
223 return im.publishRegister.AreAPIsPublished((*[]publishapi.ServiceAPIDescription)(apis))
226 func (im *InvokerManager) getId(invokerInfo *string) *string {
227 idAsString := "api_invoker_id_"
228 if invokerInfo != nil {
229 idAsString = idAsString + strings.ReplaceAll(*invokerInfo, " ", "_")
231 idAsString = idAsString + strconv.FormatInt(im.nextId, 10)
232 im.nextId = im.nextId + 1
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)