Improve locking/unlocking 43/10243/2
authorelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 9 Jan 2023 10:28:54 +0000 (11:28 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 9 Jan 2023 12:30:12 +0000 (13:30 +0100)
Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: Ifd25c12c7f865e691f50805820f613d1c9a30def

capifcore/internal/eventservice/eventservice.go
capifcore/internal/invokermanagement/invokermanagement.go
capifcore/internal/providermanagement/providermanagement.go
capifcore/internal/publishservice/publishservice.go

index 0f7a8e8..21742ef 100644 (file)
@@ -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)
 }
index ecf8de5..52b86cd 100644 (file)
@@ -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")
        }
index 87f297a..f278147 100644 (file)
@@ -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)
index d85f077..52434e8 100644 (file)
@@ -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 {