a80a3b42285d19665fca05a3a6499dc790b6fe38
[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 javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
47 import javax.validation.Valid;
48 import org.onap.portalapp.controller.EPRestrictedBaseController;
49 import org.onap.portalapp.portal.domain.MicroserviceData;
50 import org.onap.portalapp.portal.domain.WidgetCatalog;
51 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
52 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
53 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
54 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
55 import org.onap.portalapp.portal.service.WidgetMService;
56 import org.onap.portalapp.portal.service.MicroserviceService;
57 import org.onap.portalapp.portal.utils.EcompPortalUtils;
58 import org.onap.portalapp.validation.DataValidator;
59 import org.onap.portalsdk.core.util.SystemProperties;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.context.annotation.EnableAspectJAutoProxy;
62 import org.springframework.core.ParameterizedTypeReference;
63 import org.springframework.http.HttpEntity;
64 import org.springframework.http.HttpMethod;
65 import org.springframework.http.ResponseEntity;
66 import org.springframework.web.bind.annotation.PathVariable;
67 import org.springframework.web.bind.annotation.RequestBody;
68 import org.springframework.web.bind.annotation.GetMapping;
69 import org.springframework.web.bind.annotation.PostMapping;
70 import org.springframework.web.bind.annotation.PutMapping;
71 import org.springframework.web.bind.annotation.DeleteMapping;
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         private final DataValidator dataValidator = new DataValidator();
82         
83         String whatService = "widgets-service";
84         RestTemplate template = new RestTemplate();
85
86         @Autowired
87         private WidgetMService widgetMService;
88
89         @Autowired
90         private MicroserviceService microserviceService;
91
92         @PostMapping(value = { "/portalApi/microservices" })
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                         if(!dataValidator.isValid(newServiceData)){
100                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
101                                         "ERROR", "MicroserviceData is not valid");
102                         }
103                 }
104                 long serviceId = microserviceService.saveMicroservice(newServiceData);
105
106                 try {
107                         microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
108                 } catch (Exception e) {
109                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
110                 }
111
112                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
113         }
114
115         @GetMapping(value = { "/portalApi/microservices" })
116         public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
117                         throws Exception {
118                 return microserviceService.getMicroserviceData();
119         }
120
121         @PutMapping(value = { "/portalApi/microservices/{serviceId}" })
122         public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
123                         @PathVariable("serviceId") long serviceId, @Valid @RequestBody MicroserviceData newServiceData) {
124
125                 if (newServiceData == null) {
126                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE",
127                                 "MicroserviceData cannot be null or empty");
128                 }else {
129                         if(!dataValidator.isValid(newServiceData)){
130                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
131                                         "ERROR", "MicroserviceData is not valid");
132                         }
133                 }
134                 try {
135                         microserviceService.updateMicroservice(serviceId, newServiceData);
136                 } catch (Exception e) {
137                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
138                 }
139                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
140         }
141         
142         @DeleteMapping(value = { "/portalApi/microservices/{serviceId}" })
143         public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
144                         @PathVariable("serviceId") long serviceId) {
145                 try {
146                         ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
147                         };
148                         // If this service is assoicated with widgets, cannnot be deleted
149                         ResponseEntity<List<WidgetCatalog>> ans = template.exchange(
150                                         EcompPortalUtils.widgetMsProtocol() + "://" + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
151                                                         + "/widget/microservices/widgetCatalog/service/" + serviceId,
152                                         HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef);
153                         List<WidgetCatalog> widgets = ans.getBody();
154                         if(widgets.size() == 0)
155                                 microserviceService.deleteMicroservice(serviceId);
156                         else{
157                                 StringBuilder sb = new StringBuilder();
158                                 for(int i = 0; i < widgets.size(); i++){
159                                         sb.append("'").append(widgets.get(i).getName()).append("' ");
160                                         if(i < (widgets.size()-1)){
161                                                 sb.append(",");
162                                         }
163                                 }
164                                 return new PortalRestResponse<>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE",
165                                         sb.toString());
166                         }
167                 } catch (Exception e) {
168                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
169                 }
170                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
171         }
172
173 }