2 * ============LICENSE_START=======================================================
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
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.feeder.controller;
23 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
24 import org.onap.datalake.feeder.domain.Design;
25 import org.onap.datalake.feeder.dto.DesignConfig;
26 import org.onap.datalake.feeder.repository.DesignRepository;
27 import org.onap.datalake.feeder.service.DesignService;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.MediaType;
32 import org.springframework.validation.BindingResult;
33 import org.springframework.web.bind.annotation.*;
35 import io.swagger.annotations.ApiOperation;
37 import java.io.IOException;
38 import java.util.List;
40 import javax.servlet.http.HttpServletResponse;
44 * This controller manages design settings
49 @RequestMapping(value = "/designs", produces = MediaType.APPLICATION_JSON_VALUE)
50 public class DesignController {
52 private final Logger log = LoggerFactory.getLogger(this.getClass());
55 private DesignRepository designRepository;
58 private DesignService designService;
62 @ApiOperation(value="Create a design.")
63 public PostReturnBody<DesignConfig> createDesign(@RequestBody DesignConfig designConfig, BindingResult result, HttpServletResponse response) throws IOException {
65 if (result.hasErrors()) {
66 sendError(response, 400, "Error parsing DesignConfig: "+result.toString());
72 design = designService.fillDesignConfiguration(designConfig);
73 } catch (Exception e) {
74 log.debug("FillDesignConfiguration failed", e.getMessage());
75 sendError(response, 400, "Error FillDesignConfiguration: "+e.getMessage());
78 designRepository.save(design);
79 log.info("Design save successed");
80 return mkPostReturnBody(200, design);
86 @ApiOperation(value="Update a design.")
87 public PostReturnBody<DesignConfig> updateDesign(@RequestBody DesignConfig designConfig, BindingResult result, @PathVariable Integer id, HttpServletResponse response) throws IOException {
89 if (result.hasErrors()) {
90 sendError(response, 400, "Error parsing DesignConfig: "+result.toString());
94 Design design = designService.getDesign(id);
97 designService.fillDesignConfiguration(designConfig, design);
98 } catch (Exception e) {
99 log.debug("FillDesignConfiguration failed", e.getMessage());
100 sendError(response, 400, "Error FillDesignConfiguration: "+e.getMessage());
103 designRepository.save(design);
104 log.info("Design update successed");
105 return mkPostReturnBody(200, design);
107 sendError(response, 400, "Design not found: "+id);
114 @DeleteMapping("/{id}")
116 @ApiOperation(value="delete a design.")
117 public void deleteDesign(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException{
119 Design oldDesign = designService.getDesign(id);
120 if (oldDesign == null) {
121 sendError(response, 400, "design not found "+id);
123 designRepository.delete(oldDesign);
124 response.setStatus(204);
131 @ApiOperation(value="List all Designs")
132 public List<DesignConfig> queryAllDesign(){
133 return designService.queryAllDesign();
137 @PostMapping("/deploy/{id}")
139 @ApiOperation(value="Design deploy")
140 public void deployDesign(@PathVariable Integer id, HttpServletResponse response) throws IOException {
142 Design design = null;
144 design = designRepository.findById(id).get();
147 flag = designService.deploy(design);
149 design.setSubmitted(true);
150 designRepository.save(design);
151 response.setStatus(204);
153 sendError(response, 400, "DeployDesign failed, id: "+id);
155 } catch (Exception e) {
156 log.debug("The request failed", e.getMessage());
157 sendError(response, 400, "The request failed : "+e.getMessage());
159 } catch (Exception e) {
160 log.debug("Design is null", e.getMessage());
161 sendError(response, 400, "Design not found, id: "+id);
167 private PostReturnBody<DesignConfig> mkPostReturnBody(int statusCode, Design design) {
168 PostReturnBody<DesignConfig> retBody = new PostReturnBody<>();
169 retBody.setStatusCode(statusCode);
170 retBody.setReturnBody(design.getDesignConfig());
174 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
176 response.sendError(sc, msg);