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