diff options
author | ZhangZihao <zhangzihao@chinamobile.com> | 2019-06-24 16:14:49 +0800 |
---|---|---|
committer | Zihao Zhang <zhangzihao@chinamobile.com> | 2019-06-24 08:47:13 +0000 |
commit | 0add5561508b4a0796b4237142a6e1265da2055b (patch) | |
tree | b6f3cb77b3c65087d801ab89e9adfae811a32883 | |
parent | 9594bc115e27c6589799c885009fc4ff22fa65f5 (diff) |
portaldesign and httpclientutil
Change-Id: Ib31248a8eee2fe1511472c127abb52a0875c5b21
Issue-ID: DCAEGEN2-1625
Signed-off-by: ZhangZihao <zhangzihao@chinamobile.com>
6 files changed, 150 insertions, 59 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 3e67f38a..e371af1b 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 @@ -80,5 +80,6 @@ public class ApplicationConfiguration { private String datalakeVersion; //Kibana - private String KibanaDashboardImportApi; + private String kibanaDashboardImportApi; + private Integer kibanaPort; } diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/PortalDesignController.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/PortalDesignController.java index 7b717268..989a9f45 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/PortalDesignController.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/PortalDesignController.java @@ -35,7 +35,6 @@ import org.springframework.web.bind.annotation.*; import io.swagger.annotations.ApiOperation; import java.io.IOException; -import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -46,7 +45,6 @@ import javax.servlet.http.HttpServletResponse; * * @author guochunmeng */ -@CrossOrigin(origins = "*") @RestController @RequestMapping(value = "/portalDesigns", produces = MediaType.APPLICATION_JSON_VALUE) public class PortalDesignController { @@ -132,17 +130,7 @@ public class PortalDesignController { @ResponseBody @ApiOperation(value="List all PortalDesigns") public List<PortalDesignConfig> queryAllPortalDesign(){ - - List<PortalDesign> portalDesignList = null; - List<PortalDesignConfig> portalDesignConfigList = new ArrayList<>(); - portalDesignList = (List<PortalDesign>) portalDesignRepository.findAll(); - if (portalDesignList != null && portalDesignList.size() > 0) { - log.info("PortalDesignList is not null"); - for (PortalDesign portalDesign : portalDesignList) { - portalDesignConfigList.add(portalDesign.getPortalDesignConfig()); - } - } - return portalDesignConfigList; + return portalDesignService.queryAllPortalDesign(); } @@ -154,21 +142,20 @@ public class PortalDesignController { PortalDesign portalDesign = null; try { portalDesign = portalDesignRepository.findById(id).get(); - if (portalDesign.getDesignType() != null && portalDesign.getDesignType().getName().startsWith("Kibana")) { - boolean flag = portalDesignService.deployKibanaImport(portalDesign); + boolean flag; + try { + flag = portalDesignService.deploy(portalDesign); if (flag) { + portalDesign.setSubmitted(true); + portalDesignRepository.save(portalDesign); + response.setStatus(204); + } else { sendError(response, 400, "DeployPortalDesign failed, id: "+id); } - } else if (portalDesign.getDesignType() != null && portalDesign.getDesignType().getName().startsWith("Elasticsearch")) { - //TODO Elasticsearch template import - sendError(response, 400, "DeployPortalDesign failed, id: "+id); - } else { - //TODO Druid import - sendError(response, 400, "DeployPortalDesign failed, id: "+id); + } catch (Exception e) { + log.debug("The request failed", e.getMessage()); + sendError(response, 400, "The request failed : "+e.getMessage()); } - portalDesign.setSubmitted(true); - portalDesignRepository.save(portalDesign); - response.setStatus(204); } catch (Exception e) { log.debug("PortalDesign is null", e.getMessage()); sendError(response, 400, "PortalDesign not found, id: "+id); diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PortalDesignService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PortalDesignService.java index 3f1aabd2..33093ee0 100755 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PortalDesignService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PortalDesignService.java @@ -20,12 +20,10 @@ package org.onap.datalake.feeder.service;
-import java.util.HashMap;
+import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
-import com.google.gson.Gson;
import org.onap.datalake.feeder.config.ApplicationConfiguration;
import org.onap.datalake.feeder.domain.DesignType;
import org.onap.datalake.feeder.domain.Portal;
@@ -35,6 +33,8 @@ import org.onap.datalake.feeder.dto.PortalDesignConfig; import org.onap.datalake.feeder.repository.DesignTypeRepository;
import org.onap.datalake.feeder.repository.PortalDesignRepository;
import org.onap.datalake.feeder.util.HttpClientUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -46,7 +46,11 @@ import org.springframework.stereotype.Service; @Service
public class PortalDesignService {
-
+
+ private final Logger log = LoggerFactory.getLogger(this.getClass());
+
+ static String POST_FLAG;
+
@Autowired
private PortalDesignRepository portalDesignRepository;
@@ -59,6 +63,9 @@ public class PortalDesignService { @Autowired
private ApplicationConfiguration applicationConfiguration;
+ @Autowired
+ private DbService dbService;
+
public PortalDesign fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig) throws Exception
{
PortalDesign portalDesign = new PortalDesign();
@@ -73,13 +80,9 @@ public class PortalDesignService { private void fillPortalDesign(PortalDesignConfig portalDesignConfig, PortalDesign portalDesign) throws IllegalArgumentException {
portalDesign.setId(portalDesignConfig.getId());
-
portalDesign.setBody(portalDesignConfig.getBody());
-
portalDesign.setName(portalDesignConfig.getName());
-
portalDesign.setNote(portalDesignConfig.getNote());
-
portalDesign.setSubmitted(portalDesignConfig.getSubmitted());
if (portalDesignConfig.getTopic() != null) {
@@ -108,13 +111,46 @@ public class PortalDesignService { }
- private String kibanaImportUrl(String host, Integer port){
- return "http://"+host+":"+port+applicationConfiguration.getKibanaDashboardImportApi();
+ public List<PortalDesignConfig> queryAllPortalDesign(){
+
+ List<PortalDesign> portalDesignList = null;
+ List<PortalDesignConfig> portalDesignConfigList = new ArrayList<>();
+ portalDesignList = (List<PortalDesign>) portalDesignRepository.findAll();
+ if (portalDesignList != null && portalDesignList.size() > 0) {
+ log.info("PortalDesignList is not null");
+ for (PortalDesign portalDesign : portalDesignList) {
+ portalDesignConfigList.add(portalDesign.getPortalDesignConfig());
+ }
+ }
+ return portalDesignConfigList;
+ }
+
+
+ public boolean deploy(PortalDesign portalDesign){
+ boolean flag =true;
+ String designTypeName = portalDesign.getDesignType().getName();
+ if (portalDesign.getDesignType() != null && "kibana_db".equals(designTypeName)) {
+ flag = deployKibanaImport(portalDesign);
+ } else if (portalDesign.getDesignType() != null && "kibana_visual".equals(designTypeName)) {
+ //TODO
+ flag =false;
+ } else if (portalDesign.getDesignType() != null && "kibana_search".equals(designTypeName)) {
+ //TODO
+ flag = false;
+ } else if (portalDesign.getDesignType() != null && "es_mapping".equals(designTypeName)) {
+ flag = postEsMappingTemplate(portalDesign, portalDesign.getTopic().getName().toLowerCase());
+ } else if (portalDesign.getDesignType() != null && "druid_kafka_spec".equals(designTypeName)) {
+ //TODO
+ flag =false;
+ } else {
+ flag =false;
+ }
+ return flag;
}
- public boolean deployKibanaImport(PortalDesign portalDesign) {
- boolean flag = false;
+ private boolean deployKibanaImport(PortalDesign portalDesign) throws RuntimeException {
+ POST_FLAG = "KibanaDashboardImport";
String requestBody = portalDesign.getBody();
Portal portal = portalDesign.getDesignType().getPortal();
String portalHost = portal.getHost();
@@ -128,26 +164,32 @@ public class PortalDesignService { } else {
url = kibanaImportUrl(portalHost, portalPort);
}
+ return HttpClientUtil.sendPostHttpClient(url, requestBody, POST_FLAG);
- //Send httpclient to kibana
- String kibanaResponse = HttpClientUtil.sendPostToKibana(url, requestBody);
- Gson gson = new Gson();
- Map<String, Object> map = new HashMap<>();
- map = gson.fromJson(kibanaResponse, map.getClass());
- List objectsList = (List) map.get("objects");
-
- if (objectsList != null && objectsList.size() > 0) {
- Map<String, Object> map2 = new HashMap<>();
- for (int i = 0; i < objectsList.size(); i++){
- map2 = (Map<String, Object>)objectsList.get(i);
- for(String key : map2.keySet()){
- if ("error".equals(key)) {
- return true;
- }
- }
- }
+ }
+
+
+ private String kibanaImportUrl(String host, Integer port){
+ if (port == null) {
+ port = applicationConfiguration.getKibanaPort();
}
- return flag;
+ return "http://"+host+":"+port+applicationConfiguration.getKibanaDashboardImportApi();
+ }
+
+
+ /**
+ * successed resp:
+ * {
+ * "acknowledged": true
+ * }
+ * @param portalDesign
+ * @param templateName
+ * @return flag
+ */
+ public boolean postEsMappingTemplate(PortalDesign portalDesign, String templateName) throws RuntimeException {
+ POST_FLAG = "ElasticsearchMappingTemplate";
+ String requestBody = portalDesign.getBody();
+ return HttpClientUtil.sendPostHttpClient("http://"+dbService.getElasticsearch().getHost()+":9200/_template/"+templateName, requestBody, POST_FLAG);
}
}
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 b02139f3..459e8ef9 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 @@ -20,6 +20,7 @@ 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; @@ -29,6 +30,10 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * HttpClient * @@ -37,7 +42,13 @@ import org.apache.http.util.EntityUtils; */ public class HttpClientUtil { - public static String sendPostToKibana(String url, String json){ + private final static String KIBANA_DASHBOARD_IMPORT = "KibanaDashboardImport"; + + private final static String ELASTICSEARCH_MAPPING_TEMPLATE = "ElasticsearchMappingTemplate"; + + public static boolean sendPostHttpClient(String url, String json, String postFlag){ + + boolean flag = true; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); String response = null; @@ -55,10 +66,55 @@ public class HttpClientUtil { String result = EntityUtils.toString(res.getEntity()); response = result; } + Gson gson = new Gson(); + Map<String, Object> map = new HashMap<>(); + map = gson.fromJson(response, map.getClass()); + switch (postFlag) { + case KIBANA_DASHBOARD_IMPORT: + flag = flagOfKibanaDashboardImport(map); + break; + case ELASTICSEARCH_MAPPING_TEMPLATE : + flag = flagOfPostEsMappingTemplate(map); + break; + } } catch (Exception e) { throw new RuntimeException(e); } - return response; + 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<>(); + for (int i = 0; i < objectsList.size(); i++){ + map2 = (Map<String, Object>)objectsList.get(i); + for(String key : map2.keySet()){ + if ("error".equals(key)) { + return false; + } + } + } + } + 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) { + break; + } else { + flag = false; + } + } + 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 60fcb1a2..f94dae1f 100644 --- a/components/datalake-handler/feeder/src/main/resources/application.properties +++ b/components/datalake-handler/feeder/src/main/resources/application.properties @@ -61,5 +61,8 @@ logging.level.org.onap.datalake=DEBUG datalakeVersion=0.0.1 #####################KibanaDashboardImportApi -KibanaDashboardImportApi=/api/kibana/dashboards/import?exclude=index-pattern +kibanaDashboardImportApi=/api/kibana/dashboards/import?exclude=index-pattern + +#####################KibanaPort +kibanaPort=5601 diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalDesignControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalDesignControllerTest.java index ac9a17a8..3bd0449f 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalDesignControllerTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalDesignControllerTest.java @@ -53,6 +53,8 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PortalDesignControllerTest { + + //static String Kibana_Dashboard_Import_Api = "/api/kibana/dashboards/import?exclude=index-pattern"; @Mock private HttpServletResponse httpServletResponse; @@ -140,8 +142,8 @@ public class PortalDesignControllerTest { PortalDesign testPortalDesign = fillDomain(); Integer id = 1; testPortalDesign.setId(1); + //when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn(Kibana_Dashboard_Import_Api); when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign))); - when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn("/api/kibana/dashboards/import?exclude=index-pattern"); testPortalDesignController.deployPortalDesign(id, httpServletResponse); } |