42a62b5f686d4a3c9931646cd0c11cfc7d373716
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / MicroserviceController.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import java.util.List;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
28 import org.openecomp.portalapp.portal.domain.MicroserviceData;
29 import org.openecomp.portalapp.portal.domain.WidgetCatalog;
30 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
31 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
32 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
33 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
34 import org.openecomp.portalapp.portal.service.ConsulHealthService;
35 import org.openecomp.portalapp.portal.service.MicroserviceService;
36 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.EnableAspectJAutoProxy;
40 import org.springframework.core.ParameterizedTypeReference;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestMethod;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.springframework.web.client.RestTemplate;
50
51 @SuppressWarnings("unchecked")
52 @RestController
53 @org.springframework.context.annotation.Configuration
54 @EnableAspectJAutoProxy
55 @EPAuditLog
56 public class MicroserviceController extends EPRestrictedBaseController {
57         
58         String whatService = "widgets-service";
59         RestTemplate template = new RestTemplate();
60
61         @Autowired
62         private ConsulHealthService consulHealthService;
63
64         @Autowired
65         private MicroserviceService microserviceService;
66
67         @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.POST)
68         public PortalRestResponse<String> createMicroservice(HttpServletRequest request, HttpServletResponse response,
69                         @RequestBody MicroserviceData newServiceData) throws Exception {
70                 if (newServiceData == null) {
71                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
72                                         "MicroserviceData cannot be null or empty");
73                 }
74                 long serviceId = microserviceService.saveMicroservice(newServiceData);
75
76                 try {
77                         microserviceService.saveServiceParameters(serviceId, newServiceData.getParameterList());
78                 } catch (Exception e) {
79                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
80                 }
81
82                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
83         }
84
85         @RequestMapping(value = { "/portalApi/microservices" }, method = RequestMethod.GET)
86         public List<MicroserviceData> getMicroservice(HttpServletRequest request, HttpServletResponse response)
87                         throws Exception {
88                 List<MicroserviceData> list = microserviceService.getMicroserviceData();
89                 return list;
90         }
91
92         @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.PUT)
93         public PortalRestResponse<String> updateMicroservice(HttpServletRequest request, HttpServletResponse response,
94                         @PathVariable("serviceId") long serviceId, @RequestBody MicroserviceData newServiceData) throws Exception {
95
96                 if (newServiceData == null) {
97                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
98                                         "MicroserviceData cannot be null or empty");
99                 }
100                 try {
101                         microserviceService.updateMicroservice(serviceId, newServiceData);
102                 } catch (Exception e) {
103                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
104                 }
105                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
106         }
107         
108         @RequestMapping(value = { "/portalApi/microservices/{serviceId}" }, method = RequestMethod.DELETE)
109         public PortalRestResponse<String> deleteMicroservice(HttpServletRequest request, HttpServletResponse response,
110                         @PathVariable("serviceId") long serviceId) throws Exception {
111                 try {
112                         ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
113                         };
114                         // If this service is assoicated with widgets, cannnot be deleted
115                         ResponseEntity<List<WidgetCatalog>> ans = (ResponseEntity<List<WidgetCatalog>>) template.exchange(
116                                         EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
117                                                         + "/widget/microservices/widgetCatalog/service/" + serviceId,
118                                         HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef);
119                         List<WidgetCatalog> widgets = ans.getBody();
120                         if(widgets.size() == 0)
121                                 microserviceService.deleteMicroservice(serviceId);
122                         else{
123                                 StringBuilder sb = new StringBuilder();
124                                 for(int i = 0; i < widgets.size(); i++){
125                                         sb.append("'" + widgets.get(i).getName() + "' ");
126                                         if(i < (widgets.size()-1)){
127                                                 sb.append(",");
128                                         }
129                                 }
130                                 return new PortalRestResponse<String>(PortalRestStatusEnum.WARN, "SOME WIDGETS ASSOICATE WITH THIS SERVICE", sb.toString());
131                         }
132                 } catch (Exception e) {
133                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
134                 }
135                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
136         }
137
138 }