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 org.onap.aai.logging.LogFormatTools;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import com.google.common.base.CaseFormat;
50 * The Class ListEndpoints.
52 public class ListEndpoints {
54 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ListEndpoints.class);
56 private final String start = "inventory";
57 private final String[] blacklist = { "search", "aai-internal" };
59 private List<String> endpoints = new ArrayList<>();
60 private Map<String, String> endpointToLogicalName = new HashMap<String, String>();
65 * @param args the arguments
67 public static void main(String[] args) {
68 ListEndpoints endPoints = new ListEndpoints(AAIProperties.LATEST);
70 System.out.println(endPoints.toString("relationship-list"));
75 * Instantiates a new list endpoints.
77 * @param version the version
79 public ListEndpoints(Version version) {
81 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
84 final Introspector start = loader.introspectorFromName(this.start);
85 Set<String> startMap = new HashSet<>();
86 beginAudit(start, "/aai/" + version, startMap);
87 } catch (AAIUnknownObjectException e) {
88 throw new RuntimeException("Failed to find object " + this.start + ", cannot run ListEndpoints audit");
98 private void beginAudit(Introspector obj, String uri, Set<String> visited) {
100 String currentUri = "";
102 if (!obj.getDbName().equals("inventory")) {
103 currentUri = uri + obj.getGenericURI();
107 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
110 if (!obj.isContainer()) {
111 endpoints.add(currentUri);
114 String dbName = obj.getDbName();
116 populateLogicalName(obj, uri, currentUri);
118 Set<String> properties = obj.getProperties();
119 Set<String> props = new LinkedHashSet<>(properties);
120 if (obj.isContainer()) {
121 for (String key : visited) {
122 if (props.remove(key)) {
124 endpoints.add(currentUri + obj.getLoader().introspectorFromName(key).getGenericURI());
125 } catch (AAIUnknownObjectException e) {
126 LOGGER.warn("Skipping endpoint for " + key + " (Unknown object) " + LogFormatTools.getStackTop(e));
132 outer: for (String propName : props) {
134 for (String item : blacklist) {
135 if (propName.equals(item)) {
139 if (obj.isListType(propName)) {
140 if (obj.isComplexGenericType(propName)) {
142 final Introspector nestedObj = obj.newIntrospectorInstanceOfNestedProperty(propName);
143 Set<String> newVisited = new HashSet<>();
144 newVisited.addAll(visited);
145 newVisited.add(nestedObj.getDbName());
151 } catch (AAIUnknownObjectException e) {
152 LOGGER.warn("Skipping nested endpoint for " + propName + " (Unknown Object) " + LogFormatTools.getStackTop(e));
155 } else if (obj.isComplexType(propName)) {
157 final Introspector nestedObj = obj.newIntrospectorInstanceOfProperty(propName);
158 Set<String> newVisited = new HashSet<>();
159 newVisited.addAll(visited);
160 newVisited.add(nestedObj.getDbName());
161 beginAudit(nestedObj,
165 } catch (AAIUnknownObjectException e) {
166 LOGGER.warn("Skipping nested enpoint for " + propName + " (Unknown Object) "+ LogFormatTools.getStackTop(e));
174 * Populate logical name.
178 * @param currentUri the current uri
180 private void populateLogicalName(Introspector obj, String uri, String currentUri) {
182 if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
186 if (uri.endsWith("/relationship-list")) {
187 uri = uri.substring(0, uri.lastIndexOf("/"));
190 String logicalName = "";
194 if (!obj.getAllKeys().isEmpty()) {
196 Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
197 Matcher m = p.matcher(currentUri);
200 keys = StringUtils.join(obj.getAllKeys(), "-and-");
202 keys = StringUtils.join(obj.getAllKeys(), "-or-");
204 keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
205 if (!keys.isEmpty()) {
206 keys = "With" + keys;
210 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
212 if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
213 logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
214 } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf("/")))) {
215 logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf("/")));
218 endpointToLogicalName.put(currentUri, logicalName);
223 * Gets the logical names.
225 * @return the logical names
227 public Map<String, String> getLogicalNames() {
229 return endpointToLogicalName;
234 * Gets the endpoints.
236 * @return the endpoints
238 public List<String> getEndpoints() {
240 return this.getEndpoints("");
245 * Gets the endpoints.
247 * @param filterOut the filter out
248 * @return the endpoints
250 public List<String> getEndpoints(String filterOut) {
251 List<String> result = new ArrayList<>();
254 if (!filterOut.equals("")) {
255 p = Pattern.compile(filterOut);
258 for (String s : endpoints) {
277 public String toString() {
278 StringBuilder sb = new StringBuilder();
279 for (String s : endpoints) {
282 return sb.toString();
289 * @param filterOut the filter out
292 public String toString(String filterOut) {
293 StringBuilder sb = new StringBuilder();
294 Pattern p = Pattern.compile(filterOut);
296 for (String s : endpoints) {
302 return sb.toString();