bd9b742bed2e60638a3277466bb06d60757e9a5d
[dcaegen2/services.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DataLake
4 * ================================================================================
5 * Copyright 2019 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 package org.onap.datalake.feeder.controller;
21
22 import java.io.IOException;
23 import java.util.*;
24
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.onap.datalake.feeder.domain.Db;
28 import org.onap.datalake.feeder.domain.Topic;
29 import org.onap.datalake.feeder.repository.DbRepository;
30 import org.onap.datalake.feeder.repository.TopicRepository;
31 import org.onap.datalake.feeder.service.DbService;
32 import org.onap.datalake.feeder.dto.DbConfig;
33 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.MediaType;
38 import org.springframework.validation.BindingResult;
39 import org.springframework.web.bind.annotation.*;
40
41 import io.swagger.annotations.ApiOperation;
42
43 /**
44  * This controller manages the big data storage settings. All the settings are
45  * saved in database.
46  * 
47  * @author Guobiao Mo
48  *
49  */
50
51 @RestController
52 @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE })
53
54 //@Api(value = "db", consumes = "application/json", produces = "application/json")
55 public class DbController {
56
57         private final Logger log = LoggerFactory.getLogger(this.getClass());
58
59         @Autowired
60         private DbRepository dbRepository;
61
62         @Autowired
63         private TopicRepository topicRepository;
64
65         @Autowired
66         private DbService dbService;
67
68         //list all dbs 
69         @GetMapping("")
70         @ResponseBody
71         @ApiOperation(value="Gat all databases name")
72         //public Iterable<Db> list() throws IOException {
73         public List<String> list() throws IOException {
74                 Iterable<Db> ret = dbRepository.findAll();
75                 List<String> retString = new ArrayList<>();
76                 for(Db db : ret)
77                 {
78                         log.info(db.getName());
79                         retString.add(db.getName());
80
81                 }
82                 return retString;
83         }
84
85         //Create a  DB
86         @PostMapping("")
87         @ResponseBody
88         @ApiOperation(value="Create a new database.")
89         public PostReturnBody<DbConfig> createDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
90                 if (result.hasErrors()) {
91                         sendError(response, 400, "Malformed format of Post body: " + result.toString());
92                         return null;
93                 }
94
95                 Db oldDb = dbService.getDb(dbConfig.getName());
96                 if (oldDb != null) {
97                         sendError(response, 400, "Db already exists: " + dbConfig.getName());
98                         return null;
99                 } else {
100                         Db newdb = new Db();
101                         newdb.setName(dbConfig.getName());
102                         newdb.setHost(dbConfig.getHost());
103                         newdb.setPort(dbConfig.getPort());
104                         newdb.setEnabled(dbConfig.isEnabled());
105                         newdb.setLogin(dbConfig.getLogin());
106                         newdb.setPass(dbConfig.getPassword());
107                         newdb.setEncrypt(dbConfig.isEncrypt());
108
109                         if(!dbConfig.getName().equals("Elecsticsearch") || !dbConfig.getName().equals("Druid"))
110                         {
111                                 newdb.setDatabase(new String(dbConfig.getDatabase()));
112                         }
113                         dbRepository.save(newdb);
114                         DbConfig retMsg;
115                         PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
116                         retMsg = new DbConfig();
117                         composeRetMessagefromDbConfig(newdb, retMsg);
118                         retBody.setReturnBody(retMsg);
119                         retBody.setStatusCode(200);
120                         return retBody;
121                 }
122         }
123
124         //Show a db
125         //the topics are missing in the return, since in we use @JsonBackReference on Db's topics 
126         //need to the the following method to retrieve the topic list 
127         @GetMapping("/{dbName}")
128         @ResponseBody
129         @ApiOperation(value="Get a database's details.")
130         public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
131                 /*Db db = dbService.getDb(dbName);
132                 if (db == null) {
133                         sendError(response, 404, "Db not found: " + dbName);
134                 }*/
135                 Db db = dbRepository.findByName(dbName);
136                 if (db == null) {
137                         sendError(response, 404, "Db not found: " + dbName);
138                 }
139                 return db;
140         }
141
142
143         //Delete a db
144         //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
145         //need to the the following method to retrieve the topic list
146         @DeleteMapping("/{dbName}")
147         @ResponseBody
148         @ApiOperation(value="Delete a database.")
149         public void deleteDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
150
151                 Db delDb = dbRepository.findByName(dbName);
152                 if (delDb == null) {
153                         sendError(response, 404, "Db not found: " + dbName);
154                         return;
155                 }
156                 Set<Topic> topicRelation = delDb.getTopics();
157                 topicRelation.clear();
158                 dbRepository.save(delDb);
159                 dbRepository.delete(delDb);
160                 response.setStatus(204);
161         }
162
163         //Read topics in a DB
164         @GetMapping("/{dbName}/topics")
165         @ResponseBody
166         @ApiOperation(value="Get a database's all topics.")
167         public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
168                 //Db db = dbService.getDb(dbName);
169                 Set<Topic> topics;
170                 try {
171                         Db db = dbRepository.findByName(dbName);
172                         topics = db.getTopics();
173                 }catch(Exception ex)
174                 {
175                         sendError(response, 404, "DB: " + dbName + " or Topics not found");
176                         return null;
177
178                 }
179                 return topics;
180         }
181
182
183         //Update Db
184         @PutMapping("")
185         @ResponseBody
186         @ApiOperation(value="Update a database.")
187         public PostReturnBody<DbConfig> updateDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
188
189                 if (result.hasErrors()) {
190                         sendError(response, 400, "Error parsing DB: " + result.toString());
191                         return null;
192                 }
193
194                 Db oldDb = dbService.getDb(dbConfig.getName());
195                 if (oldDb == null) {
196                         sendError(response, 404, "Db not found: " + dbConfig.getName());
197                         return null;
198                 } else {
199                         oldDb.setHost(dbConfig.getHost());
200                         oldDb.setPort(dbConfig.getPort());
201                         oldDb.setEnabled(dbConfig.isEnabled());
202                         oldDb.setLogin(dbConfig.getLogin());
203                         oldDb.setPass(dbConfig.getPassword());
204                         oldDb.setEncrypt(dbConfig.isEncrypt());
205                         if (!oldDb.getName().equals("Elecsticsearch") || !oldDb.getName().equals("Druid")) {
206                                 oldDb.setDatabase(dbConfig.getDatabase());
207                         }
208
209                         dbRepository.save(oldDb);
210                         DbConfig retMsg;
211                         PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
212                         retMsg = new DbConfig();
213                         composeRetMessagefromDbConfig(oldDb, retMsg);
214                         retBody.setReturnBody(retMsg);
215                         retBody.setStatusCode(200);
216                         return retBody;
217                 }
218
219         }
220
221
222         @PostMapping("/verify")
223         @ResponseBody
224         @ApiOperation(value="Database connection verification")
225         public PostReturnBody<DbConfig> verifyDbConnection(@RequestBody DbConfig dbConfig, HttpServletResponse response) throws IOException {
226
227                 /*
228                         Not implemented yet.
229                  */
230
231                 response.setStatus(501);
232                 return null;
233         }
234
235         private void composeRetMessagefromDbConfig(Db db, DbConfig dbConfigMsg)
236         {
237                 dbConfigMsg.setName(db.getName());
238                 dbConfigMsg.setHost(db.getHost());
239                 dbConfigMsg.setEnabled(db.isEnabled());
240                 dbConfigMsg.setPort(db.getPort());
241                 dbConfigMsg.setLogin(db.getLogin());
242                 dbConfigMsg.setDatabase(db.getDatabase());
243
244
245         }
246
247         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
248                 log.info(msg);
249                 response.sendError(sc, msg);
250         }
251 }