aboutsummaryrefslogtreecommitdiffstats
path: root/aai-traversal/src/main/java/org/onap/aai/rest/CQ2Gremlin.java
blob: c8f1f0b5bcbdb04afde56f2136cb7e6f2a1b6256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.rest;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.onap.aai.config.SpringContextAware;
import org.onap.aai.introspection.LoaderFactory;
import org.onap.aai.rest.db.HttpEntry;
import org.onap.aai.rest.search.CustomQueryConfigDTO;
import org.onap.aai.rest.search.CustomQueryDTO;
import org.onap.aai.restcore.RESTAPI;
import org.onap.aai.restcore.search.GroovyQueryBuilder;
import org.onap.aai.serialization.db.EdgeSerializer;
import org.onap.aai.serialization.engines.TransactionalGraphEngine;
import org.onap.aai.setup.SchemaVersions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;

@Path("/cq2gremlin")
public class CQ2Gremlin extends RESTAPI {

    private HttpEntry traversalUriHttpEntry;

    @Autowired
    protected LoaderFactory loaderFactory;

    @Autowired
    protected EdgeSerializer rules;

    @Autowired
    public CQ2Gremlin(HttpEntry traversalUriHttpEntry,
        @Value("${schema.uri.base.path}") String basePath) {
        this.traversalUriHttpEntry = traversalUriHttpEntry;
    }

    @PUT
    @Path("")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON})
    public Response getC2Qgremlin(@RequestBody Map<String, CustomQueryConfigDTO> content,
        @Context HttpHeaders headers, @Context UriInfo info) {
        if (content.size() == 0) {
            return Response.status(HttpStatus.BAD_REQUEST.value())
                .entity("At least one custom query should be passed").build();
        }
        return processGremlinQuery(content.values().toArray(new CustomQueryConfigDTO[0])[0], info,
            headers);
    }

    protected Response processGremlinQuery(CustomQueryConfigDTO content, UriInfo info,
        HttpHeaders headers) {
        try {
            LinkedHashMap<String, Object> params;
            CustomQueryDTO queryDTO = content.getQueryDTO();
            String query = queryDTO.getQuery();
            params = new LinkedHashMap<>();

            List<String> optionalParameters = queryDTO.getQueryOptionalProperties();
            if (!optionalParameters.isEmpty()) {
                for (String key : optionalParameters) {
                    params.put(key, key);
                }
            }

            List<String> requiredParameters = queryDTO.getQueryRequiredProperties();
            if (!requiredParameters.isEmpty()) {
                for (String key : requiredParameters) {
                    params.put(key, key);
                }
            }

            SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
            traversalUriHttpEntry.setHttpEntryProperties(schemaVersions.getDefaultVersion());
            traversalUriHttpEntry.setPaginationParameters("-1", "-1");

            TransactionalGraphEngine dbEngine = traversalUriHttpEntry.getDbEngine();

            query = new GroovyQueryBuilder().executeTraversal(dbEngine, query, params);
            query = "g" + query;
            return Response.ok(query).build();
        } catch (Exception ex) {
            return Response.status(500)
                .entity("Query conversion failed with following reason: " + ex.toString()).build();
        }

    }
}