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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============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, final Object propertyValue, final Tag customTag) {
53 if (propertyValue == null) {
56 if (ignoredPropertySet.contains(property.getName())) {
59 if (javaBean instanceof ToscaTemplate) {
60 return handleToscaTemplate(javaBean, property, propertyValue, customTag);
62 if (javaBean instanceof ToscaPropertyConstraintValidValues) {
63 return handleToscaPropertyConstraintValidValues(javaBean, property, propertyValue, customTag);
65 if (javaBean instanceof ToscaProperty) {
66 return handleToscaProperty(javaBean, property, propertyValue, customTag);
68 return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
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());
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());
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);
96 return super.representJavaBeanProperty(javaBean, property, propertyValueObj, customTag);
100 protected MappingNode representJavaBean(final Set<Property> properties, final Object javaBean) {
101 if (!classTags.containsKey(javaBean.getClass())) {
102 addClassTag(javaBean.getClass(), Tag.MAP);
104 return super.representJavaBean(properties, javaBean);
107 private class RepresentNull implements Represent {
110 public Node representData(final Object data) {
111 return representScalar(Tag.NULL, "");