Catalog alignment
[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 org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
24 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
25 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
26 import org.openecomp.sdc.be.model.Component;
27
28 import java.lang.reflect.Field;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 public class ToscaUtils {
35
36     private ToscaUtils() {}
37
38     public static boolean isNotComplexVfc(Component component) {
39         if (ComponentTypeEnum.RESOURCE == component.getComponentType()) {
40             ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition()).getResourceType();
41             if (ResourceTypeEnum.CVFC == resourceType) {
42                 return false;
43             }
44         }
45         return true;
46     }
47
48     public static Map<String, Object> objectToMap(Object objectToConvert, Class<?> clazz) throws IllegalAccessException {
49         Map<String, Object> map = new HashMap<>();
50         List<Field> fields = new ArrayList<>();
51
52         fields = getAllFields(fields, clazz);
53
54         for (Field field : fields) {
55             field.setAccessible(true);
56             map.put(field.getName(), field.get(objectToConvert));
57         }
58         return map;
59     }
60
61     private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
62         fields.addAll(Arrays.asList(type.getDeclaredFields()));
63         if (type.getSuperclass() != null) {
64             return getAllFields(fields, type.getSuperclass());
65         }
66         return fields;
67     }
68
69     public static class SubstitutionEntry {
70
71         private String fullName = "";
72         private String sourceName = "";
73         private String owner = "";
74
75         SubstitutionEntry(String fullName, String sourceName, String owner) {
76             if(fullName != null) {
77                 this.fullName = fullName;
78             }
79             if(sourceName != null) {
80                 this.sourceName = sourceName;
81             }
82             if(owner != null) {
83                 this.owner = owner;
84             }
85         }
86
87         public String getFullName() {
88             return fullName;
89         }
90
91         public void setFullName(String fullName) {
92             this.fullName = fullName;
93         }
94
95         public String getSourceName() {
96             return sourceName;
97         }
98
99         public void setSourceName(String sourceName) {
100             this.sourceName = sourceName;
101         }
102
103         public String getOwner() {
104             return owner;
105         }
106
107         public void setOwner(String owner) {
108             this.owner = owner;
109         }
110     }
111
112 }