2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modification Copyright © 2019 IBM
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.parsers.uri;
24 import java.io.UnsupportedEncodingException;
26 import java.net.URISyntaxException;
29 import javax.ws.rs.core.MultivaluedHashMap;
30 import javax.ws.rs.core.MultivaluedMap;
31 import javax.ws.rs.core.UriBuilder;
33 import org.onap.aai.edges.enums.EdgeType;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
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.springframework.web.util.UriUtils;
44 * The Class URIParser.
46 public class URIParser {
48 private URI uri = null;
49 protected Loader loader = null;
50 protected Loader originalLoader = null;
51 private URI originalURI = null;
52 private MultivaluedMap<String, String> queryParams = null;
53 private static String aaiExceptionCode = "AAI_3001";
56 * Instantiates a new URI parser.
58 * @param loader the loader
61 public URIParser(Loader loader, URI uri) {
63 this.originalLoader = loader;
64 // Load the latest version because we need it for cloud region
69 * Instantiates a new URI parser.
71 * @param loader the loader
73 * @param queryParams the query params
75 public URIParser(Loader loader, URI uri, MultivaluedMap<String, String> queryParams) {
77 this.queryParams = queryParams;
80 public Loader getLoader() {
85 * Gets the original URI.
87 * @return the original URI
89 public URI getOriginalURI() {
90 return this.originalURI;
97 * @throws UnsupportedEncodingException the unsupported encoding exception
98 * @throws AAIException the AAI exception
100 public void parse(Parsable p) throws UnsupportedEncodingException, AAIException {
102 boolean isRelative = uri.getRawPath().startsWith("./");
104 if (p.useOriginalLoader()) {
105 this.loader = this.originalLoader;
107 String[] parts = uri.getRawPath().split("/");
108 Introspector previousObj = null;
109 EdgeType type = EdgeType.TREE;
110 for (int i = 0; i < parts.length;) {
111 String part = parts[i];
112 if (part.equals(RestTokens.COUSIN.toString())) {
113 boolean isPathInvalid = i == parts.length - 1;
115 throw new AAIException("AAI_3000",
116 uri + " not a valid path. Cannot end in " + RestTokens.COUSIN);
118 boolean isFinalContainer = i == parts.length - 2;
119 previousObj = parseCousin(p, parts[i + 1], previousObj, isFinalContainer);
124 Introspector introspector = loader.introspectorFromName(part);
125 validatePath(isRelative, previousObj, part, introspector);
126 Set<String> keys = introspector.getKeys();
127 if (keys.size() > 0) {
128 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
129 boolean isLastPart = i+1 == parts.length;
130 if (isLastPart && queryParams != null) {
131 uriKeys = queryParams;
133 for (String key : keys) {
134 String value = UriUtils.decode(parts[i+1], "UTF-8");
135 introspector.setValue(key, value);
136 // skip this for further processing
141 p.processObject(introspector, type, uriKeys);
142 type = EdgeType.TREE;
143 } else if (introspector.isContainer()) {
144 boolean isFinalContainer = i == parts.length - 1;
145 MultivaluedMap<String, String> uriKeys = isFinalContainer && queryParams != null
147 : new MultivaluedHashMap<>();
148 p.processContainer(introspector, type, uriKeys, isFinalContainer);
150 p.processNamespace(introspector);
153 previousObj = introspector;
155 } catch (AAIException e) {
157 } catch (Exception e) {
158 throw new AAIException(aaiExceptionCode, e);
162 private void validatePath(boolean isRelative, Introspector previousObj,
163 String part, Introspector introspector) throws AAIException, DoesNotStartWithValidNamespaceException {
164 if (introspector == null) {
165 throw new AAIException(aaiExceptionCode, "invalid item found in path: " + part);
167 // previous has current as property
168 boolean isPathInvalid = previousObj != null && !previousObj.hasChild(introspector)
169 && !previousObj.getDbName().equals("nodes");
171 throw new AAIException(aaiExceptionCode, uri + " not a valid path. " + part + " not valid");
173 if (previousObj == null) {
174 // first time through, make sure it starts from a valid namespace
175 // ignore abstract types
176 String abstractType = introspector.getMetadata(ObjectMetadata.ABSTRACT);
177 Introspector validNamespaces = loader.introspectorFromName("inventory");
178 if (!isRelative && !"true".equals(abstractType) && !validNamespaces.hasChild(introspector)) {
179 throw new DoesNotStartWithValidNamespaceException(
180 uri + " not a valid path. It does not start from a valid namespace");
185 private Introspector parseCousin(Parsable p, String name, Introspector previousObj, boolean isFinalContainer)
186 throws AAIException, AAIUnknownObjectException {
187 Introspector introspector;
188 if (null == previousObj) {
189 throw new AAIException(aaiExceptionCode);
191 introspector = loader.introspectorFromName(name);
192 if (previousObj.isContainer() && introspector.isContainer()) {
193 throw new AAIException("AAI_3000", uri + " not a valid path. Cannot chain plurals together");
196 if (introspector.isContainer()) {
197 MultivaluedMap<String, String> uriKeys = isFinalContainer && queryParams != null
199 : new MultivaluedHashMap<>();
201 * Related-to could be COUSIN OR TREE and in some cases BOTH. So Let
202 * EdgeRuleBuilder use all the
205 p.processContainer(introspector, EdgeType.ALL, uriKeys, isFinalContainer);
210 public boolean validate() throws UnsupportedEncodingException, AAIException {
211 this.parse(new URIValidate());
221 protected URI trimURI(URI uri) {
222 String result = uri.getRawPath();
223 if (result.startsWith("/")) {
224 result = result.substring(1, result.length());
227 if (result.endsWith("/")) {
228 result = result.substring(0, result.length() - 1);
231 // TODO - Check if this makes to do for model driven for base uri path
232 result = result.replaceFirst("[a-z][a-z]*/v\\d+/", "");
234 return UriBuilder.fromPath(result).build();
237 private URI formatUri() throws URISyntaxException {
238 uri = this.trimURI(uri);
239 this.originalURI = UriBuilder.fromPath(uri.getRawPath()).build();
240 boolean isRelative = uri.getRawPath().startsWith("./");
242 uri = new URI(uri.getRawPath().replaceFirst("\\./", ""));