diff options
author | vasraz <vasyl.razinkov@est.tech> | 2021-04-26 17:08:41 +0100 |
---|---|---|
committer | Vasyl Razinkov <vasyl.razinkov@est.tech> | 2021-04-28 20:08:27 +0000 |
commit | 116c4a25d915411617a3382db47e56930fb6a3ba (patch) | |
tree | 991e15aa234fe99d0109b93bbd7b12d7f6654825 /catalog-be/src | |
parent | 5aa2c74c18bc5f7e9774f192d7419259a0fac733 (diff) |
Add Service Template instance attributes to TOSCA Export
Change-Id: I66c4f682814981b9829ad099e35f96a80927e7dc
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Issue-ID: SDC-3568
Diffstat (limited to 'catalog-be/src')
6 files changed, 453 insertions, 287 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java index 83799e6405..561480dd64 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java @@ -32,7 +32,7 @@ import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter; -import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; +import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter; import org.openecomp.sdc.be.tosca.model.ToscaAttribute; import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition; import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; @@ -69,7 +69,7 @@ public class AttributeConverter { * @param attributeDefinition the attribute definition to be converted * @return the {@link ToscaAttribute} instance based on the the given {@link AttributeDefinition} instance */ - public ToscaAttribute convert(final AttributeDefinition attributeDefinition) throws ToscaConversionException { + public ToscaAttribute convert(final AttributeDefinition attributeDefinition) { final ToscaAttribute toscaAttribute = new ToscaAttribute(); LOGGER.trace("Converting attribute '{}' from type '{}' with default value '{}'", attributeDefinition.getName(), attributeDefinition.getType(), attributeDefinition.getDefaultValue()); @@ -77,13 +77,11 @@ public class AttributeConverter { toscaAttribute.setType(attributeDefinition.getType()); toscaAttribute.setDescription(attributeDefinition.getDescription()); toscaAttribute.setStatus(attributeDefinition.getStatus()); - final Object defaultValue = convertToToscaObject(attributeDefinition.getName(), attributeDefinition.getType(), - attributeDefinition.getDefaultValue(), attributeDefinition.getEntry_schema(), false); + final Object defaultValue = convertToToscaObject(attributeDefinition, attributeDefinition.getDefaultValue(), false); if (defaultValue != null) { toscaAttribute.setDefault(defaultValue); } - final Object value = convertToToscaObject(attributeDefinition.getName(), attributeDefinition.getType(), attributeDefinition.getValue(), - attributeDefinition.getEntry_schema(), false); + final Object value = convertToToscaObject(attributeDefinition, attributeDefinition.getValue(), false); if (value != null) { toscaAttribute.setValue(value); } @@ -100,8 +98,13 @@ public class AttributeConverter { return toscaSchemaDefinition; } - private Object convertToToscaObject(final String name, final String attributeType, String value, final EntrySchema schemaDefinition, - final boolean preserveEmptyValue) throws ToscaConversionException { + private Object convertToToscaObject(final AttributeDefinition attributeDefinition, + String value, + final boolean preserveEmptyValue) { + final String name = attributeDefinition.getName(); + final String attributeType = attributeDefinition.getType(); + final EntrySchema schemaDefinition = attributeDefinition.getEntry_schema(); + final String innerType = schemaDefinition == null ? attributeType : schemaDefinition.getType(); LOGGER.trace("Converting attribute '{}' of type '{}', value '{}', innerType '{}'", name, attributeType, value, innerType); if (StringUtils.isEmpty(value)) { @@ -141,11 +144,11 @@ public class AttributeConverter { } catch (final JsonParseException e) { final String errorMsg = "Failed to parse json value"; LOGGER.error(EcompLoggerErrorCode.SCHEMA_ERROR, "Attribute Converter", errorMsg, e); - throw new ToscaConversionException(errorMsg, e); + return null; } catch (final Exception e) { final String errorMsg = "Unexpected error occurred while converting attribute value to TOSCA"; LOGGER.error(EcompLoggerErrorCode.UNKNOWN_ERROR, "Attribute Converter", errorMsg, e); - throw new ToscaConversionException(errorMsg, e); + return null; } } @@ -167,4 +170,13 @@ public class AttributeConverter { private String getTypeDefaultValue(final String attributeType) { return DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(attributeType, dataTypes); } + + public void convertAndAddValue(final Map<String, Object> attribs, + final AttributeDefinition attribute) { + final Object convertedValue = convertToToscaObject(attribute, attribute.getDefaultValue(), false); + if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) { + attribs.put(attribute.getName(), convertedValue); + } + } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index d211a8bb76..45180008ab 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -53,6 +53,7 @@ import org.apache.commons.lang3.tuple.ImmutableTriple; import org.apache.commons.lang3.tuple.Triple; import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException; +import org.openecomp.sdc.be.config.Configuration; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition; @@ -77,6 +78,7 @@ import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; 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.ComponentInstanceInterface; import org.openecomp.sdc.be.model.ComponentInstanceProperty; @@ -151,24 +153,32 @@ public class ToscaExportHandler { private static final String FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION = "convertToToscaTemplate - failed to get Default Imports section from configuration"; private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}"; private static final String NATIVE_ROOT = "tosca.nodes.Root"; - private static final List<String> EXCLUDED_CATEGORY_SPECIFIC_METADATA = List.of("Service Function", "Service Role", "Naming Policy", "Service Type"); + private static final List<String> EXCLUDED_CATEGORY_SPECIFIC_METADATA = List + .of("Service Function", "Service Role", "Naming Policy", "Service Type"); private static final YamlUtil yamlUtil = new YamlUtil(); + private static final String COULD_NOT_PARSE_COMPONENT_ATTRIBUTES_COMPONENT_UNIQUE_ID = "Could not parse component '{}' attributes. Component unique id '{}'."; private ApplicationDataTypeCache dataTypeCache; private ToscaOperationFacade toscaOperationFacade; private CapabilityRequirementConverter capabilityRequirementConverter; private PolicyExportParser policyExportParser; private GroupExportParser groupExportParser; private PropertyConvertor propertyConvertor; + private AttributeConverter attributeConverter; private InputConverter inputConverter; private OutputConverter outputConverter; private InterfaceLifecycleOperation interfaceLifecycleOperation; private InterfacesOperationsConverter interfacesOperationsConverter; @Autowired - public ToscaExportHandler(final ApplicationDataTypeCache dataTypeCache, final ToscaOperationFacade toscaOperationFacade, - final CapabilityRequirementConverter capabilityRequirementConverter, final PolicyExportParser policyExportParser, - final GroupExportParser groupExportParser, final PropertyConvertor propertyConvertor, - final InputConverter inputConverter, final OutputConverter outputConverter, + public ToscaExportHandler(final ApplicationDataTypeCache dataTypeCache, + final ToscaOperationFacade toscaOperationFacade, + final CapabilityRequirementConverter capabilityRequirementConverter, + final PolicyExportParser policyExportParser, + final GroupExportParser groupExportParser, + final PropertyConvertor propertyConvertor, + final AttributeConverter attributeConverter, + final InputConverter inputConverter, + final OutputConverter outputConverter, final InterfaceLifecycleOperation interfaceLifecycleOperation, final InterfacesOperationsConverter interfacesOperationsConverter) { this.dataTypeCache = dataTypeCache; @@ -177,6 +187,7 @@ public class ToscaExportHandler { this.policyExportParser = policyExportParser; this.groupExportParser = groupExportParser; this.propertyConvertor = propertyConvertor; + this.attributeConverter = attributeConverter; this.inputConverter = inputConverter; this.outputConverter = outputConverter; this.interfaceLifecycleOperation = interfaceLifecycleOperation; @@ -184,7 +195,7 @@ public class ToscaExportHandler { } public static String getInterfaceFilename(String artifactName) { - return artifactName.substring(0, artifactName.lastIndexOf('.')) + ToscaExportHandler.TOSCA_INTERFACE_NAME; + return artifactName.substring(0, artifactName.lastIndexOf('.')) + TOSCA_INTERFACE_NAME; } private static void removeOperationImplementationForProxyNodeType(Map<String, InterfaceDefinition> proxyComponentInterfaces) { @@ -206,10 +217,12 @@ public class ToscaExportHandler { return Either.right(ToscaError.GENERAL_ERROR); } List<Triple<String, String, Component>> dependencies = new ArrayList<>(); - if (component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().startsWith("org.openecomp.resource.abstract.nodes.")) { - final Either<Component, StorageOperationStatus> baseType = toscaOperationFacade.getByToscaResourceNameAndVersion(component.getDerivedFromGenericType(), component.getDerivedFromGenericVersion()); + if (component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType() + .startsWith("org.openecomp.resource.abstract.nodes.")) { + final Either<Component, StorageOperationStatus> baseType = toscaOperationFacade + .getByToscaResourceNameAndVersion(component.getDerivedFromGenericType(), component.getDerivedFromGenericVersion()); if (baseType.isLeft() && baseType.left().value() != null) { - addDependencies(imports, dependencies , baseType.left().value()); + addDependencies(imports, dependencies, baseType.left().value()); } else { log.debug("Failed to fetch derived from type {}", component.getDerivedFromGenericType()); } @@ -245,9 +258,9 @@ public class ToscaExportHandler { Yaml yaml = new Yaml(representer, options); String yamlAsString = yaml.dumpAsMap(toscaTemplate); StringBuilder sb = new StringBuilder(); - sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactHeader()); + sb.append(getConfiguration().getHeatEnvArtifactHeader()); sb.append(yamlAsString); - sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter()); + sb.append(getConfiguration().getHeatEnvArtifactFooter()); return ToscaRepresentation.make(sb.toString().getBytes(), toscaTemplate); } @@ -327,18 +340,15 @@ public class ToscaExportHandler { outputs = outputConverter.convert(component.getOutputs(), dataTypes); } catch (final ToscaConversionException e) { log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaExportHandler.class.getName(), - "Could not parse component '{}' outputs. Component unique id '{}'.", new Object[]{component.getName(), component.getUniqueId(), e}); + "Could not parse component '{}' outputs. Component unique id '{}'.", component.getName(), component.getUniqueId(), e); return Either.right(ToscaError.GENERAL_ERROR); } if (!outputs.isEmpty()) { topologyTemplate.setOutputs(outputs); } - final List<ComponentInstance> componentInstances = component.getComponentInstances(); - Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component.getComponentInstancesProperties(); - Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = component.getComponentInstancesInterfaces(); - if (CollectionUtils.isNotEmpty(componentInstances)) { - final Either<Map<String, ToscaNodeTemplate>, ToscaError> nodeTemplates = convertNodeTemplates(component, componentInstances, - componentInstancesProperties, componentInstanceInterfaces, componentCache, dataTypes, topologyTemplate); + if (CollectionUtils.isNotEmpty(component.getComponentInstances())) { + final Either<Map<String, ToscaNodeTemplate>, ToscaError> nodeTemplates = + convertNodeTemplates(component, componentCache, dataTypes, topologyTemplate); if (nodeTemplates.isRight()) { return Either.right(nodeTemplates.right().value()); } @@ -483,8 +493,9 @@ public class ToscaExportHandler { log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType()); } for (final String key : component.getCategorySpecificMetadata().keySet()) { - if (!EXCLUDED_CATEGORY_SPECIFIC_METADATA.contains(key)) + if (!EXCLUDED_CATEGORY_SPECIFIC_METADATA.contains(key)) { toscaMetadata.put(key, component.getCategorySpecificMetadata().get(key)); + } } return toscaMetadata; } @@ -509,7 +520,7 @@ public class ToscaExportHandler { List<Triple<String, String, Component>> dependencies = new ArrayList<>(); Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts(); if (isNotEmpty(toscaArtifacts)) { - ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE); + ArtifactDefinition artifactDefinition = toscaArtifacts.get(ASSET_TOSCA_TEMPLATE); if (artifactDefinition != null) { Map<String, Map<String, String>> importsListMember = new HashMap<>(); Map<String, String> interfaceFiles = new HashMap<>(); @@ -534,7 +545,7 @@ public class ToscaExportHandler { } private List<Map<String, Map<String, String>>> getDefaultToscaImportConfig() { - return ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultImports(); + return getConfiguration().getDefaultImports(); } private void createDependency(final Map<String, Component> componentCache, final List<Map<String, Map<String, String>>> imports, @@ -705,8 +716,7 @@ public class ToscaExportHandler { toscaAttributeMap = convertToToscaAttributes(component.getAttributes(), dataTypes); } catch (final ToscaConversionException e) { log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaExportHandler.class.getName(), - "Could not parse component '{}' attributes. Component unique id '{}'.", - new Object[]{component.getName(), component.getUniqueId(), e}); + COULD_NOT_PARSE_COMPONENT_ATTRIBUTES_COMPONENT_UNIQUE_ID, component.getName(), component.getUniqueId(), e); return Either.right(ToscaError.GENERAL_ERROR); } if (!toscaAttributeMap.isEmpty()) { @@ -757,10 +767,10 @@ public class ToscaExportHandler { if (CollectionUtils.isEmpty(attributeList)) { return Collections.emptyMap(); } - final AttributeConverter attributeConverter = new AttributeConverter(dataTypes); + final AttributeConverter converter = new AttributeConverter(dataTypes); final Map<String, ToscaAttribute> toscaAttributeMap = new HashMap<>(); for (final AttributeDefinition attributeDefinition : attributeList) { - toscaAttributeMap.put(attributeDefinition.getName(), attributeConverter.convert(attributeDefinition)); + toscaAttributeMap.put(attributeDefinition.getName(), converter.convert(attributeDefinition)); } return toscaAttributeMap; } @@ -779,8 +789,7 @@ public class ToscaExportHandler { log.debug("Capabilities converted for {}", component.getUniqueId()); Either<ToscaNodeType, ToscaError> requirements = capabilityRequirementConverter - .convertRequirements(componentsCache, component, - toscaNodeType); + .convertRequirements(componentsCache, component, toscaNodeType); if (requirements.isRight()) { return Either.right(requirements.right().value()); } @@ -808,22 +817,23 @@ public class ToscaExportHandler { return Either.left(toscaNode); } - protected Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplates( - Component component, - List<ComponentInstance> componentInstances, - Map<String, List<ComponentInstanceProperty>> componentInstancesProperties, - Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces, - Map<String, Component> componentCache, Map<String, DataTypeDefinition> dataTypes, - ToscaTopolgyTemplate topologyTemplate) { + private Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplates(final Component component, + final Map<String, Component> componentCache, + final Map<String, DataTypeDefinition> dataTypes, + final ToscaTopolgyTemplate topologyTemplate) { + + final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component.getComponentInstancesProperties(); + final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = component.getComponentInstancesAttributes(); + final Map<String, List<ComponentInstanceInput>> componentInstancesInputs = component.getComponentInstancesInputs(); + final Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = component.getComponentInstancesInterfaces(); + final List<RequirementCapabilityRelDef> componentInstancesRelations = component.getComponentInstancesRelations(); Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplatesRes = null; - log.debug("start convert topology template for {} for type {}", component.getUniqueId(), - component.getComponentType()); - Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>(); - Map<String, List<ComponentInstanceInput>> componentInstancesInputs = component.getComponentInstancesInputs(); + log.debug("start convert topology template for {} for type {}", component.getUniqueId(), component.getComponentType()); + final Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>(); Map<String, ToscaGroupTemplate> groupsMap = null; - for (ComponentInstance componentInstance : componentInstances) { + for (final ComponentInstance componentInstance : component.getComponentInstances()) { ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate(); if (MapUtils.isNotEmpty(componentInstance.getToscaArtifacts())) { nodeTemplate.setArtifacts(convertToNodeTemplateArtifacts(componentInstance.getToscaArtifacts())); @@ -832,35 +842,34 @@ public class ToscaExportHandler { nodeTemplate.setDirectives(componentInstance.getDirectives()); nodeTemplate.setNode_filter(convertToNodeTemplateNodeFilterComponent(componentInstance.getNodeFilter())); - Either<Component, Boolean> originComponentRes = capabilityRequirementConverter + final Either<Component, Boolean> originComponentRes = capabilityRequirementConverter .getOriginComponent(componentCache, componentInstance); if (originComponentRes.isRight()) { convertNodeTemplatesRes = Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR); break; } - Either<ToscaNodeTemplate, ToscaError> requirements = convertComponentInstanceRequirements(component, - componentInstance, component.getComponentInstancesRelations(), nodeTemplate, - originComponentRes.left().value(), componentCache); + final Either<ToscaNodeTemplate, ToscaError> requirements = convertComponentInstanceRequirements(component, componentInstance, + componentInstancesRelations, nodeTemplate, originComponentRes.left().value(), componentCache); if (requirements.isRight()) { convertNodeTemplatesRes = Either.right(requirements.right().value()); break; } - String instanceUniqueId = componentInstance.getUniqueId(); + final String instanceUniqueId = componentInstance.getUniqueId(); log.debug("Component instance Requirements converted for instance {}", instanceUniqueId); nodeTemplate = requirements.left().value(); - Component originalComponent = componentCache.get(componentInstance.getActualComponentUid()); + final Component originalComponent = componentCache.get(componentInstance.getActualComponentUid()); if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) { - Component componentOfProxy = componentCache.get(componentInstance.getComponentUid()); + final Component componentOfProxy = componentCache.get(componentInstance.getComponentUid()); nodeTemplate.setMetadata(convertMetadata(componentOfProxy, true, componentInstance)); } else { nodeTemplate.setMetadata(convertMetadata(originalComponent, true, componentInstance)); } - Either<ToscaNodeTemplate, ToscaError> capabilities = capabilityRequirementConverter - .convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate); + final Either<ToscaNodeTemplate, ToscaError> capabilities = + capabilityRequirementConverter.convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate); if (capabilities.isRight()) { convertNodeTemplatesRes = Either.right(capabilities.right().value()); break; @@ -868,49 +877,47 @@ public class ToscaExportHandler { log.debug("Component instance Capabilities converted for instance {}", instanceUniqueId); nodeTemplate = capabilities.left().value(); - Map<String, Object> props = new HashMap<>(); + final Map<String, Object> props = new HashMap<>(); + final Map<String, Object> attribs = new HashMap<>(); if (originalComponent.getComponentType() == ComponentTypeEnum.RESOURCE) { // Adds the properties of parent component to map addPropertiesOfParentComponent(dataTypes, originalComponent, props); + addAttributesOfParentComponent(originalComponent, attribs); } if (null != componentInstancesProperties && componentInstancesProperties.containsKey(instanceUniqueId)) { - addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes, instanceUniqueId, - props); + addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes, instanceUniqueId, props); + } + if (null != componentInstancesAttributes && componentInstancesAttributes.containsKey(instanceUniqueId)) { + addAttributesOfComponentInstance(componentInstancesAttributes, instanceUniqueId, attribs); } - if (componentInstancesInputs != null && componentInstancesInputs.containsKey(instanceUniqueId) + if (componentInstancesInputs != null + && componentInstancesInputs.containsKey(instanceUniqueId) && !isComponentOfTypeServiceProxy(componentInstance)) { //For service proxy the inputs are already handled under instance properties above addComponentInstanceInputs(dataTypes, componentInstancesInputs, instanceUniqueId, props); } //M3[00001] - NODE TEMPLATE INTERFACES - START - handleInstanceInterfaces(componentInstanceInterfaces, componentInstance, dataTypes, nodeTemplate, - instanceUniqueId, component); + handleInstanceInterfaces(componentInstanceInterfaces, componentInstance, dataTypes, nodeTemplate, instanceUniqueId, component); //M3[00001] - NODE TEMPLATE INTERFACES - END - if (props != null && !props.isEmpty()) { + if (MapUtils.isNotEmpty(props)) { nodeTemplate.setProperties(props); } + if (MapUtils.isNotEmpty(attribs)) { + nodeTemplate.setAttributes(attribs); + } - List<GroupInstance> groupInstances = componentInstance.getGroupInstances(); - if (groupInstances != null) { + final List<GroupInstance> groupInstances = componentInstance.getGroupInstances(); + if (CollectionUtils.isNotEmpty(groupInstances)) { if (groupsMap == null) { groupsMap = new HashMap<>(); } - for (GroupInstance groupInst : groupInstances) { - boolean addToTosca = true; - - List<String> artifacts = groupInst.getArtifacts(); - if (artifacts == null || artifacts.isEmpty()) { - addToTosca = false; - } - - if (addToTosca) { - ToscaGroupTemplate toscaGroup = groupExportParser - .getToscaGroupTemplate(groupInst, componentInstance.getInvariantName()); - groupsMap.put(groupInst.getName(), toscaGroup); + for (final GroupInstance groupInst : groupInstances) { + if (CollectionUtils.isNotEmpty(groupInst.getArtifacts())) { + groupsMap.put(groupInst.getName(), groupExportParser.getToscaGroupTemplate(groupInst, componentInstance.getInvariantName())); } } } @@ -923,19 +930,15 @@ public class ToscaExportHandler { } if (component.getComponentType() == ComponentTypeEnum.SERVICE && isNotEmpty( ((Service) component).getForwardingPaths())) { - log.debug("Starting converting paths for component {}, name {}", component.getUniqueId(), - component.getName()); + log.debug("Starting converting paths for component {}, name {}", component.getUniqueId(), component.getName()); ForwardingPathToscaUtil - .addForwardingPaths((Service) component, nodeTemplates, capabilityRequirementConverter, componentCache, - toscaOperationFacade); - log.debug("Finished converting paths for component {}, name {}", component.getUniqueId(), - component.getName()); + .addForwardingPaths((Service) component, nodeTemplates, capabilityRequirementConverter, componentCache, toscaOperationFacade); + log.debug("Finished converting paths for component {}, name {}", component.getUniqueId(), component.getName()); } if (convertNodeTemplatesRes == null) { convertNodeTemplatesRes = Either.left(nodeTemplates); } - log.debug("finish convert topology template for {} for type {}", component.getUniqueId(), - component.getComponentType()); + log.debug("finish convert topology template for {} for type {}", component.getUniqueId(), component.getComponentType()); return convertNodeTemplatesRes; } @@ -986,16 +989,30 @@ public class ToscaExportHandler { } } - private void addPropertiesOfComponentInstance( - Map<String, List<ComponentInstanceProperty>> componentInstancesProperties, - Map<String, DataTypeDefinition> dataTypes, String instanceUniqueId, - Map<String, Object> props) { + private void addPropertiesOfComponentInstance(final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties, + final Map<String, DataTypeDefinition> dataTypes, + final String instanceUniqueId, + final Map<String, Object> props) { if (isNotEmpty(componentInstancesProperties)) { componentInstancesProperties.get(instanceUniqueId) // Converts and adds each value to property map - .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop, - prop::getValue)); + .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop, prop::getValue)); + } + } + + private void addAttributesOfComponentInstance(final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes, + final String instanceUniqueId, + final Map<String, Object> attribs) { + + if (isNotEmpty(componentInstancesAttributes) && componentInstancesAttributes.containsKey(instanceUniqueId)) { + componentInstancesAttributes.get(instanceUniqueId).stream() + // Filters out Attributes with empty default values + .filter(attributeDefinition -> StringUtils.isNotEmpty(attributeDefinition.getDefaultValue())) + // Converts and adds each value to attribute map + .forEach(attributeDefinition -> { + attributeConverter.convertAndAddValue(attribs, attributeDefinition); + }); } } @@ -1008,8 +1025,21 @@ public class ToscaExportHandler { // Filters out properties with empty default values .filter(prop -> StringUtils.isNotEmpty(prop.getDefaultValue())) // Converts and adds each value to property map - .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop, - prop::getDefaultValue)); + .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop, prop::getDefaultValue)); + } + } + + private void addAttributesOfParentComponent(final Component componentOfInstance, final Map<String, Object> attribs) { + + final List<AttributeDefinition> componentAttributes = componentOfInstance.getAttributes(); + if (isNotEmpty(componentAttributes)) { + componentAttributes.stream() + // Filters out Attributes with empty default values + .filter(attrib -> StringUtils.isNotEmpty(attrib.getDefaultValue())) + // Converts and adds each value to attribute map + .forEach(attributeDefinition -> { + attributeConverter.convertAndAddValue(attribs, attributeDefinition); + }); } } @@ -1459,7 +1489,7 @@ public class ToscaExportHandler { return arts; } - protected NodeFilter convertToNodeTemplateNodeFilterComponent(CINodeFilterDataDefinition inNodeFilter) { + private NodeFilter convertToNodeTemplateNodeFilterComponent(CINodeFilterDataDefinition inNodeFilter) { if (inNodeFilter == null) { return null; } @@ -1530,7 +1560,7 @@ public class ToscaExportHandler { Map<String, List<Object>> propertyMapCopy = new HashMap<>(); for (RequirementNodeFilterPropertyDataDefinition propertyDataDefinition : origProperties.getListToscaDataDefinition()) { for (String propertyInfoEntry : propertyDataDefinition.getConstraints()) { - Map propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class); + Map<String, List<Object>> propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class); String propertyName = propertyDataDefinition.getName(); if (propertyMapCopy.containsKey(propertyName)) { addPropertyConstraintValueToList(propertyName, propertyValObj, propertyMapCopy.get(propertyName)); @@ -1592,7 +1622,7 @@ public class ToscaExportHandler { if (component == null || CollectionUtils.isEmpty(component.getInputs())) { return Collections.emptyMap(); } - return component.getInputs().stream().filter(input -> input.isMappedToComponentProperty()).map(PropertyDataDefinition::getName) + return component.getInputs().stream().filter(InputDefinition::isMappedToComponentProperty).map(PropertyDataDefinition::getName) .collect(Collectors.toMap(inputName -> inputName, inputName -> new String[]{inputName}, (inputName1, inputName2) -> inputName1)); } @@ -1797,4 +1827,9 @@ public class ToscaExportHandler { return new LinkedHashSet<>(fields); } } + + private Configuration getConfiguration() { + return ConfigurationManager.getConfigurationManager().getConfiguration(); + } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeTemplate.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeTemplate.java index e65d9a2425..f909a9c7b1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeTemplate.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeTemplate.java @@ -38,6 +38,7 @@ public class ToscaNodeTemplate { private Map<String, String> metadata; private String description; private Map<String, Object> properties; + private Map<String, Object> attributes; private List<Map<String, ToscaTemplateRequirement>> requirements; private Map<String, ToscaTemplateCapability> capabilities; private Map<String, ToscaTemplateArtifact> artifacts; diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java index 2bd529541b..fcc0ebfffd 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.be.tosca; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -44,7 +45,7 @@ class AttributeConverterTest { @Test void testScalarTypeConversion() throws ToscaConversionException { //given - final AttributeConverter attributeConverter = new AttributeConverter(Collections.emptyMap()); + final AttributeConverter attributeConverter = createTestSubject(); final AttributeDefinition attributeDefinition = new AttributeDefinition(); attributeDefinition.setType(ToscaPropertyType.STRING.getType()); attributeDefinition.setDescription("aDescription"); @@ -59,7 +60,7 @@ class AttributeConverterTest { @Test void testScalarNoDefaultValueConversion() throws ToscaConversionException { //given - final AttributeConverter attributeConverter = new AttributeConverter(Collections.emptyMap()); + final AttributeConverter attributeConverter = createTestSubject(); final AttributeDefinition attributeDefinition = new AttributeDefinition(); attributeDefinition.setType(ToscaPropertyType.STRING.getType()); attributeDefinition.setDescription("aDescription"); @@ -132,13 +133,29 @@ class AttributeConverterTest { @Test void testInvalidDefaultValueJsonConversion() { //given - final AttributeConverter attributeConverter = new AttributeConverter(Collections.emptyMap()); + final AttributeConverter attributeConverter = createTestSubject(); final AttributeDefinition attributeDefinition = new AttributeDefinition(); attributeDefinition.setDefaultValue(": thisIsAnInvalidJson"); //when, then - final ToscaConversionException toscaConversionException = assertThrows(ToscaConversionException.class, - () -> attributeConverter.convert(attributeDefinition)); - assertEquals("Failed to parse json value", toscaConversionException.getMessage()); + final ToscaAttribute attribute = attributeConverter.convert(attributeDefinition); + assertNotNull(attribute); + assertNull(attribute.getDefault()); + } + + @Test + void testConvertAndAddValue() throws ToscaConversionException { + final AttributeConverter converter = createTestSubject(); + final AttributeDefinition attribute = new AttributeDefinition(); + attribute.setName("attrib"); + attribute.setDefaultValue("default"); + final Map<String, Object> attribs = new HashMap<>(); + converter.convertAndAddValue(attribs, attribute); + assertEquals(1, attribs.size()); + assertEquals("default", attribs.get("attrib")); + } + + private AttributeConverter createTestSubject() { + return new AttributeConverter(Collections.emptyMap()); } private void assertAttribute(final AttributeDefinition expectedAttributeDefinition, final ToscaAttribute actualToscaAttribute) { diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java index 3c425c1f08..771d52f6b0 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java @@ -57,8 +57,6 @@ import org.apache.commons.collections4.MapUtils; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.be.DummyConfigurationManager; import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; @@ -105,7 +103,7 @@ class InterfacesOperationsConverterTest { @BeforeEach public void setUpBeforeTest() { interfacesOperationsConverter = - new InterfacesOperationsConverter(new PropertyConvertor()); + new InterfacesOperationsConverter(new PropertyConvertor()); } @Test @@ -122,10 +120,10 @@ class InterfacesOperationsConverterTest { component.setInterfaces(new HashMap<>()); component.getInterfaces().put(interfaceType, addedInterface); final Map<String, Object> interfaceTypeElement = - addInterfaceTypeElement(component, new ArrayList<>()); + addInterfaceTypeElement(component, new ArrayList<>()); - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,null, - interfacesOperationsConverter); + ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, + interfacesOperationsConverter); ToscaTemplate template = new ToscaTemplate("test"); template.setInterface_types(interfaceTypeElement); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); @@ -150,10 +148,10 @@ class InterfacesOperationsConverterTest { component.setInterfaces(new HashMap<>()); component.getInterfaces().put(interfaceType, addedInterface); final Map<String, Object> interfaceTypeElement = - addInterfaceTypeElement(component, new ArrayList<>()); + addInterfaceTypeElement(component, new ArrayList<>()); - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,null, - interfacesOperationsConverter); + ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, + interfacesOperationsConverter); ToscaTemplate template = new ToscaTemplate("testService"); template.setInterface_types(interfaceTypeElement); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); @@ -178,8 +176,8 @@ class InterfacesOperationsConverterTest { ToscaNodeType nodeType = new ToscaNodeType(); interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,null, - interfacesOperationsConverter); + ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, + interfacesOperationsConverter); ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put(NODE_TYPE_NAME, nodeType); @@ -208,8 +206,8 @@ class InterfacesOperationsConverterTest { ToscaNodeType nodeType = new ToscaNodeType(); interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,null, - interfacesOperationsConverter); + ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, + interfacesOperationsConverter); ToscaTemplate template = new ToscaTemplate("testService"); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put(NODE_TYPE_NAME, nodeType); @@ -236,7 +234,7 @@ class InterfacesOperationsConverterTest { component.setInterfaces(new HashMap<>()); component.getInterfaces().put(interfaceType, addedInterface); Map<String, Object> interfacesMap = interfacesOperationsConverter - .getInterfacesMap(component, null, component.getInterfaces(), null, false, true); + .getInterfacesMap(component, null, component.getInterfaces(), null, false, true); ToscaNodeType nodeType = new ToscaNodeType(); nodeType.setInterfaces(interfacesMap); ToscaExportHandler handler = new ToscaExportHandler(); @@ -267,8 +265,8 @@ class InterfacesOperationsConverterTest { ToscaNodeType nodeType = new ToscaNodeType(); interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, null, false); - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,null, - interfacesOperationsConverter); + ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, + interfacesOperationsConverter); ToscaTemplate template = new ToscaTemplate("test"); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put("test", nodeType); @@ -290,19 +288,19 @@ class InterfacesOperationsConverterTest { addedInterface.setType(addedInterfaceType); addOperationsToInterface(component, addedInterface, 2, 2, true, true); addedInterface.getOperationsMap().values().stream() - .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( - "name_for_op_0")) - .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() - .filter(operationInputDefinition -> operationInputDefinition.getName().contains("integer")) - .forEach(operationInputDefinition -> operationInputDefinition.setInputId(addedInterfaceType + - ".name_for_op_1.output_integer_1"))); + .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( + "name_for_op_0")) + .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() + .filter(operationInputDefinition -> operationInputDefinition.getName().contains("integer")) + .forEach(operationInputDefinition -> operationInputDefinition.setInputId(addedInterfaceType + + ".name_for_op_1.output_integer_1"))); component.setInterfaces(new HashMap<>()); component.getInterfaces().put(addedInterfaceType, addedInterface); ToscaNodeType nodeType = new ToscaNodeType(); interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,null, - interfacesOperationsConverter); + ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, + interfacesOperationsConverter); ToscaTemplate template = new ToscaTemplate("test"); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put("test", nodeType); @@ -325,24 +323,24 @@ class InterfacesOperationsConverterTest { addedInterface.setType(addedInterfaceType); addOperationsToInterface(component, addedInterface, 2, 2, true, true); addedInterface.getOperationsMap().values().stream() - .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( - "name_for_op_0")) - .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() - .filter(opInputDef -> opInputDef.getName().contains("integer")) - .forEach(opInputDef -> opInputDef.setInputId( - addedInterfaceType +".name_for_op_1.output_integer_1"))); + .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( + "name_for_op_0")) + .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() + .filter(opInputDef -> opInputDef.getName().contains("integer")) + .forEach(opInputDef -> opInputDef.setInputId( + addedInterfaceType + ".name_for_op_1.output_integer_1"))); //Mapping to operation from another interface String secondInterfaceType = "org.test.lifecycle.standard.interfaceType.second"; InterfaceDefinition secondInterface = new InterfaceDefinition(); secondInterface.setType(secondInterfaceType); addOperationsToInterface(component, secondInterface, 2, 2, true, true); secondInterface.getOperationsMap().values().stream() - .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( - "name_for_op_0")) - .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() - .filter(opInputDef -> opInputDef.getName().contains("integer")) - .forEach(opInputDef -> opInputDef.setInputId( - addedInterfaceType +".name_for_op_1.output_integer_1"))); + .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( + "name_for_op_0")) + .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() + .filter(opInputDef -> opInputDef.getName().contains("integer")) + .forEach(opInputDef -> opInputDef.setInputId( + addedInterfaceType + ".name_for_op_1.output_integer_1"))); component.setInterfaces(new HashMap<>()); component.getInterfaces().put(addedInterfaceType, addedInterface); component.getInterfaces().put(secondInterfaceType, secondInterface); @@ -350,8 +348,8 @@ class InterfacesOperationsConverterTest { ToscaNodeType nodeType = new ToscaNodeType(); interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,null, - interfacesOperationsConverter); + ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, + interfacesOperationsConverter); ToscaTemplate template = new ToscaTemplate("test"); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put("test", nodeType); @@ -385,7 +383,7 @@ class InterfacesOperationsConverterTest { component.getInterfaces().put(interfaceName, aInterfaceWithInput); final ToscaNodeType nodeType = new ToscaNodeType(); interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - final ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null,null, + final ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, interfacesOperationsConverter); final ToscaTemplate template = new ToscaTemplate("testService"); final Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); @@ -393,7 +391,8 @@ class InterfacesOperationsConverterTest { template.setNode_types(nodeTypes); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); final String toscaTemplateYaml = new String(toscaRepresentation.getMainYaml()); - assertThat(toscaTemplateYaml, allOf(containsString(INPUTS.getElementName() + ":"), containsString(input1Name), containsString(interfaceName))); + assertThat(toscaTemplateYaml, + allOf(containsString(INPUTS.getElementName() + ":"), containsString(input1Name), containsString(interfaceName))); validateInterfaceInputs(toscaTemplateYaml, interfaceName, inputMap); } @@ -439,15 +438,15 @@ class InterfacesOperationsConverterTest { @FunctionalInterface interface MainYamlAssertion extends Function<String, Boolean> {} - private static Function<String, Boolean> all(MainYamlAssertion...fs) { + private static Function<String, Boolean> all(MainYamlAssertion... fs) { return s -> io.vavr.collection.List.of(fs).map(f -> f.apply(s)).fold(true, (l, r) -> l && r); } - private static MainYamlAssertion containsNone(String...expected) { + private static MainYamlAssertion containsNone(String... expected) { return s -> io.vavr.collection.List.of(expected).map(e -> !s.contains(e)).fold(true, (l, r) -> l && r); } - private static MainYamlAssertion containsAll(String...expected) { + private static MainYamlAssertion containsAll(String... expected) { return s -> io.vavr.collection.List.of(expected).map(s::contains).fold(true, (l, r) -> l && r); } @@ -467,7 +466,7 @@ class InterfacesOperationsConverterTest { } if (hasOutputs) { operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(), - operation.getName(), numOfInputsPerOp)); + operation.getName(), numOfInputsPerOp)); } addedInterface.getOperations().put(operation.getName(), operation); } @@ -496,7 +495,7 @@ class InterfacesOperationsConverterTest { for (int i = 0; i < numOfInputs; i++) { String mappedPropertyName = java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME + i; operationInputDefinitionList.add(createMockOperationInputDefinition( - INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i)); + INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i)); addMappedPropertyAsComponentInput(component, mappedPropertyName); } @@ -519,7 +518,7 @@ class InterfacesOperationsConverterTest { ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>(); for (int i = 0; i < numOfOutputs; i++) { operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName, - OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i)); + OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i)); } return operationOutputDefinitionList; } @@ -559,8 +558,8 @@ class InterfacesOperationsConverterTest { private void validateOperationInputs(final String mainYaml, int numOfInputsPerOp, String mappedOperationName) { String nodeTypeKey = NODE_TYPE_NAME + ":"; String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(), - mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length() - + String.valueOf(numOfInputsPerOp).length()); + mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length() + + String.valueOf(numOfInputsPerOp).length()); YamlToObjectConverter objectConverter = new YamlToObjectConverter(); ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class); Map<String, Object> interfaces = toscaNodeType.getInterfaces(); @@ -575,7 +574,7 @@ class InterfacesOperationsConverterTest { if (operationVal instanceof Map) { //Since the inputs are mapped to output operations from only first interface so using that name validateOperationInputDefinition(interfaces.keySet().iterator().next(), mappedOperationName, - operationVal); + operationVal); } } } @@ -588,7 +587,7 @@ class InterfacesOperationsConverterTest { String[] inputNameSplit = inputEntry.getKey().split("_"); Map<String, Object> inputValueObject = (Map<String, Object>) inputEntry.getValue(); validateOperationInputDefinitionDefaultValue(interfaceType, operationName, inputNameSplit[1], - Integer.parseInt(inputNameSplit[2]), inputValueObject); + Integer.parseInt(inputNameSplit[2]), inputValueObject); } } @@ -613,15 +612,15 @@ class InterfacesOperationsConverterTest { assertTrue(mappedPropertyDefaultValue.contains(operationName)); assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue)); } else { - fail("Invalid Tosca function in default value. Allowed values: "+ ToscaFunctions.GET_PROPERTY.getFunctionName() + - "/"+ ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); + fail("Invalid Tosca function in default value. Allowed values: " + ToscaFunctions.GET_PROPERTY.getFunctionName() + + "/" + ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); } } private void validateServiceProxyOperationInputs(String mainYaml) { String nodeTypeKey = NODE_TYPE_NAME + ":"; String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(), - mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length()); + mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length()); YamlUtil yamlUtil = new YamlUtil(); ToscaNodeType toscaNodeType = yamlUtil.yamlToObject(nodeTypesRepresentation, ToscaNodeType.class); for (Object interfaceVal : toscaNodeType.getInterfaces().values()) { @@ -650,10 +649,10 @@ class InterfacesOperationsConverterTest { service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>()))); Map<String, Object> resultMap = InterfacesOperationsConverter.addInterfaceTypeElement(service, - Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard")); + Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard")); assertTrue(MapUtils.isNotEmpty(resultMap) - && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface")); + && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface")); } @Test @@ -662,13 +661,13 @@ class InterfacesOperationsConverterTest { service.setComponentMetadataDefinition(new ServiceMetadataDefinition()); service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface"); service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null, - new HashMap<>()))); + new HashMap<>()))); Map<String, Object> resultMap = interfacesOperationsConverter.getInterfacesMap(service, null, - service.getInterfaces(), null, false, false); + service.getInterfaces(), null, false, false); assertTrue(MapUtils.isNotEmpty(resultMap) - && resultMap.containsKey("NotLocal")); + && resultMap.containsKey("NotLocal")); } @Test diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java index 1b38a245c4..55c2e8972f 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java @@ -40,6 +40,7 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum.VF; import static org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType.PROPERTY; import fj.data.Either; @@ -47,8 +48,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; @@ -66,9 +67,6 @@ import org.openecomp.sdc.be.components.BeConfDependentTest; import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; @@ -82,7 +80,6 @@ import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; -import org.openecomp.sdc.be.model.ComponentInstanceAttribOutput; import org.openecomp.sdc.be.model.ComponentInstanceAttribute; import org.openecomp.sdc.be.model.ComponentInstanceInput; import org.openecomp.sdc.be.model.ComponentInstanceProperty; @@ -92,7 +89,6 @@ import org.openecomp.sdc.be.model.GroupDefinition; import org.openecomp.sdc.be.model.GroupInstance; import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; -import org.openecomp.sdc.be.model.OutputDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.RelationshipInfo; import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; @@ -108,7 +104,6 @@ import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; import org.openecomp.sdc.be.tosca.model.SubstitutionMapping; import org.openecomp.sdc.be.tosca.model.ToscaCapability; -import org.openecomp.sdc.be.tosca.model.ToscaMetadata; import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; @@ -165,12 +160,15 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { @Mock private PolicyExportParser policyExportParser; + @Mock + private AttributeConverter attributeConverter; + @Before public void setUpMock() { MockitoAnnotations.initMocks(this); - doReturn(new ToscaProperty()).when(propertyConvertor).convertProperty(any(), any(), eq(PROPERTY)); + doReturn(new ToscaProperty()).when(propertyConvertor).convertProperty(any(), any(), eq(PROPERTY)); doReturn(new HashMap<String, Object>()).when(interfacesOperationsConverter) - .getInterfacesMap(any(), isNull(), anyMap(), anyMap(), anyBoolean(), anyBoolean()); + .getInterfacesMap(any(), isNull(), anyMap(), anyMap(), anyBoolean(), anyBoolean()); } private Resource getNewResource() { @@ -239,7 +237,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), - any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.left(Collections.emptyMap())); @@ -249,7 +247,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component = getNewService(); when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Service.class), - any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); // default test when component is Service @@ -273,7 +271,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), - any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // default test when convertInterfaceNodeType is left result = testSubject.exportComponentInterface(component, false); @@ -302,7 +300,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // when convertRequirements is called, make it return the same value as 3rd (index=2) argument. when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), - any(ToscaNodeType.class))).thenAnswer(i -> Either.left(i.getArgument(2))); + any(ToscaNodeType.class))).thenAnswer(i -> Either.left(i.getArgument(2))); Either<ToscaTemplate, ToscaError> result = (Either<ToscaTemplate, ToscaError>) Deencapsulation .invoke(testSubject, "convertInterfaceNodeType", new HashMap<String, Component>(), component, @@ -355,7 +353,6 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component.setToscaArtifacts(artifactList); ToscaTemplate toscaTemplate = new ToscaTemplate(""); - ComponentInstance ci = new ComponentInstance(); ci.setComponentUid("name"); ci.setOriginType(OriginTypeEnum.PNF); @@ -372,11 +369,11 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { verify(toscaOperationFacade, times(1)).getToscaFullElement("name"); Assert.assertTrue(result.isLeft()); ToscaTemplate toscaTemplateRes = result.left().value().left; - Assert.assertEquals(8 , toscaTemplateRes.getImports().size()); + Assert.assertEquals(8, toscaTemplateRes.getImports().size()); Assert.assertNotNull(toscaTemplateRes.getImports().get(6).get("resource-TestResourceName-interface")); Assert.assertNotNull(toscaTemplateRes.getImports().get(7).get("resource-TestResourceName")); - Assert.assertEquals(1 , toscaTemplateRes.getDependencies().size()); - Assert.assertEquals("name.name2",toscaTemplateRes.getDependencies().get(0).getLeft()); + Assert.assertEquals(1, toscaTemplateRes.getDependencies().size()); + Assert.assertEquals("name.name2", toscaTemplateRes.getDependencies().get(0).getLeft()); } @Test @@ -521,14 +518,14 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { when(toscaOperationFacade.getToscaFullElement(any(String.class))) .thenReturn(Either.left(component)); - + Resource baseType = getNewResource(); Map<String, ArtifactDefinition> baseTypeToscaArtifacts = new HashMap<>(); ArtifactDefinition baseTypeArtifact = new ArtifactDefinition(); baseTypeArtifact.setArtifactName("typeA"); baseTypeToscaArtifacts.put("assettoscatemplate", baseTypeArtifact); baseType.setToscaArtifacts(baseTypeToscaArtifacts); - + component.setDerivedFromGenericType("org.typeA"); component.setDerivedFromGenericVersion("1.0"); when(toscaOperationFacade.getByToscaResourceNameAndVersion("org.typeA", "1.0")).thenReturn(Either.left(baseType)); @@ -606,7 +603,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { .thenReturn(Either.left(Collections.emptyMap())); when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), - any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // default test result = Deencapsulation.invoke(testSubject, "convertInterfaceNodeType", new HashMap<>(), component, toscaNode @@ -629,7 +626,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { .thenReturn(new HashMap<>()); when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), - any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // default test result = Deencapsulation @@ -640,7 +637,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component = new Service(); when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Service.class), - any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // test when component is service result = Deencapsulation @@ -650,125 +647,212 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { } @Test - public void testConvertNodeTemplates() throws Exception { - Component component = getNewResource(); - List<ComponentInstance> componentInstances = new ArrayList<>(); - Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>(); - Map<String, Component> componentCache = new HashMap<>(); - Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); - ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate(); - Either<Map<String, ToscaNodeTemplate>, ToscaError> result; - Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>(); - List<ComponentInstanceInput> inputs = new ArrayList<>(); - inputs.add(new ComponentInstanceInput()); - componentInstancesInputs.put("key", inputs); - List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>(); - RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef(); + public void testConvertNodeTemplatesWhenComponentIsService() throws Exception { + final Component component = getNewService(); + final List<ComponentInstance> componentInstances = new ArrayList<>(); + final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>(); + final Map<String, Component> componentCache = new HashMap<>(); + final Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); + final ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate(); + final Either<Map<String, ToscaNodeTemplate>, ToscaError> result; + final Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>(); + final List<ComponentInstanceInput> inputs = new ArrayList<>(); + final ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput(); + componentInstanceInput.setUniqueId("uuid"); + inputs.add(componentInstanceInput); + componentInstancesInputs.put("uuid", inputs); + final List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>(); + final RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef(); reldef.setFromNode("node"); resourceInstancesRelations.add(reldef); component.setComponentInstancesRelations(resourceInstancesRelations); - ComponentInstance instance = new ComponentInstance(); + final ComponentInstance instance = new ComponentInstance(); instance.setUniqueId("id"); instance.setComponentUid("uid"); instance.setOriginType(OriginTypeEnum.ServiceProxy); - List<GroupInstance> groupInstances = new ArrayList<>(); - GroupInstance groupInst = new GroupInstance(); - List<String> artifacts = new ArrayList<>(); + final List<GroupInstance> groupInstances = new ArrayList<>(); + final GroupInstance groupInst = new GroupInstance(); + final List<String> artifacts = new ArrayList<>(); artifacts.add("artifact"); groupInst.setArtifacts(artifacts); groupInst.setType("type"); groupInstances.add(groupInst); instance.setGroupInstances(groupInstances); + + final List<PropertyDefinition> properties = new ArrayList<>(); + properties.add(new PropertyDefinition()); + instance.setProperties(properties); + + instance.setUniqueId("uuid"); + instance.setDescription("desc"); + instance.setSourceModelUid("sourceModelUid"); + componentInstances.add(instance); + component.setComponentInstances(componentInstances); + component.setComponentInstancesInputs(componentInstancesInputs); component.setInvariantUUID("uuid"); component.setUUID("uuid"); component.setDescription("desc"); + component.setUniqueId("uid"); componentCache.put("uid", component); - componentInstancesProperties.put("id", new ArrayList<>()); - componentInstancesInputs.put("id", new ArrayList<>()); + final List<ComponentInstanceProperty> componentInstanceProperties = new ArrayList<>(); + componentInstanceProperties.add(new ComponentInstanceProperty()); + + componentInstancesProperties.put("uuid", componentInstanceProperties); + component.setComponentInstancesProperties(componentInstancesProperties); - when(capabilityRequirementConverter.getOriginComponent(any(Map.class), - any(ComponentInstance.class))).thenReturn(Either.left(component)); + final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = new HashMap<>(); + final List<ComponentInstanceAttribute> componentInstanceAttributes = new ArrayList<>(); + final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute(); + componentInstanceAttribute.setDefaultValue("def value"); + componentInstanceAttributes.add(componentInstanceAttribute); - when(capabilityRequirementConverter.convertComponentInstanceCapabilities( - any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) + componentInstancesAttributes.put("uuid", componentInstanceAttributes); + component.setComponentInstancesAttributes(componentInstancesAttributes); + + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); + when(capabilityRequirementConverter + .convertComponentInstanceCapabilities(any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) .thenReturn(Either.left(new ToscaNodeTemplate())); + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), any(ToscaNodeType.class))) + .thenReturn(Either.left(new ToscaNodeType())); + when(toscaOperationFacade.getToscaFullElement("uid")).thenReturn(Either.left(component)); + when(toscaOperationFacade.getToscaFullElement("sourceModelUid")).thenReturn(Either.left(component)); + when(toscaOperationFacade.getLatestByName("serviceProxy")).thenReturn(Either.left(new Resource())); + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(new Resource())); + + final Map<String, String[]> substitutionMappingMap = new HashMap<>(); + final String[] array = {"value1", "value2"}; + substitutionMappingMap.put("key", array); + when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(anyMap(), any(Component.class))) + .thenReturn(Either.left(substitutionMappingMap)); + + when(capabilityRequirementConverter + .convertSubstitutionMappingRequirements(any(Map.class), any(Component.class), any(SubstitutionMapping.class))) + .thenReturn(Either.left(new SubstitutionMapping())); // default test - result = Deencapsulation.invoke(testSubject, "convertNodeTemplates", component, componentInstances, - componentInstancesProperties, new HashMap<>(), componentCache, dataTypes, topologyTemplate); - Assert.assertNotNull(result); + final Either<ToscaRepresentation, ToscaError> toscaRepresentationToscaErrorEither = testSubject.exportComponent(component); + assertNotNull(toscaRepresentationToscaErrorEither); + } @Test - public void testConvertNodeTemplatesWhenComponentIsService() throws Exception { - Component component = getNewService(); - List<ComponentInstance> componentInstances = new ArrayList<>(); - Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>(); - Map<String, List<ComponentInstanceProperty>> componentInstancesInterfaces = new HashMap<>(); - Map<String, Component> componentCache = new HashMap<>(); - Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); - ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate(); - Either<Map<String, ToscaNodeTemplate>, ToscaError> result; - Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>(); - List<ComponentInstanceInput> inputs = new ArrayList<>(); - inputs.add(new ComponentInstanceInput()); - componentInstancesInputs.put("key", inputs); - List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>(); - RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef(); + public void testConvertNodeTemplatesWhenComponentIsResource() throws Exception { + final Resource component = getNewResource(); + component.setResourceType(VF); + final List<ComponentInstance> componentInstances = new ArrayList<>(); + final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>(); + final Map<String, Component> componentCache = new HashMap<>(); + final Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); + final ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate(); + final Either<Map<String, ToscaNodeTemplate>, ToscaError> result; + final Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>(); + final List<ComponentInstanceInput> inputs = new ArrayList<>(); + final ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput(); + componentInstanceInput.setUniqueId("uuid"); + inputs.add(componentInstanceInput); + componentInstancesInputs.put("uuid", inputs); + final List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>(); + final RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef(); reldef.setFromNode("node"); resourceInstancesRelations.add(reldef); component.setComponentInstancesRelations(resourceInstancesRelations); - ComponentInstance instance = new ComponentInstance(); + final ComponentInstance instance = new ComponentInstance(); instance.setUniqueId("id"); instance.setComponentUid("uid"); instance.setOriginType(OriginTypeEnum.ServiceProxy); - List<GroupInstance> groupInstances = new ArrayList<>(); - GroupInstance groupInst = new GroupInstance(); - List<String> artifacts = new ArrayList<>(); + final List<GroupInstance> groupInstances = new ArrayList<>(); + final GroupInstance groupInst = new GroupInstance(); + final List<String> artifacts = new ArrayList<>(); artifacts.add("artifact"); groupInst.setArtifacts(artifacts); groupInst.setType("type"); groupInstances.add(groupInst); instance.setGroupInstances(groupInstances); + + final List<PropertyDefinition> properties = new ArrayList<>(); + properties.add(new PropertyDefinition()); + instance.setProperties(properties); + + instance.setUniqueId("uuid"); + instance.setDescription("desc"); + instance.setSourceModelUid("sourceModelUid"); + final Map<String, ArtifactDefinition> artifactList = new HashMap<>(); + final ArtifactDefinition artifact = new ArtifactDefinition(); + artifact.setArtifactName("name.name2"); + artifactList.put("assettoscatemplate", artifact); + instance.setArtifacts(artifactList); + + Map<String, ToscaArtifactDataDefinition> toscaArtifactDataDefinitionMap = new HashMap<>(); + toscaArtifactDataDefinitionMap.put("assettoscatemplate", new ToscaArtifactDataDefinition()); + instance.setToscaArtifacts(toscaArtifactDataDefinitionMap); + componentInstances.add(instance); + component.setComponentInstances(componentInstances); + component.setComponentInstancesInputs(componentInstancesInputs); component.setInvariantUUID("uuid"); component.setUUID("uuid"); component.setDescription("desc"); + component.setUniqueId("uid"); + + componentCache.put("uid", component); - Map<String, ForwardingPathDataDefinition> forwardingPaths = new HashMap<>(); - ForwardingPathDataDefinition path = new ForwardingPathDataDefinition(); - ListDataDefinition<ForwardingPathElementDataDefinition> list = new ListDataDefinition<>(); - path.setPathElements(list); - forwardingPaths.put("key", path); + final List<ComponentInstanceProperty> componentInstanceProperties = new ArrayList<>(); + componentInstanceProperties.add(new ComponentInstanceProperty()); - ((Service) component).setForwardingPaths(forwardingPaths); + componentInstancesProperties.put("uuid", componentInstanceProperties); + component.setComponentInstancesProperties(componentInstancesProperties); - componentCache.put("uid", component); + final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = new HashMap<>(); + final List<ComponentInstanceAttribute> componentInstanceAttributes = new ArrayList<>(); + final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute(); + componentInstanceAttribute.setDefaultValue("def value"); + componentInstanceAttributes.add(componentInstanceAttribute); - componentInstancesProperties.put("id", new ArrayList<>()); - componentInstancesInterfaces.put("id", new ArrayList<>()); - componentInstancesInputs.put("id", new ArrayList<>()); + componentInstancesAttributes.put("uuid", componentInstanceAttributes); + component.setComponentInstancesAttributes(componentInstancesAttributes); - when(capabilityRequirementConverter.getOriginComponent(any(Map.class), - any(ComponentInstance.class))).thenReturn(Either.left(component)); + component.setArtifacts(artifactList); + component.setToscaArtifacts(artifactList); - when(capabilityRequirementConverter.convertComponentInstanceCapabilities( - any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); + when(capabilityRequirementConverter + .convertComponentInstanceCapabilities(any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) .thenReturn(Either.left(new ToscaNodeTemplate())); + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), any(ToscaNodeType.class))) + .thenReturn(Either.left(new ToscaNodeType())); + when(toscaOperationFacade.getToscaFullElement("uid")).thenReturn(Either.left(component)); + when(toscaOperationFacade.getToscaFullElement("sourceModelUid")).thenReturn(Either.left(component)); + when(toscaOperationFacade.getLatestByName("serviceProxy")).thenReturn(Either.left(new Resource())); + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(new Resource())); + + final Map<String, String[]> substitutionMappingMap = new HashMap<>(); + final String[] array = {"value1", "value2"}; + substitutionMappingMap.put("key", array); + when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(anyMap(), any(Component.class))) + .thenReturn(Either.left(substitutionMappingMap)); + + when(capabilityRequirementConverter + .convertSubstitutionMappingRequirements(any(Map.class), any(Component.class), any(SubstitutionMapping.class))) + .thenReturn(Either.left(new SubstitutionMapping())); // default test - result = Deencapsulation.invoke(testSubject, "convertNodeTemplates", component, componentInstances, - componentInstancesProperties, componentInstancesInterfaces, componentCache, dataTypes, topologyTemplate); - Assert.assertNotNull(result); + final Either<ToscaRepresentation, ToscaError> toscaRepresentationToscaErrorEither = testSubject.exportComponent(component); + assertNotNull(toscaRepresentationToscaErrorEither); + } @Test @@ -796,6 +880,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { instance.setComponentUid("uid"); instance.setOriginType(OriginTypeEnum.ServiceProxy); componentInstances.add(instance); + component.setComponentInstances(componentInstances); component.setComponentInstancesInputs(componentInstancesInputs); component.setInvariantUUID("uuid"); @@ -804,17 +889,18 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentCache.put("uid", component); - when(capabilityRequirementConverter.getOriginComponent(any(Map.class), - any(ComponentInstance.class))).thenReturn(Either.left(component)); - - when(capabilityRequirementConverter.convertComponentInstanceCapabilities( - any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); + when(capabilityRequirementConverter + .convertComponentInstanceCapabilities(any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) .thenReturn(Either.right(ToscaError.GENERAL_ERROR)); + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // default test - result = Deencapsulation.invoke(testSubject, "convertNodeTemplates", component, componentInstances, - componentInstancesProperties, componentInstancesInterfaces, componentCache, dataTypes, topologyTemplate); - Assert.assertNotNull(result); + final Either<ToscaRepresentation, ToscaError> toscaRepresentationToscaErrorEither = testSubject.exportComponent(component); + assertNotNull(toscaRepresentationToscaErrorEither); } @Test @@ -842,6 +928,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { instance.setComponentUid("uid"); instance.setOriginType(OriginTypeEnum.ServiceProxy); componentInstances.add(instance); + component.setComponentInstances(componentInstances); component.setComponentInstancesInputs(componentInstancesInputs); component.setInvariantUUID("uuid"); @@ -850,25 +937,27 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentCache.put("uid", component); - when(capabilityRequirementConverter.getOriginComponent(any(Map.class), - any(ComponentInstance.class))).thenReturn(Either.right(false)); + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.right(false)); + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // default test - result = Deencapsulation.invoke(testSubject, "convertNodeTemplates", component, componentInstances, - componentInstancesProperties, componentInstancesInterfaces, componentCache, dataTypes, topologyTemplate); - Assert.assertNotNull(result); + final Either<ToscaRepresentation, ToscaError> toscaRepresentationToscaErrorEither = testSubject.exportComponent(component); + assertNotNull(toscaRepresentationToscaErrorEither); } @Test public void testConvertNodeTemplatesWhenConvertComponentInstanceRequirmentsIsRight() { - Component component = new Resource(); + Resource component = getNewResource(); + component.setResourceType(VF); List<ComponentInstance> componentInstances = new ArrayList<>(); Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>(); Map<String, List<ComponentInstanceProperty>> componentInstancesInterfaces = new HashMap<>(); Map<String, Component> componentCache = new HashMap<>(); Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate(); - Either<Map<String, ToscaNodeTemplate>, ToscaError> result; Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>(); List<ComponentInstanceInput> inputs = new ArrayList<>(); inputs.add(new ComponentInstanceInput()); @@ -885,20 +974,35 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { resourceInstancesRelations.add(reldef); component.setComponentInstancesRelations(resourceInstancesRelations); + Map<String, ArtifactDefinition> artifactList = new HashMap<>(); + ArtifactDefinition artifact = new ArtifactDefinition(); + artifact.setArtifactName("name.name2"); + artifactList.put("assettoscatemplate", artifact); + component.setArtifacts(artifactList); + component.setToscaArtifacts(artifactList); + ComponentInstance instance = new ComponentInstance(); instance.setUniqueId("id"); + instance.setComponentUid("id"); + instance.setOriginType(OriginTypeEnum.VF); componentInstances.add(instance); + component.setComponentInstances(componentInstances); component.setComponentInstancesInputs(componentInstancesInputs); component.setComponentInstances(componentInstances); - when(capabilityRequirementConverter.getOriginComponent(any(Map.class), - any(ComponentInstance.class))).thenReturn(Either.left(component)); + doReturn(Either.left(component)).when(toscaOperationFacade).getToscaFullElement("id"); + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), + any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) + .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); // default test - result = Deencapsulation.invoke(testSubject, "convertNodeTemplates", component, componentInstances, - componentInstancesProperties, componentInstancesInterfaces, componentCache, dataTypes, topologyTemplate); - Assert.assertNotNull(result); + final Either<ToscaRepresentation, ToscaError> result = testSubject.exportComponent(component); + assertNotNull(result); } @Test @@ -1233,7 +1337,6 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { .format("Failed to find a requirement with uniqueId %s on a component with uniqueId %s", relation.getRequirementUid(), fromOriginComponent.getUniqueId()); - assertThrows(ToscaExportException.class, () -> Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, instancesList, relationshipDefinition, new HashMap<>()), expectedError); @@ -1265,7 +1368,6 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) .thenReturn(Either.left(toOriginComponent)); - requirementDefinition.setCapability(capabilityName); relation.setCapability("wrong"); final String requirementPreviousName = "requirementPreviousName"; @@ -1609,7 +1711,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { public void testGetProxyNodeTypeInterfacesNoInterfaces() { Component service = new Service(); Optional<Map<String, Object>> proxyNodeTypeInterfaces = - testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); + testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); Assert.assertFalse(proxyNodeTypeInterfaces.isPresent()); } @@ -1617,7 +1719,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { public void testGetProxyNodeTypeInterfaces() { Component service = getTestComponent(); Optional<Map<String, Object>> proxyNodeTypeInterfaces = - testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); + testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); Assert.assertTrue(proxyNodeTypeInterfaces.isPresent()); Map<String, Object> componentInterfaces = proxyNodeTypeInterfaces.get(); Assert.assertNotNull(componentInterfaces); @@ -1627,7 +1729,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { @Test public void testGetProxyNodeTypePropertiesComponentNull() { Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties = - testSubject.getProxyNodeTypeProperties(null, DATA_TYPES); + testSubject.getProxyNodeTypeProperties(null, DATA_TYPES); Assert.assertFalse(proxyNodeTypeProperties.isPresent()); } @@ -1635,7 +1737,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { public void testGetProxyNodeTypePropertiesNoProperties() { Component service = new Service(); Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties = - testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); Assert.assertFalse(proxyNodeTypeProperties.isPresent()); } @@ -1643,9 +1745,9 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { public void testGetProxyNodeTypeProperties() { Component service = getTestComponent(); service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), - createMockProperty("componentPropInt", null))); + createMockProperty("componentPropInt", null))); Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties = - testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); Assert.assertTrue(proxyNodeTypeProperties.isPresent()); Map<String, ToscaProperty> componentProperties = proxyNodeTypeProperties.get(); Assert.assertNotNull(componentProperties); @@ -1656,9 +1758,9 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { public void testAddInputsToPropertiesNoInputs() { Component service = getTestComponent(); service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), - createMockProperty("componentPropInt", null))); + createMockProperty("componentPropInt", null))); Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult = - testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); @@ -1673,11 +1775,11 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { public void testAddInputsToPropertiesWithInputs() { Component service = getTestComponent(); service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), - createMockProperty("componentPropInt", null))); + createMockProperty("componentPropInt", null))); service.setInputs(Arrays.asList(createMockInput("componentInputStr1", - "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); + "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult = - testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); @@ -1690,9 +1792,9 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { public void testAddInputsToPropertiesOnlyInputs() { Component service = getTestComponent(); service.setInputs(Arrays.asList(createMockInput("componentInputStr1", - "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); + "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult = - testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); @@ -1705,7 +1807,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { public void testOperationImplementationInProxyNodeTypeNotPresent() { Component service = getTestComponent(); InterfaceDefinition interfaceDefinition = - service.getInterfaces().get("normalizedServiceComponentName-interface"); + service.getInterfaces().get("normalizedServiceComponentName-interface"); interfaceDefinition.setOperations(new HashMap<>()); final OperationDataDefinition operation = new OperationDataDefinition(); operation.setName("start"); @@ -1716,9 +1818,9 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { interfaceDefinition.getOperations().put(operation.getName(), operation); service.getInterfaces().put("normalizedServiceComponentName-interface", interfaceDefinition); service.setInputs(Arrays.asList(createMockInput("componentInputStr1", - "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); + "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); Optional<Map<String, Object>> proxyNodeTypeInterfaces = - testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); + testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); Assert.assertTrue(proxyNodeTypeInterfaces.isPresent()); Map<String, Object> componentInterfaces = proxyNodeTypeInterfaces.get(); Assert.assertNotNull(componentInterfaces); @@ -1735,7 +1837,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { return component; } - private PropertyDefinition createMockProperty(String propertyName, String defaultValue){ + private PropertyDefinition createMockProperty(String propertyName, String defaultValue) { PropertyDefinition propertyDefinition = new PropertyDefinition(); propertyDefinition.setName(propertyName); propertyDefinition.setType("string"); @@ -1743,7 +1845,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { return propertyDefinition; } - private InputDefinition createMockInput(String inputName, String defaultValue){ + private InputDefinition createMockInput(String inputName, String defaultValue) { InputDefinition inputDefinition = new InputDefinition(); inputDefinition.setName(inputName); inputDefinition.setType("string"); |