2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.converter.impl.pnfd;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Collections;
26 import java.util.List;
28 import java.util.Objects;
30 import java.util.stream.Collectors;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.onap.sdc.tosca.services.YamlUtil;
33 import org.openecomp.core.converter.impl.pnfd.parser.TransformationYamlParser;
34 import org.openecomp.core.converter.pnfd.model.Transformation;
35 import org.openecomp.core.converter.pnfd.model.TransformationDescription;
36 import org.openecomp.sdc.logging.api.Logger;
37 import org.openecomp.sdc.logging.api.LoggerFactory;
40 * Reads the PNF Transformation Description
42 public class PnfdTransformationDescriptorReader {
44 private static final Logger LOGGER = LoggerFactory.getLogger(PnfdTransformationDescriptorReader.class);
47 * Parse the transformation description to {@link TransformationDescription} class.
49 * The {@link TransformationDescription} instance.
51 public TransformationDescription parse(final InputStream transformationDescriptionInputStream) {
52 final List<Object> transformationList = readDescriptionYaml(transformationDescriptionInputStream);
53 final Set<Transformation> transformationSet = parseTransformationList(transformationList);
55 return new TransformationDescription(transformationSet);
59 * Reads the description file that has the required YAML format.
61 * The yaml parsed to Object
63 private List<Object> readDescriptionYaml(final InputStream transformationDescriptionPath) {
64 try (final InputStream fileInputStream = transformationDescriptionPath) {
65 return YamlUtil.yamlToList(fileInputStream).orElse(Collections.emptyList());
66 } catch (final FileNotFoundException e) {
67 LOGGER.error("Could not find the resource on path.", e);
68 } catch (final IOException e) {
69 LOGGER.error("Could not load resource.", e);
71 return Collections.emptyList();
75 * Parse the transformation list represented in a YAML object to {@link Transformation}.
76 * @param transformationYamlList the YAML object read from the transformation description file
78 * The set of transformations represented as {@link Transformation} class
80 private Set<Transformation> parseTransformationList(final List<Object> transformationYamlList) {
81 if (CollectionUtils.isEmpty(transformationYamlList)) {
82 return Collections.emptySet();
85 return transformationYamlList.stream()
86 .map(conversionMap -> TransformationYamlParser.parse((Map<String, Object>) conversionMap).orElse(null))
87 .filter(Objects::nonNull)
88 .collect(Collectors.toSet());