2b806fbea0f44f7a0dec46a2f6d722de1a339924
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / services / yamlutil / ToscaExtensionYamlUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.tosca.services.yamlutil;
22
23 import org.openecomp.core.utilities.yaml.YamlUtil;
24 import org.yaml.snakeyaml.constructor.Constructor;
25 import org.yaml.snakeyaml.introspector.Property;
26 import org.yaml.snakeyaml.introspector.PropertyUtils;
27 import org.yaml.snakeyaml.nodes.MappingNode;
28 import org.yaml.snakeyaml.nodes.NodeId;
29
30 import java.beans.IntrospectionException;
31
32 public class ToscaExtensionYamlUtil extends YamlUtil {
33
34   @Override
35   public <T> Constructor getConstructor(Class<T> typClass) {
36     return new ToscaWithHeatExtensionConstructor(typClass);
37   }
38
39   @Override
40   protected PropertyUtils getPropertyUtils() {
41     return new ToscaPropertyUtilsWithHeatExtension();
42   }
43
44   public class ToscaPropertyUtilsWithHeatExtension extends MyPropertyUtils {
45     @Override
46     public Property getProperty(Class<? extends Object> type, String name)
47         throws IntrospectionException {
48       try {
49         if (type
50             .equals(Class.forName("org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition"))) {
51           type = Class
52               .forName("org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt");
53         }
54       } catch (ClassNotFoundException ex) {
55         throw new RuntimeException(ex);
56       }
57       return super.getProperty(type, name);
58     }
59   }
60
61   protected class ToscaWithHeatExtensionConstructor extends StrictMapAppenderConstructor {
62     public ToscaWithHeatExtensionConstructor(Class<?> theRoot) {
63       super(theRoot);
64       yamlClassConstructors.put(NodeId.mapping, new MyPersistentObjectConstruct());
65     }
66
67     class MyPersistentObjectConstruct extends ConstructMapping {
68       @Override
69       protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
70         Class type = node.getType();
71         try {
72           if (type.equals(
73               Class.forName("org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition"))) {
74             Class extendHeatClass = Class.forName(
75                 "org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt");
76             Object extendHeatObject = extendHeatClass.newInstance();
77             // create JavaBean
78             return super.constructJavaBean2ndStep(node, extendHeatObject);
79           } else {
80             // create JavaBean
81             return super.constructJavaBean2ndStep(node, object);
82           }
83         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
84           throw new RuntimeException(ex);
85         }
86       }
87     }
88   }
89 }
90
91
92