2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.aai.rest.search;
23 import java.util.ArrayList;
24 import java.util.List;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.Status;
38 import org.openecomp.aai.db.props.AAIProperties;
39 import org.openecomp.aai.dbgraphmap.SearchGraph;
40 import org.openecomp.aai.dbmap.DBConnectionType;
41 import org.openecomp.aai.exceptions.AAIException;
42 import org.openecomp.aai.introspection.Loader;
43 import org.openecomp.aai.introspection.LoaderFactory;
44 import org.openecomp.aai.introspection.ModelType;
45 import org.openecomp.aai.introspection.Version;
46 import org.openecomp.aai.logging.ErrorLogHelper;
47 import org.openecomp.aai.restcore.RESTAPI;
48 import org.openecomp.aai.serialization.db.DBSerializer;
49 import org.openecomp.aai.serialization.engines.QueryStyle;
50 import org.openecomp.aai.serialization.engines.TitanDBEngine;
51 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
52 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
55 * Implements the search subdomain in the REST API. All API calls must include
56 * X-FromAppId and X-TransactionId in the header.
62 @Path("/{version: v2|v[789]|v1[01]|latest}/search")
63 public class SearchProvider extends RESTAPI {
65 protected static String authPolicyFunctionName = "search";
67 public static final String GENERIC_QUERY = "/generic-query";
69 public static final String NODES_QUERY = "/nodes-query";
72 * Gets the generic query response.
74 * @param headers the headers
76 * @param startNodeType the start node type
77 * @param startNodeKeyParams the start node key params
78 * @param includeNodeTypes the include node types
79 * @param depth the depth
80 * @return the generic query response
82 /* ---------------- Start Generic Query --------------------- */
84 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
86 public Response getGenericQueryResponse(@Context HttpHeaders headers,
87 @Context HttpServletRequest req,
88 @QueryParam("start-node-type") final String startNodeType,
89 @QueryParam("key") final List<String> startNodeKeyParams,
90 @QueryParam("include") final List<String> includeNodeTypes,
91 @QueryParam("depth") final int depth,
92 @PathParam("version")String versionParam
95 AAIException ex = null;
96 Response searchResult = null;
97 String fromAppId = null;
98 String transId = null;
99 String rqstTm = genDate();
100 ArrayList<String> templateVars = new ArrayList<String>();
102 fromAppId = getFromAppId(headers);
103 transId = getTransId(headers);
104 String realTime = headers.getRequestHeaders().getFirst("Real-Time");
105 //only consider header value for search
106 DBConnectionType type = this.determineConnectionType("force-cache", realTime);
107 final Version version;
108 if ("latest".equals(versionParam)) {
109 version = AAIProperties.LATEST;
111 version = Version.valueOf(versionParam);
113 final ModelType factoryType = ModelType.MOXY;
114 Loader loader = LoaderFactory.createLoaderForVersion(factoryType, version);
115 TransactionalGraphEngine dbEngine = new TitanDBEngine(
116 QueryStyle.TRAVERSAL,
119 DBSerializer dbSerializer = new DBSerializer(version, dbEngine, factoryType, fromAppId);
120 UrlBuilder urlBuilder = new UrlBuilder(version, dbSerializer);
121 SearchGraph searchGraph = new SearchGraph();
122 searchResult = searchGraph.runGenericQuery(
134 String respTm = genDate();
136 } catch (AAIException e) {
137 // send error response
139 templateVars.add("GET Search");
140 templateVars.add("getGenericQueryResponse");
141 searchResult = Response
142 .status(e.getErrorObject().getHTTPResponseCode())
143 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars))
145 } catch (Exception e) {
146 // send error response
147 ex = new AAIException("AAI_4000", e);
148 templateVars.add("GET Search");
149 templateVars.add("getGenericQueryResponse");
150 searchResult = Response
151 .status(Status.INTERNAL_SERVER_ERROR)
152 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
155 // log success or failure
157 ErrorLogHelper.logException(ex);
164 /* ---------------- End Generic Query --------------------- */
167 * Gets the nodes query response.
169 * @param headers the headers
171 * @param searchNodeType the search node type
172 * @param edgeFilterList the edge filter list
173 * @param filterList the filter list
174 * @return the nodes query response
176 /* ---------------- Start Nodes Query --------------------- */
178 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
180 public Response getNodesQueryResponse(@Context HttpHeaders headers,
181 @Context HttpServletRequest req,
182 @QueryParam("search-node-type") final String searchNodeType,
183 @QueryParam("edge-filter") final List<String> edgeFilterList,
184 @QueryParam("filter") final List<String> filterList,
185 @PathParam("version")String versionParam) {
186 AAIException ex = null;
187 Response searchResult = null;
188 String fromAppId = null;
189 String transId = null;
190 String rqstTm = genDate();
191 ArrayList<String> templateVars = new ArrayList<String>();
193 fromAppId = getFromAppId(headers);
194 transId = getTransId(headers);
195 String realTime = headers.getRequestHeaders().getFirst("Real-Time");
196 //only consider header value for search
197 DBConnectionType type = this.determineConnectionType("force-cache", realTime);
199 final Version version;
200 if ("latest".equals(versionParam)) {
201 version = AAIProperties.LATEST;
203 version = Version.valueOf(versionParam);
205 final ModelType factoryType = ModelType.MOXY;
206 Loader loader = LoaderFactory.createLoaderForVersion(factoryType, version);
207 TransactionalGraphEngine dbEngine = new TitanDBEngine(
208 QueryStyle.TRAVERSAL,
211 DBSerializer dbSerializer = new DBSerializer(version, dbEngine, factoryType, fromAppId);
212 UrlBuilder urlBuilder = new UrlBuilder(version, dbSerializer);
213 SearchGraph searchGraph = new SearchGraph();
215 searchResult = searchGraph.runNodesQuery(headers,
223 String respTm = genDate();
224 } catch (AAIException e) {
225 // send error response
227 templateVars.add("GET Search");
228 templateVars.add("getNodesQueryResponse");
229 searchResult = Response
230 .status(e.getErrorObject().getHTTPResponseCode())
231 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars))
233 } catch (Exception e) {
234 // send error response
235 ex = new AAIException("AAI_4000", e);
236 templateVars.add("GET Search");
237 templateVars.add("getNodesQueryResponse");
238 searchResult = Response
239 .status(Status.INTERNAL_SERVER_ERROR)
240 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
243 // log success or failure
245 ErrorLogHelper.logException(ex);