[AAI-178 Amsterdam] Make Edge Properties to be
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / parsers / query / LegacyQueryParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.parsers.query;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import org.openecomp.aai.exceptions.AAIException;
26 import org.openecomp.aai.introspection.Introspector;
27 import org.openecomp.aai.introspection.Loader;
28 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
29 import org.openecomp.aai.parsers.uri.Parsable;
30 import org.openecomp.aai.parsers.uri.URIParser;
31 import org.openecomp.aai.parsers.uri.URIToObject;
32 import org.openecomp.aai.query.builder.QueryBuilder;
33 import org.openecomp.aai.restcore.util.URITools;
34 import org.openecomp.aai.schema.enums.PropertyMetadata;
35 import org.openecomp.aai.serialization.db.EdgeType;
36
37 import javax.ws.rs.core.MultivaluedMap;
38 import java.io.UnsupportedEncodingException;
39 import java.net.URI;
40 import java.net.URISyntaxException;
41 import java.util.*;
42 import java.util.Map.Entry;
43
44 /**
45  * The Class LegacyQueryParser.
46  */
47 public class LegacyQueryParser extends QueryParser implements Parsable {
48
49         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(LegacyQueryParser.class);
50
51         private Introspector previous = null;
52
53         /**
54          * Instantiates a new legacy query parser.
55          *
56          * @param loader the loader
57          * @param queryBuilder the query builder
58          * @param uri the uri
59          * @throws UnsupportedEncodingException the unsupported encoding exception
60          * @throws AAIException the AAI exception
61          */
62         public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri) throws UnsupportedEncodingException, AAIException {
63                 super(loader, queryBuilder, uri);
64                 URIParser parser = new URIParser(loader, uri);
65                 parser.parse(this);
66         }
67         
68         /**
69          * Instantiates a new legacy query parser.
70          *
71          * @param loader the loader
72          * @param queryBuilder the query builder
73          * @param uri the uri
74          * @param queryParams the query params
75          * @throws UnsupportedEncodingException the unsupported encoding exception
76          * @throws AAIException the AAI exception
77          */
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);
81                 parser.parse(this);
82         }
83
84         /**
85          * Instantiates a new legacy query parser.
86          *
87          * @param loader the loader
88          * @param queryBuilder the query builder
89          */
90         public LegacyQueryParser(Loader loader, QueryBuilder queryBuilder) {
91                 super(loader, queryBuilder);
92         }
93
94         /**
95          * @throws AAIException 
96          * @{inheritDoc}
97          */
98         @Override
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);
103                 }
104                 if (previous == null) {
105                         queryBuilder.createDBQuery(obj);
106                         this.handleUriKeys(obj, uriKeys);
107                 } else {
108                         queryBuilder.createKeyQuery(obj);
109                         this.handleUriKeys(obj, uriKeys);
110                 }
111                 previous = obj;
112                 this.resultResource = obj.getDbName();          
113         }
114         
115         /**
116          * @{inheritDoc}
117          */
118         @Override
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);
124                         }
125                         
126                         if (previous == null) {
127                                 queryBuilder.createContainerQuery(obj);
128                                 queryBuilder.markParentBoundary();
129                         }
130                         if (!uriKeys.isEmpty()) {
131
132                                 try {
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)", e);
137                                 }
138                         }
139                         
140                         this.resultResource = obj.getChildDBName();
141                         this.containerResource = obj.getName();
142                 }
143         }
144         private void handleUriKeys(Introspector obj, MultivaluedMap<String, String> uriKeys) throws AAIException {
145                 for (String key : uriKeys.keySet()) {
146                         //to validate whether this property exists
147                         if (!obj.hasProperty(key)) {
148                                 throw new AAIException("AAI_3000", "property: " + key + " not found on " + obj.getDbName());
149                         }
150
151                         List<String> values = uriKeys.get(key);
152                         String dbPropertyName = key;
153                         Map<String, String> linkedProperties = new HashMap<>();
154                         final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(key);
155                         if (metadata.containsKey(PropertyMetadata.DATA_LINK)) {
156                                 linkedProperties.put(key, metadata.get(PropertyMetadata.DATA_LINK));
157                         }
158                         if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
159                                 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
160                         }
161                         
162                         if (!linkedProperties.containsKey(key)) {
163                                 if (values.size() > 1) {
164                                         queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values));
165                                 } else {
166                                         queryBuilder.getVerticesByIndexedProperty(dbPropertyName, obj.castValueAccordingToSchema(key, values.get(0)));
167                                 }
168                         }
169                         handleLinkedProperties(obj, uriKeys, linkedProperties);
170                 }
171         }
172         private void handleLinkedProperties(Introspector obj, MultivaluedMap<String, String> uriKeys, Map<String, String> linkedProperties) throws AAIException {
173                 
174                 QueryBuilder[] builders = new QueryBuilder[linkedProperties.keySet().size()];
175                 Set<Entry<String, String>> entrySet = linkedProperties.entrySet();
176                 int i = 0;
177                 Iterator<Entry<String, String>> itr = entrySet.iterator();
178                 
179                 while (itr.hasNext()) {
180                         Entry<String, String> entry = itr.next();
181                         Introspector child;
182                         try {
183                                 child = new URIToObject(this.latestLoader, new URI(URITools.replaceTemplates(obj, entry.getValue(), PropertyMetadata.DATA_LINK, true).orElse(""))).getEntity();
184                         } catch (IllegalArgumentException | UnsupportedEncodingException | URISyntaxException e) {
185                                 throw new AAIException("AAI_4000", e);
186                         } 
187                         List<String> values = uriKeys.get(entry.getKey());
188                         QueryBuilder builder = queryBuilder.newInstance();
189                         builder.createEdgeTraversal(EdgeType.TREE, obj, child);
190                         if (values.size() > 1) {
191                                 builder.getVerticesByIndexedProperty(entry.getKey(), obj.castValueAccordingToSchema(entry.getKey(), values));
192                         } else {
193                                 builder.getVerticesByIndexedProperty(entry.getKey(), obj.castValueAccordingToSchema(entry.getKey(), values.get(0)));
194                         }
195                         
196                         builders[i] = builder;
197                         i++;
198                 }
199                 
200                 queryBuilder.where(builders);
201                 
202         }
203
204         /**
205          * @{inheritDoc}
206          */
207         @Override
208         public void processNamespace(Introspector obj) {
209         
210         }
211         
212         /**
213          * @{inheritDoc}
214          */
215         @Override
216         public String getCloudRegionTransform() {
217                 return "add";
218         }
219         
220         /**
221          * @{inheritDoc}
222          */
223         @Override
224         public boolean useOriginalLoader() {
225                 return false;
226         }
227 }