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