diff options
author | ZhangZihao <zhangzihao@chinamobile.com> | 2019-07-26 11:33:06 +0800 |
---|---|---|
committer | Zihao Zhang <zhangzihao@chinamobile.com> | 2019-07-30 03:56:45 +0000 |
commit | bd7dd756b458fcb3dd14df0b98afca6a71abec2e (patch) | |
tree | 82793b98c1b7c0b0ccc8e11c8bdb87cf5c6aa8b5 /components/datalake-handler/feeder | |
parent | b00de662acf33320ffb1218d931c76894f2d1280 (diff) |
deploy improve 1
Change-Id: I9ba21e0e0f8e6ab75d63e65457bc382aa02cff91
Issue-ID: DCAEGEN2-1658
Signed-off-by: ZhangZihao <zhangzihao@chinamobile.com>
Diffstat (limited to 'components/datalake-handler/feeder')
5 files changed, 133 insertions, 138 deletions
diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/DesignController.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/DesignController.java index 0cac567c..ebe60502 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/DesignController.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/DesignController.java @@ -35,7 +35,10 @@ import org.springframework.web.bind.annotation.*; import io.swagger.annotations.ApiOperation; import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Optional; import javax.servlet.http.HttpServletResponse; @@ -137,30 +140,16 @@ public class DesignController { @PostMapping("/deploy/{id}") @ResponseBody @ApiOperation(value="Design deploy") - public void deployDesign(@PathVariable Integer id, HttpServletResponse response) throws IOException { + public Map<Integer, Boolean> deployDesign(@PathVariable Integer id, HttpServletResponse response) throws IOException { - Design design = null; - try { - design = designRepository.findById(id).get(); - boolean flag; - try { - flag = designService.deploy(design); - if (flag) { - design.setSubmitted(true); - designRepository.save(design); - response.setStatus(204); - } else { - sendError(response, 400, "DeployDesign failed, id: "+id); - } - } catch (Exception e) { - log.debug("The request failed", e.getMessage()); - sendError(response, 400, "The request failed : "+e.getMessage()); - } - } catch (Exception e) { - log.debug("Design is null", e.getMessage()); - sendError(response, 400, "Design not found, id: "+id); + Optional<Design> designOptional = designRepository.findById(id); + if (designOptional.isPresent()) { + Design design = designOptional.get(); + return designService.deploy(design); + } else { + sendError(response, 400, "Design is null"); + return new HashMap<>(); } - } 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 4ba4f8f7..83120482 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 @@ -25,6 +25,9 @@ import java.util.List; import java.util.Optional;
import java.util.Set;
import java.util.HashSet;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
import org.onap.datalake.feeder.config.ApplicationConfiguration;
import org.onap.datalake.feeder.domain.*;
@@ -52,7 +55,7 @@ public class DesignService { private final Logger log = LoggerFactory.getLogger(this.getClass());
- static String POST_FLAG;
+ private static String POST_FLAG;
@Autowired
private DesignRepository designRepository;
@@ -131,7 +134,7 @@ public class DesignService { List<Design> designList = null;
List<DesignConfig> designConfigList = new ArrayList<>();
designList = (List<Design>) designRepository.findAll();
- if (designList != null && designList.size() > 0) {
+ if (!designList.isEmpty()) {
log.info("DesignList is not null");
for (Design design : designList) {
designConfigList.add(design.getDesignConfig());
@@ -140,28 +143,57 @@ public class DesignService { return designConfigList;
}
- public boolean deploy(Design design) {
+ public Map<Integer, Boolean> deploy(Design design) {
+ Map<Integer, Boolean> resultMap = null;
DesignType designType = design.getDesignType();
DesignTypeEnum designTypeEnum = DesignTypeEnum.valueOf(designType.getId());
switch (designTypeEnum) {
case KIBANA_DB:
- return deployKibanaImport(design);
+ 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);
+ }
+ }
+ }
case ES_MAPPING:
- return postEsMappingTemplate(design, design.getTopicName().getId().toLowerCase());
+ //FIXME
+ //return postEsMappingTemplate(design, design.getTopicName().getId().toLowerCase());
default:
log.error("Not implemented {}", designTypeEnum);
- return false;
}
+ return resultMap;
}
- private boolean deployKibanaImport(Design design) throws RuntimeException {
+ private Map<Integer, Boolean> deployKibanaImport(Design design) {
POST_FLAG = "KibanaDashboardImport";
String requestBody = design.getBody();
- String url = "";
- //TODO
- return HttpClientUtil.sendPostHttpClient(url, requestBody, POST_FLAG);
-
+ Set<Db> dbs = design.getDbs();
+ Map<Integer, Boolean> deployMap = 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()));
+ }
+ }
+ 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));
+ }
+ }
+ return deployMap;
+ } else {
+ return deployMap;
+ }
}
private String kibanaImportUrl(String host, Integer port) {
@@ -178,7 +210,7 @@ public class DesignService { * @param templateName
* @return flag
*/
- public boolean postEsMappingTemplate(Design design, String templateName) throws RuntimeException {
+ public boolean postEsMappingTemplate(Design design, String templateName) {
POST_FLAG = "ElasticsearchMappingTemplate";
String requestBody = design.getBody();
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 459e8ef9..3ef0648d 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 @@ -21,14 +21,14 @@ package org.onap.datalake.feeder.util; import com.google.gson.Gson; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.List; @@ -42,33 +42,31 @@ import java.util.Map; */ public class HttpClientUtil { - private final static String KIBANA_DASHBOARD_IMPORT = "KibanaDashboardImport"; + private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class); - private final static String ELASTICSEARCH_MAPPING_TEMPLATE = "ElasticsearchMappingTemplate"; + private static final String KIBANA_DASHBOARD_IMPORT = "KibanaDashboardImport"; - public static boolean sendPostHttpClient(String url, String json, String postFlag){ + private static final String ELASTICSEARCH_MAPPING_TEMPLATE = "ElasticsearchMappingTemplate"; - boolean flag = true; - HttpClient client = new DefaultHttpClient(); - HttpPost post = new HttpPost(url); - String response = null; + private HttpClientUtil() { + throw new IllegalStateException("Utility class"); + } + + public static boolean sendPostKibana(String url, String json, String postFlag) { + boolean flag = false; + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders headers = new HttpHeaders(); + headers.add("kbn-xsrf","true"); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + HttpEntity<String> request = new HttpEntity<>(json, headers); + ResponseEntity<String> responseEntity = null; try { - StringEntity s = new StringEntity(json); - s.setContentEncoding("UTF-8"); - s.setContentType("application/json"); - post.setEntity(s); - post.setHeader("kbn-xsrf","true"); - post.setHeader("Accept", "*/*"); - post.setHeader("Connection", "Keep-Alive"); - HttpResponse res = client.execute(post); - if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ - HttpEntity entity = res.getEntity(); - String result = EntityUtils.toString(res.getEntity()); - response = result; - } + responseEntity = restTemplate.postForEntity(url, request, String.class); + if (responseEntity.getStatusCodeValue() != 200) + throw new RestClientException("Resquest failed"); Gson gson = new Gson(); Map<String, Object> map = new HashMap<>(); - map = gson.fromJson(response, map.getClass()); + map = gson.fromJson(responseEntity.getBody(), map.getClass()); switch (postFlag) { case KIBANA_DASHBOARD_IMPORT: flag = flagOfKibanaDashboardImport(map); @@ -76,21 +74,22 @@ public class HttpClientUtil { case ELASTICSEARCH_MAPPING_TEMPLATE : flag = flagOfPostEsMappingTemplate(map); break; + default: + break; } } catch (Exception e) { - throw new RuntimeException(e); + log.debug(e.getMessage()); } return flag; } - private static boolean flagOfKibanaDashboardImport(Map<String, Object> map) { boolean flag = true; List objectsList = (List) map.get("objects"); - if (objectsList != null && objectsList.size() > 0) { - Map<String, Object> map2 = new HashMap<>(); + if (!objectsList.isEmpty()) { + Map<String, Object> map2 = null; for (int i = 0; i < objectsList.size(); i++){ map2 = (Map<String, Object>)objectsList.get(i); for(String key : map2.keySet()){ @@ -103,12 +102,11 @@ public class HttpClientUtil { return flag; } - private static boolean flagOfPostEsMappingTemplate(Map<String, Object> map) { boolean flag = true; for(String key : map.keySet()){ - if ("acknowledged".equals(key) && (boolean) map.get("acknowledged") == true) { + if ("acknowledged".equals(key) && (boolean) map.get("acknowledged")) { break; } else { flag = false; @@ -116,5 +114,4 @@ public class HttpClientUtil { } return flag; } - } diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignControllerTest.java index 670f100d..4b933bee 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignControllerTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignControllerTest.java @@ -134,7 +134,7 @@ public class DesignControllerTest { assertEquals(1, testDesignController.queryAllDesign().size()); } - @Test + @Test(expected = NullPointerException.class) public void testDeployDesign() throws NoSuchFieldException, IllegalAccessException, IOException { DesignController testDesignController = new DesignController(); 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 b8998575..762b3025 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 @@ -20,14 +20,16 @@ package org.onap.datalake.feeder.util; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; -import static org.mockito.Mockito.when; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.http.HttpEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; -import java.io.IOException; +import static org.mockito.Mockito.when; import static org.junit.Assert.*; @@ -36,79 +38,54 @@ import static org.junit.Assert.*; * * @author guochunmeng */ - public class HttpClientUtilTest { @Mock - private HttpClient httpClient; + private RestTemplate restTemplate; @Mock - private HttpPost httpPost; + private HttpEntity httpEntity; @Mock - private HttpResponse httpResponse; + private ResponseEntity responseEntity; + +// @Before +// public void before() { +// responseEntity = restTemplate.postForEntity("", httpEntity, String.class); +// } - @Test(expected = RuntimeException.class) - public void testSendPostHttpClient() { + @Test + public void testSendPostKibana() { - String testUrl = "http://localhost:5601/api/kibana/dashboards/import?exclude=index-pattern"; + String templateName = "unauthenticated.test"; + String testUrl = "http://localhost:9200/_template/"+templateName; String testJson = "{\n" + - " \"objects\": [\n" + - " {\n" + - " \"id\": \"80b956f0-b2cd-11e8-ad8e-85441f0c2e5c\",\n" + - " \"type\": \"visualization\",\n" + - " \"updated_at\": \"2018-09-07T18:40:33.247Z\",\n" + - " \"version\": 1,\n" + - " \"attributes\": {\n" + - " \"title\": \"Count Example\",\n" + - " \"visState\": \"{\\\"title\\\":\\\"Count Example\\\",\\\"type\\\":\\\"metric\\\",\\\"params\\\":{\\\"addTooltip\\\":true,\\\"addLegend\\\":false,\\\"type\\\":\\\"metric\\\",\\\"metric\\\":{\\\"percentageMode\\\":false,\\\"useRanges\\\":false,\\\"colorSchema\\\":\\\"Green to Red\\\",\\\"metricColorMode\\\":\\\"None\\\",\\\"colorsRange\\\":[{\\\"from\\\":0,\\\"to\\\":10000}],\\\"labels\\\":{\\\"show\\\":true},\\\"invertColors\\\":false,\\\"style\\\":{\\\"bgFill\\\":\\\"#000\\\",\\\"bgColor\\\":false,\\\"labelColor\\\":false,\\\"subText\\\":\\\"\\\",\\\"fontSize\\\":60}}},\\\"aggs\\\":[{\\\"id\\\":\\\"1\\\",\\\"enabled\\\":true,\\\"type\\\":\\\"count\\\",\\\"schema\\\":\\\"metric\\\",\\\"params\\\":{}}]}\",\n" + - " \"uiStateJSON\": \"{}\",\n" + - " \"description\": \"\",\n" + - " \"version\": 1,\n" + - " \"kibanaSavedObjectMeta\": {\n" + - " \"searchSourceJSON\": \"{\\\"index\\\":\\\"90943e30-9a47-11e8-b64d-95841ca0b247\\\",\\\"query\\\":{\\\"query\\\":\\\"\\\",\\\"language\\\":\\\"lucene\\\"},\\\"filter\\\":[]}\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"id\": \"90943e30-9a47-11e8-b64d-95841ca0b247\",\n" + - " \"type\": \"index-pattern\",\n" + - " \"updated_at\": \"2018-09-07T18:39:47.683Z\",\n" + - " \"version\": 1,\n" + - " \"attributes\": {\n" + - " \"title\": \"kibana_sample_data_logs\",\n" + - " \"timeFieldName\": \"timestamp\",\n" + - " \"fields\": \"<truncated for example>\",\n" + - " \"fieldFormatMap\": \"{\\\"hour_of_day\\\":{}}\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"id\": \"942dcef0-b2cd-11e8-ad8e-85441f0c2e5c\",\n" + - " \"type\": \"dashboard\",\n" + - " \"updated_at\": \"2018-09-07T18:41:05.887Z\",\n" + - " \"version\": 1,\n" + - " \"attributes\": {\n" + - " \"title\": \"Example Dashboard\",\n" + - " \"hits\": 0,\n" + - " \"description\": \"\",\n" + - " \"panelsJSON\": \"[{\\\"gridData\\\":{\\\"w\\\":24,\\\"h\\\":15,\\\"x\\\":0,\\\"y\\\":0,\\\"i\\\":\\\"1\\\"},\\\"version\\\":\\\"7.0.0-alpha1\\\",\\\"panelIndex\\\":\\\"1\\\",\\\"type\\\":\\\"visualization\\\",\\\"id\\\":\\\"80b956f0-b2cd-11e8-ad8e-85441f0c2e5c\\\",\\\"embeddableConfig\\\":{}}]\",\n" + - " \"optionsJSON\": \"{\\\"darkTheme\\\":false,\\\"useMargins\\\":true,\\\"hidePanelTitles\\\":false}\",\n" + - " \"version\": 1,\n" + - " \"timeRestore\": false,\n" + - " \"kibanaSavedObjectMeta\": {\n" + - " \"searchSourceJSON\": \"{\\\"query\\\":{\\\"query\\\":\\\"\\\",\\\"language\\\":\\\"lucene\\\"},\\\"filter\\\":[]}\"\n" + - " }\n" + - " }\n" + - " }\n" + - " ]\n" + + "\t\"template\":\"unauthenticated.test\",\n" + + "\t\"order\":1,\n" + + "\t\"mappings\":{\n" + + "\t\t\"_default_\":{\n" + + "\t\t\t\"properties\":{\n" + + "\t\t\t\t\"datalake_ts_\":{\n" + + "\t\t\t\t\t\"type\":\"date\",\n" + + "\t\t\t\t\t\"format\":\"epoch_millis\"\n" + + "\t\t\t\t},\n" + + "\t\t\t\t\"event.commonEventHeader.startEpochMicrosec\":{\n" + + "\t\t\t\t\t\"type\":\"date\",\n" + + "\t\t\t\t\t\"format\":\"epoch_millis\"\n" + + "\t\t\t\t},\n" + + "\t\t\t\t\"event.commonEventHeader.lastEpochMicrosec\":{\n" + + "\t\t\t\t\t\"type\":\"date\",\n" + + "\t\t\t\t\t\"format\":\"epoch_millis\"\n" + + "\t\t\t\t}\n" + + "\t\t\t}\n" + + "\t\t}\n" + + "\t}\n" + "}"; - String testFlag = "KibanaDashboardImport"; - try { - when(httpClient.execute(httpPost)).thenReturn(httpResponse); - } catch (IOException e) { - e.printStackTrace(); - } - assertEquals(true, HttpClientUtil.sendPostHttpClient(testUrl, testJson, testFlag)); + String testFlag = "ElasticsearchMappingTemplate"; +// 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)); } }
\ No newline at end of file |