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
26 import java.util.Set;
\r
28 import org.onap.datalake.feeder.config.ApplicationConfiguration;
\r
29 import org.onap.datalake.feeder.domain.Db;
\r
30 import org.onap.datalake.feeder.domain.DbType;
\r
31 import org.onap.datalake.feeder.domain.DesignType;
\r
32 import org.onap.datalake.feeder.domain.Portal;
\r
33 import org.onap.datalake.feeder.domain.PortalDesign;
\r
34 import org.onap.datalake.feeder.domain.Topic;
\r
35 import org.onap.datalake.feeder.domain.TopicName;
\r
36 import org.onap.datalake.feeder.dto.PortalDesignConfig;
\r
37 import org.onap.datalake.feeder.enumeration.DbTypeEnum;
\r
38 import org.onap.datalake.feeder.enumeration.DesignTypeEnum;
\r
39 import org.onap.datalake.feeder.repository.DesignTypeRepository;
\r
40 import org.onap.datalake.feeder.repository.PortalDesignRepository;
\r
41 import org.onap.datalake.feeder.repository.TopicNameRepository;
\r
42 import org.onap.datalake.feeder.service.db.CouchbaseService;
\r
43 import org.onap.datalake.feeder.service.db.DbStoreService;
\r
44 import org.onap.datalake.feeder.service.db.ElasticsearchService;
\r
45 import org.onap.datalake.feeder.service.db.HdfsService;
\r
46 import org.onap.datalake.feeder.service.db.MongodbService;
\r
47 import org.onap.datalake.feeder.util.HttpClientUtil;
\r
48 import org.slf4j.Logger;
\r
49 import org.slf4j.LoggerFactory;
\r
50 import org.springframework.beans.factory.annotation.Autowired;
\r
51 import org.springframework.stereotype.Service;
\r
54 * Service for portalDesigns
\r
56 * @author guochunmeng
\r
60 public class PortalDesignService {
\r
62 private final Logger log = LoggerFactory.getLogger(this.getClass());
\r
64 static String POST_FLAG;
\r
67 private PortalDesignRepository portalDesignRepository;
\r
70 private TopicNameRepository topicNameRepository;
\r
73 private DesignTypeRepository designTypeRepository;
\r
76 private ApplicationConfiguration applicationConfiguration;
\r
78 public PortalDesign fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig) throws Exception {
\r
79 PortalDesign portalDesign = new PortalDesign();
\r
80 fillPortalDesign(portalDesignConfig, portalDesign);
\r
81 return portalDesign;
\r
84 public void fillPortalDesignConfiguration(PortalDesignConfig portalDesignConfig, PortalDesign portalDesign) throws Exception {
\r
85 fillPortalDesign(portalDesignConfig, portalDesign);
\r
88 private void fillPortalDesign(PortalDesignConfig portalDesignConfig, PortalDesign portalDesign) throws IllegalArgumentException {
\r
90 portalDesign.setId(portalDesignConfig.getId());
\r
91 portalDesign.setBody(portalDesignConfig.getBody());
\r
92 portalDesign.setName(portalDesignConfig.getName());
\r
93 portalDesign.setNote(portalDesignConfig.getNote());
\r
94 portalDesign.setSubmitted(portalDesignConfig.getSubmitted());
\r
96 if (portalDesignConfig.getTopic() != null) {
\r
97 Optional<TopicName> topicName = topicNameRepository.findById(portalDesignConfig.getTopic());
\r
98 if (topicName.isPresent()) {
\r
99 portalDesign.setTopicName(topicName.get());
\r
101 throw new IllegalArgumentException("topic is null " + portalDesignConfig.getTopic());
\r
104 throw new IllegalArgumentException("Can not find topic in DB, topic name: " + portalDesignConfig.getTopic());
\r
107 if (portalDesignConfig.getDesignType() != null) {
\r
108 DesignType designType = designTypeRepository.findById(portalDesignConfig.getDesignType()).get();
\r
109 if (designType == null)
\r
110 throw new IllegalArgumentException("designType is null");
\r
111 portalDesign.setDesignType(designType);
\r
113 throw new IllegalArgumentException("Can not find designType in Design_type, designType name " + portalDesignConfig.getDesignType());
\r
118 public PortalDesign getPortalDesign(Integer id) {
\r
120 Optional<PortalDesign> ret = portalDesignRepository.findById(id);
\r
121 return ret.isPresent() ? ret.get() : null;
\r
124 public List<PortalDesignConfig> queryAllPortalDesign() {
\r
126 List<PortalDesign> portalDesignList = null;
\r
127 List<PortalDesignConfig> portalDesignConfigList = new ArrayList<>();
\r
128 portalDesignList = (List<PortalDesign>) portalDesignRepository.findAll();
\r
129 if (portalDesignList != null && portalDesignList.size() > 0) {
\r
130 log.info("PortalDesignList is not null");
\r
131 for (PortalDesign portalDesign : portalDesignList) {
\r
132 portalDesignConfigList.add(portalDesign.getPortalDesignConfig());
\r
135 return portalDesignConfigList;
\r
138 public boolean deploy(PortalDesign portalDesign) {
\r
139 DesignType designType = portalDesign.getDesignType();
\r
140 DesignTypeEnum designTypeEnum = DesignTypeEnum.valueOf(designType.getId());
\r
142 switch (designTypeEnum) {
\r
144 return deployKibanaImport(portalDesign);
\r
146 return postEsMappingTemplate(portalDesign, portalDesign.getTopicName().getId().toLowerCase());
\r
148 log.error("Not implemented {}", designTypeEnum);
\r
153 private boolean deployKibanaImport(PortalDesign portalDesign) throws RuntimeException {
\r
154 POST_FLAG = "KibanaDashboardImport";
\r
155 String requestBody = portalDesign.getBody();
\r
156 Portal portal = portalDesign.getDesignType().getPortal();
\r
157 String portalHost = portal.getHost();
\r
158 Integer portalPort = portal.getPort();
\r
161 if (portalHost == null || portalPort == null) {
\r
162 String dbHost = portal.getDb().getHost();
\r
163 Integer dbPort = portal.getDb().getPort();
\r
164 url = kibanaImportUrl(dbHost, dbPort);
\r
166 url = kibanaImportUrl(portalHost, portalPort);
\r
168 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
180 * successed resp: { "acknowledged": true }
\r
182 * @param portalDesign
\r
183 * @param templateName
\r
186 public boolean postEsMappingTemplate(PortalDesign portalDesign, String templateName) throws RuntimeException {
\r
187 POST_FLAG = "ElasticsearchMappingTemplate";
\r
188 String requestBody = portalDesign.getBody();
\r
191 Set<Db> dbs = portalDesign.getDbs();
\r
192 //submit to each ES in dbs
\r
194 //return HttpClientUtil.sendPostHttpClient("http://"+dbService.getElasticsearch().getHost()+":9200/_template/"+templateName, requestBody, POST_FLAG);
\r