Merge "Added docs directory and index file for traversal"
[aai/traversal.git] / aai-traversal / src / main / java / org / openecomp / aai / rest / search / SearchProvider.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.rest.search;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
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;
37
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;
53
54 /**
55  * Implements the search subdomain in the REST API. All API calls must include
56  * X-FromAppId and X-TransactionId in the header.
57  * 
58  
59  *
60  */
61
62 @Path("/{version: v2|v[789]|v1[01]|latest}/search")
63 public class SearchProvider extends RESTAPI {
64         
65         protected static String authPolicyFunctionName = "search";
66
67         public static final String GENERIC_QUERY = "/generic-query";
68
69         public static final String NODES_QUERY = "/nodes-query";
70
71         /**
72          * Gets the generic query response.
73          *
74          * @param headers the headers
75          * @param req the req
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
81          */
82         /* ---------------- Start Generic Query --------------------- */
83         @GET
84         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
85         @Path(GENERIC_QUERY)
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
93                                                                                         ) {
94                 
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>();
101                 try { 
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;
110                         } else {
111                                 version = Version.valueOf(versionParam);
112                         }
113                         final ModelType factoryType = ModelType.MOXY;
114                         Loader loader = LoaderFactory.createLoaderForVersion(factoryType, version);
115                         TransactionalGraphEngine dbEngine = new TitanDBEngine(
116                                         QueryStyle.TRAVERSAL,
117                                         type,
118                                         loader);
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(
123                                                                                                            headers,
124                                                                                                            startNodeType,
125                                                                                                            startNodeKeyParams,
126                                                                                                            includeNodeTypes, 
127                                                                                                            depth,
128                                                                                                            dbEngine,
129                                                                                                            loader,
130                                                                                                            urlBuilder
131                                                                                                            
132                                                                                                            );
133         
134                         String respTm = genDate();
135                 
136                 } catch (AAIException e) { 
137                         // send error response
138                         ex = e;
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))
144                                                         .build();
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))
153                                                         .build();
154                 } finally {
155                         // log success or failure
156                         if (ex != null){
157                                 ErrorLogHelper.logException(ex);
158                         }
159                 }
160
161                 return searchResult;
162         }
163
164         /* ---------------- End Generic Query --------------------- */
165
166         /**
167          * Gets the nodes query response.
168          *
169          * @param headers the headers
170          * @param req the req
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
175          */
176         /* ---------------- Start Nodes Query --------------------- */
177         @GET
178         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
179         @Path(NODES_QUERY)
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>();       
192                 try { 
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);
198                         
199                         final Version version;
200                         if ("latest".equals(versionParam)) {
201                                 version = AAIProperties.LATEST;
202                         } else {
203                                 version = Version.valueOf(versionParam);
204                         }
205                         final ModelType factoryType = ModelType.MOXY;
206                         Loader loader = LoaderFactory.createLoaderForVersion(factoryType, version);
207                         TransactionalGraphEngine dbEngine = new TitanDBEngine(
208                                         QueryStyle.TRAVERSAL,
209                                         type,
210                                         loader);
211                         DBSerializer dbSerializer = new DBSerializer(version, dbEngine, factoryType, fromAppId);
212                         UrlBuilder urlBuilder = new UrlBuilder(version, dbSerializer);
213                         SearchGraph searchGraph = new SearchGraph();
214                         
215                         searchResult = searchGraph.runNodesQuery(headers,
216                                                                                                         searchNodeType,
217                                                                                                         edgeFilterList, 
218                                                                                                         filterList,
219                                                                                                         dbEngine,
220                                                                                                         loader,
221                                                                                                         urlBuilder);
222         
223                         String respTm = genDate();
224                 } catch (AAIException e) { 
225                         // send error response
226                         ex = e;
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))
232                                                         .build();
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))
241                                                         .build();
242                 } finally {
243                         // log success or failure
244                         if (ex != null){
245                                 ErrorLogHelper.logException(ex);
246                         }
247                 }
248                 return searchResult;
249         }
250
251
252         
253 }