Plugin to Generate Service ETSI NSD CSAR
[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  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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.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,
53                                                   final Object propertyValue, final Tag customTag) {
54         if (propertyValue == null) {
55             return null;
56         }
57
58         if (ignoredPropertySet.contains(property.getName())) {
59             return null;
60         }
61
62         if (javaBean instanceof ToscaTemplate) {
63             return handleToscaTemplate(javaBean, property, propertyValue, customTag);
64         }
65
66         if (javaBean instanceof ToscaPropertyConstraintValidValues) {
67             return handleToscaPropertyConstraintValidValues(javaBean, property, propertyValue, customTag);
68         }
69
70         if (javaBean instanceof ToscaProperty) {
71             return handleToscaProperty(javaBean, property, propertyValue, customTag);
72         }
73
74         return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
75     }
76
77     private NodeTuple handleToscaProperty(final Object javaBean, final Property property,
78                                           final Object propertyValue, final Tag customTag) {
79         final NodeTuple nodeTuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
80         if ("_defaultp_".equals(property.getName())) {
81             return new NodeTuple(representData("default"), nodeTuple.getValueNode());
82         }
83
84         return nodeTuple;
85     }
86
87     private NodeTuple handleToscaPropertyConstraintValidValues(final Object javaBean, final Property property,
88                                                                final Object propertyValue, final Tag customTag) {
89         final NodeTuple nodeTuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
90         if ("validValues".equals(property.getName())) {
91             final String validValuesEntryName = ToscaPropertyConstraintValidValues.getEntryToscaName("validValues");
92             return new NodeTuple(representData(validValuesEntryName), nodeTuple.getValueNode());
93         }
94
95         return nodeTuple;
96     }
97
98     private NodeTuple handleToscaTemplate(final Object javaBean, final Property property,
99                                           final Object propertyValueObj, final Tag customTag) {
100         if ("imports".equals(property.getName())) {
101             final List<Map<String, Map<String, String>>> importsList =
102                 (List<Map<String, Map<String, String>>>) propertyValueObj;
103
104             final List<Map<String, String>> newImportList = new ArrayList<>();
105             importsList.forEach(importMap -> importMap.forEach((key, value) -> newImportList.add(value)));
106
107             return super.representJavaBeanProperty(javaBean, property, newImportList, customTag);
108         }
109
110         return super.representJavaBeanProperty(javaBean, property, propertyValueObj, customTag);
111     }
112
113     @Override
114     protected MappingNode representJavaBean(final Set<Property> properties, final Object javaBean) {
115         if (!classTags.containsKey(javaBean.getClass())) {
116             addClassTag(javaBean.getClass(), Tag.MAP);
117         }
118
119         return super.representJavaBean(properties, javaBean);
120     }
121
122     private class RepresentNull implements Represent {
123
124         @Override
125         public Node representData(final Object data) {
126             return representScalar(Tag.NULL, "");
127         }
128     }
129 }