supports multiple Kafka clusters and DBs
[dcaegen2/services.git] / components / datalake-handler / feeder / src / main / java / org / onap / datalake / feeder / service / PortalDesignService.java
index 3f1aabd..408e497 100755 (executable)
 \r
 package org.onap.datalake.feeder.service;\r
 \r
-import java.util.HashMap;\r
+import java.util.ArrayList;\r
 import java.util.List;\r
-import java.util.Map;\r
 import java.util.Optional;\r
+import java.util.Set;\r
 \r
-import com.google.gson.Gson;\r
 import org.onap.datalake.feeder.config.ApplicationConfiguration;\r
+import org.onap.datalake.feeder.domain.Db;\r
+import org.onap.datalake.feeder.domain.DbType;\r
 import org.onap.datalake.feeder.domain.DesignType;\r
 import org.onap.datalake.feeder.domain.Portal;\r
 import org.onap.datalake.feeder.domain.PortalDesign;\r
 import org.onap.datalake.feeder.domain.Topic;\r
+import org.onap.datalake.feeder.domain.TopicName;\r
 import org.onap.datalake.feeder.dto.PortalDesignConfig;\r
+import org.onap.datalake.feeder.enumeration.DbTypeEnum;\r
+import org.onap.datalake.feeder.enumeration.DesignTypeEnum;\r
 import org.onap.datalake.feeder.repository.DesignTypeRepository;\r
 import org.onap.datalake.feeder.repository.PortalDesignRepository;\r
+import org.onap.datalake.feeder.repository.TopicNameRepository;\r
+import org.onap.datalake.feeder.service.db.CouchbaseService;\r
+import org.onap.datalake.feeder.service.db.DbStoreService;\r
+import org.onap.datalake.feeder.service.db.ElasticsearchService;\r
+import org.onap.datalake.feeder.service.db.HdfsService;\r
+import org.onap.datalake.feeder.service.db.MongodbService;\r
 import org.onap.datalake.feeder.util.HttpClientUtil;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
 import org.springframework.beans.factory.annotation.Autowired;\r
 import org.springframework.stereotype.Service;\r
 \r
@@ -46,12 +58,16 @@ import org.springframework.stereotype.Service;
 \r
 @Service\r
 public class PortalDesignService {\r
-       \r
-    @Autowired\r
-    private PortalDesignRepository portalDesignRepository;\r
 \r
-    @Autowired\r
-       private TopicService topicService;\r
+       private final Logger log = LoggerFactory.getLogger(this.getClass());\r
+\r
+       static String POST_FLAG;\r
+\r
+       @Autowired\r
+       private PortalDesignRepository portalDesignRepository;\r
+\r
+       @Autowired\r
+       private TopicNameRepository topicNameRepository;\r
 \r
        @Autowired\r
        private DesignTypeRepository designTypeRepository;\r
@@ -59,62 +75,83 @@ public class PortalDesignService {
        @Autowired\r
        private ApplicationConfiguration applicationConfiguration;\r
 \r
-       public PortalDesign fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig) throws Exception\r
-       {\r
+       public PortalDesign fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig) throws Exception {\r
                PortalDesign portalDesign = new PortalDesign();\r
                fillPortalDesign(portalDesignConfig, portalDesign);\r
                return portalDesign;\r
        }\r
-       public void fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig, PortalDesign portalDesign) throws Exception\r
-       {\r
+\r
+       public void fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig, PortalDesign portalDesign) throws Exception {\r
                fillPortalDesign(portalDesignConfig, portalDesign);\r
        }\r
 \r
        private void fillPortalDesign(PortalDesignConfig portalDesignConfig, PortalDesign portalDesign) throws IllegalArgumentException {\r
 \r
                portalDesign.setId(portalDesignConfig.getId());\r
-\r
                portalDesign.setBody(portalDesignConfig.getBody());\r
-\r
                portalDesign.setName(portalDesignConfig.getName());\r
-\r
                portalDesign.setNote(portalDesignConfig.getNote());\r
-\r
                portalDesign.setSubmitted(portalDesignConfig.getSubmitted());\r
 \r
                if (portalDesignConfig.getTopic() != null) {\r
-                       Topic topic = topicService.getTopic(portalDesignConfig.getTopic());\r
-                       if (topic == null) throw new IllegalArgumentException("topic is null");\r
-                       portalDesign.setTopic(topic);\r
-               }else {\r
-                       throw new IllegalArgumentException("Can not find topic in DB, topic name: "+portalDesignConfig.getTopic());\r
+                       Optional<TopicName> topicName = topicNameRepository.findById(portalDesignConfig.getTopic());\r
+                       if (topicName.isPresent()) {\r
+                               portalDesign.setTopicName(topicName.get());\r
+                       } else {\r
+                               throw new IllegalArgumentException("topic is null " + portalDesignConfig.getTopic());\r
+                       }\r
+               } else {\r
+                       throw new IllegalArgumentException("Can not find topic in DB, topic name: " + portalDesignConfig.getTopic());\r
                }\r
 \r
                if (portalDesignConfig.getDesignType() != null) {\r
                        DesignType designType = designTypeRepository.findById(portalDesignConfig.getDesignType()).get();\r
-                       if (designType == null) throw new IllegalArgumentException("designType is null");\r
+                       if (designType == null)\r
+                               throw new IllegalArgumentException("designType is null");\r
                        portalDesign.setDesignType(designType);\r
-               }else {\r
-                       throw new IllegalArgumentException("Can not find designType in Design_type, designType name "+portalDesignConfig.getDesignType());\r
+               } else {\r
+                       throw new IllegalArgumentException("Can not find designType in Design_type, designType name " + portalDesignConfig.getDesignType());\r
                }\r
 \r
        }\r
 \r
