6ef5caf21bfd265979825af07fc482d69e452365
[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.Set;
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;
37
38 /**
39  * Reads the PNF Transformation Description
40  */
41 public class PnfdTransformationDescriptorReader {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(PnfdTransformationDescriptorReader.class);
44
45     /**
46      * Parse the transformation description to {@link TransformationDescription} class.
47      * @return
48      *  The {@link TransformationDescription} instance.
49      */
50     public TransformationDescription parse(final InputStream transformationDescriptionInputStream) {
51         final List<Object> transformationList = readDescriptionYaml(transformationDescriptionInputStream);
52         final Set<Transformation> transformationSet = parseTransformationList(transformationList);
53
54         return new TransformationDescription(transformationSet);
55     }
56
57     /**
58      * Reads the description file that has the required YAML format.
59      * @return
60      *  The yaml parsed to Object
61      */
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);
69         }
70         return Collections.emptyList();
71     }
72
73     /**
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
76      * @return
77      *  The set of transformations represented as {@link Transformation} class
78      */
79     private Set<Transformation> parseTransformationList(final List<Object> transformationYamlList) {
80         if (CollectionUtils.isEmpty(transformationYamlList)) {
81             return Collections.emptySet();
82         }
83
84         return transformationYamlList.stream()
85             .map(conversionMap -> TransformationYamlParser.parse((Map<String, Object>) conversionMap))
86             .collect(Collectors.toSet());
87     }
88
89 }