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.query;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import java.io.UnsupportedEncodingException;
28 import java.net.URISyntaxException;
30 import java.util.Map.Entry;
32 import javax.ws.rs.core.MultivaluedMap;
34 import org.onap.aai.edges.enums.EdgeType;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Introspector;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
39 import org.onap.aai.logging.LogFormatTools;
40 import org.onap.aai.parsers.uri.Parsable;
41 import org.onap.aai.parsers.uri.URIParser;
42 import org.onap.aai.parsers.uri.URIToObject;
43 import org.onap.aai.query.builder.QueryBuilder;
44 import org.onap.aai.restcore.util.URITools;
45 import org.onap.aai.schema.enums.PropertyMetadata;
48 * The Class LegacyQueryParser.
50 public class LegacyQueryParser extends QueryParser implements Parsable {
52 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyQueryParser.class);
54 private Introspector previous = null;
57 * Instantiates a new legacy query parser.
59 * @param loader the loader
60 * @param queryBuilder the query builder
62 * @throws UnsupportedEncodingException the unsupported encoding exception
63 * @throws AAIException the AAI exception
65 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri)
66 throws UnsupportedEncodingException, AAIException {
67 super(loader, queryBuilder, uri);
68 URIParser parser = new URIParser(loader, uri);
73 * Instantiates a new legacy query parser.
75 * @param loader the loader
76 * @param queryBuilder the query builder
78 * @param queryParams the query params
79 * @throws UnsupportedEncodingException the unsupported encoding exception
80 * @throws AAIException the AAI exception
82 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri,
83 MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException {
84 super(loader, queryBuilder, uri);
85 URIParser parser = new URIParser(loader, uri, queryParams);
90 * Instantiates a new legacy query parser.
92 * @param loader the loader
93 * @param queryBuilder the query builder
95 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder) {
96 super(loader, queryBuilder);
100 * @throws AAIException
104 public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
105 throws AAIException {
106 if (previous != null) {
107 this.parentResourceType = previous.getDbName();
108 queryBuilder.createEdgeTraversal(type, previous, obj);
110 if (previous == null) {
111 queryBuilder.createDBQuery(obj);
112 this.handleUriKeys(obj, uriKeys);
114 queryBuilder.createKeyQuery(obj);
115 this.handleUriKeys(obj, uriKeys);
118 this.resultResource = obj.getDbName();
125 public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
126 boolean isFinalContainer) throws AAIException {
127 if (isFinalContainer) {
128 if (previous != null) {
129 this.parentResourceType = previous.getDbName();
130 queryBuilder.createEdgeTraversal(type, previous, obj);
133 if (previous == null) {
134 queryBuilder.createContainerQuery(obj);
135 queryBuilder.markParentBoundary();
137 if (!uriKeys.isEmpty()) {
140 Introspector child = obj.newIntrospectorInstanceOfNestedProperty(obj.getChildName());
141 this.handleUriKeys(child, uriKeys);
142 } catch (AAIUnknownObjectException e) {
143 LOGGER.warn("Skipping container child " + obj.getChildName() + " (Unknown Object) "
144 + LogFormatTools.getStackTop(e));
148 this.resultResource = obj.getChildDBName();
149 this.containerResource = obj.getName();
153 private void handleUriKeys(Introspector obj, MultivaluedMap<String, String> uriKeys) throws AAIException {
154 for (String key : uriKeys.keySet()) {
155 // to validate whether this property exists
156 if (!obj.hasProperty(key)) {
157 throw new AAIException("AAI_3000", "property: " + key + " not found on " + obj.getDbName());
160 List<String> values = uriKeys.get(key);
161 String dbPropertyName = key;
162 Map<String, String> linkedProperties = new HashMap<>();
163 final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(key);
164 if (metadata.containsKey(PropertyMetadata.DATA_LINK)) {
165 linkedProperties.put(key, metadata.get(PropertyMetadata.DATA_LINK));
167 if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
168 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
171 if (!linkedProperties.containsKey(key)) {
172 if (values.size() > 1) {
173 queryBuilder.getVerticesByIndexedProperty(dbPropertyName,
174 obj.castValueAccordingToSchema(key, values));
176 queryBuilder.getVerticesByIndexedProperty(dbPropertyName,
177 obj.castValueAccordingToSchema(key, values.get(0)));
180 handleLinkedProperties(obj, uriKeys, linkedProperties);
184 private void handleLinkedProperties(Introspector obj, MultivaluedMap<String, String> uriKeys,
185 Map<String, String> linkedProperties) throws AAIException {
187 QueryBuilder[] builders = new QueryBuilder[linkedProperties.keySet().size()];
188 Set<Entry<String, String>> entrySet = linkedProperties.entrySet();
190 Iterator<Entry<String, String>> itr = entrySet.iterator();
192 while (itr.hasNext()) {
193 Entry<String, String> entry = itr.next();
196 child = new URIToObject(this.latestLoader, new URI(
197 URITools.replaceTemplates(obj, entry.getValue(), PropertyMetadata.DATA_LINK, true).orElse("")))
199 } catch (IllegalArgumentException | UnsupportedEncodingException | URISyntaxException e) {
200 throw new AAIException("AAI_4000", e);
202 List<String> values = uriKeys.get(entry.getKey());
203 QueryBuilder builder = queryBuilder.newInstance();
204 builder.createEdgeTraversal(EdgeType.TREE, obj, child);
205 if (values.size() > 1) {
206 builder.getVerticesByIndexedProperty(entry.getKey(),
207 obj.castValueAccordingToSchema(entry.getKey(), values));
209 builder.getVerticesByIndexedProperty(entry.getKey(),
210 obj.castValueAccordingToSchema(entry.getKey(), values.get(0)));
213 builders[i] = builder;
217 queryBuilder.where(builders);
225 public void processNamespace(Introspector obj) {
233 public String getCloudRegionTransform() {
241 public boolean useOriginalLoader() {