Fix getFlat for datatype
[sdc.git] / common / onap-tosca-datatype / src / main / java / org / onap / sdc / tosca / services / ToscaExtensionYamlUtil.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.onap.sdc.tosca.services;
18
19 import org.onap.sdc.tosca.error.ToscaRuntimeException;
20 import org.yaml.snakeyaml.constructor.Constructor;
21 import org.yaml.snakeyaml.introspector.Property;
22 import org.yaml.snakeyaml.introspector.PropertyUtils;
23 import org.yaml.snakeyaml.nodes.MappingNode;
24 import org.yaml.snakeyaml.nodes.NodeId;
25
26 import java.beans.IntrospectionException;
27
28
29 public class ToscaExtensionYamlUtil extends YamlUtil {
30
31     public static final String TOSCA_MODEL_PARAMETER_DEFINITION =
32             "org.onap.sdc.tosca.datatypes.model.ParameterDefinition";
33     public static final String TOSCA_MODEL_EXT_PARAMETER_DEFINITION =
34             "org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt";
35     public static final String TOSCA_MODEL_REQUIREMENT_ASSIGNMENT =
36             "org.onap.sdc.tosca.datatypes.model.RequirementAssignment";
37     public static final String TOSCA_MODEL_EXT_REQUIREMENT_ASSIGNMENT =
38             "org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment";
39
40     @Override
41     public <T> Constructor getConstructor(Class<T> typClass) {
42         return new ToscaWithHeatExtensionConstructor(typClass);
43     }
44
45     @Override
46     protected PropertyUtils getPropertyUtils() {
47         return new ToscaPropertyUtilsWithHeatExtension();
48     }
49
50     public class ToscaPropertyUtilsWithHeatExtension extends MyPropertyUtils {
51
52         @Override
53         public Property getProperty(Class<? extends Object> type, String name) throws IntrospectionException {
54             Class<? extends Object> classType = type;
55             try {
56                 if (type.equals(Class.forName(TOSCA_MODEL_PARAMETER_DEFINITION))) {
57                     classType = Class.forName(TOSCA_MODEL_EXT_PARAMETER_DEFINITION);
58                 }
59                 if (type.equals(Class.forName(TOSCA_MODEL_REQUIREMENT_ASSIGNMENT))) {
60                     classType = Class.forName(TOSCA_MODEL_EXT_REQUIREMENT_ASSIGNMENT);
61                 }
62             } catch (ClassNotFoundException ex) {
63                 throw new ToscaRuntimeException(ex);
64             }
65             return super.getProperty(classType, name);
66         }
67     }
68
69     protected class ToscaWithHeatExtensionConstructor extends StrictMapAppenderConstructor {
70
71         public ToscaWithHeatExtensionConstructor(Class<?> theRoot) {
72             super(theRoot);
73             yamlClassConstructors.put(NodeId.mapping, new MyPersistentObjectConstruct());
74         }
75
76         class MyPersistentObjectConstruct extends Constructor.ConstructMapping {
77
78             @Override
79             protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
80                 Class type = node.getType();
81                 try {
82                     if (type.equals(Class.forName(TOSCA_MODEL_PARAMETER_DEFINITION))) {
83                         Class extendHeatClass = Class.forName(TOSCA_MODEL_EXT_PARAMETER_DEFINITION);
84                         Object extendHeatObject = extendHeatClass.newInstance();
85                         // create JavaBean
86                         return super.constructJavaBean2ndStep(node, extendHeatObject);
87                     } else if (type.equals(Class.forName(TOSCA_MODEL_REQUIREMENT_ASSIGNMENT))) {
88                         Class extendHeatClass = Class.forName(TOSCA_MODEL_EXT_REQUIREMENT_ASSIGNMENT);
89                         Object extendHeatObject = extendHeatClass.newInstance();
90                         // create JavaBean
91                         return super.constructJavaBean2ndStep(node, extendHeatObject);
92                     } else {
93                         // create JavaBean
94                         return super.constructJavaBean2ndStep(node, object);
95                     }
96                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
97                     throw new ToscaRuntimeException(ex);
98                 }
99             }
100         }
101     }
102 }