X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=capifcore%2Finternal%2Fprovidermanagement%2Fprovidermanagement.go;h=0213e89f83da61572ca63b1ccc4f4e9e05f48804;hb=fe5863c7537e25426382bd1e376882c5c7d81a4a;hp=63ed4d8c35f24b661ae32b5be4f9ee3ff28ba590;hpb=ff18417912d0634ea14fd02197cd285aba403b3c;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/providermanagement/providermanagement.go b/capifcore/internal/providermanagement/providermanagement.go index 63ed4d8..0213e89 100644 --- a/capifcore/internal/providermanagement/providermanagement.go +++ b/capifcore/internal/providermanagement/providermanagement.go @@ -21,6 +21,7 @@ package providermanagement import ( + "fmt" "net/http" "path" "strings" @@ -98,13 +99,7 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error { return sendCoreError(ctx, http.StatusBadRequest, "Provider missing required ApiProvDomInfo") } - pm.lock.Lock() - defer pm.lock.Unlock() - - newProvider.ApiProvDomId = pm.getDomainId(newProvider.ApiProvDomInfo) - - pm.registerFunctions(newProvider.ApiProvFuncs) - pm.onboardedProviders[*newProvider.ApiProvDomId] = newProvider + pm.prepareNewProvider(&newProvider) uri := ctx.Request().Host + ctx.Request().URL.String() ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newProvider.ApiProvDomId)) @@ -117,46 +112,57 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error { return nil } -func (pm *ProviderManager) DeleteRegistrationsRegistrationId(ctx echo.Context, registrationId string) error { +func (pm *ProviderManager) prepareNewProvider(newProvider *provapi.APIProviderEnrolmentDetails) { pm.lock.Lock() defer pm.lock.Unlock() + newProvider.ApiProvDomId = pm.getDomainId(newProvider.ApiProvDomInfo) + + pm.registerFunctions(newProvider.ApiProvFuncs) + pm.onboardedProviders[*newProvider.ApiProvDomId] = *newProvider +} + +func (pm *ProviderManager) DeleteRegistrationsRegistrationId(ctx echo.Context, registrationId string) error { + log.Debug(pm.onboardedProviders) if _, ok := pm.onboardedProviders[registrationId]; ok { - log.Debug("Deleting provider", registrationId) - delete(pm.onboardedProviders, registrationId) + pm.deleteProvider(registrationId) } return ctx.NoContent(http.StatusNoContent) } +func (pm *ProviderManager) deleteProvider(registrationId string) { + log.Debug("Deleting provider", registrationId) + pm.lock.Lock() + defer pm.lock.Unlock() + delete(pm.onboardedProviders, registrationId) +} + func (pm *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, registrationId string) error { - registeredProvider, ok := pm.onboardedProviders[registrationId] - if !ok { - return sendCoreError(ctx, http.StatusBadRequest, "Provider must be onboarded before updating it") + pm.lock.Lock() + defer pm.lock.Unlock() + errMsg := "Unable to update provider due to %s." + registeredProvider, err := pm.checkIfProviderIsRegistered(registrationId, ctx) + if err != nil { + return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err)) } - var updatedProvider provapi.APIProviderEnrolmentDetails - err := ctx.Bind(&updatedProvider) + updatedProvider, err := getProviderFromRequest(ctx) if err != nil { - return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for provider") + return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err)) } - pm.lock.Lock() - defer pm.lock.Unlock() + updateDomainInfo(&updatedProvider, registeredProvider) - for _, function := range *updatedProvider.ApiProvFuncs { - if function.ApiProvFuncId == nil { - function.ApiProvFuncId = pm.getFuncId(function.ApiProvFuncRole, function.ApiProvFuncInfo) - registeredFuncs := *registeredProvider.ApiProvFuncs - newFuncs := append(registeredFuncs, function) - registeredProvider.ApiProvFuncs = &newFuncs - pm.onboardedProviders[*registeredProvider.ApiProvDomId] = registeredProvider - } + registeredProvider.ApiProvFuncs, err = updateFuncs(updatedProvider.ApiProvFuncs, registeredProvider.ApiProvFuncs) + if err != nil { + return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err)) } - err = ctx.JSON(http.StatusOK, registeredProvider) + pm.onboardedProviders[*registeredProvider.ApiProvDomId] = *registeredProvider + err = ctx.JSON(http.StatusOK, *registeredProvider) if err != nil { // Something really bad happened, tell Echo that our handler failed return err @@ -165,6 +171,60 @@ func (pm *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, regi return nil } +func (pm *ProviderManager) checkIfProviderIsRegistered(registrationId string, ctx echo.Context) (*provapi.APIProviderEnrolmentDetails, error) { + registeredProvider, ok := pm.onboardedProviders[registrationId] + if !ok { + return nil, fmt.Errorf("provider not onboarded") + } + return ®isteredProvider, nil +} + +func getProviderFromRequest(ctx echo.Context) (provapi.APIProviderEnrolmentDetails, error) { + var updatedProvider provapi.APIProviderEnrolmentDetails + err := ctx.Bind(&updatedProvider) + if err != nil { + return provapi.APIProviderEnrolmentDetails{}, fmt.Errorf("invalid format for provider") + } + return updatedProvider, nil +} + +func updateDomainInfo(updatedProvider, registeredProvider *provapi.APIProviderEnrolmentDetails) { + if updatedProvider.ApiProvDomInfo != nil { + registeredProvider.ApiProvDomInfo = updatedProvider.ApiProvDomInfo + } +} + +func updateFuncs(updatedFuncs, registeredFuncs *[]provapi.APIProviderFunctionDetails) (*[]provapi.APIProviderFunctionDetails, error) { + addedFuncs := []provapi.APIProviderFunctionDetails{} + changedFuncs := []provapi.APIProviderFunctionDetails{} + for _, function := range *updatedFuncs { + if function.ApiProvFuncId == nil { + function.ApiProvFuncId = getFuncId(function.ApiProvFuncRole, function.ApiProvFuncInfo) + addedFuncs = append(addedFuncs, function) + } else { + registeredFunction, ok := getApiFunc(*function.ApiProvFuncId, registeredFuncs) + if !ok { + return nil, fmt.Errorf("function with ID %s is not registered for the provider", *function.ApiProvFuncId) + } + if function.ApiProvFuncInfo != nil { + registeredFunction.ApiProvFuncInfo = function.ApiProvFuncInfo + } + changedFuncs = append(changedFuncs, function) + } + } + modifiedFuncs := append(changedFuncs, addedFuncs...) + return &modifiedFuncs, nil +} + +func getApiFunc(funcId string, apiFunctions *[]provapi.APIProviderFunctionDetails) (provapi.APIProviderFunctionDetails, bool) { + for _, function := range *apiFunctions { + if *function.ApiProvFuncId == funcId { + return function, true + } + } + return provapi.APIProviderFunctionDetails{}, false +} + func (pm *ProviderManager) ModifyIndApiProviderEnrolment(ctx echo.Context, registrationId string) error { return ctx.NoContent(http.StatusNotImplemented) } @@ -174,7 +234,7 @@ func (pm *ProviderManager) registerFunctions(provFuncs *[]provapi.APIProviderFun return } for i, provFunc := range *provFuncs { - (*provFuncs)[i].ApiProvFuncId = pm.getFuncId(provFunc.ApiProvFuncRole, provFunc.ApiProvFuncInfo) + (*provFuncs)[i].ApiProvFuncId = getFuncId(provFunc.ApiProvFuncRole, provFunc.ApiProvFuncInfo) } } @@ -183,7 +243,7 @@ func (pm *ProviderManager) getDomainId(domainInfo *string) *string { return &idAsString } -func (pm *ProviderManager) getFuncId(role provapi.ApiProviderFuncRole, funcInfo *string) *string { +func getFuncId(role provapi.ApiProviderFuncRole, funcInfo *string) *string { var idPrefix string switch role { case provapi.ApiProviderFuncRoleAPF: