2 * ============LICENSE_START=======================================================
3 * ONAP : Data Extraction Service
4 * ================================================================================
5 * Copyright 2020 China Mobile
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.datalake.des.service;
23 import java.io.IOException;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Optional;
28 import org.onap.datalake.des.domain.DataExposure;
29 import org.onap.datalake.des.dto.DataExposureConfig;
30 import org.onap.datalake.des.repository.DataExposureRepository;
31 import org.onap.datalake.feeder.domain.Db;
32 import org.onap.datalake.feeder.repository.DbRepository;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
39 * Service for DataExposure
45 public class DataExposureService {
47 private final Logger log = LoggerFactory.getLogger(this.getClass());
49 private DataExposureRepository dataExposureRepository;
51 private DbRepository dbRepository;
56 * @param serviceId serviceId
58 * @return DataExposure
61 public DataExposure getDataExposure(String serviceId) {
62 Optional<DataExposure> ret = dataExposureRepository.findById(serviceId);
63 return ret.isPresent() ? ret.get() : null;
66 public List<DataExposureConfig> queryAllDataExposure() {
67 List<DataExposure> dataExposureList = null;
68 List<DataExposureConfig> dataExposureConfigList = new ArrayList<>();
69 dataExposureList = (List<DataExposure>) dataExposureRepository.findAll();
70 if (!dataExposureList.isEmpty()) {
71 log.info("DataExposureList is not null");
72 for (DataExposure dataExposure : dataExposureList) {
73 dataExposureConfigList.add(dataExposure.getDataExposureConfig());
76 return dataExposureConfigList;
80 * getDataExposureById.
84 * @return data exposure
87 public DataExposure getDataExposureById(String id) {
88 Optional<DataExposure> ret = dataExposureRepository.findById(id);
89 return ret.isPresent() ? ret.get() : null;
93 * fillDataExposureConfiguration.
95 * @param dataExposureConfig DataExposureConfig
97 * @return data exposure
100 public DataExposure fillDataExposureConfiguration(DataExposureConfig dataExposureConfig) {
101 DataExposure dataExposure = new DataExposure();
102 fillDataExposure(dataExposureConfig, dataExposure);
107 * fillDataExposureConfiguration.
109 * @param dataExposureConfig DataExposureConfig
110 * @param dataExposure DataExposure
112 * @return data exposure
115 public void fillDataExposureConfiguration(DataExposureConfig dataExposureConfig, DataExposure dataExposure) {
116 fillDataExposure(dataExposureConfig, dataExposure);
119 private void fillDataExposure(DataExposureConfig dataExposureConfig, DataExposure dataExposure)
120 throws IllegalArgumentException {
121 dataExposure.setId(dataExposureConfig.getId());
122 dataExposure.setNote(dataExposureConfig.getNote());
123 dataExposure.setSqlTemplate(dataExposureConfig.getSqlTemplate());
124 if (dataExposureConfig.getDbId() == null)
125 throw new IllegalArgumentException("Can not find db_id in db, db_id: " + dataExposureConfig.getDbId());
126 Optional<Db> dbOptional = dbRepository.findById(dataExposureConfig.getDbId());
127 if (!dbOptional.isPresent())
128 throw new IllegalArgumentException("db_id is null " + dataExposureConfig.getDbId());
129 dataExposure.setDb(dbOptional.get());