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