2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.audit;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.LinkedHashSet;
28 import java.util.List;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
34 import org.apache.commons.lang.StringUtils;
36 import org.onap.aai.db.props.AAIProperties;
37 import org.onap.aai.introspection.Introspector;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.LoaderFactory;
40 import org.onap.aai.introspection.ModelType;
41 import org.onap.aai.introspection.Version;
42 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45 import com.google.common.base.CaseFormat;
48 * The Class ListEndpoints.
50 public class ListEndpoints {
52 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ListEndpoints.class);
54 private final String start = "inventory";
55 private final String[] blacklist = { "search", "aai-internal" };
57 private List<String> endpoints = new ArrayList<>();
58 private Map<String, String> endpointToLogicalName = new HashMap<String, String>();
63 * @param args the arguments
65 public static void main(String[] args) {
66 ListEndpoints endPoints = new ListEndpoints(AAIProperties.LATEST);
68 System.out.println(endPoints.toString("relationship-list"));
73 * Instantiates a new list endpoints.
75 * @param version the version
77 public ListEndpoints(Version version) {
79 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
82 final Introspector start = loader.introspectorFromName(this.start);
83 Set<String> startMap = new HashSet<>();
84 beginAudit(start, "/aai/" + version, startMap);
85 } catch (AAIUnknownObjectException e) {
86 throw new RuntimeException("Failed to find object " + this.start + ", cannot run ListEndpoints audit");
96 private void beginAudit(Introspector obj, String uri, Set<String> visited) {
98 String currentUri = "";
100 if (!obj.getDbName().equals("inventory")) {
101 currentUri = uri + obj.getGenericURI();
105 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
108 if (!obj.isContainer()) {
109 endpoints.add(currentUri);
112 String dbName = obj.getDbName();
114 populateLogicalName(obj, uri, currentUri);
116 Set<String> properties = obj.getProperties();
117 Set<String> props = new LinkedHashSet<>(properties);
118 if (obj.isContainer()) {
119 for (String key : visited) {
120 if (props.remove(key)) {
122 endpoints.add(currentUri + obj.getLoader().introspectorFromName(key).getGenericURI());
123 } catch (AAIUnknownObjectException e) {
124 LOGGER.warn("Skipping endpoint for " + key + " (Unknown object)", e);
130 outer: for (String propName : props) {
132 for (String item : blacklist) {
133 if (propName.equals(item)) {
137 if (obj.isListType(propName)) {
138 if (obj.isComplexGenericType(propName)) {
140 final Introspector nestedObj = obj.newIntrospectorInstanceOfNestedProperty(propName);
141 Set<String> newVisited = new HashSet<>();
142 newVisited.addAll(visited);
143 newVisited.add(nestedObj.getDbName());
149 } catch (AAIUnknownObjectException e) {
150 LOGGER.warn("Skipping nested endpoint for " + propName + " (Unknown Object)", e);
153 } else if (obj.isComplexType(propName)) {
155 final Introspector nestedObj = obj.newIntrospectorInstanceOfProperty(propName);
156 Set<String> newVisited = new HashSet<>();
157 newVisited.addAll(visited);
158 newVisited.add(nestedObj.getDbName());
159 beginAudit(nestedObj,
163 } catch (AAIUnknownObjectException e) {
164 LOGGER.warn("Skipping nested enpoint for " + propName + " (Unknown Object)", e);
172 * Populate logical name.
176 * @param currentUri the current uri
178 private void populateLogicalName(Introspector obj, String uri, String currentUri) {
180 if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
184 if (uri.endsWith("/relationship-list")) {
185 uri = uri.substring(0, uri.lastIndexOf("/"));
188 String logicalName = "";
192 if (!obj.getAllKeys().isEmpty()) {
194 Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
195 Matcher m = p.matcher(currentUri);
198 keys = StringUtils.join(obj.getAllKeys(), "-and-");
200 keys = StringUtils.join(obj.getAllKeys(), "-or-");
202 keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
203 if (!keys.isEmpty()) {
204 keys = "With" + keys;
208 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
210 if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
211 logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
212 } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf("/")))) {
213 logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf("/")));
216 endpointToLogicalName.put(currentUri, logicalName);
221 * Gets the logical names.
223 * @return the logical names
225 public Map<String, String> getLogicalNames() {
227 return endpointToLogicalName;
232 * Gets the endpoints.
234 * @return the endpoints
236 public List<String> getEndpoints() {
238 return this.getEndpoints("");
243 * Gets the endpoints.
245 * @param filterOut the filter out
246 * @return the endpoints
248 public List<String> getEndpoints(String filterOut) {
249 List<String> result = new ArrayList<>();
252 if (!filterOut.equals("")) {
253 p = Pattern.compile(filterOut);
256 for (String s : endpoints) {
275 public String toString() {
276 StringBuilder sb = new StringBuilder();
277 for (String s : endpoints) {
280 return sb.toString();
287 * @param filterOut the filter out
290 public String toString(String filterOut) {
291 StringBuilder sb = new StringBuilder();
292 Pattern p = Pattern.compile(filterOut);
294 for (String s : endpoints) {
300 return sb.toString();