2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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.onap.aai.parsers.uri;
23 import java.io.UnsupportedEncodingException;
27 import javax.ws.rs.core.MultivaluedHashMap;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.UriBuilder;
31 import org.onap.aai.config.SpringContextAware;
32 import org.onap.aai.edges.enums.EdgeType;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.LoaderFactory;
37 import org.onap.aai.logging.ErrorLogHelper;
38 import org.onap.aai.parsers.exceptions.DoesNotStartWithValidNamespaceException;
39 import org.onap.aai.rest.RestTokens;
40 import org.onap.aai.schema.enums.ObjectMetadata;
41 import org.onap.aai.setup.SchemaVersion;
42 import org.onap.aai.util.AAIConfig;
43 import org.springframework.web.util.UriUtils;
46 * The Class URIParser.
48 public class URIParser {
50 private URI uri = null;
52 protected Loader loader = null;
54 protected Loader originalLoader = null;
56 private URI originalURI = null;
58 private MultivaluedMap<String, String> queryParams = null;
61 * Instantiates a new URI parser.
63 * @param loader the loader
66 public URIParser(Loader loader, URI uri) {
69 this.originalLoader = loader;
70 // Load the latest version because we need it for cloud region
76 * Instantiates a new URI parser.
78 * @param loader the loader
80 * @param queryParams the query params
82 public URIParser(Loader loader, URI uri, MultivaluedMap<String, String> queryParams) {
84 this.queryParams = queryParams;
87 public Loader getLoader() {
94 * Gets the original URI.
96 * @return the original URI
98 public URI getOriginalURI() {
99 return this.originalURI;
106 * @throws UnsupportedEncodingException the unsupported encoding exception
107 * @throws AAIException the AAI exception
109 public void parse(Parsable p) throws UnsupportedEncodingException, AAIException {
111 boolean isRelative = false;
112 uri = this.trimURI(uri);
113 uri = handleCloudRegion(p.getCloudRegionTransform(), uri);
114 if (p.useOriginalLoader()) {
115 this.loader = this.originalLoader;
117 this.originalURI = UriBuilder.fromPath(uri.getRawPath()).build();
118 if (uri.getRawPath().startsWith("./")) {
119 uri = new URI(uri.getRawPath().replaceFirst("\\./", ""));
122 String[] parts = uri.getRawPath().split("/");
123 Introspector validNamespaces = loader.introspectorFromName("inventory");
124 Set<String> keys = null;
126 Introspector previousObj = null;
127 EdgeType type = EdgeType.TREE;
128 for (int i = 0; i < parts.length;) {
130 Introspector introspector = null;
131 if (part.equals(RestTokens.COUSIN.toString())) {
132 if (i == parts.length - 1) {
133 throw new AAIException("AAI_3000",
134 uri + " not a valid path. Cannot end in " + RestTokens.COUSIN);
136 introspector = loader.introspectorFromName(parts[i + 1]);
137 if (null == previousObj) {
138 throw new AAIException("AAI_3001");
140 if (previousObj.isContainer() && introspector.isContainer()) {
141 throw new AAIException("AAI_3000", uri + " not a valid path. Cannot chain plurals together");
143 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
144 if (i == parts.length - 2 && queryParams != null) {
145 Set<String> queryKeys = queryParams.keySet();
146 for (String key : queryKeys) {
147 uriKeys.put(key, queryParams.get(key));
151 if (introspector.isContainer()) {
152 boolean isFinalContainer = i == parts.length - 2;
154 * Related-to could be COUSIN OR TREE and in some cases BOTH. So Let EdgeRuleBuilder use all the
157 p.processContainer(introspector, EdgeType.ALL, uriKeys, isFinalContainer);
159 previousObj = introspector;
164 introspector = loader.introspectorFromName(part);
165 if (introspector != null) {
167 // previous has current as property
168 if (previousObj != null && !previousObj.hasChild(introspector)
169 && !previousObj.getDbName().equals("nodes")) {
170 throw new AAIException("AAI_3001", uri + " not a valid path. " + part + " not valid");
171 } else if (previousObj == null) {
172 String abstractType = introspector.getMetadata(ObjectMetadata.ABSTRACT);
173 if (abstractType == null) {
176 // first time through, make sure it starts from a namespace
177 // ignore abstract types
178 if (!isRelative && !abstractType.equals("true") && !validNamespaces.hasChild(introspector)) {
179 throw new DoesNotStartWithValidNamespaceException(
180 uri + " not a valid path. It does not start from a valid namespace");
184 keys = introspector.getKeys();
185 if (keys.size() > 0) {
186 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
188 if (i == parts.length && queryParams != null) {
189 Set<String> queryKeys = queryParams.keySet();
190 for (String key : queryKeys) {
191 uriKeys.put(key, queryParams.get(key));
194 for (String key : keys) {
195 part = UriUtils.decode(parts[i], "UTF-8");
197 introspector.setValue(key, part);
199 // skip this for further processing
204 p.processObject(introspector, type, uriKeys);
205 type = EdgeType.TREE;
206 } else if (introspector.isContainer()) {
207 boolean isFinalContainer = i == parts.length - 1;
208 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
210 if (isFinalContainer && queryParams != null) {
211 Set<String> queryKeys = queryParams.keySet();
212 for (String key : queryKeys) {
213 uriKeys.put(key, queryParams.get(key));
217 p.processContainer(introspector, type, uriKeys, isFinalContainer);
220 p.processNamespace(introspector);
224 previousObj = introspector;
226 // invalid item found should log
227 // original said bad path
228 throw new AAIException("AAI_3001", "invalid item found in path: " + part);
231 } catch (AAIException e) {
233 } catch (Exception e) {
234 throw new AAIException("AAI_3001", e);
238 public boolean validate() throws UnsupportedEncodingException, AAIException {
239 this.parse(new URIValidate());
244 * Handle cloud region.
246 * @param action the action
250 protected URI handleCloudRegion(String action, URI uri) {
262 protected URI trimURI(URI uri) {
264 String result = uri.getRawPath();
265 if (result.startsWith("/")) {
266 result = result.substring(1, result.length());
269 if (result.endsWith("/")) {
270 result = result.substring(0, result.length() - 1);
273 // TODO - Check if this makes to do for model driven for base uri path
274 result = result.replaceFirst("[a-z][a-z]*/v\\d+/", "");
276 return UriBuilder.fromPath(result).build();