diff options
Diffstat (limited to 'catalog-be')
4 files changed, 73 insertions, 29 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java index 41dc3a6db3..d64349b0fa 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java @@ -183,25 +183,32 @@ public class YamlTemplateParsingHandler { Map<String, Object> foundPolicies = findFirstToscaMapElement(toscaJson, POLICIES) .left() .on(err -> logPoliciesNotFound(fileName)); + Map<String, Object> foundNodeTemplates = findFirstToscaMapElement(toscaJson, NODE_TEMPLATES) + .left() + .on(err -> failIfNoNodeTemplates(fileName)); + Map<String, Object> foundGroups = findFirstToscaMapElement(toscaJson, GROUPS) + .left() + .on(err -> logGroupsNotFound(fileName)); if (MapUtils.isNotEmpty(foundPolicies)) { return foundPolicies .entrySet() .stream() - .map(this::createPolicy) + .map(policyNameValue -> createPolicy(policyNameValue, foundNodeTemplates, foundGroups)) .collect(Collectors.toMap(PolicyDefinition::getName, p -> p)); } return Collections.emptyMap(); } - private PolicyDefinition createPolicy(Map.Entry<String, Object> policyNameValue) { + private PolicyDefinition createPolicy(Map.Entry<String, Object> policyNameValue, + Map<String, Object> nodeTemplatesJson, Map<String, Object> groupsTemplateJson) { PolicyDefinition emptyPolicyDef = new PolicyDefinition(); String policyName = policyNameValue.getKey(); emptyPolicyDef.setName(policyName); try { if (policyNameValue.getValue() != null && policyNameValue.getValue() instanceof Map) { Map<String, Object> policyTemplateJsonMap = (Map<String, Object>) policyNameValue.getValue(); - validateAndFillPolicy(emptyPolicyDef, policyTemplateJsonMap); + validateAndFillPolicy(emptyPolicyDef, policyTemplateJsonMap, nodeTemplatesJson, groupsTemplateJson); } else { rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE); } @@ -217,7 +224,9 @@ public class YamlTemplateParsingHandler { return Collections.emptyMap(); } - private void validateAndFillPolicy(PolicyDefinition emptyPolicyDefinition, Map<String, Object> policyTemplateJsonMap) { + private void validateAndFillPolicy(PolicyDefinition emptyPolicyDefinition, + Map<String, Object> policyTemplateJsonMap, Map<String, Object> nodeTemplatesJson, + Map<String, Object> groupsTemplateJson) { String policyTypeName = (String) policyTemplateJsonMap.get(TYPE.getElementName()); if(StringUtils.isEmpty(policyTypeName)){ log.debug("#validateAndFillPolicy - The 'type' member is not found under policy {}", emptyPolicyDefinition.getName()); @@ -225,7 +234,7 @@ public class YamlTemplateParsingHandler { } emptyPolicyDefinition.setType(policyTypeName); // set policy targets - emptyPolicyDefinition.setTargets(validateFillPolicyTargets(policyTemplateJsonMap)); + emptyPolicyDefinition.setTargets(validateFillPolicyTargets(policyTemplateJsonMap, nodeTemplatesJson, groupsTemplateJson)); PolicyTypeDefinition policyTypeDefinition = validateGetPolicyTypeDefinition(policyTypeName); // set policy properties emptyPolicyDefinition.setProperties(validateFillPolicyProperties(policyTypeDefinition, policyTemplateJsonMap)); @@ -272,12 +281,14 @@ public class YamlTemplateParsingHandler { return newPropertyDef; } - private Map<PolicyTargetType, List<String>> validateFillPolicyTargets(Map<String, Object> policyTemplateJson) { + private Map<PolicyTargetType, List<String>> validateFillPolicyTargets(Map<String, Object> policyTemplateJson, + Map<String, Object> nodeTemplatesJson, Map<String, Object> groupTemplateJson) { Map<PolicyTargetType, List<String>> targets = new EnumMap<>(PolicyTargetType.class); if (policyTemplateJson.containsKey(TARGETS.getElementName()) && policyTemplateJson.get(TARGETS.getElementName()) instanceof List ) { List<String> targetsElement = (List<String>) policyTemplateJson.get(TARGETS.getElementName()); - targets.put(PolicyTargetType.COMPONENT_INSTANCES, targetsElement); + targets.put(PolicyTargetType.COMPONENT_INSTANCES, targetsElement.stream().filter(nodeTemplatesJson::containsKey).collect(toList())); + targets.put(PolicyTargetType.GROUPS, targetsElement.stream().filter(groupTemplateJson::containsKey).collect(toList())); } return targets; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyBusinessLogic.java index f872074f26..1e516301e7 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyBusinessLogic.java @@ -38,11 +38,13 @@ import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstInputsMap; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.GroupDefinition; import org.openecomp.sdc.be.model.PolicyDefinition; import org.openecomp.sdc.be.model.PolicyTypeDefinition; import org.openecomp.sdc.be.model.Resource; @@ -187,10 +189,41 @@ public class PolicyBusinessLogic extends BaseBusinessLogic { if (MapUtils.isEmpty(targets)) { return policyDefinition; } - List<String> targetsToUpdate = targets.get(PolicyTargetType.COMPONENT_INSTANCES); - if (CollectionUtils.isEmpty(targetsToUpdate)) { - return policyDefinition; + policyDefinition.setTargets(getPolicyTargets(component, targets)); + policyDefinition = validateAndUpdatePolicyTargets(component, policyDefinition.getUniqueId(), + policyDefinition.getTargets()); + return policyDefinition; + } + + private EnumMap<PolicyTargetType, List<String>> getPolicyTargets(Component component, + Map<PolicyTargetType, List<String>> targets) { + EnumMap<PolicyTargetType, List<String>> updatedTargets = new EnumMap<>(PolicyTargetType.class); + handlePolicyTargetsFromComponentInstances(component, targets, updatedTargets); + handlePolicyTargetsFromGroups(component, targets, updatedTargets); + return updatedTargets; + } + + private void handlePolicyTargetsFromGroups(Component component, Map<PolicyTargetType, List<String>> targets, + EnumMap<PolicyTargetType, List<String>> updatedTargets) { + List<String> targetsToUpdate = targets.getOrDefault(PolicyTargetType.GROUPS, Collections.EMPTY_LIST); + + List<String> targetUniqueIds = new ArrayList<>(); + + for (String targetName : targetsToUpdate) { + Optional<GroupDefinition> group = component.getGroupByInvariantName(targetName); + String groupUniqueId = group + .orElseThrow(() -> new ByActionStatusComponentException( + ActionStatus.GROUP_INSTANCE_NOT_FOUND_ON_COMPONENT_INSTANCE)).getUniqueId(); + targetUniqueIds.add(groupUniqueId); } + updatedTargets.put(PolicyTargetType.GROUPS, targetUniqueIds); + } + + private void handlePolicyTargetsFromComponentInstances(Component component, + Map<PolicyTargetType, List<String>> targets, EnumMap<PolicyTargetType, List<String>> updatedTargets) { + + List<String> targetsToUpdate = targets.getOrDefault(PolicyTargetType.COMPONENT_INSTANCES, Collections.EMPTY_LIST); + // update targets to uniqueIds of respective component instance List<String> targetsUniqueIds = new ArrayList<>(); for (String targetName : targetsToUpdate) { @@ -204,12 +237,7 @@ public class PolicyBusinessLogic extends BaseBusinessLogic { .getUniqueId(); targetsUniqueIds.add(componentUniqueId); } - EnumMap<PolicyTargetType, List<String>> updatedTargets = new EnumMap<>(PolicyTargetType.class); updatedTargets.put(PolicyTargetType.COMPONENT_INSTANCES, targetsUniqueIds); - policyDefinition.setTargets(updatedTargets); - policyDefinition = validateAndUpdatePolicyTargets(component, policyDefinition.getUniqueId(), - policyDefinition.getTargets()); - return policyDefinition; } /*public Either<PolicyDefinition, ResponseFormat> createPolicy(ComponentTypeEnum componentType, String componentId, String policyTypeName, String userId, boolean shouldLock) { diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/PolicyBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/PolicyBusinessLogicTest.java index b8bb4a775b..7a2df78754 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/PolicyBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/PolicyBusinessLogicTest.java @@ -122,8 +122,8 @@ public class PolicyBusinessLogicTest { private final static String POLICY_NAME = "policyName"; private final static String OTHER_POLICY_NAME = "otherPolicyName"; private final static String USER_ID = "jh0003"; - private final static String UNIQUE_ID_EXSISTS = "uniqueIdExists"; - private final static String UNIQUE_ID_DOESNT_EXSISTS = "uniqueIdDoesntExists"; + private final static String UNIQUE_ID_EXISTS = "uniqueIdExists"; + private final static String UNIQUE_ID_DOESNT_EXISTS = "uniqueIdDoesntExists"; private final static String CREATE_POLICY = "create Policy"; private final static String PROPERTY_NAME = "propDefinition"; private final static User user = buildUser(); @@ -197,14 +197,19 @@ public class PolicyBusinessLogicTest { List<ComponentInstance> instanceList = new ArrayList<>(); ComponentInstance componentInstance = new ComponentInstance(); - componentInstance.setUniqueId(UNIQUE_ID_EXSISTS); - componentInstance.setName(UNIQUE_ID_EXSISTS); + componentInstance.setUniqueId(UNIQUE_ID_EXISTS); + componentInstance.setName(UNIQUE_ID_EXISTS); instanceList.add(componentInstance); + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setUniqueId(UNIQUE_ID_EXISTS); + groupDefinition.setName(UNIQUE_ID_EXISTS); + Resource newResource = buildResource(); newResource.setPolicies(policies); newResource.setComponentInstances(instanceList); - + newResource.addGroups(Collections.singletonList(groupDefinition)); + when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeSuccessEither); when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(Either.left(policy)); when(toscaOperationFacade.getToscaFullElement(eq(COMPONENT_ID))).thenReturn(Either.left(newResource)); @@ -221,7 +226,7 @@ public class PolicyBusinessLogicTest { assertNotNull(newPolicy.getTargets()); assertNotNull(newPolicy.getProperties()); assertEquals(2, newPolicy.getProperties().size()); - assertEquals(1, newPolicy.getTargets().size()); + assertEquals(2, newPolicy.getTargets().size()); } @Test @@ -497,16 +502,16 @@ public class PolicyBusinessLogicTest { private Either<Component, StorageOperationStatus> buildElementEither() { ResourceBuilder builder = new ResourceBuilder(); - GroupDefinition groupDefinition = GroupDefinitionBuilder.create().setUniqueId(UNIQUE_ID_EXSISTS).build(); + GroupDefinition groupDefinition = GroupDefinitionBuilder.create().setUniqueId(UNIQUE_ID_EXISTS).build(); ComponentInstanceBuilder componentInstanceBuilder = new ComponentInstanceBuilder(); - ComponentInstance componentInstance = componentInstanceBuilder.setUniqueId(UNIQUE_ID_EXSISTS).build(); + ComponentInstance componentInstance = componentInstanceBuilder.setUniqueId(UNIQUE_ID_EXISTS).build(); return Either.left(builder.addGroup(groupDefinition).addComponentInstance(componentInstance).build()); } private Map<PolicyTargetType, List<String>> getTargets() { Map<PolicyTargetType, List<String>> targets = new HashMap<>(); - targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_EXSISTS)); - targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_EXSISTS)); + targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_EXISTS)); + targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_EXISTS)); return targets; } @@ -552,14 +557,14 @@ public class PolicyBusinessLogicTest { private Map<PolicyTargetType, List<String>> getTargetListFakeType() { Map<PolicyTargetType, List<String>> targets = new HashMap<>(); - targets.put(PolicyTargetType.TYPE_DOES_NOT_EXIST, Collections.singletonList(UNIQUE_ID_EXSISTS)); + targets.put(PolicyTargetType.TYPE_DOES_NOT_EXIST, Collections.singletonList(UNIQUE_ID_EXISTS)); return targets; } private Map<PolicyTargetType, List<String>> getTargetListFakeId() { Map<PolicyTargetType, List<String>> targets = new HashMap<>(); - targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_DOESNT_EXSISTS)); - targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_DOESNT_EXSISTS)); + targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_DOESNT_EXISTS)); + targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_DOESNT_EXISTS)); return targets; } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java index b499830138..e620d87e20 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java @@ -56,7 +56,7 @@ public class PolicyExportParserImplTest { private static final String[] POLICY_TYPE_NAMES = {"policyTypeName_1","policyTypeName_2"}; private static final String[] POLICY_COMPONENT_INSTANCES = {"policyComponentInstanceId"}; private static final String POLICY_COMPONENT_INSTANCES_NAME = "policyComponentInstanceName"; - private static final String[] POLICY_GROUPS = {"policyGroupId"}; + private static final String[] POLICY_GROUPS = {"PolicyGroupName"}; private static final String POLICY_GROUP_NAME = "PolicyGroupName"; private PolicyExportParser policiyExportParser; |