2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import java.util.List;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
45 import org.onap.portalapp.controller.EPRestrictedBaseController;
46 import org.onap.portalapp.portal.domain.MicroserviceData;
47 import org.onap.portalapp.portal.domain.WidgetCatalog;
48 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
49 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
50 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
51 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
52 import org.onap.portalapp.portal.service.ConsulHealthService;
53 import org.onap.portalapp.portal.service.MicroserviceService;
54 import org.onap.portalapp.portal.utils.EcompPortalUtils;
55 import org.onap.portalsdk.core.util.SystemProperties;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.context.annotation.EnableAspectJAutoProxy;
58 import org.springframework.core.ParameterizedTypeReference;
59 import org.springframework.http.HttpEntity;
60 import org.springframework.http.HttpMethod;
61 import org.springframework.http.ResponseEntity;
62 import org.springframework.web.bind.annotation.PathVariable;
63 import org.springframework.web.bind.annotation.RequestBody;
64 import org.springframework.web.bind.annotation.RequestMapping;
65 import org.springframework.web.bind.annotation.RequestMethod;
66 import org.springframework.web.bind.annotation.RestController;
67 import org.springframework.web.client.RestTemplate;
69 @SuppressWarnings("unchecked")
71 @org.springframework.context.annotation.Configuration
72 @EnableAspectJAutoProxy
74 public class MicroserviceController extends EPRestrictedBaseController {
76 String whatService = "widgets-service";
77 RestTemplate template = new RestTemplate();
80 private ConsulHealthService consulHealthService;
83 private MicroserviceService microserviceService;
85 @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.POST)
86 public PortalRestResponse<String> createMicroservice(HttpServletRequest request, HttpServletResponse response,
87 @RequestBody MicroserviceData newServiceData) throws Exception {
88 if (newServiceData == null) {
89 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
90 "MicroserviceData cannot be null or empty");
92 long serviceId = microserviceService.saveMicroservice(newServiceData);
95 microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
96 } catch (Exception e) {
97 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
100 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
103 @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
104 public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
106 List<MicroserviceData> list = microserviceService.getMicroserviceData();
110 @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.PUT)
111 public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
112 @PathVariable("serviceId") long serviceId, @RequestBody MicroserviceData newServiceData) throws Exception {
114 if (newServiceData == null) {
115 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
116 "MicroserviceData cannot be null or empty");
119 microserviceService.updateMicroservice(serviceId, newServiceData);
120 } catch (Exception e) {
121 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
123 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
126 @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.DELETE)
127 public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
128 @PathVariable("serviceId") long serviceId) throws Exception {
130 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
132 // If this service is assoicated with widgets, cannnot be deleted
133 ResponseEntity<List<WidgetCatalog>> ans = (ResponseEntity<List<WidgetCatalog>>) template.exchange(
134 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
135 + "/widget/microservices/widgetCatalog/service/" + serviceId,
136 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef);
137 List<WidgetCatalog> widgets = ans.getBody();
138 if(widgets.size() == 0)
139 microserviceService.deleteMicroservice(serviceId);
141 StringBuilder sb = new StringBuilder();
142 for(int i = 0; i < widgets.size(); i++){
143 sb.append("'" + widgets.get(i).getName() + "' ");
144 if(i < (widgets.size()-1)){
148 return new PortalRestResponse<String>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE", sb.toString());
150 } catch (Exception e) {
151 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
153 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");