2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.aai.audit;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import com.google.common.base.CaseFormat;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.LinkedHashSet;
33 import java.util.List;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 import org.apache.commons.lang.StringUtils;
40 import org.onap.aai.config.SpringContextAware;
41 import org.onap.aai.introspection.Introspector;
42 import org.onap.aai.introspection.Loader;
43 import org.onap.aai.introspection.LoaderFactory;
44 import org.onap.aai.introspection.ModelType;
45 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
46 import org.onap.aai.logging.LogFormatTools;
47 import org.onap.aai.setup.SchemaVersion;
48 import org.onap.aai.setup.SchemaVersions;
49 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
52 * The Class ListEndpoints.
54 public class ListEndpoints {
56 private static final Logger LOGGER = LoggerFactory.getLogger(ListEndpoints.class);
58 private static final String START = "inventory";
59 private static final String[] blacklist = {"search", "aai-internal"};
61 private List<String> endpoints = new ArrayList<>();
62 private Map<String, String> endpointToLogicalName = new HashMap<>();
67 * @param args the arguments
69 public static void main(String[] args) {
71 AnnotationConfigApplicationContext context =
72 new AnnotationConfigApplicationContext("org.onap.aai.config", "org.onap.aai.setup");
74 String schemaUriBasePath = context.getEnvironment().getProperty("schema.uri.base.path");
76 if (schemaUriBasePath == null) {
77 String errorMsg = "Unable to find the property schema.uri.base.path,"
78 + " please check if specified in system property or in schema-ingest.properties";
79 LOGGER.error(errorMsg);
82 SchemaVersions schemaVersions = context.getBean(SchemaVersions.class);
83 ListEndpoints endPoints = new ListEndpoints(schemaUriBasePath, schemaVersions.getDefaultVersion());
85 LOGGER.info(endPoints.toString("relationship-list"));
89 * Instantiates a new list endpoints.
91 * @param version the version
93 public ListEndpoints(String basePath, SchemaVersion version) {
95 Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY, version);
98 final Introspector start = loader.introspectorFromName(START);
99 Set<String> startMap = new HashSet<>();
100 beginAudit(start, basePath + "/" + version, startMap);
101 } catch (AAIUnknownObjectException e) {
102 throw new RuntimeException("Failed to find object " + START + ", cannot run ListEndpoints audit");
112 private void beginAudit(Introspector obj, String uri, Set<String> visited) {
114 String currentUri = "";
116 if (!obj.getDbName().equals(START)) {
117 currentUri = uri + obj.getGenericURI();
121 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
124 if (!obj.isContainer()) {
125 endpoints.add(currentUri);
128 populateLogicalName(obj, uri, currentUri);
130 Set<String> properties = obj.getProperties();
131 Set<String> props = new LinkedHashSet<>(properties);
132 if (obj.isContainer()) {
133 for (String key : visited) {
134 if (props.remove(key)) {
136 endpoints.add(currentUri + obj.getLoader().introspectorFromName(key).getGenericURI());
137 } catch (AAIUnknownObjectException e) {
139 "Skipping endpoint for " + key + " (Unknown object) " + LogFormatTools.getStackTop(e));
145 outer: for (String propName : props) {
147 for (String item : blacklist) {
148 if (propName.equals(item)) {
152 if (obj.isListType(propName)) {
153 if (obj.isComplexGenericType(propName)) {
155 final Introspector nestedObj = obj.newIntrospectorInstanceOfNestedProperty(propName);
156 Set<String> newVisited = new HashSet<>();
157 newVisited.addAll(visited);
158 newVisited.add(nestedObj.getDbName());
159 beginAudit(nestedObj, currentUri, newVisited);
160 } catch (AAIUnknownObjectException e) {
161 LOGGER.warn("Skipping nested endpoint for " + propName + " (Unknown Object) "
162 + LogFormatTools.getStackTop(e));
165 } else if (obj.isComplexType(propName)) {
167 final Introspector nestedObj = obj.newIntrospectorInstanceOfProperty(propName);
168 Set<String> newVisited = new HashSet<>();
169 newVisited.addAll(visited);
170 newVisited.add(nestedObj.getDbName());
171 beginAudit(nestedObj, currentUri, visited);
172 } catch (AAIUnknownObjectException e) {
173 LOGGER.warn("Skipping nested enpoint for " + propName + " (Unknown Object) "
174 + LogFormatTools.getStackTop(e));
182 * Populate logical name.
186 * @param currentUri the current uri
188 private void populateLogicalName(Introspector obj, String uri, String currentUri) {
190 if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4
191 || currentUri.endsWith("relationship-list")) {
195 if (uri.endsWith("/relationship-list")) {
196 uri = uri.substring(0, uri.lastIndexOf('/'));
199 String logicalName = "";
202 if (!obj.getAllKeys().isEmpty()) {
204 Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
205 Matcher m = p.matcher(currentUri);
208 keys = StringUtils.join(obj.getAllKeys(), "-and-");
210 keys = StringUtils.join(obj.getAllKeys(), "-or-");
212 keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
213 if (!keys.isEmpty()) {
214 keys = "With" + keys;
218 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
220 if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
221 logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
222 } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf('/')))) {
223 logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf('/')));
226 endpointToLogicalName.put(currentUri, logicalName);
231 * Gets the logical names.
233 * @return the logical names
235 public Map<String, String> getLogicalNames() {
237 return endpointToLogicalName;
242 * Gets the endpoints.
244 * @return the endpoints
246 public List<String> getEndpoints() {
248 return this.getEndpoints("");
253 * Gets the endpoints.
255 * @param filterOut the filter out
256 * @return the endpoints
258 public List<String> getEndpoints(String filterOut) {
259 List<String> result = new ArrayList<>();
262 if (!filterOut.equals("")) {
263 p = Pattern.compile(filterOut);
265 for (String s : endpoints) {
284 public String toString() {
285 StringBuilder sb = new StringBuilder();
286 for (String s : endpoints) {
289 return sb.toString();
296 * @param filterOut the filter out
299 public String toString(String filterOut) {
300 StringBuilder sb = new StringBuilder();
301 Pattern p = Pattern.compile(filterOut);
303 for (String s : endpoints) {
309 return sb.toString();