NONRTRIC-946: Servicemanager - mock kong and capif as library
[nonrtric/plt/sme.git] / capifcore / internal / publishservice / publishservice.go
index 17f4b28..54a7f67 100644 (file)
@@ -2,7 +2,8 @@
 //   ========================LICENSE_START=================================
 //   O-RAN-SC
 //   %%
-//   Copyright (C) 2022: Nordix Foundation
+//   Copyright (C) 2022-2023: Nordix Foundation
+//   Copyright (C) 2024: OpenInfra Foundation Europe
 //   %%
 //   Licensed under the Apache License, Version 2.0 (the "License");
 //   you may not use this file except in compliance with the License.
@@ -27,7 +28,7 @@ import (
        "strings"
        "sync"
 
-       "github.com/labstack/echo/v4"
+       echo "github.com/labstack/echo/v4"
        "k8s.io/utils/strings/slices"
 
        "oransc.org/nonrtric/capifcore/internal/common29122"
@@ -42,15 +43,13 @@ import (
 
 //go:generate mockery --name PublishRegister
 type PublishRegister interface {
-       // Checks if the provided APIs are published.
-       // Returns true if all provided APIs have been published, false otherwise.
-       AreAPIsPublished(serviceDescriptions *[]publishapi.ServiceAPIDescription) bool
        // Checks if the provided API is published.
        // Returns true if the provided API has been published, false otherwise.
        IsAPIPublished(aefId, path string) bool
        // Gets all published APIs.
        // Returns a list of all APIs that has been published.
        GetAllPublishedServices() []publishapi.ServiceAPIDescription
+       GetAllowedPublishedServices(invokerApiList []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription
 }
 
 type PublishService struct {
@@ -71,15 +70,6 @@ func NewPublishService(serviceRegister providermanagement.ServiceRegister, hm he
        }
 }
 
-func (ps *PublishService) AreAPIsPublished(serviceDescriptions *[]publishapi.ServiceAPIDescription) bool {
-
-       if serviceDescriptions != nil {
-               registeredApis := ps.getAllAefIds()
-               return checkNewDescriptions(*serviceDescriptions, registeredApis)
-       }
-       return true
-}
-
 func (ps *PublishService) getAllAefIds() []string {
        ps.lock.Lock()
        defer ps.lock.Unlock()
@@ -87,46 +77,12 @@ func (ps *PublishService) getAllAefIds() []string {
        allIds := []string{}
        for _, descriptions := range ps.publishedServices {
                for _, description := range descriptions {
-                       allIds = append(allIds, getIdsFromDescription(description)...)
+                       allIds = append(allIds, description.GetAefIds()...)
                }
        }
        return allIds
 }
 
-func getIdsFromDescription(description publishapi.ServiceAPIDescription) []string {
-       allIds := []string{}
-       if description.AefProfiles != nil {
-               for _, aefProfile := range *description.AefProfiles {
-                       allIds = append(allIds, aefProfile.AefId)
-               }
-       }
-       return allIds
-}
-
-func checkNewDescriptions(newDescriptions []publishapi.ServiceAPIDescription, registeredAefIds []string) bool {
-       registered := true
-       for _, newApi := range newDescriptions {
-               if !checkProfiles(newApi.AefProfiles, registeredAefIds) {
-                       registered = false
-                       break
-               }
-       }
-       return registered
-}
-
-func checkProfiles(newProfiles *[]publishapi.AefProfile, registeredAefIds []string) bool {
-       allRegistered := true
-       if newProfiles != nil {
-               for _, profile := range *newProfiles {
-                       if !slices.Contains(registeredAefIds, profile.AefId) {
-                               allRegistered = false
-                               break
-                       }
-               }
-       }
-       return allRegistered
-}
-
 func (ps *PublishService) IsAPIPublished(aefId, path string) bool {
        return slices.Contains(ps.getAllAefIds(), aefId)
 }
@@ -139,42 +95,77 @@ func (ps *PublishService) GetAllPublishedServices() []publishapi.ServiceAPIDescr
        return publishedDescriptions
 }
 
+func (ps *PublishService) GetAllowedPublishedServices(apiListRequestedServices []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription {
+       apiListAllPublished := ps.GetAllPublishedServices()
+       allowedPublishedServices := join(apiListAllPublished, apiListRequestedServices)
+       return allowedPublishedServices
+}
+
+func join(a, b []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription {
+       var result []publishapi.ServiceAPIDescription
+
+       if (a == nil) || (b == nil) || (len(a) == 0) || (len(b) == 0) {
+               return result
+       }
+
+       for _, itemA := range a {
+               for _, itemB := range b {
+                       if itemA.ApiName == itemB.ApiName {
+                               result = append(result, itemA)
+                               break
+                       }
+               }
+       }
+       return result
+}
+
 // Retrieve all published APIs.
 func (ps *PublishService) GetApfIdServiceApis(ctx echo.Context, apfId string) error {
-       serviceDescriptions, ok := ps.publishedServices[apfId]
-       if ok {
-               err := ctx.JSON(http.StatusOK, serviceDescriptions)
-               if err != nil {
-                       // Something really bad happened, tell Echo that our handler failed
-                       return err
-               }
-       } else {
-               return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf("Provider %s not registered", apfId))
+       if !ps.serviceRegister.IsPublishingFunctionRegistered(apfId) {
+               errorMsg := fmt.Sprintf("Unable to get the service due to %s api is only available for publishers", apfId)
+               return sendCoreError(ctx, http.StatusNotFound, errorMsg)
        }
 
+       serviceDescriptions := ps.publishedServices[apfId]
+       err := ctx.JSON(http.StatusOK, serviceDescriptions)
+       if err != nil {
+               // Something really bad happened, tell Echo that our handler failed
+               return err
+       }
        return nil
 }
 
 // Publish a new API.
 func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) error {
        var newServiceAPIDescription publishapi.ServiceAPIDescription
+       errorMsg := "Unable to publish the service due to %s "
        err := ctx.Bind(&newServiceAPIDescription)
        if err != nil {
-               return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for service "+apfId)
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, "invalid format for service "+apfId))
+       }
+
+       if !ps.serviceRegister.IsPublishingFunctionRegistered(apfId) {
+               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, "api is only available for publishers "+apfId))
+       }
+
+       if err := ps.isServicePublished(newServiceAPIDescription); err != nil {
+               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, err))
        }
 
