Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / MicroserviceController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.controller;
39
40 import java.util.List;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
46 import org.openecomp.portalapp.portal.domain.MicroserviceData;
47 import org.openecomp.portalapp.portal.domain.WidgetCatalog;
48 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
49 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
50 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
51 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
52 import org.openecomp.portalapp.portal.service.ConsulHealthService;
53 import org.openecomp.portalapp.portal.service.MicroserviceService;
54 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
55 import org.openecomp.portalsdk.core.util.SystemProperties;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.context.annotation.EnableAspectJAutoProxy;
58 import org.springframework.core.ParameterizedTypeReference;
59 import org.springframework.http.HttpEntity;
60 import org.springframework.http.HttpMethod;
61 import org.springframework.http.ResponseEntity;
62 import org.springframework.web.bind.annotation.PathVariable;
63 import org.springframework.web.bind.annotation.RequestBody;
64 import org.springframework.web.bind.annotation.RequestMapping;
65 import org.springframework.web.bind.annotation.RequestMethod;
66 import org.springframework.web.bind.annotation.RestController;
67 import org.springframework.web.client.RestTemplate;
68
69 @SuppressWarnings("unchecked")
70 @RestController
71 @org.springframework.context.annotation.Configuration
72 @EnableAspectJAutoProxy
73 @EPAuditLog
74 public class MicroserviceController extends EPRestrictedBaseController {
75         
76         String whatService = "widgets-service";
77         RestTemplate template = new RestTemplate();
78
79         @Autowired
80         private ConsulHealthService consulHealthService;
81
82         @Autowired
83         private MicroserviceService microserviceService;
84
85         @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.POST)
86         public PortalRestResponse<String> createMicroservice(HttpServletRequest request, HttpServletResponse response,
87                         @RequestBody MicroserviceData newServiceData) throws Exception {
88                 if (newServiceData == null) {
89                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
90                                         "MicroserviceData cannot be null or empty");
91                 }
92                 long serviceId = microserviceService.saveMicroservice(newServiceData);
93
94                 try {
95                         microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
96                 } catch (Exception e) {
97                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
98                 }
99
100                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
101         }
102
103         @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
104         public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
105                         throws Exception {
106                 List<MicroserviceData> list = microserviceService.getMicroserviceData();
107                 return list;
108         }
109
110         @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.PUT)
111         public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
112                         @PathVariable("serviceId") long serviceId, @RequestBody MicroserviceData newServiceData) throws Exception {
113
114                 if (newServiceData == null) {
115                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
116                                         "MicroserviceData cannot be null or empty");
117                 }
118                 try {
119                         microserviceService.updateMicroservice(serviceId, newServiceData);
120                 } catch (Exception e) {
121                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
122                 }
123                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
124         }
125         
126         @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.DELETE)
127         public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
128                         @PathVariable("serviceId") long serviceId) throws Exception {
129                 try {
130                         ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
131                         };
132                         // If this service is assoicated with widgets, cannnot be deleted
133                         ResponseEntity<List<WidgetCatalog>> ans = (ResponseEntity<List<WidgetCatalog>>) template.exchange(
134                                         EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
135                                                         + "/widget/microservices/widgetCatalog/service/" + serviceId,
136                                         HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef);
137                         List<WidgetCatalog> widgets = ans.getBody();
138                         if(widgets.size() == 0)
139                                 microserviceService.deleteMicroservice(serviceId);
140                         else{
141                                 StringBuilder sb = new StringBuilder();
142                                 for(int i = 0; i < widgets.size(); i++){
143                                         sb.append("'" + widgets.get(i).getName() + "' ");
144                                         if(i < (widgets.size()-1)){
145                                                 sb.append(",");
146                                         }
147                                 }
148                                 return new PortalRestResponse<String>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE", sb.toString());
149                         }
150                 } catch (Exception e) {
151                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
152                 }
153                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
154         }
155
156 }