2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.aai.audit;
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;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
33 import org.apache.commons.lang.StringUtils;
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;
47 * The Class ListEndpoints.
49 public class ListEndpoints {
51 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ListEndpoints.class);
53 private final String start = "inventory";
54 private final String[] blacklist = { "search", "aai-internal" };
56 private List<String> endpoints = new ArrayList<>();
57 private Map<String, String> endpointToLogicalName = new HashMap<String, String>();
62 * @param args the arguments
64 public static void main(String[] args) {
65 ListEndpoints endPoints = new ListEndpoints(AAIProperties.LATEST);
67 System.out.println(endPoints.toString("relationship-list"));
72 * Instantiates a new list endpoints.
74 * @param version the version
76 public ListEndpoints(Version version) {
78 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
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");
95 private void beginAudit(Introspector obj, String uri, Set<String> visited) {
97 String currentUri = "";
99 if (!obj.getDbName().equals("inventory")) {
100 currentUri = uri + obj.getGenericURI();
104 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
107 if (!obj.isContainer()) {
108 endpoints.add(currentUri);
111 String dbName = obj.getDbName();
113 populateLogicalName(obj, uri, currentUri);
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)) {
121 endpoints.add(currentUri + obj.getLoader().introspectorFromName(key).getGenericURI());
122 } catch (AAIUnknownObjectException e) {
123 LOGGER.warn("Skipping endpoint for " + key + " (Unknown object)", e);
129 outer: for (String propName : props) {
131 for (String item : blacklist) {
132 if (propName.equals(item)) {
136 if (obj.isListType(propName)) {
137 if (obj.isComplexGenericType(propName)) {
139 final Introspector nestedObj = obj.newIntrospectorInstanceOfNestedProperty(propName);
140 Set<String> newVisited = new HashSet<>();
141 newVisited.addAll(visited);
142 newVisited.add(nestedObj.getDbName());
148 } catch (AAIUnknownObjectException e) {
149 LOGGER.warn("Skipping nested endpoint for " + propName + " (Unknown Object)", e);
152 } else if (obj.isComplexType(propName)) {
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,
162 } catch (AAIUnknownObjectException e) {
163 LOGGER.warn("Skipping nested enpoint for " + propName + " (Unknown Object)", e);
171 * Populate logical name.
175 * @param currentUri the current uri
177 private void populateLogicalName(Introspector obj, String uri, String currentUri) {
179 if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
183 if (uri.endsWith("/relationship-list")) {
184 uri = uri.substring(0, uri.lastIndexOf("/"));
187 String logicalName = "";
191 if (!obj.getAllKeys().isEmpty()) {
193 Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
194 Matcher m = p.matcher(currentUri);
197 keys = StringUtils.join(obj.getAllKeys(), "-and-");
199 keys = StringUtils.join(obj.getAllKeys(), "-or-");
201 keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
202 if (!keys.isEmpty()) {
203 keys = "With" + keys;
207 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
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("/")));
215 endpointToLogicalName.put(currentUri, logicalName);
220 * Gets the logical names.
222 * @return the logical names
224 public Map<String, String> getLogicalNames() {
226 return endpointToLogicalName;
231 * Gets the endpoints.
233 * @return the endpoints
235 public List<String> getEndpoints() {
237 return this.getEndpoints("");
242 * Gets the endpoints.
244 * @param filterOut the filter out
245 * @return the endpoints
247 public List<String> getEndpoints(String filterOut) {
248 List<String> result = new ArrayList<>();
251 if (!filterOut.equals("")) {
252 p = Pattern.compile(filterOut);
255 for (String s : endpoints) {
274 public String toString() {
275 StringBuilder sb = new StringBuilder();
276 for (String s : endpoints) {
279 return sb.toString();
286 * @param filterOut the filter out
289 public String toString(String filterOut) {
290 StringBuilder sb = new StringBuilder();
291 Pattern p = Pattern.compile(filterOut);
293 for (String s : endpoints) {
299 return sb.toString();