2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.parsers.query;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.introspection.Introspector;
28 import org.onap.aai.introspection.Loader;
29 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
30 import org.onap.aai.logging.LogFormatTools;
31 import org.onap.aai.parsers.uri.Parsable;
32 import org.onap.aai.parsers.uri.URIParser;
33 import org.onap.aai.parsers.uri.URIToObject;
34 import org.onap.aai.query.builder.QueryBuilder;
35 import org.onap.aai.restcore.util.URITools;
36 import org.onap.aai.schema.enums.PropertyMetadata;
37 import org.onap.aai.serialization.db.EdgeType;
39 import javax.ws.rs.core.MultivaluedMap;
40 import java.io.UnsupportedEncodingException;
42 import java.net.URISyntaxException;
44 import java.util.Map.Entry;
47 * The Class LegacyQueryParser.
49 public class LegacyQueryParser extends QueryParser implements Parsable {
51 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(LegacyQueryParser.class);
53 private Introspector previous = null;
56 * Instantiates a new legacy query parser.
58 * @param loader the loader
59 * @param queryBuilder the query builder
61 * @throws UnsupportedEncodingException the unsupported encoding exception
62 * @throws AAIException the AAI exception
64 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri) throws UnsupportedEncodingException, AAIException {
65 super(loader, queryBuilder, uri);
66 URIParser parser = new URIParser(loader, uri);
71 * Instantiates a new legacy query parser.
73 * @param loader the loader
74 * @param queryBuilder the query builder
76 * @param queryParams the query params
77 * @throws UnsupportedEncodingException the unsupported encoding exception
78 * @throws AAIException the AAI exception
80 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException {
81 super(loader, queryBuilder, uri);
82 URIParser parser = new URIParser(loader, uri, queryParams);
87 * Instantiates a new legacy query parser.
89 * @param loader the loader
90 * @param queryBuilder the query builder
92 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder) {
93 super(loader, queryBuilder);
97 * @throws AAIException
101 public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys) throws AAIException {
102 if (previous != null) {
103 this.parentResourceType = previous.getDbName();
104 queryBuilder.createEdgeTraversal(type, previous, obj);
106 if (previous == null) {
107 queryBuilder.createDBQuery(obj);
108 this.handleUriKeys(obj, uriKeys);
110 queryBuilder.createKeyQuery(obj);
111 this.handleUriKeys(obj, uriKeys);
114 this.resultResource = obj.getDbName();
121 public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys, boolean isFinalContainer) throws AAIException {
122 if (isFinalContainer) {
123 if (previous != null) {
124 this.parentResourceType = previous.getDbName();
125 queryBuilder.createEdgeTraversal(type, previous, obj);
128 if (previous == null) {
129 queryBuilder.createContainerQuery(obj);
130 queryBuilder.markParentBoundary();
132 if (!uriKeys.isEmpty()) {
135 Introspector child = obj.newIntrospectorInstanceOfNestedProperty(obj.getChildName());
136 this.handleUriKeys(child, uriKeys);
137 } catch (AAIUnknownObjectException e) {
138 LOGGER.warn("Skipping container child " + obj.getChildName() + " (Unknown Object) " +
139 LogFormatTools.getStackTop(e));
143 this.resultResource = obj.getChildDBName();
144 this.containerResource = obj.getName();
147 private void handleUriKeys(Introspector obj, MultivaluedMap<String, String> uriKeys) throws AAIException {
148 for (String key : uriKeys.keySet()) {
149 //to validate whether this property exists
150 if (!obj.hasProperty(key)) {
151 throw new AAIException("AAI_3000", "property: " + key + " not found on " + obj.getDbName());
154 List<String> values = uriKeys.get(key);
155 String dbPropertyName = key;
156 Map<String, String> linkedProperties = new HashMap<>();
157 final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(key);
158 if (metadata.containsKey(PropertyMetadata.DATA_LINK)) {
159 linkedProperties.put(key, metadata.get(PropertyMetadata.DATA_LINK));
161 if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
162 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
165 if (!linkedProperties.containsKey(key)) {
166 if (values.size() > 1) {
167 queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values));
169 queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values.get(0)));
172 handleLinkedProperties(obj, uriKeys, linkedProperties);
175 private void handleLinkedProperties(Introspector obj, MultivaluedMap<String, String> uriKeys, Map<String, String> linkedProperties) throws AAIException {
177 QueryBuilder[] builders = new QueryBuilder[linkedProperties.keySet().size()];
178 Set<Entry<String, String>> entrySet = linkedProperties.entrySet();
180 Iterator<Entry<String, String>> itr = entrySet.iterator();
182 while (itr.hasNext()) {
183 Entry<String, String> entry = itr.next();
186 child = new URIToObject(this.latestLoader, new URI(URITools.replaceTemplates(obj, entry.getValue(), PropertyMetadata.DATA_LINK, true).orElse(""))).getEntity();
187 } catch (IllegalArgumentException | UnsupportedEncodingException | URISyntaxException e) {
188 throw new AAIException("AAI_4000", e);
190 List<String> values = uriKeys.get(entry.getKey());
191 QueryBuilder builder = queryBuilder.newInstance();
192 builder.createEdgeTraversal(EdgeType.TREE, obj, child);
193 if (values.size() > 1) {
194 builder.getVerticesByIndexedProperty(entry.getKey(), obj.castValueAccordingToSchema(entry.getKey(), values));
196 builder.getVerticesByIndexedProperty(entry.getKey(), obj.castValueAccordingToSchema(entry.getKey(), values.get(0)));
199 builders[i] = builder;
203 queryBuilder.where(builders);
211 public void processNamespace(Introspector obj) {
219 public String getCloudRegionTransform() {
227 public boolean useOriginalLoader() {