X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Ftosca%2FToscaExportHandler.java;h=976842136fae8a78c530af56e37741ee109e218f;hb=8fea9935b3dd4a020bcfdd7cea3125b8f90edad0;hp=127d6f67af19a302c73ac0138de33d1727622046;hpb=0da3e7d52c83b54b126dd52f23da3bb94e152fba;p=sdc.git 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 127d6f67af..976842136f 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 @@ -1,28 +1,47 @@ /*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * +* ============LICENSE_START======================================================= +* SDC +* ================================================================================ +* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +* ================================================================================ +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* * http://www.apache.org/licenses/LICENSE-2.0 - * +* * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ package org.openecomp.sdc.be.tosca; +import static org.apache.commons.collections.CollectionUtils.isEmpty; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +import static org.apache.commons.collections.MapUtils.isNotEmpty; +import static org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import fj.data.Either; +import java.beans.IntrospectionException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.function.Supplier; +import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; @@ -32,25 +51,61 @@ 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.ConfigurationManager; -import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.*; +import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.CapabilityDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceInput; +import org.openecomp.sdc.be.model.ComponentInstanceInterface; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.DataTypeDefinition; +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.PropertyDefinition; +import org.openecomp.sdc.be.model.RelationshipInfo; +import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; +import org.openecomp.sdc.be.model.RequirementDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.category.CategoryDefinition; -import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; -import org.openecomp.sdc.be.model.tosca.ToscaFunctions; import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter; -import org.openecomp.sdc.be.tosca.model.*; +import org.openecomp.sdc.be.tosca.model.CapabilityFilter; +import org.openecomp.sdc.be.tosca.model.NodeFilter; +import org.openecomp.sdc.be.tosca.model.SubstitutionMapping; +import org.openecomp.sdc.be.tosca.model.ToscaCapability; +import org.openecomp.sdc.be.tosca.model.ToscaDataType; +import org.openecomp.sdc.be.tosca.model.ToscaGroupTemplate; +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.ToscaPolicyTemplate; +import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.model.ToscaRequirement; +import org.openecomp.sdc.be.tosca.model.ToscaTemplate; +import org.openecomp.sdc.be.tosca.model.ToscaTemplateArtifact; +import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement; +import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate; import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil; import org.openecomp.sdc.be.tosca.utils.InputConverter; -import org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.externalupload.utils.ServiceUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -67,18 +122,6 @@ import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Represent; import org.yaml.snakeyaml.representer.Representer; -import java.beans.IntrospectionException; -import java.util.*; -import java.util.Map.Entry; -import java.util.function.Supplier; -import java.util.stream.Collectors; - -import static org.apache.commons.collections.CollectionUtils.isEmpty; -import static org.apache.commons.collections.CollectionUtils.isNotEmpty; -import static org.apache.commons.collections.MapUtils.isNotEmpty; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceDefinitionElement; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceTypeElement; - @org.springframework.stereotype.Component("tosca-export-handler") public class ToscaExportHandler { @@ -90,20 +133,24 @@ public class ToscaExportHandler { private PropertyConvertor propertyConvertor; private InputConverter inputConverter; private InterfaceLifecycleOperation interfaceLifecycleOperation; + private InterfacesOperationsConverter interfacesOperationsConverter; @Autowired public ToscaExportHandler(ApplicationDataTypeCache dataTypeCache, ToscaOperationFacade toscaOperationFacade, CapabilityRequirementConverter capabilityRequirementConverter, PolicyExportParser policyExportParser, - GroupExportParser groupExportParser, InputConverter inputConverter, InterfaceLifecycleOperation interfaceLifecycleOperation) { - this.dataTypeCache = dataTypeCache; - this.toscaOperationFacade = toscaOperationFacade; - this.capabilityRequirementConverter = capabilityRequirementConverter; - this.policyExportParser = policyExportParser; - this.groupExportParser = groupExportParser; - this.propertyConvertor = PropertyConvertor.getInstance(); - this.inputConverter = inputConverter; - this.interfaceLifecycleOperation = interfaceLifecycleOperation; - } + GroupExportParser groupExportParser, PropertyConvertor propertyConvertor, InputConverter inputConverter, + InterfaceLifecycleOperation interfaceLifecycleOperation, + InterfacesOperationsConverter interfacesOperationsConverter) { + this.dataTypeCache = dataTypeCache; + this.toscaOperationFacade = toscaOperationFacade; + this.capabilityRequirementConverter = capabilityRequirementConverter; + this.policyExportParser = policyExportParser; + this.groupExportParser = groupExportParser; + this.propertyConvertor = propertyConvertor; + this.inputConverter = inputConverter; + this.interfaceLifecycleOperation = interfaceLifecycleOperation; + this.interfacesOperationsConverter = interfacesOperationsConverter; + } private static final Logger log = Logger.getLogger(ToscaExportHandler.class); @@ -223,7 +270,8 @@ public class ToscaExportHandler { } toscaNode = importsRes.left().value().left; Map componentCache = importsRes.left().value().right; - Either, ToscaError> nodeTypesMapEither = createProxyNodeTypes(componentCache , component ); + Either, ToscaError> nodeTypesMapEither = createProxyNodeTypes(componentCache, + component); if (nodeTypesMapEither.isRight()) { log.debug("Failed to fetch normative service proxy resource by tosca name, error {}", nodeTypesMapEither.right().value()); @@ -234,8 +282,18 @@ public class ToscaExportHandler { toscaNode.setNode_types(nodeTypesMap); } + Either, ToscaError> proxyInterfaceTypesEither = createProxyInterfaceTypes(component); + if (proxyInterfaceTypesEither.isRight()) { + log.debug("Failed to populate service proxy local interface types in tosca, error {}", + nodeTypesMapEither.right().value()); + return Either.right(proxyInterfaceTypesEither.right().value()); + } + Map proxyInterfaceTypes = proxyInterfaceTypesEither.left().value(); + if (MapUtils.isNotEmpty(proxyInterfaceTypes)) { + toscaNode.setInterface_types(proxyInterfaceTypes); + } - Either, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll(); + Either, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll(); if (dataTypesEither.isRight()) { log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value()); return Either.right(ToscaError.GENERAL_ERROR); @@ -282,17 +340,17 @@ public class ToscaExportHandler { SubstitutionMapping substitutionMapping = new SubstitutionMapping(); String toscaResourceName; switch (component.getComponentType()) { - case RESOURCE: - toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition() - .getMetadataDataDefinition()).getToscaResourceName(); - break; - case SERVICE: - toscaResourceName = SERVICE_NODE_TYPE_PREFIX - + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName(); - break; - default: - log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType()); - return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE); + case RESOURCE: + toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition() + .getMetadataDataDefinition()).getToscaResourceName(); + break; + case SERVICE: + toscaResourceName = SERVICE_NODE_TYPE_PREFIX + + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName(); + break; + default: + log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType()); + return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE); } substitutionMapping.setNode_type(toscaResourceName); @@ -303,7 +361,7 @@ public class ToscaExportHandler { substitutionMapping = capabilities.left().value(); Either requirements = capabilityRequirementConverter - .convertSubstitutionMappingRequirements(componentCache, component, substitutionMapping); + .convertSubstitutionMappingRequirements(componentCache, component, substitutionMapping); if (requirements.isRight()) { return Either.right(requirements.right().value()); } @@ -317,21 +375,19 @@ public class ToscaExportHandler { } private void addGroupsToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) { - - Map groups = groupExportParser.getGroups(component); if(groups!= null) { topologyTemplate.addGroups(groups); } } - private void addPoliciesToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) - throws SdcResourceNotFoundException { - Map policies = policyExportParser.getPolicies(component); - if(policies!= null) { - topologyTemplate.addPolicies(policies); - } - } + private void addPoliciesToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) + throws SdcResourceNotFoundException { + Map policies = policyExportParser.getPolicies(component); + if(policies!= null) { + topologyTemplate.addPolicies(policies); + } + } private ToscaMetadata convertMetadata(Component component) { return convertMetadata(component, false, null); @@ -353,7 +409,7 @@ public class ToscaExportHandler { toscaMetadata.setVersion(component.getVersion()); toscaMetadata.setCustomizationUUID(componentInstance.getCustomizationUUID()); if (componentInstance.getSourceModelInvariant() != null - && !componentInstance.getSourceModelInvariant().isEmpty()) { + && !componentInstance.getSourceModelInvariant().isEmpty()) { toscaMetadata.setVersion(componentInstance.getComponentVersion()); toscaMetadata.setSourceModelInvariant(componentInstance.getSourceModelInvariant()); toscaMetadata.setSourceModelUuid(componentInstance.getSourceModelUuid()); @@ -365,35 +421,36 @@ public class ToscaExportHandler { } switch (component.getComponentType()) { - case RESOURCE: - Resource resource = (Resource) component; + case RESOURCE: + Resource resource = (Resource) component; - if (isInstance && componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) { - toscaMetadata.setType(componentInstance.getOriginType().getDisplayValue()); - } else { - toscaMetadata.setType(resource.getResourceType().name()); - } - toscaMetadata.setSubcategory(categoryDefinition.getSubcategories().get(0).getName()); - toscaMetadata.setResourceVendor(resource.getVendorName()); - toscaMetadata.setResourceVendorRelease(resource.getVendorRelease()); - toscaMetadata.setResourceVendorModelNumber(resource.getResourceVendorModelNumber()); - break; - case SERVICE: - Service service = (Service) component; - toscaMetadata.setType(component.getComponentType().getValue()); - toscaMetadata.setServiceType(service.getServiceType()); - toscaMetadata.setServiceRole(service.getServiceRole()); - toscaMetadata.setEnvironmentContext(service.getEnvironmentContext()); - resolveInstantiationTypeAndSetItToToscaMetaData(toscaMetadata, service); - if (!isInstance) { - // DE268546 - toscaMetadata.setServiceEcompNaming(((Service) component).isEcompGeneratedNaming()); - toscaMetadata.setEcompGeneratedNaming(((Service) component).isEcompGeneratedNaming()); - toscaMetadata.setNamingPolicy(((Service) component).getNamingPolicy()); - } - break; - default: - log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType()); + if (isInstance && componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) { + toscaMetadata.setType(componentInstance.getOriginType().getDisplayValue()); + } else { + toscaMetadata.setType(resource.getResourceType().name()); + } + toscaMetadata.setSubcategory(categoryDefinition.getSubcategories().get(0).getName()); + toscaMetadata.setResourceVendor(resource.getVendorName()); + toscaMetadata.setResourceVendorRelease(resource.getVendorRelease()); + toscaMetadata.setResourceVendorModelNumber(resource.getResourceVendorModelNumber()); + break; + case SERVICE: + Service service = (Service) component; + toscaMetadata.setType(component.getComponentType().getValue()); + toscaMetadata.setServiceType(service.getServiceType()); + toscaMetadata.setServiceRole(service.getServiceRole()); + toscaMetadata.setServiceFunction(service.getServiceFunction()); + toscaMetadata.setEnvironmentContext(service.getEnvironmentContext()); + resolveInstantiationTypeAndSetItToToscaMetaData(toscaMetadata, service); + if (!isInstance) { + // DE268546 + toscaMetadata.setServiceEcompNaming(((Service) component).isEcompGeneratedNaming()); + toscaMetadata.setEcompGeneratedNaming(((Service) component).isEcompGeneratedNaming()); + toscaMetadata.setNamingPolicy(((Service) component).getNamingPolicy()); + } + break; + default: + log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType()); } return toscaMetadata; } @@ -458,7 +515,7 @@ public class ToscaExportHandler { if (componentRI == null) { // all resource must be only once! Either resource = toscaOperationFacade - .getToscaFullElement(ci.getComponentUid()); + .getToscaFullElement(ci.getComponentUid()); if ((resource.isRight()) && (log.isDebugEnabled())) { log.debug("Failed to fetch resource with id {} for instance {}",ci.getComponentUid() ,ci.getUniqueId()); return ; @@ -469,7 +526,7 @@ public class ToscaExportHandler { if (ci.getOriginType() == OriginTypeEnum.ServiceProxy){ Either sourceService = toscaOperationFacade - .getToscaFullElement(ci.getSourceModelUid()); + .getToscaFullElement(ci.getSourceModelUid()); if (sourceService.isRight() && (log.isDebugEnabled())) { log.debug("Failed to fetch source service with id {} for proxy {}", ci.getSourceModelUid(), ci.getUniqueId()); } @@ -515,8 +572,8 @@ public class ToscaExportHandler { } private Either convertInterfaceNodeType(Map componentsCache, - Component component, ToscaTemplate toscaNode, - Map nodeTypes, + Component component, ToscaTemplate toscaNode, + Map nodeTypes, boolean isAssociatedComponent) { log.debug("start convert node type for {}", component.getUniqueId()); ToscaNodeType toscaNodeType = createNodeType(component); @@ -528,13 +585,13 @@ public class ToscaExportHandler { return Either.right(ToscaError.GENERAL_ERROR); } List allGlobalInterfaceTypes = lifecycleTypeEither.left().value() - .values() - .stream() + .values() + .stream() .map(InterfaceDataDefinition::getType) - .collect(Collectors.toList()); + .collect(Collectors.toList()); toscaNode.setInterface_types(addInterfaceTypeElement(component, allGlobalInterfaceTypes)); - Either, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll(); + Either, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll(); if (dataTypesEither.isRight()) { log.debug("Failed to fetch all data types :", dataTypesEither.right().value()); return Either.right(ToscaError.GENERAL_ERROR); @@ -544,23 +601,20 @@ public class ToscaExportHandler { List inputDef = component.getInputs(); Map mergedProperties = new HashMap<>(); - addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); - if (inputDef != null) { - addInputsToProperties(dataTypes, inputDef, mergedProperties); - } + interfacesOperationsConverter.addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); + addInputsToProperties(dataTypes, inputDef, mergedProperties); if(CollectionUtils.isNotEmpty(component.getProperties())) { List properties = component.getProperties(); - Map convertedProperties; - convertedProperties = properties.stream().collect(Collectors.toMap( - PropertyDataDefinition::getName, - property -> propertyConvertor.convertProperty(dataTypes, property, - PropertyConvertor.PropertyType.PROPERTY))); + Map convertedProperties = properties.stream() + .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition, component.getInputs())) + .collect(Collectors.toMap(PropertyDataDefinition::getName, + property -> propertyConvertor.convertProperty(dataTypes, property, + PropertyConvertor.PropertyType.PROPERTY))); // merge component properties and inputs properties mergedProperties.putAll(convertedProperties); } - if (MapUtils.isNotEmpty(mergedProperties) && Objects.nonNull(inputDef)) { - resolveDefaultPropertyValue(inputDef, mergedProperties, dataTypes); + if (MapUtils.isNotEmpty(mergedProperties)) { toscaNodeType.setProperties(mergedProperties); } @@ -591,50 +645,6 @@ public class ToscaExportHandler { return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes); } - private void resolveDefaultPropertyValue(List inputDef, - Map mergedProperties, - Map dataTypes) { - for (Map.Entry mergedPropertyEntry : mergedProperties.entrySet()) { - ToscaProperty value = mergedPropertyEntry.getValue(); - if (Objects.nonNull(value) && value.getDefaultp() instanceof Map) { - Map valueAsMap = (Map) value.getDefaultp(); - Object getInputValue = valueAsMap.get(ToscaFunctions.GET_INPUT.getFunctionName()); - if (getInputValue instanceof String) { - String inputName = (String)getInputValue; - Optional matchedInputDefinition = inputDef.stream() - .filter(componentInput -> componentInput.getName().equals(inputName)) - .findFirst(); - if (matchedInputDefinition.isPresent()) { - InputDefinition matchedInput = matchedInputDefinition.get(); - Object resolvedDefaultValue = new PropertyConvertor().convertToToscaObject(matchedInput.getType(), - matchedInput.getDefaultValue(), matchedInput.getSchemaType(), dataTypes, false); - value.setDefaultp(resolvedDefaultValue); - mergedProperties.put(mergedPropertyEntry.getKey(), value); - } - } else if (getInputValue instanceof List) { - // new get_input syntax to refer to sub-element (introduced from TOSCA v1.3) - // e.g. get_input: [input_name, INDEX, inner_property] - // currently resolving default value for the syntax is not supported - log.debug("#resolveDefaultPropertyValue: ignore get_input list syntax. propname={}, val={}", - mergedPropertyEntry.getKey(), getInputValue); - } else { - // Ignore unknown get_input syntax - log.debug("#resolveDefaultPropertyValue: ignore unknown get_input syntax. propname={}, val={}", - mergedPropertyEntry.getKey(), getInputValue); - } - } - } - } - - private void addInputsToProperties(Map dataTypes, - List inputDef, - Map mergedProperties) { - for(InputDefinition input : inputDef) { - ToscaProperty property = propertyConvertor.convertProperty(dataTypes, input, PropertyConvertor.PropertyType.INPUT); - mergedProperties.put(input.getName(), property); - } - } - private Either convertReqCapAndTypeName(Map componentsCache, Component component, ToscaTemplate toscaNode, Map nodeTypes, ToscaNodeType toscaNodeType, Map dataTypes) { @@ -655,17 +665,17 @@ public class ToscaExportHandler { String toscaResourceName; switch (component.getComponentType()) { - case RESOURCE: - toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition() - .getMetadataDataDefinition()).getToscaResourceName(); - break; - case SERVICE: - toscaResourceName = SERVICE_NODE_TYPE_PREFIX - + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName(); - break; - default: - log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType()); - return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE); + case RESOURCE: + toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition() + .getMetadataDataDefinition()).getToscaResourceName(); + break; + case SERVICE: + toscaResourceName = SERVICE_NODE_TYPE_PREFIX + + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName(); + break; + default: + log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType()); + return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE); } nodeTypes.put(toscaResourceName, toscaNodeType); @@ -691,12 +701,15 @@ public class ToscaExportHandler { Map groupsMap = null; for (ComponentInstance componentInstance : componentInstances) { ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate(); + if (MapUtils.isNotEmpty(componentInstance.getToscaArtifacts())) { + nodeTemplate.setArtifacts(convertToNodeTemplateArtifacts(componentInstance.getToscaArtifacts())); + } nodeTemplate.setType(componentInstance.getToscaComponentName()); nodeTemplate.setDirectives(componentInstance.getDirectives()); nodeTemplate.setNode_filter(convertToNodeTemplateNodeFilterComponent(componentInstance.getNodeFilter())); Either originComponentRes = capabilityRequirementConverter - .getOriginComponent(componentCache, componentInstance); + .getOriginComponent(componentCache, componentInstance); if (originComponentRes.isRight()) { convertNodeTemplatesRes = Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR); break; @@ -723,7 +736,7 @@ public class ToscaExportHandler { } Either capabilities = capabilityRequirementConverter - .convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate); + .convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate); if (capabilities.isRight()) { convertNodeTemplatesRes = Either.right(capabilities.right().value()); break; @@ -738,14 +751,14 @@ public class ToscaExportHandler { addPropertiesOfParentComponent(dataTypes, originalComponent, props); } - if (null != componentInstancesProperties && componentInstancesProperties.containsKey(instanceUniqueId) - && !isComponentOfTypeServiceProxy(componentInstance)) { - addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes, - instanceUniqueId, props); + if (null != componentInstancesProperties && componentInstancesProperties.containsKey(instanceUniqueId)) { + addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes, instanceUniqueId, + props); } 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); } @@ -819,7 +832,7 @@ public class ToscaExportHandler { currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface .getUniqueId(), instInterface)); - interfaces = InterfacesOperationsToscaUtil + interfaces = interfacesOperationsConverter .getInterfacesMap(parentComponent, componentInstance, tmpInterfaces, dataTypes, true, true); } } else { @@ -862,8 +875,8 @@ public class ToscaExportHandler { } private void addComponentInstanceInputs(Map dataTypes, - Map> componentInstancesInputs, - String instanceUniqueId, Map props) { + Map> componentInstancesInputs, + String instanceUniqueId, Map props) { List instanceInputsList = componentInstancesInputs.get(instanceUniqueId); if (instanceInputsList != null) { @@ -871,7 +884,7 @@ public class ToscaExportHandler { Supplier supplier = () -> input.getValue() != null && !Objects.isNull(input.getValue()) ? input.getValue() : input.getDefaultValue(); - propertyConvertor.convertAndAddValue(dataTypes, props, input, supplier); + propertyConvertor.convertAndAddValue(dataTypes, props, input, supplier); }); } } @@ -883,14 +896,14 @@ public class ToscaExportHandler { if (isNotEmpty(componentInstancesProperties)) { componentInstancesProperties.get(instanceUniqueId) - // Converts and adds each value to property map - .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop, - prop::getValue)); + // Converts and adds each value to property map + .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop, + prop::getValue)); } } private void addPropertiesOfParentComponent(Map dataTypes, - Component componentOfInstance, Map props) { + Component componentOfInstance, Map props) { List componentProperties = componentOfInstance.getProperties(); if (isNotEmpty(componentProperties)) { @@ -899,7 +912,7 @@ public class ToscaExportHandler { .filter(prop -> StringUtils.isNotEmpty(prop.getDefaultValue())) // Converts and adds each value to property map .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop, - prop::getDefaultValue)); + prop::getDefaultValue)); } } @@ -927,8 +940,7 @@ public class ToscaExportHandler { if (input.getSchema() != null && input.getSchema().getProperty() != null) { innerType = input.getSchema().getProperty().getType(); } - return propertyConvertor.convertToToscaObject(propertyType, supplier.get(), innerType, - dataTypes, true); + return propertyConvertor.convertToToscaObject(input, supplier.get(), dataTypes, true); } private ToscaNodeType createNodeType(Component component) { @@ -940,13 +952,65 @@ public class ToscaExportHandler { toscaNodeType.setDescription(component.getDescription()); } else { String derivedFrom = null != component.getDerivedFromGenericType() ? component.getDerivedFromGenericType() - : "tosca.nodes.Root"; + : "tosca.nodes.Root"; toscaNodeType.setDerived_from(derivedFrom); } return toscaNodeType; } - private Either, ToscaError> createProxyNodeTypes(Map componentCache ,Component container ) { + private Either, ToscaError> createProxyInterfaceTypes(Component container) { + + Map proxyInterfaceTypes = new HashMap<>(); + Either, ToscaError> res = Either.left(proxyInterfaceTypes); + List componentInstances = container.getComponentInstances(); + if (CollectionUtils.isEmpty(componentInstances)) { + return res; + } + Map serviceProxyInstanceList = new HashMap<>(); + componentInstances.stream() + .filter(this::isComponentOfTypeServiceProxy) + .forEach(inst -> serviceProxyInstanceList.put(inst.getToscaComponentName(), inst)); + if (MapUtils.isEmpty(serviceProxyInstanceList)) { + return res; + } + for (Entry entryProxy : serviceProxyInstanceList.entrySet()) { + Component serviceComponent; + ComponentParametersView componentParametersView = new ComponentParametersView(); + componentParametersView.disableAll(); + componentParametersView.setIgnoreInterfaces(false); + Either service = toscaOperationFacade + .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView); + if (service.isRight()) { + log.debug("Failed to fetch original service component with id {} for instance {}", + entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName()); + return Either.right(ToscaError.GENERAL_ERROR); + } else { + serviceComponent = service.left().value(); + } + + Either, StorageOperationStatus> lifecycleTypeEither = + interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(); + if(lifecycleTypeEither.isRight()){ + log.debug("Failed to retrieve global interface types :", lifecycleTypeEither.right().value()); + return Either.right(ToscaError.GENERAL_ERROR); + } + + List allGlobalInterfaceTypes = lifecycleTypeEither.left().value().values().stream() + .map(InterfaceDataDefinition::getType) + .collect(Collectors.toList()); + //Add interface types for local interfaces in the original service component for proxy + Map localInterfaceTypes = addInterfaceTypeElement(serviceComponent, + allGlobalInterfaceTypes); + if (MapUtils.isNotEmpty(localInterfaceTypes)) { + proxyInterfaceTypes.putAll(localInterfaceTypes); + } + + } + return Either.left(proxyInterfaceTypes); + } + + private Either, ToscaError> createProxyNodeTypes(Map componentCache, + Component container) { Map nodeTypesMap = new HashMap<>(); Either, ToscaError> res = Either.left(nodeTypesMap); @@ -958,8 +1022,8 @@ public class ToscaExportHandler { } Map serviceProxyInstanceList = new HashMap<>(); List proxyInst = componentInstances.stream() - .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceProxy.name())) - .collect(Collectors.toList()); + .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceProxy.name())) + .collect(Collectors.toList()); if (proxyInst != null && !proxyInst.isEmpty()) { for (ComponentInstance inst : proxyInst) { serviceProxyInstanceList.put(inst.getToscaComponentName(), inst); @@ -969,11 +1033,8 @@ public class ToscaExportHandler { if (serviceProxyInstanceList.isEmpty()) { return res; } - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreCapabilities(false); - filter.setIgnoreComponentInstances(false); Either serviceProxyOrigin = toscaOperationFacade - .getLatestByName("serviceProxy"); + .getLatestByName("serviceProxy"); if (serviceProxyOrigin.isRight()) { log.debug("Failed to fetch normative service proxy resource by tosca name, error {}", serviceProxyOrigin.right().value()); @@ -986,36 +1047,53 @@ public class ToscaExportHandler { ComponentParametersView componentParametersView = new ComponentParametersView(); componentParametersView.disableAll(); componentParametersView.setIgnoreCategories(false); + componentParametersView.setIgnoreProperties(false); + componentParametersView.setIgnoreInputs(false); + componentParametersView.setIgnoreInterfaces(false); + componentParametersView.setIgnoreRequirements(false); Either service = toscaOperationFacade - .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView); + .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView); if (service.isRight()) { log.debug("Failed to fetch resource with id {} for instance {}", entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName()); } else { serviceComponent = service.left().value(); } - ToscaNodeType toscaNodeType = createProxyNodeType(componentCache , origComponent, serviceComponent, entryProxy.getValue()); + ToscaNodeType toscaNodeType = createProxyNodeType(componentCache, origComponent, serviceComponent, + entryProxy.getValue()); nodeTypesMap.put(entryProxy.getKey(), toscaNodeType); } return Either.left(nodeTypesMap); } - private ToscaNodeType createProxyNodeType(Map componentCache , Component origComponent, Component proxyComponent, - ComponentInstance instance) { + private ToscaNodeType createProxyNodeType(Map componentCache , Component origComponent, + Component proxyComponent, ComponentInstance instance) { ToscaNodeType toscaNodeType = new ToscaNodeType(); String derivedFrom = ((Resource) origComponent).getToscaResourceName(); toscaNodeType.setDerived_from(derivedFrom); - Either, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll(); + Either, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll(); if (dataTypesEither.isRight()) { log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value()); } Map dataTypes = dataTypesEither.left().value(); Map capabilities = this.capabilityRequirementConverter - .convertProxyCapabilities( componentCache ,origComponent, proxyComponent, instance, dataTypes); + .convertProxyCapabilities(componentCache, instance, dataTypes); - toscaNodeType.setCapabilities(capabilities); + if (MapUtils.isNotEmpty(capabilities)) { + toscaNodeType.setCapabilities(capabilities); + } + List> proxyNodeTypeRequirements = this.capabilityRequirementConverter + .convertProxyRequirements(componentCache, instance); + if (CollectionUtils.isNotEmpty(proxyNodeTypeRequirements)) { + toscaNodeType.setRequirements(proxyNodeTypeRequirements); + } + Optional> proxyProperties = getProxyNodeTypeProperties(proxyComponent, dataTypes); + proxyProperties.ifPresent(toscaNodeType::setProperties); + + Optional> proxyInterfaces = getProxyNodeTypeInterfaces(proxyComponent, dataTypes); + proxyInterfaces.ifPresent(toscaNodeType::setInterfaces); return toscaNodeType; } @@ -1041,10 +1119,10 @@ public class ToscaExportHandler { List relations, Component originComponent, List> toscaRequirements, Map componentCache) { List filteredRelations = relations.stream() - .filter(p -> componentInstance.getUniqueId().equals(p.getFromNode())).collect(Collectors.toList()); + .filter(p -> componentInstance.getUniqueId().equals(p.getFromNode())).collect(Collectors.toList()); return isEmpty(filteredRelations) || - filteredRelations.stream() - .allMatch(rel -> addRequirement(componentInstance, originComponent, component.getComponentInstances(), rel, toscaRequirements, componentCache)); + filteredRelations.stream() + .allMatch(rel -> addRequirement(componentInstance, originComponent, component.getComponentInstances(), rel, toscaRequirements, componentCache)); } private boolean addRequirement(ComponentInstance fromInstance, Component fromOriginComponent, @@ -1060,7 +1138,7 @@ public class ToscaExportHandler { Optional capOpt = Optional.empty(); ComponentInstance toInstance = instancesList.stream().filter(i -> rel.getToNode().equals(i.getUniqueId())) - .findFirst().orElse(null); + .findFirst().orElse(null); if (toInstance == null) { log.debug("Failed to find a relation from the node {} to the node {}", fromInstance.getName(), rel.getToNode()); @@ -1089,13 +1167,13 @@ public class ToscaExportHandler { if (result) { toOriginComponent = getOriginRes.left().value(); capOpt = toOriginComponent.getCapabilities().get(reqOpt.get().getCapability()).stream() - .filter(c -> isCapabilityBelongToRelation(reqAndRelationshipPair, c)).findFirst(); + .filter(c -> isCapabilityBelongToRelation(reqAndRelationshipPair, c)).findFirst(); if (!capOpt.isPresent()) { capOpt = findCapability(reqAndRelationshipPair, toOriginComponent, fromOriginComponent, reqOpt.get()); if(!capOpt.isPresent()){ - result = false; - log.debug("Failed to find a capability with name {} on a component with uniqueId {}", - reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId()); + result = false; + log.debug("Failed to find a capability with name {} on a component with uniqueId {}", + reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId()); } } } @@ -1173,16 +1251,18 @@ public class ToscaExportHandler { return false; } return ModelConverter.isAtomicComponent(originComponent) || - isRequirementBelongToOwner(reqAndRelationshipPair, requirement, fromInstanceId, originComponent); + isRequirementBelongToOwner(reqAndRelationshipPair, requirement, fromInstanceId, originComponent); } private boolean isRequirementBelongToOwner(RelationshipInfo reqAndRelationshipPair, RequirementDefinition requirement, String fromInstanceId, Component originComponent) { - return StringUtils.equals(requirement.getOwnerId(), reqAndRelationshipPair.getRequirementOwnerId()) || (isCvfc(originComponent) && StringUtils.equals(fromInstanceId, reqAndRelationshipPair.getRequirementOwnerId())); + return StringUtils.equals(requirement.getOwnerId(), reqAndRelationshipPair.getRequirementOwnerId()) + || (isCvfc(originComponent) && StringUtils.equals(fromInstanceId, reqAndRelationshipPair.getRequirementOwnerId()) + || StringUtils.equals(requirement.getOwnerId(), originComponent.getUniqueId())); } private boolean isCvfc(Component component) { return component.getComponentType() == ComponentTypeEnum.RESOURCE && - ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC; + ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC; } private Either convertCapabilities(Component component, @@ -1190,7 +1270,7 @@ public class ToscaExportHandler { Either result = Either.left(substitutionMappings); Either, ToscaError> toscaCapabilitiesRes = capabilityRequirementConverter - .convertSubstitutionMappingCapabilities(componentCache, component); + .convertSubstitutionMappingCapabilities(componentCache, component); if (toscaCapabilitiesRes.isRight()) { result = Either.right(toscaCapabilitiesRes.right().value()); log.debug("Failed convert capabilities for the component {}. ", component.getName()); @@ -1214,6 +1294,19 @@ public class ToscaExportHandler { return Either.left(nodeType); } + private Map convertToNodeTemplateArtifacts(Map artifacts) { + if (artifacts == null) { + return null; + } + Map arts = new HashMap<>(); + for (Map.Entry entry : artifacts.entrySet()) { + ToscaTemplateArtifact artifact = new ToscaTemplateArtifact(); + artifact.setFile(entry.getValue().getFile()); + artifact.setType(entry.getValue().getType()); + arts.put(entry.getKey(), artifact); + } + return arts; + } protected NodeFilter convertToNodeTemplateNodeFilterComponent(CINodeFilterDataDefinition inNodeFilter) { if (inNodeFilter == null){ @@ -1289,29 +1382,47 @@ public class ToscaExportHandler { origProperties.isEmpty()) { return; } + Map> propertyMapCopy = new HashMap<>(); for(RequirementNodeFilterPropertyDataDefinition propertyDataDefinition : origProperties.getListToscaDataDefinition()) { - Map> propertyMapCopy = new HashMap<>(); for(String propertyInfoEntry : propertyDataDefinition.getConstraints()) { Map propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class); - if (propertyMapCopy.containsKey(propertyDataDefinition.getName())){ - propertyMapCopy.get(propertyDataDefinition.getName()).add(propertyValObj); + String propertyName = propertyDataDefinition.getName(); + if (propertyMapCopy.containsKey(propertyName)){ + addPropertyConstraintValueToList(propertyName, propertyValObj, propertyMapCopy.get(propertyName)); } else { - if (propertyDataDefinition.getName() != null) { - List propsList =new ArrayList(); - propsList.add(propertyValObj); - propertyMapCopy.put(propertyDataDefinition.getName(), propsList); + if (propertyName != null) { + List propsList = new ArrayList(); + addPropertyConstraintValueToList(propertyName, propertyValObj, propsList); + propertyMapCopy.put(propertyName, propsList); } else { propertyMapCopy.putAll(propertyValObj); } } } - propertiesCopy.add(propertyMapCopy); } + propertyMapCopy.entrySet().stream().forEach(entry -> + addCalculatedConstraintsIntoPropertiesList(propertiesCopy, entry)); + } + + private void addPropertyConstraintValueToList(String propertyName, Map propertyValObj, List propsList) { + if(propertyValObj.containsKey(propertyName)) { + propsList.add(propertyValObj.get(propertyName)); + } else { + propsList.add(propertyValObj); + } + } + + + private void addCalculatedConstraintsIntoPropertiesList(List>> propertiesCopy, + Entry> entry) { + Map> tempMap = new HashMap<>(); + tempMap.put(entry.getKey(), entry.getValue()); + propertiesCopy.add(tempMap); } private static class CustomRepresenter extends Representer { - public CustomRepresenter() { + CustomRepresenter() { super(); // null representer is exceptional and it is stored as an instance // variable. @@ -1324,16 +1435,15 @@ public class ToscaExportHandler { Tag customTag) { if (propertyValue == null) { return null; - } else { - // skip not relevant for Tosca property - if ("dependencies".equals(property.getName())) { - return null; - } - NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); - - return "_defaultp_".equals(property.getName()) - ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode; } + // skip not relevant for Tosca property + if ("dependencies".equals(property.getName())) { + return null; + } + NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + + return "_defaultp_".equals(property.getName()) + ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode; } @Override @@ -1381,5 +1491,61 @@ public class ToscaExportHandler { return interfaceObject; } + + Optional> getProxyNodeTypeProperties(Component proxyComponent, + Map + dataTypes) { + if (Objects.isNull(proxyComponent)) { + return Optional.empty(); + } + Map proxyProperties = new HashMap<>(); + addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyProperties); + if (CollectionUtils.isNotEmpty(proxyComponent.getProperties())) { + proxyProperties.putAll(proxyComponent.getProperties().stream() + .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition, + proxyComponent.getInputs())) + .collect(Collectors.toMap(PropertyDataDefinition::getName, + property -> propertyConvertor.convertProperty(dataTypes, property, + PropertyConvertor.PropertyType.PROPERTY)))); + } + return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty(); + } + + void addInputsToProperties(Map dataTypes, + List componentInputs, + Map mergedProperties) { + if (CollectionUtils.isEmpty(componentInputs)) { + return; + } + for(InputDefinition input : componentInputs) { + ToscaProperty property = propertyConvertor.convertProperty(dataTypes, input, + PropertyConvertor.PropertyType.INPUT); + mergedProperties.put(input.getName(), property); + } + } + + Optional> getProxyNodeTypeInterfaces(Component proxyComponent, + Map dataTypes) { + if (Objects.isNull(proxyComponent) || MapUtils.isEmpty(proxyComponent.getInterfaces())) { + return Optional.empty(); + } + Map proxyComponentInterfaces = proxyComponent.getInterfaces(); + //Unset artifact path for operation implementation for proxy node types as for operations with artifacts it is + // always available in the proxy node template + removeOperationImplementationForProxyNodeType(proxyComponentInterfaces); + return Optional.ofNullable(interfacesOperationsConverter + .getInterfacesMap(proxyComponent, null, proxyComponentInterfaces, dataTypes, + false, false)); + } + + private static void removeOperationImplementationForProxyNodeType( + Map proxyComponentInterfaces) { + if (MapUtils.isEmpty(proxyComponentInterfaces)) { + return; + } + proxyComponentInterfaces.values().stream().map(InterfaceDataDefinition::getOperations) + .filter(MapUtils::isNotEmpty) + .forEach(operations -> operations.values().forEach(operation -> operation.setImplementation(null))); + } }