Refactored put in publish service
[nonrtric/plt/sme.git] / capifcore / internal / publishservice / publishservice.go
index 2314039..3209024 100644 (file)
@@ -31,6 +31,7 @@ import (
        "k8s.io/utils/strings/slices"
 
        "oransc.org/nonrtric/capifcore/internal/common29122"
+       "oransc.org/nonrtric/capifcore/internal/eventsapi"
        publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
 
        "oransc.org/nonrtric/capifcore/internal/helmmanagement"
@@ -56,15 +57,17 @@ type PublishService struct {
        publishedServices map[string][]publishapi.ServiceAPIDescription
        serviceRegister   providermanagement.ServiceRegister
        helmManager       helmmanagement.HelmManager
+       eventChannel      chan<- eventsapi.EventNotification
        lock              sync.Mutex
 }
 
 // Creates a service that implements both the PublishRegister and the publishserviceapi.ServerInterface interfaces.
-func NewPublishService(serviceRegister providermanagement.ServiceRegister, hm helmmanagement.HelmManager) *PublishService {
+func NewPublishService(serviceRegister providermanagement.ServiceRegister, hm helmmanagement.HelmManager, eventChannel chan<- eventsapi.EventNotification) *PublishService {
        return &PublishService{
                helmManager:       hm,
                publishedServices: make(map[string][]publishapi.ServiceAPIDescription),
                serviceRegister:   serviceRegister,
+               eventChannel:      eventChannel,
        }
 }
 
@@ -177,6 +180,7 @@ func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) e
        if shouldReturn {
                return returnValue
        }
+       go ps.sendEvent(newServiceAPIDescription, eventsapi.CAPIFEventSERVICEAPIAVAILABLE)
 
        _, ok := ps.publishedServices[apfId]
        if ok {
@@ -220,8 +224,9 @@ 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)
                }
        }
        return ctx.NoContent(http.StatusNoContent)
@@ -230,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 {
@@ -272,7 +277,108 @@ func (ps *PublishService) ModifyIndAPFPubAPI(ctx echo.Context, apfId string, ser
 
 // Update a published service API.
 func (ps *PublishService) PutApfIdServiceApisServiceApiId(ctx echo.Context, apfId string, serviceApiId string) error {
-       return ctx.NoContent(http.StatusNotImplemented)
+       ps.lock.Lock()
+       defer ps.lock.Unlock()
+       errMsg := "Unable to update service due to %s."
+       pos, publishedService, err := ps.checkIfServiceIsPublished(apfId, serviceApiId, ctx)
+       if err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
+       }
+       updatedServiceDescription, err := getServiceFromRequest(ctx)
+       if err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
+       }
+       ps.updateDescription(pos, apfId, &updatedServiceDescription, &publishedService)
+       pos, publishedService.AefProfiles, err = ps.updateProfiles(pos, apfId, updatedServiceDescription.AefProfiles, publishedService.AefProfiles)
+       if err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
+       }
+       ps.publishedServices[apfId][pos] = publishedService
+       err = ctx.JSON(http.StatusOK, publishedService)
+       if err != nil {
+               // Something really bad happened, tell Echo that our handler failed
+               return err
+       }
+       return nil
+}
+func (ps *PublishService) checkIfServiceIsPublished(apfId string, serviceApiId string, ctx echo.Context) (int, publishapi.ServiceAPIDescription, error) {
+       publishedServices, ok := ps.publishedServices[apfId]
+       if !ok {
+               return 0, publishapi.ServiceAPIDescription{}, fmt.Errorf("service must be published before updating it")
+       } else {
+               for pos, description := range publishedServices {
+                       if *description.ApiId == serviceApiId {
+                               return pos, description, nil
+                       }
+               }
+       }
+       return 0, publishapi.ServiceAPIDescription{}, fmt.Errorf("service must be published before updating it")
+}
+func getServiceFromRequest(ctx echo.Context) (publishapi.ServiceAPIDescription, error) {
+       var updatedServiceDescription publishapi.ServiceAPIDescription
+       err := ctx.Bind(&updatedServiceDescription)
+       if err != nil {
+               return publishapi.ServiceAPIDescription{}, fmt.Errorf("invalid format for service")
+       }
+       return updatedServiceDescription, nil
+}
+func (ps *PublishService) updateDescription(pos int, apfId string, updatedServiceDescription, publishedService *publishapi.ServiceAPIDescription) {
+       if updatedServiceDescription.Description != nil {
+               publishedService.Description = updatedServiceDescription.Description
+               go ps.sendEvent(*publishedService, eventsapi.CAPIFEventSERVICEAPIUPDATE)
+       }
+}
+
+func (ps *PublishService) sendEvent(service publishapi.ServiceAPIDescription, eventType eventsapi.CAPIFEvent) {
+       apiIds := []string{*service.ApiId}
+       apis := []publishapi.ServiceAPIDescription{service}
+       event := eventsapi.EventNotification{
+               EventDetail: &eventsapi.CAPIFEventDetail{
+                       ApiIds:                 &apiIds,
+                       ServiceAPIDescriptions: &apis,
+               },
+               Events: eventType,
+       }
+       ps.eventChannel <- event
+}
+
+func (ps *PublishService) updateProfiles(pos int, apfId string, updatedServiceDescription *[]publishapi.AefProfile, publishedService *[]publishapi.AefProfile) (int, *[]publishapi.AefProfile, error) {
+       registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
+       addedProfiles := []publishapi.AefProfile{}
+       changedProfiles := []publishapi.AefProfile{}
+       for _, profile := range *updatedServiceDescription {
+               if !slices.Contains(registeredFuncs, profile.AefId) {
+                       return 0, nil, fmt.Errorf("function %s not registered", profile.AefId)
+               }
+               for _, publishedProfile := range *publishedService {
+                       if publishedProfile.AefId != profile.AefId {
+                               registeredProfiles := *publishedService
+                               newProfiles := append(registeredProfiles, profile)
+                               publishedService = &newProfiles
+                               addedProfiles = append(addedProfiles, profile)
+                       } else {
+                               pos, registeredProfile, err := getProfile(profile.AefId, publishedService)
+                               if err != nil {
+                                       return pos, updatedServiceDescription, fmt.Errorf("unable to update service due to: %s", err.Error())
+                               }
+                               if profile.DomainName != nil {
+                                       registeredProfile.DomainName = profile.DomainName
+                                       (*publishedService)[pos] = registeredProfile
+                               }
+                               changedProfiles = append(changedProfiles, profile)
+                       }
+               }
+       }
+       modifiedProfiles := append(changedProfiles, addedProfiles...)
+       return 0, &modifiedProfiles, nil
+}
+func getProfile(profileId string, apiProfiles *[]publishapi.AefProfile) (int, publishapi.AefProfile, error) {
+       for pos, profile := range *apiProfiles {
+               if profile.AefId == profileId {
+                       return pos, profile, nil
+               }
+       }
+       return 0, publishapi.AefProfile{}, fmt.Errorf("profile with ID %s is not registered for the service", profileId)
 }
 
 // This function wraps sending of an error in the Error format, and