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