2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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 package org.onap.aai.audit;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.LinkedHashSet;
26 import java.util.List;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 import org.apache.commons.lang.StringUtils;
34 import org.onap.aai.db.props.AAIProperties;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.LoaderFactory;
38 import org.onap.aai.introspection.ModelType;
39 import org.onap.aai.introspection.Version;
40 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
41 import org.onap.aai.logging.LogFormatTools;
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 static String start = "inventory";
55 private final static 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(start)) {
101 currentUri = uri + obj.getGenericURI();
105 if ("relationship-data".equals(obj.getName()) || "related-to-property".equals(obj.getName())) {
108 if (!obj.isContainer()) {
109 endpoints.add(currentUri);
112 populateLogicalName(obj, uri, currentUri);
114 Set<String> properties = obj.getProperties();
115 Set<String> props = new LinkedHashSet<>(properties);
116 if (obj.isContainer()) {
117 for (String key : visited) {
118 if (props.remove(key)) {
120 endpoints.add(currentUri + obj.getLoader().introspectorFromName(key).getGenericURI());
121 } catch (AAIUnknownObjectException e) {
122 LOGGER.warn("Skipping endpoint for " + key + " (Unknown object) " + LogFormatTools.getStackTop(e));
128 outer: for (String propName : props) {
130 for (String item : blacklist) {
131 if (propName.equals(item)) {
135 if (obj.isListType(propName)) {
136 if (obj.isComplexGenericType(propName)) {
138 final Introspector nestedObj = obj.newIntrospectorInstanceOfNestedProperty(propName);
139 Set<String> newVisited = new HashSet<>();
140 newVisited.addAll(visited);
141 newVisited.add(nestedObj.getDbName());
147 } catch (AAIUnknownObjectException e) {
148 LOGGER.warn("Skipping nested endpoint for " + propName + " (Unknown Object) " + LogFormatTools.getStackTop(e));
151 } else if (obj.isComplexType(propName)) {
153 final Introspector nestedObj = obj.newIntrospectorInstanceOfProperty(propName);
154 Set<String> newVisited = new HashSet<>();
155 newVisited.addAll(visited);
156 newVisited.add(nestedObj.getDbName());
157 beginAudit(nestedObj,
161 } catch (AAIUnknownObjectException e) {
162 LOGGER.warn("Skipping nested enpoint for " + propName + " (Unknown Object) "+ LogFormatTools.getStackTop(e));
170 * Populate logical name.
174 * @param currentUri the current uri
176 private void populateLogicalName(Introspector obj, String uri, String currentUri) {
178 if (obj.getDbName().equals(start) || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
182 if (uri.endsWith("/relationship-list")) {
183 uri = uri.substring(0, uri.lastIndexOf("/"));
186 String logicalName = "";
190 if (!obj.getAllKeys().isEmpty()) {
192 Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
193 Matcher m = p.matcher(currentUri);
196 keys = StringUtils.join(obj.getAllKeys(), "-and-");
198 keys = StringUtils.join(obj.getAllKeys(), "-or-");
200 keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
201 if (!keys.isEmpty()) {
202 keys = "With" + keys;
206 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
208 if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
209 logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
210 } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf("/")))) {
211 logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf("/")));
214 endpointToLogicalName.put(currentUri, logicalName);
219 * Gets the logical names.
221 * @return the logical names
223 public Map<String, String> getLogicalNames() {
225 return endpointToLogicalName;
230 * Gets the endpoints.
232 * @return the endpoints
234 public List<String> getEndpoints() {
236 return this.getEndpoints("");
241 * Gets the endpoints.
243 * @param filterOut the filter out
244 * @return the endpoints
246 public List<String> getEndpoints(String filterOut) {
247 List<String> result = new ArrayList<>();
250 if (!filterOut.isEmpty()) {
251 p = Pattern.compile(filterOut);
254 for (String s : endpoints) {
273 public String toString() {
274 StringBuilder sb = new StringBuilder();
275 for (String s : endpoints) {
278 return sb.toString();
285 * @param filterOut the filter out
288 public String toString(String filterOut) {
289 StringBuilder sb = new StringBuilder();
290 Pattern p = Pattern.compile(filterOut);
292 for (String s : endpoints) {
298 return sb.toString();