e354973574e281eb585d62b574fc2bdfcce5ef69
[aai/aai-service.git] / ajsc-aai / src / main / java / org / openecomp / aai / audit / ListEndpoints.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.audit;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
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;
39
40 import com.google.common.base.CaseFormat;
41
42 /**
43  * The Class ListEndpoints.
44  */
45 public class ListEndpoints {
46
47         
48         private DynamicJAXBContext context = null;
49         
50         private final String start = "inventory";
51         
52         private final String[] blacklist = { "search", "aai-internal", "models", "named-queries" };
53         
54         private List<String> endpoints = new ArrayList<>();
55         
56         private Map<String, String> endpointToLogicalName = new HashMap<String, String>();
57         
58         private final LogLineBuilder llBuilder = new LogLineBuilder();
59
60         /**
61          * Instantiates a new list endpoints.
62          *
63          * @param version the version
64          */
65         public ListEndpoints(Version version) {
66
67                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder);
68                 Introspector start = loader.introspectorFromName(this.start);
69
70                 beginAudit(start, "/aai/" + version);
71
72         }
73
74         /**
75          * Begin audit.
76          *
77          * @param obj the obj
78          * @param uri the uri
79          */
80         private void beginAudit(Introspector obj, String uri) {
81                 String currentUri = "";
82
83                 if (!obj.getDbName().equals("inventory")) {
84                         currentUri = uri + obj.getGenericURI();
85                 } else {
86                         currentUri = uri;
87                 }
88                 if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) {
89                         return;
90                 }
91                 if (!obj.isContainer()) {
92                         endpoints.add(currentUri);
93                 }
94                 
95                 populateLogicalName(obj, uri, currentUri);
96                 
97                 outer: for (String propName : obj.getProperties()) {
98                         for (String item : blacklist) {
99                                 if (propName.equals(item)) {
100                                         continue outer;
101                                 }
102                         }
103                         if (obj.isListType(propName)) {
104                                 if (obj.isComplexGenericType(propName)) {
105                                         beginAudit(
106                                                         IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfNestedProperty(propName), llBuilder),
107                                                         currentUri);
108                                 }
109                         } else if (obj.isComplexType(propName)) {
110                                 beginAudit(IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfProperty(propName), llBuilder),
111                                                 currentUri);
112                         }
113                 }
114
115         }
116
117         /**
118          * Populate logical name.
119          *
120          * @param obj the obj
121          * @param uri the uri
122          * @param currentUri the current uri
123          */
124         private void populateLogicalName(Introspector obj, String uri, String currentUri) {
125
126                 if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) {
127                         return;
128                 }
129                 
130                 if (uri.endsWith("/relationship-list")) {
131                         uri = uri.substring(0, uri.lastIndexOf("/"));
132                 }
133
134                 String logicalName = "";
135                 String keys = "";
136                 
137
138                 if (!obj.getAllKeys().isEmpty()) {
139                         
140                         Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$");
141                         Matcher m = p.matcher(currentUri);
142                         
143                         if (m.find()) {
144                                 keys = StringUtils.join(obj.getAllKeys(), "-and-");
145                         } else {
146                                 keys = StringUtils.join(obj.getAllKeys(), "-or-");
147                         }
148                         keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys);
149                         if (!keys.isEmpty()) {
150                                 keys = "With" + keys;
151                         }
152                 }
153
154                 logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys;
155                 
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("/")));
160                 }
161
162                 endpointToLogicalName.put(currentUri, logicalName);
163                 
164         }
165         
166         /**
167          * Gets the logical names.
168          *
169          * @return the logical names
170          */
171         public Map<String, String> getLogicalNames() {
172                 
173                 return endpointToLogicalName;
174
175         }
176
177         /**
178          * Gets the endpoints.
179          *
180          * @return the endpoints
181          */
182         public List<String> getEndpoints() {
183
184                 return this.getEndpoints("");
185
186         }
187
188         /**
189          * Gets the endpoints.
190          *
191          * @param filterOut the filter out
192          * @return the endpoints
193          */
194         public List<String> getEndpoints(String filterOut) {
195                 List<String> result = new ArrayList<>();
196                 Pattern p = null;
197                 Matcher m = null;
198                 if (!filterOut.equals("")) {
199                         p = Pattern.compile(filterOut);
200                         m = null;
201                 }
202                 for (String s : endpoints) {
203                         if (p != null) {
204                                 m = p.matcher(s);
205                                 if (m.find()) {
206                                         continue;
207                                 }
208                         }
209
210                         result.add(s);
211                 }
212
213                 return result;
214
215         }
216
217         /** 
218          * {@inheritDoc}
219          */
220         @Override
221         public String toString() {
222                 StringBuilder sb = new StringBuilder();
223                 for (String s : endpoints) {
224                         sb.append(s + "\n");
225                 }
226                 return sb.toString();
227
228         }
229
230         /**
231          * To string.
232          *
233          * @param filterOut the filter out
234          * @return the string
235          */
236         public String toString(String filterOut) {
237                 StringBuilder sb = new StringBuilder();
238                 Pattern p = Pattern.compile(filterOut);
239                 Matcher m = null;
240                 for (String s : endpoints) {
241                         m = p.matcher(s);
242                         if (!m.find()) {
243                                 sb.append(s + "\n");
244                         }
245                 }
246                 return sb.toString();
247         }
248
249 }