Reformat catalog-be-plugins
[sdc.git] / catalog-be-plugins / etsi-nfv-nsd-csar-plugin / src / main / java / org / openecomp / sdc / be / plugins / etsi / nfv / nsd / tosca / yaml / NsdTemplateRepresenter.java
1  
2 /*
3  * ============LICENSE_START=======================================================
4  *  Copyright (C) 2020 Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27 import java.util.stream.Stream;
28 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
29 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
30 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
31 import org.yaml.snakeyaml.introspector.Property;
32 import org.yaml.snakeyaml.nodes.MappingNode;
33 import org.yaml.snakeyaml.nodes.Node;
34 import org.yaml.snakeyaml.nodes.NodeTuple;
35 import org.yaml.snakeyaml.nodes.Tag;
36 import org.yaml.snakeyaml.representer.Represent;
37 import org.yaml.snakeyaml.representer.Representer;
38
39 /**
40  * A NSD YAML Representer
41  */
42 public class NsdTemplateRepresenter extends Representer {
43
44     private final Set<String> ignoredPropertySet = Stream.of("dependencies").collect(Collectors.toSet());
45
46     public NsdTemplateRepresenter() {
47         super();
48         this.nullRepresenter = new RepresentNull();
49     }
50
51     @Override
52     protected NodeTuple representJavaBeanProperty(final Object javaBean, final Property property, final Object propertyValue, final Tag customTag) {
53         if (propertyValue == null) {
54             return null;
55         }
56         if (ignoredPropertySet.contains(property.getName())) {
57             return null;
58         }
59         if (javaBean instanceof ToscaTemplate) {
60             return handleToscaTemplate(javaBean, property, propertyValue, customTag);
61         }
62         if (javaBean instanceof ToscaPropertyConstraintValidValues) {
63             return handleToscaPropertyConstraintValidValues(javaBean, property, propertyValue, customTag);
64         }
65         if (javaBean instanceof ToscaProperty) {
66             return handleToscaProperty(javaBean, property, propertyValue, customTag);
67         }
68         return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
69     }
70
71     private NodeTuple handleToscaProperty(final Object javaBean, final Property property, final Object propertyValue, final Tag customTag) {
72         final NodeTuple nodeTuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
73         if ("_defaultp_".equals(property.getName())) {
74             return new NodeTuple(representData("default"), nodeTuple.getValueNode());
75         }
76         return nodeTuple;
77     }
78
79     private NodeTuple handleToscaPropertyConstraintValidValues(final Object javaBean, final Property property, final Object propertyValue,
80                                                                final Tag customTag) {
81         final NodeTuple nodeTuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
82         if ("validValues".equals(property.getName())) {
83             final String validValuesEntryName = ToscaPropertyConstraintValidValues.getEntryToscaName("validValues");
84             return new NodeTuple(representData(validValuesEntryName), nodeTuple.getValueNode());
85         }
86         return nodeTuple;
87     }
88
89     private NodeTuple handleToscaTemplate(final Object javaBean, final Property property, final Object propertyValueObj, final Tag customTag) {
90         if ("imports".equals(property.getName())) {
91             final List<Map<String, Map<String, String>>> importsList = (List<Map<String, Map<String, String>>>) propertyValueObj;
92             final List<Map<String, String>> newImportList = new ArrayList<>();
93             importsList.forEach(importMap -> importMap.forEach((key, value) -> newImportList.add(value)));
94             return super.representJavaBeanProperty(javaBean, property, newImportList, customTag);
95         }
96         return super.representJavaBeanProperty(javaBean, property, propertyValueObj, customTag);
97     }
98
99     @Override
100     protected MappingNode representJavaBean(final Set<Property> properties, final Object javaBean) {
101         if (!classTags.containsKey(javaBean.getClass())) {
102             addClassTag(javaBean.getClass(), Tag.MAP);
103         }
104         return super.representJavaBean(properties, javaBean);
105     }
106
107     private class RepresentNull implements Represent {
108
109         @Override
110         public Node representData(final Object data) {
111             return representScalar(Tag.NULL, "");
112         }
113     }
114 }