From 8fea9935b3dd4a020bcfdd7cea3125b8f90edad0 Mon Sep 17 00:00:00 2001 From: talig Date: Thu, 13 Feb 2020 09:02:47 +0200 Subject: Fix PropertyConvertor initialization (use spring) refactor static InterfacesOperationsToscaUtil to be non-static InterfacesOperationsConverter (as it uses PropertyConvertor), move logic from static ToscaExportUtils to ToscaExportHandler (as it uses PropertyConvertor) Change-Id: I917351ca59ee1e792c7b0850d6a8b246b71b4b03 Issue-ID: SDC-2765 Signed-off-by: talig --- .../impl/InterfaceOperationBusinessLogic.java | 2 +- .../be/components/impl/ServiceBusinessLogic.java | 2 +- .../PropertyDataValueMergeBusinessLogic.java | 11 +- .../components/utils/InterfaceOperationUtils.java | 6 +- .../sdc/be/servlets/ServiceConsumptionServlet.java | 2 +- .../be/tosca/CapabilityRequirementConverter.java | 33 ++- .../sdc/be/tosca/GroupExportParserImpl.java | 5 +- .../be/tosca/InterfacesOperationsConverter.java | 324 +++++++++++++++++++++ .../sdc/be/tosca/PolicyExportParserImpl.java | 5 +- .../openecomp/sdc/be/tosca/PropertyConvertor.java | 60 +--- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 136 ++++++--- .../sdc/be/tosca/utils/InputConverter.java | 9 +- .../tosca/utils/InterfacesOperationsToscaUtil.java | 317 -------------------- .../sdc/be/tosca/utils/ToscaExportUtils.java | 101 ------- 14 files changed, 475 insertions(+), 538 deletions(-) create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java delete mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java delete mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java (limited to 'catalog-be/src/main/java/org') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java index a0ea5c9d04..b6d64d2fc7 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java @@ -75,7 +75,7 @@ import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getO import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToComponentInput; import static org.openecomp.sdc.be.components.utils.PropertiesUtils.getPropertyCapabilityFromAllCapProps; import static org.openecomp.sdc.be.components.utils.PropertiesUtils.isCapabilityProperty; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.SELF; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; @Component("interfaceOperationBusinessLogic") public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java index c1f7808958..ba04c00cdb 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java @@ -167,7 +167,7 @@ import static org.openecomp.sdc.be.components.utils.ConsumptionUtils.isAssignedV import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getOperationOutputName; import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToOtherOperationOutput; import static org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum.UPDATE_SERVICE_METADATA; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.SELF; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; import static org.openecomp.sdc.be.types.ServiceConsumptionSource.SERVICE_INPUT; import static org.openecomp.sdc.be.types.ServiceConsumptionSource.STATIC; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java index c02eb2820d..b4621498a8 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java @@ -30,6 +30,7 @@ import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.tosca.ToscaFunctions; import org.openecomp.sdc.be.tosca.PropertyConvertor; import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; @@ -47,13 +48,15 @@ public class PropertyDataValueMergeBusinessLogic { private final PropertyValueMerger propertyValueMerger; private final ApplicationDataTypeCache dataTypeCache; - private final PropertyConvertor propertyConvertor = PropertyConvertor.getInstance(); + private final PropertyConvertor propertyConvertor; private final Gson gson = new Gson(); - - public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache dataTypeCache) { + @Autowired + public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, + ApplicationDataTypeCache dataTypeCache, PropertyConvertor propertyConvertor) { this.propertyValueMerger = propertyValueMerger; this.dataTypeCache = dataTypeCache; + this.propertyConvertor = propertyConvertor; } /** @@ -94,7 +97,7 @@ public class PropertyDataValueMergeBusinessLogic { String propValue = propertyDataDefinition.getValue() == null ? "": propertyDataDefinition.getValue(); String propertyType = propertyDataDefinition.getType(); String innerType = propertyDataDefinition.getSchemaType(); - return propertyConvertor.convertToToscaObject(propertyType, propValue, innerType, dataTypes, true); + return propertyConvertor.convertToToscaObject(propertyDataDefinition, propValue, dataTypes, true); } protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java index 4c891f8da9..9c82a291f9 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java @@ -28,7 +28,7 @@ import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.tosca.ToscaFunctions; -import org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil; +import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter; import java.util.ArrayList; import java.util.Arrays; @@ -116,7 +116,7 @@ public class InterfaceOperationUtils { public static Map> createMappedInputPropertyDefaultValue(String propertyName) { Map> getPropertyMap = new HashMap<>(); List values = new ArrayList<>(); - values.add(InterfacesOperationsToscaUtil.SELF); + values.add(InterfacesOperationsConverter.SELF); if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) { values.addAll(Arrays.asList(propertyName.split("\\."))); } @@ -128,7 +128,7 @@ public class InterfaceOperationUtils { String propertyName) { Map> getPropertyMap = new HashMap<>(); List values = new ArrayList<>(); - values.add(InterfacesOperationsToscaUtil.SELF); + values.add(InterfacesOperationsConverter.SELF); values.add(capabilityName); if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceConsumptionServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceConsumptionServlet.java index d438784122..a8d1eb64c4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceConsumptionServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceConsumptionServlet.java @@ -75,7 +75,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.SELF; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; @Loggable(prepend = true, value = Loggable.DEBUG, trim = false) @Path("/v1/catalog") diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverter.java index 81380301f8..9a55fb885c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverter.java @@ -20,9 +20,22 @@ package org.openecomp.sdc.be.tosca; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +import static org.apache.commons.lang3.StringUtils.isBlank; +import static org.apache.commons.lang3.StringUtils.isNoneBlank; + import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import fj.data.Either; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -55,20 +68,6 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.function.Function; -import java.util.stream.Collectors; - -import static org.apache.commons.collections.CollectionUtils.isNotEmpty; -import static org.apache.commons.lang3.StringUtils.isBlank; -import static org.apache.commons.lang3.StringUtils.isNoneBlank; - /** * Allows to convert requirements\capabilities of a component to requirements\capabilities of a substitution mappings section of a tosca template * @@ -86,6 +85,8 @@ public class CapabilityRequirementConverter { @Autowired private ToscaOperationFacade toscaOperationFacade; + @Autowired + private PropertyConvertor propertyConvertor; public CapabilityRequirementConverter() {} @@ -182,7 +183,7 @@ public class CapabilityRequirementConverter { innerType = prop.getSchema().getProperty().getType(); } String propValue = prop.getValue() == null ? prop.getDefaultValue() : prop.getValue(); - return PropertyConvertor.getInstance().convertToToscaObject(propertyType, propValue, innerType, dataTypes, false); + return propertyConvertor.convertToToscaObject(prop, propValue, dataTypes, false); } /** * Allows to convert requirements of a node type to tosca template requirements representation @@ -573,7 +574,7 @@ public class CapabilityRequirementConverter { if (isNotEmpty(properties)) { Map toscaProperties = new HashMap<>(); for (PropertyDefinition property : properties) { - ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.CAPABILITY); + ToscaProperty toscaProperty = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.CAPABILITY); toscaProperties.put(property.getName(), toscaProperty); } toscaCapability.setProperties(toscaProperties); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/GroupExportParserImpl.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/GroupExportParserImpl.java index 0b6d5d87a6..981c444197 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/GroupExportParserImpl.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/GroupExportParserImpl.java @@ -69,11 +69,12 @@ public class GroupExportParserImpl implements GroupExportParser { private Map dataTypes; private ApplicationDataTypeCache dataTypeCache; - private PropertyConvertor propertyConvertor = PropertyConvertor.getInstance(); + private final PropertyConvertor propertyConvertor; @Autowired - public GroupExportParserImpl(ApplicationDataTypeCache dataTypeCache) { + public GroupExportParserImpl(ApplicationDataTypeCache dataTypeCache, PropertyConvertor propertyConvertor) { this.dataTypeCache = dataTypeCache; + this.propertyConvertor = propertyConvertor; this.dataTypes = getDataTypes(); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java new file mode 100644 index 0000000000..0e5c55e087 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java @@ -0,0 +1,324 @@ +/* + * Copyright © 2016-2020 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.be.tosca; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import org.apache.commons.collections.MapUtils; +import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.Product; +import org.openecomp.sdc.be.model.tosca.ToscaFunctions; +import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; +import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType; +import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition; +import org.openecomp.sdc.be.tosca.model.ToscaNodeType; +import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +@Service +public class InterfacesOperationsConverter { + + + private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard"; + private static final String DERIVED_FROM_BASE_DEFAULT = "org.openecomp.interfaces.node.lifecycle."; + private static final String OPERATIONS_KEY = "operations"; + + private static final String DEFAULT = "default"; + private static final String DEFAULT_HAS_UNDERSCORE = "_default"; + private static final String DOT = "."; + private static final String DEFAULTP = "defaultp"; + + public static final String SELF = "SELF"; + private static final String LOCAL_INTERFACE_TYPE = "Local"; + + private PropertyConvertor propertyConvertor; + + @Autowired + public InterfacesOperationsConverter(PropertyConvertor propertyConvertor) { + this.propertyConvertor = propertyConvertor; + } + + /** + * Creates the interface_types element. + * + * @param component to work on + * @return the added element + */ + public static Map addInterfaceTypeElement(Component component, List allInterfaceTypes) { + if (component instanceof Product) { + return null; + } + final Map interfaces = component.getInterfaces(); + if (MapUtils.isEmpty(interfaces)) { + return null; + } + + Map toscaInterfaceTypes = new HashMap<>(); + for (InterfaceDefinition interfaceDefinition : interfaces.values()) { + boolean isInterfaceTypeExistInGlobalType = + allInterfaceTypes.stream().anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType())); + if (!isInterfaceTypeExistInGlobalType) { + ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType(); + toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE); + + final Map operations = interfaceDefinition.getOperations(); + Map toscaOperations = new HashMap<>(); + + for (Map.Entry operationEntry : operations.entrySet()) { + toscaOperations.put(operationEntry.getValue().getName(), null); + } + toscaInterfaceType.setOperations(toscaOperations); + Map interfacesAsMap = getObjectAsMap(toscaInterfaceType); + Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS_KEY); + interfacesAsMap.putAll(operationsMap); + + toscaInterfaceTypes.put(getInterfaceType(component, LOCAL_INTERFACE_TYPE), interfacesAsMap); + } + } + return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null; + } + + /** + * Adds the 'interfaces' element to the node type provided. + * + * @param component to work on + * @param nodeType to which the interfaces element will be added + */ + public void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType, + Map dataTypes, + boolean isAssociatedComponent) { + if (component instanceof Product) { + return; + } + final Map interfaces = component.getInterfaces(); + if (MapUtils.isEmpty(interfaces)) { + return; + } + Map toscaInterfaceDefinitions = getInterfacesMap(component, dataTypes, + isAssociatedComponent); + if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) { + nodeType.setInterfaces(toscaInterfaceDefinitions); + } + } + + private Map getInterfacesMap(Component component, + Map dataTypes, + boolean isAssociatedComponent) { + return getInterfacesMap(component, null, component.getInterfaces(), dataTypes, isAssociatedComponent, false); + } + + public Map getInterfacesMap(Component component, + ComponentInstance componentInstance, + Map interfaces, + Map dataTypes, + boolean isAssociatedComponent, + boolean isServiceProxyInterface) { + if(MapUtils.isEmpty(interfaces)) { + return null; + } + + Map toscaInterfaceDefinitions = new HashMap<>(); + for (InterfaceDefinition interfaceDefinition : interfaces.values()) { + ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition(); + String interfaceType; + if(componentInstance != null && LOCAL_INTERFACE_TYPE.equals(interfaceDefinition.getType())) { + interfaceType = DERIVED_FROM_BASE_DEFAULT + componentInstance.getSourceModelName(); + } else { + interfaceType = getInterfaceType(component, interfaceDefinition.getType()); + } + toscaInterfaceDefinition.setType(interfaceType); + final Map operations = interfaceDefinition.getOperations(); + Map toscaOperations = new HashMap<>(); + + String operationArtifactPath; + for (Map.Entry operationEntry : operations.entrySet()) { + ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition(); + if (isArtifactPresent(operationEntry)) { + operationArtifactPath = OperationArtifactUtil + .createOperationArtifactPath(component, componentInstance, operationEntry.getValue(), + isAssociatedComponent); + toscaOperation.setImplementation(operationArtifactPath); + } + toscaOperation.setDescription(operationEntry.getValue().getDescription()); + fillToscaOperationInputs(operationEntry.getValue(), dataTypes, toscaOperation, isServiceProxyInterface); + + toscaOperations.put(operationEntry.getValue().getName(), toscaOperation); + } + + toscaInterfaceDefinition.setOperations(toscaOperations); + Map interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition); + Map operationsMap = (Map) interfaceDefAsMap.remove(OPERATIONS_KEY); + if (isServiceProxyInterface) { + //Remove input type and copy default value directly into the proxy node template from the node type + handleServiceProxyOperationInputValue(operationsMap, interfaceType); + } else { + handleDefaults(operationsMap); + } + interfaceDefAsMap.putAll(operationsMap); + toscaInterfaceDefinitions.put(getLastPartOfName(interfaceType), interfaceDefAsMap); + } + + return toscaInterfaceDefinitions; + } + + private static void handleServiceProxyOperationInputValue(Map operationsMap, String parentKey) { + for (Map.Entry operationEntry : operationsMap.entrySet()) { + final Object value = operationEntry.getValue(); + final String key = operationEntry.getKey(); + if (value instanceof Map) { + if ("inputs".equals(parentKey)) { + Object defaultValue = getDefaultValue((Map) value); + operationsMap.put(key, defaultValue); + } else { + handleServiceProxyOperationInputValue((Map) value, key); + } + } + } + } + + private static Object getDefaultValue(Map inputValueMap) { + Object defaultValue = null; + for (Map.Entry operationEntry : inputValueMap.entrySet()) { + final Object value = operationEntry.getValue(); + if (value instanceof Map) { + getDefaultValue((Map) value); + } + final String key = operationEntry.getKey(); + if (key.equals(DEFAULTP)) { + defaultValue = inputValueMap.remove(key); + } + } + return defaultValue; + } + + /* + * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in + * ToscaExportHandler so, any string Map key named "defaultp" will have its named changed to "default" + * @param operationsMap the map to update + */ + private static void handleDefaults(Map operationsMap) { + for (Map.Entry operationEntry : operationsMap.entrySet()) { + final Object value = operationEntry.getValue(); + if (value instanceof Map) { + handleDefaults((Map) value); + } + final String key = operationEntry.getKey(); + if (key.equals(DEFAULTP)) { + Object removed = operationsMap.remove(key); + operationsMap.put(DEFAULT, removed); + } + } + } + + private static String getLastPartOfName(String toscaResourceName) { + return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1); + } + + private static boolean isArtifactPresent(Map.Entry operationEntry) { + final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation()); + if (isImplementationPresent) { + return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName()); + } + return false; + } + + private void fillToscaOperationInputs(OperationDataDefinition operation, + Map dataTypes, + ToscaLifecycleOperationDefinition toscaOperation, + boolean isServiceProxyInterface) { + if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) { + toscaOperation.setInputs(null); + return; + } + Map toscaInputs = new HashMap<>(); + + for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) { + ToscaProperty toscaInput = new ToscaProperty(); + toscaInput.setDescription(input.getDescription()); + toscaInput.setType(input.getType()); + toscaInput.setRequired(input.isRequired()); + if (isServiceProxyInterface) { + String inputValue = Objects.nonNull(input.getValue()) ? getInputValue(input.getValue()) : + getInputValue(input.getToscaDefaultValue()); + toscaInput.setDefaultp(propertyConvertor.convertToToscaObject(input, inputValue, dataTypes, false)); + } else { + toscaInput.setDefaultp(propertyConvertor + .convertToToscaObject(input, getInputValue(input.getToscaDefaultValue()), + dataTypes, false)); + } + toscaInputs.put(input.getName(), toscaInput); + } + toscaOperation.setInputs(toscaInputs); + } + + private static String getInputValue(String inputValue) { + String toscaInputValue = inputValue; + if (Objects.nonNull(inputValue) && inputValue.contains(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) { + Gson gson = new Gson(); + Map> consumptionValue = gson.fromJson(inputValue, Map.class); + List mappedOutputValue = + consumptionValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); + //Extract the interface name from the interface type + String interfaceType = mappedOutputValue.get(1); + String interfaceName = interfaceType.substring(interfaceType.lastIndexOf('.') + 1); + mappedOutputValue.remove(1); + mappedOutputValue.add(1, interfaceName); + toscaInputValue = gson.toJson(consumptionValue); + } + return toscaInputValue; + } + + private static String getInterfaceType(Component component, String interfaceType) { + if (LOCAL_INTERFACE_TYPE.equals(interfaceType)) { + return DERIVED_FROM_BASE_DEFAULT + + component.getComponentMetadataDefinition() + .getMetadataDataDefinition().getSystemName(); + } + + return interfaceType; + } + + private static Map getObjectAsMap(Object obj) { + ObjectMapper objectMapper = new ObjectMapper(); + if (obj instanceof ToscaInterfaceDefinition) { + //Prevent empty field serialization in interface definition + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + Map objectAsMap = + obj instanceof Map ? (Map) obj : objectMapper.convertValue(obj, Map.class); + + if (objectAsMap.containsKey(DEFAULT)) { + Object defaultValue = objectAsMap.get(DEFAULT); + objectAsMap.remove(DEFAULT); + objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue); + } + return objectAsMap; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PolicyExportParserImpl.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PolicyExportParserImpl.java index 776676f7d8..ca4d40517a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PolicyExportParserImpl.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PolicyExportParserImpl.java @@ -58,11 +58,12 @@ public class PolicyExportParserImpl implements PolicyExportParser { private ApplicationDataTypeCache dataTypeCache; private Map dataTypes; - private PropertyConvertor propertyConvertor = PropertyConvertor.getInstance(); + private PropertyConvertor propertyConvertor; @Autowired - public PolicyExportParserImpl(ApplicationDataTypeCache dataTypeCache) { + public PolicyExportParserImpl(ApplicationDataTypeCache dataTypeCache, PropertyConvertor propertyConvertor) { this.dataTypeCache = dataTypeCache; + this.propertyConvertor = propertyConvertor; this.dataTypes = getDataTypes(); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java index 4e4afb005d..81c91cce99 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java @@ -20,12 +20,17 @@ package org.openecomp.sdc.be.tosca; -import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import fj.data.Either; +import java.io.StringReader; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Supplier; import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; @@ -43,16 +48,10 @@ import org.openecomp.sdc.be.tosca.model.EntrySchema; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.stereotype.Service; -import java.io.StringReader; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.function.Supplier; - +@Service public class PropertyConvertor { - private static PropertyConvertor instance; private JsonParser jsonParser = new JsonParser(); private static final Logger log = Logger.getLogger(PropertyConvertor.class); public enum PropertyType { @@ -60,17 +59,7 @@ public class PropertyConvertor { INPUT, PROPERTY } - Gson gson = new Gson(); - public PropertyConvertor() { - } - - public static synchronized PropertyConvertor getInstance() { - if (instance == null) { - instance = new PropertyConvertor(); - } - return instance; - } public Either convertProperties(Component component, ToscaNodeType toscaNodeType, Map dataTypes) { @@ -94,32 +83,19 @@ public class PropertyConvertor { public ToscaProperty convertProperty(Map dataTypes, PropertyDefinition property, PropertyType propertyType) { ToscaProperty prop = new ToscaProperty(); - - String innerType = null; + log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue()); SchemaDefinition schema = property.getSchema(); if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) { - innerType = schema.getProperty().getType(); EntrySchema eschema = new EntrySchema(); - eschema.setType(innerType); + eschema.setType(schema.getProperty().getType()); eschema.setDescription(schema.getProperty().getDescription()); prop.setEntry_schema(eschema); } - return getToscaProperty(dataTypes, property, prop, innerType, propertyType); - - } - - private ToscaProperty getToscaProperty(Map dataTypes, - PropertyDataDefinition property, - ToscaProperty prop, - String innerType, - PropertyType propertyType) { - log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue()); String defaultValue = property.getDefaultValue(); if(Objects.isNull(defaultValue)) { defaultValue = property.getValue(); } - Object convertedObj = - convertToToscaObject(property.getType(), defaultValue, innerType, dataTypes, false); + Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false); if (convertedObj != null) { prop.setDefaultp(convertedObj); } @@ -133,7 +109,9 @@ public class PropertyConvertor { } - public Object convertToToscaObject(String propertyType, String value, String innerType, Map dataTypes, boolean preserveEmptyValue) { + public Object convertToToscaObject(PropertyDataDefinition property, String value, Map dataTypes, boolean preserveEmptyValue) { + String propertyType = property.getType(); + String innerType = property.getSchemaType(); log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType); if (StringUtils.isEmpty(value)) { value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes); @@ -144,7 +122,7 @@ public class PropertyConvertor { try { ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance(); ToscaValueConverter innerConverter = null; - Boolean isScalar = true; + boolean isScalar = true; ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType); if (type == null) { @@ -234,12 +212,6 @@ public class PropertyConvertor { private Object convertValue(Map dataTypes, T input, Supplier supplier) { - String propertyType = input.getType(); - String innerType = null; - if (input.getSchema() != null && input.getSchema().getProperty() != null) { - innerType = input.getSchema().getProperty().getType(); - } - return convertToToscaObject(propertyType, supplier.get(), innerType, dataTypes, false); + return convertToToscaObject(input, supplier.get(), dataTypes, false); } - } 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 995d511acb..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 @@ -20,9 +20,28 @@ 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; @@ -87,7 +106,6 @@ 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; @@ -104,30 +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.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 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.utils.InterfacesOperationsToscaUtil.addInterfaceDefinitionElement; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceTypeElement; -import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.addInputsToProperties; -import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.getProxyNodeTypeInterfaces; -import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.getProxyNodeTypeProperties; - @org.springframework.stereotype.Component("tosca-export-handler") public class ToscaExportHandler { @@ -139,19 +133,23 @@ 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) { + CapabilityRequirementConverter capabilityRequirementConverter, PolicyExportParser policyExportParser, + 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.getInstance(); + this.propertyConvertor = propertyConvertor; this.inputConverter = inputConverter; this.interfaceLifecycleOperation = interfaceLifecycleOperation; + this.interfacesOperationsConverter = interfacesOperationsConverter; } @@ -603,7 +601,7 @@ public class ToscaExportHandler { List inputDef = component.getInputs(); Map mergedProperties = new HashMap<>(); - addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); + interfacesOperationsConverter.addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); addInputsToProperties(dataTypes, inputDef, mergedProperties); if(CollectionUtils.isNotEmpty(component.getProperties())) { @@ -834,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 { @@ -942,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) { @@ -1425,7 +1422,7 @@ public class ToscaExportHandler { } private static class CustomRepresenter extends Representer { - public CustomRepresenter() { + CustomRepresenter() { super(); // null representer is exceptional and it is stored as an instance // variable. @@ -1438,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 @@ -1495,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))); + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java index 46305407f7..475627b827 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java @@ -33,17 +33,18 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; @org.springframework.stereotype.Component public class InputConverter { private PropertyConvertor propertyConvertor; private static final Logger log = Logger.getLogger(ToscaExportHandler.class); - - public InputConverter() { - this.propertyConvertor = PropertyConvertor.getInstance(); - + @Autowired + public InputConverter(PropertyConvertor propertyConvertor) { + this.propertyConvertor = propertyConvertor; } + /** * This is the converter made for input * input is derived from properties and is similar to properties diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java deleted file mode 100644 index 4c0fb25fcd..0000000000 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java +++ /dev/null @@ -1,317 +0,0 @@ -/* - * Copyright © 2016-2018 European Support Limited - * - * 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. - */ - -package org.openecomp.sdc.be.tosca.utils; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.Gson; -import org.apache.commons.collections.MapUtils; -import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.ComponentInstance; -import org.openecomp.sdc.be.model.DataTypeDefinition; -import org.openecomp.sdc.be.model.InterfaceDefinition; -import org.openecomp.sdc.be.model.Product; -import org.openecomp.sdc.be.model.tosca.ToscaFunctions; -import org.openecomp.sdc.be.tosca.PropertyConvertor; -import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; -import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType; -import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition; -import org.openecomp.sdc.be.tosca.model.ToscaNodeType; -import org.openecomp.sdc.be.tosca.model.ToscaProperty; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - - -public class InterfacesOperationsToscaUtil { - - private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard"; - private static final String DERIVED_FROM_BASE_DEFAULT = "org.openecomp.interfaces.node.lifecycle."; - private static final String OPERATIONS_KEY = "operations"; - - private static final String DEFAULT = "default"; - private static final String DEFAULT_HAS_UNDERSCORE = "_default"; - private static final String DOT = "."; - private static final String DEFAULTP = "defaultp"; - - public static final String SELF = "SELF"; - private static final String LOCAL_INTERFACE_TYPE = "Local"; - - private InterfacesOperationsToscaUtil() { - } - - /** - * Creates the interface_types element. - * - * @param component to work on - * @return the added element - */ - public static Map addInterfaceTypeElement(Component component, List allInterfaceTypes) { - if (component instanceof Product) { - return null; - } - final Map interfaces = component.getInterfaces(); - if (MapUtils.isEmpty(interfaces)) { - return null; - } - - Map toscaInterfaceTypes = new HashMap<>(); - for (InterfaceDefinition interfaceDefinition : interfaces.values()) { - boolean isInterfaceTypeExistInGlobalType = - allInterfaceTypes.stream().anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType())); - if (!isInterfaceTypeExistInGlobalType) { - ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType(); - toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE); - - final Map operations = interfaceDefinition.getOperations(); - Map toscaOperations = new HashMap<>(); - - for (Map.Entry operationEntry : operations.entrySet()) { - toscaOperations.put(operationEntry.getValue().getName(), null); - } - toscaInterfaceType.setOperations(toscaOperations); - Map interfacesAsMap = getObjectAsMap(toscaInterfaceType); - Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS_KEY); - interfacesAsMap.putAll(operationsMap); - - toscaInterfaceTypes.put(getInterfaceType(component, LOCAL_INTERFACE_TYPE), interfacesAsMap); - } - } - return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null; - } - - /** - * Adds the 'interfaces' element to the node type provided. - * - * @param component to work on - * @param nodeType to which the interfaces element will be added - */ - public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType, - Map dataTypes, - boolean isAssociatedComponent) { - if (component instanceof Product) { - return; - } - final Map interfaces = component.getInterfaces(); - if (MapUtils.isEmpty(interfaces)) { - return; - } - Map toscaInterfaceDefinitions = getInterfacesMap(component, dataTypes, - isAssociatedComponent); - if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) { - nodeType.setInterfaces(toscaInterfaceDefinitions); - } - } - - private static Map getInterfacesMap(Component component, - Map dataTypes, - boolean isAssociatedComponent) { - return getInterfacesMap(component, null, component.getInterfaces(), dataTypes, isAssociatedComponent, false); - } - - public static Map getInterfacesMap(Component component, - ComponentInstance componentInstance, - Map interfaces, - Map dataTypes, - boolean isAssociatedComponent, - boolean isServiceProxyInterface) { - if(MapUtils.isEmpty(interfaces)) { - return null; - } - - Map toscaInterfaceDefinitions = new HashMap<>(); - for (InterfaceDefinition interfaceDefinition : interfaces.values()) { - ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition(); - String interfaceType; - if(componentInstance != null && LOCAL_INTERFACE_TYPE.equals(interfaceDefinition.getType())) { - interfaceType = DERIVED_FROM_BASE_DEFAULT + componentInstance.getSourceModelName(); - } else { - interfaceType = getInterfaceType(component, interfaceDefinition.getType()); - } - toscaInterfaceDefinition.setType(interfaceType); - final Map operations = interfaceDefinition.getOperations(); - Map toscaOperations = new HashMap<>(); - - String operationArtifactPath; - for (Map.Entry operationEntry : operations.entrySet()) { - ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition(); - if (isArtifactPresent(operationEntry)) { - operationArtifactPath = OperationArtifactUtil - .createOperationArtifactPath(component, componentInstance, operationEntry.getValue(), - isAssociatedComponent); - toscaOperation.setImplementation(operationArtifactPath); - } - toscaOperation.setDescription(operationEntry.getValue().getDescription()); - fillToscaOperationInputs(operationEntry.getValue(), dataTypes, toscaOperation, isServiceProxyInterface); - - toscaOperations.put(operationEntry.getValue().getName(), toscaOperation); - } - - toscaInterfaceDefinition.setOperations(toscaOperations); - Map interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition); - Map operationsMap = (Map) interfaceDefAsMap.remove(OPERATIONS_KEY); - if (isServiceProxyInterface) { - //Remove input type and copy default value directly into the proxy node template from the node type - handleServiceProxyOperationInputValue(operationsMap, interfaceType); - } else { - handleDefaults(operationsMap); - } - interfaceDefAsMap.putAll(operationsMap); - toscaInterfaceDefinitions.put(getLastPartOfName(interfaceType), interfaceDefAsMap); - } - - return toscaInterfaceDefinitions; - } - - private static void handleServiceProxyOperationInputValue(Map operationsMap, String parentKey) { - for (Map.Entry operationEntry : operationsMap.entrySet()) { - final Object value = operationEntry.getValue(); - final String key = operationEntry.getKey(); - if (value instanceof Map) { - if ("inputs".equals(parentKey)) { - Object defaultValue = getDefaultValue((Map) value); - operationsMap.put(key, defaultValue); - } else { - handleServiceProxyOperationInputValue((Map) value, key); - } - } - } - } - - private static Object getDefaultValue(Map inputValueMap) { - Object defaultValue = null; - for (Map.Entry operationEntry : inputValueMap.entrySet()) { - final Object value = operationEntry.getValue(); - if (value instanceof Map) { - getDefaultValue((Map) value); - } - final String key = operationEntry.getKey(); - if (key.equals(DEFAULTP)) { - defaultValue = inputValueMap.remove(key); - } - } - return defaultValue; - } - - /* - * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in - * ToscaExportHandler so, any string Map key named "defaultp" will have its named changed to "default" - * @param operationsMap the map to update - */ - private static void handleDefaults(Map operationsMap) { - for (Map.Entry operationEntry : operationsMap.entrySet()) { - final Object value = operationEntry.getValue(); - if (value instanceof Map) { - handleDefaults((Map) value); - } - final String key = operationEntry.getKey(); - if (key.equals(DEFAULTP)) { - Object removed = operationsMap.remove(key); - operationsMap.put(DEFAULT, removed); - } - } - } - - private static String getLastPartOfName(String toscaResourceName) { - return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1); - } - - private static boolean isArtifactPresent(Map.Entry operationEntry) { - final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation()); - if (isImplementationPresent) { - return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName()); - } - return false; - } - - private static void fillToscaOperationInputs(OperationDataDefinition operation, - Map dataTypes, - ToscaLifecycleOperationDefinition toscaOperation, - boolean isServiceProxyInterface) { - if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) { - toscaOperation.setInputs(null); - return; - } - Map toscaInputs = new HashMap<>(); - - for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) { - ToscaProperty toscaInput = new ToscaProperty(); - toscaInput.setDescription(input.getDescription()); - toscaInput.setType(input.getType()); - toscaInput.setRequired(input.isRequired()); - if (isServiceProxyInterface) { - String inputValue = Objects.nonNull(input.getValue()) ? getInputValue(input.getValue()) : - getInputValue(input.getToscaDefaultValue()); - toscaInput.setDefaultp(new PropertyConvertor().convertToToscaObject(input.getType(), - inputValue, input.getSchemaType(), dataTypes, false)); - } else { - toscaInput.setDefaultp(new PropertyConvertor().convertToToscaObject(input.getType(), - getInputValue(input.getToscaDefaultValue()), input.getSchemaType(), dataTypes, false)); - } - toscaInputs.put(input.getName(), toscaInput); - } - toscaOperation.setInputs(toscaInputs); - } - - private static String getInputValue(String inputValue) { - String toscaInputValue = inputValue; - if (Objects.nonNull(inputValue) && inputValue.contains(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) { - Gson gson = new Gson(); - Map> consumptionValue = gson.fromJson(inputValue, Map.class); - List mappedOutputValue = - consumptionValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); - //Extract the interface name from the interface type - String interfaceType = mappedOutputValue.get(1); - String interfaceName = interfaceType.substring(interfaceType.lastIndexOf('.') + 1); - mappedOutputValue.remove(1); - mappedOutputValue.add(1, interfaceName); - toscaInputValue = gson.toJson(consumptionValue); - } - return toscaInputValue; - } - - private static String getInterfaceType(Component component, String interfaceType) { - if (LOCAL_INTERFACE_TYPE.equals(interfaceType)) { - return DERIVED_FROM_BASE_DEFAULT - + component.getComponentMetadataDefinition() - .getMetadataDataDefinition().getSystemName(); - } - - return interfaceType; - } - - private static Map getObjectAsMap(Object obj) { - ObjectMapper objectMapper = new ObjectMapper(); - if (obj instanceof ToscaInterfaceDefinition) { - //Prevent empty field serialization in interface definition - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - } - Map objectAsMap = - obj instanceof Map ? (Map) obj : objectMapper.convertValue(obj, Map.class); - - if (objectAsMap.containsKey(DEFAULT)) { - Object defaultValue = objectAsMap.get(DEFAULT); - objectAsMap.remove(DEFAULT); - objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue); - } - return objectAsMap; - } -} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java deleted file mode 100644 index beb9dab7e8..0000000000 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright © 2016-2019 European Support Limited - * - * 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. - */ - -package org.openecomp.sdc.be.tosca.utils; - -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; -import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.DataTypeDefinition; -import org.openecomp.sdc.be.model.InputDefinition; -import org.openecomp.sdc.be.model.InterfaceDefinition; -import org.openecomp.sdc.be.tosca.PropertyConvertor; -import org.openecomp.sdc.be.tosca.model.ToscaProperty; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; - -import static org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput; - -public class ToscaExportUtils { - - private ToscaExportUtils() { - //Hiding implicit default constructor - } - - public static 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(InterfacesOperationsToscaUtil - .getInterfacesMap(proxyComponent, null, proxyComponentInterfaces, dataTypes, false, false)); - } - - public static 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.getInstance().convertProperty(dataTypes, property, - PropertyConvertor.PropertyType.PROPERTY)))); - } - return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty(); - } - - public static void addInputsToProperties(Map dataTypes, - List componentInputs, - Map mergedProperties) { - if (CollectionUtils.isEmpty(componentInputs)) { - return; - } - for(InputDefinition input : componentInputs) { - ToscaProperty property = new PropertyConvertor().convertProperty(dataTypes, input, - PropertyConvertor.PropertyType.INPUT); - mergedProperties.put(input.getName(), property); - } - } - - 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))); - } -} -- cgit 1.2.3-korg