d040c93d0975b5a2878782df1fa22e51a581525f
[sdc.git] /
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.core.converter.impl.pnfd;
21
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;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Set;
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;
38
39 /**
40  * Reads the PNF Transformation Description
41  */
42 public class PnfdTransformationDescriptorReader {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(PnfdTransformationDescriptorReader.class);
45
46     /**
47      * Parse the transformation description to {@link TransformationDescription} class.
48      * @return
49      *  The {@link TransformationDescription} instance.
50      */
51     public TransformationDescription parse(final InputStream transformationDescriptionInputStream) {
52         final List<Object> transformationList = readDescriptionYaml(transformationDescriptionInputStream);
53         final Set<Transformation> transformationSet = parseTransformationList(transformationList);
54
55         return new TransformationDescription(transformationSet);
56     }
57
58     /**
59      * Reads the description file that has the required YAML format.
60      * @return
61      *  The yaml parsed to Object
62      */
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);
70         }
71         return Collections.emptyList();
72     }
73
74     /**
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
77      * @return
78      *  The set of transformations represented as {@link Transformation} class
79      */
80     private Set<Transformation> parseTransformationList(final List<Object> transformationYamlList) {
81         if (CollectionUtils.isEmpty(transformationYamlList)) {
82             return Collections.emptySet();
83         }
84
85         return transformationYamlList.stream()
86             .map(conversionMap -> TransformationYamlParser.parse((Map<String, Object>) conversionMap).orElse(null))
87             .filter(Objects::nonNull)
88             .collect(Collectors.toSet());
89     }
90
91 }