Portal Spring Boot Development
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / 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.portalapp.portal.controller;
39
40 import java.util.List;
41
42 import java.util.Set;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
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;
74
75 @SuppressWarnings("unchecked")
76 @RestController
77 @org.springframework.context.annotation.Configuration
78 @EnableAspectJAutoProxy
79 @EPAuditLog
80 public class MicroserviceController extends EPRestrictedBaseController {
81         public static final ValidatorFactory VALIDATOR_FACTORY = Validation.buildDefaultValidatorFactory();
82         
83         String whatService = "widgets-service";
84         RestTemplate template = new RestTemplate();
85
86         @Autowired
87         private ConsulHealthService consulHealthService;
88
89         @Autowired
90         private MicroserviceService microserviceService;
91
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");
98                 }else {
99                         Validator validator = VALIDATOR_FACTORY.getValidator();
100
101                         Set<ConstraintViolation<MicroserviceData>> constraintViolations = validator.validate(newServiceData);
102                         if(!constraintViolations.isEmpty()){
103                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
104                                         "ERROR", "MicroserviceData is not valid");
105                         }
106                 }
107                 long serviceId = microserviceService.saveMicroservice(newServiceData);
108
109                 try {
110                         microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
111                 } catch (Exception e) {
112                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
113                 }
114
115                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
116         }
117
118         @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
119         public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
120                         throws Exception {
121                 return microserviceService.getMicroserviceData();
122         }
123
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) {
127
128                 if (newServiceData == null) {
129                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
130                                 "MicroserviceData cannot be null or empty");
131                 }else {
132                         Validator validator = VALIDATOR_FACTORY.getValidator();
133
134                         Set<ConstraintViolation<MicroserviceData>> constraintViolations = validator.validate(newServiceData);
135                         if(!constraintViolations.isEmpty()){
136                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
137                                         "ERROR", "MicroserviceData is not valid");
138                         }
139                 }
140                 try {
141                         microserviceService.updateMicroservice(serviceId, newServiceData);
142                 } catch (Exception e) {
143                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
144                 }
145                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
146         }
147         
148         @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.DELETE)
149         public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
150                         @PathVariable("serviceId") long serviceId) {
151                 try {
152                         ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
153                         };
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);
162                         else{
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)){
167                                                 sb.append(",");
168                                         }
169                                 }
170                                 return new PortalRestResponse<>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE",
171                                         sb.toString());
172                         }
173                 } catch (Exception e) {
174                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
175                 }
176                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
177         }
178
179 }