c2d54c6b17477030218eff301ad50beca75cb244
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / SchedulerController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.controller;
39
40 import java.text.DateFormat;
41 import java.text.SimpleDateFormat;
42 import java.util.Date;
43 import java.util.UUID;
44
45 import javax.servlet.http.HttpServletRequest;
46
47 import org.json.simple.JSONObject;
48 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
49 import org.openecomp.portalapp.portal.scheduler.SchedulerProperties;
50 import org.openecomp.portalapp.portal.scheduler.SchedulerRestInterface;
51 import org.openecomp.portalapp.portal.scheduler.SchedulerUtil;
52 import org.openecomp.portalapp.portal.scheduler.restobjects.GetTimeSlotsRestObject;
53 import org.openecomp.portalapp.portal.scheduler.restobjects.PostCreateNewVnfRestObject;
54 import org.openecomp.portalapp.portal.scheduler.restobjects.PostSubmitVnfChangeRestObject;
55 import org.openecomp.portalapp.portal.scheduler.wrapper.GetTimeSlotsWrapper;
56 import org.openecomp.portalapp.portal.scheduler.wrapper.PostCreateNewVnfWrapper;
57 import org.openecomp.portalapp.portal.scheduler.wrapper.PostSubmitVnfChangeTimeSlotsWrapper;
58 import org.openecomp.portalapp.portal.utils.PortalConstants;
59 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import org.openecomp.portalsdk.core.util.SystemProperties;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.context.annotation.Configuration;
63 import org.springframework.context.annotation.EnableAspectJAutoProxy;
64 import org.springframework.http.HttpStatus;
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.RequestMapping;
69 import org.springframework.web.bind.annotation.RequestMethod;
70 import org.springframework.web.bind.annotation.RestController;
71
72 @RestController
73 @RequestMapping(PortalConstants.REST_AUX_API)
74 @Configuration
75 @EnableAspectJAutoProxy
76 @EPAuditLog
77 public class SchedulerController implements BasicAuthenticationController {
78
79         @Autowired
80         private SchedulerRestInterface schedulerRestController;
81
82         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerController.class);
83
84         /** The request date format. */
85         public DateFormat requestDateFormat = new SimpleDateFormat("EEE, dd MMM YYYY HH:mm:ss z");
86
87         @RequestMapping(value = "/get_time_slots/{scheduler_request}", method = RequestMethod.GET)
88         public ResponseEntity<String> getTimeSlots(HttpServletRequest request,
89                         @PathVariable("scheduler_request") String scheduler_request) throws Exception {
90
91                 Date startingTime = new Date();
92                 String startTimeRequest = requestDateFormat.format(startingTime);
93
94                 System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
95                 System.out.println(startTimeRequest + " | Controller Scheduler GET : /get_time_slots/{scheduler_request} \n");
96                 System.out.println("Original Request : \n " + scheduler_request + '\n');
97
98                 String path = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_GET_TIME_SLOTS) + scheduler_request;
99
100                 GetTimeSlotsWrapper schedulerResWrapper = getTimeSlots(scheduler_request, path, scheduler_request);
101
102                 Date endTime = new Date();
103                 String endTimeRequest = requestDateFormat.format(endTime);
104                 System.out.println(endTimeRequest + " | Controller Scheduler - GET\n");
105
106                 return (new ResponseEntity<String>(schedulerResWrapper.getResponse(), HttpStatus.OK));
107
108         }
109
110         protected GetTimeSlotsWrapper getTimeSlots(String request, String path, String uuid) throws Exception {
111
112                 try {
113                         // STARTING REST API CALL AS AN FACTORY INSTACE
114                         System.out.println("<== Get Time Slots Request START \n");
115
116                         GetTimeSlotsRestObject<String> restObjStr = new GetTimeSlotsRestObject<String>();
117                         String str = new String();
118
119                         restObjStr.set(str);
120
121                         schedulerRestController.Get(str, uuid, path, restObjStr);
122                         GetTimeSlotsWrapper schedulerRespWrapper = SchedulerUtil.getTimeSlotsWrapResponse(restObjStr);
123
124                         System.out.println(
125                                         "<== Get Time Slots Request END : Response = " + schedulerRespWrapper.getResponse() + '\n');
126
127                         return schedulerRespWrapper;
128
129                 } catch (Exception e) {
130                         System.out.println("<== Get Time Slots Request ERROR : " + e.toString() + '\n');
131                         logger.error(EELFLoggerDelegate.errorLogger, "Get Time Slots Request failed", e);
132                         throw e;
133                 }
134         }
135
136         @SuppressWarnings("unchecked")
137         @RequestMapping(value = "/post_create_new_vnf_change", method = RequestMethod.POST)
138         public ResponseEntity<String> postCreateNewVNFChange(HttpServletRequest request,
139                         @RequestBody JSONObject scheduler_request) throws Exception {
140
141                 Date startingTime = new Date();
142                 String startTimeRequest = requestDateFormat.format(startingTime);
143
144                 System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
145                 System.out.println(startTimeRequest + " | Controller Scheduler POST : post_create_new_vnf_change \n");
146
147                 // Generating uuid
148                 String uuid = UUID.randomUUID().toString();
149
150                 scheduler_request.put("scheduleId", uuid);
151                 System.out.println("<== UUID : " + uuid + '\n');
152
153                 // adding uuid to the request payload
154                 scheduler_request.put("scheduleId", uuid);
155
156                 System.out.println("<== UUID : " + uuid + '\n');
157                 System.out.println("Original Request : \n " + scheduler_request.toString() + '\n');
158
159                 String path = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_CREATE_NEW_VNF_CHANGE_INSTANCE_VAL)
160                                 + uuid;
161
162                 PostCreateNewVnfWrapper responseWrapper = postSchedulingRequest(scheduler_request, path, uuid);
163
164                 Date endTime = new Date();
165                 String endTimeRequest = requestDateFormat.format(endTime);
166                 System.out.println(endTimeRequest + " | Controller Scheduler - POST\n");
167
168                 return (new ResponseEntity<String>(responseWrapper.getResponse(), HttpStatus.OK));
169         }
170
171         protected PostCreateNewVnfWrapper postSchedulingRequest(JSONObject request, String path, String uuid)
172                         throws Exception {
173
174                 try {
175                         // STARTING REST API CALL AS AN FACTORY INSTACE
176                         System.out.println("<== Post Create New Vnf Scheduling Request START \n");
177
178                         PostCreateNewVnfRestObject<String> restObjStr = new PostCreateNewVnfRestObject<String>();
179                         String str = new String();
180
181                         restObjStr.set(str);
182                         schedulerRestController.<String>Post(str, request, path, restObjStr);
183
184                         int status = restObjStr.getStatusCode();
185                         if (status >= 200 && status <= 299) {
186                                 restObjStr.setUUID(uuid);
187                         }
188
189                         PostCreateNewVnfWrapper responseWrapper = SchedulerUtil.postCreateNewVnfWrapResponse(restObjStr);
190
191                         System.out.println("<== Post Create New Vnf Scheduling Request END : Response = "
192                                         + responseWrapper.getResponse() + '\n');
193
194                         return responseWrapper;
195
196                 } catch (Exception e) {
197                         System.out.println("<== Post Create New Vnf Scheduling Request ERROR : " + e.toString() + '\n');
198                         logger.error(EELFLoggerDelegate.errorLogger, "Post Create New Vnf Scheduling Request failed", e);
199                         throw e;
200                 }
201         }
202
203         @RequestMapping(value = "/submit_vnf_change_timeslots", method = RequestMethod.POST)
204         public ResponseEntity<String> postSubmitVnfChangeTimeslots(HttpServletRequest request,
205                         @RequestBody JSONObject scheduler_request) throws Exception {
206
207                 Date startingTime = new Date();
208                 String startTimeRequest = requestDateFormat.format(startingTime);
209
210                 System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
211                 System.out.println(startTimeRequest + " | Controller Scheduler POST : submit_vnf_change_timeslots \n");
212
213                 // Generating uuid
214                 String uuid = (String) scheduler_request.get("scheduleId");
215                 scheduler_request.remove("scheduleId");
216
217                 System.out.println("<== UUID : " + uuid + '\n');
218                 System.out.println("Original Request : \n " + scheduler_request.toString() + '\n');
219
220                 String path = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SUBMIT_NEW_VNF_CHANGE)
221                                 .replace("{scheduleId}", uuid);
222
223                 PostSubmitVnfChangeTimeSlotsWrapper responseWrapper = postSubmitSchedulingRequest(scheduler_request, path,
224                                 uuid);
225
226                 Date endTime = new Date();
227                 String endTimeRequest = requestDateFormat.format(endTime);
228                 System.out.println(endTimeRequest + " | Controller Scheduler - POST Submit\n");
229
230                 return (new ResponseEntity<String>(responseWrapper.getResponse(), HttpStatus.OK));
231         }
232
233         protected PostSubmitVnfChangeTimeSlotsWrapper postSubmitSchedulingRequest(JSONObject request, String path,
234                         String uuid) throws Exception {
235
236                 try {
237                         // STARTING REST API CALL AS AN FACTORY INSTACE
238                         System.out.println("<== Post Submit Scheduling Request START \n");
239
240                         PostSubmitVnfChangeRestObject<String> restObjStr = new PostSubmitVnfChangeRestObject<String>();
241                         String str = new String();
242
243                         restObjStr.set(str);
244                         schedulerRestController.<String>Post(str, request, path, restObjStr);
245
246                         int status = restObjStr.getStatusCode();
247                         if (status >= 200 && status <= 299) {
248                                 restObjStr.setUUID(uuid);
249                         }
250
251                         PostSubmitVnfChangeTimeSlotsWrapper responseWrapper = SchedulerUtil
252                                         .postSubmitNewVnfWrapResponse(restObjStr);
253
254                         System.out.println(
255                                         "<== Post Submit Scheduling Request END : Response = " + responseWrapper.getResponse() + '\n');
256
257                         return responseWrapper;
258
259                 } catch (Exception e) {
260                         System.out.println("<== Post Submit Scheduling Request ERROR : " + e.toString() + '\n');
261                         logger.error(EELFLoggerDelegate.errorLogger, "Post Submit Scheduling Request failed", e);
262                         throw e;
263                 }
264         }
265
266 }