Adding pass through shema creation
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / rest / SearchServiceApi.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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.sa.rest;
22
23 import org.onap.aai.sa.auth.SearchDbServiceAuth;
24 import org.onap.aai.sa.rest.ApiUtils.Action;
25 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
26 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
27
28 import java.security.cert.X509Certificate;
29 import javax.security.auth.x500.X500Principal;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.HttpHeaders;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43
44
45 public class SearchServiceApi {
46
47   /**
48    * The Data Access Object that we will use to interact with the
49    * document store.
50    */
51   protected DocumentStoreInterface documentStore = null;
52   protected ApiUtils apiUtils = null;
53
54
55   /**
56    * Create a new instance of the end point.
57    */
58   public SearchServiceApi() {
59
60     // Perform one-time initialization.
61     init();
62   }
63
64
65   /**
66    * Performs all one-time initialization required for the end point.
67    */
68   public void init() {
69
70     // Instantiate our Document Store DAO.
71     documentStore = ElasticSearchHttpController.getInstance();
72
73     apiUtils = new ApiUtils();
74   }
75
76   @PUT
77   @Path("/indexes/{index}")
78   @Consumes({MediaType.APPLICATION_JSON})
79   public Response processCreateIndex(String requestBody,
80                                      @Context HttpServletRequest request,
81                                      @Context HttpHeaders headers,
82                                      @PathParam("index") String index) {
83
84     // Forward the request to our index API to create the index.
85     IndexApi indexApi = new IndexApi(this);
86     return indexApi.processCreateIndex(requestBody, request, headers, index, documentStore);
87   }
88
89   @PUT
90   @Path("/indexes/dynamic/{index}")
91   @Consumes({MediaType.APPLICATION_JSON})
92   public Response processCreateDynamicIndex(String requestBody,
93                                      @Context HttpServletRequest request,
94                                      @Context HttpHeaders headers,
95                                      @PathParam("index") String index) {
96
97     // Forward the request to our index API to create the index.
98     IndexApi indexApi = new IndexApi(this);
99     return indexApi.processCreateDynamicIndex(requestBody, request, headers, index, documentStore);
100   }
101
102   @DELETE
103   @Path("/indexes/{index}")
104   @Consumes({MediaType.APPLICATION_JSON})
105   public Response processDeleteIndex(String requestBody,
106                                      @Context HttpServletRequest request,
107                                      @Context HttpHeaders headers,
108                                      @PathParam("index") String index) {
109
110     // Forward the request to our index API to delete the index.
111     IndexApi indexApi = new IndexApi(this);
112     return indexApi.processDelete(index, request, headers, documentStore);
113   }
114
115
116   @GET
117   @Path("/indexes/{index}/documents/{id}")
118   @Consumes({MediaType.APPLICATION_JSON})
119   public Response processGetDocument(String requestBody,
120                                      @Context HttpServletRequest request,
121                                      @Context HttpServletResponse httpResponse,
122                                      @Context HttpHeaders headers,
123                                      @PathParam("index") String index,
124                                      @PathParam("id") String id) {
125
126     // Forward the request to our document API to retrieve the document.
127     DocumentApi documentApi = new DocumentApi(this);
128     return documentApi.processGet(requestBody, request, headers, httpResponse,
129         index, id, documentStore);
130   }
131
132   @POST
133   @Path("/indexes/{index}/documents")
134   @Consumes({MediaType.APPLICATION_JSON})
135   public Response processCreateDocWithoutId(String requestBody,
136                                             @Context HttpServletRequest request,
137                                             @Context HttpServletResponse httpResponse,
138                                             @Context HttpHeaders headers,
139                                             @PathParam("index") String index) {
140
141     // Forward the request to our document API to create the document.
142     DocumentApi documentApi = new DocumentApi(this);
143     return documentApi.processPost(requestBody, request, headers, httpResponse,
144         index, documentStore);
145   }
146
147   @PUT
148   @Path("/indexes/{index}/documents/{id}")
149   @Consumes({MediaType.APPLICATION_JSON})
150   public Response processUpsertDoc(String requestBody,
151                                    @Context HttpServletRequest request,
152                                    @Context HttpServletResponse httpResponse,
153                                    @Context HttpHeaders headers,
154                                    @PathParam("index") String index,
155                                    @PathParam("id") String id) {
156
157     // Forward the request to our document API to upsert the document.
158     DocumentApi documentApi = new DocumentApi(this);
159     return documentApi.processPut(requestBody, request, headers, httpResponse,
160         index, id, documentStore);
161   }
162
163   @DELETE
164   @Path("/indexes/{index}/documents/{id}")
165   @Consumes({MediaType.APPLICATION_JSON})
166   public Response processDeleteDoc(String requestBody,
167                                    @Context HttpServletRequest request,
168                                    @Context HttpServletResponse httpResponse,
169                                    @Context HttpHeaders headers,
170                                    @PathParam("index") String index,
171                                    @PathParam("id") String id) {
172
173     // Forward the request to our document API to delete the document.
174     DocumentApi documentApi = new DocumentApi(this);
175     return documentApi.processDelete(requestBody, request, headers, httpResponse,
176         index, id, documentStore);
177   }
178
179
180   @GET
181   @Path("/indexes/{index}/query/{queryText}")
182   @Consumes({MediaType.APPLICATION_JSON})
183   public Response processInlineQuery(String requestBody,
184                                      @Context HttpServletRequest request,
185                                      @Context HttpHeaders headers,
186                                      @PathParam("index") String index,
187                                      @PathParam("queryText") String queryText) {
188
189     // Forward the request to our document API to delete the document.
190     DocumentApi documentApi = new DocumentApi(this);
191     return documentApi.processSearchWithGet(requestBody, request, headers,
192         index, queryText, documentStore);
193   }
194
195
196   @GET
197   @Path("/indexes/{index}/query")
198   @Consumes({MediaType.APPLICATION_JSON})
199   public Response processQueryWithGet(String requestBody,
200                                       @Context HttpServletRequest request,
201                                       @Context HttpHeaders headers,
202                                       @PathParam("index") String index) {
203
204     // Forward the request to our document API to delete the document.
205     DocumentApi documentApi = new DocumentApi(this);
206     return documentApi.queryWithGetWithPayload(requestBody, request, headers, index, documentStore);
207   }
208
209   @POST
210   @Path("/indexes/{index}/query")
211   @Consumes({MediaType.APPLICATION_JSON})
212   public Response processQuery(String requestBody,
213                                @Context HttpServletRequest request,
214                                @Context HttpHeaders headers,
215                                @PathParam("index") String index) {
216
217     // Forward the request to our document API to delete the document.
218     DocumentApi documentApi = new DocumentApi(this);
219     return documentApi.processSearchWithPost(requestBody, request, headers, index, documentStore);
220   }
221
222
223   @POST
224   @Path("/bulk")
225   @Consumes({MediaType.APPLICATION_JSON})
226   public Response processBulkRequest(String requestBody,
227                                      @Context HttpServletRequest request,
228                                      @Context HttpHeaders headers,
229                                      @PathParam("index") String index) {
230
231     // Forward the request to our document API to delete the document.
232     BulkApi bulkApi = new BulkApi(this);
233     return bulkApi.processPost(requestBody, request, headers, documentStore, apiUtils);
234   }
235
236   protected boolean validateRequest(HttpHeaders headers,
237                                     HttpServletRequest req,
238                                     Action action,
239                                     String authPolicyFunctionName) throws Exception {
240
241     SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
242
243     String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
244     String authUser = null;
245     if (cipherSuite != null) {
246       Object x509CertAttribute = req.getAttribute("javax.servlet.request.X509Certificate");
247       if (x509CertAttribute != null) {
248         X509Certificate[] certChain = (X509Certificate[]) x509CertAttribute;
249         X509Certificate clientCert = certChain[0];
250         X500Principal subjectDn = clientCert.getSubjectX500Principal();
251         authUser = subjectDn.toString();
252       }
253     }
254
255     if (authUser == null) {
256       return false;
257     }
258
259     String status = serviceAuth.authUser(headers, authUser.toLowerCase(),
260         action.toString() + ":" + authPolicyFunctionName);
261     if (!status.equals("OK")) {
262       return false;
263     }
264
265     return true;
266   }
267 }