diff options
author | ZhangZihao <zhangzihao@chinamobile.com> | 2019-08-01 14:54:17 +0800 |
---|---|---|
committer | ZhangZihao <zhangzihao@chinamobile.com> | 2019-08-01 14:54:36 +0800 |
commit | fc1a715457bf90f38afd018e3d44baac4ecb004d (patch) | |
tree | 8870d241c4593cf8af4d991c3505a3b1bb90131a /components/datalake-handler/feeder | |
parent | f093423fccc6c2059f1c94f3fe226202b8fcec12 (diff) |
design modify 2
Change-Id: I7c75460b30184bf2803f8a936a4252867300e898
Issue-ID: DCAEGEN2-1658
Signed-off-by: ZhangZihao <zhangzihao@chinamobile.com>
Diffstat (limited to 'components/datalake-handler/feeder')
5 files changed, 105 insertions, 43 deletions
diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/config/ApplicationConfiguration.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/config/ApplicationConfiguration.java index 05d6e887..b93924c4 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/config/ApplicationConfiguration.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/config/ApplicationConfiguration.java @@ -69,4 +69,8 @@ public class ApplicationConfiguration { //Kibana private String kibanaDashboardImportApi; private Integer kibanaPort; + + //Elasticsearch + private String esTemplateMappingApi; + private Integer esPort; } diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DesignService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DesignService.java index 83120482..d0940baa 100755 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DesignService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DesignService.java @@ -57,6 +57,8 @@ public class DesignService { private static String POST_FLAG;
+ private static String URL_FlAG;
+
@Autowired
private DesignRepository designRepository;
@@ -150,57 +152,44 @@ public class DesignService { switch (designTypeEnum) {
case KIBANA_DB:
- resultMap = deployKibanaImport(design);
- if (!resultMap.isEmpty()) {
- Iterator<Map.Entry<Integer, Boolean>> it = resultMap.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<Integer, Boolean> entry = it.next();
- if (entry.getValue()) {
- design.setSubmitted(true);
- designRepository.save(design);
- }
- }
- }
+ log.info("Deploy kibana dashboard");
+ resultMap = deployKibanaDashboardImport(design);
+ deploySave(resultMap, design);
+ break;
case ES_MAPPING:
- //FIXME
- //return postEsMappingTemplate(design, design.getTopicName().getId().toLowerCase());
+ log.info("Deploy elasticsearch mapping template");
+ resultMap = postEsMappingTemplate(design, design.getTopicName().getId().toLowerCase());
+ deploySave(resultMap, design);
+ break;
default:
log.error("Not implemented {}", designTypeEnum);
+ break;
}
+ log.info("Response resultMap: " + resultMap);
return resultMap;
}
- private Map<Integer, Boolean> deployKibanaImport(Design design) {
+ private Map<Integer, Boolean> deployKibanaDashboardImport(Design design) {
+ URL_FlAG = "Kibana";
POST_FLAG = "KibanaDashboardImport";
String requestBody = design.getBody();
Set<Db> dbs = design.getDbs();
- Map<Integer, Boolean> deployMap = new HashMap<>();
+ Map<Integer, Boolean> deployKibanaMap = new HashMap<>();
if (!dbs.isEmpty()) {
- Map<Integer, String> map = new HashMap<>();
- for (Db item : dbs) {
- if (item.isEnabled()) {
- map.put(item.getId(), kibanaImportUrl(item.getHost(), item.getPort()));
- }
- }
+ Map<Integer, String> map = urlMap(dbs, URL_FlAG);
+ log.info("Deploy kibana dashboard url map: " + map);
if (!map.isEmpty()) {
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
- deployMap.put(entry.getKey(), HttpClientUtil.sendPostKibana(entry.getValue(), requestBody, POST_FLAG));
+ deployKibanaMap.put(entry.getKey(), HttpClientUtil.sendHttpClientPost(entry.getValue(), requestBody, POST_FLAG, URL_FlAG));
}
}
- return deployMap;
+ return deployKibanaMap;
} else {
- return deployMap;
- }
- }
-
- private String kibanaImportUrl(String host, Integer port) {
- if (port == null) {
- port = applicationConfiguration.getKibanaPort();
+ return deployKibanaMap;
}
- return "http://" + host + ":" + port + applicationConfiguration.getKibanaDashboardImportApi();
}
/**
@@ -210,16 +199,74 @@ public class DesignService { * @param templateName
* @return flag
*/
- public boolean postEsMappingTemplate(Design design, String templateName) {
+ public Map<Integer, Boolean> postEsMappingTemplate(Design design, String templateName) {
+ URL_FlAG = "Elasticsearch";
POST_FLAG = "ElasticsearchMappingTemplate";
String requestBody = design.getBody();
-
- //FIXME
Set<Db> dbs = design.getDbs();
- //submit to each ES in dbs
+ Map<Integer, Boolean> deployEsMap = new HashMap<>();
- //return HttpClientUtil.sendPostHttpClient("http://"+dbService.getElasticsearch().getHost()+":9200/_template/"+templateName, requestBody, POST_FLAG);
- return false;
+ if (!dbs.isEmpty()) {
+ Map<Integer, String> map = urlMap(dbs, URL_FlAG);
+ log.info("Deploy elasticsearch url map: " + map);
+ if (!map.isEmpty()) {
+ Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<Integer, String> entry = it.next();
+ deployEsMap.put(entry.getKey(), HttpClientUtil.sendHttpClientPost(entry.getValue()+templateName, requestBody, POST_FLAG, URL_FlAG));
+ }
+ }
+ return deployEsMap;
+ } else {
+ return deployEsMap;
+ }
+ }
+
+ private Map<Integer, String> urlMap (Set<Db> dbs, String flag) {
+ Map<Integer, String> map = new HashMap<>();
+ for (Db item : dbs) {
+ if (item.isEnabled()) {
+ map.put(item.getId(), httpRequestUrl(item.getHost(), item.getPort(), flag));
+ }
+ }
+ return map;
+ }
+
+ private String httpRequestUrl(String host, Integer port, String urlFlag) {
+ String url = "";
+ switch (urlFlag) {
+ case "Kibana":
+ if (port == null) {
+ port = applicationConfiguration.getKibanaPort();
+ }
+ url = "http://" + host + ":" + port + applicationConfiguration.getKibanaDashboardImportApi();
+ log.info("Kibana url: " + url);
+ break;
+ case "Elasticsearch":
+ if (port == null) {
+ port = applicationConfiguration.getEsPort();
+ }
+ url = "http://" + host + ":" + port + applicationConfiguration.getEsTemplateMappingApi();
+ log.info("Elasticsearch url: " + url);
+ break;
+ default:
+ break;
+ }
+ return url;
+ }
+
+ private void deploySave(Map<Integer, Boolean> map, Design design) {
+ if (!map.isEmpty()) {
+ Iterator<Map.Entry<Integer, Boolean>> it = map.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<Integer, Boolean> entry = it.next();
+ if (entry.getValue()) {
+ design.setSubmitted(true);
+ designRepository.save(design);
+ log.info("Status was modified");
+ }
+ }
+ }
}
}
diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/HttpClientUtil.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/HttpClientUtil.java index 3ef0648d..64b643ac 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/HttpClientUtil.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/HttpClientUtil.java @@ -44,6 +44,8 @@ public class HttpClientUtil { private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class); + private static final String KIBANA = "Kibana"; + private static final String KIBANA_DASHBOARD_IMPORT = "KibanaDashboardImport"; private static final String ELASTICSEARCH_MAPPING_TEMPLATE = "ElasticsearchMappingTemplate"; @@ -52,11 +54,14 @@ public class HttpClientUtil { throw new IllegalStateException("Utility class"); } - public static boolean sendPostKibana(String url, String json, String postFlag) { + public static boolean sendHttpClientPost(String url, String json, String postFlag, String urlFlag) { boolean flag = false; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); - headers.add("kbn-xsrf","true"); + if (urlFlag.equals(KIBANA)) { + log.info("urlFlag is Kibana, add header"); + headers.add("kbn-xsrf","true"); + } headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<String> request = new HttpEntity<>(json, headers); ResponseEntity<String> responseEntity = null; @@ -78,7 +83,7 @@ public class HttpClientUtil { break; } } catch (Exception e) { - log.debug(e.getMessage()); + log.debug("Resquest failed: " + e.getMessage()); } return flag; } diff --git a/components/datalake-handler/feeder/src/main/resources/application.properties b/components/datalake-handler/feeder/src/main/resources/application.properties index aadd3e8d..72fc1ccd 100644 --- a/components/datalake-handler/feeder/src/main/resources/application.properties +++ b/components/datalake-handler/feeder/src/main/resources/application.properties @@ -65,4 +65,9 @@ kibanaDashboardImportApi=/api/kibana/dashboards/import?exclude=index-pattern #####################KibanaPort kibanaPort=5601 - + +#####################Elasticsearch Template API +esTemplateMappingApi=/_template/ + +#####################Elasticsearch port +esPort=9200
\ No newline at end of file diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/util/HttpClientUtilTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/util/HttpClientUtilTest.java index 762b3025..c73b8ea9 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/util/HttpClientUtilTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/util/HttpClientUtilTest.java @@ -55,7 +55,7 @@ public class HttpClientUtilTest { // } @Test - public void testSendPostKibana() { + public void testSendHttpClientPost() { String templateName = "unauthenticated.test"; String testUrl = "http://localhost:9200/_template/"+templateName; @@ -82,10 +82,11 @@ public class HttpClientUtilTest { "\t}\n" + "}"; String testFlag = "ElasticsearchMappingTemplate"; + String testUrlFlag = "Elasticsearch"; // when(restTemplate.postForEntity(testUrl, httpEntity, String.class)).thenReturn(responseEntity); // when(responseEntity.getStatusCodeValue()).thenReturn(200); // when(responseEntity.getBody()).thenReturn("{ \"acknowledged\": true }"); - assertEquals(false, HttpClientUtil.sendPostKibana(testUrl, testJson, testFlag)); + assertEquals(false, HttpClientUtil.sendHttpClientPost(testUrl, testJson, testFlag, testUrlFlag)); } }
\ No newline at end of file |