24586d9ea03404ea5742b137c2e8879512e157e0
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / ToscaUtils.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.be.tosca;
22
23 import java.lang.reflect.Field;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
33 import org.openecomp.sdc.be.model.Component;
34
35 public class ToscaUtils {
36
37         public static boolean isAtomicType(Component component) {
38                 ComponentTypeEnum componentType = component.getComponentType();
39                 if (ComponentTypeEnum.RESOURCE.equals(componentType)) {
40                         ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition()).getResourceType();
41                         if (ResourceTypeEnum.CP == resourceType || ResourceTypeEnum.VL == resourceType || ResourceTypeEnum.VFC == resourceType || ResourceTypeEnum.VFCMT == resourceType || ResourceTypeEnum.ABSTRACT == resourceType) {
42                                 return true;
43                         }
44                 }
45                 return false;
46         }
47         
48         public static boolean isComplexVfc(Component component) {
49                 if (ComponentTypeEnum.RESOURCE == component.getComponentType()) {
50                         ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition()).getResourceType();
51                         if (ResourceTypeEnum.CVFC == resourceType) {
52                                 return true;
53                         }
54                 }
55                 return false;
56         }
57
58         public static Map<String, Object> objectToMap(Object objectToConvert, Class clazz) throws IllegalArgumentException, IllegalAccessException {
59                 Map<String, Object> map = new HashMap<>();
60                 List<Field> fields = new ArrayList<>();
61
62                 fields = getAllFields(fields, clazz);
63
64                 for (Field field : fields) {
65                         field.setAccessible(true);
66                         map.put(field.getName(), field.get(objectToConvert));
67                 }
68                 return map;
69         }
70
71         public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
72                 fields.addAll(Arrays.asList(type.getDeclaredFields()));
73
74                 if (type.getSuperclass() != null) {
75                         fields = getAllFields(fields, type.getSuperclass());
76                 }
77                 return fields;
78         }
79 }