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.List;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
30 import org.apache.commons.lang.StringUtils;
31 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
32 import org.openecomp.aai.introspection.Introspector;
33 import org.openecomp.aai.introspection.IntrospectorFactory;
34 import org.openecomp.aai.introspection.Loader;
35 import org.openecomp.aai.introspection.LoaderFactory;
36 import org.openecomp.aai.introspection.ModelType;
37 import org.openecomp.aai.introspection.Version;
38 import org.openecomp.aai.logging.LogLineBuilder;
40 import com.google.common.base.CaseFormat;
43 * The Class ListEndpoints.
45 public class ListEndpoints {
48 private DynamicJAXBContext context = null;
50 private final String start = "inventory";
52 private final String[] blacklist = { "search", "aai-internal", "models", "named-queries" };
54 private List<String> endpoints = new ArrayList<>();
56 private Map<String, String> endpointToLogicalName = new HashMap<String, String>();
58 private final LogLineBuilder llBuilder = new LogLineBuilder();
61 * Instantiates a new list endpoints.
63 * @param version the version
65 public ListEndpoints(Version version) {
67 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder);
68 Introspector start = loader.introspectorFromName(this.start);
70 beginAudit(start, "/aai/" + version);
80 private void beginAudit(Introspector obj, String uri) {
81 String currentUri = "";
83 if (!obj.getDbName().equals("inventory")) {
84 currentUri = uri + obj.getGenericURI();
88 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
91 if (!obj.isContainer()) {
92 endpoints.add(currentUri);
95 populateLogicalName(obj, uri, currentUri);
97 outer: for (String propName : obj.getProperties()) {
98 for (String item : blacklist) {
99 if (propName.equals(item)) {
103 if (obj.isListType(propName)) {
104 if (obj.isComplexGenericType(propName)) {
106 IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfNestedProperty(propName), llBuilder),
109 } else if (obj.isComplexType(propName)) {
110 beginAudit(IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfProperty(propName), llBuilder),
118 * Populate logical name.
122 * @param currentUri the current uri
124 private void populateLogicalName(Introspector obj, String uri, String currentUri) {
126 if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
130 if (uri.endsWith("/relationship-list")) {
131 uri = uri.substring(0, uri.lastIndexOf("/"));
134 String logicalName = "";
138 if (!obj.getAllKeys().isEmpty()) {
140 Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
141 Matcher m = p.matcher(currentUri);
144 keys = StringUtils.join(obj.getAllKeys(), "-and-");
146 keys = StringUtils.join(obj.getAllKeys(), "-or-");
148 keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
149 if (!keys.isEmpty()) {
150 keys = "With" + keys;
154 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
156 if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
157 logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
158 } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf("/")))) {
159 logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf("/")));
162 endpointToLogicalName.put(currentUri, logicalName);
167 * Gets the logical names.
169 * @return the logical names
171 public Map<String, String> getLogicalNames() {
173 return endpointToLogicalName;
178 * Gets the endpoints.
180 * @return the endpoints
182 public List<String> getEndpoints() {
184 return this.getEndpoints("");
189 * Gets the endpoints.
191 * @param filterOut the filter out
192 * @return the endpoints
194 public List<String> getEndpoints(String filterOut) {
195 List<String> result = new ArrayList<>();
198 if (!filterOut.equals("")) {
199 p = Pattern.compile(filterOut);
202 for (String s : endpoints) {
221 public String toString() {
222 StringBuilder sb = new StringBuilder();
223 for (String s : endpoints) {
226 return sb.toString();
233 * @param filterOut the filter out
236 public String toString(String filterOut) {
237 StringBuilder sb = new StringBuilder();
238 Pattern p = Pattern.compile(filterOut);
240 for (String s : endpoints) {
246 return sb.toString();