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.portal.controller;
40 import java.util.List;
41 import java.util.stream.Collectors;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import javax.validation.Valid;
45 import org.onap.portal.domain.db.ep.EpMicroservice;
46 import org.onap.portal.domain.dto.PortalRestResponse;
47 import org.onap.portal.domain.dto.PortalRestStatusEnum;
48 import org.onap.portal.domain.dto.ecomp.WidgetCatalog;
49 import org.onap.portal.restTemplates.PortalWMSTemplate;
50 import org.onap.portal.service.microservice.EpMicroserviceService;
51 import org.onap.portal.validation.DataValidator;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.context.annotation.Configuration;
54 import org.springframework.context.annotation.EnableAspectJAutoProxy;
55 import org.springframework.core.ParameterizedTypeReference;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.web.bind.annotation.PathVariable;
58 import org.springframework.web.bind.annotation.RequestBody;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestMethod;
61 import org.springframework.web.bind.annotation.RestController;
63 @SuppressWarnings("unchecked")
66 @EnableAspectJAutoProxy
67 public class MicroserviceController {
69 private final DataValidator dataValidator = new DataValidator();
71 private final PortalWMSTemplate template;
72 private final EpMicroserviceService microserviceService;
75 public MicroserviceController(PortalWMSTemplate template, EpMicroserviceService microserviceService) {
76 this.template = template;
77 this.microserviceService = microserviceService;
80 @RequestMapping(value = {"/portalApi/microservices"}, method = RequestMethod.POST)
81 public PortalRestResponse<String> createMicroservice(HttpServletRequest request, HttpServletResponse response,
82 @Valid @RequestBody EpMicroservice newServiceData) {
83 if (newServiceData == null) {
84 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
85 "MicroserviceData cannot be null or empty");
87 if (!dataValidator.isValid(newServiceData)) {
88 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
89 "ERROR", "MicroserviceData is not valid");
92 EpMicroservice serviceId = microserviceService.saveOne(newServiceData);
94 microserviceService.saveServiceParameters(serviceId.getId(), newServiceData.getEpMicroserviceParameters());
95 } catch (Exception e) {
96 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
98 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
101 @RequestMapping(value = {"/portalApi/microservices"}, method = RequestMethod.GET)
102 public List<EpMicroservice> getMicroservice(HttpServletRequest request, HttpServletResponse response) {
103 return microserviceService.getAll();
106 @RequestMapping(value = {"/portalApi/microservices/{serviceId}"}, method = RequestMethod.PUT)
107 public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
108 @PathVariable("serviceId") long serviceId, @Valid @RequestBody EpMicroservice newServiceData) {
110 if (newServiceData == null) {
111 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
112 "MicroserviceData cannot be null or empty");
114 if (!dataValidator.isValid(newServiceData)) {
115 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
116 "ERROR", "MicroserviceData is not valid");
120 microserviceService.updateMicroservice(serviceId, newServiceData);
121 } catch (Exception e) {
122 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
124 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
127 @RequestMapping(value = {"/portalApi/microservices/{serviceId}"}, method = RequestMethod.DELETE)
128 public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
129 @PathVariable("serviceId") long serviceId) {
131 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
133 // If this service is assoicated with widgets, cannnot be deleted
134 ResponseEntity<List<WidgetCatalog>> ans = template.getWidgets(serviceId, typeRef);
135 List<WidgetCatalog> widgets = ans.getBody();
136 if (widgets.size() == 0) {
137 microserviceService.deleteById(serviceId);
139 String sb = widgets.stream().map(WidgetCatalog::getName).collect(Collectors.joining("' "));
140 return new PortalRestResponse<>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE",
143 } catch (Exception e) {
144 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
146 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");