c7d642b5187370265895798d1adcbf00bf74e383
[dcaegen2/services.git] /
1 /*
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
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
19 */
20
21 package org.onap.datalake.des.service;
22
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;
37
38 /**
39  * Service for DataExposure
40  *
41  * @author Kai Lu
42  *
43  */
44 @Service
45 public class DataExposureService {
46
47         private final Logger log = LoggerFactory.getLogger(this.getClass());
48         @Autowired
49         private DataExposureRepository dataExposureRepository;
50         @Autowired
51         private DbRepository dbRepository;
52
53     /**
54      * getDataExposure.
55      *
56      * @param serviceId serviceId
57      *
58          * @return DataExposure
59      *
60      */
61         public DataExposure getDataExposure(String serviceId) {
62                 Optional<DataExposure> ret = dataExposureRepository.findById(serviceId);
63                 return ret.isPresent() ? ret.get() : null;
64         }
65
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());
74                         }
75                 }
76                 return dataExposureConfigList;
77         }
78
79     /**
80      * getDataExposureById.
81      *
82      * @param id id
83      *
84          * @return data exposure
85      *
86      */
87         public DataExposure getDataExposureById(String id) {
88                 Optional<DataExposure> ret = dataExposureRepository.findById(id);
89                 return ret.isPresent() ? ret.get() : null;
90         }
91
92     /**
93      * fillDataExposureConfiguration.
94      *
95      * @param dataExposureConfig DataExposureConfig
96      *
97          * @return data exposure
98      *
99      */
100         public DataExposure fillDataExposureConfiguration(DataExposureConfig dataExposureConfig) {
101                 DataExposure dataExposure = new DataExposure();
102                 fillDataExposure(dataExposureConfig, dataExposure);
103                 return dataExposure;
104         }
105
106     /**
107      * fillDataExposureConfiguration.
108      *
109      * @param dataExposureConfig DataExposureConfig
110      * @param dataExposure DataExposure
111      *
112          * @return data exposure
113      *
114      */
115         public void fillDataExposureConfiguration(DataExposureConfig dataExposureConfig, DataExposure dataExposure) {
116                 fillDataExposure(dataExposureConfig, dataExposure);
117         }
118
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());
130         }
131 }