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
44 GetInvokerApiList(invokerId string) *invokerapi.APIList
47 type InvokerManager struct {
48 onboardedInvokers map[string]invokerapi.APIInvokerEnrolmentDetails
49 apiRegister publishservice.APIRegister
54 func NewInvokerManager(apiRegister publishservice.APIRegister) *InvokerManager {
55 return &InvokerManager{
56 onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails),
57 apiRegister: apiRegister,
62 func (im *InvokerManager) IsInvokerRegistered(invokerId string) bool {
64 defer im.lock.Unlock()
66 _, registered := im.onboardedInvokers[invokerId]
70 func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
72 defer im.lock.Unlock()
75 if invoker, registered := im.onboardedInvokers[invokerId]; registered {
76 verified = *invoker.OnboardingInformation.OnboardingSecret == secret
81 func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APIList {
82 invoker, ok := im.onboardedInvokers[invokerId]
84 return invoker.ApiList
89 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
90 var newInvoker invokerapi.APIInvokerEnrolmentDetails
91 err := ctx.Bind(&newInvoker)
93 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
96 shouldReturn, coreError := im.validateInvoker(newInvoker, ctx)
102 defer im.lock.Unlock()
104 newInvoker.ApiInvokerId = im.getId(newInvoker.ApiInvokerInformation)
105 onboardingSecret := "onboarding_secret_"
106 if newInvoker.ApiInvokerInformation != nil {
107 onboardingSecret = onboardingSecret + strings.ReplaceAll(*newInvoker.ApiInvokerInformation, " ", "_")
109 onboardingSecret = onboardingSecret + *newInvoker.ApiInvokerId
111 newInvoker.OnboardingInformation.OnboardingSecret = &onboardingSecret
113 im.onboardedInvokers[*newInvoker.ApiInvokerId] = newInvoker
115 uri := ctx.Request().Host + ctx.Request().URL.String()
116 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
117 err = ctx.JSON(http.StatusCreated, newInvoker)
119 // Something really bad happened, tell Echo that our handler failed
126 func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
128 defer im.lock.Unlock()
130 delete(im.onboardedInvokers, onboardingId)
132 return ctx.NoContent(http.StatusNoContent)
135 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
136 var invoker invokerapi.APIInvokerEnrolmentDetails
137 err := ctx.Bind(&invoker)
139 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
142 if onboardingId != *invoker.ApiInvokerId {
143 return sendCoreError(ctx, http.StatusBadRequest, "Invoker ApiInvokerId not matching")
146 shouldReturn, coreError := im.validateInvoker(invoker, ctx)
152 defer im.lock.Unlock()
154 if _, ok := im.onboardedInvokers[onboardingId]; ok {
155 im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
157 return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
160 err = ctx.JSON(http.StatusOK, invoker)
162 // Something really bad happened, tell Echo that our handler failed
169 func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
170 return ctx.NoContent(http.StatusNotImplemented)
173 func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) (bool, error) {
174 if invoker.NotificationDestination == "" {
175 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
178 if invoker.OnboardingInformation.ApiInvokerPublicKey == "" {
179 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
182 if !im.areAPIsRegistered(invoker.ApiList) {
183 return true, sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
189 func (im *InvokerManager) areAPIsRegistered(apis *invokerapi.APIList) bool {
193 return im.apiRegister.AreAPIsRegistered((*[]publishapi.ServiceAPIDescription)(apis))
196 func (im *InvokerManager) getId(invokerInfo *string) *string {
197 idAsString := "api_invoker_id_"
198 if invokerInfo != nil {
199 idAsString = idAsString + strings.ReplaceAll(*invokerInfo, " ", "_")
201 idAsString = idAsString + strconv.FormatInt(im.nextId, 10)
202 im.nextId = im.nextId + 1
207 // This function wraps sending of an error in the Error format, and
208 // handling the failure to marshal that.
209 func sendCoreError(ctx echo.Context, code int, message string) error {
210 pd := common29122.ProblemDetails{
214 err := ctx.JSON(code, pd)