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;
29 import java.util.stream.Collectors;
30 import org.apache.commons.collections.CollectionUtils;
31 import org.onap.sdc.tosca.services.YamlUtil;
32 import org.openecomp.core.converter.impl.pnfd.parser.TransformationYamlParser;
33 import org.openecomp.core.converter.pnfd.model.Transformation;
34 import org.openecomp.core.converter.pnfd.model.TransformationDescription;
35 import org.openecomp.sdc.logging.api.Logger;
36 import org.openecomp.sdc.logging.api.LoggerFactory;
39 * Reads the PNF Transformation Description
41 public class PnfdTransformationDescriptorReader {
43 private static final Logger LOGGER = LoggerFactory.getLogger(PnfdTransformationDescriptorReader.class);
46 * Parse the transformation description to {@link TransformationDescription} class.
48 * The {@link TransformationDescription} instance.
50 public TransformationDescription parse(final InputStream transformationDescriptionInputStream) {
51 final List<Object> transformationList = readDescriptionYaml(transformationDescriptionInputStream);
52 final Set<Transformation> transformationSet = parseTransformationList(transformationList);
54 return new TransformationDescription(transformationSet);
58 * Reads the description file that has the required YAML format.
60 * The yaml parsed to Object
62 private List<Object> readDescriptionYaml(final InputStream transformationDescriptionPath) {
63 try (final InputStream fileInputStream = transformationDescriptionPath) {
64 return YamlUtil.yamlToList(fileInputStream).orElse(Collections.emptyList());
65 } catch (final FileNotFoundException e) {
66 LOGGER.error("Could not find the resource on path.", e);
67 } catch (final IOException e) {
68 LOGGER.error("Could not load resource.", e);
70 return Collections.emptyList();
74 * Parse the transformation list represented in a YAML object to {@link Transformation}.
75 * @param transformationYamlList the YAML object read from the transformation description file
77 * The set of transformations represented as {@link Transformation} class
79 private Set<Transformation> parseTransformationList(final List<Object> transformationYamlList) {
80 if (CollectionUtils.isEmpty(transformationYamlList)) {
81 return Collections.emptySet();
84 return transformationYamlList.stream()
85 .map(conversionMap -> TransformationYamlParser.parse((Map<String, Object>) conversionMap))
86 .collect(Collectors.toSet());