NONRTRIC-946: Fix Capifcore intersection panic
[nonrtric/plt/sme.git] / capifcore / internal / providermanagement / providermanagement.go
index 2d6bd61..5fa786c 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.
@@ -26,12 +27,11 @@ import (
        "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
@@ -139,7 +139,7 @@ func (pm *ProviderManager) deleteProvider(registrationId string) {
 
 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))
        }
@@ -153,6 +153,12 @@ func (pm *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, regi
                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))
        }
@@ -168,7 +174,7 @@ func (pm *ProviderManager) ModifyIndApiProviderEnrolment(ctx echo.Context, regis
        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")