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 com.google.common.base.CaseFormat;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import org.apache.commons.lang.StringUtils;
31 import org.openecomp.aai.introspection.Introspector;
32 import org.openecomp.aai.introspection.IntrospectorFactory;
33 import org.openecomp.aai.introspection.Loader;
34 import org.openecomp.aai.introspection.LoaderFactory;
35 import org.openecomp.aai.introspection.ModelType;
36 import org.openecomp.aai.introspection.Version;
37 import org.openecomp.aai.logging.LogLineBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The Class ListEndpoints.
44 public class ListEndpoints {
47 private static final Logger log = LoggerFactory.getLogger(ListEndpoints.class);
48 private static final String INVENTORY = "inventory";
50 private static final String[] blacklist = {"search", "aai-internal", "models", "named-queries"};
52 private List<String> endpoints = new ArrayList<>();
54 private Map<String, String> endpointToLogicalName = new HashMap<>();
56 private final LogLineBuilder llBuilder = new LogLineBuilder();
59 * Instantiates a new list endpoints.
61 * @param version the version
63 public ListEndpoints(Version version) {
65 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder);
67 Introspector introspector = loader.introspectorFromName(INVENTORY);
69 beginAudit(introspector, "/aai/" + version);
71 log.error(String.format("failed to create logger for %s version", version));
81 private void beginAudit(Introspector obj, String uri) {
82 String currentUri = getCurrentUri(obj, uri);
84 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
87 if (!obj.isContainer()) {
88 endpoints.add(currentUri);
91 populateLogicalName(obj, uri, currentUri);
93 handleAudit(obj, currentUri);
96 private String getCurrentUri(Introspector obj, String uri) {
97 if (!obj.getDbName().equals(INVENTORY)) {
98 return uri + obj.getGenericURI();
103 private void handleAudit(Introspector obj, String currentUri) {
105 for (String propName : obj.getProperties()) {
106 for (String item : blacklist) {
107 if (propName.equals(item)) {
111 if (obj.isListType(propName)) {
112 if (obj.isComplexGenericType(propName)) {
115 .newInstance(ModelType.MOXY, obj.newInstanceOfNestedProperty(propName), llBuilder),
118 } else if (obj.isComplexType(propName)) {
120 IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfProperty(propName), llBuilder),
127 * Populate logical name.
130 * @param uriString the uri
131 * @param currentUri the current uri
133 private void populateLogicalName(Introspector obj, String uriString, String currentUri) {
135 if (obj.getDbName().equals(INVENTORY) || currentUri.split("/").length <= 4 || currentUri
136 .endsWith("relationship-list")) {
140 if (uriString.endsWith("/relationship-list")) {
141 uri = uriString.substring(0, uriString.lastIndexOf('/'));
147 if (!obj.getAllKeys().isEmpty()) {
149 Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
150 Matcher m = p.matcher(currentUri);
153 keys = StringUtils.join(obj.getAllKeys(), "-and-");
155 keys = StringUtils.join(obj.getAllKeys(), "-or-");
157 keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
158 if (!keys.isEmpty()) {
159 keys = "With" + keys;
163 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
165 if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
166 logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
167 } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf('/')))) {
168 logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf('/')));
171 endpointToLogicalName.put(currentUri, logicalName);
175 * Gets the logical names.
177 * @return the logical names
179 public Map<String, String> getLogicalNames() {
181 return endpointToLogicalName;
185 * Gets the endpoints.
187 * @return the endpoints
189 public List<String> getEndpoints() {
191 return this.getEndpoints("");
195 * Gets the endpoints.
197 * @param filterOut the filter out
198 * @return the endpoints
200 public List<String> getEndpoints(String filterOut) {
201 List<String> result = new ArrayList<>();
204 if (!"".equals(filterOut)) {
205 p = Pattern.compile(filterOut);
207 for (String s : endpoints) {
224 public String toString() {
225 StringBuilder sb = new StringBuilder();
226 for (String s : endpoints) {
227 sb.append(s).append("\n");
229 return sb.toString();
235 * @param filterOut the filter out
238 public String toString(String filterOut) {
239 StringBuilder sb = new StringBuilder();
240 Pattern p = Pattern.compile(filterOut);
242 for (String s : endpoints) {
245 sb.append(s).append("\n");
248 return sb.toString();