2  * ============LICENSE_START=======================================================
\r 
   4  * ================================================================================
\r 
   5  * Copyright 2019 China Mobile
\r 
   6  *=================================================================================
\r 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   8  * you may not use this file except in compliance with the License.
\r 
   9  * You may obtain a copy of the License at
\r 
  11  *     http://www.apache.org/licenses/LICENSE-2.0
\r 
  13  * Unless required by applicable law or agreed to in writing, software
\r 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  16  * See the License for the specific language governing permissions and
\r 
  17  * limitations under the License.
\r 
  18  * ============LICENSE_END=========================================================
\r 
  21 package org.onap.datalake.feeder.service;
\r 
  23 import java.util.ArrayList;
\r 
  24 import java.util.List;
\r 
  25 import java.util.Optional;
\r 
  27 import org.onap.datalake.feeder.config.ApplicationConfiguration;
\r 
  28 import org.onap.datalake.feeder.domain.DesignType;
\r 
  29 import org.onap.datalake.feeder.domain.Portal;
\r 
  30 import org.onap.datalake.feeder.domain.PortalDesign;
\r 
  31 import org.onap.datalake.feeder.domain.Topic;
\r 
  32 import org.onap.datalake.feeder.dto.PortalDesignConfig;
\r 
  33 import org.onap.datalake.feeder.repository.DesignTypeRepository;
\r 
  34 import org.onap.datalake.feeder.repository.PortalDesignRepository;
\r 
  35 import org.onap.datalake.feeder.util.HttpClientUtil;
\r 
  36 import org.slf4j.Logger;
\r 
  37 import org.slf4j.LoggerFactory;
\r 
  38 import org.springframework.beans.factory.annotation.Autowired;
\r 
  39 import org.springframework.stereotype.Service;
\r 
  42  * Service for portalDesigns
\r 
  44  * @author guochunmeng
\r 
  48 public class PortalDesignService {
\r 
  50         private final Logger log = LoggerFactory.getLogger(this.getClass());
\r 
  52         static String POST_FLAG;
\r 
  55     private PortalDesignRepository portalDesignRepository;
\r 
  58         private TopicService topicService;
\r 
  61         private DesignTypeRepository designTypeRepository;
\r 
  64         private ApplicationConfiguration applicationConfiguration;
\r 
  67         private DbService dbService;
\r 
  69         public PortalDesign fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig) throws Exception
\r 
  71                 PortalDesign portalDesign = new PortalDesign();
\r 
  72                 fillPortalDesign(portalDesignConfig, portalDesign);
\r 
  73                 return portalDesign;
\r 
  75         public void fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig, PortalDesign portalDesign) throws Exception
\r 
  77                 fillPortalDesign(portalDesignConfig, portalDesign);
