Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / audit / ListEndpoints.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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.aai.audit;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.apache.commons.lang.StringUtils;
34
35 import org.openecomp.aai.db.props.AAIProperties;
36 import org.openecomp.aai.introspection.Introspector;
37 import org.openecomp.aai.introspection.Loader;
38 import org.openecomp.aai.introspection.LoaderFactory;
39 import org.openecomp.aai.introspection.ModelType;
40 import org.openecomp.aai.introspection.Version;
41 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
44 import com.google.common.base.CaseFormat;
45
46 /**
47  * The Class ListEndpoints.
48  */
49 public class ListEndpoints {
50
51         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ListEndpoints.class);
52
53         private final String start = "inventory";
54         private final String[] blacklist = { "search", "aai-internal" };
55
56         private List<String> endpoints = new ArrayList<>();
57         private Map<String, String> endpointToLogicalName = new HashMap<String, String>();
58
59         /**
60          * The main method.
61          *
62          * @param args the arguments
63          */
64         public static void main(String[] args) {
65                 ListEndpoints endPoints = new ListEndpoints(AAIProperties.LATEST);
66
67                 System.out.println(endPoints.toString("relationship-list"));
68
69         }
70
71         /**
72          * Instantiates a new list endpoints.
73          *
74          * @param version the version
75          */
76         public ListEndpoints(Version version) {
77
78                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
79
80                 try {
81                         final Introspector start = loader.introspectorFromName(this.start);
82                         Set<String> startMap = new HashSet<>();
83                         beginAudit(start, "/aai/" + version, startMap);
84                 } catch (AAIUnknownObjectException e) {
85                         throw new RuntimeException("Failed to find object " + this.start + ", cannot run ListEndpoints audit");
86                 }
87         }
88
89         /**
90          * Begin audit.
91          *
92          * @param obj the obj
93          * @param uri the uri
94          */
95         private void beginAudit(Introspector obj, String uri, Set<String> visited) {
96
97                 String currentUri = "";
98
99                 if (!obj.getDbName().equals("inventory")) {
100                         currentUri = uri + obj.getGenericURI();
101                 } else {
102                         currentUri = uri;
103                 }
104                 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
105                         return;
106                 }
107                 if (!obj.isContainer()) {
108                         endpoints.add(currentUri);
109                 }
110                 
111                 String dbName = obj.getDbName();
112                 
113                 populateLogicalName(obj, uri, currentUri);
114                 
115                 Set<String> properties = obj.getProperties();
116                 Set<String> props = new LinkedHashSet<>(properties);
117                 if (obj.isContainer()) {
118                         for (String key : visited) {
119                                 if (props.remove(key)) {
120                                         try {
121                                                 endpoints.add(currentUri + obj.getLoader().introspectorFromName(key).getGenericURI());
122                                         } catch (AAIUnknownObjectException e) {
123                                                 LOGGER.warn("Skipping endpoint for " + key + " (Unknown object)", e);
124                                         }
125                                 }
126                         }
127                 }
128                 
129                 outer: for (String propName : props) {
130                         
131                         for (String item : blacklist) {
132                                 if (propName.equals(item)) {
133                                         continue outer;
134                                 }
135                         }
136                         if (obj.isListType(propName)) {
137                                 if (obj.isComplexGenericType(propName)) {
138                                         try {
139                                                 final Introspector nestedObj = obj.newIntrospectorInstanceOfNestedProperty(propName);
140                                                 Set<String> newVisited = new HashSet<>();
141                                                 newVisited.addAll(visited);
142                                                 newVisited.add(nestedObj.getDbName());
143                                                 beginAudit(
144                                                                 nestedObj,
145                                                                 currentUri,
146                                                                 newVisited
147                                                                 );
148                                         } catch (AAIUnknownObjectException e) {
149                                                 LOGGER.warn("Skipping nested endpoint for " + propName + " (Unknown Object)", e);
150                                         }
151                                 }
152                         } else if (obj.isComplexType(propName)) {
153                                 try {
154                                         final Introspector nestedObj = obj.newIntrospectorInstanceOfProperty(propName);
155                                         Set<String> newVisited = new HashSet<>();
156                                         newVisited.addAll(visited);
157                                         newVisited.add(nestedObj.getDbName());
158                                         beginAudit(nestedObj,
159                                                         currentUri,
160                                                         visited
161                                                         );
162                                 } catch (AAIUnknownObjectException e) {
163                                         LOGGER.warn("Skipping nested enpoint for " + propName + " (Unknown Object)", e);
164                                 }
165                         }
166                 }
167
168         }
169
170         /**
171          * Populate logical name.
172          *
173          * @param obj the obj
174          * @param uri the uri
175          * @param currentUri the current uri
176          */
177         private void populateLogicalName(Introspector obj, String uri, String currentUri) {
178
179                 if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
180                         return;
181                 }
182                 
183                 if (uri.endsWith("/relationship-list")) {
184                         uri = uri.substring(0, uri.lastIndexOf("/"));
185                 }
186
187                 String logicalName = "";
188                 String keys = "";
189                 
190
191                 if (!obj.getAllKeys().isEmpty()) {
192                         
193                         Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
194                         Matcher m = p.matcher(currentUri);
195                         
196                         if (m.find()) {
197                                 keys = StringUtils.join(obj.getAllKeys(), "-and-");
198                         } else {
199                                 keys = StringUtils.join(obj.getAllKeys(), "-or-");
200                         }
201                         keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
202                         if (!keys.isEmpty()) {
203                                 keys = "With" + keys;
204                         }
205                 }
206
207                 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
208                 
209                 if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
210                         logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
211                 } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf("/")))) {
212                         logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf("/")));
213                 }
214
215                 endpointToLogicalName.put(currentUri, logicalName);
216                 
217         }
218         
219         /**
220          * Gets the logical names.
221          *
222          * @return the logical names
223          */
224         public Map<String, String> getLogicalNames() {
225                 
226                 return endpointToLogicalName;
227
228         }
229
230         /**
231          * Gets the endpoints.
232          *
233          * @return the endpoints
234          */
235         public List<String> getEndpoints() {
236
237                 return this.getEndpoints("");
238
239         }
240
241         /**
242          * Gets the endpoints.
243          *
244          * @param filterOut the filter out
245          * @return the endpoints
246          */
247         public List<String> getEndpoints(String filterOut) {
248                 List<String> result = new ArrayList<>();
249                 Pattern p = null;
250                 Matcher m = null;
251                 if (!filterOut.equals("")) {
252                         p = Pattern.compile(filterOut);
253                         m = null;
254                 }
255                 for (String s : endpoints) {
256                         if (p != null) {
257                                 m = p.matcher(s);
258                                 if (m.find()) {
259                                         continue;
260                                 }
261                         }
262
263                         result.add(s);
264                 }
265
266                 return result;
267
268         }
269
270         /** 
271          * {@inheritDoc}
272          */
273         @Override
274         public String toString() {
275                 StringBuilder sb = new StringBuilder();
276                 for (String s : endpoints) {
277                         sb.append(s + "\n");
278                 }
279                 return sb.toString();
280
281         }
282
283         /**
284          * To string.
285          *
286          * @param filterOut the filter out
287          * @return the string
288          */
289         public String toString(String filterOut) {
290                 StringBuilder sb = new StringBuilder();
291                 Pattern p = Pattern.compile(filterOut);
292                 Matcher m = null;
293                 for (String s : endpoints) {
294                         m = p.matcher(s);
295                         if (!m.find()) {
296                                 sb.append(s + "\n");
297                         }
298                 }
299                 return sb.toString();
300         }
301
302 }