// ========================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.
"path"
"sync"
- "github.com/labstack/echo/v4"
+ echo "github.com/labstack/echo/v4"
+ log "github.com/sirupsen/logrus"
"oransc.org/nonrtric/capifcore/internal/common29122"
provapi "oransc.org/nonrtric/capifcore/internal/providermanagementapi"
-
- log "github.com/sirupsen/logrus"
)
//go:generate mockery --name ServiceRegister
type ServiceRegister interface {
IsFunctionRegistered(functionId string) bool
GetAefsForPublisher(apfId string) []string
+ IsPublishingFunctionRegistered(apiProvFuncId string) bool
}
type ProviderManager struct {
return nil
}
+func (pm *ProviderManager) IsPublishingFunctionRegistered(apiProvFuncId string) bool {
+ for _, provider := range pm.registeredProviders {
+ if provider.IsPublishingFunctionRegistered(apiProvFuncId) {
+ return true
+ }
+ }
+ return false
+}
+
func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error {
var newProvider provapi.APIProviderEnrolmentDetails
+ errMsg := "Unable to register provider due to %s"
if err := ctx.Bind(&newProvider); err != nil {
- return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for provider")
+ return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for provider"))
+ }
+
+ if err := pm.isProviderRegistered(newProvider); err != nil {
+ return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err))
}
if err := newProvider.Validate(); err != nil {
- return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf("Provider not valid due to %s", err))
+ return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
}
pm.prepareNewProvider(&newProvider)
return nil
}
+func (pm *ProviderManager) isProviderRegistered(newProvider provapi.APIProviderEnrolmentDetails) error {
+ for _, prov := range pm.registeredProviders {
+ if err := prov.ValidateAlreadyRegistered(newProvider); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
func (pm *ProviderManager) prepareNewProvider(newProvider *provapi.APIProviderEnrolmentDetails) {
pm.lock.Lock()
defer pm.lock.Unlock()
func (pm *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, registrationId string) error {
errMsg := "Unable to update provider due to %s."
- registeredProvider, err := pm.checkIfProviderIsRegistered(registrationId, ctx)
+ registeredProvider, err := pm.checkIfProviderIsRegistered(registrationId)
if err != nil {
return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
}
return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
}
+ // Additional validation for PUT
+ if (updatedProvider.ApiProvDomId == nil) || (*updatedProvider.ApiProvDomId != registrationId) {
+ errDetail := "APIProviderEnrolmentDetails ApiProvDomId doesn't match path parameter"
+ return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, errDetail))
+ }
+
if err = pm.updateProvider(updatedProvider, registeredProvider); err != nil {
return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
}
return ctx.NoContent(http.StatusNotImplemented)
}
-func (pm *ProviderManager) checkIfProviderIsRegistered(registrationId string, ctx echo.Context) (*provapi.APIProviderEnrolmentDetails, error) {
+func (pm *ProviderManager) checkIfProviderIsRegistered(registrationId string) (*provapi.APIProviderEnrolmentDetails, error) {
registeredProvider, ok := pm.registeredProviders[registrationId]
if !ok {
return nil, fmt.Errorf("provider not onboarded")