X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fcomponents%2Fimpl%2FServiceImportBusinessLogic.java;h=1ba74a1594a295e6110cee84c8de02cbeb87f1b7;hb=7c61c7bb0369bc27d05c8801c8cb0cca41e494bf;hp=04798ec40abae98eb3ecfa1322df14a609c90dac;hpb=6fa01940a1dec7a323f87f7a141dbef04d8af3d3;p=sdc.git 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 04798ec40a..1ba74a1594 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 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.openecomp.sdc.be.components.impl; import static java.util.stream.Collectors.joining; @@ -29,7 +30,9 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import fj.data.Either; +import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.EnumMap; @@ -42,6 +45,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -49,6 +53,7 @@ import lombok.Getter; import lombok.Setter; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.json.simple.JSONObject; @@ -59,6 +64,7 @@ import org.openecomp.sdc.be.components.csar.ServiceCsarInfo; import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum; import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo; import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException; +import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.impl.model.ToscaTypeImportData; @@ -73,7 +79,6 @@ import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils; -import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition; import org.openecomp.sdc.be.datatypes.elements.CustomYamlFunction; import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition; @@ -85,6 +90,7 @@ import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction; import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterPropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; @@ -99,6 +105,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; @@ -145,6 +152,7 @@ import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation; import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation; import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; +import org.openecomp.sdc.be.model.operations.impl.ModelOperation; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; @@ -199,16 +207,15 @@ public class ServiceImportBusinessLogic { private final IGraphLockOperation graphLockOperation; private final ToscaFunctionService toscaFunctionService; private final DataTypeBusinessLogic dataTypeBusinessLogic; - private ApplicationDataTypeCache applicationDataTypeCache; private final ArtifactTypeOperation artifactTypeOperation; - private final GroupTypeImportManager groupTypeImportManager; private final GroupTypeOperation groupTypeOperation; - private InterfaceLifecycleOperation interfaceLifecycleTypeOperation; - private InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager; - private final CapabilityTypeImportManager capabilityTypeImportManager; private final CapabilityTypeOperation capabilityTypeOperation; + private ApplicationDataTypeCache applicationDataTypeCache; + private final InterfaceLifecycleOperation interfaceLifecycleTypeOperation; + private final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager; + private final ModelOperation modelOperation; public ServiceImportBusinessLogic(final GroupBusinessLogic groupBusinessLogic, final ArtifactsBusinessLogic artifactsBusinessLogic, final ComponentsUtils componentsUtils, final ToscaOperationFacade toscaOperationFacade, @@ -225,7 +232,8 @@ public class ServiceImportBusinessLogic { final InterfaceLifecycleOperation interfaceLifecycleTypeOperation, final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager, final CapabilityTypeImportManager capabilityTypeImportManager, - final CapabilityTypeOperation capabilityTypeOperation) { + final CapabilityTypeOperation capabilityTypeOperation, + final ModelOperation modelOperation) { this.componentsUtils = componentsUtils; this.toscaOperationFacade = toscaOperationFacade; this.serviceBusinessLogic = serviceBusinessLogic; @@ -251,6 +259,7 @@ public class ServiceImportBusinessLogic { this.interfaceLifecycleTypeImportManager = interfaceLifecycleTypeImportManager; this.capabilityTypeImportManager = capabilityTypeImportManager; this.capabilityTypeOperation = capabilityTypeOperation; + this.modelOperation = modelOperation; } @Autowired @@ -258,14 +267,64 @@ public class ServiceImportBusinessLogic { this.applicationDataTypeCache = applicationDataTypeCache; } + public Service updateServiceFromToscaTemplate(final String serviceId, final User modifier, final String data) { + final Either serviceResponseFormatEither = serviceBusinessLogic.getService(serviceId, modifier); + if (serviceResponseFormatEither.isRight()) { + throw new ByActionStatusComponentException(ActionStatus.SERVICE_NOT_FOUND, serviceId); + } + final Service serviceOriginal = serviceResponseFormatEither.left().value(); + final Map metadata = (Map) new Yaml().loadAs(data, Map.class).get("metadata"); + validateServiceMetadataBeforeCreate(serviceOriginal, metadata); + + final Service newService = cloneServiceIdentifications(serviceOriginal); + final Map payload = new HashMap<>(); + payload.put("Definitions/service-" + metadata.get("name") + "-template.yml", data.getBytes()); + updateServiceMetadata(newService, metadata); + return createService(newService, AuditingActionEnum.UPDATE_SERVICE_TOSCA_TEMPLATE, modifier, payload, null); + } + + private Service cloneServiceIdentifications(final Service serviceOriginal) { + final Service newService = new Service(serviceOriginal.getComponentMetadataDefinition()); + newService.setCategories(serviceOriginal.getCategories()); + newService.setInvariantUUID(serviceOriginal.getInvariantUUID()); + newService.setUniqueId(serviceOriginal.getUniqueId()); + newService.setName(serviceOriginal.getName()); + newService.setUUID(serviceOriginal.getUUID()); + return newService; + } + + private void validateServiceMetadataBeforeCreate(final Service service, final Map metadata) { + if (MapUtils.isEmpty(metadata)) { + throw new ByActionStatusComponentException(ActionStatus.MISSING_SERVICE_METADATA); + } + final String uuid = metadata.get("UUID"); + if (!service.getUUID().equals(uuid)) { + throw new ByActionStatusComponentException(ActionStatus.UNCHANGEABLE_PROPERTY_ERROR, "UUID"); + } + final String invariantUUID = metadata.get("invariantUUID"); + if (!service.getInvariantUUID().equals(invariantUUID)) { + throw new ByActionStatusComponentException(ActionStatus.UNCHANGEABLE_PROPERTY_ERROR, "invariantUUID"); + } + final String name = metadata.get("name"); + if (!service.getName().equals(name)) { + throw new ByActionStatusComponentException(ActionStatus.UNCHANGEABLE_PROPERTY_ERROR, "name"); + } + final String version = metadata.get("template_version"); + if (!service.getVersion().equals(version)) { + throw new ByActionStatusComponentException(ActionStatus.UNCHANGEABLE_PROPERTY_ERROR, "template_version"); + } + } + public Service createService(Service service, AuditingActionEnum auditingAction, User user, Map csarUIPayload, String payloadName) { log.debug("enter createService"); - service.setCreatorUserId(user.getUserId()); - service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); - service.setVersion(INITIAL_VERSION); - service.setConformanceLevel(ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel()); - service.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED); + if (AuditingActionEnum.CREATE_SERVICE.equals(auditingAction)) { + service.setCreatorUserId(user.getUserId()); + service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + service.setVersion(INITIAL_VERSION); + service.setConformanceLevel(ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel()); + service.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED); + } try { final var serviceBeforeCreate = serviceBusinessLogic.validateServiceBeforeCreate(service, user, auditingAction); if (serviceBeforeCreate.isRight()) { @@ -273,10 +332,12 @@ public class ServiceImportBusinessLogic { } log.debug("enter createService,validateServiceBeforeCreate success"); String csarUUID = payloadName == null ? service.getCsarUUID() : payloadName; - log.debug("enter createService,get csarUUID:{}", csarUUID); - csarBusinessLogic.validateCsarBeforeCreate(service, csarUUID); + if (AuditingActionEnum.CREATE_SERVICE.equals(auditingAction)) { + log.debug("enter createService,get csarUUID:{}", csarUUID); + csarBusinessLogic.validateCsarBeforeCreate(service, csarUUID); + } log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID); - return createServiceFromCsar(service, user, csarUIPayload, csarUUID); + return createServiceFromCsar(service, user, csarUIPayload, csarUUID, auditingAction); } catch (final ComponentException e) { log.debug("Exception occurred when createService: {}", e.getMessage(), e); throw e; @@ -286,58 +347,77 @@ public class ServiceImportBusinessLogic { } } - protected Service createServiceFromCsar(Service service, User user, Map csarUIPayload, String csarUUID) { + private void updateServiceMetadata(final Service service, final Map metadata) { + metadata.entrySet().forEach(s -> { + final Optional find = + Arrays.stream(service.getClass().getMethods()).filter(method -> method.getName().equalsIgnoreCase("set" + s.getKey())).findAny(); + if (find.isPresent()) { + try { + find.get().invoke(service, s.getValue()); + } catch (final Exception e) { + log.warn("Unable to set '{}' with value '{}'", s.getKey(), s.getValue()); + } + } + }); + } + + protected Service createServiceFromCsar(Service service, User user, Map csarUIPayload, String csarUUID, + AuditingActionEnum auditingAction) { log.trace("************* created successfully from YAML, resource TOSCA "); try { - ServiceCsarInfo csarInfo = csarBusinessLogic.getCsarInfo(service, null, user, csarUIPayload, csarUUID); - - final Map dataTypesToCreate = getDatatypesToCreate(service.getModel(), csarInfo); + final ServiceCsarInfo csarInfo; + if (AuditingActionEnum.UPDATE_SERVICE_TOSCA_TEMPLATE.equals(auditingAction)) { + csarInfo = new ServiceCsarInfo(user, csarUUID, csarUIPayload, service.getName(), service.getModel(), + csarUIPayload.keySet().iterator().next(), new String(csarUIPayload.values().iterator().next()), true, modelOperation); + } else { + csarInfo = csarBusinessLogic.getCsarInfo(service, null, user, csarUIPayload, csarUUID, auditingAction); + } + final String serviceModel = service.getModel(); + final Map dataTypesToCreate = getDatatypesToCreate(serviceModel, csarInfo); if (MapUtils.isNotEmpty(dataTypesToCreate)) { - dataTypeBusinessLogic.createDataTypeFromYaml(new Yaml().dump(dataTypesToCreate), service.getModel(), true); - dataTypesToCreate.entrySet().stream().forEach(createdOrUpdatedDataType -> { - applicationDataTypeCache.reload(service.getModel(), - UniqueIdBuilder.buildDataTypeUid(service.getModel(), createdOrUpdatedDataType.getKey())); - }); + dataTypeBusinessLogic.createDataTypeFromYaml(new Yaml().dump(dataTypesToCreate), serviceModel, true); + dataTypesToCreate.keySet().forEach(key -> + applicationDataTypeCache.reload(serviceModel, UniqueIdBuilder.buildDataTypeUid(serviceModel, key)) + ); } - final Map artifactTypesToCreate = getArtifactTypesToCreate(service.getModel(), csarInfo); + final Map artifactTypesToCreate = getArtifactTypesToCreate(serviceModel, csarInfo); if (MapUtils.isNotEmpty(artifactTypesToCreate)) { - artifactTypeImportManager.createArtifactTypes(new Yaml().dump(artifactTypesToCreate), service.getModel(), true); + artifactTypeImportManager.createArtifactTypes(new Yaml().dump(artifactTypesToCreate), serviceModel, true); } - final List nodeTypesToCreate = getNodeTypesToCreate(service.getModel(), csarInfo); + final List nodeTypesToCreate = getNodeTypesToCreate(serviceModel, csarInfo); if (CollectionUtils.isNotEmpty(nodeTypesToCreate)) { - createNodeTypes(nodeTypesToCreate, service.getModel(), csarInfo.getModifier()); + ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic.getParsedToscaYamlInfo(csarInfo, service); + createNodeTypes(nodeTypesToCreate, parsedToscaYamlInfo.getInstances(), serviceModel, csarInfo.getModifier()); } - final Map groupTypesToCreate = getGroupTypesToCreate(service.getModel(), csarInfo); + final Map groupTypesToCreate = getGroupTypesToCreate(serviceModel, csarInfo); if (MapUtils.isNotEmpty(groupTypesToCreate)) { final Map toscaTypeMetadata = fillToscaTypeMetadata(groupTypesToCreate); final ToscaTypeImportData toscaTypeImportData = new ToscaTypeImportData(new Yaml().dump(groupTypesToCreate), toscaTypeMetadata); - groupTypeImportManager.createGroupTypes(toscaTypeImportData, service.getModel(), true); + groupTypeImportManager.createGroupTypes(toscaTypeImportData, serviceModel, true); } - final Map interfaceTypesToCreate = getInterfaceTypesToCreate(service.getModel(), csarInfo); + final Map interfaceTypesToCreate = getInterfaceTypesToCreate(serviceModel, csarInfo); if (MapUtils.isNotEmpty(interfaceTypesToCreate)) { - interfaceLifecycleTypeImportManager - .createLifecycleTypes(new Yaml().dump(interfaceTypesToCreate), service.getModel(), true); + interfaceLifecycleTypeImportManager.createLifecycleTypes(new Yaml().dump(interfaceTypesToCreate), serviceModel, true); } - final Map capabilityTypesToCreate = getCapabilityTypesToCreate(service.getModel(), csarInfo); - + final Map capabilityTypesToCreate = getCapabilityTypesToCreate(serviceModel, csarInfo); if (MapUtils.isNotEmpty(capabilityTypesToCreate)) { - capabilityTypeImportManager.createCapabilityTypes(new Yaml().dump(capabilityTypesToCreate), service.getModel(), true); + capabilityTypeImportManager.createCapabilityTypes(new Yaml().dump(capabilityTypesToCreate), serviceModel, true); } Map nodeTypesInfo = csarInfo.extractTypesInfo(); - Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = serviceImportParseLogic - .findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo, service); + Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes + = serviceImportParseLogic.findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo, service); if (findNodeTypesArtifactsToHandleRes.isRight()) { log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID()); throw new ComponentException(findNodeTypesArtifactsToHandleRes.right().value()); } return createServiceFromYaml(service, csarInfo.getMainTemplateContent(), csarInfo.getMainTemplateName(), nodeTypesInfo, csarInfo, - findNodeTypesArtifactsToHandleRes.left().value(), true, false, null, user.getUserId()); + findNodeTypesArtifactsToHandleRes.left().value(), true, false, null, user.getUserId(), auditingAction); } catch (final ComponentException e) { log.debug("Exception occurred when createServiceFromCsar,error is:{}", e.getMessage(), e); throw e; @@ -453,21 +533,22 @@ 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(); } - private void createNodeTypes(List nodeTypesToCreate, String model, User user) { + private void createNodeTypes(List nodeTypesToCreate, Map instancesFromCsar, String model, User user) { NodeTypesMetadataList nodeTypesMetadataList = new NodeTypesMetadataList(); List nodeTypeMetadataList = new ArrayList<>(); final Map allTypesToCreate = new HashMap<>(); - nodeTypesToCreate.stream().forEach(nodeType -> { + nodeTypesToCreate.forEach(nodeType -> { allTypesToCreate.put(nodeType.getMappedNodeType().getKey(), nodeType.getMappedNodeType().getValue()); nodeTypeMetadataList.add(nodeType.getNodeTypeMetadata()); }); nodeTypesMetadataList.setNodeMetadataList(nodeTypeMetadataList); - resourceImportManager.importAllNormativeResource(allTypesToCreate, nodeTypesMetadataList, user, model, true, false); + resourceImportManager.importAllNormativeResource(allTypesToCreate, nodeTypesMetadataList, instancesFromCsar, user, model, true, false); } private List getNodeTypesToCreate(final String model, final ServiceCsarInfo csarInfo) { @@ -480,14 +561,17 @@ public class ServiceImportBusinessLogic { namesOfNodeTypesToCreate.add(nodeTypeDefinition); } else if (result.isLeft()) { Resource latestResource = (Resource) result.left().value(); - Entry latestMappedToscaTemplate = getResourceToscaTemplate(latestResource.getUniqueId(), + Entry existingMappedToscaTemplate = getResourceToscaTemplate(latestResource.getUniqueId(), latestResource.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE), csarInfo.getModifier().getUserId()); - Map mappedToscaTemplate = (Map) nodeTypeDefinition.getMappedNodeType().getValue(); - Map newMappedToscaTemplate = - getNewChangesToToscaTemplate(mappedToscaTemplate, (Map) latestMappedToscaTemplate.getValue()); - if (!newMappedToscaTemplate.equals(latestMappedToscaTemplate.getValue())) { - latestMappedToscaTemplate.setValue(newMappedToscaTemplate); - nodeTypeDefinition.setMappedNodeType(latestMappedToscaTemplate); + Map newMappedToscaTemplate = (Map) nodeTypeDefinition.getMappedNodeType().getValue(); + Map combinedMappedToscaTemplate = + getNewChangesToToscaTemplate(newMappedToscaTemplate, (Map) existingMappedToscaTemplate.getValue()); + if (!combinedMappedToscaTemplate.equals(existingMappedToscaTemplate.getValue())) { + if (latestResource.getComponentMetadataDefinition().getMetadataDataDefinition().isNormative()) { + nodeTypeDefinition.getNodeTypeMetadata().setNormative(true); + } + existingMappedToscaTemplate.setValue(combinedMappedToscaTemplate); + nodeTypeDefinition.setMappedNodeType(existingMappedToscaTemplate); namesOfNodeTypesToCreate.add(nodeTypeDefinition); } } @@ -508,35 +592,120 @@ public class ServiceImportBusinessLogic { return eitherNodeTypes.left().value().entrySet().iterator().next(); } - private Map getNewChangesToToscaTemplate(Map mappedToscaTemplate, Map latestMappedToscaTemplate) { - Map newMappedToscaTemplate = new HashMap<>(latestMappedToscaTemplate); + private Map getNewChangesToToscaTemplate(Map newMappedToscaTemplate, + Map existingMappedToscaTemplate) { + Map combinedMappedToscaTemplate = new HashMap<>(existingMappedToscaTemplate); + combinePropertiesIntoToscaTemplate((Map) newMappedToscaTemplate.get("properties"), + (Map) existingMappedToscaTemplate.get("properties"), combinedMappedToscaTemplate); + combineAttributesIntoToscaTemplate((Map) newMappedToscaTemplate.get("attributes"), + (Map) existingMappedToscaTemplate.get("attributes"), combinedMappedToscaTemplate); + combineRequirementsIntoToscaTemplate((List>) newMappedToscaTemplate.get("requirements"), + (List>) existingMappedToscaTemplate.get("requirements"), combinedMappedToscaTemplate); + combineCapabilitiesIntoToscaTemplate((Map) newMappedToscaTemplate.get("capabilities"), + (Map) existingMappedToscaTemplate.get("capabilities"), combinedMappedToscaTemplate); + combineInterfacesIntoToscaTemplate((Map>) newMappedToscaTemplate.get("interfaces"), + (Map>) existingMappedToscaTemplate.get("interfaces"), combinedMappedToscaTemplate); + return combinedMappedToscaTemplate; + } + + private void combineInterfacesIntoToscaTemplate(Map> newInterfaces, + Map> existingInterfaces, + Map combinedMappedToscaTemplate) { + Map> combinedInterfaces = combineAdditionalInterfaces(existingInterfaces, newInterfaces); + if ((MapUtils.isEmpty(existingInterfaces) && MapUtils.isNotEmpty(combinedInterfaces)) + || (MapUtils.isNotEmpty(existingInterfaces) && !existingInterfaces.equals(combinedInterfaces))) { + combinedMappedToscaTemplate.put("interfaces", combinedInterfaces); + } + } + + private void combineCapabilitiesIntoToscaTemplate(Map newCapabilities, Map existingCapabilities, + Map combinedMappedToscaTemplate) { + Map combinedCapabilities = combineEntries(newCapabilities, existingCapabilities); + if ((MapUtils.isEmpty(existingCapabilities) && MapUtils.isNotEmpty(combinedCapabilities)) || + (MapUtils.isNotEmpty(existingCapabilities) && !combinedCapabilities.equals(existingCapabilities))) { + combinedMappedToscaTemplate.put("capabilities", combinedCapabilities); + } + } + + private void combineRequirementsIntoToscaTemplate(List> newRequirements, List> existingRequirements, + Map combinedMappedToscaTemplate) { + List> combinedRequirements = combineAdditionalRequirements(newRequirements, existingRequirements); + if ((CollectionUtils.isEmpty(existingRequirements) && CollectionUtils.isNotEmpty(combinedRequirements)) + || (CollectionUtils.isNotEmpty(existingRequirements) && !combinedRequirements.equals(existingRequirements))) { + combinedMappedToscaTemplate.put("requirements", combinedRequirements); + } + } + + private void combineAttributesIntoToscaTemplate(Map newAttributes, Map existingAttributes, + Map combinedMappedToscaTemplate) { + Map combinedAttributes = combineEntries(newAttributes, existingAttributes); + if ((MapUtils.isEmpty(existingAttributes) && MapUtils.isNotEmpty(combinedAttributes)) || + (MapUtils.isNotEmpty(existingAttributes) && !combinedAttributes.equals(existingAttributes))) { + combinedMappedToscaTemplate.put("attributes", combinedAttributes); + } + } + + private void combinePropertiesIntoToscaTemplate(Map newProperties, Map existingProperties, + Map combinedMappedToscaTemplate) { + Map combinedProperties = combineEntries(newProperties, existingProperties); + if ((MapUtils.isEmpty(existingProperties) && MapUtils.isNotEmpty(combinedProperties)) || + (MapUtils.isNotEmpty(existingProperties) && !combinedProperties.equals(existingProperties))) { + combinedMappedToscaTemplate.put("properties", combinedProperties); + } + } + + private Map> combineAdditionalInterfaces(Map> existingInterfaces, + Map> newInterfaces) { + if (MapUtils.isEmpty(newInterfaces)) { + newInterfaces = new HashMap<>(); + } + Map> combinedEntries = new HashMap<>(newInterfaces); + 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()); + }); + }); + return combinedEntries; + } - Map properties = (Map) mappedToscaTemplate.get("properties"); - Map latestProperties = (Map) latestMappedToscaTemplate.get("properties"); - Map allProperties = combinedEntries(properties, latestProperties); - if ((MapUtils.isEmpty(latestProperties) && MapUtils.isNotEmpty(allProperties)) || - (MapUtils.isNotEmpty(latestProperties) && !allProperties.equals(latestProperties))) { - newMappedToscaTemplate.put("properties", allProperties); + 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())); + combinedReqs.addAll(existingResourceReqs); + if (CollectionUtils.isEmpty(newReqs)) { + return new ArrayList<>(combinedReqs); } - return newMappedToscaTemplate; + combinedReqs.addAll(newReqs); + return new ArrayList<>(combinedReqs); } - private Map combinedEntries(Map firstMap, Map secondMap) { - if (MapUtils.isEmpty(firstMap)) { - firstMap = new HashMap<>(); + private Map combineEntries(Map newMap, Map existingMap) { + if (MapUtils.isEmpty(newMap)) { + newMap = new HashMap<>(); } - Map combinedEntries = new HashMap<>(firstMap); - if (MapUtils.isEmpty(secondMap)) { + Map combinedEntries = new HashMap<>(newMap); + if (MapUtils.isEmpty(existingMap)) { return combinedEntries; } - combinedEntries.putAll(secondMap); + combinedEntries.putAll(existingMap); return combinedEntries; } protected Service createServiceFromYaml(Service service, String topologyTemplateYaml, String yamlName, Map nodeTypesInfo, CsarInfo csarInfo, - Map>> nodeTypesArtifactsToCreate, - boolean shouldLock, boolean inTransaction, String nodeName, final String userId) + Map>> nodeTypesArtifactsToCreate, + boolean shouldLock, boolean inTransaction, String nodeName, final String userId, + AuditingActionEnum auditingAction) throws BusinessLogicException { List createdArtifacts = new ArrayList<>(); Service createdService; @@ -552,7 +721,8 @@ public class ServiceImportBusinessLogic { csfyp.setNodeTypesInfo(nodeTypesInfo); csfyp.setCsarInfo(csarInfo); csfyp.setNodeName(nodeName); - createdService = createServiceAndRIsFromYaml(service, false, nodeTypesArtifactsToCreate, shouldLock, inTransaction, csfyp, userId); + createdService = createServiceAndRIsFromYaml(service, false, nodeTypesArtifactsToCreate, shouldLock, inTransaction, csfyp, userId, + auditingAction); log.debug("#createResourceFromYaml - The resource {} has been created ", service.getName()); } catch (ComponentException | BusinessLogicException e) { log.debug("Create Service from yaml failed", e); @@ -565,10 +735,11 @@ public class ServiceImportBusinessLogic { } protected Service createServiceAndRIsFromYaml(Service service, boolean isNormative, - Map>> nodeTypesArtifactsToCreate, + Map>> nodeTypesArtifactsToCreate, boolean shouldLock, boolean inTransaction, CreateServiceFromYamlParameter csfyp, - final String userId) + final String userId, AuditingActionEnum auditingAction) throws BusinessLogicException { + List nodeTypesNewCreatedArtifacts = new ArrayList<>(); String yamlName = csfyp.getYamlName(); ParsedToscaYamlInfo parsedToscaYamlInfo = csfyp.getParsedToscaYamlInfo(); @@ -580,7 +751,6 @@ public class ServiceImportBusinessLogic { if (shouldLock) { Either lockResult = serviceBusinessLogic.lockComponentByName(service.getSystemName(), service, CREATE_RESOURCE); if (lockResult.isRight()) { - serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); throw new ComponentException(lockResult.right().value()); } log.debug("name is locked {} status = {}", service.getSystemName(), lockResult); @@ -598,12 +768,13 @@ public class ServiceImportBusinessLogic { service.setProperties(propertiesList); } log.trace("************* createResourceFromYaml before full create resource {}", yamlName); - service = serviceImportParseLogic.createServiceTransaction(service, csarInfo.getModifier(), isNormative); + service = serviceImportParseLogic.createServiceTransaction(service, csarInfo.getModifier(), isNormative, auditingAction); log.trace("************* Going to add inputs from yaml {}", yamlName); Map inputs = parsedToscaYamlInfo.getInputs(); service = serviceImportParseLogic.createInputsOnService(service, inputs); log.trace("************* Finished to add inputs from yaml {}", yamlName); - ListDataDefinition substitutionFilterProperties = parsedToscaYamlInfo.getSubstitutionFilterProperties(); + ListDataDefinition substitutionFilterProperties = + parsedToscaYamlInfo.getSubstitutionFilterProperties(); service = serviceImportParseLogic.createSubstitutionFilterOnService(service, substitutionFilterProperties); log.trace("************* Added Substitution filter from interface yaml {}", yamlName); Map uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances(); @@ -619,7 +790,6 @@ public class ServiceImportBusinessLogic { Either, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), service.getSystemName()); if (validateUpdateVfGroupNamesRes.isRight()) { - serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); throw new ComponentException(validateUpdateVfGroupNamesRes.right().value()); } Map groups; @@ -631,31 +801,27 @@ public class ServiceImportBusinessLogic { } Either createGroupsOnResource = createGroupsOnResource(service, groups); if (createGroupsOnResource.isRight()) { - serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); throw new ComponentException(createGroupsOnResource.right().value()); } service = createGroupsOnResource.left().value(); Either createPoliciesOnResource = createPoliciesOnResource(service, parsedToscaYamlInfo.getPolicies()); if (createPoliciesOnResource.isRight()) { - serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); throw new ComponentException(createPoliciesOnResource.right().value()); } service = createPoliciesOnResource.left().value(); log.trace("************* Going to add artifacts from yaml {}", yamlName); - NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToCreate); Either createArtifactsEither = createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, - createdArtifacts, yamlName, csarInfo, service, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock); + createdArtifacts, yamlName, csarInfo, service, inTransaction, shouldLock); if (createArtifactsEither.isRight()) { - serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); throw new ComponentException(createArtifactsEither.right().value()); } service = serviceImportParseLogic.getServiceWithGroups(createArtifactsEither.left().value().getUniqueId()); - service = updateInputs(service, userId); + service = updateInputs(service, userId, parsedToscaYamlInfo.getSubstitutionMappingProperties()); ASDCKpiApi.countCreatedResourcesKPI(); return service; - } catch (ComponentException | StorageException | BusinessLogicException e) { + } catch (Exception e) { rollback = true; serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); throw e; @@ -668,52 +834,56 @@ public class ServiceImportBusinessLogic { } } if (shouldLock) { - graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(), NodeTypeEnum.Resource); + graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(), NodeTypeEnum.Service); } } } - private Service updateInputs(final Service component, final String userId) { + private Service updateInputs(final Service component, final String userId, final Map> substitutionMappingProperties) { final List inputs = component.getInputs(); - final List componentInstances = component.getComponentInstances(); - final String componentUniqueId = component.getUniqueId(); - final Map> componentInstancesProperties = component.getComponentInstancesProperties(); - for (final InputDefinition input : inputs) { - if (isInputFromComponentInstanceProperty(input.getName(), componentInstances, componentInstancesProperties)) { - associateInputToComponentInstanceProperty(userId, input, componentInstances, componentInstancesProperties, - componentUniqueId); - } else { - associateInputToServiceProperty(userId, input, component); + if (CollectionUtils.isNotEmpty(inputs)) { + final List componentInstances = component.getComponentInstances(); + final String componentUniqueId = component.getUniqueId(); + List propertyMissingNames = new ArrayList<>(); + for (final InputDefinition input : inputs) { + boolean isSubMapProp = false; + if (substitutionMappingProperties != null && !substitutionMappingProperties.isEmpty()) { + isSubMapProp = substitutionMappingProperties.entrySet().stream() + .anyMatch(stringEntry -> stringEntry.getValue().get(0).equals(input.getName())); + } + if (!isSubMapProp && isInputFromComponentInstanceProperty(input.getName(), componentInstances)) { + associateInputToComponentInstanceProperty(userId, input, componentInstances, componentUniqueId); + } else { + String propertyName = associateInputToServiceProperty(userId, input, component, substitutionMappingProperties); + if (StringUtils.isNotBlank(propertyName)) { + propertyMissingNames.add(propertyName); + } + } + } + if (CollectionUtils.isNotEmpty(propertyMissingNames)) { + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.MISSING_PROPERTIES_ERROR, propertyMissingNames.toString())); + } + Either, StorageOperationStatus> either = toscaOperationFacade.updateInputsToComponent(inputs, componentUniqueId); + if (either.isRight()) { + throw new ComponentException(ActionStatus.GENERAL_ERROR); } - - } - - final Either, StorageOperationStatus> either - = toscaOperationFacade.updateInputsToComponent(inputs, componentUniqueId); - if (either.isRight()) { - throw new ComponentException(ActionStatus.GENERAL_ERROR); } return component; } - private boolean isInputFromComponentInstanceProperty(final String inputName, final List componentInstances, - final Map> componentInstancesProperties) { + private boolean isInputFromComponentInstanceProperty(final String inputName, final List componentInstances) { + if (CollectionUtils.isNotEmpty(componentInstances)) { - // get instance's names - final List componentInstancesNames = componentInstances.stream().map(ComponentInstanceDataDefinition::getNormalizedName) - .collect(toList()); - final Optional componentInstancesNameOptional = componentInstancesNames.stream() - .filter(cin -> inputName.startsWith(cin + "_")).findFirst(); - if (componentInstancesNameOptional.isPresent() && MapUtils.isNotEmpty(componentInstancesProperties)) { - final Optional componentInstanceIdOptional = componentInstancesProperties.keySet().stream() - .filter(key -> key.endsWith("." + componentInstancesNameOptional.get())).findFirst(); - if (componentInstanceIdOptional.isPresent()) { - // get property's name - final String propertyNameFromInput = extractPropertyNameFromInputName(inputName, componentInstancesNames); - return componentInstancesProperties.get(componentInstanceIdOptional.get()).stream() - .anyMatch(prop -> prop.getName().equals(propertyNameFromInput) && prop.getValue() != null - && prop.getValue().contains(ToscaGetFunctionType.GET_INPUT.getFunctionName())); + for (ComponentInstance instance : componentInstances) { + for (PropertyDefinition instanceProperty : instance.getProperties()) { + if (CollectionUtils.isNotEmpty(instanceProperty.getGetInputValues())) { + for (GetInputValueDataDefinition getInputValueDataDefinition : instanceProperty.getGetInputValues()) { + if (inputName.equals(getInputValueDataDefinition.getInputName())) { + return true; + } + } + } } } } @@ -722,25 +892,28 @@ public class ServiceImportBusinessLogic { private void associateInputToComponentInstanceProperty(final String userId, final InputDefinition input, final List componentInstances, - final Map> componentInstancesProperties, String componentUniqueId) { - // From Instance - final List componentInstancesNames = componentInstances.stream().map(ComponentInstanceDataDefinition::getNormalizedName) - .collect(toList()); - final String propertyNameFromInput = extractPropertyNameFromInputName(input.getName(), componentInstancesNames); - - final Optional componentInstancesNameOptional = componentInstancesNames.stream() - .filter(cin -> input.getName().startsWith(cin + "_")).findFirst(); - final Optional componentInstanceIdOptional = componentInstancesProperties.keySet().stream() - .filter(key -> key.endsWith("." + componentInstancesNameOptional.get())).findFirst(); - - final String componentInstanceId = componentInstanceIdOptional.get(); - final List componentInstanceProperties = componentInstancesProperties.get(componentInstanceId); + String componentInstanceId = null; + ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(); + + outer: + for (ComponentInstance instance : componentInstances) { + for (PropertyDefinition instanceProperty : instance.getProperties()) { + if (CollectionUtils.isNotEmpty(instanceProperty.getGetInputValues())) { + for (GetInputValueDataDefinition getInputValueDataDefinition : instanceProperty.getGetInputValues()) { + if (input.getName().equals(getInputValueDataDefinition.getInputName())) { + componentInstanceId = instance.getUniqueId(); + componentInstanceProperty = new ComponentInstanceProperty(instanceProperty); + break outer; + } + } + } + } + } - final ComponentInstanceProperty componentInstanceProperty = componentInstanceProperties.stream() - .filter(prop -> prop.getName().equals(propertyNameFromInput) && prop.getValue() != null - && prop.getValue().contains(ToscaGetFunctionType.GET_INPUT.getFunctionName())).findFirst().get(); + //unmapping instance property declared inputs from substitution mapping + input.setMappedToComponentProperty(false); // From Instance updateInput(input, componentInstanceProperty, userId, componentInstanceId); @@ -753,13 +926,21 @@ public class ServiceImportBusinessLogic { } } - private void associateInputToServiceProperty(final String userId, - final InputDefinition input, final Service component) { + private String associateInputToServiceProperty(final String userId, + final InputDefinition input, final Service component, + final Map> substitutionMappingProperties) { final List properties = component.getProperties(); - if (CollectionUtils.isNotEmpty(properties)) { - final String propertyNameFromInput = input.getName(); - final Optional propDefOptional = properties.stream().filter(prop -> prop.getName().equals(propertyNameFromInput)) - .findFirst(); + if (CollectionUtils.isNotEmpty(properties) && MapUtils.isNotEmpty(substitutionMappingProperties)) { + AtomicReference propertyNameFromInput = new AtomicReference<>(" "); + substitutionMappingProperties.entrySet().forEach(stringEntry -> { + if (stringEntry.getValue().get(0).equals(input.getName())) { + propertyNameFromInput.set(stringEntry.getKey()); + } + }); + + final Optional propDefOptional = + properties.stream().filter(prop -> prop.getName().equals(propertyNameFromInput.get())) + .findFirst(); if (propDefOptional.isPresent()) { // From SELF final String componentUniqueId = component.getUniqueId(); @@ -775,8 +956,12 @@ public class ServiceImportBusinessLogic { if (either.isRight()) { throw new ComponentException(ActionStatus.GENERAL_ERROR); } + } else { + input.setMappedToComponentProperty(false); + return propertyNameFromInput.get(); } } + return ""; } private void updateProperty(final PropertyDefinition propertyDefinition, final InputDefinition input, final String componentUniqueId) { @@ -797,12 +982,6 @@ public class ServiceImportBusinessLogic { input.setParentPropertyType(propertyDefinition.getType()); } - private String extractPropertyNameFromInputName(final String inputName, final List componentInstancesNames) { - final AtomicReference result = new AtomicReference<>(inputName); - componentInstancesNames.forEach(cin -> result.set(result.get().replace(cin + "_", ""))); - return result.get(); - } - protected Either createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum operation, List createdArtifacts, String yamlFileName, CsarInfo csarInfo, Resource preparedResource, @@ -810,8 +989,9 @@ public class ServiceImportBusinessLogic { boolean inTransaction, boolean shouldLock) { String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName(); Resource resource = preparedResource; - Map>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts - .getNodeTypesArtifactsToHandle(); + Map>> nodeTypesArtifactsToHandle = + nodeTypeInfoToUpdateArtifacts + .getNodeTypesArtifactsToHandle(); if (preparedResource.getResourceType() == ResourceTypeEnum.VF) { if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) { Either, ResponseFormat> handleNodeTypeArtifactsRes = handleNodeTypeArtifacts(preparedResource, @@ -938,7 +1118,8 @@ public class ServiceImportBusinessLogic { vfCsarArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value()); } else { - Either>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle( + Either>, ResponseFormat> + findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle( component, artifactPathAndNameList.left().value(), csarInfo.getModifier()); if (findVfCsarArtifactsToHandleRes.isRight()) { resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value()); @@ -1097,7 +1278,6 @@ public class ServiceImportBusinessLogic { protected Either createOrUpdateArtifacts(ArtifactOperationEnum operation, List createdArtifacts, String yamlFileName, CsarInfo csarInfo, Service preparedService, - NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts, boolean inTransaction, boolean shouldLock) { Either createdCsarArtifactsEither = handleVfCsarArtifacts(preparedService, csarInfo, createdArtifacts, new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction); @@ -1229,7 +1409,8 @@ public class ServiceImportBusinessLogic { EnumMap> nodeTypeArtifactsToHandle = new EnumMap<>( ArtifactsBusinessLogic.ArtifactOperationEnum.class); Wrapper responseWrapper = new Wrapper<>(); - Either>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either + Either>, ResponseFormat> + nodeTypeArtifactsToHandleRes = Either .left(nodeTypeArtifactsToHandle); try { List artifactsToUpload = new ArrayList<>(artifactPathAndNameList); @@ -1335,21 +1516,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); @@ -1765,11 +1945,12 @@ public class ServiceImportBusinessLogic { log.debug("************* Going to create all nodes {}", yamlName); handleServiceNodeTypes(yamlName, service, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeName); + List serviceProperties = null != service ? service.getProperties() : Collections.emptyList(); if (MapUtils.isNotEmpty(uploadComponentInstanceInfoMap)) { log.debug("************* Going to create all resource instances {}", yamlName); service = createServiceInstances(yamlName, service, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes()); log.debug("************* Going to create all relations {}", yamlName); - service = createServiceInstancesRelations(csarInfo.getModifier(), yamlName, service, uploadComponentInstanceInfoMap); + service = createServiceInstancesRelations(csarInfo.getModifier(), yamlName, service, uploadComponentInstanceInfoMap, serviceProperties); log.debug("************* Going to create positions {}", yamlName); compositionBusinessLogic.setPositionsForComponentInstances(service, csarInfo.getModifier().getUserId()); log.debug("************* Finished to set positions {}", yamlName); @@ -1778,7 +1959,8 @@ public class ServiceImportBusinessLogic { } protected Service createServiceInstancesRelations(User user, String yamlName, Service service, - Map uploadResInstancesMap) { + Map uploadResInstancesMap, + List serviceProperties) { log.debug("#createResourceInstancesRelations - Going to create relations "); List componentInstancesList = service.getComponentInstances(); if (MapUtils.isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList)) { // PNF can have no resource instances @@ -1806,6 +1988,7 @@ public class ServiceImportBusinessLogic { final Map allDataTypesMap = componentsUtils.getAllDataTypes(applicationDataTypeCache, service.getModel()); final Service service1 = service; + service1.setProperties(serviceProperties); uploadResInstancesMap.values().forEach( i -> processComponentInstance(yamlName, service1, componentInstancesList, allDataTypesMap, instProperties, @@ -1864,7 +2047,7 @@ public class ServiceImportBusinessLogic { .forEach(instanceProperty -> { toscaFunctionService.updateFunctionWithDataFromSelfComponent(instanceProperty.getToscaFunction(), updatedService, instancePropertyMap, instanceAttributeMap); - instanceProperty.setValue(instanceProperty.getToscaFunction().getValue()); + instanceProperty.setValue(StringEscapeUtils.unescapeJava(instanceProperty.getToscaFunction().getValue())); }) ); } @@ -1911,7 +2094,9 @@ public class ServiceImportBusinessLogic { instAttributes.put(resourceInstanceId, originResource.getAttributes()); addAttributeValueToResourceInstance(instAttributes, uploadComponentInstanceInfo.getAttributes()); } - if (uploadComponentInstanceInfo.getUploadNodeFilterInfo() != null) { + if (uploadComponentInstanceInfo.getUploadNodeFilterInfo() == null) { + instNodeFilter.put(resourceInstanceId, new UploadNodeFilterInfo()); + } else { instNodeFilter.put(resourceInstanceId, uploadComponentInstanceInfo.getUploadNodeFilterInfo()); } if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getInterfaces())) { @@ -2202,12 +2387,22 @@ public class ServiceImportBusinessLogic { Map operations = uploadInterfaceInfo.getOperations(); for (Map.Entry operation : operations.entrySet()) { - OperationDataDefinition templateOperation = currentInterfaceDef.getOperationsMap().get(operation.getKey()); OperationDataDefinition instanceOperation = operation.getValue(); + OperationDataDefinition templateOperation = currentInterfaceDef.getOperationsMap().getOrDefault(operation.getKey(), new Operation(instanceOperation)); //Inputs ListDataDefinition instanceInputs = instanceOperation.getInputs(); - mergeOperationInputDefinitions(templateOperation.getInputs(), instanceInputs); - templateOperation.setInputs(instanceInputs); + if (null != instanceInputs) { + mergeOperationInputDefinitions(templateOperation.getInputs(), instanceInputs); + component.getProperties() + .forEach(property -> instanceInputs.getListToscaDataDefinition().stream() + .filter(instanceInput -> + instanceInput.getToscaFunction() instanceof ToscaGetFunctionDataDefinition && + property.getName().equals(instanceInput.getToscaFunction() != null ? + ((ToscaGetFunctionDataDefinition) instanceInput.getToscaFunction()).getPropertyName() : null)) + .forEach(oldInput -> oldInput.setType(property.getType())) + ); + templateOperation.setInputs(instanceInputs); + } //Implementation templateOperation.setImplementation(instanceOperation.getImplementation()); //Description @@ -2220,7 +2415,7 @@ public class ServiceImportBusinessLogic { interfaceDef.setUniqueId(currentInterfaceDef.getType()); interfaceDef.setDescription(uploadInterfaceInfo.getDescription()); interfaceDef.setOperations(operationsToAdd); - instInterfacesMap.put(interfaceName, interfaceDef); + instInterfacesMap.put(currentInterfaceDef.getType(), interfaceDef); currInterfacesMap.remove(interfaceName); } } @@ -2589,7 +2784,8 @@ public class ServiceImportBusinessLogic { if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName()); } - Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo, existingnodeTypeMap); + Resource refResource = + validateResourceInstanceBeforeCreate(yamlName, component.getModel(), uploadComponentInstanceInfo, existingnodeTypeMap); ComponentInstance componentInstance = new ComponentInstance(); componentInstance.setComponentUid(refResource.getUniqueId()); Collection directives = uploadComponentInstanceInfo.getDirectives(); @@ -2620,27 +2816,32 @@ public class ServiceImportBusinessLogic { Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType()); componentInstance.setName(uploadComponentInstanceInfo.getName()); componentInstance.setIcon(origResource.getIcon()); + componentInstance.setMinOccurrences(uploadComponentInstanceInfo.getMinOccurrences()); + componentInstance.setMaxOccurrences(uploadComponentInstanceInfo.getMaxOccurrences()); + componentInstance.setInstanceCount(uploadComponentInstanceInfo.getInstanceCount()); resourcesInstancesMap.put(componentInstance, origResource); - } catch (Exception e) { + } catch (final ComponentException e) { + throw e; + } catch (final Exception e) { throw new ComponentException(ActionStatus.GENERAL_ERROR, e.getMessage()); } } - protected Resource validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo, + protected Resource validateResourceInstanceBeforeCreate(String yamlName, String model, UploadComponentInstanceInfo uploadComponentInstanceInfo, Map nodeNamespaceMap) { Resource refResource; try { if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()); } else { - Either findResourceEither = toscaOperationFacade - .getLatestResourceByToscaResourceName(uploadComponentInstanceInfo.getType()); - if (findResourceEither.isRight()) { + final Either resourceEither = + toscaOperationFacade.getLatestByToscaResourceName(uploadComponentInstanceInfo.getType(), model); + if (resourceEither.isRight()) { ResponseFormat responseFormat = componentsUtils - .getResponseFormat(componentsUtils.convertFromStorageResponse(findResourceEither.right().value())); + .getResponseFormat(componentsUtils.convertFromStorageResponse(resourceEither.right().value())); throw new ComponentException(responseFormat); } - refResource = findResourceEither.left().value(); + refResource = (Resource) resourceEither.left().value(); nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource); } String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState(); @@ -2658,7 +2859,9 @@ public class ServiceImportBusinessLogic { throw new ComponentException(responseFormat); } return refResource; - } catch (Exception e) { + } catch (final ComponentException e) { + throw e; + } catch (final Exception e) { throw new ComponentException(ActionStatus.GENERAL_ERROR, e.getMessage()); } }