Improve locking/unlocking
[nonrtric/plt/sme.git] / capifcore / internal / invokermanagement / invokermanagement.go
index c1ff264..52b86cd 100644 (file)
@@ -27,6 +27,7 @@ import (
        "strings"
        "sync"
 
+       "oransc.org/nonrtric/capifcore/internal/eventsapi"
        publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
 
        "oransc.org/nonrtric/capifcore/internal/common29122"
@@ -54,15 +55,17 @@ type InvokerManager struct {
        onboardedInvokers map[string]invokerapi.APIInvokerEnrolmentDetails
        publishRegister   publishservice.PublishRegister
        nextId            int64
+       eventChannel      chan<- eventsapi.EventNotification
        lock              sync.Mutex
 }
 
 // Creates a manager that implements both the InvokerRegister and the invokermanagementapi.ServerInterface interfaces.
-func NewInvokerManager(publishRegister publishservice.PublishRegister) *InvokerManager {
+func NewInvokerManager(publishRegister publishservice.PublishRegister, eventChannel chan<- eventsapi.EventNotification) *InvokerManager {
        return &InvokerManager{
                onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails),
                publishRegister:   publishRegister,
                nextId:            1000,
+               eventChannel:      eventChannel,
        }
 }
 
@@ -110,22 +113,9 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
                return coreError
        }
 
-       im.lock.Lock()
-       defer im.lock.Unlock()
+       im.prepareNewInvoker(&newInvoker)
 
-       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
-
-       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()
        ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
@@ -138,12 +128,36 @@ 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)
 }
@@ -165,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")
        }
@@ -221,6 +234,17 @@ func (im *InvokerManager) getId(invokerInfo *string) *string {
        return &idAsString
 }
 
+func (im *InvokerManager) sendEvent(invokerId string, eventType eventsapi.CAPIFEvent) {
+       invokerIds := []string{invokerId}
+       event := eventsapi.EventNotification{
+               EventDetail: &eventsapi.CAPIFEventDetail{
+                       ApiInvokerIds: &invokerIds,
+               },
+               Events: eventType,
+       }
+       im.eventChannel <- event
+}
+
 // This function wraps sending of an error in the Error format, and
 // handling the failure to marshal that.
 func sendCoreError(ctx echo.Context, code int, message string) error {