Release 1.16.0
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / DslConsumer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Deutsche Telekom SA.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.rest;
22
23 import java.io.FileNotFoundException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Optional;
30 import java.util.Set;
31 import java.util.stream.Collectors;
32
33 import jakarta.servlet.http.HttpServletRequest;
34 import jakarta.ws.rs.core.MultivaluedHashMap;
35 import jakarta.ws.rs.core.MultivaluedMap;
36
37 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
38 import org.javatuples.Pair;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.ModelType;
41 import org.onap.aai.query.builder.Pageable;
42 import org.onap.aai.rest.db.HttpEntry;
43 import org.onap.aai.rest.dsl.DslQueryProcessor;
44 import org.onap.aai.rest.dsl.V1DslQueryProcessor;
45 import org.onap.aai.rest.dsl.V2DslQueryProcessor;
46 import org.onap.aai.rest.dsl.v1.DslListener;
47 import org.onap.aai.rest.enums.QueryVersion;
48 import org.onap.aai.rest.search.GenericQueryProcessor;
49 import org.onap.aai.rest.search.GremlinServerSingleton;
50 import org.onap.aai.rest.search.QueryProcessorType;
51 import org.onap.aai.rest.util.PaginationUtil;
52 import org.onap.aai.serialization.db.DBSerializer;
53 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
54 import org.onap.aai.serialization.queryformats.Format;
55 import org.onap.aai.serialization.queryformats.FormatFactory;
56 import org.onap.aai.serialization.queryformats.Formatter;
57 import org.onap.aai.serialization.queryformats.SubGraphStyle;
58 import org.onap.aai.setup.SchemaVersion;
59 import org.onap.aai.setup.SchemaVersions;
60 import org.onap.aai.transforms.XmlFormatTransformer;
61 import org.onap.aai.util.AAIConfig;
62 import org.onap.aai.util.TraversalConstants;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.beans.factory.annotation.Qualifier;
67 import org.springframework.beans.factory.annotation.Value;
68 import org.springframework.http.HttpHeaders;
69 import org.springframework.http.MediaType;
70 import org.springframework.http.ResponseEntity;
71 import org.springframework.web.bind.annotation.PathVariable;
72 import org.springframework.web.bind.annotation.PutMapping;
73 import org.springframework.web.bind.annotation.RequestBody;
74 import org.springframework.web.bind.annotation.RequestHeader;
75 import org.springframework.web.bind.annotation.RequestMapping;
76 import org.springframework.web.bind.annotation.RequestParam;
77 import org.springframework.web.bind.annotation.RestController;
78
79 import com.google.gson.JsonElement;
80 import com.google.gson.JsonObject;
81 import com.google.gson.JsonParser;
82
83 import io.micrometer.core.annotation.Timed;
84
85 @Timed
86 @RestController
87 @RequestMapping("/{version:v[1-9][0-9]*|latest}/dsl")
88 public class DslConsumer extends TraversalConsumer {
89
90     private static final Logger LOGGER = LoggerFactory.getLogger(DslConsumer.class);
91     private static final QueryProcessorType processorType = QueryProcessorType.LOCAL_GROOVY;
92     private static final QueryVersion DEFAULT_VERSION = QueryVersion.V1;
93
94     private final HttpEntry httpEntry;
95     private final SchemaVersions schemaVersions;
96     private final String basePath;
97     private final GremlinServerSingleton gremlinServerSingleton;
98     private final XmlFormatTransformer xmlFormatTransformer;
99     private final DslListener v1DslListener;
100     private final org.onap.aai.rest.dsl.v2.DslListener v2DslListener;
101
102     private QueryVersion dslApiVersion = DEFAULT_VERSION;
103     Map<QueryVersion, DslQueryProcessor> dslQueryProcessors;
104
105     @Autowired
106     public DslConsumer(@Qualifier("requestScopedTraversalUriHttpEntry") HttpEntry requestScopedTraversalUriHttpEntry,
107             SchemaVersions schemaVersions, GremlinServerSingleton gremlinServerSingleton,
108             XmlFormatTransformer xmlFormatTransformer,
109             @Value("${schema.uri.base.path}") String basePath, DslListener v1DslListener, org.onap.aai.rest.dsl.v2.DslListener v2DslListener) {
110         this.httpEntry = requestScopedTraversalUriHttpEntry;
111         this.schemaVersions = schemaVersions;
112         this.gremlinServerSingleton = gremlinServerSingleton;
113         this.xmlFormatTransformer = xmlFormatTransformer;
114         this.basePath = basePath;
115         this.v1DslListener = v1DslListener;
116         this.v2DslListener = v2DslListener;
117     }
118
119     @PutMapping(produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
120     public ResponseEntity<String> executeQuery(@RequestBody String dslQuery,
121                                                @PathVariable("version") String versionParam,
122                                                @RequestParam(defaultValue = "graphson") String format,
123                                                @RequestParam(defaultValue = "no_op") String subgraph,
124                                                @RequestParam(defaultValue = "all") String validate,
125                                                @RequestParam(defaultValue = "-1") int resultIndex,
126                                                @RequestParam(defaultValue = "-1") int resultSize,
127                                                @RequestHeader HttpHeaders headers,
128                                                HttpServletRequest request) throws FileNotFoundException, AAIException {
129         Set<String> roles = Collections.emptySet();
130
131         return processExecuteQuery(dslQuery, request, versionParam, format, subgraph,
132                 validate, headers, new Pageable(resultIndex, resultSize), roles);
133     }
134
135     public ResponseEntity<String> processExecuteQuery(String dslQuery, HttpServletRequest request, String versionParam,
136             String queryFormat, String subgraph, String validate, HttpHeaders headers,
137            Pageable pageable, Set<String> roles) throws FileNotFoundException, AAIException {
138
139         final SchemaVersion version = new SchemaVersion(versionParam);
140         final String sourceOfTruth = headers.getFirst("X-FromAppId");
141         final String dslOverride = headers.getFirst("X-DslOverride");
142         final MultivaluedMap<String,String> queryParams = toMultivaluedMap(request.getParameterMap());
143
144         Optional<String> dslApiVersionHeader =
145             Optional.ofNullable(headers.getFirst("X-DslApiVersion"));
146         if (dslApiVersionHeader.isPresent()) {
147             try {
148                 dslApiVersion = QueryVersion.valueOf(dslApiVersionHeader.get());
149             } catch (IllegalArgumentException e) {
150                 LOGGER.debug("Defaulting DSL Api Version to  " + DEFAULT_VERSION);
151             }
152         }
153
154         Pair<List<Object>,Map<String,List<String>>> executionResult = executeQuery(dslQuery, request, queryFormat, subgraph, validate, queryParams, pageable,
155                 roles, version, sourceOfTruth, dslOverride);
156         List<Object> vertices = executionResult.getValue0();
157
158         String result = serializeResponse(request, queryFormat, headers, version, sourceOfTruth, queryParams, executionResult.getValue1(), vertices);
159
160         if (PaginationUtil.hasValidPaginationParams(pageable)) {
161             int totalCount = vertices.size();
162             long totalPages = PaginationUtil.getTotalPages(pageable, totalCount);
163             return ResponseEntity.ok()
164                 .header("total-results", String.valueOf(totalCount))
165                 .header("total-pages", String.valueOf(totalPages))
166                 .body(result);
167         } else {
168             return ResponseEntity.ok(result);
169         }
170     }
171
172     private String serializeResponse(HttpServletRequest request, String queryFormat, HttpHeaders headers,
173             final SchemaVersion version, final String sourceOfTruth, MultivaluedMap<String, String> queryParameters, final Map<String, List<String>> propertiesMap,
174             List<Object> vertices) throws AAIException {
175         DBSerializer serializer =
176             new DBSerializer(version, httpEntry.getDbEngine(), ModelType.MOXY, sourceOfTruth);
177         String serverBase = request.getRequestURL().toString().replaceAll("/(v[0-9]+|latest)/.*", "/");
178         FormatFactory ff = new FormatFactory(httpEntry.getLoader(), serializer,
179                 schemaVersions, this.basePath, serverBase);
180
181         MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
182         mvm.putAll(queryParameters);
183         Format format = Format.getFormat(queryFormat);
184         if (isHistory(format)) {
185             mvm.putSingle("startTs", Long.toString(getStartTime(format, mvm)));
186             mvm.putSingle("endTs", Long.toString(getEndTime(mvm)));
187         }
188         Formatter formatter = ff.get(format, mvm);
189
190         String result = "";
191         if (propertiesMap != null && !propertiesMap.isEmpty()) {
192             result = formatter.output(vertices, propertiesMap).toString();
193         } else {
194             result = formatter.output(vertices).toString();
195         }
196
197         MediaType acceptType = headers.getAccept().stream()
198             .filter(Objects::nonNull)
199             .filter(header -> !header.equals(MediaType.ALL))
200             .findAny()
201             .orElse(MediaType.APPLICATION_JSON);
202
203         if (MediaType.APPLICATION_XML.isCompatibleWith(acceptType)) {
204             result = xmlFormatTransformer.transform(result);
205         }
206         return result;
207     }
208
209     private Pair<List<Object>,Map<String,List<String>>> executeQuery(String content, HttpServletRequest req, String queryFormat, String subgraph,
210             String validate, MultivaluedMap<String, String> queryParameters, Pageable pageable, Set<String> roles,
211             final SchemaVersion version, final String sourceOfTruth, final String dslOverride)
212             throws AAIException, FileNotFoundException {
213         final String serverBase =
214             req.getRequestURL().toString().replaceAll("/(v[0-9]+|latest)/.*", "/");
215         httpEntry.setHttpEntryProperties(version, serverBase);
216
217         JsonObject input = JsonParser.parseString(content).getAsJsonObject();
218         JsonElement dslElement = input.get("dsl");
219         String dsl = "";
220         if (dslElement != null) {
221             dsl = dslElement.getAsString();
222         }
223
224         boolean isDslOverride = dslOverride != null
225                 && !AAIConfig.get(TraversalConstants.DSL_OVERRIDE).equals("false")
226                 && dslOverride.equals(AAIConfig.get(TraversalConstants.DSL_OVERRIDE));
227
228         DslQueryProcessor dslQueryProcessor = dslApiVersion.equals(QueryVersion.V1)
229             ? new V1DslQueryProcessor()
230             : new V2DslQueryProcessor();
231         if (isDslOverride) {
232             dslQueryProcessor.setStartNodeValidationFlag(false);
233         }
234
235         dslQueryProcessor.setValidationRules(validate);
236
237         Format format = Format.getFormat(queryFormat);
238
239         if (isAggregate(format)) {
240             dslQueryProcessor.setAggregate(true);
241         }
242
243         if (isHistory(format)) {
244             validateHistoryParams(format, queryParameters);
245         }
246
247         final TransactionalGraphEngine dbEngine = httpEntry.getDbEngine();
248         GraphTraversalSource traversalSource =
249             getTraversalSource(dbEngine, format, queryParameters, roles);
250
251         GenericQueryProcessor processor =
252             new GenericQueryProcessor.Builder(dbEngine, gremlinServerSingleton)
253                 .queryFrom(dsl, "dsl").queryProcessor(dslQueryProcessor).version(dslApiVersion)
254                 .processWith(processorType).format(format).uriParams(queryParameters)
255                 .traversalSource(isHistory(format), traversalSource).create();
256
257         SubGraphStyle subGraphStyle = SubGraphStyle.valueOf(subgraph);
258         List<Object> vertTemp = processor.execute(subGraphStyle);
259
260         List<Object> vertices;
261         if (isAggregate(format)) {
262             // Dedup if duplicate objects are returned in each array in the aggregate format
263             // scenario.
264             List<Object> vertTempDedupedObjectList = dedupObjectInAggregateFormatResultStreams(vertTemp);
265             vertices = PaginationUtil.hasValidPaginationParams(pageable)
266                 ? vertices = PaginationUtil.getPaginatedVertexListForAggregateFormat(vertTempDedupedObjectList, pageable)
267                 : vertTempDedupedObjectList;
268         } else {
269             int startIndex = pageable.getPage() * pageable.getPageSize();
270             vertices = PaginationUtil.hasValidPaginationParams(pageable)
271                 ? vertTemp.subList(startIndex, startIndex + pageable.getPageSize())
272                 : vertTemp;
273         }
274
275         return Pair.with(vertices, processor.getPropertiesMap());
276     }
277
278     private List<Object> dedupObjectInAggregateFormatResultStreams(List<Object> vertTemp) {
279         return vertTemp.stream()
280             .filter(o -> o instanceof ArrayList)
281             .map(o -> ((ArrayList<?>) o).stream().distinct().collect(Collectors.toList()))
282             .collect(Collectors.toList());
283     }
284
285     private MultivaluedMap<String, String> toMultivaluedMap(Map<String, String[]> map) {
286         MultivaluedMap<String, String> multivaluedMap = new MultivaluedHashMap<>();
287
288         for (Map.Entry<String, String[]> entry : map.entrySet()) {
289             for (String val : entry.getValue())
290             multivaluedMap.add(entry.getKey(), val);
291         }
292
293         return multivaluedMap;
294     }
295 }