cb3ff344c3758c9cab98a88ff427401621e033e5
[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 @CrossOrigin(origins = "*")
48 @RestController
49 @RequestMapping(value = "/portals", produces = { MediaType.APPLICATION_JSON_VALUE })
50 public class PortalController {
51
52     private final Logger log = LoggerFactory.getLogger(this.getClass());
53
54     @Autowired
55     private PortalRepository portalRepository;
56
57     @Autowired
58     private PortalService portalService;
59
60     @PutMapping("")
61     @ResponseBody
62     @ApiOperation("update portal")
63     public PostReturnBody<PortalConfig> updatePortal(@RequestBody PortalConfig portalConfig, BindingResult result, HttpServletResponse response) throws IOException {
64
65         if (result.hasErrors()) {
66             sendError(response, 400, "Error binding PortalConfig: "+result.toString());
67             return null;
68         }
69
70         Portal portal = null;
71         try {
72             portal = portalRepository.findById(portalConfig.getName()).get();
73             if (portalConfig.getEnabled() == false) {
74                 log.info("Disable portal "+portalConfig.getName());
75                 portal.setPort(null);
76                 portal.setHost(null);
77                 portal.setLogin(null);
78                 portal.setPass(null);
79                 portal.setEnabled(false);
80             }else {
81                 log.info("Update portal "+portalConfig);
82                 portalService.fillPortalConfiguration(portalConfig, portal);
83             }
84             portalRepository.save(portal);
85             return mkPostReturnBody(200, portal);
86         } catch (Exception e) {
87             log.debug("Update or delete portal failed, Portal: "+portalConfig, e.getMessage());
88             sendError(response, 400, "Error update or delete portal: "+portal);
89             return null;
90         }
91     }
92
93
94     @GetMapping("")
95     @ResponseBody
96     @ApiOperation(value = "List all portals")
97     public List<PortalConfig> getPortals() {
98
99         List<Portal> portalList = null;
100         List<PortalConfig> portalConfigList = new ArrayList<>();
101         portalList = (List<Portal>)portalRepository.findAll();
102         if (portalList != null && portalList.size() > 0) {
103             log.info("PortalList is not null");
104             for(Portal portal : portalList) {
105                 portalConfigList.add(portal.getPortalConfig());
106             }
107         }
108         return portalConfigList;
109     }
110
111
112     private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
113         log.info(msg);
114         response.sendError(sc, msg);
115     }
116
117
118     private PostReturnBody<PortalConfig> mkPostReturnBody(int statusCode, Portal portal) {
119         PostReturnBody<PortalConfig> retBody = new PostReturnBody<>();
120         retBody.setStatusCode(statusCode);
121         retBody.setReturnBody(portal.getPortalConfig());
122         return retBody;
123     }
124
125 }