From 3928f2d539956636d31902d9e3650a3a50410df3 Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Mon, 9 Jan 2023 11:28:54 +0100 Subject: [PATCH] Improve locking/unlocking Issue-ID: NONRTRIC-814 Signed-off-by: elinuxhenrik Change-Id: Ifd25c12c7f865e691f50805820f613d1c9a30def --- capifcore/internal/eventservice/eventservice.go | 2 +- .../invokermanagement/invokermanagement.go | 48 +++++++++++++--------- .../providermanagement/providermanagement.go | 20 +++++---- .../internal/publishservice/publishservice.go | 6 +-- 4 files changed, 44 insertions(+), 32 deletions(-) diff --git a/capifcore/internal/eventservice/eventservice.go b/capifcore/internal/eventservice/eventservice.go index 0f7a8e8..21742ef 100644 --- a/capifcore/internal/eventservice/eventservice.go +++ b/capifcore/internal/eventservice/eventservice.go @@ -89,13 +89,13 @@ func (es *EventService) PostSubscriberIdSubscriptions(ctx echo.Context, subscrib func (es *EventService) DeleteSubscriberIdSubscriptionsSubscriptionId(ctx echo.Context, subscriberId string, subscriptionId string) error { es.lock.Lock() - defer es.lock.Unlock() log.Debug(es.subscriptions) if _, ok := es.subscriptions[subscriptionId]; ok { log.Debug("Deleting subscription", subscriptionId) delete(es.subscriptions, subscriptionId) } + es.lock.Unlock() return ctx.NoContent(http.StatusNoContent) } diff --git a/capifcore/internal/invokermanagement/invokermanagement.go b/capifcore/internal/invokermanagement/invokermanagement.go index ecf8de5..52b86cd 100644 --- a/capifcore/internal/invokermanagement/invokermanagement.go +++ b/capifcore/internal/invokermanagement/invokermanagement.go @@ -113,22 +113,8 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error { return coreError } - im.lock.Lock() - defer im.lock.Unlock() - - newInvoker.ApiInvokerId = im.getId(newInvoker.ApiInvokerInformation) - onboardingSecret := "onboarding_secret_" - if newInvoker.ApiInvokerInformation != nil { - onboardingSecret = onboardingSecret + strings.ReplaceAll(*newInvoker.ApiInvokerInformation, " ", "_") - } else { - onboardingSecret = onboardingSecret + *newInvoker.ApiInvokerId - } - newInvoker.OnboardingInformation.OnboardingSecret = &onboardingSecret + im.prepareNewInvoker(&newInvoker) - var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices() - newInvoker.ApiList = &apiList - - im.onboardedInvokers[*newInvoker.ApiInvokerId] = newInvoker go im.sendEvent(*newInvoker.ApiInvokerId, eventsapi.CAPIFEventAPIINVOKERONBOARDED) uri := ctx.Request().Host + ctx.Request().URL.String() @@ -142,12 +128,35 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error { return nil } -// Deletes an individual API Invoker. -func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error { +func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnrolmentDetails) { im.lock.Lock() defer im.lock.Unlock() + newInvoker.ApiInvokerId = im.getId(newInvoker.ApiInvokerInformation) + newInvoker.OnboardingInformation.OnboardingSecret = getOnboardingSecret(*newInvoker) + + var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices() + newInvoker.ApiList = &apiList + + im.onboardedInvokers[*newInvoker.ApiInvokerId] = *newInvoker +} + +func getOnboardingSecret(newInvoker invokerapi.APIInvokerEnrolmentDetails) *string { + onboardingSecret := "onboarding_secret_" + if newInvoker.ApiInvokerInformation != nil { + onboardingSecret = onboardingSecret + strings.ReplaceAll(*newInvoker.ApiInvokerInformation, " ", "_") + } else { + onboardingSecret = onboardingSecret + *newInvoker.ApiInvokerId + } + return &onboardingSecret +} + +// Deletes an individual API Invoker. +func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error { + im.lock.Lock() delete(im.onboardedInvokers, onboardingId) + im.lock.Unlock() + go im.sendEvent(onboardingId, eventsapi.CAPIFEventAPIINVOKEROFFBOARDED) return ctx.NoContent(http.StatusNoContent) @@ -170,11 +179,10 @@ func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onb return coreError } - im.lock.Lock() - defer im.lock.Unlock() - if _, ok := im.onboardedInvokers[onboardingId]; ok { + im.lock.Lock() im.onboardedInvokers[*invoker.ApiInvokerId] = invoker + im.lock.Unlock() } else { return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded") } diff --git a/capifcore/internal/providermanagement/providermanagement.go b/capifcore/internal/providermanagement/providermanagement.go index 87f297a..f278147 100644 --- a/capifcore/internal/providermanagement/providermanagement.go +++ b/capifcore/internal/providermanagement/providermanagement.go @@ -99,13 +99,7 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error { return sendCoreError(ctx, http.StatusBadRequest, "Provider missing required ApiProvDomInfo") } - pm.lock.Lock() - defer pm.lock.Unlock() - - newProvider.ApiProvDomId = pm.getDomainId(newProvider.ApiProvDomInfo) - - pm.registerFunctions(newProvider.ApiProvFuncs) - pm.onboardedProviders[*newProvider.ApiProvDomId] = newProvider + pm.prepareNewProvider(&newProvider) uri := ctx.Request().Host + ctx.Request().URL.String() ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newProvider.ApiProvDomId)) @@ -118,14 +112,24 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error { return nil } -func (pm *ProviderManager) DeleteRegistrationsRegistrationId(ctx echo.Context, registrationId string) error { +func (pm *ProviderManager) prepareNewProvider(newProvider *provapi.APIProviderEnrolmentDetails) { pm.lock.Lock() defer pm.lock.Unlock() + newProvider.ApiProvDomId = pm.getDomainId(newProvider.ApiProvDomInfo) + + pm.registerFunctions(newProvider.ApiProvFuncs) + pm.onboardedProviders[*newProvider.ApiProvDomId] = *newProvider +} + +func (pm *ProviderManager) DeleteRegistrationsRegistrationId(ctx echo.Context, registrationId string) error { + log.Debug(pm.onboardedProviders) if _, ok := pm.onboardedProviders[registrationId]; ok { log.Debug("Deleting provider", registrationId) + pm.lock.Lock() delete(pm.onboardedProviders, registrationId) + pm.lock.Unlock() } return ctx.NoContent(http.StatusNoContent) diff --git a/capifcore/internal/publishservice/publishservice.go b/capifcore/internal/publishservice/publishservice.go index d85f077..52434e8 100644 --- a/capifcore/internal/publishservice/publishservice.go +++ b/capifcore/internal/publishservice/publishservice.go @@ -224,8 +224,8 @@ func (ps *PublishService) DeleteApfIdServiceApisServiceApiId(ctx echo.Context, a log.Debug("Deleted service: ", serviceApiId) } ps.lock.Lock() - defer ps.lock.Unlock() ps.publishedServices[string(apfId)] = removeServiceDescription(pos, serviceDescriptions) + ps.lock.Unlock() go ps.sendEvent(*description, eventsapi.CAPIFEventSERVICEAPIUNAVAILABLE) } } @@ -235,9 +235,9 @@ func (ps *PublishService) DeleteApfIdServiceApisServiceApiId(ctx echo.Context, a // Retrieve a published service API. func (ps *PublishService) GetApfIdServiceApisServiceApiId(ctx echo.Context, apfId string, serviceApiId string) error { ps.lock.Lock() - defer ps.lock.Unlock() - serviceDescriptions, ok := ps.publishedServices[apfId] + ps.lock.Unlock() + if ok { _, serviceDescription := getServiceDescription(serviceApiId, serviceDescriptions) if serviceDescription == nil { -- 2.16.6