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=========================================================
20 package org.onap.aai.parsers.query;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.onap.aai.exceptions.AAIException;
25 import org.onap.aai.introspection.Introspector;
26 import org.onap.aai.introspection.Loader;
27 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
28 import org.onap.aai.logging.LogFormatTools;
29 import org.onap.aai.parsers.uri.Parsable;
30 import org.onap.aai.parsers.uri.URIParser;
31 import org.onap.aai.parsers.uri.URIToObject;
32 import org.onap.aai.query.builder.QueryBuilder;
33 import org.onap.aai.restcore.util.URITools;
34 import org.onap.aai.schema.enums.PropertyMetadata;
35 import org.onap.aai.edges.enums.EdgeType;
37 import javax.ws.rs.core.MultivaluedMap;
38 import java.io.UnsupportedEncodingException;
40 import java.net.URISyntaxException;
42 import java.util.Map.Entry;
45 * The Class LegacyQueryParser.
47 public class LegacyQueryParser extends QueryParser implements Parsable {
49 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(LegacyQueryParser.class);
51 private Introspector previous = null;
54 * Instantiates a new legacy query parser.
56 * @param loader the loader
57 * @param queryBuilder the query builder
59 * @throws UnsupportedEncodingException the unsupported encoding exception
60 * @throws AAIException the AAI exception
62 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri) throws UnsupportedEncodingException, AAIException {
63 super(loader, queryBuilder, uri);
64 URIParser parser = new URIParser(loader, uri);
69 * Instantiates a new legacy query parser.
71 * @param loader the loader
72 * @param queryBuilder the query builder
74 * @param queryParams the query params
75 * @throws UnsupportedEncodingException the unsupported encoding exception
76 * @throws AAIException the AAI exception
78 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException {
79 super(loader, queryBuilder, uri);
80 URIParser parser = new URIParser(loader, uri, queryParams);
85 * Instantiates a new legacy query parser.
87 * @param loader the loader
88 * @param queryBuilder the query builder
90 public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder) {
91 super(loader, queryBuilder);
95 * @throws AAIException
99 public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys) throws AAIException {
100 if (previous != null) {
101 this.parentResourceType = previous.getDbName();
102 queryBuilder.createEdgeTraversal(type, previous, obj);
104 if (previous == null) {
105 queryBuilder.createDBQuery(obj);
106 this.handleUriKeys(obj, uriKeys);
108 queryBuilder.createKeyQuery(obj);
109 this.handleUriKeys(obj, uriKeys);
112 this.resultResource = obj.getDbName();
119 public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys, boolean isFinalContainer) throws AAIException {
120 if (isFinalContainer) {
121 if (previous != null) {
122 this.parentResourceType = previous.getDbName();
123 queryBuilder.createEdgeTraversal(type, previous, obj);
126 if (previous == null) {
127 queryBuilder.createContainerQuery(obj);
128 queryBuilder.markParentBoundary();
130 if (!uriKeys.isEmpty()) {
133 Introspector child = obj.newIntrospectorInstanceOfNestedProperty(obj.getChildName());
134 this.handleUriKeys(child, uriKeys);
135 } catch (AAIUnknownObjectException e) {
136 LOGGER.warn("Skipping container child " + obj.getChildName() + " (Unknown Object) " +
137 LogFormatTools.getStackTop(e));
141 this.resultResource = obj.getChildDBName();
142 this.containerResource = obj.getName();
145 private void handleUriKeys(Introspector obj, MultivaluedMap<String, String> uriKeys) throws AAIException {
146 for (String key : uriKeys.keySet()) {
147 //to validate whether this property exists
148 if (!obj.hasProperty(key)) {
149 throw new AAIException("AAI_3000", "property: " + key + " not found on " + obj.getDbName());
152 List<String> values = uriKeys.get(key);
153 String dbPropertyName = key;
154 Map<String, String> linkedProperties = new HashMap<>();
155 final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(key);
156 if (metadata.containsKey(PropertyMetadata.DATA_LINK)) {
157 linkedProperties.put(key, metadata.get(PropertyMetadata.DATA_LINK));
159 if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
160 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
163 if (!linkedProperties.containsKey(key)) {
164 if (values.size() > 1) {
165 queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values));
167 queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values.get(0)));
170 handleLinkedProperties(obj, uriKeys, linkedProperties);
173 private void handleLinkedProperties(Introspector obj, MultivaluedMap<String, String> uriKeys, Map<String, String> linkedProperties) throws AAIException {
175 QueryBuilder[] builders = new QueryBuilder[linkedProperties.keySet().size()];
176 Set<Entry<String, String>> entrySet = linkedProperties.entrySet();
178 Iterator<Entry<String, String>> itr = entrySet.iterator();
180 while (itr.hasNext()) {
181 Entry<String, String> entry = itr.next();
184 child = new URIToObject(this.latestLoader, new URI(URITools.replaceTemplates(obj, entry.getValue(), PropertyMetadata.DATA_LINK, true).orElse(""))).getEntity();
185 } catch (IllegalArgumentException | UnsupportedEncodingException | URISyntaxException e) {
186 throw new AAIException("AAI_4000", e);
188 List<String> values = uriKeys.get(entry.getKey());
189 QueryBuilder builder = queryBuilder.newInstance();
190 builder.createEdgeTraversal(EdgeType.TREE, obj, child);
191 if (values.size() > 1) {
192 builder.getVerticesByIndexedProperty(entry.getKey(), obj.castValueAccordingToSchema(entry.getKey(), values));
194 builder.getVerticesByIndexedProperty(entry.getKey(), obj.castValueAccordingToSchema(entry.getKey(), values.get(0)));
197 builders[i] = builder;
201 queryBuilder.where(builders);
209 public void processNamespace(Introspector obj) {
217 public String getCloudRegionTransform() {
225 public boolean useOriginalLoader() {