diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java')
16 files changed, 845 insertions, 85 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/AbstractPnfdTransformationEngine.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/AbstractPnfdTransformationEngine.java new file mode 100644 index 0000000000..2391ed6ecc --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/AbstractPnfdTransformationEngine.java @@ -0,0 +1,86 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.converter.impl.pnfd; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.core.converter.pnfd.PnfdTransformationEngine; +import org.openecomp.core.converter.pnfd.model.Transformation; +import org.openecomp.core.converter.pnfd.model.TransformationBlock; +import org.openecomp.core.converter.pnfd.model.TransformationDescription; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +/** + * Engine that manages the PNF Descriptor transformation process. + */ +public abstract class AbstractPnfdTransformationEngine implements PnfdTransformationEngine { + + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPnfdTransformationEngine.class); + + protected final ServiceTemplate templateTo; + protected final ServiceTemplateReaderService templateFrom; + private final PnfdTransformationDescriptorReader pnfdTransformationDescriptorReader = + new PnfdTransformationDescriptorReader(); + protected TransformationDescription transformationDescription; + protected Map<TransformationBlock, List<Transformation>> transformationGroupByBlockMap; + private final String descriptorResourcePath; + + public AbstractPnfdTransformationEngine(final ServiceTemplateReaderService templateFrom, + final ServiceTemplate templateTo) { + this(templateFrom, templateTo, "pnfdTransformationTemplate/model-driven-conversion.yaml"); + } + + //used for tests purposes + AbstractPnfdTransformationEngine(final ServiceTemplateReaderService templateFrom, + final ServiceTemplate templateTo, + final String descriptorResourcePath) { + this.templateFrom = templateFrom; + this.templateTo = templateTo; + this.descriptorResourcePath = descriptorResourcePath; + } + + /** + * Gets and Reads the transformation description yaml file. + */ + protected void readDefinition() { + try (final InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(descriptorResourcePath)) { + if (resourceAsStream == null) { + if (LOGGER.isErrorEnabled()) { + LOGGER.error("Could not find resource '{}'", descriptorResourcePath); + } + return; + } + transformationDescription = pnfdTransformationDescriptorReader.parse(resourceAsStream); + } catch (final IOException e) { + LOGGER.error("Could not find resource '{}'", descriptorResourcePath, e); + } + } + + /** + * Executes all transformations specified in the descriptor. + */ + protected abstract void executeTransformations(); + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationEngine.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTemplateTransformationEngine.java index a2365b4f8a..b2038a00dd 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationEngine.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTemplateTransformationEngine.java @@ -19,7 +19,6 @@ package org.openecomp.core.converter.impl.pnfd; -import java.io.InputStream; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -35,35 +34,22 @@ import org.openecomp.core.converter.impl.pnfd.factory.PnfdBlockParserFactory; import org.openecomp.core.converter.impl.pnfd.parser.ConversionQueryYamlParser; import org.openecomp.core.converter.pnfd.model.Transformation; import org.openecomp.core.converter.pnfd.model.TransformationBlock; -import org.openecomp.core.converter.pnfd.model.TransformationDescription; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; /** - * Engine that manages the PNF Descriptor transformation process. + * Engine that manages the PNF Descriptor transformation process for the NodeTemplate block. */ -public class PnfdTransformationEngine { +public class PnfdNodeTemplateTransformationEngine extends AbstractPnfdTransformationEngine { - private static final Logger LOGGER = LoggerFactory.getLogger(PnfdTransformationEngine.class); - - private final ServiceTemplate templateTo; - private final ServiceTemplateReaderService templateFrom; - private final PnfdTransformationDescriptorReader pnfdTransformationDescriptorReader = - new PnfdTransformationDescriptorReader(); - private TransformationDescription transformationDescription; - private Map<TransformationBlock, List<Transformation>> transformationGroupByBlockMap; - private final String descriptorResourcePath; - - public PnfdTransformationEngine(final ServiceTemplateReaderService templateFrom, final ServiceTemplate templateTo) { - this(templateFrom, templateTo, "pnfdTransformationTemplate/model-driven-conversion.yaml"); + public PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom, + final ServiceTemplate templateTo) { + super(templateFrom, templateTo); } //used for tests purposes - PnfdTransformationEngine(final ServiceTemplateReaderService templateFrom, final ServiceTemplate templateTo, - final String descriptorResourcePath) { - this.templateFrom = templateFrom; - this.templateTo = templateTo; - this.descriptorResourcePath = descriptorResourcePath; + PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom, + final ServiceTemplate templateTo, + final String descriptorResourcePath) { + super(templateFrom, templateTo, descriptorResourcePath); } /** @@ -89,17 +75,12 @@ public class PnfdTransformationEngine { } } - /** - * Reads the transformation description yaml file. - */ - private void readDefinition() { - transformationDescription = pnfdTransformationDescriptorReader.parse(getDefinitionFileInputStream()); - } /** * Execute all transformations specified in the descriptor. */ - private void executeTransformations() { + @Override + protected void executeTransformations() { final Set<Transformation> transformationSet = transformationDescription.getTransformationSet(); if (CollectionUtils.isEmpty(transformationSet)) { return; @@ -107,6 +88,7 @@ public class PnfdTransformationEngine { transformationGroupByBlockMap = transformationSet.stream() .filter(Transformation::isValid) .collect(Collectors.groupingBy(Transformation::getBlock)); + executeCustomTypeTransformations(); final Map<String, String> inputsToConvertMap = executeNodeTemplateTransformations(); executeGetInputFunctionTransformations(inputsToConvertMap); } @@ -158,7 +140,9 @@ public class PnfdTransformationEngine { if (transformation != null) { final Map<String, Object> conversionQueryMap = new HashMap<>(); conversionQueryMap.put(inputName, null); - transformation.setConversionQuery(ConversionQueryYamlParser.parse(conversionQueryMap)); + transformation.setConversionQuery( + ConversionQueryYamlParser.parse(conversionQueryMap).orElse(null) + ); PnfdBlockParserFactory.getInstance().get(transformation) .ifPresent(pnfParser -> pnfParser.parse(templateFrom, templateTo)); } @@ -167,17 +151,17 @@ public class PnfdTransformationEngine { } /** - * Gets the transformation definition yaml file path. - * @return The transformation definition yaml path. + * Parses a Customized Node Type that extend from a valid ONAP NodeType. */ - private InputStream getDefinitionFileInputStream() { - final InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(descriptorResourcePath); - if (resourceAsStream == null) { - if (LOGGER.isErrorEnabled()) { - LOGGER.error(String.format("Could not find resource '%s'", descriptorResourcePath)); - } - return null; + private void executeCustomTypeTransformations() { + final List<Transformation> transformationList = transformationGroupByBlockMap + .get((TransformationBlock.CUSTOM_NODE_TYPE)); + if (CollectionUtils.isEmpty(transformationList)) { + return; } - return resourceAsStream; + transformationList.forEach(transformation -> + PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfdBlockParser -> + pnfdBlockParser.parse(templateFrom, templateTo))); } + } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTypeTransformationEngine.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTypeTransformationEngine.java new file mode 100644 index 0000000000..48d06b67a4 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTypeTransformationEngine.java @@ -0,0 +1,72 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.converter.impl.pnfd; + +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.collections.CollectionUtils; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.core.converter.impl.pnfd.factory.PnfdBlockParserFactory; +import org.openecomp.core.converter.pnfd.model.Transformation; +import org.openecomp.core.converter.pnfd.model.TransformationBlock; + +/** + * Engine that manages the PNF Descriptor transformation process for the NodeType block. + */ +public class PnfdNodeTypeTransformationEngine extends AbstractPnfdTransformationEngine { + + public PnfdNodeTypeTransformationEngine(final ServiceTemplateReaderService templateFrom, + final ServiceTemplate templateTo) { + super(templateFrom, templateTo); + } + + /** + * Runs the transformation process. + */ + @Override + public void transform() { + readDefinition(); + executeTransformations(); + } + + /** + * Execute all transformations specified in the descriptor. + */ + @Override + protected void executeTransformations() { + final Set<Transformation> transformationSet = transformationDescription.getTransformationSet(); + if (CollectionUtils.isEmpty(transformationSet)) { + return; + } + final Set<Transformation> validNodeTypeTransformationSet = transformationSet.stream() + .filter(transformation -> transformation.getBlock() == TransformationBlock.NODE_TYPE) + .filter(Transformation::isValid) + .collect(Collectors.toSet()); + + if (CollectionUtils.isEmpty(validNodeTypeTransformationSet)) { + return; + } + validNodeTypeTransformationSet.forEach(transformation -> + PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfdBlockParser -> + pnfdBlockParser.parse(templateFrom, templateTo))); + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationDescriptorReader.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationDescriptorReader.java index 6ef5caf21b..d040c93d09 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationDescriptorReader.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationDescriptorReader.java @@ -25,6 +25,7 @@ import java.io.InputStream; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; @@ -82,7 +83,8 @@ public class PnfdTransformationDescriptorReader { } return transformationYamlList.stream() - .map(conversionMap -> TransformationYamlParser.parse((Map<String, Object>) conversionMap)) + .map(conversionMap -> TransformationYamlParser.parse((Map<String, Object>) conversionMap).orElse(null)) + .filter(Objects::nonNull) .collect(Collectors.toSet()); } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/factory/PnfdBlockParserFactory.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/factory/PnfdBlockParserFactory.java index 512fdb5ca7..12dd790c9d 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/factory/PnfdBlockParserFactory.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/factory/PnfdBlockParserFactory.java @@ -20,9 +20,11 @@ package org.openecomp.core.converter.impl.pnfd.factory; import java.util.Optional; -import org.openecomp.core.converter.pnfd.model.Transformation; import org.openecomp.core.converter.impl.pnfd.parser.PnfdInputBlockParser; import org.openecomp.core.converter.impl.pnfd.parser.PnfdNodeTemplateBlockParser; +import org.openecomp.core.converter.impl.pnfd.parser.PnfdCustomNodeTypeBlockParser; +import org.openecomp.core.converter.impl.pnfd.parser.PnfdNodeTypeBlockParser; +import org.openecomp.core.converter.pnfd.model.Transformation; import org.openecomp.core.converter.pnfd.parser.PnfdBlockParser; /** @@ -46,6 +48,10 @@ public class PnfdBlockParserFactory { switch (transformation.getBlock()) { case NODE_TEMPLATE: return Optional.of(new PnfdNodeTemplateBlockParser(transformation)); + case NODE_TYPE: + return Optional.of(new PnfdNodeTypeBlockParser(transformation)); + case CUSTOM_NODE_TYPE: + return Optional.of(new PnfdCustomNodeTypeBlockParser(transformation)); case INPUT: case GET_INPUT_FUNCTION: return Optional.of(new PnfdInputBlockParser(transformation)); diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/AbstractPnfdBlockParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/AbstractPnfdBlockParser.java new file mode 100644 index 0000000000..056d07aed0 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/AbstractPnfdBlockParser.java @@ -0,0 +1,226 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.converter.impl.pnfd.parser; + +import com.google.common.collect.ImmutableMap; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.commons.collections.MapUtils; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.core.converter.impl.pnfd.PnfdQueryExecutor; +import org.openecomp.core.converter.pnfd.model.ConversionDefinition; +import org.openecomp.core.converter.pnfd.model.ConversionQuery; +import org.openecomp.core.converter.pnfd.model.PnfTransformationToken; +import org.openecomp.core.converter.pnfd.model.Transformation; +import org.openecomp.core.converter.pnfd.parser.PnfdBlockParser; + +public abstract class AbstractPnfdBlockParser implements PnfdBlockParser { + + protected final Transformation transformation; + protected ServiceTemplateReaderService templateFrom; + protected ServiceTemplate templateTo; + protected Object attributeValueToBeConverted = ""; + + public AbstractPnfdBlockParser(final Transformation transformation) { + this.transformation = transformation; + } + + /** + * Parses a PNFD block based on the {@link Transformation} provided during the {@link PnfdBlockParser} + * instantiation. + * + * @param templateFrom the original PNFD template + * @param templateTo the resulting PNFD template + */ + public void parse(final ServiceTemplateReaderService templateFrom, final ServiceTemplate templateTo) { + this.templateFrom = templateFrom; + this.templateTo = templateTo; + final Set<Map<String, Object>> blockToParseSet = findBlocksToParse(); + if (!blockToParseSet.isEmpty()) { + blockToParseSet.forEach(this::parse); + } + } + + /** + * Applies all specified conversions in {@link Transformation#getConversionDefinitionList()} for the given + * blockYamlObject. + * + * @param blockYamlObject the block content as a YAML object + */ + protected void parse(final Map<String, Object> blockYamlObject) { + if (MapUtils.isEmpty(blockYamlObject)) { + return; + } + final List<ConversionDefinition> conversionDefinitionList = transformation.getConversionDefinitionList(); + final Map<String, Object> parsedBlockYamlObject = new HashMap<>(); + final String blockName = blockYamlObject.keySet().iterator().next(); + conversionDefinitionList.stream() + .filter(conversionDefinition -> conversionDefinition.getConversionQuery().isValidAttributeQuery()) + .forEach(conversionDefinition -> { + final Map<String, Object> query = + (Map<String, Object>) conversionDefinition.getConversionQuery().getQuery(); + final Map<String, Object> blockAttributeMap = (Map<String, Object>) blockYamlObject.get(blockName); + final Optional<Map<String, Object>> parsedBlockAttributeMap = buildParsedBlock(query, blockAttributeMap + , conversionDefinition); + parsedBlockAttributeMap.ifPresent(convertedNodeTemplateAttributeMap1 -> + mergeYamlObjects(parsedBlockYamlObject, convertedNodeTemplateAttributeMap1) + ); + }); + + write(blockName, parsedBlockYamlObject); + } + + /** + * Writes the block in the resulting {@link ServiceTemplate} {@link #templateTo}. + * + * @param blockName the name of the block + * @param parsedBlockYamlObject the block content as a YAML object + */ + protected abstract void write(final String blockName, final Map<String, Object> parsedBlockYamlObject); + + /** + * Uses the provided attribute query to find a attribute in the original YAML object and apply the provided + * conversion. + * + * @param attributeQuery the attribute query + * @param fromNodeTemplateAttributeMap the original YAML object + * @param conversionDefinition the conversion + * @return the rebuilt original YAML object with the converted attribute + */ + protected abstract Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery, + final Map<String, Object> fromNodeTemplateAttributeMap, + final ConversionDefinition conversionDefinition); + + /** + * Merges two YAML objects. + * + * @param originalMap original YAML object + * @param toBeMergedMap YAML object to be merged + * @return the new YAML object representing the merge result. + */ + protected Map<String, Object> mergeYamlObjects(final Map<String, Object> originalMap, + final Map<String, Object> toBeMergedMap) { + toBeMergedMap.forEach( + (key, value) -> originalMap.merge(key, value, + (toBeMergedValue, originalValue) -> { + if (originalValue instanceof Map) { + return mergeYamlObjects((Map) originalValue, (Map) toBeMergedValue); + } + return originalValue; + }) + ); + + return originalMap; + } + + /** + * Executes the provided {@link #transformation getConversionQuery} YAML query to find the blocks to be parsed in + * {@link #templateFrom}. + * + * @return The YAML blocks found + */ + protected abstract Set<Map<String, Object>> findBlocksToParse(); + + /** + * Checks if the YAML object is a TOSCA get_input call + * + * @param yamlObject the YAML object + * @return {@code true} if the YAML object is a TOSCA get_input call, {@code false} otherwise + */ + protected boolean isGetInputFunction(final Object yamlObject) { + if (yamlObject instanceof Map) { + final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject; + return yamlMap.containsKey(PnfTransformationToken.GET_INPUT.getName()); + } + + return false; + } + + /** + * Extracts the value from an YAML Object. + * @param yamlObject + * @return The Object value from the yamlObject parameter. + */ + protected String extractObjectValue(final Object yamlObject) { + if (yamlObject instanceof Map) { + final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject; + return (String) yamlMap.values().stream().findFirst().orElse(null); + } + + return null; + } + + /** + * Gets the stored input names called with TOSCA get_input function and its transformation configured in {@link + * ConversionDefinition#getToGetInput()} + */ + public Optional<Map<String, String>> getInputAndTransformationNameMap() { + return Optional.empty(); + } + + /** + * Finds all the derived node types from the provided node types. + * + * @param rootNodeTypeMap a map with the root node types to find the derived ones + * @param derivedNodeTypeMap a map that will be filled with the derived node types + */ + private void findAllDerivedNodeType(final Map<String, Object> rootNodeTypeMap, + final Map<String, Object> derivedNodeTypeMap) { + templateFrom.getNodeTypes().entrySet().stream() + .filter(nodeEntry -> rootNodeTypeMap.containsKey(extractObjectValue(nodeEntry.getValue()))) + .forEach(nodeEntry -> { + if (!derivedNodeTypeMap.containsKey(nodeEntry.getKey())) { + derivedNodeTypeMap.put(nodeEntry.getKey(), nodeEntry.getValue()); + final ImmutableMap<String, Object> newRootNodeTypeMap = ImmutableMap + .of(nodeEntry.getKey(), nodeEntry.getValue()); + findAllDerivedNodeType(newRootNodeTypeMap, derivedNodeTypeMap); + } + }); + } + + /** + * Fetches all Custom NodeTypes based on the query result. + * @return a map with all custom Node Types that matches with the query result. + */ + protected Map<String, Object> fetchCustomNodeType() { + final Map<String, Object> nodeTypesMap = templateFrom.getNodeTypes(); + if (MapUtils.isEmpty(nodeTypesMap)) { + return Collections.emptyMap(); + } + final ConversionQuery conversionQuery = transformation.getConversionQuery(); + final Map<String, Object> customNodeTypesMap = new HashMap<>(); + nodeTypesMap.entrySet().stream() + .filter(nodeEntry -> PnfdQueryExecutor.find(conversionQuery, nodeEntry.getValue())) + .forEach(customNode -> { + attributeValueToBeConverted = extractObjectValue(customNode.getValue()); + customNodeTypesMap.put(customNode.getKey(), customNode.getValue()); + }); + final Map<String, Object> childNodeTypeMap = new HashMap<>(); + findAllDerivedNodeType(customNodeTypesMap, childNodeTypeMap); + customNodeTypesMap.putAll(childNodeTypeMap); + return customNodeTypesMap; + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionDefinitionYamlParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionDefinitionYamlParser.java index 143185210e..2ea9f2dc93 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionDefinitionYamlParser.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionDefinitionYamlParser.java @@ -20,16 +20,21 @@ package org.openecomp.core.converter.impl.pnfd.parser; import java.util.Map; +import java.util.Optional; import org.openecomp.core.converter.pnfd.model.ConversionDefinition; import org.openecomp.core.converter.pnfd.model.ConversionQuery; import org.openecomp.core.converter.pnfd.model.PnfTransformationToken; import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; /** * Handles YAML from/to {@link ConversionDefinition} conversions */ public class ConversionDefinitionYamlParser { + private static final Logger LOGGER = LoggerFactory.getLogger(ConversionDefinitionYamlParser.class); + private ConversionDefinitionYamlParser() { } @@ -40,16 +45,20 @@ public class ConversionDefinitionYamlParser { * @return * A new instance of {@link ConversionDefinition}. */ - public static ConversionDefinition parse(final Map<String, Object> conversionYaml) { + public static Optional<ConversionDefinition> parse(final Map<String, Object> conversionYaml) { final ConversionQuery conversionQuery = ConversionQueryYamlParser - .parse(conversionYaml.get(PnfTransformationToken.QUERY.getName())); + .parse(conversionYaml.get(PnfTransformationToken.QUERY.getName())).orElse(null); + if (conversionQuery == null) { + LOGGER.warn("Invalid '{}' for '{}'", PnfTransformationToken.QUERY.getName(), conversionYaml.toString()); + return Optional.empty(); + } final String toName = (String) conversionYaml.get(PnfTransformationToken.TO_NAME.getName()); final PnfdConversionStrategy toValue = PnfdConversionStrategyYamlParser .parse((Map<String, Object>) conversionYaml.get(PnfTransformationToken.TO_VALUE.getName())) .orElse(null); final String toGetInput = (String) conversionYaml.get(PnfTransformationToken.TO_GET_INPUT.getName()); - return new ConversionDefinition(conversionQuery, toName, toValue, toGetInput); + return Optional.of(new ConversionDefinition(conversionQuery, toName, toValue, toGetInput)); } } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionQueryYamlParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionQueryYamlParser.java index 417262c61c..374bf3702f 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionQueryYamlParser.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionQueryYamlParser.java @@ -19,6 +19,7 @@ package org.openecomp.core.converter.impl.pnfd.parser; +import java.util.Optional; import org.openecomp.core.converter.pnfd.model.ConversionQuery; /** @@ -36,7 +37,11 @@ public class ConversionQueryYamlParser { * @return * A new instance of {@link ConversionQuery}. */ - public static ConversionQuery parse(final Object conversionYaml) { - return new ConversionQuery(conversionYaml); + public static Optional<ConversionQuery> parse(final Object conversionYaml) { + if (conversionYaml == null) { + return Optional.empty(); + } + + return Optional.of(new ConversionQuery(conversionYaml)); } } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/NodeTypeYamlParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/NodeTypeYamlParser.java new file mode 100644 index 0000000000..b0b52e0c40 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/NodeTypeYamlParser.java @@ -0,0 +1,67 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.converter.impl.pnfd.parser; + +import java.util.List; +import java.util.Map; +import org.onap.sdc.tosca.datatypes.model.ArtifactDefinition; +import org.onap.sdc.tosca.datatypes.model.AttributeDefinition; +import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition; +import org.onap.sdc.tosca.datatypes.model.NodeType; +import org.onap.sdc.tosca.datatypes.model.PropertyDefinition; +import org.onap.sdc.tosca.datatypes.model.RequirementDefinition; +import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; + +/** + * Handles YAML from/to {@link NodeType} conversions + */ +public class NodeTypeYamlParser { + + private NodeTypeYamlParser() { + } + + /** + * Parses the given a YAML object to a {@link NodeType} instance. + * @param nodeTypeYaml the YAML object representing a TOSCA Node Type + * @return A new instance of {@link NodeType}. + */ + @SuppressWarnings("unchecked") + public static NodeType parse(final Map<String, Object> nodeTypeYaml) { + final NodeType nodeType = new NodeType(); + nodeType.setDerived_from((String) nodeTypeYaml.get(ToscaTagNamesEnum.DERIVED_FROM.getElementName())); + nodeType.setDescription((String) nodeTypeYaml.get(ToscaTagNamesEnum.DESCRIPTION.getElementName())); + nodeType.setVersion((String) nodeTypeYaml.get("version")); + nodeType.setProperties( + (Map<String, PropertyDefinition>) nodeTypeYaml.get(ToscaTagNamesEnum.PROPERTIES.getElementName())); + nodeType.setArtifacts((Map<String, ArtifactDefinition>) nodeTypeYaml.get("artifacts")); + nodeType.setMetadata((Map<String, String>) nodeTypeYaml.get("metadata")); + nodeType.setInterfaces( + (Map<String, Object>) nodeTypeYaml.get(ToscaTagNamesEnum.INTERFACES.getElementName())); + nodeType.setRequirements( + (List<Map<String, RequirementDefinition>>) nodeTypeYaml.get(ToscaTagNamesEnum.REQUIREMENTS.getElementName())); + nodeType.setCapabilities( + (Map<String, CapabilityDefinition>) nodeTypeYaml.get(ToscaTagNamesEnum.CAPABILITIES.getElementName())); + nodeType.setAttributes( + (Map<String, AttributeDefinition>) nodeTypeYaml.get(ToscaTagNamesEnum.ATTRIBUTES.getElementName())); + + return nodeType; + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdCustomNodeTypeBlockParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdCustomNodeTypeBlockParser.java new file mode 100644 index 0000000000..3efde199b4 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdCustomNodeTypeBlockParser.java @@ -0,0 +1,88 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.converter.impl.pnfd.parser; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.collections.MapUtils; +import org.onap.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.core.converter.pnfd.model.ConversionDefinition; +import org.openecomp.core.converter.pnfd.model.Transformation; +import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy; +import org.openecomp.sdc.tosca.services.DataModelUtil; + +public class PnfdCustomNodeTypeBlockParser extends AbstractPnfdBlockParser { + + public PnfdCustomNodeTypeBlockParser(final Transformation transformation) { + super(transformation); + } + + @Override + protected Set<Map<String, Object>> findBlocksToParse() { + final Map<String, Object> nodeTemplateMap = templateFrom.getNodeTemplates(); + final Map<String, Object> customNodeTypeMap = fetchCustomNodeType(); + if (customNodeTypeMap.isEmpty() || MapUtils.isEmpty(nodeTemplateMap)) { + return Collections.emptySet(); + } + return customNodeTypeMap.entrySet().stream() + .map(customNode -> { + final Map<String, Object> map = new HashMap<>(); + nodeTemplateMap.entrySet().stream() + .filter(nodeTemplate -> + extractObjectValue(nodeTemplate.getValue()).equalsIgnoreCase(customNode.getKey())) + .forEach(nodeType -> map.put(nodeType.getKey(), nodeType.getValue())); + return map; + }).collect(Collectors.toSet()); + } + + @Override + protected Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery, + final Map<String, Object> fromNodeTemplateAttributeMap, + final ConversionDefinition conversionDefinition) { + //cannot query for more than one attribute + if (attributeQuery.keySet().size() > 1) { + return Optional.empty(); + } + final String attribute = attributeQuery.keySet().iterator().next(); + final Object queryValue = attributeQuery.get(attribute); + final Map<String, Object> parsedNodeTemplate = new HashMap<>(); + if (queryValue == null) { + final PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy(); + final Optional convertedAttribute = pnfdConversionStrategy.convert(attributeValueToBeConverted); + if (convertedAttribute.isPresent()) { + parsedNodeTemplate.put(conversionDefinition.getToAttributeName(), convertedAttribute.get()); + } + } + return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate); + } + + @Override + protected void write(final String nodeTemplateName, final Map<String, Object> parsedTemplateMap) { + if (!parsedTemplateMap.isEmpty()) { + final NodeTemplate parsedNodeTemplate = NodeTemplateYamlParser.parse(parsedTemplateMap); + DataModelUtil.addNodeTemplate(templateTo, nodeTemplateName, parsedNodeTemplate); + } + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdInputBlockParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdInputBlockParser.java index 4aec8c8ac3..4ce7b2e1c7 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdInputBlockParser.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdInputBlockParser.java @@ -28,7 +28,6 @@ import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections.MapUtils; import org.onap.sdc.tosca.datatypes.model.ParameterDefinition; -import org.openecomp.core.converter.pnfd.parser.AbstractPnfdBlockParser; import org.openecomp.core.converter.impl.pnfd.PnfdQueryExecutor; import org.openecomp.core.converter.pnfd.model.ConversionDefinition; import org.openecomp.core.converter.pnfd.model.ConversionQuery; diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTemplateBlockParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTemplateBlockParser.java index 5539840561..5d7a6049cd 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTemplateBlockParser.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTemplateBlockParser.java @@ -27,12 +27,11 @@ import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections.MapUtils; import org.onap.sdc.tosca.datatypes.model.NodeTemplate; -import org.openecomp.core.converter.pnfd.parser.AbstractPnfdBlockParser; import org.openecomp.core.converter.impl.pnfd.PnfdQueryExecutor; +import org.openecomp.core.converter.impl.pnfd.strategy.CopyConversionStrategy; import org.openecomp.core.converter.pnfd.model.ConversionDefinition; import org.openecomp.core.converter.pnfd.model.ConversionQuery; import org.openecomp.core.converter.pnfd.model.Transformation; -import org.openecomp.core.converter.impl.pnfd.strategy.CopyConversionStrategy; import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy; import org.openecomp.sdc.tosca.services.DataModelUtil; @@ -44,7 +43,25 @@ public class PnfdNodeTemplateBlockParser extends AbstractPnfdBlockParser { super(transformation); } - public Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery, + @Override + protected Set<Map<String, Object>> findBlocksToParse() { + final ConversionQuery conversionQuery = transformation.getConversionQuery(); + final Map<String, Object> nodeTemplateMap = templateFrom.getNodeTemplates(); + if (MapUtils.isEmpty(nodeTemplateMap)) { + return Collections.emptySet(); + } + + return nodeTemplateMap.entrySet().stream() + .filter(mapEntry -> PnfdQueryExecutor.find(conversionQuery, mapEntry.getValue())) + .map(stringObjectEntry -> { + final Map<String, Object> map = new HashMap<>(); + map.put(stringObjectEntry.getKey(), stringObjectEntry.getValue()); + return map; + }).collect(Collectors.toSet()); + } + + @Override + protected Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery, final Map<String, Object> fromNodeTemplateAttributeMap, final ConversionDefinition conversionDefinition) { //cannot query for more than one attribute @@ -57,7 +74,7 @@ public class PnfdNodeTemplateBlockParser extends AbstractPnfdBlockParser { if (queryValue == null) { PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy(); if (isGetInputFunction(attributeValueToConvert)) { - inputNameToConvertMap.put(extractGetInputFunctionValue(attributeValueToConvert) + inputNameToConvertMap.put(extractObjectValue(attributeValueToConvert) , conversionDefinition.getToGetInput() ); pnfdConversionStrategy = new CopyConversionStrategy(); @@ -83,24 +100,8 @@ public class PnfdNodeTemplateBlockParser extends AbstractPnfdBlockParser { } } - protected Set<Map<String, Object>> findBlocksToParse() { - final ConversionQuery conversionQuery = transformation.getConversionQuery(); - final Map<String, Object> nodeTemplateMap = templateFrom.getNodeTemplates(); - if (MapUtils.isEmpty(nodeTemplateMap)) { - return Collections.emptySet(); - } - - return nodeTemplateMap.entrySet().stream() - .filter(mapEntry -> PnfdQueryExecutor.find(conversionQuery, mapEntry.getValue())) - .map(stringObjectEntry -> { - final Map<String, Object> map = new HashMap<>(); - map.put(stringObjectEntry.getKey(), stringObjectEntry.getValue()); - return map; - }).collect(Collectors.toSet()); - } - @Override - public void write(final String nodeTemplateName, final Map<String, Object> parsedNodeTemplateMap) { + protected void write(final String nodeTemplateName, final Map<String, Object> parsedNodeTemplateMap) { if (!parsedNodeTemplateMap.isEmpty()) { final NodeTemplate parsedNodeTemplate = NodeTemplateYamlParser.parse(parsedNodeTemplateMap); DataModelUtil.addNodeTemplate(templateTo, nodeTemplateName, parsedNodeTemplate); diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTypeBlockParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTypeBlockParser.java new file mode 100644 index 0000000000..386bbfb558 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTypeBlockParser.java @@ -0,0 +1,94 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.converter.impl.pnfd.parser; + +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DERIVED_FROM; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.onap.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.core.converter.pnfd.model.ConversionDefinition; +import org.openecomp.core.converter.pnfd.model.Transformation; +import org.openecomp.core.converter.pnfd.model.TransformationPropertyType; +import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy; +import org.openecomp.sdc.tosca.services.DataModelUtil; + +public class PnfdNodeTypeBlockParser extends AbstractPnfdBlockParser { + + public PnfdNodeTypeBlockParser(final Transformation transformation) { + super(transformation); + } + + @Override + protected Set<Map<String, Object>> findBlocksToParse() { + final Map<String, Object> customNodeTypeMap = fetchCustomNodeType(); + if (customNodeTypeMap.isEmpty()) { + return Collections.emptySet(); + } + + final String nodeNamePrefix = + transformation.getPropertyValue(TransformationPropertyType.NODE_NAME_PREFIX, String.class) + .orElse(""); + + return customNodeTypeMap.entrySet().parallelStream() + .map(nodeEntry -> { + final Map<String, Object> map = new HashMap<>(); + map.put(nodeNamePrefix.concat(nodeEntry.getKey()), nodeEntry.getValue()); + return map; + }).collect(Collectors.toSet()); + } + + @Override + protected Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery, + final Map<String, Object> fromNodeTemplateAttributeMap, + final ConversionDefinition conversionDefinition) { + //cannot query for more than one attribute + if (attributeQuery.keySet().size() > 1) { + return Optional.empty(); + } + final String attribute = attributeQuery.keySet().iterator().next(); + final Object queryValue = attributeQuery.get(attribute); + final Object attributeValueToConvert = fromNodeTemplateAttributeMap.get(attribute); + final Map<String, Object> parsedNodeTemplate = new HashMap<>(); + if (queryValue == null) { + PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy(); + final Optional convertedAttribute = pnfdConversionStrategy + .convert(DERIVED_FROM.getElementName() + .equalsIgnoreCase(attribute) ? attributeValueToBeConverted : attributeValueToConvert); + if (convertedAttribute.isPresent()) { + parsedNodeTemplate.put(conversionDefinition.getToAttributeName(), convertedAttribute.get()); + } + } + return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate); + } + + @Override + protected void write(final String blockName, final Map<String, Object> parsedBlockYamlObject) { + if (!parsedBlockYamlObject.isEmpty()) { + final NodeType nodeTypeYamlParser = NodeTypeYamlParser.parse(parsedBlockYamlObject); + DataModelUtil.addNodeType(templateTo, blockName, nodeTypeYamlParser); + } + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/TransformationYamlParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/TransformationYamlParser.java index 16d06704b3..6ad04b0f7a 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/TransformationYamlParser.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/TransformationYamlParser.java @@ -21,24 +21,43 @@ package org.openecomp.core.converter.impl.pnfd.parser; import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.CONVERSIONS; import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.DESCRIPTION; +import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.FROM; import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.NAME; +import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.PROPERTIES; import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.QUERY; +import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.TO; import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.TRANSFORMATION_FOR; +import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +import org.openecomp.core.converter.impl.pnfd.strategy.ReplaceConversionStrategy; import org.openecomp.core.converter.pnfd.model.ConversionDefinition; +import org.openecomp.core.converter.pnfd.model.ConversionQuery; import org.openecomp.core.converter.pnfd.model.Transformation; import org.openecomp.core.converter.pnfd.model.TransformationBlock; +import org.openecomp.core.converter.pnfd.model.TransformationProperty; +import org.openecomp.core.converter.pnfd.model.TransformationPropertyType; +import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; /** * Handles YAML from/to {@link Transformation} conversions */ public class TransformationYamlParser { + private static final Logger LOGGER = LoggerFactory.getLogger(TransformationYamlParser.class); + private TransformationYamlParser() { } @@ -49,20 +68,101 @@ public class TransformationYamlParser { * @return * A new instance of {@link Transformation}. */ - public static Transformation parse(final Map<String, Object> transformationYaml) { + public static Optional<Transformation> parse(final Map<String, Object> transformationYaml) { final Transformation transformation = new Transformation(); - transformation.setName((String) transformationYaml.get(NAME.getName())); - transformation.setDescription((String) transformationYaml.get(DESCRIPTION.getName())); + final Optional<String> name = parseStringAttribute(NAME.getName(), transformationYaml); + if (!name.isPresent()) { + LOGGER.warn("Invalid '{}' value in transformation '{}'", NAME.getName(), transformationYaml.toString()); + } + transformation.setName(name.orElse(null)); + transformation.setDescription(parseStringAttribute(DESCRIPTION.getName(), transformationYaml).orElse(null)); + transformation.setPropertySet(readProperties(transformationYaml)); + + final String block = parseStringAttribute(TRANSFORMATION_FOR.getName(), transformationYaml).orElse(null); + final Optional<TransformationBlock> transformationBlockOptional = TransformationBlock.parse(block); + if (transformationBlockOptional.isPresent()) { + final TransformationBlock transformationBlock = transformationBlockOptional.get(); + transformation.setBlock(transformationBlock); + parseTransformationBlock(transformationBlock, transformation, transformationYaml); + } else { + LOGGER.warn("Invalid '{}' value in transformation '{}'", TRANSFORMATION_FOR.getName(), + transformationYaml.toString()); + } + + if (transformation.isValid()) { + return Optional.of(transformation); + } + + return Optional.empty(); + } + + private static Set<TransformationProperty> readProperties(final Map<String, Object> transformationYaml) { + final Map<String, Object> propertyMap = (Map<String, Object>) transformationYaml.get(PROPERTIES.getName()); + if (MapUtils.isEmpty(propertyMap)) { + return Collections.emptySet(); + } + + final Set<TransformationProperty> propertySet = new HashSet<>(); + + propertyMap.forEach((key, value) -> { + final TransformationPropertyType transformationPropertyType = TransformationPropertyType.parse(key) + .orElse(null); + + if(transformationPropertyType != null) { + if (value instanceof String) { + propertySet.add(new TransformationProperty<>(transformationPropertyType, (String) value)); + } else if (value instanceof Boolean) { + propertySet.add(new TransformationProperty<>(transformationPropertyType, (Boolean) value)); + } else if (value instanceof Integer) { + propertySet.add(new TransformationProperty<>(transformationPropertyType, (Integer) value)); + } else { + propertySet.add(new TransformationProperty<>(transformationPropertyType, value)); + } + } + }); - final String block = (String) transformationYaml.get(TRANSFORMATION_FOR.getName()); - transformation.setBlock(TransformationBlock.parse(block).orElse(null)); + return propertySet; + } + + private static void parseTransformationBlock(final TransformationBlock transformationBlock, + final Transformation transformationReference, + final Map<String, Object> transformationYaml) { + if (transformationBlock == TransformationBlock.CUSTOM_NODE_TYPE) { + parseCustomNodeTypeBlock(transformationReference, transformationYaml); + return; + } - transformation.setConversionQuery( - ConversionQueryYamlParser.parse(transformationYaml.get(QUERY.getName())) - ); - transformation.setConversionDefinitionList(parseConversions(transformationYaml)); + ConversionQueryYamlParser.parse(transformationYaml.get(QUERY.getName())) + .ifPresent(transformationReference::setConversionQuery); - return transformation; + transformationReference.setConversionDefinitionList(parseConversions(transformationYaml)); + } + + private static void parseCustomNodeTypeBlock(final Transformation transformationReference, + final Map<String, Object> transformationYaml) { + final Object fromAttribute = transformationYaml.get(FROM.getName()); + if (!(fromAttribute instanceof String)) { + return; + } + final String from = parseStringAttribute(FROM.getName(), transformationYaml).orElse(null); + + final Object toAttribute = transformationYaml.get(TO.getName()); + if (!(toAttribute instanceof String)) { + return; + } + final String to = parseStringAttribute(TO.getName(), transformationYaml).orElse(null); + + final HashMap<String, String> transformationQuery = new HashMap<>(); + transformationQuery.put(ToscaTagNamesEnum.DERIVED_FROM.getElementName(), from); + transformationReference.setConversionQuery(new ConversionQuery(transformationQuery)); + + final List<ConversionDefinition> conversionDefinitionList = new ArrayList<>(); + final HashMap<String, String> conversionDefinitionQuery = new HashMap<>(); + conversionDefinitionQuery.put(ToscaTagNamesEnum.TYPE.getElementName(), null); + ConversionDefinition conversionDefinition = new ConversionDefinition(new ConversionQuery(conversionDefinitionQuery) + , ToscaTagNamesEnum.TYPE.getElementName(), new ReplaceConversionStrategy(from, to)); + conversionDefinitionList.add(conversionDefinition); + transformationReference.setConversionDefinitionList(conversionDefinitionList); } private static List<ConversionDefinition> parseConversions(final Map<String, Object> conversionYaml) { @@ -73,8 +173,18 @@ public class TransformationYamlParser { } return conversionList.stream() - .map(conversion -> ConversionDefinitionYamlParser.parse((Map<String, Object>) conversion)) + .map(conversion -> ConversionDefinitionYamlParser.parse((Map<String, Object>) conversion).orElse(null)) + .filter(Objects::nonNull) .collect(Collectors.toList()); } + private static Optional<String> parseStringAttribute(final String attribute, final Map<String, Object> transformationYaml) { + try { + return Optional.of((String) transformationYaml.get(attribute)); + } catch (final Exception ignore) { + LOGGER.warn("Could not parse the String '{}' in transformation '{}'", attribute, transformationYaml.toString()); + return Optional.empty(); + } + } + } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java index fa1de6751c..b7ba56ee5a 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java @@ -212,7 +212,7 @@ public abstract class AbstractToscaConverter implements ToscaConverter { serviceTemplate.setMetadata(finalMetadata); } - private void convertNodeTypes(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) { + protected void convertNodeTypes(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) { Map<String, Object> nodeTypes = readerService.getNodeTypes(); if (MapUtils.isEmpty(nodeTypes)) { return; diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolModelDrivenConverterPnf.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolModelDrivenConverterPnf.java index ee2c4060e5..b861980f24 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolModelDrivenConverterPnf.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolModelDrivenConverterPnf.java @@ -21,7 +21,9 @@ package org.openecomp.core.impl; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; import org.openecomp.core.converter.ServiceTemplateReaderService; -import org.openecomp.core.converter.impl.pnfd.PnfdTransformationEngine; +import org.openecomp.core.converter.impl.pnfd.PnfdNodeTemplateTransformationEngine; +import org.openecomp.core.converter.impl.pnfd.PnfdNodeTypeTransformationEngine; +import org.openecomp.core.converter.pnfd.PnfdTransformationEngine; public class ToscaSolModelDrivenConverterPnf extends AbstractToscaSolConverter { @@ -31,9 +33,18 @@ public class ToscaSolModelDrivenConverterPnf extends AbstractToscaSolConverter { * @param readerService */ @Override - public void convertTopologyTemplate(final ServiceTemplate serviceTemplate, final ServiceTemplateReaderService readerService) { - final PnfdTransformationEngine pnfdTransformationEngine = new PnfdTransformationEngine(readerService, serviceTemplate); + public void convertTopologyTemplate(final ServiceTemplate serviceTemplate, + final ServiceTemplateReaderService readerService) { + final PnfdTransformationEngine pnfdTransformationEngine = + new PnfdNodeTemplateTransformationEngine(readerService, serviceTemplate); pnfdTransformationEngine.transform(); } + @Override + protected void convertNodeTypes(final ServiceTemplate serviceTemplate, + final ServiceTemplateReaderService readerService) { + final PnfdTransformationEngine pnfdTransformationEngine = + new PnfdNodeTypeTransformationEngine(readerService, serviceTemplate); + pnfdTransformationEngine.transform(); + } }
\ No newline at end of file |