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=========================================================
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;
35 import org.onap.aai.config.SpringContextAware;
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.setup.SchemaVersion;
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;
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 EELFLogger LOGGER = EELFManager.getInstance().getLogger(ListEndpoints.class);
58 private final String start = "inventory";
59 private final String[] blacklist = { "search", "aai-internal" };
61 private List<String> endpoints = new ArrayList<>();
62 private Map<String, String> endpointToLogicalName = new HashMap<String, String>();
67 * @param args the arguments
69 public static void main(String[] args) {
71 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
72 "org.onap.aai.config",
76 String schemaUriBasePath = context.getEnvironment().getProperty("schema.uri.base.path");
78 if(schemaUriBasePath == null){
79 String errorMsg = "Unable to find the property schema.uri.base.path,"
80 +" please check if specified in system property or in schema-ingest.properties";
81 System.err.println(errorMsg);
82 LOGGER.error(errorMsg);
85 SchemaVersions schemaVersions = context.getBean(SchemaVersions.class);
86 ListEndpoints endPoints = new ListEndpoints(schemaUriBasePath, schemaVersions.getDefaultVersion());
88 LOGGER.info(endPoints.toString("relationship-list"));
92 * Instantiates a new list endpoints.
94 * @param version the version
96 public ListEndpoints(String basePath, SchemaVersion version) {
98 Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY, version);
101 final Introspector start = loader.introspectorFromName(this.start);
102 Set<String> startMap = new HashSet<>();
103 beginAudit(start, basePath + "/" + version, startMap);
104 } catch (AAIUnknownObjectException e) {
105 throw new RuntimeException("Failed to find object " + this.start + ", cannot run ListEndpoints audit");
115 private void beginAudit(Introspector obj, String uri, Set<String> visited) {
117 String currentUri = "";
119 if (!obj.getDbName().equals("inventory")) {
120 currentUri = uri + obj.getGenericURI();
124 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
127 if (!obj.isContainer()) {
128 endpoints.add(currentUri);
131 String dbName = obj.getDbName();
133 populateLogicalName(obj, uri, currentUri);
135 Set<String> properties = obj.getProperties();
136 Set<String> props = new LinkedHashSet<>(properties);
137 if (obj.isContainer()) {
138 for (String key : visited) {
139 if (props.remove(key)) {
141 endpoints.add(currentUri + obj.getLoader().introspectorFromName(key).getGenericURI());
142 } catch (AAIUnknownObjectException e) {
143 LOGGER.warn("Skipping endpoint for " + key + " (Unknown object) " + LogFormatTools.getStackTop(e));
149 outer: for (String propName : props) {
151 for (String item : blacklist) {
152 if (propName.equals(item)) {
156 if (obj.isListType(propName)) {
157 if (obj.isComplexGenericType(propName)) {
159 final Introspector nestedObj = obj.newIntrospectorInstanceOfNestedProperty(propName);
160 Set<String> newVisited = new HashSet<>();
161 newVisited.addAll(visited);
162 newVisited.add(nestedObj.getDbName());
168 } catch (AAIUnknownObjectException e) {
169 LOGGER.warn("Skipping nested endpoint for " + propName + " (Unknown Object) " + LogFormatTools.getStackTop(e));
172 } else if (obj.isComplexType(propName)) {
174 final Introspector nestedObj = obj.newIntrospectorInstanceOfProperty(propName);
175 Set<String> newVisited = new HashSet<>();
176 newVisited.addAll(visited);
177 newVisited.add(nestedObj.getDbName());
178 beginAudit(nestedObj,
182 } catch (AAIUnknownObjectException e) {
183 LOGGER.warn("Skipping nested enpoint for " + propName + " (Unknown Object) "+ LogFormatTools.getStackTop(e));
191 * Populate logical name.
195 * @param currentUri the current uri
197 private void populateLogicalName(Introspector obj, String uri, String currentUri) {
199 if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
203 if (uri.endsWith("/relationship-list")) {
204 uri = uri.substring(0, uri.lastIndexOf("/"));
207 String logicalName = "";
211 if (!obj.getAllKeys().isEmpty()) {
213 Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
214 Matcher m = p.matcher(currentUri);
217 keys = StringUtils.join(obj.getAllKeys(), "-and-");
219 keys = StringUtils.join(obj.getAllKeys(), "-or-");
221 keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
222 if (!keys.isEmpty()) {
223 keys = "With" + keys;
227 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
229 if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
230 logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
231 } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf("/")))) {
232 logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf("/")));
235 endpointToLogicalName.put(currentUri, logicalName);
240 * Gets the logical names.
242 * @return the logical names
244 public Map<String, String> getLogicalNames() {
246 return endpointToLogicalName;
251 * Gets the endpoints.
253 * @return the endpoints
255 public List<String> getEndpoints() {
257 return this.getEndpoints("");
262 * Gets the endpoints.
264 * @param filterOut the filter out
265 * @return the endpoints
267 public List<String> getEndpoints(String filterOut) {
268 List<String> result = new ArrayList<>();
271 if (!filterOut.equals("")) {
272 p = Pattern.compile(filterOut);
275 for (String s : endpoints) {
294 public String toString() {
295 StringBuilder sb = new StringBuilder();
296 for (String s : endpoints) {
299 return sb.toString();
306 * @param filterOut the filter out
309 public String toString(String filterOut) {
310 StringBuilder sb = new StringBuilder();
311 Pattern p = Pattern.compile(filterOut);
313 for (String s : endpoints) {
319 return sb.toString();