@Test public void testPublishWithNoAnnotations() throws Exception{ elasticSearchDataPublisher = new ElasticSearchDataPublisher(true); List<AutoFixTransaction> autoFixTrans = Lists.newArrayList(); AutoFixTransaction autoFixTransaction = new AutoFixTransaction(); autoFixTransaction.setDesc("desc"); autoFixTransaction.setAction(AutoFixAction.AUTOFIX_ACTION_BACKUP); autoFixTransaction.setExecutionId("executionId"); autoFixTransaction.setResourceId("resourceId"); autoFixTransaction.setRuleId("ruleId"); autoFixTransaction.setTransactionId("transactionId"); autoFixTransaction.setTransationTime("transationTime"); autoFixTrans.add(autoFixTransaction); AutoFixTransaction autoFixTransaction1 = new AutoFixTransaction(AutoFixAction.AUTOFIX_ACTION_BACKUP, "resourceId", "ruleId", "executionId", "transactionId", "desc","type","targetType","annotationId"); assertTrue(autoFixTransaction1.equals(autoFixTransaction1)); assertNotNull(elasticSearchDataPublisher.publishAutoFixTransactions(autoFixTrans,new HashMap<>())); }
public int publishAutoFixTransactions(List<AutoFixTransaction> autoFixTrans, Map<String, String> ruleParam) { if (autoFixTrans != null && autoFixTrans.size() == 0) { return 0; } BulkRequest bulkRequest = new BulkRequest(); Gson gson = new Gson(); StringBuffer bulkRequestBody = new StringBuffer(); String response = ""; List<Map<String, Map>> responseList = new ArrayList<>(); String esUrl = ESUtils.getEsUrl(); String bulkPostUrl = esUrl + AnnotationPublisher.BULK_WITH_REFRESH_TRUE; final String autoFixType= PacmanSdkConstants.TYPE_FOR_AUTO_FIX_RECORD+"_"+ruleParam.get(PacmanSdkConstants.TARGET_TYPE); for (AutoFixTransaction autoFixTransaction : autoFixTrans) { if(AutoFixAction.AUTOFIX_ACTION_FIX.equals(autoFixTransaction.getAction())) { if(!ESUtils.isValidType(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType)){ try { ESUtils.createMappingWithParent(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType, ruleParam.get(PacmanSdkConstants.TARGET_TYPE)); } catch (Exception e) { logger.error("uanble to create child type"); } } try{ bulkRequestBody.append(String.format(BULK_INDEX_REQUEST_TEMPLATE, getIndexName(ruleParam),getDocId(autoFixTransaction), autoFixType)); bulkRequestBody.append(gson.toJson(autoFixTransaction)); bulkRequestBody.append("\n"); if (bulkRequestBody.toString().getBytes().length / (1024 * 1024) >= PacmanSdkConstants.ES_MAX_BULK_POST_SIZE) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap()); responseList.add(gson.fromJson(response, Map.class)); bulkRequestBody.setLength(0); } }catch (Exception e) { logger.error("error occured while indexing auto fix document",e); autoFixTransaction.setAdditionalInfo("error occured while indexing auto fix document " + e.getMessage()); } } IndexRequest indexRequest = new IndexRequest( CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_TYPE_NAME_KEY)); indexRequest.source(gson.toJson(autoFixTransaction), XContentType.JSON); bulkRequest.add(indexRequest); } if (bulkRequestBody.length() > 0) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap<>()); } responseList.add(gson.fromJson(response, Map.class)); try { if(null!=client){ BulkResponse bulkResponse = client.bulk(bulkRequest); if (bulkResponse.hasFailures()) { if (!isIndexAvaialble(bulkResponse.getItems())) { logger.info("index not found to write the transaction logs, creating one"); try { CommonUtils .doHttpPut( ESUtils.getEsUrl() + "/" + CommonUtils .getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), ""); publishAutoFixTransactions(autoFixTrans,ruleParam); } catch (Exception e) { logger.error("error creating index", e); } } } } } catch (IOException e) { logger.error("error posting auto fix transaction log", e); return -1; } return 0; }
ElasticSearchDataPublisher { public int publishAutoFixTransactions(List<AutoFixTransaction> autoFixTrans, Map<String, String> ruleParam) { if (autoFixTrans != null && autoFixTrans.size() == 0) { return 0; } BulkRequest bulkRequest = new BulkRequest(); Gson gson = new Gson(); StringBuffer bulkRequestBody = new StringBuffer(); String response = ""; List<Map<String, Map>> responseList = new ArrayList<>(); String esUrl = ESUtils.getEsUrl(); String bulkPostUrl = esUrl + AnnotationPublisher.BULK_WITH_REFRESH_TRUE; final String autoFixType= PacmanSdkConstants.TYPE_FOR_AUTO_FIX_RECORD+"_"+ruleParam.get(PacmanSdkConstants.TARGET_TYPE); for (AutoFixTransaction autoFixTransaction : autoFixTrans) { if(AutoFixAction.AUTOFIX_ACTION_FIX.equals(autoFixTransaction.getAction())) { if(!ESUtils.isValidType(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType)){ try { ESUtils.createMappingWithParent(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType, ruleParam.get(PacmanSdkConstants.TARGET_TYPE)); } catch (Exception e) { logger.error("uanble to create child type"); } } try{ bulkRequestBody.append(String.format(BULK_INDEX_REQUEST_TEMPLATE, getIndexName(ruleParam),getDocId(autoFixTransaction), autoFixType)); bulkRequestBody.append(gson.toJson(autoFixTransaction)); bulkRequestBody.append("\n"); if (bulkRequestBody.toString().getBytes().length / (1024 * 1024) >= PacmanSdkConstants.ES_MAX_BULK_POST_SIZE) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap()); responseList.add(gson.fromJson(response, Map.class)); bulkRequestBody.setLength(0); } }catch (Exception e) { logger.error("error occured while indexing auto fix document",e); autoFixTransaction.setAdditionalInfo("error occured while indexing auto fix document " + e.getMessage()); } } IndexRequest indexRequest = new IndexRequest( CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_TYPE_NAME_KEY)); indexRequest.source(gson.toJson(autoFixTransaction), XContentType.JSON); bulkRequest.add(indexRequest); } if (bulkRequestBody.length() > 0) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap<>()); } responseList.add(gson.fromJson(response, Map.class)); try { if(null!=client){ BulkResponse bulkResponse = client.bulk(bulkRequest); if (bulkResponse.hasFailures()) { if (!isIndexAvaialble(bulkResponse.getItems())) { logger.info("index not found to write the transaction logs, creating one"); try { CommonUtils .doHttpPut( ESUtils.getEsUrl() + "/" + CommonUtils .getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), ""); publishAutoFixTransactions(autoFixTrans,ruleParam); } catch (Exception e) { logger.error("error creating index", e); } } } } } catch (IOException e) { logger.error("error posting auto fix transaction log", e); return -1; } return 0; } }
ElasticSearchDataPublisher { public int publishAutoFixTransactions(List<AutoFixTransaction> autoFixTrans, Map<String, String> ruleParam) { if (autoFixTrans != null && autoFixTrans.size() == 0) { return 0; } BulkRequest bulkRequest = new BulkRequest(); Gson gson = new Gson(); StringBuffer bulkRequestBody = new StringBuffer(); String response = ""; List<Map<String, Map>> responseList = new ArrayList<>(); String esUrl = ESUtils.getEsUrl(); String bulkPostUrl = esUrl + AnnotationPublisher.BULK_WITH_REFRESH_TRUE; final String autoFixType= PacmanSdkConstants.TYPE_FOR_AUTO_FIX_RECORD+"_"+ruleParam.get(PacmanSdkConstants.TARGET_TYPE); for (AutoFixTransaction autoFixTransaction : autoFixTrans) { if(AutoFixAction.AUTOFIX_ACTION_FIX.equals(autoFixTransaction.getAction())) { if(!ESUtils.isValidType(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType)){ try { ESUtils.createMappingWithParent(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType, ruleParam.get(PacmanSdkConstants.TARGET_TYPE)); } catch (Exception e) { logger.error("uanble to create child type"); } } try{ bulkRequestBody.append(String.format(BULK_INDEX_REQUEST_TEMPLATE, getIndexName(ruleParam),getDocId(autoFixTransaction), autoFixType)); bulkRequestBody.append(gson.toJson(autoFixTransaction)); bulkRequestBody.append("\n"); if (bulkRequestBody.toString().getBytes().length / (1024 * 1024) >= PacmanSdkConstants.ES_MAX_BULK_POST_SIZE) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap()); responseList.add(gson.fromJson(response, Map.class)); bulkRequestBody.setLength(0); } }catch (Exception e) { logger.error("error occured while indexing auto fix document",e); autoFixTransaction.setAdditionalInfo("error occured while indexing auto fix document " + e.getMessage()); } } IndexRequest indexRequest = new IndexRequest( CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_TYPE_NAME_KEY)); indexRequest.source(gson.toJson(autoFixTransaction), XContentType.JSON); bulkRequest.add(indexRequest); } if (bulkRequestBody.length() > 0) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap<>()); } responseList.add(gson.fromJson(response, Map.class)); try { if(null!=client){ BulkResponse bulkResponse = client.bulk(bulkRequest); if (bulkResponse.hasFailures()) { if (!isIndexAvaialble(bulkResponse.getItems())) { logger.info("index not found to write the transaction logs, creating one"); try { CommonUtils .doHttpPut( ESUtils.getEsUrl() + "/" + CommonUtils .getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), ""); publishAutoFixTransactions(autoFixTrans,ruleParam); } catch (Exception e) { logger.error("error creating index", e); } } } } } catch (IOException e) { logger.error("error posting auto fix transaction log", e); return -1; } return 0; } ElasticSearchDataPublisher();  ElasticSearchDataPublisher(Boolean testMode); }
ElasticSearchDataPublisher { public int publishAutoFixTransactions(List<AutoFixTransaction> autoFixTrans, Map<String, String> ruleParam) { if (autoFixTrans != null && autoFixTrans.size() == 0) { return 0; } BulkRequest bulkRequest = new BulkRequest(); Gson gson = new Gson(); StringBuffer bulkRequestBody = new StringBuffer(); String response = ""; List<Map<String, Map>> responseList = new ArrayList<>(); String esUrl = ESUtils.getEsUrl(); String bulkPostUrl = esUrl + AnnotationPublisher.BULK_WITH_REFRESH_TRUE; final String autoFixType= PacmanSdkConstants.TYPE_FOR_AUTO_FIX_RECORD+"_"+ruleParam.get(PacmanSdkConstants.TARGET_TYPE); for (AutoFixTransaction autoFixTransaction : autoFixTrans) { if(AutoFixAction.AUTOFIX_ACTION_FIX.equals(autoFixTransaction.getAction())) { if(!ESUtils.isValidType(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType)){ try { ESUtils.createMappingWithParent(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType, ruleParam.get(PacmanSdkConstants.TARGET_TYPE)); } catch (Exception e) { logger.error("uanble to create child type"); } } try{ bulkRequestBody.append(String.format(BULK_INDEX_REQUEST_TEMPLATE, getIndexName(ruleParam),getDocId(autoFixTransaction), autoFixType)); bulkRequestBody.append(gson.toJson(autoFixTransaction)); bulkRequestBody.append("\n"); if (bulkRequestBody.toString().getBytes().length / (1024 * 1024) >= PacmanSdkConstants.ES_MAX_BULK_POST_SIZE) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap()); responseList.add(gson.fromJson(response, Map.class)); bulkRequestBody.setLength(0); } }catch (Exception e) { logger.error("error occured while indexing auto fix document",e); autoFixTransaction.setAdditionalInfo("error occured while indexing auto fix document " + e.getMessage()); } } IndexRequest indexRequest = new IndexRequest( CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_TYPE_NAME_KEY)); indexRequest.source(gson.toJson(autoFixTransaction), XContentType.JSON); bulkRequest.add(indexRequest); } if (bulkRequestBody.length() > 0) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap<>()); } responseList.add(gson.fromJson(response, Map.class)); try { if(null!=client){ BulkResponse bulkResponse = client.bulk(bulkRequest); if (bulkResponse.hasFailures()) { if (!isIndexAvaialble(bulkResponse.getItems())) { logger.info("index not found to write the transaction logs, creating one"); try { CommonUtils .doHttpPut( ESUtils.getEsUrl() + "/" + CommonUtils .getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), ""); publishAutoFixTransactions(autoFixTrans,ruleParam); } catch (Exception e) { logger.error("error creating index", e); } } } } } catch (IOException e) { logger.error("error posting auto fix transaction log", e); return -1; } return 0; } ElasticSearchDataPublisher();  ElasticSearchDataPublisher(Boolean testMode); int publishAutoFixTransactions(List<AutoFixTransaction> autoFixTrans, Map<String, String> ruleParam); static String getIndexName(Map<String, String> ruleParam); void close(); Boolean postDocAsChildOfType(AutoFixPlan plan,String docId,Map<String, String> ruleParam, String planTypeName, String parentType,String parentId ); }
ElasticSearchDataPublisher { public int publishAutoFixTransactions(List<AutoFixTransaction> autoFixTrans, Map<String, String> ruleParam) { if (autoFixTrans != null && autoFixTrans.size() == 0) { return 0; } BulkRequest bulkRequest = new BulkRequest(); Gson gson = new Gson(); StringBuffer bulkRequestBody = new StringBuffer(); String response = ""; List<Map<String, Map>> responseList = new ArrayList<>(); String esUrl = ESUtils.getEsUrl(); String bulkPostUrl = esUrl + AnnotationPublisher.BULK_WITH_REFRESH_TRUE; final String autoFixType= PacmanSdkConstants.TYPE_FOR_AUTO_FIX_RECORD+"_"+ruleParam.get(PacmanSdkConstants.TARGET_TYPE); for (AutoFixTransaction autoFixTransaction : autoFixTrans) { if(AutoFixAction.AUTOFIX_ACTION_FIX.equals(autoFixTransaction.getAction())) { if(!ESUtils.isValidType(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType)){ try { ESUtils.createMappingWithParent(ESUtils.getEsUrl(),getIndexName(ruleParam),autoFixType, ruleParam.get(PacmanSdkConstants.TARGET_TYPE)); } catch (Exception e) { logger.error("uanble to create child type"); } } try{ bulkRequestBody.append(String.format(BULK_INDEX_REQUEST_TEMPLATE, getIndexName(ruleParam),getDocId(autoFixTransaction), autoFixType)); bulkRequestBody.append(gson.toJson(autoFixTransaction)); bulkRequestBody.append("\n"); if (bulkRequestBody.toString().getBytes().length / (1024 * 1024) >= PacmanSdkConstants.ES_MAX_BULK_POST_SIZE) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap()); responseList.add(gson.fromJson(response, Map.class)); bulkRequestBody.setLength(0); } }catch (Exception e) { logger.error("error occured while indexing auto fix document",e); autoFixTransaction.setAdditionalInfo("error occured while indexing auto fix document " + e.getMessage()); } } IndexRequest indexRequest = new IndexRequest( CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), CommonUtils.getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_TYPE_NAME_KEY)); indexRequest.source(gson.toJson(autoFixTransaction), XContentType.JSON); bulkRequest.add(indexRequest); } if (bulkRequestBody.length() > 0) { response = CommonUtils.doHttpPost(bulkPostUrl, bulkRequestBody.toString(),new HashMap<>()); } responseList.add(gson.fromJson(response, Map.class)); try { if(null!=client){ BulkResponse bulkResponse = client.bulk(bulkRequest); if (bulkResponse.hasFailures()) { if (!isIndexAvaialble(bulkResponse.getItems())) { logger.info("index not found to write the transaction logs, creating one"); try { CommonUtils .doHttpPut( ESUtils.getEsUrl() + "/" + CommonUtils .getPropValue(PacmanSdkConstants.AUTO_FIX_TRAN_INDEX_NAME_KEY), ""); publishAutoFixTransactions(autoFixTrans,ruleParam); } catch (Exception e) { logger.error("error creating index", e); } } } } } catch (IOException e) { logger.error("error posting auto fix transaction log", e); return -1; } return 0; } ElasticSearchDataPublisher();  ElasticSearchDataPublisher(Boolean testMode); int publishAutoFixTransactions(List<AutoFixTransaction> autoFixTrans, Map<String, String> ruleParam); static String getIndexName(Map<String, String> ruleParam); void close(); Boolean postDocAsChildOfType(AutoFixPlan plan,String docId,Map<String, String> ruleParam, String planTypeName, String parentType,String parentId );  }