portaldesign and httpclientutil

Change-Id: Ib31248a8eee2fe1511472c127abb52a0875c5b21
Issue-ID: DCAEGEN2-1625
Signed-off-by: ZhangZihao <zhangzihao@chinamobile.com>
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 3e67f38..e371af1 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 @@
 	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 7b71726..989a9f4 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 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 @@
  *
  * @author guochunmeng
  */
-@CrossOrigin(origins = "*")
 @RestController
 @RequestMapping(value = "/portalDesigns", produces = MediaType.APPLICATION_JSON_VALUE)
 public class PortalDesignController {
@@ -132,17 +130,7 @@
 	@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 @@
 		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 3f1aabd..33093ee 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.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 @@
 

 @Service

 public class PortalDesignService {

-	

+

+	private final Logger log = LoggerFactory.getLogger(this.getClass());

+

+	static String POST_FLAG;

+

     @Autowired

     private PortalDesignRepository portalDesignRepository;

 

@@ -59,6 +63,9 @@
 	@Autowired

 	private ApplicationConfiguration applicationConfiguration;

 

+	@Autowired

+	private DbService dbService;

+

 	public PortalDesign fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig) throws Exception

 	{

 		PortalDesign portalDesign = new PortalDesign();

@@ -73,13 +80,9 @@
 	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 @@
 	}

 

 

-	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 deployKibanaImport(PortalDesign portalDesign) {

-		boolean flag = false;

+	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;

+	}

+

+

+	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 @@
 		} 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 b02139f..459e8ef 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.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 @@
  */
 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 @@
                 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 60fcb1a..f94dae1 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 @@
 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 ac9a17a..3bd0449 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 @@
 
 @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 @@
         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);
     }