Resolved below sonar issues:
[aai/aai-service.git] / ajsc-aai / 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 com.google.common.base.CaseFormat;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import org.apache.commons.lang.StringUtils;
31 import org.openecomp.aai.introspection.Introspector;
32 import org.openecomp.aai.introspection.IntrospectorFactory;
33 import org.openecomp.aai.introspection.Loader;
34 import org.openecomp.aai.introspection.LoaderFactory;
35 import org.openecomp.aai.introspection.ModelType;
36 import org.openecomp.aai.introspection.Version;
37 import org.openecomp.aai.logging.LogLineBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The Class ListEndpoints.
43  */
44 public class ListEndpoints {
45
46
47     private static final Logger log = LoggerFactory.getLogger(ListEndpoints.class);
48     private static final String INVENTORY = "inventory";
49
50     private static final String[] blacklist = {"search", "aai-internal", "models", "named-queries"};
51
52     private List<String> endpoints = new ArrayList<>();
53
54     private Map<String, String> endpointToLogicalName = new HashMap<>();
55
56     private final LogLineBuilder llBuilder = new LogLineBuilder();
57
58     /**
59      * Instantiates a new list endpoints.
60      *
61      * @param version the version
62      */
63     public ListEndpoints(Version version) {
64
65         Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder);
66         if (loader != null) {
67             Introspector introspector = loader.introspectorFromName(INVENTORY);
68
69             beginAudit(introspector, "/aai/" + version);
70         } else {
71             log.error(String.format("failed to create logger for %s version", version));
72         }
73     }
74
75     /**
76      * Begin audit.
77      *
78      * @param obj the obj
79      * @param uri the uri
80      */
81     private void beginAudit(Introspector obj, String uri) {
82         String currentUri = getCurrentUri(obj, uri);
83
84         if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
85             return;
86         }
87         if (!obj.isContainer()) {
88             endpoints.add(currentUri);
89         }
90
91         populateLogicalName(obj, uri, currentUri);
92
93         handleAudit(obj, currentUri);
94     }
95
96     private String getCurrentUri(Introspector obj, String uri) {
97         if (!obj.getDbName().equals(INVENTORY)) {
98             return uri + obj.getGenericURI();
99         }
100         return uri;
101     }
102
103     private void handleAudit(Introspector obj, String currentUri) {
104         outer:
105         for (String propName : obj.getProperties()) {
106             for (String item : blacklist) {
107                 if (propName.equals(item)) {
108                     continue outer;
109                 }
110             }
111             if (obj.isListType(propName)) {
112                 if (obj.isComplexGenericType(propName)) {
113                     beginAudit(
114                         IntrospectorFactory
115                             .newInstance(ModelType.MOXY, obj.newInstanceOfNestedProperty(propName), llBuilder),
116                         currentUri);
117                 }
118             } else if (obj.isComplexType(propName)) {
119                 beginAudit(
120                     IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfProperty(propName), llBuilder),
121                     currentUri);
122             }
123         }
124     }
125
126     /**
127      * Populate logical name.
128      *
129      * @param obj the obj
130      * @param uriString the uri
131      * @param currentUri the current uri
132      */
133     private void populateLogicalName(Introspector obj, String uriString, String currentUri) {
134
135         if (obj.getDbName().equals(INVENTORY) || currentUri.split("/").length <= 4 || currentUri
136             .endsWith("relationship-list")) {
137             return;
138         }
139         String uri = "";
140         if (uriString.endsWith("/relationship-list")) {
141             uri = uriString.substring(0, uriString.lastIndexOf('/'));
142         }
143
144         String logicalName;
145         String keys = "";
146
147         if (!obj.getAllKeys().isEmpty()) {
148
149             Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
150             Matcher m = p.matcher(currentUri);
151
152             if (m.find()) {
153                 keys = StringUtils.join(obj.getAllKeys(), "-and-");
154             } else {
155                 keys = StringUtils.join(obj.getAllKeys(), "-or-");
156             }
157             keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
158             if (!keys.isEmpty()) {
159                 keys = "With" + keys;
160             }
161         }
162
163         logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
164
165         if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
166             logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
167         } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf('/')))) {
168             logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf('/')));
169         }
170
171         endpointToLogicalName.put(currentUri, logicalName);
172     }
173
174     /**
175      * Gets the logical names.
176      *
177      * @return the logical names
178      */
179     public Map<String, String> getLogicalNames() {
180
181         return endpointToLogicalName;
182     }
183
184     /**
185      * Gets the endpoints.
186      *
187      * @return the endpoints
188      */
189     public List<String> getEndpoints() {
190
191         return this.getEndpoints("");
192     }
193
194     /**
195      * Gets the endpoints.
196      *
197      * @param filterOut the filter out
198      * @return the endpoints
199      */
200     public List<String> getEndpoints(String filterOut) {
201         List<String> result = new ArrayList<>();
202         Pattern p = null;
203         Matcher m;
204         if (!"".equals(filterOut)) {
205             p = Pattern.compile(filterOut);
206         }
207         for (String s : endpoints) {
208             if (p != null) {
209                 m = p.matcher(s);
210                 if (m.find()) {
211                     continue;
212                 }
213             }
214             result.add(s);
215         }
216
217         return result;
218     }
219
220     /**
221      * {@inheritDoc}
222      */
223     @Override
224     public String toString() {
225         StringBuilder sb = new StringBuilder();
226         for (String s : endpoints) {
227             sb.append(s).append("\n");
228         }
229         return sb.toString();
230     }
231
232     /**
233      * To string.
234      *
235      * @param filterOut the filter out
236      * @return the string
237      */
238     public String toString(String filterOut) {
239         StringBuilder sb = new StringBuilder();
240         Pattern p = Pattern.compile(filterOut);
241         Matcher m;
242         for (String s : endpoints) {
243             m = p.matcher(s);
244             if (!m.find()) {
245                 sb.append(s).append("\n");
246             }
247         }
248         return sb.toString();
249     }
250 }