lowered code smells
[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  *  Modification Copyright © 2020 IBM.
8  * ===================================================================
9  *
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
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
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
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
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.
35  *
36  * ============LICENSE_END============================================
37  *
38  * 
39  */
40 package org.onap.portalapp.portal.controller;
41
42 import java.util.List;
43
44 import java.util.Set;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
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;
81
82 @SuppressWarnings("unchecked")
83 @RestController
84 @org.springframework.context.annotation.Configuration
85 @EnableAspectJAutoProxy
86 @EPAuditLog
87 public class MicroserviceController extends EPRestrictedBaseController {
88         private final DataValidator dataValidator = new DataValidator();
89         
90         String whatService = "widgets-service";
91         RestTemplate template = new RestTemplate();
92
93         @Autowired
94         private WidgetMService widgetMService;
95
96         @Autowired
97         private MicroserviceService microserviceService;
98
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");
105                 }else {
106                         if(!dataValidator.isValid(newServiceData)){
107                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
108                                         "ERROR", "MicroserviceData is not valid");
109                         }
110                 }
111                 long serviceId = microserviceService.saveMicroservice(newServiceData);
112
113                 try {
114                         microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
115                 } catch (Exception e) {
116                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
117                 }
118
119                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
120         }
121
122         @GetMapping(value = { "/portalApi/microservices" })
123         public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
124                         throws Exception {
125                 return microserviceService.getMicroserviceData();
126         }
127
128         @PutMapping(value = { "/portalApi/microservices/{serviceId}" })
129         public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
130                         @PathVariable("serviceId") long serviceId, @Valid @RequestBody MicroserviceData newServiceData) {
131
132                 if (newServiceData == null) {
133                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
134                                 "MicroserviceData cannot be null or empty");
135                 }else {
136                         if(!dataValidator.isValid(newServiceData)){
137                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
138                                         "ERROR", "MicroserviceData is not valid");
139                         }
140                 }
141                 try {
142                         microserviceService.updateMicroservice(serviceId, newServiceData);
143                 } catch (Exception e) {
144                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
145                 }
146                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
147         }
148         
149         @DeleteMapping(value = { "/portalApi/microservices/{serviceId}" })
150         public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
151                         @PathVariable("serviceId") long serviceId) {
152                 try {
153                         ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
154                         };
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);
163                         else{
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)){
168                                                 sb.append(",");
169                                         }
170                                 }
171                                 return new PortalRestResponse<>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE",
172                                         sb.toString());
173                         }
174                 } catch (Exception e) {
175                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
176                 }
177                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
178         }
179
180 }