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