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;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
46 import javax.validation.ConstraintViolation;
47 import javax.validation.Valid;
48 import javax.validation.Validation;
49 import javax.validation.Validator;
50 import javax.validation.ValidatorFactory;
51 import org.onap.portalapp.controller.EPRestrictedBaseController;
52 import org.onap.portalapp.portal.domain.MicroserviceData;
53 import org.onap.portalapp.portal.domain.WidgetCatalog;
54 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
55 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
56 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
57 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
58 import org.onap.portalapp.portal.service.ConsulHealthService;
59 import org.onap.portalapp.portal.service.MicroserviceService;
60 import org.onap.portalapp.portal.utils.EcompPortalUtils;
61 import org.onap.portalsdk.core.util.SystemProperties;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.context.annotation.EnableAspectJAutoProxy;
64 import org.springframework.core.ParameterizedTypeReference;
65 import org.springframework.http.HttpEntity;
66 import org.springframework.http.HttpMethod;
67 import org.springframework.http.ResponseEntity;
68 import org.springframework.web.bind.annotation.PathVariable;
69 import org.springframework.web.bind.annotation.RequestBody;
70 import org.springframework.web.bind.annotation.RequestMapping;
71 import org.springframework.web.bind.annotation.RequestMethod;
72 import org.springframework.web.bind.annotation.RestController;
73 import org.springframework.web.client.RestTemplate;
75 @SuppressWarnings("unchecked")
77 @org.springframework.context.annotation.Configuration
78 @EnableAspectJAutoProxy
80 public class MicroserviceController extends EPRestrictedBaseController {
81 public static final ValidatorFactory VALIDATOR_FACTORY = Validation.buildDefaultValidatorFactory();
83 String whatService = "widgets-service";
84 RestTemplate template = new RestTemplate();
87 private ConsulHealthService consulHealthService;
90 private MicroserviceService microserviceService;
92 @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.POST)
93 public PortalRestResponse<String> createMicroservice(HttpServletRequest request, HttpServletResponse response,
94 @Valid @RequestBody MicroserviceData newServiceData) throws Exception {
95 if (newServiceData == null) {
96 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
97 "MicroserviceData cannot be null or empty");
99 Validator validator = VALIDATOR_FACTORY.getValidator();
101 Set<ConstraintViolation<MicroserviceData>> constraintViolations = validator.validate(newServiceData);
102 if(!constraintViolations.isEmpty()){
103 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
104 "ERROR", "MicroserviceData is not valid");
107 long serviceId = microserviceService.saveMicroservice(newServiceData);
110 microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
111 } catch (Exception e) {
112 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
115 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
118 @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
119 public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
121 return microserviceService.getMicroserviceData();
124 @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.PUT)
125 public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
126 @PathVariable("serviceId") long serviceId, @Valid @RequestBody MicroserviceData newServiceData) {
128 if (newServiceData == null) {
129 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
130 "MicroserviceData cannot be null or empty");
132 Validator validator = VALIDATOR_FACTORY.getValidator();
134 Set<ConstraintViolation<MicroserviceData>> constraintViolations = validator.validate(newServiceData);
135 if(!constraintViolations.isEmpty()){
136 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
137 "ERROR", "MicroserviceData is not valid");
141 microserviceService.updateMicroservice(serviceId, newServiceData);
142 } catch (Exception e) {
143 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
145 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
148 @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.DELETE)
149 public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
150 @PathVariable("serviceId") long serviceId) {
152 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
154 // If this service is assoicated with widgets, cannnot be deleted
155 ResponseEntity<List<WidgetCatalog>> ans = template.exchange(
156 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
157 + "/widget/microservices/widgetCatalog/service/" + serviceId,
158 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef);
159 List<WidgetCatalog> widgets = ans.getBody();
160 if(widgets.size() == 0)
161 microserviceService.deleteMicroservice(serviceId);
163 StringBuilder sb = new StringBuilder();
164 for(int i = 0; i < widgets.size(); i++){
165 sb.append("'").append(widgets.get(i).getName()).append("' ");
166 if(i < (widgets.size()-1)){
170 return new PortalRestResponse<>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE",
173 } catch (Exception e) {
174 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
176 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");