Implement PNFD Model driven conversion
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / main / java / org / openecomp / core / converter / impl / pnfd / parser / TransformationYamlParser.java
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.parser;
21
22 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.CONVERSIONS;
23 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.DESCRIPTION;
24 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.NAME;
25 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.QUERY;
26 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.TRANSFORMATION_FOR;
27
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.stream.Collectors;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.openecomp.core.converter.pnfd.model.ConversionDefinition;
34 import org.openecomp.core.converter.pnfd.model.Transformation;
35 import org.openecomp.core.converter.pnfd.model.TransformationBlock;
36
37 /**
38  * Handles YAML from/to {@link Transformation} conversions
39  */
40 public class TransformationYamlParser {
41
42     private TransformationYamlParser() {
43
44     }
45
46     /**
47      * Parses the given YAML object to a {@link Transformation} instance.
48      * @param transformationYaml      the YAML object representing a transformation
49      * @return
50      *  A new instance of {@link Transformation}.
51      */
52     public static Transformation parse(final Map<String, Object> transformationYaml) {
53         final Transformation transformation = new Transformation();
54         transformation.setName((String) transformationYaml.get(NAME.getName()));
55         transformation.setDescription((String) transformationYaml.get(DESCRIPTION.getName()));
56
57         final String block = (String) transformationYaml.get(TRANSFORMATION_FOR.getName());
58         transformation.setBlock(TransformationBlock.parse(block).orElse(null));
59
60         transformation.setConversionQuery(
61             ConversionQueryYamlParser.parse(transformationYaml.get(QUERY.getName()))
62         );
63         transformation.setConversionDefinitionList(parseConversions(transformationYaml));
64
65         return transformation;
66     }
67
68     private static List<ConversionDefinition> parseConversions(final Map<String, Object> conversionYaml) {
69         final List<Object> conversionList = (List<Object>) conversionYaml.get(CONVERSIONS.getName());
70
71         if (CollectionUtils.isEmpty(conversionList)) {
72             return Collections.emptyList();
73         }
74
75         return conversionList.stream()
76             .map(conversion -> ConversionDefinitionYamlParser.parse((Map<String, Object>) conversion))
77             .collect(Collectors.toList());
78     }
79
80 }