aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/datarouter/util/SearchServiceAgent.java
blob: 3d27425937a879353a9382736af470a1ef7c7fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
 * ============LICENSE_START=======================================================
 * DataRouter
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property.
 * Copyright © 2017 Amdocs
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP and OpenECOMP are trademarks
 * and service marks of AT&T Intellectual Property.
 */
package org.openecomp.datarouter.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.Status;

import org.eclipse.jetty.util.security.Password;
import org.openecomp.cl.api.Logger;
import org.openecomp.cl.mdc.MdcContext;
import org.openecomp.datarouter.logging.DataRouterMsgs;
import org.openecomp.datarouter.policy.EntityEventPolicy;
import org.openecomp.restclient.client.Headers;
import org.openecomp.restclient.client.OperationResult;
import org.openecomp.restclient.client.RestClient;
import org.openecomp.restclient.enums.RestAuthenticationMode;
import org.openecomp.restclient.rest.HttpUtil;
import org.slf4j.MDC;

import com.sun.jersey.core.util.MultivaluedMapImpl;

public class SearchServiceAgent {

  private Logger logger;
  
  private RestClient searchClient = null;
  private Map<String, String> indexSchemaMapping = new HashMap<String, String>();
  
  private String searchUrl = null;
  private String documentEndpoint = null;
  
  
  /**
   * Creates a new instance of the search service agent.
   * 
   * @param certName         - Certificate to use for talking to the Search Service.
   * @param keystore         - Keystore to use for talking to the Search Service.
   * @param keystorePwd      - Keystore password for talking to the Search Service.
   * @param searchUrl        - URL at which the Search Service can be reached.
   * @param documentEndpoint - Endpoint for accessing document resources on the Search Service.
   * @param logger           - Logger to use for system logs.
   */
  public SearchServiceAgent(String certName, 
                            String keystore, 
                            String keystorePwd,
                            String searchUrl,
                            String documentEndpoint,
                            Logger logger) {
    
    initialize(certName, keystore, keystorePwd, searchUrl, documentEndpoint, logger);
  }
  
  
  /**
   * Performs all one-time initialization required for the search agent.
   * 
   * @param certName         - Certificate to use for talking to the Search Service.
   * @param keystore         - Keystore to use for talking to the Search Service.
   * @param keystorePwd      - Keystore password for talking to the Search Service.
   * @param searchUrl        - URL at which the Search Service can be reached.
   * @param documentEndpoint - Endpoint for accessing document resources on the Search Service.
   * @param logger           - Logger to use for system logs.
   */
  private void initialize(String certName, 
                          String keystore, 
                          String keystorePwd, 
                          String searchUrl, 
                          String documentEndpoint, 
                          Logger logger) {
    
    // Create REST client for search service
    searchClient = new RestClient()
                    .authenticationMode(RestAuthenticationMode.SSL_CERT)
                    .validateServerHostname(false)
                    .validateServerCertChain(true)
                    .clientCertFile(DataRouterConstants.DR_HOME_AUTH + certName)
                    .clientCertPassword(Password.deobfuscate(keystorePwd))
                    .trustStore(DataRouterConstants.DR_HOME_AUTH + keystore);
    
    this.searchUrl        = searchUrl;
    this.documentEndpoint = documentEndpoint;
    
    this.logger           = logger;
  }
  
  
  /**
   * Creates an index through the search db abstraction
   * 
   * @param index          - The name of the index to be created.
   * @param schemaLocation - The name of the schema file for the index.
   */
  public void createSearchIndex(String index, String schemaLocation) {
     
    // Create a mapping of the index name to schema location 
    indexSchemaMapping.put(index, schemaLocation);
    
    // Now, create the index.
    createIndex(index, schemaLocation);
  }
  
  
  /**
   * This method performs the actual work of creating a search index.
   * 
   * @param index          - The name of the index to be created.
   * @param schemaLocation - The name of the schema file for the index.
   */
  private void createIndex(String index, String schemaLocation) {
    
    logger.debug("Creating search index, index name: = " + index + ", schemaLocation = " + schemaLocation);
    
    MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
    headers.put("Accept", Arrays.asList("application/json"));
    headers.put(Headers.FROM_APP_ID, Arrays.asList("DL"));
    headers.put(Headers.TRANSACTION_ID, Arrays.asList(UUID.randomUUID().toString()));
      
    String url = concatSubUri(searchUrl, index);
    try {

      OperationResult result = searchClient.put(url, loadFileData(schemaLocation), headers,
                                                MediaType.APPLICATION_JSON_TYPE, null);

      if (!HttpUtil.isHttpResponseClassSuccess(result.getResultCode())) {
        logger.error(DataRouterMsgs.FAIL_TO_CREATE_SEARCH_INDEX, index, result.getFailureCause());
      } else {
        logger.info(DataRouterMsgs.SEARCH_INDEX_CREATE_SUCCESS, index);
      }

    } catch (Exception e) {
      logger.error(DataRouterMsgs.FAIL_TO_CREATE_SEARCH_INDEX, index, e.getLocalizedMessage());
    }
  }
  
  
  /**
   * Retrieves a document from the search service.
   * 
   * @param index - The index to retrieve the document from.
   * @param id    - The unique identifier for the document.
   * 
   * @return - The REST response returned from the Search Service.
   */
  public OperationResult getDocument(String index, String id) {
    
    Map<String, List<String>> headers = new HashMap<>();
    headers.put(Headers.FROM_APP_ID, Arrays.asList("Data Router"));
    headers.put(Headers.TRANSACTION_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
    
    String url = concatSubUri(searchUrl, index, documentEndpoint, id);
    return searchClient.get(url, headers, MediaType.APPLICATION_JSON_TYPE);    
  }
  
  
  /**
   * Creates or updates a document in the Search Service.
   * 
   * @param index   - The index to create or update the document in.
   * @param id      - The identifier for the document.
   * @param payload - The document contents.
   * @param headers - HTTP headers.
   */
  public void putDocument(String index, String id, String payload, Map<String, List<String>> headers) {
        
    // Try to post the document to the search service.
    OperationResult result = doDocumentPut(index, id, payload, headers);
    
    // A 404 response from the Search Service may indicate that the index we are writing
    // to does not actually exist.  We will try creating it now.
    if(result.getResultCode() == Status.NOT_FOUND.getStatusCode()) {
            
      // Lookup the location of the schema that we want to create.
      String indexSchemaLocation = indexSchemaMapping.get(index);
      if(indexSchemaLocation != null) {
        
        // Try creating the index now...
        logger.info(DataRouterMsgs.CREATE_MISSING_INDEX, index);
        createIndex(index, indexSchemaLocation);
        
        // ...and retry the document post.
        result = doDocumentPut(index, id, payload, headers);
      }
    }
    
    if(!resultSuccessful(result)) {
      logger.error(DataRouterMsgs.FAIL_TO_CREATE_UPDATE_DOC, index, result.getFailureCause());
    }
  }
  
  
  /**
   * This method does the actual work of submitting a document PUT request to the Search Service.
   * 
   * @param index   - The index to create or update the document in.
   * @param id      - The identifier for the document.
   * @param payload - The document contents.
   * @param headers - HTTP headers.
   * 
   * @return - The HTTP response returned by the Search Service.
   */
  private OperationResult doDocumentPut(String index, String id, String payload, Map<String, List<String>> headers) {
    
    String url = concatSubUri(searchUrl, index, documentEndpoint, id);
    return searchClient.put(url, payload, headers, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
  }
  
  
  /**
   * Creates a document in the Search Service.
   * 
   * @param index   - The index to create the document in.
   * @param payload - The document contents.
   * @param headers - HTTP headers.
   */
  public void postDocument(String index, String payload, Map<String, List<String>> headers) {
    
    // Try to post the document to the search service.
    OperationResult result = doDocumentPost(index, payload, headers);
    
    // A 404 response from the Search Service may indicate that the index we are writing
    // to does not actually exist.  We will try creating it now.
    if(result.getResultCode() == Status.NOT_FOUND.getStatusCode()) {
      
      // Lookup the location of the schema that we want to create.
      String indexSchemaLocation = indexSchemaMapping.get(index);
      if(indexSchemaLocation != null) {
        
        // Try creating the index now...
        logger.info(DataRouterMsgs.CREATE_MISSING_INDEX, index);
        createIndex(index, indexSchemaLocation);
        
        // ...and retry the document post.
        result = doDocumentPost(index, payload, headers);
      }
    }
    
    if(!resultSuccessful(result)) {
      logger.error(DataRouterMsgs.FAIL_TO_CREATE_UPDATE_DOC, index, result.getFailureCause());
    }
  }
  
  
  /**
   * This method does the actual work of submitting a document PUT request to the Search Service.
   * 
   * @param index   - The index to create or update the document in.
   * @param payload - The document contents.
   * @param headers - HTTP headers.
   * 
   * @return - The HTTP response returned by the Search Service.
   */
  private OperationResult doDocumentPost(String index, String payload, Map<String, List<String>> headers) {
    
    String url = concatSubUri(searchUrl, index, documentEndpoint);
    return searchClient.post(url, payload, headers, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
  }
  
  
  /**
   * Removes a document from the Search Service.
   * 
   * @param index   - The index to create the document in.
   * @param id      - The identifier for the document.
   * @param payload - The document contents.
   * @param headers - HTTP headers.
   */
  public void deleteDocument(String index, String documentId, Map<String, List<String>> headers) {
    
    String url = concatSubUri(searchUrl, index, documentEndpoint, documentId);
    searchClient.delete(url, headers, null);
  }
  
  
  /**
   * Convenience method to load up all the data from a file into a string
   * 
   * @param filename the filename to read from disk
   * @return the data contained within the file
   * @throws Exception
   */
  protected String loadFileData(String filename) throws Exception {
    StringBuilder data = new StringBuilder();
    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(
          EntityEventPolicy.class.getClassLoader().getResourceAsStream("/" + filename),
          StandardCharsets.UTF_8));
      String line;

      while ((line = in.readLine()) != null) {
        data.append(line);
      }
    } catch (Exception e) {
      throw new Exception("Failed to read from file = " + filename + ".", e);
    }

    return data.toString();
  }
  
  
  /**
   * Helper utility to concatenate substrings of a URI together to form a proper URI.
   * 
   * @param suburis the list of substrings to concatenate together
   * @return the concatenated list of substrings
   */
  public static String concatSubUri(String... suburis) {
    String finalUri = "";

    for (String suburi : suburis) {

      if (suburi != null) {
        // Remove any leading / since we only want to append /
        suburi = suburi.replaceFirst("^/*", "");

        // Add a trailing / if one isn't already there
        finalUri += suburi.endsWith("/") ? suburi : suburi + "/";
      }
    }

    return finalUri;
  }
  
  
  /**
   * Helper utility to check the response code of an HTTP response.
   * 
   * @param aResult - The response that we want to check.
   * 
   * @return - true if the response contains a success code,
   *           false otherwise.
   */
  private boolean resultSuccessful(OperationResult aResult) {
    
    return (aResult.getResultCode() >= 200) && (aResult.getResultCode() < 300);
  }
}