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 IsInvokerRegistered(invokerId string) bool
43 VerifyInvokerSecret(invokerId, secret string) bool
46 type InvokerManager struct {
47 onboardedInvokers map[string]invokerapi.APIInvokerEnrolmentDetails
48 apiRegister publishservice.APIRegister
53 func NewInvokerManager(apiRegister publishservice.APIRegister) *InvokerManager {
54 return &InvokerManager{
55 onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails),
56 apiRegister: apiRegister,
61 func (im *InvokerManager) IsInvokerRegistered(invokerId string) bool {
63 defer im.lock.Unlock()
65 _, registered := im.onboardedInvokers[invokerId]
69 func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
71 defer im.lock.Unlock()
74 if invoker, registered := im.onboardedInvokers[invokerId]; registered {
75 verified = *invoker.OnboardingInformation.OnboardingSecret == secret
80 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
81 var newInvoker invokerapi.APIInvokerEnrolmentDetails
82 err := ctx.Bind(&newInvoker)
84 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
87 shouldReturn, coreError := im.validateInvoker(newInvoker, ctx)
93 defer im.lock.Unlock()
95 newInvoker.ApiInvokerId = im.getId(newInvoker.ApiInvokerInformation)
96 onboardingSecret := "onboarding_secret_"
97 if newInvoker.ApiInvokerInformation != nil {
98 onboardingSecret = onboardingSecret + strings.ReplaceAll(*newInvoker.ApiInvokerInformation, " ", "_")
100 onboardingSecret = onboardingSecret + *newInvoker.ApiInvokerId
102 newInvoker.OnboardingInformation.OnboardingSecret = &onboardingSecret
104 im.onboardedInvokers[*newInvoker.ApiInvokerId] = newInvoker
106 uri := ctx.Request().Host + ctx.Request().URL.String()
107 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
108 err = ctx.JSON(http.StatusCreated, newInvoker)
110 // Something really bad happened, tell Echo that our handler failed
117 func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
119 defer im.lock.Unlock()
121 delete(im.onboardedInvokers, onboardingId)
123 return ctx.NoContent(http.StatusNoContent)
126 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
127 var invoker invokerapi.APIInvokerEnrolmentDetails
128 err := ctx.Bind(&invoker)
130 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
133 if onboardingId != *invoker.ApiInvokerId {
134 return sendCoreError(ctx, http.StatusBadRequest, "Invoker ApiInvokerId not matching")
137 shouldReturn, coreError := im.validateInvoker(invoker, ctx)
143 defer im.lock.Unlock()
145 if _, ok := im.onboardedInvokers[onboardingId]; ok {
146 im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
148 return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
151 err = ctx.JSON(http.StatusOK, invoker)
153 // Something really bad happened, tell Echo that our handler failed
160 func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
161 return ctx.NoContent(http.StatusNotImplemented)
164 func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) (bool, error) {
165 if invoker.NotificationDestination == "" {
166 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
169 if invoker.OnboardingInformation.ApiInvokerPublicKey == "" {
170 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
173 if !im.areAPIsRegistered(invoker.ApiList) {
174 return true, sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
180 func (im *InvokerManager) areAPIsRegistered(apis *invokerapi.APIList) bool {
184 return im.apiRegister.AreAPIsRegistered((*[]publishapi.ServiceAPIDescription)(apis))
187 func (im *InvokerManager) getId(invokerInfo *string) *string {
188 idAsString := "api_invoker_id_"
189 if invokerInfo != nil {
190 idAsString = idAsString + strings.ReplaceAll(*invokerInfo, " ", "_")
192 idAsString = idAsString + strconv.FormatInt(im.nextId, 10)
193 im.nextId = im.nextId + 1
198 // This function wraps sending of an error in the Error format, and
199 // handling the failure to marshal that.
200 func sendCoreError(ctx echo.Context, code int, message string) error {
201 pd := common29122.ProblemDetails{
205 err := ctx.JSON(code, pd)