aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/audit/ListEndpoints.java
blob: e354973574e281eb585d62b574fc2bdfcce5ef69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.aai.audit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.openecomp.aai.introspection.Introspector;
import org.openecomp.aai.introspection.IntrospectorFactory;
import org.openecomp.aai.introspection.Loader;
import org.openecomp.aai.introspection.LoaderFactory;
import org.openecomp.aai.introspection.ModelType;
import org.openecomp.aai.introspection.Version;
import org.openecomp.aai.logging.LogLineBuilder;

import com.google.common.base.CaseFormat;

/**
 * The Class ListEndpoints.
 */
public class ListEndpoints {

	
	private DynamicJAXBContext context = null;
	
	private final String start = "inventory";
	
	private final String[] blacklist = { "search", "aai-internal", "models", "named-queries" };
	
	private List<String> endpoints = new ArrayList<>();
	
	private Map<String, String> endpointToLogicalName = new HashMap<String, String>();
	
	private final LogLineBuilder llBuilder = new LogLineBuilder();

	/**
	 * Instantiates a new list endpoints.
	 *
	 * @param version the version
	 */
	public ListEndpoints(Version version) {

		Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder);
		Introspector start = loader.introspectorFromName(this.start);

		beginAudit(start, "/aai/" + version);

	}

	/**
	 * Begin audit.
	 *
	 * @param obj the obj
	 * @param uri the uri
	 */
	private void beginAudit(Introspector obj, String uri) {
		String currentUri = "";

		if (!obj.getDbName().equals("inventory")) {
			currentUri = uri + obj.getGenericURI();
		} else {
			currentUri = uri;
		}
		if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
			return;
		}
		if (!obj.isContainer()) {
			endpoints.add(currentUri);
		}
		
		populateLogicalName(obj, uri, currentUri);
		
		outer: for (String propName : obj.getProperties()) {
			for (String item : blacklist) {
				if (propName.equals(item)) {
					continue outer;
				}
			}
			if (obj.isListType(propName)) {
				if (obj.isComplexGenericType(propName)) {
					beginAudit(
							IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfNestedProperty(propName), llBuilder),
							currentUri);
				}
			} else if (obj.isComplexType(propName)) {
				beginAudit(IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfProperty(propName), llBuilder),
						currentUri);
			}
		}

	}

	/**
	 * Populate logical name.
	 *
	 * @param obj the obj
	 * @param uri the uri
	 * @param currentUri the current uri
	 */
	private void populateLogicalName(Introspector obj, String uri, String currentUri) {

		if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
			return;
		}
		
		if (uri.endsWith("/relationship-list")) {
			uri = uri.substring(0, uri.lastIndexOf("/"));
		}

		String logicalName = "";
		String keys = "";
		

		if (!obj.getAllKeys().isEmpty()) {
			
			Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
			Matcher m = p.matcher(currentUri);
			
			if (m.find()) {
				keys = StringUtils.join(obj.getAllKeys(), "-and-");
			} else {
				keys = StringUtils.join(obj.getAllKeys(), "-or-");
			}
			keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
			if (!keys.isEmpty()) {
				keys = "With" + keys;
			}
		}

		logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
		
		if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) {
			logicalName = logicalName + "From" + endpointToLogicalName.get(uri);
 		} else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf("/")))) {
 			logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf("/")));
 		}

		endpointToLogicalName.put(currentUri, logicalName);
		
	}
	
	/**
	 * Gets the logical names.
	 *
	 * @return the logical names
	 */
	public Map<String, String> getLogicalNames() {
		
		return endpointToLogicalName;

	}

	/**
	 * Gets the endpoints.
	 *
	 * @return the endpoints
	 */
	public List<String> getEndpoints() {

		return this.getEndpoints("");

	}

	/**
	 * Gets the endpoints.
	 *
	 * @param filterOut the filter out
	 * @return the endpoints
	 */
	public List<String> getEndpoints(String filterOut) {
		List<String> result = new ArrayList<>();
		Pattern p = null;
		Matcher m = null;
		if (!filterOut.equals("")) {
			p = Pattern.compile(filterOut);
			m = null;
		}
		for (String s : endpoints) {
			if (p != null) {
				m = p.matcher(s);
				if (m.find()) {
					continue;
				}
			}

			result.add(s);
		}

		return result;

	}

	/** 
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (String s : endpoints) {
			sb.append(s + "\n");
		}
		return sb.toString();

	}

	/**
	 * To string.
	 *
	 * @param filterOut the filter out
	 * @return the string
	 */
	public String toString(String filterOut) {
		StringBuilder sb = new StringBuilder();
		Pattern p = Pattern.compile(filterOut);
		Matcher m = null;
		for (String s : endpoints) {
			m = p.matcher(s);
			if (!m.find()) {
				sb.append(s + "\n");
			}
		}
		return sb.toString();
	}

}