Change openecomp to onap and update license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / parsers / query / LegacyQueryParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.parsers.query;
23
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.parsers.uri.Parsable;
31 import org.onap.aai.parsers.uri.URIParser;
32 import org.onap.aai.parsers.uri.URIToObject;
33 import org.onap.aai.query.builder.QueryBuilder;
34 import org.onap.aai.restcore.util.URITools;
35 import org.onap.aai.schema.enums.PropertyMetadata;
36 import org.onap.aai.serialization.db.EdgeType;
37
38 import javax.ws.rs.core.MultivaluedMap;
39 import java.io.UnsupportedEncodingException;
40 import java.net.URI;
41 import java.net.URISyntaxException;
42 import java.util.*;
43 import java.util.Map.Entry;
44
45 /**
46  * The Class LegacyQueryParser.
47  */
48 public class LegacyQueryParser extends QueryParser implements Parsable {
49
50         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(LegacyQueryParser.class);
51
52         private Introspector previous = null;
53
54         /**
55          * Instantiates a new legacy query parser.
56          *
57          * @param loader the loader
58          * @param queryBuilder the query builder
59          * @param uri the uri
60          * @throws UnsupportedEncodingException the unsupported encoding exception
61          * @throws AAIException the AAI exception
62          */
63         public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri) throws UnsupportedEncodingException, AAIException {
64                 super(loader, queryBuilder, uri);
65                 URIParser parser = new URIParser(loader, uri);
66                 parser.parse(this);
67         }
68         
69         /**
70          * Instantiates a new legacy query parser.
71          *
72          * @param loader the loader
73          * @param queryBuilder the query builder
74          * @param uri the uri
75          * @param queryParams the query params
76          * @throws UnsupportedEncodingException the unsupported encoding exception
77          * @throws AAIException the AAI exception
78          */
79         public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException {
80                 super(loader, queryBuilder, uri);
81                 URIParser parser = new URIParser(loader, uri, queryParams);
82                 parser.parse(this);
83         }
84
85         /**
86          * Instantiates a new legacy query parser.
87          *
88          * @param loader the loader
89          * @param queryBuilder the query builder
90          */
91         public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder) {
92                 super(loader, queryBuilder);
93         }
94
95         /**
96          * @throws AAIException 
97          * @{inheritDoc}
98          */
99         @Override
100         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys) throws AAIException {
101                 if (previous != null) {
102                         this.parentResourceType = previous.getDbName();
103                         queryBuilder.createEdgeTraversal(type, previous, obj);
104                 }
105                 if (previous == null) {
106                         queryBuilder.createDBQuery(obj);
107                         this.handleUriKeys(obj, uriKeys);
108                 } else {
109                         queryBuilder.createKeyQuery(obj);
110                         this.handleUriKeys(obj, uriKeys);
111                 }
112                 previous = obj;
113                 this.resultResource = obj.getDbName();          
114         }
115         
116         /**
117          * @{inheritDoc}
118          */
119         @Override
120         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys, boolean isFinalContainer) throws AAIException {
121                 if (isFinalContainer) {
122                         if (previous != null) {
123                                 this.parentResourceType = previous.getDbName();
124                                 queryBuilder.createEdgeTraversal(type, previous, obj);
125                         }
126                         
127                         if (previous == null) {
128                                 queryBuilder.createContainerQuery(obj);
129                                 queryBuilder.markParentBoundary();
130                         }
131                         if (!uriKeys.isEmpty()) {
132
133                                 try {
134                                         Introspector child = obj.newIntrospectorInstanceOfNestedProperty(obj.getChildName());
135                                         this.handleUriKeys(child, uriKeys);
136                                 } catch (AAIUnknownObjectException e) {
137                                         LOGGER.warn("Skipping container child " + obj.getChildName() + " (Unknown Object)", e);
138                                 }
139                         }
140                         
141                         this.resultResource = obj.getChildDBName();
142                         this.containerResource = obj.getName();
143                 }
144         }
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());
150                         }
151
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));
158                         }
159                         if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
160                                 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
161                         }
162                         
163                         if (!linkedProperties.containsKey(key)) {
164                                 if (values.size() > 1) {
165                                         queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values));
166                                 } else {
167                                         queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values.get(0)));
168                                 }
169                         }
170                         handleLinkedProperties(obj, uriKeys, linkedProperties);
171                 }
172         }
173         private void handleLinkedProperties(Introspector obj, MultivaluedMap<String, String> uriKeys, Map<String, String> linkedProperties) throws AAIException {
174                 
175                 QueryBuilder[] builders = new QueryBuilder[linkedProperties.keySet().size()];
176                 Set<Entry<String, String>> entrySet = linkedProperties.entrySet();
177                 int i = 0;
178                 Iterator<Entry<String, String>> itr = entrySet.iterator();
179                 
180                 while (itr.hasNext()) {
181                         Entry<String, String> entry = itr.next();
182                         Introspector child;
183                         try {
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);
187                         } 
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));
193                         } else {
194                                 builder.getVerticesByIndexedProperty(entry.getKey(), obj.castValueAccordingToSchema(entry.getKey(), values.get(0)));
195                         }
196                         
197                         builders[i] = builder;
198                         i++;
199                 }
200                 
201                 queryBuilder.where(builders);
202                 
203         }
204
205         /**
206          * @{inheritDoc}
207          */
208         @Override
209         public void processNamespace(Introspector obj) {
210         
211         }
212         
213         /**
214          * @{inheritDoc}
215          */
216         @Override
217         public String getCloudRegionTransform() {
218                 return "add";
219         }
220         
221         /**
222          * @{inheritDoc}
223          */
224         @Override
225         public boolean useOriginalLoader() {
226                 return false;
227         }
228 }