\r 
  80         private void fillPortalDesign(PortalDesignConfig portalDesignConfig, PortalDesign portalDesign) throws IllegalArgumentException {
\r 
  82                 portalDesign.setId(portalDesignConfig.getId());
\r 
  83                 portalDesign.setBody(portalDesignConfig.getBody());
\r 
  84                 portalDesign.setName(portalDesignConfig.getName());
\r 
  85                 portalDesign.setNote(portalDesignConfig.getNote());
\r 
  86                 portalDesign.setSubmitted(portalDesignConfig.getSubmitted());
\r 
  88                 if (portalDesignConfig.getTopic() != null) {
\r 
  89                         Topic topic = topicService.getTopic(portalDesignConfig.getTopic());
\r 
  90                         if (topic == null) throw new IllegalArgumentException("topic is null");
\r 
  91                         portalDesign.setTopicName(topic.getTopicName());
\r 
  93                         throw new IllegalArgumentException("Can not find topic in DB, topic name: "+portalDesignConfig.getTopic());
\r 
  96                 if (portalDesignConfig.getDesignType() != null) {
\r 
  97                         DesignType designType = designTypeRepository.findById(portalDesignConfig.getDesignType()).get();
\r 
  98                         if (designType == null) throw new IllegalArgumentException("designType is null");
\r 
  99                         portalDesign.setDesignType(designType);
\r 
 101                         throw new IllegalArgumentException("Can not find designType in Design_type, designType name "+portalDesignConfig.getDesignType());
\r 
 107         public PortalDesign getPortalDesign(Integer id) {
\r 
 109                 Optional<PortalDesign> ret = portalDesignRepository.findById(id);
\r 
 110                 return ret.isPresent() ? ret.get() : null;
\r 
 114         public List<PortalDesignConfig> queryAllPortalDesign(){
\r 
 116                 List<PortalDesign> portalDesignList = null;
\r 
 117                 List<PortalDesignConfig> portalDesignConfigList = new ArrayList<>();
\r 
 118                 portalDesignList = (List<PortalDesign>) portalDesignRepository.findAll();
\r 
 119                 if (portalDesignList != null && portalDesignList.size() > 0) {
\r 
 120                         log.info("PortalDesignList is not null");
\r 
 121                         for (PortalDesign portalDesign : portalDesignList) {
\r 
 122                                 portalDesignConfigList.add(portalDesign.getPortalDesignConfig());
\r 
 125                 return portalDesignConfigList;
\r 
 129         public boolean deploy(PortalDesign portalDesign){
\r 
 130                 boolean flag =true;
\r 
 131                 String designTypeName = portalDesign.getDesignType().getName();
\r 
 132                 if (portalDesign.getDesignType() != null && "kibana_db".equals(designTypeName)) {
\r 
 133                         flag = deployKibanaImport(portalDesign);
\r 
 134                 } else if (portalDesign.getDesignType() != null && "kibana_visual".equals(designTypeName)) {
\r 
 137                 } else if (portalDesign.getDesignType() != null && "kibana_search".equals(designTypeName)) {
\r 
 140                 } else if (portalDesign.getDesignType() != null && "es_mapping".equals(designTypeName)) {
\r 
 141                         flag = postEsMappingTemplate(portalDesign, portalDesign.getTopicName().getId().toLowerCase());
\r 
 142                 } else if (portalDesign.getDesignType() != null && "druid_kafka_spec".equals(designTypeName)) {
\r 
 152         private boolean deployKibanaImport(PortalDesign portalDesign) throws RuntimeException {
\r 
 153                 POST_FLAG = "KibanaDashboardImport";
\r 
 154                 String requestBody = portalDesign.getBody();
\r 
 155                 Portal portal = portalDesign.getDesignType().getPortal();
\r 
 156                 String portalHost = portal.getHost();
\r 
 157                 Integer portalPort = portal.getPort();
\r 
 160                 if (portalHost == null || portalPort == null) {
\r 
 161                         String dbHost = portal.getDb().getHost();
\r 
 162                         Integer dbPort = portal.getDb().getPort();
\r 
 163                         url = kibanaImportUrl(dbHost, dbPort);
\r 
 165                         url = kibanaImportUrl(portalHost, portalPort);
\r 
 167                 return HttpClientUtil.sendPostHttpClient(url, requestBody, POST_FLAG);
\r 
 172         private String kibanaImportUrl(String host, Integer port){
\r 
 173                 if (port == null) {
\r 
 174                         port = applicationConfiguration.getKibanaPort();
\r 
 176                 return "http://"+host+":"+port+applicationConfiguration.getKibanaDashboardImportApi();
\r 
 183          *     "acknowledged": true
\r 
 185          * @param portalDesign
\r 
 186          * @param templateName
\r 
 189         public boolean postEsMappingTemplate(PortalDesign portalDesign, String templateName) throws RuntimeException {
\r 
 190                 POST_FLAG = "ElasticsearchMappingTemplate";
\r 
 191                 String requestBody = portalDesign.getBody();
\r 
 192                 return HttpClientUtil.sendPostHttpClient("http://"+dbService.getElasticsearch().getHost()+":9200/_template/"+templateName, requestBody, POST_FLAG);
\r