Update tinkerpop to 3.2.4
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / parsers / uri / URIParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.aai.parsers.uri;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.Set;
28
29 import javax.ws.rs.core.MultivaluedHashMap;
30 import javax.ws.rs.core.MultivaluedMap;
31 import javax.ws.rs.core.UriBuilder;
32
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;
42
43 /**
44  * The Class URIParser.
45  */
46 public class URIParser {
47
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";
54
55     /**
56      * Instantiates a new URI parser.
57      *
58      * @param loader the loader
59      * @param uri the uri
60      */
61     public URIParser(Loader loader, URI uri) {
62         this.uri = uri;
63         this.originalLoader = loader;
64         // Load the latest version because we need it for cloud region
65         this.loader = loader;
66     }
67
68     /**
69      * Instantiates a new URI parser.
70      *
71      * @param loader the loader
72      * @param uri the uri
73      * @param queryParams the query params
74      */
75     public URIParser(Loader loader, URI uri, MultivaluedMap<String, String> queryParams) {
76         this(loader, uri);
77         this.queryParams = queryParams;
78     }
79
80     public Loader getLoader() {
81         return this.loader;
82     }
83
84     /**
85      * Gets the original URI.
86      *
87      * @return the original URI
88      */
89     public URI getOriginalURI() {
90         return this.originalURI;
91     }
92
93     /**
94      * Parses the.
95      *
96      * @param p the p
97      * @throws UnsupportedEncodingException the unsupported encoding exception
98      * @throws AAIException the AAI exception
99      */
100     public void parse(Parsable p) throws UnsupportedEncodingException, AAIException {
101         try {
102             boolean isRelative = uri.getRawPath().startsWith("./");
103             uri = formatUri();
104             if (p.useOriginalLoader()) {
105                 this.loader = this.originalLoader;
106             }
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;
114                     if (isPathInvalid) {
115                         throw new AAIException("AAI_3000",
116                                 uri + " not a valid path. Cannot end in " + RestTokens.COUSIN);
117                     }
118                     boolean isFinalContainer = i == parts.length - 2;
119                     previousObj = parseCousin(p, parts[i + 1], previousObj, isFinalContainer);
120                     type = EdgeType.ALL;
121                     i += 2;
122                     continue;
123                 }
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;
132                     } else {
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
137                             i++;
138                         }
139                     }
140
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
146                             ? queryParams
147                             : new MultivaluedHashMap<>();
148                     p.processContainer(introspector, type, uriKeys, isFinalContainer);
149                 } else {
150                     p.processNamespace(introspector);
151                 }
152                 i++;
153                 previousObj = introspector;
154             }
155         } catch (AAIException e) {
156             throw e;
157         } catch (Exception e) {
158             throw new AAIException(aaiExceptionCode, e);
159         }
160     }
161
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);
166         }
167         // previous has current as property
168         boolean isPathInvalid = previousObj != null && !previousObj.hasChild(introspector)
169                 && !previousObj.getDbName().equals("nodes");
170         if (isPathInvalid) {
171             throw new AAIException(aaiExceptionCode, uri + " not a valid path. " + part + " not valid");
172         }
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");
181             }
182         }
183     }
184
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);
190         }
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");
194         }
195
196         if (introspector.isContainer()) {
197             MultivaluedMap<String, String> uriKeys = isFinalContainer && queryParams != null
198                     ? queryParams
199                     : new MultivaluedHashMap<>();
200             /*
201              * Related-to could be COUSIN OR TREE and in some cases BOTH. So Let
202              * EdgeRuleBuilder use all the
203              * edgeTypes
204              */
205             p.processContainer(introspector, EdgeType.ALL, uriKeys, isFinalContainer);
206         }
207         return introspector;
208     }
209
210     public boolean validate() throws UnsupportedEncodingException, AAIException {
211         this.parse(new URIValidate());
212         return true;
213     }
214
215     /**
216      * Trim URI.
217      *
218      * @param uri the uri
219      * @return the uri
220      */
221     protected URI trimURI(URI uri) {
222         String result = uri.getRawPath();
223         if (result.startsWith("/")) {
224             result = result.substring(1, result.length());
225         }
226
227         if (result.endsWith("/")) {
228             result = result.substring(0, result.length() - 1);
229         }
230
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+/", "");
233
234         return UriBuilder.fromPath(result).build();
235     }
236
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("./");
241         if (isRelative) {
242             uri = new URI(uri.getRawPath().replaceFirst("\\./", ""));
243         }
244         return uri;
245     }
246
247 }