2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modification Copyright © 2020 IBM.
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
40 package org.onap.portalapp.portal.controller;
42 import java.util.List;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import javax.validation.ConstraintViolation;
49 import javax.validation.Valid;
50 import javax.validation.Validation;
51 import javax.validation.Validator;
52 import javax.validation.ValidatorFactory;
53 import org.onap.portalapp.controller.EPRestrictedBaseController;
54 import org.onap.portalapp.portal.domain.MicroserviceData;
55 import org.onap.portalapp.portal.domain.WidgetCatalog;
56 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
57 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
58 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
59 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
60 import org.onap.portalapp.portal.service.WidgetMService;
61 import org.onap.portalapp.portal.service.MicroserviceService;
62 import org.onap.portalapp.portal.utils.EcompPortalUtils;
63 import org.onap.portalapp.validation.DataValidator;
64 import org.onap.portalsdk.core.util.SystemProperties;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.context.annotation.EnableAspectJAutoProxy;
67 import org.springframework.core.ParameterizedTypeReference;
68 import org.springframework.http.HttpEntity;
69 import org.springframework.http.HttpMethod;
70 import org.springframework.http.ResponseEntity;
71 import org.springframework.web.bind.annotation.PathVariable;
72 import org.springframework.web.bind.annotation.RequestBody;
73 import org.springframework.web.bind.annotation.RequestMapping;
74 import org.springframework.web.bind.annotation.GetMapping;
75 import org.springframework.web.bind.annotation.PostMapping;
76 import org.springframework.web.bind.annotation.PutMapping;
77 import org.springframework.web.bind.annotation.DeleteMapping;
78 import org.springframework.web.bind.annotation.RequestMethod;
79 import org.springframework.web.bind.annotation.RestController;
80 import org.springframework.web.client.RestTemplate;
82 @SuppressWarnings("unchecked")
84 @org.springframework.context.annotation.Configuration
85 @EnableAspectJAutoProxy
87 public class MicroserviceController extends EPRestrictedBaseController {
88 private final DataValidator dataValidator = new DataValidator();
90 String whatService = "widgets-service";
91 RestTemplate template = new RestTemplate();
94 private WidgetMService widgetMService;
97 private MicroserviceService microserviceService;
99 @PostMapping(value = { "/portalApi/microservices" })
100 public PortalRestResponse<String> createMicroservice(HttpServletRequest request, HttpServletResponse response,
101 @Valid @RequestBody MicroserviceData newServiceData) throws Exception {
102 if (newServiceData == null) {
103 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
104 "MicroserviceData cannot be null or empty");
106 if(!dataValidator.isValid(newServiceData)){
107 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
108 "ERROR", "MicroserviceData is not valid");
111 long serviceId = microserviceService.saveMicroservice(newServiceData);
114 microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
115 } catch (Exception e) {
116 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
119 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
122 @GetMapping(value = { "/portalApi/microservices" })
123 public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
125 return microserviceService.getMicroserviceData();
128 @PutMapping(value = { "/portalApi/microservices/{serviceId}" })
129 public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
130 @PathVariable("serviceId") long serviceId, @Valid @RequestBody MicroserviceData newServiceData) {
132 if (newServiceData == null) {
133 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
134 "MicroserviceData cannot be null or empty");
136 if(!dataValidator.isValid(newServiceData)){
137 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
138 "ERROR", "MicroserviceData is not valid");
142 microserviceService.updateMicroservice(serviceId, newServiceData);
143 } catch (Exception e) {
144 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
146 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
149 @DeleteMapping(value = { "/portalApi/microservices/{serviceId}" })
150 public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
151 @PathVariable("serviceId") long serviceId) {
153 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
155 // If this service is assoicated with widgets, cannnot be deleted
156 ResponseEntity<List<WidgetCatalog>> ans = template.exchange(
157 EcompPortalUtils.widgetMsProtocol() + "://" + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
158 + "/widget/microservices/widgetCatalog/service/" + serviceId,
159 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef);
160 List<WidgetCatalog> widgets = ans.getBody();
161 if(widgets.size() == 0)
162 microserviceService.deleteMicroservice(serviceId);
164 StringBuilder sb = new StringBuilder();
165 for(int i = 0; i < widgets.size(); i++){
166 sb.append("'").append(widgets.get(i).getName()).append("' ");
167 if(i < (widgets.size()-1)){
171 return new PortalRestResponse<>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE",
174 } catch (Exception e) {
175 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
177 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");