diff options
author | 2019-09-03 13:45:28 +0000 | |
---|---|---|
committer | 2019-09-03 15:39:43 +0000 | |
commit | 7de4f3e788ff2b71936ae43be7dfbdf48740f9be (patch) | |
tree | a7419656377e146ec1095d6eaf82dbbffd717b41 /openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp | |
parent | ccc1f364baea63733e93c6f4eb26ea97eeb77960 (diff) |
Implement PNFD Model driven conversion
Change-Id: I7427e82353cac17817457294d78953f867d2d6e8
Issue-ID: SDC-2509
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp')
17 files changed, 1188 insertions, 271 deletions
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<Object> transformationList = readDescriptionYaml(transformationDescriptionInputStream); + final Set<Transformation> 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<Object> 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<Transformation> parseTransformationList(final List<Object> transformationYamlList) { + if (CollectionUtils.isEmpty(transformationYamlList)) { + return Collections.emptySet(); + } + + return transformationYamlList.stream() + .map(conversionMap -> TransformationYamlParser.parse((Map<String, Object>) 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<TransformationBlock, List<Transformation>> 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<Transformation> transformationSet = transformationDescription.getTransformationSet(); + if (CollectionUtils.isEmpty(transformationSet)) { + return; + } + transformationGroupByBlockMap = transformationSet.stream() + .filter(Transformation::isValid) + .collect(Collectors.groupingBy(Transformation::getBlock)); + final Map<String, String> 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<String, String> executeNodeTemplateTransformations() { + final List<Transformation> transformationList = transformationGroupByBlockMap + .get(TransformationBlock.NODE_TEMPLATE); + if (CollectionUtils.isEmpty(transformationList)) { + return Collections.emptyMap(); + } + + final Map<String, String> 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<String, String> inputsToConvertMap) { + final List<Transformation> transformationListOfGetInputFunction = transformationGroupByBlockMap + .get(TransformationBlock.GET_INPUT_FUNCTION); + + if(MapUtils.isEmpty(inputsToConvertMap) || CollectionUtils.isEmpty(transformationListOfGetInputFunction)) { + return; + } + + final Map<String, List<Transformation>> transformationByName = transformationListOfGetInputFunction.stream() + .collect(Collectors.groupingBy(Transformation::getName)); + + inputsToConvertMap.forEach((inputName, transformationName) -> { + final List<Transformation> transformationList = transformationByName.get(transformationName); + if (!CollectionUtils.isEmpty(transformationList)) { + final Transformation transformation = transformationList.stream() + .findFirst().orElse(null); + if (transformation != null) { + final Map<String, Object> 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<PnfdBlockParser> 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<String, Object> 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<String, Object>) conversionYaml.get(PnfTransformationToken.TO_VALUE.getName())) + .orElse(null); + final String toGetInput = (String) conversionYaml.get(PnfTransformationToken.TO_GET_INPUT.getName()); + + return new ConversionDefinition(conversionQuery, toName, toValue, toGetInput); + } + +} 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<String, Object> 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<String, Object>) nodeTemplateYaml.get("properties")); + nodeTemplate.setAttributes((Map<String, Object>) nodeTemplateYaml.get("attributes")); + nodeTemplate.setDirectives((List<String>) nodeTemplateYaml.get("directives")); + nodeTemplate.setMetadata((Map<String, String>) nodeTemplateYaml.get("metadata")); + nodeTemplate.setInterfaces((Map<String, Object>) 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<String, Object> parameterDefinitionYaml) { + final ParameterDefinition parameterDefinition = new ParameterDefinition(); + parameterDefinition.set_default(parameterDefinitionYaml.get("default")); + parameterDefinition.setDescription((String) parameterDefinitionYaml.get("description")); + final Map<String, Object> entrySchemaYaml = (Map<String, Object>) 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<PnfdConversionStrategy> parse(final Map<String, Object> strategyYaml) { + final Optional<ConversionStrategyType> 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<Map<String, Object>>) 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<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery, + final Map<String, Object> 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<String, Object> 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<Map<String, Object>> builtInput = buildParsedBlock((Map<String, Object>) attributeQuery.get(attribute), + (Map<String, Object>) 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<String, Object> parsedBlockYamlObject) { + if (!parsedBlockYamlObject.isEmpty()) { + final ParameterDefinition parameterDefinition = ParameterDefinitionYamlParser.parse(parsedBlockYamlObject); + DataModelUtil.addInputParameterToTopologyTemplate(templateTo, blockName, parameterDefinition); + } + } + + @Override + protected Set<Map<String, Object>> findBlocksToParse() { + final ConversionQuery conversionQuery = transformation.getConversionQuery(); + final Map<String, Object> 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<String, Object> 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<String, String> inputNameToConvertMap = new HashMap<>(); + + public PnfdNodeTemplateBlockParser(final Transformation transformation) { + super(transformation); + } + + public Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery, + final Map<String, Object> fromNodeTemplateAttributeMap, + final ConversionDefinition conversionDefinition) { + //cannot query for more than one attribute + if (attributeQuery.keySet().size() > 1) { + return Optional.empty(); + } + final String attribute = attributeQuery.keySet().iterator().next(); + final Object queryValue = attributeQuery.get(attribute); + final Object attributeValueToConvert = fromNodeTemplateAttributeMap.get(attribute); + if (queryValue == null) { + PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy(); + if (isGetInputFunction(attributeValueToConvert)) { + inputNameToConvertMap.put(extractGetInputFunctionValue(attributeValueToConvert) + , conversionDefinition.getToGetInput() + ); + pnfdConversionStrategy = new CopyConversionStrategy(); + } + final Map<String, Object> 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<String, Object> parsedNodeTemplate = new HashMap<>(); + final Optional<Map<String, Object>> builtNodeTemplate = buildParsedBlock( + (Map<String, Object>) queryValue, + (Map<String, Object>) attributeValueToConvert, conversionDefinition); + builtNodeTemplate.ifPresent(builtNodeTemplate1 -> parsedNodeTemplate.put(attribute, builtNodeTemplate1)); + + return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate); + } + } + + protected Set<Map<String, Object>> findBlocksToParse() { + final ConversionQuery conversionQuery = transformation.getConversionQuery(); + final Map<String, Object> nodeTemplateMap = templateFrom.getNodeTemplates(); + if (MapUtils.isEmpty(nodeTemplateMap)) { + return Collections.emptySet(); + } + + return nodeTemplateMap.entrySet().stream() + .filter(mapEntry -> PnfdQueryExecutor.find(conversionQuery, mapEntry.getValue())) + .map(stringObjectEntry -> { + final Map<String, Object> map = new HashMap<>(); + map.put(stringObjectEntry.getKey(), stringObjectEntry.getValue()); + return map; + }).collect(Collectors.toSet()); + } + + @Override + public void write(final String nodeTemplateName, final Map<String, Object> parsedNodeTemplateMap) { + if (!parsedNodeTemplateMap.isEmpty()) { + final NodeTemplate parsedNodeTemplate = NodeTemplateYamlParser.parse(parsedNodeTemplateMap); + DataModelUtil.addNodeTemplate(templateTo, nodeTemplateName, parsedNodeTemplate); + } + } + + @Override + public Optional<Map<String, String>> 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<String, Object> 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<ConversionDefinition> parseConversions(final Map<String, Object> conversionYaml) { + final List<Object> conversionList = (List<Object>) conversionYaml.get(CONVERSIONS.getName()); + + if (CollectionUtils.isEmpty(conversionList)) { + return Collections.emptyList(); + } + + return conversionList.stream() + .map(conversion -> ConversionDefinitionYamlParser.parse((Map<String, Object>) 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<Object, Object> { + + public CopyConversionStrategy() { + super(ConversionStrategyType.COPY, new HashMap<>()); + } + + @Override + public Optional<Object> 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<Object, Object> { + + public ReplaceConversionStrategy(final Object from, final Object to) { + super(ConversionStrategyType.REPLACE, new HashMap<>()); + conversionMap.put(from, to); + } + + @Override + public Optional<Object> 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<Object>, List<Object>> { + + private final Map<Object, Object> replaceInListConversionStrategyMap; + + public ReplaceInListConversionStrategy(final List<Map<String, Object>> 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<List<Object>> convert(final List<Object> originalValue) { + if (originalValue == null || originalValue.isEmpty()) { + return Optional.empty(); + } + + final List<Object> 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<String, Object> 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<Constraint>) 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<String, Object> nodeTemplates = readerService.getNodeTemplates(); - if (MapUtils.isEmpty(nodeTemplates)) { - return; - } - - nodeTemplates.entrySet().stream() - .filter(nodeTemplateEntry -> - PNF_EXT_CP_TYPE.equals(((Map<String, Object>) nodeTemplateEntry.getValue()).get(TYPE.getElementName()))) - .forEach(nodeTemplateEntry -> - DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(), - convertToEcompConnectionPointNodeType((Map<String, Object>) 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<String, Object> pnfExtCp) { - final NodeTemplate nodeTemplate = new NodeTemplate(); - nodeTemplate.setType(EXT_CP_TYPE); - final Map<String, Object> properties = (Map<String, Object>) 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<Map<String, Object>> ipRequirements = - convertToIpRequirementsProperty((List<String>) propertyValue); - if (!ipRequirements.isEmpty()) { - final Map<String, Object> 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<String, Object> 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<String, ParameterDefinition> 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<String> defaultLayerProtocolList = (List<String>) 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<Map<String, Object>> convertToIpRequirementsProperty(final List<String> 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<String, Object> createIpRequirementsEntry(final String ipVersion) { - final int version = IP_V4.equals(ipVersion) ? 4 : 6; - final Map<String, Object> 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<Constraint>) 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 |