MicroserviceController up
[portal.git] / portal-BE / src / main / java / org / onap / portal / controller / MicroserviceController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  *
37  */
38 package org.onap.portal.controller;
39
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;
62
63 @SuppressWarnings("unchecked")
64 @RestController
65 @Configuration
66 @EnableAspectJAutoProxy
67 public class MicroserviceController {
68
69     private final DataValidator dataValidator = new DataValidator();
70
71     private final PortalWMSTemplate template;
72     private final EpMicroserviceService microserviceService;
73
74     @Autowired
75     public MicroserviceController(PortalWMSTemplate template, EpMicroserviceService microserviceService) {
76         this.template = template;
77         this.microserviceService = microserviceService;
78     }
79
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");
86         } else {
87             if (!dataValidator.isValid(newServiceData)) {
88                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
89                     "ERROR", "MicroserviceData is not valid");
90             }
91         }
92         EpMicroservice serviceId = microserviceService.saveOne(newServiceData);
93         try {
94             microserviceService.saveServiceParameters(serviceId.getId(), newServiceData.getEpMicroserviceParameters());
95         } catch (Exception e) {
96             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
97         }
98         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
99     }
100
101     @RequestMapping(value = {"/portalApi/microservices"}, method = RequestMethod.GET)
102     public List<EpMicroservice> getMicroservice(HttpServletRequest request, HttpServletResponse response) {
103         return microserviceService.getAll();
104     }
105
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) {
109
110         if (newServiceData == null) {
111             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
112                 "MicroserviceData cannot be null or empty");
113         } else {
114             if (!dataValidator.isValid(newServiceData)) {
115                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
116                     "ERROR", "MicroserviceData is not valid");
117             }
118         }
119         try {
120             microserviceService.updateMicroservice(serviceId, newServiceData);
121         } catch (Exception e) {
122             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
123         }
124         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
125     }
126
127     @RequestMapping(value = {"/portalApi/microservices/{serviceId}"}, method = RequestMethod.DELETE)
128     public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
129         @PathVariable("serviceId") long serviceId) {
130         try {
131             ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
132             };
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);
138             } else {
139                 String sb = widgets.stream().map(WidgetCatalog::getName).collect(Collectors.joining("' "));
140                 return new PortalRestResponse<>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE",
141                     sb);
142             }
143         } catch (Exception e) {
144             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
145         }
146         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
147     }
148 }