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 publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
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
60 // Creates a manager that implements both the InvokerRegister and the invokermanagementapi.ServerInterface interfaces.
61 func NewInvokerManager(publishRegister publishservice.PublishRegister) *InvokerManager {
62 return &InvokerManager{
63 onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails),
64 publishRegister: publishRegister,
69 func (im *InvokerManager) IsInvokerRegistered(invokerId string) bool {
71 defer im.lock.Unlock()
73 _, registered := im.onboardedInvokers[invokerId]
77 func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
79 defer im.lock.Unlock()
82 if invoker, registered := im.onboardedInvokers[invokerId]; registered {
83 verified = *invoker.OnboardingInformation.OnboardingSecret == secret
88 func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APIList {
89 invoker, ok := im.onboardedInvokers[invokerId]
91 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
93 defer im.lock.Unlock()
94 invoker.ApiList = &apiList
100 // Creates a new individual API Invoker profile.
101 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
102 var newInvoker invokerapi.APIInvokerEnrolmentDetails
103 err := ctx.Bind(&newInvoker)
105 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
108 shouldReturn, coreError := im.validateInvoker(newInvoker, ctx)
114 defer im.lock.Unlock()
116 newInvoker.ApiInvokerId = im.getId(newInvoker.ApiInvokerInformation)
117 onboardingSecret := "onboarding_secret_"
118 if newInvoker.ApiInvokerInformation != nil {
119 onboardingSecret = onboardingSecret + strings.ReplaceAll(*newInvoker.ApiInvokerInformation, " ", "_")
121 onboardingSecret = onboardingSecret + *newInvoker.ApiInvokerId
123 newInvoker.OnboardingInformation.OnboardingSecret = &onboardingSecret
125 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
126 newInvoker.ApiList = &apiList
128 im.onboardedInvokers[*newInvoker.ApiInvokerId] = newInvoker
130 uri := ctx.Request().Host + ctx.Request().URL.String()
131 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
132 err = ctx.JSON(http.StatusCreated, newInvoker)
134 // Something really bad happened, tell Echo that our handler failed
141 // Deletes an individual API Invoker.
142 func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
144 defer im.lock.Unlock()
146 delete(im.onboardedInvokers, onboardingId)
148 return ctx.NoContent(http.StatusNoContent)
151 // Updates an individual API invoker details.
152 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
153 var invoker invokerapi.APIInvokerEnrolmentDetails
154 err := ctx.Bind(&invoker)
156 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
159 if onboardingId != *invoker.ApiInvokerId {
160 return sendCoreError(ctx, http.StatusBadRequest, "Invoker ApiInvokerId not matching")
163 shouldReturn, coreError := im.validateInvoker(invoker, ctx)
169 defer im.lock.Unlock()
171 if _, ok := im.onboardedInvokers[onboardingId]; ok {
172 im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
174 return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
177 err = ctx.JSON(http.StatusOK, invoker)
179 // Something really bad happened, tell Echo that our handler failed
186 func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
187 return ctx.NoContent(http.StatusNotImplemented)
190 func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) (bool, error) {
191 if invoker.NotificationDestination == "" {
192 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
195 if invoker.OnboardingInformation.ApiInvokerPublicKey == "" {
196 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
199 if !im.areAPIsPublished(invoker.ApiList) {
200 return true, sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
206 func (im *InvokerManager) areAPIsPublished(apis *invokerapi.APIList) bool {
210 return im.publishRegister.AreAPIsPublished((*[]publishapi.ServiceAPIDescription)(apis))
213 func (im *InvokerManager) getId(invokerInfo *string) *string {
214 idAsString := "api_invoker_id_"
215 if invokerInfo != nil {
216 idAsString = idAsString + strings.ReplaceAll(*invokerInfo, " ", "_")
218 idAsString = idAsString + strconv.FormatInt(im.nextId, 10)
219 im.nextId = im.nextId + 1
224 // This function wraps sending of an error in the Error format, and
225 // handling the failure to marshal that.
226 func sendCoreError(ctx echo.Context, code int, message string) error {
227 pd := common29122.ProblemDetails{
231 err := ctx.JSON(code, pd)