ActivitySpec - Correcting logger messages
[sdc.git] / common / openecomp-tosca-datatype / src / main / java / org / openecomp / sdc / tosca / services / ToscaExtensionYamlUtil.java
1 /*
2  * Copyright © 2016-2017 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.openecomp.sdc.tosca.services;
18
19 import org.yaml.snakeyaml.constructor.Constructor;
20 import org.yaml.snakeyaml.introspector.Property;
21 import org.yaml.snakeyaml.introspector.PropertyUtils;
22 import org.yaml.snakeyaml.nodes.MappingNode;
23 import org.yaml.snakeyaml.nodes.NodeId;
24
25 import java.beans.IntrospectionException;
26
27
28 public class ToscaExtensionYamlUtil extends YamlUtil {
29
30   @Override
31   public <T> Constructor getConstructor(Class<T> typClass) {
32     return new ToscaWithHeatExtensionConstructor(typClass);
33   }
34
35   @Override
36   protected PropertyUtils getPropertyUtils() {
37     return new ToscaPropertyUtilsWithHeatExtension();
38   }
39
40   public class ToscaPropertyUtilsWithHeatExtension extends MyPropertyUtils {
41     @Override
42     public Property getProperty(Class<? extends Object> type, String name)
43         throws IntrospectionException {
44       Class<? extends Object> classType = type;
45       try {
46         if (type
47             .equals(Class.forName("org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition"))) {
48           classType = Class
49               .forName("org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt");
50         }
51       } catch (ClassNotFoundException ex) {
52         throw new RuntimeException(ex);
53       }
54       return super.getProperty(classType, name);
55     }
56   }
57
58   protected class ToscaWithHeatExtensionConstructor extends StrictMapAppenderConstructor {
59     public ToscaWithHeatExtensionConstructor(Class<?> theRoot) {
60       super(theRoot);
61       yamlClassConstructors.put(NodeId.mapping, new MyPersistentObjectConstruct());
62     }
63
64     class MyPersistentObjectConstruct extends Constructor.ConstructMapping {
65       @Override
66       protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
67         Class type = node.getType();
68         try {
69           if (type.equals(
70               Class.forName("org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition"))) {
71             Class extendHeatClass = Class.forName(
72                 "org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt");
73             Object extendHeatObject = extendHeatClass.newInstance();
74             // create JavaBean
75             return super.constructJavaBean2ndStep(node, extendHeatObject);
76           } else {
77             // create JavaBean
78             return super.constructJavaBean2ndStep(node, object);
79           }
80         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
81           throw new RuntimeException(ex);
82         }
83       }
84     }
85   }
86 }