From 5775dd13964a56a60dfbe8fc72e5977672d5acb8 Mon Sep 17 00:00:00 2001 From: aribeiro Date: Wed, 11 Sep 2019 13:50:46 +0100 Subject: Transform custom types for PNFD. Transformation of customized Node Types for PNFD. Issue-ID: SDC-2561 Change-Id: Id3e07edff0e5a8cb134aa12c07878e14c33d1512 Signed-off-by: aribeiro --- .../pnfd/AbstractPnfdTransformationEngine.java | 86 ++++++++ .../pnfd/PnfdNodeTemplateTransformationEngine.java | 167 +++++++++++++++ .../pnfd/PnfdNodeTypeTransformationEngine.java | 72 +++++++ .../pnfd/PnfdTransformationDescriptorReader.java | 4 +- .../impl/pnfd/PnfdTransformationEngine.java | 183 ----------------- .../impl/pnfd/factory/PnfdBlockParserFactory.java | 8 +- .../impl/pnfd/parser/AbstractPnfdBlockParser.java | 226 +++++++++++++++++++++ .../parser/ConversionDefinitionYamlParser.java | 15 +- .../pnfd/parser/ConversionQueryYamlParser.java | 9 +- .../impl/pnfd/parser/NodeTypeYamlParser.java | 67 ++++++ .../pnfd/parser/PnfdCustomNodeTypeBlockParser.java | 88 ++++++++ .../impl/pnfd/parser/PnfdInputBlockParser.java | 1 - .../pnfd/parser/PnfdNodeTemplateBlockParser.java | 43 ++-- .../impl/pnfd/parser/PnfdNodeTypeBlockParser.java | 94 +++++++++ .../impl/pnfd/parser/TransformationYamlParser.java | 132 +++++++++++- .../core/impl/AbstractToscaConverter.java | 2 +- .../core/impl/ToscaSolModelDrivenConverterPnf.java | 17 +- 17 files changed, 987 insertions(+), 227 deletions(-) create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/AbstractPnfdTransformationEngine.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTemplateTransformationEngine.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTypeTransformationEngine.java delete mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationEngine.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/AbstractPnfdBlockParser.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/NodeTypeYamlParser.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdCustomNodeTypeBlockParser.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTypeBlockParser.java (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp') 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> 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/PnfdNodeTemplateTransformationEngine.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTemplateTransformationEngine.java new file mode 100644 index 0000000000..b2038a00dd --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTemplateTransformationEngine.java @@ -0,0 +1,167 @@ +/* + * ============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.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.datatypes.model.TopologyTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; +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; + +/** + * Engine that manages the PNF Descriptor transformation process for the NodeTemplate block. + */ +public class PnfdNodeTemplateTransformationEngine extends AbstractPnfdTransformationEngine { + + public PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom, + final ServiceTemplate templateTo) { + super(templateFrom, templateTo); + } + + //used for tests purposes + PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom, + final ServiceTemplate templateTo, + final String descriptorResourcePath) { + super(templateFrom, templateTo, descriptorResourcePath); + } + + /** + * Runs the transformation process. + */ + public void transform() { + readDefinition(); + initializeTopologyTemplate(); + executeTransformations(); + } + + /** + * Initializes the topology template and its node template set. + */ + private void initializeTopologyTemplate() { + TopologyTemplate topologyTemplate = templateTo.getTopology_template(); + if (topologyTemplate == null) { + topologyTemplate = new TopologyTemplate(); + templateTo.setTopology_template(topologyTemplate); + } + if (topologyTemplate.getNode_templates() == null) { + topologyTemplate.setNode_templates(new HashMap<>()); + } + } + + + /** + * Execute all transformations specified in the descriptor. + */ + @Override + protected void executeTransformations() { + final Set transformationSet = transformationDescription.getTransformationSet(); + if (CollectionUtils.isEmpty(transformationSet)) { + return; + } + transformationGroupByBlockMap = transformationSet.stream() + .filter(Transformation::isValid) + .collect(Collectors.groupingBy(Transformation::getBlock)); + executeCustomTypeTransformations(); + final Map inputsToConvertMap = executeNodeTemplateTransformations(); + executeGetInputFunctionTransformations(inputsToConvertMap); + } + + /** + * Parses all topology_template node_template. + * @return + * A map containing any input that was called with a get_input TOSCA function and its getInputFunction + * transformation name + */ + private Map executeNodeTemplateTransformations() { + final List transformationList = transformationGroupByBlockMap + .get(TransformationBlock.NODE_TEMPLATE); + if (CollectionUtils.isEmpty(transformationList)) { + return Collections.emptyMap(); + } + + final Map inputsToConvertMap = new HashMap<>(); + transformationList.forEach(transformation -> + PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfParser -> { + pnfParser.parse(templateFrom, templateTo); + if (pnfParser.getInputAndTransformationNameMap().isPresent()) { + inputsToConvertMap.putAll(pnfParser.getInputAndTransformationNameMap().get()); + } + })); + return inputsToConvertMap; + } + + /** + * Parses all topology_template inputs called with a get_input TOSCA function. + * @param inputsToConvertMap A map containing the topology_template input name and its conversion definition name + */ + private void executeGetInputFunctionTransformations(final Map inputsToConvertMap) { + final List transformationListOfGetInputFunction = transformationGroupByBlockMap + .get(TransformationBlock.GET_INPUT_FUNCTION); + + if(MapUtils.isEmpty(inputsToConvertMap) || CollectionUtils.isEmpty(transformationListOfGetInputFunction)) { + return; + } + + final Map> transformationByName = transformationListOfGetInputFunction.stream() + .collect(Collectors.groupingBy(Transformation::getName)); + + inputsToConvertMap.forEach((inputName, transformationName) -> { + final List transformationList = transformationByName.get(transformationName); + if (!CollectionUtils.isEmpty(transformationList)) { + final Transformation transformation = transformationList.stream() + .findFirst().orElse(null); + if (transformation != null) { + final Map conversionQueryMap = new HashMap<>(); + conversionQueryMap.put(inputName, null); + transformation.setConversionQuery( + ConversionQueryYamlParser.parse(conversionQueryMap).orElse(null) + ); + PnfdBlockParserFactory.getInstance().get(transformation) + .ifPresent(pnfParser -> pnfParser.parse(templateFrom, templateTo)); + } + } + }); + } + + /** + * Parses a Customized Node Type that extend from a valid ONAP NodeType. + */ + private void executeCustomTypeTransformations() { + final List transformationList = transformationGroupByBlockMap + .get((TransformationBlock.CUSTOM_NODE_TYPE)); + if (CollectionUtils.isEmpty(transformationList)) { + return; + } + 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 transformationSet = transformationDescription.getTransformationSet(); + if (CollectionUtils.isEmpty(transformationSet)) { + return; + } + final Set 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) conversionMap)) + .map(conversionMap -> TransformationYamlParser.parse((Map) 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/PnfdTransformationEngine.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationEngine.java deleted file mode 100644 index a2365b4f8a..0000000000 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationEngine.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * ============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.InputStream; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.onap.sdc.tosca.datatypes.model.TopologyTemplate; -import org.openecomp.core.converter.ServiceTemplateReaderService; -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. - */ -public class PnfdTransformationEngine { - - 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> transformationGroupByBlockMap; - private final String descriptorResourcePath; - - public PnfdTransformationEngine(final ServiceTemplateReaderService templateFrom, final ServiceTemplate templateTo) { - this(templateFrom, templateTo, "pnfdTransformationTemplate/model-driven-conversion.yaml"); - } - - //used for tests purposes - PnfdTransformationEngine(final ServiceTemplateReaderService templateFrom, final ServiceTemplate templateTo, - final String descriptorResourcePath) { - this.templateFrom = templateFrom; - this.templateTo = templateTo; - this.descriptorResourcePath = descriptorResourcePath; - } - - /** - * Runs the transformation process. - */ - public void transform() { - readDefinition(); - initializeTopologyTemplate(); - executeTransformations(); - } - - /** - * Initializes the topology template and its node template set. - */ - private void initializeTopologyTemplate() { - TopologyTemplate topologyTemplate = templateTo.getTopology_template(); - if (topologyTemplate == null) { - topologyTemplate = new TopologyTemplate(); - templateTo.setTopology_template(topologyTemplate); - } - if (topologyTemplate.getNode_templates() == null) { - topologyTemplate.setNode_templates(new HashMap<>()); - } - } - - /** - * Reads the transformation description yaml file. - */ - private void readDefinition() { - transformationDescription = pnfdTransformationDescriptorReader.parse(getDefinitionFileInputStream()); - } - - /** - * Execute all transformations specified in the descriptor. - */ - private void executeTransformations() { - final Set transformationSet = transformationDescription.getTransformationSet(); - if (CollectionUtils.isEmpty(transformationSet)) { - return; - } - transformationGroupByBlockMap = transformationSet.stream() - .filter(Transformation::isValid) - .collect(Collectors.groupingBy(Transformation::getBlock)); - final Map inputsToConvertMap = executeNodeTemplateTransformations(); - executeGetInputFunctionTransformations(inputsToConvertMap); - } - - /** - * Parses all topology_template node_template. - * @return - * A map containing any input that was called with a get_input TOSCA function and its getInputFunction - * transformation name - */ - private Map executeNodeTemplateTransformations() { - final List transformationList = transformationGroupByBlockMap - .get(TransformationBlock.NODE_TEMPLATE); - if (CollectionUtils.isEmpty(transformationList)) { - return Collections.emptyMap(); - } - - final Map inputsToConvertMap = new HashMap<>(); - transformationList.forEach(transformation -> - PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfParser -> { - pnfParser.parse(templateFrom, templateTo); - if (pnfParser.getInputAndTransformationNameMap().isPresent()) { - inputsToConvertMap.putAll(pnfParser.getInputAndTransformationNameMap().get()); - } - })); - return inputsToConvertMap; - } - - /** - * Parses all topology_template inputs called with a get_input TOSCA function. - * @param inputsToConvertMap A map containing the topology_template input name and its conversion definition name - */ - private void executeGetInputFunctionTransformations(final Map inputsToConvertMap) { - final List transformationListOfGetInputFunction = transformationGroupByBlockMap - .get(TransformationBlock.GET_INPUT_FUNCTION); - - if(MapUtils.isEmpty(inputsToConvertMap) || CollectionUtils.isEmpty(transformationListOfGetInputFunction)) { - return; - } - - final Map> transformationByName = transformationListOfGetInputFunction.stream() - .collect(Collectors.groupingBy(Transformation::getName)); - - inputsToConvertMap.forEach((inputName, transformationName) -> { - final List transformationList = transformationByName.get(transformationName); - if (!CollectionUtils.isEmpty(transformationList)) { - final Transformation transformation = transformationList.stream() - .findFirst().orElse(null); - if (transformation != null) { - final Map conversionQueryMap = new HashMap<>(); - conversionQueryMap.put(inputName, null); - transformation.setConversionQuery(ConversionQueryYamlParser.parse(conversionQueryMap)); - PnfdBlockParserFactory.getInstance().get(transformation) - .ifPresent(pnfParser -> pnfParser.parse(templateFrom, templateTo)); - } - } - }); - } - - /** - * Gets the transformation definition yaml file path. - * @return The transformation definition yaml path. - */ - 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; - } - return resourceAsStream; - } -} 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> 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 blockYamlObject) { + if (MapUtils.isEmpty(blockYamlObject)) { + return; + } + final List conversionDefinitionList = transformation.getConversionDefinitionList(); + final Map parsedBlockYamlObject = new HashMap<>(); + final String blockName = blockYamlObject.keySet().iterator().next(); + conversionDefinitionList.stream() + .filter(conversionDefinition -> conversionDefinition.getConversionQuery().isValidAttributeQuery()) + .forEach(conversionDefinition -> { + final Map query = + (Map) conversionDefinition.getConversionQuery().getQuery(); + final Map blockAttributeMap = (Map) blockYamlObject.get(blockName); + final Optional> 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 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> buildParsedBlock(final Map attributeQuery, + final Map 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 mergeYamlObjects(final Map originalMap, + final Map 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> 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 yamlMap = (Map) 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 yamlMap = (Map) 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> 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 rootNodeTypeMap, + final Map 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 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 fetchCustomNodeType() { + final Map nodeTypesMap = templateFrom.getNodeTypes(); + if (MapUtils.isEmpty(nodeTypesMap)) { + return Collections.emptyMap(); + } + final ConversionQuery conversionQuery = transformation.getConversionQuery(); + final Map 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 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 conversionYaml) { + public static Optional parse(final Map 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) 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 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 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) nodeTypeYaml.get(ToscaTagNamesEnum.PROPERTIES.getElementName())); + nodeType.setArtifacts((Map) nodeTypeYaml.get("artifacts")); + nodeType.setMetadata((Map) nodeTypeYaml.get("metadata")); + nodeType.setInterfaces( + (Map) nodeTypeYaml.get(ToscaTagNamesEnum.INTERFACES.getElementName())); + nodeType.setRequirements( + (List>) nodeTypeYaml.get(ToscaTagNamesEnum.REQUIREMENTS.getElementName())); + nodeType.setCapabilities( + (Map) nodeTypeYaml.get(ToscaTagNamesEnum.CAPABILITIES.getElementName())); + nodeType.setAttributes( + (Map) 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> findBlocksToParse() { + final Map nodeTemplateMap = templateFrom.getNodeTemplates(); + final Map customNodeTypeMap = fetchCustomNodeType(); + if (customNodeTypeMap.isEmpty() || MapUtils.isEmpty(nodeTemplateMap)) { + return Collections.emptySet(); + } + return customNodeTypeMap.entrySet().stream() + .map(customNode -> { + final Map 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> buildParsedBlock(final Map attributeQuery, + final Map 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 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 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> buildParsedBlock(final Map attributeQuery, + @Override + protected Set> findBlocksToParse() { + final ConversionQuery conversionQuery = transformation.getConversionQuery(); + final Map 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 map = new HashMap<>(); + map.put(stringObjectEntry.getKey(), stringObjectEntry.getValue()); + return map; + }).collect(Collectors.toSet()); + } + + @Override + protected Optional> buildParsedBlock(final Map attributeQuery, final Map 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> findBlocksToParse() { - final ConversionQuery conversionQuery = transformation.getConversionQuery(); - final Map 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 map = new HashMap<>(); - map.put(stringObjectEntry.getKey(), stringObjectEntry.getValue()); - return map; - }).collect(Collectors.toSet()); - } - @Override - public void write(final String nodeTemplateName, final Map parsedNodeTemplateMap) { + protected void write(final String nodeTemplateName, final Map 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> findBlocksToParse() { + final Map 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 map = new HashMap<>(); + map.put(nodeNamePrefix.concat(nodeEntry.getKey()), nodeEntry.getValue()); + return map; + }).collect(Collectors.toSet()); + } + + @Override + protected Optional> buildParsedBlock(final Map attributeQuery, + final Map 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 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 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 transformationYaml) { + public static Optional parse(final Map transformationYaml) { final Transformation transformation = new Transformation(); - transformation.setName((String) transformationYaml.get(NAME.getName())); - transformation.setDescription((String) transformationYaml.get(DESCRIPTION.getName())); + final Optional 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 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 readProperties(final Map transformationYaml) { + final Map propertyMap = (Map) transformationYaml.get(PROPERTIES.getName()); + if (MapUtils.isEmpty(propertyMap)) { + return Collections.emptySet(); + } + + final Set 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 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 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 transformationQuery = new HashMap<>(); + transformationQuery.put(ToscaTagNamesEnum.DERIVED_FROM.getElementName(), from); + transformationReference.setConversionQuery(new ConversionQuery(transformationQuery)); + + final List conversionDefinitionList = new ArrayList<>(); + final HashMap 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 parseConversions(final Map conversionYaml) { @@ -73,8 +173,18 @@ public class TransformationYamlParser { } return conversionList.stream() - .map(conversion -> ConversionDefinitionYamlParser.parse((Map) conversion)) + .map(conversion -> ConversionDefinitionYamlParser.parse((Map) conversion).orElse(null)) + .filter(Objects::nonNull) .collect(Collectors.toList()); } + private static Optional parseStringAttribute(final String attribute, final Map 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 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 -- cgit 1.2.3-korg