2 * ================================================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
20 package org.openecomp.portalapp.portal.controller;
\r
22 import java.util.ArrayList;
\r
23 import java.util.List;
\r
25 import javax.servlet.http.HttpServletRequest;
\r
26 import javax.servlet.http.HttpServletResponse;
\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
52 @SuppressWarnings("unchecked")
\r
54 @org.springframework.context.annotation.Configuration
\r
55 @EnableAspectJAutoProxy
\r
57 public class MicroserviceController extends EPRestrictedBaseController {
\r
59 private static final String HTTPS = "https://";
\r
61 String whatService = "widgets-service";
\r
62 RestTemplate template = new RestTemplate();
\r
65 private ConsulHealthService consulHealthService;
\r
68 private MicroserviceService microserviceService;
\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
77 long serviceId = microserviceService.saveMicroservice(newServiceData);
\r
80 microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
\r
81 } catch (Exception e) {
\r
82 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
\r
85 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
\r
88 @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
\r
89 public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
\r
91 List<MicroserviceData> list = microserviceService.getMicroserviceData();
\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
99 if (newServiceData == null) {
\r
100 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
\r
101 "MicroserviceData cannot be null or empty");
\r
104 microserviceService.updateMicroservice(serviceId, newServiceData);
\r
105 } catch (Exception e) {
\r
106 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
\r
108 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
\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
115 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
\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
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
133 return new PortalRestResponse<String>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE", sb.toString());
\r
135 } catch (Exception e) {
\r
136 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
\r
138 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
\r