10530d19937f663bf11217418c71901a048ed33b
[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.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;
38
39 import javax.ws.rs.core.MultivaluedMap;
40 import java.io.UnsupportedEncodingException;
41 import java.net.URI;
42 import java.net.URISyntaxException;
43 import java.util.*;
44 import java.util.Map.Entry;
45
46 /**
47  * The Class LegacyQueryParser.
48  */
49 public class LegacyQueryParser extends QueryParser implements Parsable {
50
51         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(LegacyQueryParser.class);
52
53         private Introspector previous = null;
54
55         /**
56          * Instantiates a new legacy query parser.
57          *
58          * @param loader the loader
59          * @param queryBuilder the query builder
60          * @param uri the uri
61          * @throws UnsupportedEncodingException the unsupported encoding exception
62          * @throws AAIException the AAI exception
63          */
64         public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri) throws UnsupportedEncodingException, AAIException {
65                 super(loader, queryBuilder, uri);
66                 URIParser parser = new URIParser(loader, uri);
67                 parser.parse(this);
68         }
69         
70         /**
71          * Instantiates a new legacy query parser.
72          *
73          * @param loader the loader
74          * @param queryBuilder the query builder
75          * @param uri the uri
76          * @param queryParams the query params
77          * @throws UnsupportedEncodingException the unsupported encoding exception
78          * @throws AAIException the AAI exception
79          */
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);
83                 parser.parse(this);
84         }
85
86         /**
87          * Instantiates a new legacy query parser.
88          *
89          * @param loader the loader
90          * @param queryBuilder the query builder
91          */
92         public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder) {
93                 super(loader, queryBuilder);
94         }
95
96         /**
97          * @throws AAIException 
98          * @{inheritDoc}
99          */
100         @Override
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);
105                 }
106                 if (previous == null) {
107                         queryBuilder.createDBQuery(obj);
108                         this.handleUriKeys(obj, uriKeys);
109                 } else {
110                         queryBuilder.createKeyQuery(obj);
111                         this.handleUriKeys(obj, uriKeys);
112                 }
113                 previous = obj;
114                 this.resultResource = obj.getDbName();          
115         }
116         
117         /**
118          * @{inheritDoc}
119          */
120         @Override
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);
126                         }
127                         
128                         if (previous == null) {
129                                 queryBuilder.createContainerQuery(obj);
130                                 queryBuilder.markParentBoundary();
131                         }
132                         if (!uriKeys.isEmpty()) {
133
134                                 try {
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));
140                                 }
141                         }
142                         
143                         this.resultResource = obj.getChildDBName();
144                         this.containerResource = obj.getName();
145                 }
146         }
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());
152                         }
153
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));
160                         }
161                         if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
162                                 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
163                         }
164                         
165                         if (!linkedProperties.containsKey(key)) {
166                                 if (values.size() > 1) {
167                                         queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values));
168                                 } else {
169                                         queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values.get(0)));
170                                 }
171                         }
172                         handleLinkedProperties(obj, uriKeys, linkedProperties);
173                 }
174         }
175         private void handleLinkedProperties(Introspector obj, MultivaluedMap<String, String> uriKeys, Map<String, String> linkedProperties) throws AAIException {
176                 
177                 QueryBuilder[] builders = new QueryBuilder[linkedProperties.keySet().size()];
178                 Set<Entry<String, String>> entrySet = linkedProperties.entrySet();
179                 int i = 0;
180                 Iterator<Entry<String, String>> itr = entrySet.iterator();
181                 
182                 while (itr.hasNext()) {
183                         Entry<String, String> entry = itr.next();
184                         Introspector child;
185                         try {
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);
189                         } 
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));
195                         } else {
196                                 builder.getVerticesByIndexedProperty(entry.getKey(), obj.castValueAccordingToSchema(entry.getKey(), values.get(0)));
197                         }
198                         
199                         builders[i] = builder;
200                         i++;
201                 }
202                 
203                 queryBuilder.where(builders);
204                 
205         }
206
207         /**
208          * @{inheritDoc}
209          */
210         @Override
211         public void processNamespace(Introspector obj) {
212         
213         }
214         
215         /**
216          * @{inheritDoc}
217          */
218         @Override
219         public String getCloudRegionTransform() {
220                 return "add";
221         }
222         
223         /**
224          * @{inheritDoc}
225          */
226         @Override
227         public boolean useOriginalLoader() {
228                 return false;
229         }
230 }