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