2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.portal.controller;
22 import java.util.List;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
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;
50 @SuppressWarnings("unchecked")
52 @org.springframework.context.annotation.Configuration
53 @EnableAspectJAutoProxy
55 public class MicroserviceController extends EPRestrictedBaseController {
57 private static final String HTTPS = "https://";
59 String whatService = "widgets-service";
60 RestTemplate template = new RestTemplate();
63 private ConsulHealthService consulHealthService;
66 private MicroserviceService microserviceService;
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");
75 long serviceId = microserviceService.saveMicroservice(newServiceData);
78 microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
79 } catch (Exception e) {
80 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
83 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
86 @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
87 public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
89 List<MicroserviceData> list = microserviceService.getMicroserviceData();
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 {
97 if (newServiceData == null) {
98 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
99 "MicroserviceData cannot be null or empty");
102 microserviceService.updateMicroservice(serviceId, newServiceData);
103 } catch (Exception e) {
104 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
106 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
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 {
113 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
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);
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)){
131 return new PortalRestResponse<String>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE", sb.toString());
133 } catch (Exception e) {
134 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
136 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");