-       \r
        public PortalDesign getPortalDesign(Integer id) {\r
-               \r
+\r
                Optional<PortalDesign> ret = portalDesignRepository.findById(id);\r
                return ret.isPresent() ? ret.get() : null;\r
        }\r
 \r
+       public List<PortalDesignConfig> queryAllPortalDesign() {\r
 \r
-       private String kibanaImportUrl(String host, Integer port){\r
-               return "http://"+host+":"+port+applicationConfiguration.getKibanaDashboardImportApi();\r
+               List<PortalDesign> portalDesignList = null;\r
+               List<PortalDesignConfig> portalDesignConfigList = new ArrayList<>();\r
+               portalDesignList = (List<PortalDesign>) portalDesignRepository.findAll();\r
+               if (portalDesignList != null && portalDesignList.size() > 0) {\r
+                       log.info("PortalDesignList is not null");\r
+                       for (PortalDesign portalDesign : portalDesignList) {\r
+                               portalDesignConfigList.add(portalDesign.getPortalDesignConfig());\r
+                       }\r
+               }\r
+               return portalDesignConfigList;\r
        }\r
 \r
+       public boolean deploy(PortalDesign portalDesign) {\r
+               DesignType designType = portalDesign.getDesignType();\r
+               DesignTypeEnum designTypeEnum = DesignTypeEnum.valueOf(designType.getId());\r
+\r
+               switch (designTypeEnum) {\r
+               case KIBANA_DB:\r
+                       return deployKibanaImport(portalDesign);\r
+               case ES_MAPPING:\r
+                       return postEsMappingTemplate(portalDesign, portalDesign.getTopicName().getId().toLowerCase());\r
+               default:\r
+                       log.error("Not implemented {}", designTypeEnum);\r
+                       return false;\r
+               }\r
+       }\r
 \r
-       public boolean deployKibanaImport(PortalDesign portalDesign) {\r
-               boolean flag = false;\r
+       private boolean deployKibanaImport(PortalDesign portalDesign) throws RuntimeException {\r
+               POST_FLAG = "KibanaDashboardImport";\r
                String requestBody = portalDesign.getBody();\r
                Portal portal = portalDesign.getDesignType().getPortal();\r
                String portalHost = portal.getHost();\r
@@ -128,26 +165,34 @@ public class PortalDesignService {
                } else {\r
                        url = kibanaImportUrl(portalHost, portalPort);\r
                }\r
+               return HttpClientUtil.sendPostHttpClient(url, requestBody, POST_FLAG);\r
 \r
-               //Send httpclient to kibana\r
-               String kibanaResponse = HttpClientUtil.sendPostToKibana(url, requestBody);\r
-               Gson gson = new Gson();\r
-               Map<String, Object> map = new HashMap<>();\r
-               map = gson.fromJson(kibanaResponse, map.getClass());\r
-               List objectsList = (List) map.get("objects");\r
-\r
-               if (objectsList != null && objectsList.size() > 0) {\r
-                       Map<String, Object> map2 = new HashMap<>();\r
-                       for (int i = 0; i < objectsList.size(); i++){\r
-                               map2 = (Map<String, Object>)objectsList.get(i);\r
-                               for(String key : map2.keySet()){\r
-                                       if ("error".equals(key)) {\r
-                                               return true;\r
-                                       }\r
-                               }\r
-                       }\r
+       }\r
+\r
+       private String kibanaImportUrl(String host, Integer port) {\r
+               if (port == null) {\r
+                       port = applicationConfiguration.getKibanaPort();\r
                }\r
-               return flag;\r
+               return "http://" + host + ":" + port + applicationConfiguration.getKibanaDashboardImportApi();\r
+       }\r
+\r
+       /**\r
+        * successed resp: { "acknowledged": true }\r
+        * \r
+        * @param portalDesign\r
+        * @param templateName\r
+        * @return flag\r
+        */\r
+       public boolean postEsMappingTemplate(PortalDesign portalDesign, String templateName) throws RuntimeException {\r
+               POST_FLAG = "ElasticsearchMappingTemplate";\r
+               String requestBody = portalDesign.getBody();\r
+\r
+               //FIXME\r
+               Set<Db> dbs = portalDesign.getDbs();\r
+               //submit to each ES in dbs\r
+\r
+               //return HttpClientUtil.sendPostHttpClient("http://"+dbService.getElasticsearch().getHost()+":9200/_template/"+templateName, requestBody, POST_FLAG);\r
+               return false;\r
        }\r
 \r
 }\r