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