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