7e364332918ae923e77aa9b011aafd3bf5dafde2
[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.setEnabled(dbConfig.isEnabled());
109                         newdb.setLogin(dbConfig.getLogin());
110                         newdb.setPass(dbConfig.getPassword());
111                         newdb.setEncrypt(dbConfig.isEncrypt());
112
113                         if(!dbConfig.getName().equals("Elecsticsearch") || !dbConfig.getName().equals("Druid"))
114                         {
115                                 newdb.setDatabase(new String(dbConfig.getDatabase()));
116                         }
117                         dbRepository.save(newdb);
118                         DbConfig retMsg;
119                         PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
120                         retMsg = new DbConfig();
121                         composeRetMessagefromDbConfig(newdb, retMsg);
122                         retBody.setReturnBody(retMsg);
123                         retBody.setStatusCode(200);
124                         return retBody;
125                 }
126         }
127
128         //Show a db
129         //the topics are missing in the return, since in we use @JsonBackReference on Db's topics 
130         //need to the the following method to retrieve the topic list 
131         @GetMapping("/{dbName}")
132         @ResponseBody
133         @ApiOperation(value="Get a database's details.")
134         public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
135                 /*Db db = dbService.getDb(dbName);
136                 if (db == null) {
137                         sendError(response, 404, "Db not found: " + dbName);
138                 }*/
139                 Db db = dbRepository.findByName(dbName);
140                 if (db == null) {
141                         sendError(response, 404, "Db not found: " + dbName);
142                 }
143                 return db;
144         }
145
146
147         //Update Db
148         @PutMapping("/{dbName}")
149         @ResponseBody
150         @ApiOperation(value="Update a database.")
151         public PostReturnBody<DbConfig> updateDb(@PathVariable("dbName") String dbName, @RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
152
153                 if (result.hasErrors()) {
154                         sendError(response, 400, "Error parsing DB: " + result.toString());
155                         return null;
156                 }
157
158                 if(!dbName.equals(dbConfig.getName()))
159                 {
160                         sendError(response, 400, "Mismatch DB name.");
161                         return null;
162                 }
163
164                 Db oldDb = dbService.getDb(dbConfig.getName());
165                 if (oldDb == null) {
166                         sendError(response, 404, "Db not found: " + dbConfig.getName());
167                         return null;
168                 } else {
169                         oldDb.setName(dbConfig.getName());
170                         oldDb.setHost(dbConfig.getHost());
171                         oldDb.setPort(dbConfig.getPort());
172                         oldDb.setEnabled(dbConfig.isEnabled());
173                         oldDb.setLogin(dbConfig.getLogin());
174                         oldDb.setPass(dbConfig.getPassword());
175                         oldDb.setEncrypt(dbConfig.isEncrypt());
176
177                         if(!oldDb.getName().equals("Elecsticsearch") || !oldDb.getName().equals("Druid"))
178                         {
179                                 oldDb.setDatabase(dbConfig.getDatabase());
180                         }
181                         dbRepository.save(oldDb);
182                         DbConfig retMsg;
183                         PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
184                         retMsg = new DbConfig();
185                         composeRetMessagefromDbConfig(oldDb, retMsg);
186                         retBody.setReturnBody(retMsg);
187                         retBody.setStatusCode(200);
188                         return retBody;
189                 }
190         }
191
192         //Delete a db
193         //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
194         //need to the the following method to retrieve the topic list
195         @DeleteMapping("/{dbName}")
196         @ResponseBody
197         @ApiOperation(value="Delete a database.")
198         public void deleteDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
199
200                 Db delDb = dbRepository.findByName(dbName);
201                 if (delDb == null) {
202                         sendError(response, 404, "Db not found: " + dbName);
203                         return;
204                 }
205                 Set<Topic> topicRelation = delDb.getTopics();
206                 topicRelation.clear();
207                 dbRepository.save(delDb);
208                 dbRepository.delete(delDb);
209                 response.setStatus(204);
210         }
211
212         //Read topics in a DB
213         @GetMapping("/{dbName}/topics")
214         @ResponseBody
215         @ApiOperation(value="Get a database's all topics.")
216         public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
217                 //Db db = dbService.getDb(dbName);
218                 Set<Topic> topics;
219                 try {
220                         Db db = dbRepository.findByName(dbName);
221                         topics = db.getTopics();
222                 }catch(Exception ex)
223                 {
224                         sendError(response, 404, "DB: " + dbName + " or Topics not found");
225                         return null;
226
227                 }
228                 return topics;
229         }
230
231
232         @PostMapping("/verify")
233         @ResponseBody
234         @ApiOperation(value="Database connection verification")
235         public PostReturnBody<DbConfig> verifyDbConnection(@RequestBody DbConfig dbConfig, HttpServletResponse response) throws IOException {
236
237                 /*
238                         Not implemented yet.
239                  */
240
241                 response.setStatus(501);
242                 return null;
243         }
244
245         private void composeRetMessagefromDbConfig(Db db, DbConfig dbConfigMsg)
246         {
247                 dbConfigMsg.setName(db.getName());
248                 dbConfigMsg.setHost(db.getHost());
249                 dbConfigMsg.setEnabled(db.isEnabled());
250                 dbConfigMsg.setPort(db.getPort());
251                 dbConfigMsg.setLogin(db.getLogin());
252                 dbConfigMsg.setDatabase(db.getDatabase());
253
254
255         }
256
257         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
258                 log.info(msg);
259                 response.sendError(sc, msg);
260         }
261 }