[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / MicroserviceController.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import java.util.List;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
28 import org.openecomp.portalapp.portal.domain.MicroserviceData;
29 import org.openecomp.portalapp.portal.domain.WidgetCatalog;
30 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
31 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
32 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
33 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
34 import org.openecomp.portalapp.portal.service.ConsulHealthService;
35 import org.openecomp.portalapp.portal.service.MicroserviceService;
36 import org.openecomp.portalsdk.core.util.SystemProperties;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.annotation.EnableAspectJAutoProxy;
39 import org.springframework.core.ParameterizedTypeReference;
40 import org.springframework.http.HttpEntity;
41 import org.springframework.http.HttpMethod;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.bind.annotation.RestController;
48 import org.springframework.web.client.RestTemplate;
49
50 @SuppressWarnings("unchecked")
51 @RestController
52 @org.springframework.context.annotation.Configuration
53 @EnableAspectJAutoProxy
54 @EPAuditLog
55 public class MicroserviceController extends EPRestrictedBaseController {
56         
57         private static final String HTTPS = "https://";
58         
59         String whatService = "widgets-service";
60         RestTemplate template = new RestTemplate();
61
62         @Autowired
63         private ConsulHealthService consulHealthService;
64
65         @Autowired
66         private MicroserviceService microserviceService;
67         
68         @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.POST)
69         public PortalRestResponse<String> createMicroservice(HttpServletRequest request, HttpServletResponse response,
70                         @RequestBody MicroserviceData newServiceData) throws Exception {
71                 if (newServiceData == null) {
72                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
73                                         "MicroserviceData cannot be null or empty");
74                 }
75                 long serviceId = microserviceService.saveMicroservice(newServiceData);
76
77                 try {
78                         microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
79                 } catch (Exception e) {
80                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
81                 }
82
83                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
84         }
85
86         @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
87         public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
88                         throws Exception {
89                 List<MicroserviceData> list = microserviceService.getMicroserviceData();
90                 return list;
91         }
92
93         @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.PUT)
94         public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
95                         @PathVariable("serviceId") long serviceId, @RequestBody MicroserviceData newServiceData) throws Exception {
96
97                 if (newServiceData == null) {
98                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
99                                         "MicroserviceData cannot be null or empty");
100                 }
101                 try {
102                         microserviceService.updateMicroservice(serviceId, newServiceData);
103                 } catch (Exception e) {
104                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
105                 }
106                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
107         }
108         
109         @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.DELETE)
110         public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
111                         @PathVariable("serviceId") long serviceId) throws Exception {
112                 try {
113                         ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
114                         };
115                         // If this service is assoicated with widgets, cannnot be deleted
116                         ResponseEntity<List<WidgetCatalog>> ans = (ResponseEntity<List<WidgetCatalog>>) template.exchange(
117                                         HTTPS + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
118                                                         + "/widget/microservices/widgetCatalog/service/" + serviceId,
119                                         HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef);
120                         List<WidgetCatalog> widgets = ans.getBody();
121                         if(widgets.size() == 0)
122                                 microserviceService.deleteMicroservice(serviceId);
123                         else{
124                                 StringBuilder sb = new StringBuilder();
125                                 for(int i = 0; i < widgets.size(); i++){
126                                         sb.append("'" + widgets.get(i).getName() + "' ");
127                                         if(i < (widgets.size()-1)){
128                                                 sb.append(",");
129                                         }
130                                 }
131                                 return new PortalRestResponse<String>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE", sb.toString());
132                         }
133                 } catch (Exception e) {
134                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
135                 }
136                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
137         }
138
139 }