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.portalapp.portal.utils.EcompPortalUtils;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.EnableAspectJAutoProxy;
40 import org.springframework.core.ParameterizedTypeReference;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestMethod;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.springframework.web.client.RestTemplate;
51 @SuppressWarnings("unchecked")
53 @org.springframework.context.annotation.Configuration
54 @EnableAspectJAutoProxy
56 public class MicroserviceController extends EPRestrictedBaseController {
58 String whatService = "widgets-service";
59 RestTemplate template = new RestTemplate();
62 private ConsulHealthService consulHealthService;
65 private MicroserviceService microserviceService;
67 @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.POST)
68 public PortalRestResponse<String> createMicroservice(HttpServletRequest request, HttpServletResponse response,
69 @RequestBody MicroserviceData newServiceData) throws Exception {
70 if (newServiceData == null) {
71 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
72 "MicroserviceData cannot be null or empty");
74 long serviceId = microserviceService.saveMicroservice(newServiceData);
77 microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
78 } catch (Exception e) {
79 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
82 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
85 @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
86 public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
88 List<MicroserviceData> list = microserviceService.getMicroserviceData();
92 @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.PUT)
93 public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
94 @PathVariable("serviceId") long serviceId, @RequestBody MicroserviceData newServiceData) throws Exception {
96 if (newServiceData == null) {
97 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
98 "MicroserviceData cannot be null or empty");
101 microserviceService.updateMicroservice(serviceId, newServiceData);
102 } catch (Exception e) {
103 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
105 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
108 @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.DELETE)
109 public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
110 @PathVariable("serviceId") long serviceId) throws Exception {
112 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
114 // If this service is assoicated with widgets, cannnot be deleted
115 ResponseEntity<List<WidgetCatalog>> ans = (ResponseEntity<List<WidgetCatalog>>) template.exchange(
116 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
117 + "/widget/microservices/widgetCatalog/service/" + serviceId,
118 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef);
119 List<WidgetCatalog> widgets = ans.getBody();
120 if(widgets.size() == 0)
121 microserviceService.deleteMicroservice(serviceId);
123 StringBuilder sb = new StringBuilder();
124 for(int i = 0; i < widgets.size(); i++){
125 sb.append("'" + widgets.get(i).getName() + "' ");
126 if(i < (widgets.size()-1)){
130 return new PortalRestResponse<String>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE", sb.toString());
132 } catch (Exception e) {
133 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
135 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");