From 7de4f3e788ff2b71936ae43be7dfbdf48740f9be Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Tue, 3 Sep 2019 13:45:28 +0000 Subject: Implement PNFD Model driven conversion Change-Id: I7427e82353cac17817457294d78953f867d2d6e8 Issue-ID: SDC-2509 Signed-off-by: andre.schmid --- .../converter/impl/pnfd/PnfdQueryExecutor.java | 113 +++++++++ .../pnfd/PnfdTransformationDescriptorReader.java | 89 +++++++ .../impl/pnfd/PnfdTransformationEngine.java | 183 ++++++++++++++ .../impl/pnfd/factory/PnfdBlockParserFactory.java | 57 +++++ .../parser/ConversionDefinitionYamlParser.java | 55 +++++ .../pnfd/parser/ConversionQueryYamlParser.java | 42 ++++ .../impl/pnfd/parser/NodeTemplateYamlParser.java | 55 +++++ .../pnfd/parser/ParameterDefinitionYamlParser.java | 57 +++++ .../parser/PnfdConversionStrategyYamlParser.java | 74 ++++++ .../impl/pnfd/parser/PnfdInputBlockParser.java | 94 +++++++ .../pnfd/parser/PnfdNodeTemplateBlockParser.java | 114 +++++++++ .../impl/pnfd/parser/TransformationYamlParser.java | 80 ++++++ .../impl/pnfd/strategy/CopyConversionStrategy.java | 37 +++ .../pnfd/strategy/ReplaceConversionStrategy.java | 39 +++ .../strategy/ReplaceInListConversionStrategy.java | 60 +++++ .../openecomp/core/impl/ToscaSolConverterPnf.java | 271 --------------------- .../core/impl/ToscaSolModelDrivenConverterPnf.java | 39 +++ 17 files changed, 1188 insertions(+), 271 deletions(-) create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdQueryExecutor.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationDescriptorReader.java create 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/factory/PnfdBlockParserFactory.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/ConversionDefinitionYamlParser.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/ConversionQueryYamlParser.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/NodeTemplateYamlParser.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/ParameterDefinitionYamlParser.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/PnfdConversionStrategyYamlParser.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/PnfdInputBlockParser.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/PnfdNodeTemplateBlockParser.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/TransformationYamlParser.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/CopyConversionStrategy.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/ReplaceConversionStrategy.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/ReplaceInListConversionStrategy.java delete mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterPnf.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolModelDrivenConverterPnf.java (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java') diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdQueryExecutor.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdQueryExecutor.java new file mode 100644 index 0000000000..890dedb2a7 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdQueryExecutor.java @@ -0,0 +1,113 @@ +/* + * ============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.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import org.apache.commons.collections.MapUtils; +import org.openecomp.core.converter.pnfd.exception.QueryOperationNotSupportedException; +import org.openecomp.core.converter.pnfd.model.ConversionQuery; + +/** + * Runs YAML queries + */ +public class PnfdQueryExecutor { + + private PnfdQueryExecutor() { + + } + + /** + * Finds if a YAML object contains the provided YAML query. + * @param conversionQuery The query + * @param yamlObject The YAML object to be searched + * @return + * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise. + */ + public static boolean find(final ConversionQuery conversionQuery, final Object yamlObject) { + return find(conversionQuery.getQuery(), yamlObject); + } + + /** + * Recursive structure combined with {@link #findMap(Map, Map)} to find if a YAML object contains the provided YAML query. + * Compares the objects if it's a scalar value, otherwise go further in the YAML hierarchical structure + * calling the {@link #findMap(Map, Map)}. + * @param query The current query + * @param yamlObject The current YAML object to be searched + * @return + * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise. + */ + private static boolean find(final Object query, final Object yamlObject) { + if (query == null) { + return true; + } + + checkSupportedQueryType(query); + + if (query instanceof String) { + return query.equals(yamlObject); + } + if (query instanceof Map) { + return findMap((Map) query, (Map) yamlObject); + } + return false; + } + + /** + * Recursive structure combined with {@link #find(Object, Object)} to find if a YAML object contains the provided YAML query. * + * @param query The query current object + * @param yamlObject The YAML object to be searched + * @return + * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise. + */ + private static boolean findMap(final Map query, final Map yamlObject) { + if (MapUtils.isEmpty(query) || MapUtils.isEmpty(yamlObject)) { + return false; + } + + if (!yamlObject.keySet().containsAll(query.keySet())) { + return false; + } + + return query.entrySet().parallelStream().allMatch(queryEntryObj -> { + final Entry queryEntry = (Entry) queryEntryObj; + return find(queryEntry.getValue(), yamlObject.get(queryEntry.getKey())); + }); + } + + /** + * Checks the supported types for a query. + * @param query the query to check + */ + private static void checkSupportedQueryType(final Object query) { + if (query instanceof String || query instanceof Map) { + return; + } + if (query instanceof List || query instanceof Set) { + throw new QueryOperationNotSupportedException("Yaml list query is not supported yet"); + } + throw new QueryOperationNotSupportedException( + String.format("Yaml query operation for '%s' is not supported yet", query.getClass()) + ); + } + +} 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 new file mode 100644 index 0000000000..6ef5caf21b --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationDescriptorReader.java @@ -0,0 +1,89 @@ +/* + * ============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.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +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.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.converter.impl.pnfd.parser.TransformationYamlParser; +import org.openecomp.core.converter.pnfd.model.Transformation; +import org.openecomp.core.converter.pnfd.model.TransformationDescription; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +/** + * Reads the PNF Transformation Description + */ +public class PnfdTransformationDescriptorReader { + + private static final Logger LOGGER = LoggerFactory.getLogger(PnfdTransformationDescriptorReader.class); + + /** + * Parse the transformation description to {@link TransformationDescription} class. + * @return + * The {@link TransformationDescription} instance. + */ + public TransformationDescription parse(final InputStream transformationDescriptionInputStream) { + final List transformationList = readDescriptionYaml(transformationDescriptionInputStream); + final Set transformationSet = parseTransformationList(transformationList); + + return new TransformationDescription(transformationSet); + } + + /** + * Reads the description file that has the required YAML format. + * @return + * The yaml parsed to Object + */ + private List readDescriptionYaml(final InputStream transformationDescriptionPath) { + try (final InputStream fileInputStream = transformationDescriptionPath) { + return YamlUtil.yamlToList(fileInputStream).orElse(Collections.emptyList()); + } catch (final FileNotFoundException e) { + LOGGER.error("Could not find the resource on path.", e); + } catch (final IOException e) { + LOGGER.error("Could not load resource.", e); + } + return Collections.emptyList(); + } + + /** + * Parse the transformation list represented in a YAML object to {@link Transformation}. + * @param transformationYamlList the YAML object read from the transformation description file + * @return + * The set of transformations represented as {@link Transformation} class + */ + private Set parseTransformationList(final List transformationYamlList) { + if (CollectionUtils.isEmpty(transformationYamlList)) { + return Collections.emptySet(); + } + + return transformationYamlList.stream() + .map(conversionMap -> TransformationYamlParser.parse((Map) conversionMap)) + .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 new file mode 100644 index 0000000000..a2365b4f8a --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdTransformationEngine.java @@ -0,0 +1,183 @@ +/* + * ============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 new file mode 100644 index 0000000000..512fdb5ca7 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/factory/PnfdBlockParserFactory.java @@ -0,0 +1,57 @@ +/* + * ============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.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.pnfd.parser.PnfdBlockParser; + +/** + * Factory for {@link PnfdBlockParser}. + */ +public class PnfdBlockParserFactory { + private static PnfdBlockParserFactory instance; + + private PnfdBlockParserFactory() { + } + + public static PnfdBlockParserFactory getInstance() { + if (instance == null) { + instance = new PnfdBlockParserFactory(); + } + + return instance; + } + + public Optional get(final Transformation transformation) { + switch (transformation.getBlock()) { + case NODE_TEMPLATE: + return Optional.of(new PnfdNodeTemplateBlockParser(transformation)); + case INPUT: + case GET_INPUT_FUNCTION: + return Optional.of(new PnfdInputBlockParser(transformation)); + default: + return Optional.empty(); + } + } + +} 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 new file mode 100644 index 0000000000..143185210e --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionDefinitionYamlParser.java @@ -0,0 +1,55 @@ +/* + * ============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.Map; +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; + +/** + * Handles YAML from/to {@link ConversionDefinition} conversions + */ +public class ConversionDefinitionYamlParser { + + private ConversionDefinitionYamlParser() { + + } + + /** + * Parses the given a YAML object to a {@link ConversionDefinition} instance. + * @param conversionYaml the YAML object representing a conversion definition + * @return + * A new instance of {@link ConversionDefinition}. + */ + public static ConversionDefinition parse(final Map conversionYaml) { + final ConversionQuery conversionQuery = ConversionQueryYamlParser + .parse(conversionYaml.get(PnfTransformationToken.QUERY.getName())); + 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); + } + +} 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 new file mode 100644 index 0000000000..417262c61c --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ConversionQueryYamlParser.java @@ -0,0 +1,42 @@ +/* + * ============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 org.openecomp.core.converter.pnfd.model.ConversionQuery; + +/** + * Handles YAML from/to {@link ConversionQuery} conversions + */ +public class ConversionQueryYamlParser { + + private ConversionQueryYamlParser() { + + } + + /** + * Parses the given a YAML object to a {@link ConversionQuery} instance. + * @param conversionYaml the YAML object representing a conversion query + * @return + * A new instance of {@link ConversionQuery}. + */ + public static ConversionQuery parse(final Object conversionYaml) { + return 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/NodeTemplateYamlParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/NodeTemplateYamlParser.java new file mode 100644 index 0000000000..fdf276f37d --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/NodeTemplateYamlParser.java @@ -0,0 +1,55 @@ +/* + * ============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.NodeTemplate; + +/** + * Handles YAML from/to {@link NodeTemplate} conversions + */ +public class NodeTemplateYamlParser { + + private NodeTemplateYamlParser() { + } + + /** + * Parses the given a YAML object to a {@link NodeTemplate} instance. + * @param nodeTemplateYaml the YAML object representing a TOSCA Node Template + * @return + * A new instance of {@link NodeTemplate}. + */ + public static NodeTemplate parse(final Map nodeTemplateYaml) { + final NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setType((String) nodeTemplateYaml.get("type")); + nodeTemplate.setDescription((String) nodeTemplateYaml.get("description")); + nodeTemplate.setCopy((String) nodeTemplateYaml.get("copy")); + nodeTemplate.setProperties((Map) nodeTemplateYaml.get("properties")); + nodeTemplate.setAttributes((Map) nodeTemplateYaml.get("attributes")); + nodeTemplate.setDirectives((List) nodeTemplateYaml.get("directives")); + nodeTemplate.setMetadata((Map) nodeTemplateYaml.get("metadata")); + nodeTemplate.setInterfaces((Map) nodeTemplateYaml.get("interfaces")); + + return nodeTemplate; + } + + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ParameterDefinitionYamlParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ParameterDefinitionYamlParser.java new file mode 100644 index 0000000000..d103326c11 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/ParameterDefinitionYamlParser.java @@ -0,0 +1,57 @@ +/* + * ============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.Map; +import org.onap.sdc.tosca.datatypes.model.EntrySchema; +import org.onap.sdc.tosca.datatypes.model.ParameterDefinition; +import org.onap.sdc.tosca.datatypes.model.Status; + +/** + * Handles YAML from/to {@link ParameterDefinition} conversions + */ +public class ParameterDefinitionYamlParser { + + private ParameterDefinitionYamlParser() { + } + + /** + * Parses the given a YAML object to a {@link ParameterDefinition} instance. + * @param parameterDefinitionYaml the YAML object representing a TOSCA Parameter Definition + * @return + * A new instance of {@link ParameterDefinition}. + */ + public static ParameterDefinition parse(final Map parameterDefinitionYaml) { + final ParameterDefinition parameterDefinition = new ParameterDefinition(); + parameterDefinition.set_default(parameterDefinitionYaml.get("default")); + parameterDefinition.setDescription((String) parameterDefinitionYaml.get("description")); + final Map entrySchemaYaml = (Map) parameterDefinitionYaml.get("entry_schema"); + if (entrySchemaYaml != null) { + final EntrySchema entrySchema = new EntrySchema(); + entrySchema.setType((String) entrySchemaYaml.get("type")); + parameterDefinition.setEntry_schema(entrySchema); + } + parameterDefinition.setRequired((Boolean) parameterDefinitionYaml.get("required")); + parameterDefinition.setType((String) parameterDefinitionYaml.get("type")); + parameterDefinition.setStatus((Status) parameterDefinitionYaml.get("status")); + + return parameterDefinition; + } +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdConversionStrategyYamlParser.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdConversionStrategyYamlParser.java new file mode 100644 index 0000000000..dee16749ae --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdConversionStrategyYamlParser.java @@ -0,0 +1,74 @@ +/* + * ============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 java.util.Optional; +import org.openecomp.core.converter.pnfd.model.ConversionStrategyType; +import org.openecomp.core.converter.pnfd.model.PnfTransformationToken; +import org.openecomp.core.converter.impl.pnfd.strategy.CopyConversionStrategy; +import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy; +import org.openecomp.core.converter.impl.pnfd.strategy.ReplaceConversionStrategy; +import org.openecomp.core.converter.impl.pnfd.strategy.ReplaceInListConversionStrategy; + + +/** + * Handles YAML from/to {@link PnfdConversionStrategy} conversions. + */ +public class PnfdConversionStrategyYamlParser { + + private PnfdConversionStrategyYamlParser() { + + } + + /** + * Parses the given YAML object to a {@link PnfdConversionStrategy} instance. + * @param strategyYaml the YAML object representing a conversion strategy + * @return + * A new instance of {@link PnfdConversionStrategy}. + */ + public static Optional parse(final Map strategyYaml) { + final Optional optionalStrategy = ConversionStrategyType.parse( + (String) strategyYaml.get(PnfTransformationToken.STRATEGY.getName()) + ); + + if (!optionalStrategy.isPresent()) { + return Optional.empty(); + } + + final ConversionStrategyType strategyType = optionalStrategy.get(); + if (strategyType == ConversionStrategyType.COPY) { + return Optional.of(new CopyConversionStrategy()); + } + if (strategyType == ConversionStrategyType.REPLACE) { + final Object from = strategyYaml.get(PnfTransformationToken.FROM.getName()); + final Object to = strategyYaml.get(PnfTransformationToken.TO.getName()); + return Optional.of(new ReplaceConversionStrategy(from, to)); + } + if (strategyType == ConversionStrategyType.REPLACE_IN_LIST) { + return Optional.of(new ReplaceInListConversionStrategy( + (List>) strategyYaml.get(PnfTransformationToken.LIST.getName())) + ); + } + return Optional.empty(); + } + +} 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 new file mode 100644 index 0000000000..4aec8c8ac3 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdInputBlockParser.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 com.google.common.collect.ImmutableMap; +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.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; +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 PnfdInputBlockParser extends AbstractPnfdBlockParser { + + public PnfdInputBlockParser(final Transformation transformation) { + super(transformation); + } + + @Override + protected Optional> buildParsedBlock(final Map attributeQuery, + final Map originalAttributeMap, 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 Map parsedInput = new HashMap<>(); + if (attributeQuery.get(attribute) == null) { + final PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy(); + final Optional convertedAttribute = pnfdConversionStrategy.convert(originalAttributeMap.get(attribute)); + convertedAttribute.ifPresent(convertedAttribute1 -> parsedInput.put(conversionDefinition.getToAttributeName(), convertedAttribute1)); + } else { + final Optional> builtInput = buildParsedBlock((Map) attributeQuery.get(attribute), + (Map) originalAttributeMap.get(attribute), conversionDefinition); + builtInput.ifPresent(builtInput1 -> parsedInput.put(attribute, builtInput1)); + } + + return parsedInput.isEmpty() ? Optional.empty() : Optional.of(parsedInput); + } + + @Override + protected void write(final String blockName, final Map parsedBlockYamlObject) { + if (!parsedBlockYamlObject.isEmpty()) { + final ParameterDefinition parameterDefinition = ParameterDefinitionYamlParser.parse(parsedBlockYamlObject); + DataModelUtil.addInputParameterToTopologyTemplate(templateTo, blockName, parameterDefinition); + } + } + + @Override + protected Set> findBlocksToParse() { + final ConversionQuery conversionQuery = transformation.getConversionQuery(); + final Map inputsMap = templateFrom.getInputs(); + if (MapUtils.isEmpty(inputsMap)) { + return Collections.emptySet(); + } + + return inputsMap.entrySet().stream() + .filter(inputMapEntry -> PnfdQueryExecutor + .find(conversionQuery, ImmutableMap.of(inputMapEntry.getKey(), inputMapEntry.getValue())) + ) + .map(inputMapEntry -> { + final Map map = new HashMap<>(); + map.put(inputMapEntry.getKey(), inputMapEntry.getValue()); + return map; + }).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/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 new file mode 100644 index 0000000000..5539840561 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/PnfdNodeTemplateBlockParser.java @@ -0,0 +1,114 @@ +/* + * ============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.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; +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; + +public class PnfdNodeTemplateBlockParser extends AbstractPnfdBlockParser { + + private Map inputNameToConvertMap = new HashMap<>(); + + public PnfdNodeTemplateBlockParser(final Transformation transformation) { + super(transformation); + } + + public 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); + if (queryValue == null) { + PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy(); + if (isGetInputFunction(attributeValueToConvert)) { + inputNameToConvertMap.put(extractGetInputFunctionValue(attributeValueToConvert) + , conversionDefinition.getToGetInput() + ); + pnfdConversionStrategy = new CopyConversionStrategy(); + } + final Map parsedNodeTemplate = new HashMap<>(); + final Optional convertedAttribute = pnfdConversionStrategy.convert(attributeValueToConvert); + if (convertedAttribute.isPresent()) { + parsedNodeTemplate.put(conversionDefinition.getToAttributeName(), convertedAttribute.get()); + } + + return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate); + } else { + if (!(queryValue instanceof Map) || !(attributeValueToConvert instanceof Map)) { + return Optional.empty(); + } + final Map parsedNodeTemplate = new HashMap<>(); + final Optional> builtNodeTemplate = buildParsedBlock( + (Map) queryValue, + (Map) attributeValueToConvert, conversionDefinition); + builtNodeTemplate.ifPresent(builtNodeTemplate1 -> parsedNodeTemplate.put(attribute, builtNodeTemplate1)); + + return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate); + } + } + + 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) { + if (!parsedNodeTemplateMap.isEmpty()) { + final NodeTemplate parsedNodeTemplate = NodeTemplateYamlParser.parse(parsedNodeTemplateMap); + DataModelUtil.addNodeTemplate(templateTo, nodeTemplateName, parsedNodeTemplate); + } + } + + @Override + public Optional> getInputAndTransformationNameMap() { + return Optional.of(inputNameToConvertMap); + } +} 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 new file mode 100644 index 0000000000..16d06704b3 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/TransformationYamlParser.java @@ -0,0 +1,80 @@ +/* + * ============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.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.NAME; +import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.QUERY; +import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.TRANSFORMATION_FOR; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.commons.collections.CollectionUtils; +import org.openecomp.core.converter.pnfd.model.ConversionDefinition; +import org.openecomp.core.converter.pnfd.model.Transformation; +import org.openecomp.core.converter.pnfd.model.TransformationBlock; + +/** + * Handles YAML from/to {@link Transformation} conversions + */ +public class TransformationYamlParser { + + private TransformationYamlParser() { + + } + + /** + * Parses the given YAML object to a {@link Transformation} instance. + * @param transformationYaml the YAML object representing a transformation + * @return + * A new instance of {@link Transformation}. + */ + public static Transformation parse(final Map transformationYaml) { + final Transformation transformation = new Transformation(); + transformation.setName((String) transformationYaml.get(NAME.getName())); + transformation.setDescription((String) transformationYaml.get(DESCRIPTION.getName())); + + final String block = (String) transformationYaml.get(TRANSFORMATION_FOR.getName()); + transformation.setBlock(TransformationBlock.parse(block).orElse(null)); + + transformation.setConversionQuery( + ConversionQueryYamlParser.parse(transformationYaml.get(QUERY.getName())) + ); + transformation.setConversionDefinitionList(parseConversions(transformationYaml)); + + return transformation; + } + + private static List parseConversions(final Map conversionYaml) { + final List conversionList = (List) conversionYaml.get(CONVERSIONS.getName()); + + if (CollectionUtils.isEmpty(conversionList)) { + return Collections.emptyList(); + } + + return conversionList.stream() + .map(conversion -> ConversionDefinitionYamlParser.parse((Map) conversion)) + .collect(Collectors.toList()); + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/CopyConversionStrategy.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/CopyConversionStrategy.java new file mode 100644 index 0000000000..c379dfa85c --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/CopyConversionStrategy.java @@ -0,0 +1,37 @@ +/* + * ============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.strategy; + +import java.util.HashMap; +import java.util.Optional; +import org.openecomp.core.converter.pnfd.strategy.AbstractPnfdConversionStrategy; +import org.openecomp.core.converter.pnfd.model.ConversionStrategyType; + +public class CopyConversionStrategy extends AbstractPnfdConversionStrategy { + + public CopyConversionStrategy() { + super(ConversionStrategyType.COPY, new HashMap<>()); + } + + @Override + public Optional convert(final Object originalValue) { + return Optional.ofNullable(originalValue); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/ReplaceConversionStrategy.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/ReplaceConversionStrategy.java new file mode 100644 index 0000000000..3bde6326b7 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/ReplaceConversionStrategy.java @@ -0,0 +1,39 @@ +/* + * ============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.strategy; + +import java.util.HashMap; +import java.util.Optional; +import org.openecomp.core.converter.pnfd.strategy.AbstractPnfdConversionStrategy; +import org.openecomp.core.converter.pnfd.model.ConversionStrategyType; + +public class ReplaceConversionStrategy extends AbstractPnfdConversionStrategy { + + public ReplaceConversionStrategy(final Object from, final Object to) { + super(ConversionStrategyType.REPLACE, new HashMap<>()); + conversionMap.put(from, to); + } + + @Override + public Optional convert(final Object originalValue) { + return Optional.ofNullable(conversionMap.get(originalValue)); + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/ReplaceInListConversionStrategy.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/ReplaceInListConversionStrategy.java new file mode 100644 index 0000000000..9984a8708d --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/strategy/ReplaceInListConversionStrategy.java @@ -0,0 +1,60 @@ +/* + * ============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.strategy; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; +import org.openecomp.core.converter.pnfd.strategy.AbstractPnfdConversionStrategy; +import org.openecomp.core.converter.pnfd.model.ConversionStrategyType; +import org.openecomp.core.converter.pnfd.model.PnfTransformationToken; + +public class ReplaceInListConversionStrategy extends AbstractPnfdConversionStrategy, List> { + + private final Map replaceInListConversionStrategyMap; + + public ReplaceInListConversionStrategy(final List> descriptorList) { + super(ConversionStrategyType.REPLACE_IN_LIST, new HashMap<>()); + replaceInListConversionStrategyMap = new LinkedHashMap<>(); + descriptorList + .forEach(stringObjectMap -> replaceInListConversionStrategyMap.put(stringObjectMap.get( + PnfTransformationToken.FROM.getName()) + , stringObjectMap.get(PnfTransformationToken.TO.getName())) + ); + } + + @Override + public Optional> convert(final List originalValue) { + if (originalValue == null || originalValue.isEmpty()) { + return Optional.empty(); + } + + final List convertedList = originalValue.stream() + .map(replaceInListConversionStrategyMap::get) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + return convertedList.isEmpty() ? Optional.empty() : Optional.of(convertedList); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterPnf.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterPnf.java deleted file mode 100644 index a3935ee735..0000000000 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterPnf.java +++ /dev/null @@ -1,271 +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.impl; - -import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CONSTRAINTS; -import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE; -import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION; -import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA; -import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GET_INPUT; -import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.PROPERTIES; -import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.REQUIRED; -import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE; - -import java.util.AbstractMap; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.stream.Collectors; -import org.apache.commons.collections.MapUtils; -import org.onap.sdc.tosca.datatypes.model.Constraint; -import org.onap.sdc.tosca.datatypes.model.EntrySchema; -import org.onap.sdc.tosca.datatypes.model.NodeTemplate; -import org.onap.sdc.tosca.datatypes.model.ParameterDefinition; -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.sdc.tosca.services.DataModelUtil; - -public class ToscaSolConverterPnf extends AbstractToscaSolConverter { - - private static final String PNF_EXT_CP_TYPE = "tosca.nodes.nfv.PnfExtCp"; - private static final String EXT_CP_TYPE = "org.openecomp.resource.cp.v2.extCP"; - private static final String LAYER_PROTOCOLS = "layer_protocols"; - private static final String ASSIGNMENT_METHOD = "assingment_method"; - private static final String DHCP = "dhcp"; - private static final String IP_V4 = "ipv4"; - private static final String IP_V6 = "ipv6"; - private static final String IP_VERSION = "ip_version"; - private static final String IP_REQUIREMENTS = "ip_requirements"; - private static final String IP_REQUIREMENTS_TYPE = "org.openecomp.datatypes.network.IpRequirements"; - private ServiceTemplate serviceTemplate; - private ServiceTemplateReaderService readerService; - - /** - * For PNF the node templates are converted ETSI node types to ecomp node types. All other data i.e. inputs, - * substitution mappings and outputs are simply dropped at this stage. The equivalent ecomp data will be added when - * the vsp is imported into the catalog. - * - * @param serviceTemplate - the service template - * @param readerService - the reader service - */ - @Override - public void convertTopologyTemplate(final ServiceTemplate serviceTemplate, - final ServiceTemplateReaderService readerService) { - this.serviceTemplate = serviceTemplate; - this.readerService = readerService; - convertNodeTemplatesToEcompTypes(); - addEmptyNodeTemplatesIfNoneDefined(); - } - - /** - * Copies a input from the reader (input) to the service template (output) - * @param inputName the name of the input to copy - */ - private void copyTopologyTemplateInput(final String inputName) { - final Map inputMap = readerService.getInputs(); - if (MapUtils.isEmpty(inputMap)) { - return; - } - final Map propertyMap = (Map) inputMap.get(inputName); - final Object requiredObj = propertyMap.get(REQUIRED.getElementName()); - final Object constraintsObj = propertyMap.get(CONSTRAINTS.getElementName()); - final Object entrySchemaObj = propertyMap.get(ENTRY_SCHEMA.getElementName()); - - EntrySchema entrySchema = null; - if (entrySchemaObj instanceof Map) { - final Map entrySchemaMap = ((Map) entrySchemaObj); - entrySchema = parseEntrySchema(entrySchemaMap); - } - final ParameterDefinition parameterDefinition = - DataModelUtil.createParameterDefinition( - (String) propertyMap.get(TYPE.getElementName()), - (String) propertyMap.get(DESCRIPTION.getElementName()), - requiredObj instanceof Boolean ? (Boolean) requiredObj : null, - constraintsObj instanceof List ? (List) constraintsObj : null, - entrySchema, - propertyMap.get(DEFAULT_VALUE.getElementName())); - - DataModelUtil - .addInputParameterToTopologyTemplate(serviceTemplate, inputName, parameterDefinition); - } - - /** - * PNF only has nfv.PNF and nfv.PnfExtCp types defined in ETSI SOL001 v2.5.1. - The PNF is mapped to the outer - * Abstract PNF container in ecomp model and hence nfv.PNF is dropped here. - nfv.PnfExtCp is mapped to ecomp - * v2.extCp type. - */ - private void convertNodeTemplatesToEcompTypes() { - final Map nodeTemplates = readerService.getNodeTemplates(); - if (MapUtils.isEmpty(nodeTemplates)) { - return; - } - - nodeTemplates.entrySet().stream() - .filter(nodeTemplateEntry -> - PNF_EXT_CP_TYPE.equals(((Map) nodeTemplateEntry.getValue()).get(TYPE.getElementName()))) - .forEach(nodeTemplateEntry -> - DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(), - convertToEcompConnectionPointNodeType((Map) nodeTemplateEntry.getValue()))); - } - - /** - * Converts from the ETSI PnfExtCp node type to ecomp v2.extCP node type The following properties are mapped - - * layer_protocols is mapped to ip_requirements if it contains the values ipv4 and/or ipv6. All other data e.g. - * remaining properties, requirements, capabilities are not mapped over to ecomp equivalent - * - * @param pnfExtCp - the ETSI PnfExtCp map - * @return ecomp v2.extCP node type - */ - private NodeTemplate convertToEcompConnectionPointNodeType(final Map pnfExtCp) { - final NodeTemplate nodeTemplate = new NodeTemplate(); - nodeTemplate.setType(EXT_CP_TYPE); - final Map properties = (Map) pnfExtCp.get(PROPERTIES.getElementName()); - - properties.entrySet().stream() - .filter(propertyMap -> LAYER_PROTOCOLS.equals(propertyMap.getKey())) - .forEach(propertyMap -> { - final Object propertyValue = propertyMap.getValue(); - if (propertyValue instanceof List) { - // layer_protocols: [ ipv4, ipv6, ... ] - final List> ipRequirements = - convertToIpRequirementsProperty((List) propertyValue); - if (!ipRequirements.isEmpty()) { - final Map convertedProperties = new HashMap<>(); - convertedProperties.put(IP_REQUIREMENTS, ipRequirements); - nodeTemplate.setProperties(convertedProperties); - } - } else if (propertyValue instanceof AbstractMap) { - final Map propertyValueMap = (Map) propertyValue; - if (propertyValueMap.containsKey(GET_INPUT.getElementName())) { - // layer_protocols: {get_input: anInputName} - final Map convertedProperties = new HashMap<>(); - convertedProperties.put(IP_REQUIREMENTS, propertyValueMap); - nodeTemplate.setProperties(convertedProperties); - final String getInputValue = (String) propertyValueMap.get(GET_INPUT.getElementName()); - if (!isInputAlreadyAdded(getInputValue)) { - copyTopologyTemplateInput(getInputValue); - parseLayerProtocolsInputToIpRequirements(getInputValue); - } - } - } - }); - return nodeTemplate; - } - - /** - * Checks if a topology template input was already added - * - * @param inputName The name of the input to check - * @return - * {@code true} if the input was found in the topology template structure - */ - private boolean isInputAlreadyAdded(final String inputName) { - final TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); - if (topologyTemplate == null) { - return false; - } - - final Map inputMap = topologyTemplate.getInputs(); - if (MapUtils.isNotEmpty(inputMap)) { - return inputMap.keySet().contains(inputName); - } - - return false; - } - - /** - * Parses a layer_protocol input to org.openecomp.datatypes.network.IpRequirements ecomp type. - * - * @param inputName The name of the input to parse - * - */ - private void parseLayerProtocolsInputToIpRequirements(final String inputName) { - final TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); - final ParameterDefinition layerProtocolsInput = topologyTemplate.getInputs().get(inputName); - final EntrySchema entrySchema = layerProtocolsInput.getEntry_schema(); - entrySchema.setType(IP_REQUIREMENTS_TYPE); - final List defaultLayerProtocolList = (List) layerProtocolsInput.get_default(); - layerProtocolsInput.set_default(convertToIpRequirementsProperty(defaultLayerProtocolList)); - } - - /** - * Converts each layer_protocols entry that is either {@link #IP_V4} or {@link #IP_V6} to the ecomp - * {@link #IP_REQUIREMENTS_TYPE}, ignoring other entry types. - * - * @param layerProtocols the PnfExtCp layer_protocols list - * @return - * A list of map representing a {@link #IP_REQUIREMENTS_TYPE} ecomp type - */ - private List> convertToIpRequirementsProperty(final List layerProtocols) { - return layerProtocols.stream() - .filter(layerProtocol -> IP_V4.equals(layerProtocol) || IP_V6.equals(layerProtocol)) - .map(this::createIpRequirementsEntry) - .collect(Collectors.toList()); - } - - /** - * Creates a {@link #IP_REQUIREMENTS_TYPE} based on the ip version - * @param ipVersion the provided ip version, either {@link #IP_V4} or {@link #IP_V6} - * @return - * A map representing an {@link #IP_REQUIREMENTS_TYPE} ecomp type - */ - private Map createIpRequirementsEntry(final String ipVersion) { - final int version = IP_V4.equals(ipVersion) ? 4 : 6; - final Map result = new HashMap<>(); - result.put(IP_VERSION, version); - result.put(ASSIGNMENT_METHOD, DHCP); - return result; - } - - /** - * Fills missing required entries in the service template. Checks for topology_template entry and - * topology_template->node_templates entry. - */ - private void addEmptyNodeTemplatesIfNoneDefined() { - TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); - if (Objects.isNull(topologyTemplate)) { - topologyTemplate = new TopologyTemplate(); - serviceTemplate.setTopology_template(topologyTemplate); - } - if (topologyTemplate.getNode_templates() == null) { - topologyTemplate.setNode_templates(new HashMap<>()); - } - } - - /** - * Parses an input entry schema - * - * @param entrySchemaMap the descriptor input entry schema map - * @return - * A parsed entry schema based on the provided map - */ - private EntrySchema parseEntrySchema(Map entrySchemaMap) { - return DataModelUtil.createEntrySchema((String) entrySchemaMap.get(TYPE.getElementName()) - , (String) entrySchemaMap.get(DESCRIPTION.getElementName()) - , (List) entrySchemaMap.get(CONSTRAINTS.getElementName()) - ); - } - -} \ No newline at end of file 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 new file mode 100644 index 0000000000..ee2c4060e5 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolModelDrivenConverterPnf.java @@ -0,0 +1,39 @@ +/* + * ============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.impl; + +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.core.converter.impl.pnfd.PnfdTransformationEngine; + +public class ToscaSolModelDrivenConverterPnf extends AbstractToscaSolConverter { + + /** + * Calls the model driven engine to parse the onboarding PNF descriptor + * @param serviceTemplate + * @param readerService + */ + @Override + public void convertTopologyTemplate(final ServiceTemplate serviceTemplate, final ServiceTemplateReaderService readerService) { + final PnfdTransformationEngine pnfdTransformationEngine = new PnfdTransformationEngine(readerService, serviceTemplate); + pnfdTransformationEngine.transform(); + } + +} \ No newline at end of file -- cgit 1.2.3-korg