7b717268aeeded06ffcaf3efa887030162acf310
[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
21 package org.onap.datalake.feeder.controller;
22
23 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
24 import org.onap.datalake.feeder.domain.PortalDesign;
25 import org.onap.datalake.feeder.dto.PortalDesignConfig;
26 import org.onap.datalake.feeder.repository.PortalDesignRepository;
27 import org.onap.datalake.feeder.service.PortalDesignService;
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.*;
34
35 import io.swagger.annotations.ApiOperation;
36
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import javax.servlet.http.HttpServletResponse;
42
43
44 /**
45  * This controller manages portalDesign settings
46  *
47  * @author guochunmeng
48  */
49 @CrossOrigin(origins = "*")
50 @RestController
51 @RequestMapping(value = "/portalDesigns", produces = MediaType.APPLICATION_JSON_VALUE)
52 public class PortalDesignController {
53
54     private final Logger log = LoggerFactory.getLogger(this.getClass());
55
56     @Autowired
57     private PortalDesignRepository portalDesignRepository;
58     
59     @Autowired
60     private PortalDesignService portalDesignService;
61
62         @PostMapping("")
63         @ResponseBody
64         @ApiOperation(value="Create a portalDesign.")
65     public PostReturnBody<PortalDesignConfig> createPortalDesign(@RequestBody PortalDesignConfig portalDesignConfig, BindingResult result, HttpServletResponse response) throws IOException {
66
67                 if (result.hasErrors()) {
68                         sendError(response, 400, "Error parsing PortalDesignConfig: "+result.toString());
69                         return null;
70                 }
71
72                 PortalDesign portalDesign = null;
73                 try {
74                         portalDesign = portalDesignService.fillPortalDesignConfiguration(portalDesignConfig);
75                 } catch (Exception e) {
76                         log.debug("FillPortalDesignConfiguration failed", e.getMessage());
77                         sendError(response, 400, "Error FillPortalDesignConfiguration: "+e.getMessage());
78                         return null;
79                 }
80                 portalDesignRepository.save(portalDesign);
81                 log.info("PortalDesign save successed");
82                 return mkPostReturnBody(200, portalDesign);
83     }
84
85
86         @PutMapping("{id}")
87         @ResponseBody
88         @ApiOperation(value="Update a portalDesign.")
89         public PostReturnBody<PortalDesignConfig> updatePortalDesign(@RequestBody PortalDesignConfig portalDesignConfig, BindingResult result, @PathVariable Integer id, HttpServletResponse response) throws IOException {
90
91                 if (result.hasErrors()) {
92                         sendError(response, 400, "Error parsing PortalDesignConfig: "+result.toString());
93                         return null;
94                 }
95
96                 PortalDesign portalDesign = portalDesignService.getPortalDesign(id);
97                 if (portalDesign != null) {
98                         try {
99                                 portalDesignService.fillPortalDesignConfiguration(portalDesignConfig, portalDesign);
100                         } catch (Exception e) {
101                                 log.debug("FillPortalDesignConfiguration failed", e.getMessage());
102                                 sendError(response, 400, "Error FillPortalDesignConfiguration: "+e.getMessage());
103                                 return null;
104                         }
105                         portalDesignRepository.save(portalDesign);
106                         log.info("PortalDesign update successed");
107                         return mkPostReturnBody(200, portalDesign);
108                 } else {
109                         sendError(response, 400, "PortalDesign not found: "+id);
110                         return null;
111                 }
112
113         }
114
115
116         @DeleteMapping("/{id}")
117         @ResponseBody
118         @ApiOperation(value="delete a portalDesign.")
119     public void deletePortalDesign(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException{
120                 
121                 PortalDesign oldPortalDesign= portalDesignService.getPortalDesign(id);
122                 if (oldPortalDesign == null) {
123                         sendError(response, 400, "portalDesign not found "+id);
124                 } else {
125                         portalDesignRepository.delete(oldPortalDesign);
126                         response.setStatus(204);
127                 }
128     }
129
130
131         @GetMapping("")
132         @ResponseBody
133         @ApiOperation(value="List all PortalDesigns")
134     public List<PortalDesignConfig> queryAllPortalDesign(){
135
136                 List<PortalDesign> portalDesignList = null;
137                 List<PortalDesignConfig> portalDesignConfigList = new ArrayList<>();
138                 portalDesignList = (List<PortalDesign>) portalDesignRepository.findAll();
139                 if (portalDesignList != null && portalDesignList.size() > 0) {
140                         log.info("PortalDesignList is not null");
141                         for (PortalDesign portalDesign : portalDesignList) {
142                                 portalDesignConfigList.add(portalDesign.getPortalDesignConfig());
143                         }
144                 }
145                 return portalDesignConfigList;
146     }
147
148
149         @PostMapping("/deploy/{id}")
150         @ResponseBody
151         @ApiOperation(value="PortalDesign deploy")
152         public void deployPortalDesign(@PathVariable Integer id, HttpServletResponse response) throws IOException {
153
154                 PortalDesign portalDesign = null;
155                 try {
156                         portalDesign = portalDesignRepository.findById(id).get();
157                         if (portalDesign.getDesignType() != null && portalDesign.getDesignType().getName().startsWith("Kibana")) {
158                                 boolean flag = portalDesignService.deployKibanaImport(portalDesign);
159                                 if (flag) {
160                                         sendError(response, 400, "DeployPortalDesign failed, id: "+id);
161                                 }
162                         } else if (portalDesign.getDesignType() != null && portalDesign.getDesignType().getName().startsWith("Elasticsearch")) {
163                                 //TODO Elasticsearch template import
164                                 sendError(response, 400, "DeployPortalDesign failed, id: "+id);
165                         } else {
166                                 //TODO Druid import
167                                 sendError(response, 400, "DeployPortalDesign failed, id: "+id);
168                         }
169                         portalDesign.setSubmitted(true);
170                         portalDesignRepository.save(portalDesign);
171                         response.setStatus(204);
172                 } catch (Exception e) {
173                         log.debug("PortalDesign is null", e.getMessage());
174                         sendError(response, 400, "PortalDesign not found, id: "+id);
175                 }
176
177         }
178
179
180         private PostReturnBody<PortalDesignConfig> mkPostReturnBody(int statusCode, PortalDesign portalDesign) {
181                 PostReturnBody<PortalDesignConfig> retBody = new PostReturnBody<>();
182         retBody.setStatusCode(statusCode);
183         retBody.setReturnBody(portalDesign.getPortalDesignConfig());
184         return retBody;
185         }
186     
187         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
188                 log.info(msg);
189                 response.sendError(sc, msg);            
190         }
191     
192 }