+       if err := newServiceAPIDescription.Validate(); err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, err))
+       }
        ps.lock.Lock()
        defer ps.lock.Unlock()
 
        registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
        for _, profile := range *newServiceAPIDescription.AefProfiles {
                if !slices.Contains(registeredFuncs, profile.AefId) {
-                       return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf("Function %s not registered", profile.AefId))
+                       return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf(errorMsg, fmt.Sprintf("function %s not registered", profile.AefId)))
                }
        }
 
-       newId := "api_id_" + newServiceAPIDescription.ApiName
-       newServiceAPIDescription.ApiId = &newId
+       newServiceAPIDescription.PrepareNewService()
 
        shouldReturn, returnValue := ps.installHelmChart(newServiceAPIDescription, ctx)
        if shouldReturn {
@@ -200,9 +191,20 @@ func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) e
        return nil
 }
 
+func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) error {
+       for _, services := range ps.publishedServices {
+               for _, service := range services {
+                       if err := service.ValidateAlreadyPublished(newService); err != nil {
+                               return err
+                       }
+               }
+       }
+       return nil
+}
+
 func (ps *PublishService) installHelmChart(newServiceAPIDescription publishapi.ServiceAPIDescription, ctx echo.Context) (bool, error) {
        info := strings.Split(*newServiceAPIDescription.Description, ",")
-       if len(info) == 5 {
+       if (len(info) == 5) && (ps.helmManager != nil) {
                err := ps.helmManager.InstallHelmChart(info[1], info[2], info[3], info[4])
                if err != nil {
                        return true, sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf("Unable to install Helm chart %s due to: %s", info[3], err.Error()))
@@ -219,13 +221,13 @@ func (ps *PublishService) DeleteApfIdServiceApisServiceApiId(ctx echo.Context, a
                pos, description := getServiceDescription(serviceApiId, serviceDescriptions)
                if description != nil {
                        info := strings.Split(*description.Description, ",")
-                       if len(info) == 5 {
+                       if (len(info) == 5) && (ps.helmManager != nil) {
                                ps.helmManager.UninstallHelmChart(info[1], info[3])
                                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 +237,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 {
@@ -256,7 +258,8 @@ func (ps *PublishService) GetApfIdServiceApisServiceApiId(ctx echo.Context, apfI
 
 func getServiceDescription(serviceApiId string, descriptions []publishapi.ServiceAPIDescription) (int, *publishapi.ServiceAPIDescription) {
        for pos, description := range descriptions {
-               if serviceApiId == *description.ApiId {
+               // Check for nil as we had a failure here when running unit tests in parallel against a single Capifcore instance
+               if (description.ApiId != nil) && (serviceApiId == *description.ApiId) {
                        return pos, &description
                }
        }
@@ -277,26 +280,37 @@ 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 {
-       pos, publishedService, shouldReturn, returnValue := ps.checkIfServiceIsPublished(apfId, serviceApiId, ctx)
-       if shouldReturn {
-               return returnValue
+       ps.lock.Lock()
+       defer ps.lock.Unlock()
+       errMsg := "Unable to update service due to %s."
+
+       pos, publishedService, err := ps.checkIfServiceIsPublished(apfId, serviceApiId)
+       if err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
        }
 
-       updatedServiceDescription, shouldReturn, returnValue := getServiceFromRequest(ctx)
-       if shouldReturn {
-               return returnValue
+       updatedServiceDescription, err := getServiceFromRequest(ctx)
+       if err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
        }
 
-       if updatedServiceDescription.Description != nil {
-               ps.lock.Lock()
-               defer ps.lock.Unlock()
+       // Additional validation for PUT
+       if (updatedServiceDescription.ApiId == nil) || (*updatedServiceDescription.ApiId != serviceApiId) {
+               errDetail := "ServiceAPIDescription ApiId doesn't match path parameter"
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, errDetail))
+       }
 
-               publishedService.Description = updatedServiceDescription.Description
-               ps.publishedServices[apfId][pos] = publishedService
-               go ps.sendEvent(publishedService, eventsapi.CAPIFEventSERVICEAPIUPDATE)
+       err = ps.checkProfilesRegistered(apfId, *updatedServiceDescription.AefProfiles)
+       if err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
        }
 
-       err := ctx.JSON(http.StatusOK, ps.publishedServices[apfId][pos])
+       ps.updateDescription(&updatedServiceDescription, &publishedService)
+
+       publishedService.AefProfiles = updatedServiceDescription.AefProfiles
+       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
@@ -304,30 +318,34 @@ func (ps *PublishService) PutApfIdServiceApisServiceApiId(ctx echo.Context, apfI
        return nil
 }
 
-func (ps *PublishService) checkIfServiceIsPublished(apfId string, serviceApiId string, ctx echo.Context) (int, publishapi.ServiceAPIDescription, bool, error) {
+func (ps *PublishService) checkIfServiceIsPublished(apfId string, serviceApiId string) (int, publishapi.ServiceAPIDescription, error) {
        publishedServices, ok := ps.publishedServices[apfId]
        if !ok {
-               return 0, publishapi.ServiceAPIDescription{}, true, sendCoreError(ctx, http.StatusBadRequest, "Service must be published before updating it")
+               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, false, nil
-
+                               return pos, description, nil
                        }
-
                }
-
        }
-       return 0, publishapi.ServiceAPIDescription{}, true, sendCoreError(ctx, http.StatusBadRequest, "Service must be published before updating it")
+       return 0, publishapi.ServiceAPIDescription{}, fmt.Errorf("service must be published before updating it")
 }
 
-func getServiceFromRequest(ctx echo.Context) (publishapi.ServiceAPIDescription, bool, error) {
+func getServiceFromRequest(ctx echo.Context) (publishapi.ServiceAPIDescription, error) {
        var updatedServiceDescription publishapi.ServiceAPIDescription
        err := ctx.Bind(&updatedServiceDescription)
        if err != nil {
-               return publishapi.ServiceAPIDescription{}, true, sendCoreError(ctx, http.StatusBadRequest, "Invalid format for service")
+               return publishapi.ServiceAPIDescription{}, fmt.Errorf("invalid format for service")
+       }
+       return updatedServiceDescription, nil
+}
+
+func (ps *PublishService) updateDescription(updatedServiceDescription, publishedService *publishapi.ServiceAPIDescription) {
+       if updatedServiceDescription.Description != nil {
+               publishedService.Description = updatedServiceDescription.Description
+               go ps.sendEvent(*publishedService, eventsapi.CAPIFEventSERVICEAPIUPDATE)
        }
-       return updatedServiceDescription, false, nil
 }
 
 func (ps *PublishService) sendEvent(service publishapi.ServiceAPIDescription, eventType eventsapi.CAPIFEvent) {
@@ -343,6 +361,16 @@ func (ps *PublishService) sendEvent(service publishapi.ServiceAPIDescription, ev
        ps.eventChannel <- event
 }
 
+func (ps *PublishService) checkProfilesRegistered(apfId string, updatedProfiles []publishapi.AefProfile) error {
+       registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
+       for _, profile := range updatedProfiles {
+               if !slices.Contains(registeredFuncs, profile.AefId) {
+                       return fmt.Errorf("function %s not registered", profile.AefId)
+               }
+       }
+       return nil
+}
+
 // 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 {