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 return invoker.ApiList
96 // Creates a new individual API Invoker profile.
97 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
98 var newInvoker invokerapi.APIInvokerEnrolmentDetails
99 err := ctx.Bind(&newInvoker)
101 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
104 shouldReturn, coreError := im.validateInvoker(newInvoker, ctx)
110 defer im.lock.Unlock()
112 newInvoker.ApiInvokerId = im.getId(newInvoker.ApiInvokerInformation)
113 onboardingSecret := "onboarding_secret_"
114 if newInvoker.ApiInvokerInformation != nil {
115 onboardingSecret = onboardingSecret + strings.ReplaceAll(*newInvoker.ApiInvokerInformation, " ", "_")
117 onboardingSecret = onboardingSecret + *newInvoker.ApiInvokerId
119 newInvoker.OnboardingInformation.OnboardingSecret = &onboardingSecret
121 var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
122 newInvoker.ApiList = &apiList
124 im.onboardedInvokers[*newInvoker.ApiInvokerId] = newInvoker
126 uri := ctx.Request().Host + ctx.Request().URL.String()
127 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
128 err = ctx.JSON(http.StatusCreated, newInvoker)
130 // Something really bad happened, tell Echo that our handler failed
137 // Deletes an individual API Invoker.
138 func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
140 defer im.lock.Unlock()
142 delete(im.onboardedInvokers, onboardingId)
144 return ctx.NoContent(http.StatusNoContent)
147 // Updates an individual API invoker details.
148 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
149 var invoker invokerapi.APIInvokerEnrolmentDetails
150 err := ctx.Bind(&invoker)
152 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
155 if onboardingId != *invoker.ApiInvokerId {
156 return sendCoreError(ctx, http.StatusBadRequest, "Invoker ApiInvokerId not matching")
159 shouldReturn, coreError := im.validateInvoker(invoker, ctx)
165 defer im.lock.Unlock()
167 if _, ok := im.onboardedInvokers[onboardingId]; ok {
168 im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
170 return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
173 err = ctx.JSON(http.StatusOK, invoker)
175 // Something really bad happened, tell Echo that our handler failed
182 func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
183 return ctx.NoContent(http.StatusNotImplemented)
186 func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) (bool, error) {
187 if invoker.NotificationDestination == "" {
188 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
191 if invoker.OnboardingInformation.ApiInvokerPublicKey == "" {
192 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
195 if !im.areAPIsPublished(invoker.ApiList) {
196 return true, sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
202 func (im *InvokerManager) areAPIsPublished(apis *invokerapi.APIList) bool {
206 return im.publishRegister.AreAPIsPublished((*[]publishapi.ServiceAPIDescription)(apis))
209 func (im *InvokerManager) getId(invokerInfo *string) *string {
210 idAsString := "api_invoker_id_"
211 if invokerInfo != nil {
212 idAsString = idAsString + strings.ReplaceAll(*invokerInfo, " ", "_")
214 idAsString = idAsString + strconv.FormatInt(im.nextId, 10)
215 im.nextId = im.nextId + 1
220 // This function wraps sending of an error in the Error format, and
221 // handling the failure to marshal that.
222 func sendCoreError(ctx echo.Context, code int, message string) error {
223 pd := common29122.ProblemDetails{
227 err := ctx.JSON(code, pd)