2844facd6b31350cb55f7c9ed171197bed6104e0
[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 io.swagger.annotations.ApiOperation;
24 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
25 import org.onap.datalake.feeder.domain.Portal;
26 import org.onap.datalake.feeder.dto.PortalConfig;
27 import org.onap.datalake.feeder.repository.PortalRepository;
28 import org.onap.datalake.feeder.service.PortalService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.MediaType;
33 import org.springframework.validation.BindingResult;
34 import org.springframework.web.bind.annotation.*;
35
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 /**
42  * This controller manages Portal settings
43  *
44  *
45  * @author guochunmeng
46  */
47 @RestController
48 @RequestMapping(value = "/portals", produces = { MediaType.APPLICATION_JSON_VALUE })
49 public class PortalController {
50
51     private final Logger log = LoggerFactory.getLogger(this.getClass());
52
53     @Autowired
54     private PortalRepository portalRepository;
55
56     @Autowired
57     private PortalService portalService;
58
59     @PutMapping("")
60     @ResponseBody
61     @ApiOperation("update portal")
62     public PostReturnBody<PortalConfig> updatePortal(@RequestBody PortalConfig portalConfig, BindingResult result, HttpServletResponse response) throws IOException {
63
64         if (result.hasErrors()) {
65             sendError(response, 400, "Error binding PortalConfig: "+result.toString());
66             return null;
67         }
68
69         Portal portal = null;
70         try {
71             portal = portalRepository.findById(portalConfig.getName()).get();
72             log.info("Update portal "+portalConfig);
73             portalService.fillPortalConfiguration(portalConfig, portal);
74             portalRepository.save(portal);
75             return mkPostReturnBody(200, portal);
76         } catch (Exception e) {
77             log.debug("Update or delete portal failed, Portal: "+portalConfig, e.getMessage());
78             sendError(response, 400, "Error update or delete portal: "+portal);
79             return null;
80         }
81     }
82
83
84     @GetMapping("")
85     @ResponseBody
86     @ApiOperation(value = "List all portals")
87     public List<PortalConfig> getPortals() {
88
89         List<Portal> portalList = null;
90         List<PortalConfig> portalConfigList = new ArrayList<>();
91         portalList = (List<Portal>)portalRepository.findAll();
92         if (portalList != null && portalList.size() > 0) {
93             log.info("PortalList is not null");
94             for(Portal portal : portalList) {
95                 portalConfigList.add(portal.getPortalConfig());
96             }
97         }
98         return portalConfigList;
99     }
100
101
102     private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
103         log.info(msg);
104         response.sendError(sc, msg);
105     }
106
107
108     private PostReturnBody<PortalConfig> mkPostReturnBody(int statusCode, Portal portal) {
109         PostReturnBody<PortalConfig> retBody = new PostReturnBody<>();
110         retBody.setStatusCode(statusCode);
111         retBody.setReturnBody(portal.getPortalConfig());
112         return retBody;
113     }
114
115 }