From d59a9fd8aef8442c1c96a524ff06fb0582d0d814 Mon Sep 17 00:00:00 2001 From: Einat Vinouze Date: Wed, 12 Feb 2020 12:03:35 +0200 Subject: translate owning entity name to owning entity ID Issue-ID: VID-758 Change-Id: Ic89829ac4590537f0279052ceb9e8432ea0de878 Signed-off-by: Einat Vinouze --- .../main/java/org/onap/vid/roles/RoleProvider.java | 41 ++++++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'vid-app-common/src/main/java/org') diff --git a/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java b/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java index c35f5f704..84b0fa934 100644 --- a/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java +++ b/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java @@ -21,6 +21,9 @@ package org.onap.vid.roles; +import static java.util.stream.Collectors.toMap; +import static org.apache.commons.collections4.CollectionUtils.emptyIfNull; + import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.ArrayList; @@ -31,14 +34,19 @@ import java.util.Optional; import java.util.function.Function; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; +import org.jetbrains.annotations.NotNull; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.web.support.UserUtils; import org.onap.vid.aai.AaiResponse; import org.onap.vid.aai.exceptions.RoleParsingException; +import org.onap.vid.category.CategoryParameterOptionRep; +import org.onap.vid.category.CategoryParametersResponse; +import org.onap.vid.model.CategoryParameter.Family; import org.onap.vid.model.ModelConstants; import org.onap.vid.model.Subscriber; import org.onap.vid.model.SubscriberList; import org.onap.vid.services.AaiService; +import org.onap.vid.services.CategoryParameterService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -54,21 +62,27 @@ public class RoleProvider { private Function getUserIdFunction; private Function getRolesFunction; private final RoleValidatorFactory roleValidatorFactory; + private final CategoryParameterService categoryParameterService; + @Autowired - public RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory) { + public RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory, + CategoryParameterService categoryParameterService) { this.aaiService=aaiService; this.roleValidatorFactory = roleValidatorFactory; + this.categoryParameterService = categoryParameterService; getUserIdFunction = UserUtils::getUserId; getRolesFunction = UserUtils::getRoles; } RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory, - Function getUserIdFunction, Function getRolesFunction) { + Function getUserIdFunction, Function getRolesFunction, + CategoryParameterService categoryParameterService) { this.aaiService = aaiService; this.roleValidatorFactory = roleValidatorFactory; this.getRolesFunction = getRolesFunction; this.getUserIdFunction = getUserIdFunction; + this.categoryParameterService = categoryParameterService; } public List getUserRoles(HttpServletRequest request) { @@ -79,6 +93,8 @@ public class RoleProvider { List roleList = new ArrayList<>(); Map roles = getRolesFunction.apply(request); + final Map owningEntityMap = owningEntityNameToOwningEntityIdMapper(); + for (Object role : roles.keySet()) { org.onap.portalsdk.core.domain.Role sdkRol = (org.onap.portalsdk.core.domain.Role) roles.get(role); @@ -90,7 +106,7 @@ public class RoleProvider { continue; } String[] roleParts = splitRole((sdkRol.getName()), logPrefix); - roleList.add(createRoleFromStringArr(roleParts, logPrefix)); + roleList.add(createRoleFromStringArr(roleParts, logPrefix, owningEntityMap)); String msg = String.format("%s User %s got permissions %s", logPrefix, userId, Arrays.toString(roleParts)); LOG.debug(EELFLoggerDelegate.debugLogger, msg); } catch (Exception e) { @@ -140,9 +156,9 @@ public class RoleProvider { return replacement; } - public Role createRoleFromStringArr(String[] roleParts, String rolePrefix) throws RoleParsingException { + public Role createRoleFromStringArr(String[] roleParts, String rolePrefix, Map owningEntityMap) throws RoleParsingException { String globalCustomerID = replaceSubscriberNameToGlobalCustomerID(roleParts[0], rolePrefix); - String owningEntityId = translateOwningEntityNameToOwningEntityId(roleParts[0]); + String owningEntityId = translateOwningEntityNameToOwningEntityId(roleParts[0], owningEntityMap); try { if (roleParts.length > 2) { @@ -162,8 +178,19 @@ public class RoleProvider { } - private String translateOwningEntityNameToOwningEntityId(String owningEntityName) { - return owningEntityName; // TODO: translate to id + // in case the owningEntity name is not found in the owningEntityMap - return the name + protected String translateOwningEntityNameToOwningEntityId(String owningEntityName, Map owningEntityMap) { + return owningEntityMap.getOrDefault(owningEntityName, owningEntityName); + } + + @NotNull + protected Map owningEntityNameToOwningEntityIdMapper() { + CategoryParametersResponse categoryParametersResponse = categoryParameterService.getCategoryParameters(Family.PARAMETER_STANDARDIZATION); + Map> categoryMap = categoryParametersResponse.getCategoryParameters(); + List owningEntityList = categoryMap.get("owningEntity"); + + return emptyIfNull(owningEntityList).stream().collect(toMap( + CategoryParameterOptionRep::getName, CategoryParameterOptionRep::getId)); } public RoleValidator getUserRolesValidator(HttpServletRequest request) { -- cgit 1.2.3-korg