From 8220d912803cecb22af967173e228867569450c4 Mon Sep 17 00:00:00 2001 From: MichaelMorris Date: Wed, 19 Oct 2022 18:37:01 +0100 Subject: Fix import policy issues The following issues are addressed: When a service is imported containing polices, a new name for the policy is generated instead of using the name from the imported service When a service is imported containing policies with a string value set for a property of a policy, then the value is created in the new service surrounded with quotes NPE when service imported with no instances NPE when service imported with instances with no requirements Signed-off-by: MichaelMorris Issue-ID: SDC-4224 Change-Id: I8403c0564796a045816ae28e52961688ca7804db --- .../csar/YamlTemplateParsingHandler.java | 7 ++- .../be/components/impl/PolicyBusinessLogic.java | 25 ++++++-- .../impl/ServiceImportBusinessLogic.java | 66 +++++++++++----------- 3 files changed, 61 insertions(+), 37 deletions(-) (limited to 'catalog-be/src/main') 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 3171277172..9734a0acdb 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 @@ -91,6 +91,7 @@ import org.openecomp.sdc.be.components.impl.ImportUtils; import org.openecomp.sdc.be.components.impl.NodeFilterUploadCreator; import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; +import org.openecomp.sdc.be.components.utils.PropertiesUtils; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; @@ -367,11 +368,15 @@ public class YamlTemplateParsingHandler { } final UploadPropInfo uploadPropInfo = buildProperty(propertyJson.getKey(), propertyJson.getValue()); final PropertyDefinition propertyDefinition = new PropertyDefinition(originalProperty); - propertyDefinition.setValue(gson.toJson(uploadPropInfo.getValue())); propertyDefinition.setToscaFunction(uploadPropInfo.getToscaFunction()); propertyDefinition.setSubPropertyToscaFunctions(uploadPropInfo.getSubPropertyToscaFunctions()); propertyDefinition.setGetInputValues(uploadPropInfo.getGet_input()); propertyDefinition.setDescription(uploadPropInfo.getDescription()); + String propertyValue = gson.toJson(uploadPropInfo.getValue()); + if (!propertyDefinition.isToscaFunction()) { + propertyValue = PropertiesUtils.trimQuotes(propertyValue); + } + propertyDefinition.setValue(propertyValue); return propertyDefinition; }) .filter(Objects::nonNull) 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 08ac2c4c77..3d971663b1 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 @@ -147,7 +147,7 @@ public class PolicyBusinessLogic extends BaseBusinessLogic { } // create policyDefinition final String policyTypeName = incomingPolicyDefinition.getPolicyTypeName(); - PolicyDefinition createdPolicyDefinition = createPolicy(policyTypeName, component); + PolicyDefinition createdPolicyDefinition = createPolicy(policyName, policyTypeName, component); // set isFromCsar createdPolicyDefinition.setToscaPresentationValue(JsonPresentationFields.IS_FROM_CSAR, true); // link policy to component @@ -560,15 +560,32 @@ public class PolicyBusinessLogic extends BaseBusinessLogic { } return policyById; } - + private PolicyDefinition createPolicy(String policyTypeName, Component component) { PolicyTypeDefinition policyTypeDefinition = validatePolicyTypeOnCreatePolicy(policyTypeName, component); return addPolicyToComponent(policyTypeDefinition, component); } - + + private PolicyDefinition createPolicy(final String policyName, final String policyTypeName, + final Component component) { + PolicyTypeDefinition policyTypeDefinition = validatePolicyTypeOnCreatePolicy(policyTypeName, component); + return addPolicyToComponent(policyName, policyTypeDefinition, component); + } + private PolicyDefinition addPolicyToComponent(PolicyTypeDefinition policyType, Component component) { Either associatePolicyToComponent = toscaOperationFacade - .associatePolicyToComponent(component.getUniqueId(), new PolicyDefinition(policyType), getNextPolicyCounter(component.getPolicies())); + .associatePolicyToComponent(component.getUniqueId(), new PolicyDefinition(policyType), getNextPolicyCounter(component.getPolicies())); + if (associatePolicyToComponent.isRight()) { + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(associatePolicyToComponent.right().value())); + } + return associatePolicyToComponent.left().value(); + } + + private PolicyDefinition addPolicyToComponent(final String policyName, final PolicyTypeDefinition policyType, final Component component) { + PolicyDefinition policyDefinition = new PolicyDefinition(policyType); + policyDefinition.setName(policyName); + Either associatePolicyToComponent = toscaOperationFacade + .associatePolicyToComponent(component.getUniqueId(), policyDefinition, getNextPolicyCounter(component.getPolicies())); if (associatePolicyToComponent.isRight()) { throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(associatePolicyToComponent.right().value())); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java index b1d9d0a972..82add987f2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java @@ -100,6 +100,7 @@ import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; import org.openecomp.sdc.be.model.CapabilityTypeDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceAttribute; import org.openecomp.sdc.be.model.ComponentInstanceInput; import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.ComponentParametersView; @@ -454,7 +455,8 @@ public class ServiceImportBusinessLogic { return interfaceTypesToCreate; } - private boolean hasNewProperties(final Either result, final Map> dataType) { + private boolean hasNewProperties(final Either result, + final Map> dataType) { return result.isLeft() && dataType.containsKey("properties") && result.left().value().getProperties() != null && result.left().value().getProperties().size() != dataType.get("properties").size(); } @@ -509,19 +511,19 @@ public class ServiceImportBusinessLogic { return eitherNodeTypes.left().value().entrySet().iterator().next(); } - private Map getNewChangesToToscaTemplate(Map newMappedToscaTemplate, + private Map getNewChangesToToscaTemplate(Map newMappedToscaTemplate, Map existingMappedToscaTemplate) { Map combinedMappedToscaTemplate = new HashMap<>(existingMappedToscaTemplate); combinePropertiesIntoToscaTemplate((Map) newMappedToscaTemplate.get("properties"), - (Map) existingMappedToscaTemplate.get("properties"), combinedMappedToscaTemplate); + (Map) existingMappedToscaTemplate.get("properties"), combinedMappedToscaTemplate); combineAttributesIntoToscaTemplate((Map) newMappedToscaTemplate.get("attributes"), - (Map) existingMappedToscaTemplate.get("attributes"), combinedMappedToscaTemplate); + (Map) existingMappedToscaTemplate.get("attributes"), combinedMappedToscaTemplate); combineRequirementsIntoToscaTemplate((List>) newMappedToscaTemplate.get("requirements"), - (List>) existingMappedToscaTemplate.get("requirements"), combinedMappedToscaTemplate); + (List>) existingMappedToscaTemplate.get("requirements"), combinedMappedToscaTemplate); combineCapabilitiesIntoToscaTemplate((Map) newMappedToscaTemplate.get("capabilities"), - (Map) existingMappedToscaTemplate.get("capabilities"), combinedMappedToscaTemplate); + (Map) existingMappedToscaTemplate.get("capabilities"), combinedMappedToscaTemplate); combineInterfacesIntoToscaTemplate((Map>) newMappedToscaTemplate.get("interfaces"), - (Map>) existingMappedToscaTemplate.get("interfaces"), combinedMappedToscaTemplate); + (Map>) existingMappedToscaTemplate.get("interfaces"), combinedMappedToscaTemplate); return combinedMappedToscaTemplate; } @@ -530,7 +532,7 @@ public class ServiceImportBusinessLogic { Map combinedMappedToscaTemplate) { Map> combinedInterfaces = combineAdditionalInterfaces(existingInterfaces, newInterfaces); if ((MapUtils.isEmpty(existingInterfaces) && MapUtils.isNotEmpty(combinedInterfaces)) - || (MapUtils.isNotEmpty(existingInterfaces) && !existingInterfaces.equals(combinedInterfaces))) { + || (MapUtils.isNotEmpty(existingInterfaces) && !existingInterfaces.equals(combinedInterfaces))) { combinedMappedToscaTemplate.put("interfaces", combinedInterfaces); } } @@ -539,7 +541,7 @@ public class ServiceImportBusinessLogic { Map combinedMappedToscaTemplate) { Map combinedCapabilities = combineEntries(newCapabilities, existingCapabilities); if ((MapUtils.isEmpty(existingCapabilities) && MapUtils.isNotEmpty(combinedCapabilities)) || - ( MapUtils.isNotEmpty(existingCapabilities) && !combinedCapabilities.equals(existingCapabilities))) { + (MapUtils.isNotEmpty(existingCapabilities) && !combinedCapabilities.equals(existingCapabilities))) { combinedMappedToscaTemplate.put("capabilities", combinedCapabilities); } } @@ -548,7 +550,7 @@ public class ServiceImportBusinessLogic { Map combinedMappedToscaTemplate) { List> combinedRequirements = combineAdditionalRequirements(newRequirements, existingRequirements); if ((CollectionUtils.isEmpty(existingRequirements) && CollectionUtils.isNotEmpty(combinedRequirements)) - || (CollectionUtils.isNotEmpty(existingRequirements) && !combinedRequirements.equals(existingRequirements))) { + || (CollectionUtils.isNotEmpty(existingRequirements) && !combinedRequirements.equals(existingRequirements))) { combinedMappedToscaTemplate.put("requirements", combinedRequirements); } } @@ -557,7 +559,7 @@ public class ServiceImportBusinessLogic { Map combinedMappedToscaTemplate) { Map combinedAttributes = combineEntries(newAttributes, existingAttributes); if ((MapUtils.isEmpty(existingAttributes) && MapUtils.isNotEmpty(combinedAttributes)) || - ( MapUtils.isNotEmpty(existingAttributes) && !combinedAttributes.equals(existingAttributes))) { + (MapUtils.isNotEmpty(existingAttributes) && !combinedAttributes.equals(existingAttributes))) { combinedMappedToscaTemplate.put("attributes", combinedAttributes); } } @@ -566,7 +568,7 @@ public class ServiceImportBusinessLogic { Map combinedMappedToscaTemplate) { Map combinedProperties = combineEntries(newProperties, existingProperties); if ((MapUtils.isEmpty(existingProperties) && MapUtils.isNotEmpty(combinedProperties)) || - (MapUtils.isNotEmpty(existingProperties) && !combinedProperties.equals(existingProperties))) { + (MapUtils.isNotEmpty(existingProperties) && !combinedProperties.equals(existingProperties))) { combinedMappedToscaTemplate.put("properties", combinedProperties); } } @@ -580,20 +582,21 @@ public class ServiceImportBusinessLogic { if (MapUtils.isEmpty(existingInterfaces)) { return combinedEntries; } - existingInterfaces.entrySet().forEach(interfaceDef -> { - combinedEntries.entrySet().stream().filter((interFace) -> interFace.getValue().get("type").equals(( interfaceDef.getValue()).get("type"))) - .findFirst().ifPresentOrElse((interFace) -> { - interFace.getValue().putAll(interfaceDef.getValue()); - }, () -> { - combinedEntries.put(interfaceDef.getKey(), interfaceDef.getValue()); - }); + existingInterfaces.entrySet().forEach(interfaceDef -> { + combinedEntries.entrySet().stream().filter((interFace) -> interFace.getValue().get("type").equals((interfaceDef.getValue()).get("type"))) + .findFirst().ifPresentOrElse((interFace) -> { + interFace.getValue().putAll(interfaceDef.getValue()); + }, () -> { + combinedEntries.put(interfaceDef.getKey(), interfaceDef.getValue()); + }); }); return combinedEntries; } - private List> combineAdditionalRequirements(List> newReqs, List> existingResourceReqs) { - if (CollectionUtils.isEmpty(newReqs)) { - newReqs = new ArrayList<>(); + private List> combineAdditionalRequirements(List> newReqs, + List> existingResourceReqs) { + if (CollectionUtils.isEmpty(existingResourceReqs)) { + existingResourceReqs = new ArrayList<>(); } Set> combinedReqs = new TreeSet<>((map1, map2) -> map1.keySet().equals(map2.keySet()) ? 0 : map1.keySet().iterator().next().compareTo(map2.keySet().iterator().next())); @@ -1419,21 +1422,20 @@ public class ServiceImportBusinessLogic { if (MapUtils.isEmpty(policies)) { return Either.left(service); } - final Map> instanceAttributeMap = - service.getComponentInstancesAttributes() + Map> componentInstancesAttributes = service.getComponentInstancesAttributes(); + final Map> instanceAttributeMap = new HashMap<>(); + if (MapUtils.isNotEmpty(componentInstancesAttributes)) { + instanceAttributeMap.putAll(componentInstancesAttributes .entrySet().stream() - .collect( - toMap(Entry::getKey, - entry -> entry.getValue().stream().map(AttributeDefinition.class::cast).collect(toList())) - ); + .collect(toMap(Entry::getKey, entry -> entry.getValue().stream().map(AttributeDefinition.class::cast).collect(toList())))); + } policies.values().stream() .map(PolicyDataDefinition::getProperties) .flatMap(Collection::stream) .filter(PropertyDataDefinition::isToscaFunction) - .forEach(policyDefinition -> - toscaFunctionService - .updateFunctionWithDataFromSelfComponent(policyDefinition.getToscaFunction(), service, service.getComponentInstancesProperties(), - instanceAttributeMap) + .forEach(policyDefinition -> toscaFunctionService + .updateFunctionWithDataFromSelfComponent(policyDefinition.getToscaFunction(), service, service.getComponentInstancesProperties(), + instanceAttributeMap) ); policyBusinessLogic.createPolicies(service, policies); return getServiceResponseFormatEither(service); -- cgit 1.2.3-korg