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