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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml;
22 import java.util.ArrayList;
23 import java.util.List;
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;
40 * A NSD YAML Representer
42 public class NsdTemplateRepresenter extends Representer {
44 private final Set<String> ignoredPropertySet = Stream.of("dependencies").collect(Collectors.toSet());
46 public NsdTemplateRepresenter() {
48 this.nullRepresenter = new RepresentNull();
52 protected NodeTuple representJavaBeanProperty(final Object javaBean, final Property property,
53 final Object propertyValue, final Tag customTag) {
54 if (propertyValue == null) {
58 if (ignoredPropertySet.contains(property.getName())) {
62 if (javaBean instanceof ToscaTemplate) {
63 return handleToscaTemplate(javaBean, property, propertyValue, customTag);
66 if (javaBean instanceof ToscaPropertyConstraintValidValues) {
67 return handleToscaPropertyConstraintValidValues(javaBean, property, propertyValue, customTag);
70 if (javaBean instanceof ToscaProperty) {
71 return handleToscaProperty(javaBean, property, propertyValue, customTag);
74 return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
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());
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());
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;
104 final List<Map<String, String>> newImportList = new ArrayList<>();
105 importsList.forEach(importMap -> importMap.forEach((key, value) -> newImportList.add(value)));
107 return super.representJavaBeanProperty(javaBean, property, newImportList, customTag);
110 return super.representJavaBeanProperty(javaBean, property, propertyValueObj, customTag);
114 protected MappingNode representJavaBean(final Set<Property> properties, final Object javaBean) {
115 if (!classTags.containsKey(javaBean.getClass())) {
116 addClassTag(javaBean.getClass(), Tag.MAP);
119 return super.representJavaBean(properties, javaBean);
122 private class RepresentNull implements Represent {
125 public Node representData(final Object data) {
126 return representScalar(Tag.NULL, "");