2 * Copyright (c) 2012-2013 NEC Corporation
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this
7 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.vtn.webapi.services;
11 import org.json.JSONObject;
13 import com.google.gson.JsonObject;
14 import org.opendaylight.vtn.core.util.Logger;
15 import org.opendaylight.vtn.javaapi.RestResource;
16 import org.opendaylight.vtn.webapi.enums.ApplicationConstants;
17 import org.opendaylight.vtn.webapi.enums.ResourcePathEnum;
18 import org.opendaylight.vtn.webapi.exception.VtnServiceWebAPIException;
19 import org.opendaylight.vtn.webapi.utils.VtnServiceCommonUtil;
20 import org.opendaylight.vtn.webapi.utils.VtnServiceWebUtil;
23 * The Class VtnServiceWebAPIController.This class will be the interface between WebAPI and Java API
24 * The actual flow will be covered in this class only.
26 public class VtnServiceWebAPIController {
28 /** The Constant LOG. */
29 private static final Logger LOG = Logger.getLogger(VtnServiceWebAPIController.class.getName());
31 /** The response success. */
32 private final int RESPONSE_SUCCESS = 200;
34 /** The exception status. */
35 private boolean exceptionStatus = false;
38 * This method will get the data from java api in json format and will do not pass any request to java api.
39 * First of all it will create the session and then acquire readlock from TC through JAva API.
40 * Then it will call the requested API and if we get success from lower layer then the response jaon will be recived and
41 * readlock will be released and same for session .
43 * @param sessionJson the session json
44 * @param resourcePath the resource path
45 * @return the jSON object
46 * @throws VtnServiceWebAPIException the vtn service web api exception
48 public JSONObject get(final JsonObject sessionJson, final String resourcePath) throws VtnServiceWebAPIException{
49 LOG.trace("Java API calling for GET method initialized.");
50 JSONObject responseJSON = null;
52 boolean isReadlock = true;
53 String responseStr = null;
54 final RestResource resource = new RestResource();
56 LOG.debug("acquiring session id and readlock from java API");
58 resource.setPath(ResourcePathEnum.SESSION_PATH.getPath());
59 if(resource.post(sessionJson) != RESPONSE_SUCCESS){
61 LOG.error("JAVA API returns error # "+resource.getInfo());
62 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
63 throw new VtnServiceWebAPIException();
65 sessionId = VtnServiceCommonUtil.getSessionFromJson(resource.getInfo());
66 //acquire monitoring mode
67 resource.setPath(ResourcePathEnum.ACQUIRE_RELEASE_MONITORING_PATH.getPath());
68 resource.setSessionID(sessionId);
69 if(resource.put(VtnServiceWebUtil.prepareAquireReadLockJSON()) != RESPONSE_SUCCESS){
71 LOG.error("JAVA API returns error # "+resource.getInfo());
72 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
73 throw new VtnServiceWebAPIException();
75 //send the api request json
76 resource.setPath(resourcePath);
77 resource.setSessionID(sessionId);
78 final int status = resource.get();
79 LOG.debug("JAVA API returned error code #"+status);
80 if(status != RESPONSE_SUCCESS){
81 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
82 LOG.error("JAVA API returns error # "+resource.getInfo());
83 throw new VtnServiceWebAPIException();
85 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
86 LOG.debug("JAVA API returned response # "+responseStr);
87 }catch (VtnServiceWebAPIException vtnException) {
88 exceptionStatus = true;
89 }catch (RuntimeException exception) {
90 exceptionStatus = true;
91 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
92 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
96 //release monitoring mode
98 resource.setPath(ResourcePathEnum.ACQUIRE_RELEASE_MONITORING_PATH.getPath());
99 resource.setSessionID(sessionId);
100 if(resource.delete() != RESPONSE_SUCCESS){
101 LOG.error("JAVA API returns error # "+resource.getInfo());
102 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
103 throw new VtnServiceWebAPIException();
106 if(sessionId != ApplicationConstants.ZERO){
108 resource.setPath(ResourcePathEnum.RELEASE_SESSION.getPath()+ sessionId);
109 resource.setSessionID(sessionId);
110 if(resource.delete() != RESPONSE_SUCCESS){
111 LOG.error("JAVA API returns error # "+resource.getInfo());
112 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
113 throw new VtnServiceWebAPIException();
116 responseJSON = new JSONObject(responseStr);
117 }catch (Exception exception) {
118 exceptionStatus = true;
119 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
120 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
128 *This method will get the data from java api in json format and will pass the required request to java api.
129 * First of all it will create the session and then acquire readlock from TC through JAva API.
130 * Then it will call the requested API and if we get success from lower layer then the response jaon will be recived and
131 * readlock will be released and same for session .
132 * @param sessionJson the session json
133 * @param reqObject the req object
134 * @param resourcePath the resource path
135 * @return the jSON object
136 * @throws VtnServiceWebAPIException the vtn service web api exception
138 public JSONObject get(final JsonObject sessionJson, final JsonObject reqObject, final String resourcePath) throws VtnServiceWebAPIException {
139 JSONObject responseJSON = null;
141 boolean isReadlock = true;
142 String responseStr = null;
143 final RestResource resource = new RestResource();
145 LOG.debug("acquiring session id and readlock from java API");
147 resource.setPath(ResourcePathEnum.SESSION_PATH.getPath());
148 if(resource.post(sessionJson) != RESPONSE_SUCCESS){
150 LOG.error("JAVA API returns error # "+resource.getInfo());
151 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
152 throw new VtnServiceWebAPIException();
154 sessionId = VtnServiceCommonUtil.getSessionFromJson(resource.getInfo());
155 //acquire monitoring mode
157 resource.setPath(ResourcePathEnum.ACQUIRE_RELEASE_MONITORING_PATH.getPath());
158 resource.setSessionID(sessionId);
159 if(resource.put(VtnServiceWebUtil.prepareAquireReadLockJSON()) != RESPONSE_SUCCESS){
161 LOG.error("JAVA API returns error # "+resource.getInfo());
162 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
163 throw new VtnServiceWebAPIException();
165 //send the api request json
166 resource.setPath(resourcePath);
167 resource.setSessionID(sessionId);
168 LOG.debug("Request object passing to JAVA API #"+reqObject);
169 final int status = resource.get(reqObject);
170 LOG.debug("JAVA API returned error code #"+status);
171 if(status != RESPONSE_SUCCESS){
172 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
173 LOG.error("JAVA API returns error # "+resource.getInfo());
174 throw new VtnServiceWebAPIException();
176 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
177 LOG.debug("JAVA API returned response # "+responseStr);
178 }catch (VtnServiceWebAPIException vtnException) {
179 exceptionStatus = true;
180 }catch (RuntimeException exception) {
181 exceptionStatus = true;
182 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
183 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
187 //release monitoring mode
189 resource.setPath(ResourcePathEnum.ACQUIRE_RELEASE_MONITORING_PATH.getPath());
190 resource.setSessionID(sessionId);
191 if(resource.delete() != RESPONSE_SUCCESS){
192 LOG.error("JAVA API returns error # "+resource.getInfo());
193 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
194 throw new VtnServiceWebAPIException();
198 if(sessionId != ApplicationConstants.ZERO){
200 resource.setPath(ResourcePathEnum.RELEASE_SESSION.getPath()+ sessionId);
201 resource.setSessionID(sessionId);
202 if(resource.delete() != RESPONSE_SUCCESS){
203 LOG.error("JAVA API returns error # "+resource.getInfo());
204 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
205 throw new VtnServiceWebAPIException();
208 responseJSON = new JSONObject(responseStr);
209 }catch (Exception exception) {
210 exceptionStatus = true;
211 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
212 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
221 * Post.This method will be required when we have to create any component using Java API.
222 * This method will work in the following sequence-
224 * acquire config mode
225 * calling actual Java API which is requested
226 * release config lock
229 * @param sessionJson the session json
230 * @param reqObject the req object
231 * @param resourcePath the resource path
232 * @return the jSON object
233 * @throws VtnServiceWebAPIException the vtn service web api exception
235 public JSONObject post(final JsonObject sessionJson, final JsonObject reqObject, final String resourcePath) throws VtnServiceWebAPIException{
236 JSONObject responseJSON = null;
237 String responseStr = null;
240 final RestResource resource = new RestResource();
242 LOG.debug("acquiring session id and config mode from java API");
244 resource.setPath(ResourcePathEnum.SESSION_PATH.getPath());
245 if(resource.post(sessionJson) != RESPONSE_SUCCESS){
246 LOG.error("JAVA API returns error # "+resource.getInfo());
247 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
248 throw new VtnServiceWebAPIException();
250 sessionId = VtnServiceCommonUtil.getSessionFromJson(resource.getInfo());
251 //acquire configure mode
252 resource.setPath(ResourcePathEnum.ACQUIRE_CONFIG_PATH.getPath());
253 resource.setSessionID(sessionId);
254 if(resource.post(VtnServiceWebUtil.prepareAquireConfigJSON()) != RESPONSE_SUCCESS){
255 LOG.error("JAVA API returns error # "+resource.getInfo());
256 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
257 throw new VtnServiceWebAPIException();
259 configId = VtnServiceCommonUtil.getConfigIdFromJson(resource.getInfo());
260 // Abort operation to clear candidate DB
261 responseStr = performAbort(sessionId, configId, resource);
262 //send the api request json
263 resource.setPath(resourcePath);
264 resource.setSessionID(sessionId);
265 resource.setConfigID(configId);
266 LOG.debug("Request object passing to JAVA API #"+reqObject);
267 final int status = resource.post(reqObject);
268 LOG.debug("JAVA API returned error code #"+status);
269 if(status != RESPONSE_SUCCESS){
270 LOG.error("JAVA API returns error # "+resource.getInfo());
271 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
272 throw new VtnServiceWebAPIException();//to save the below steps execution
274 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
275 LOG.debug("JAVA API returned response # "+responseStr);
276 //responseStr = performCommit(sessionId, configId, resource);
277 String commitResponse = performCommit(sessionId, configId, resource);
278 if(commitResponse != null){
279 responseStr = commitResponse;
281 }catch (VtnServiceWebAPIException vtnException) {
282 exceptionStatus = true;
283 }catch (RuntimeException exception) {
284 exceptionStatus = true;
285 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
286 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
290 if(configId != ApplicationConstants.ZERO){
291 //release monitoring mode
292 resource.setPath(ResourcePathEnum.RELEASE_CONFIGURATION.getPath()+configId);
293 resource.setSessionID(sessionId);
294 resource.setConfigID(configId);
295 if(resource.delete() != RESPONSE_SUCCESS){
296 LOG.error("JAVA API returns error # "+resource.getInfo());
297 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
298 throw new VtnServiceWebAPIException();
301 if(sessionId != ApplicationConstants.ZERO){
303 resource.setPath(ResourcePathEnum.RELEASE_SESSION.getPath()+ sessionId);
304 resource.setSessionID(sessionId);
305 if(resource.delete() != RESPONSE_SUCCESS){
306 LOG.error("JAVA API returns error # "+resource.getInfo());
307 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
308 throw new VtnServiceWebAPIException();
312 if(null == responseStr && !exceptionStatus){
313 responseJSON = VtnServiceWebUtil.prepareErrResponseJson(ApplicationConstants.STATUS_OK, ApplicationConstants.STATUS_SUCCESS);
315 responseJSON = new JSONObject(responseStr);
317 }catch (Exception exception) {
318 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
319 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
328 * Put.This method will be required when we have to update any component using Java API.
329 * This method will work in the following sequence-
331 * acquire config mode
332 * calling actual Java API which is requested
333 * release config lock
335 * @param sessionJson the session json
336 * @param reqObject the req object
337 * @param resourcePath the resource path
338 * @return the jSON object
339 * @throws VtnServiceWebAPIException the vtn service web api exception
341 public JSONObject put(final JsonObject sessionJson, final JsonObject reqObject, final String resourcePath) throws VtnServiceWebAPIException {
342 JSONObject responseJSON = null;
343 String responseStr = null;
346 final RestResource resource = new RestResource();
348 LOG.debug("acquiring session id and config mode from java API");
350 resource.setPath(ResourcePathEnum.SESSION_PATH.getPath());
351 if(resource.post(sessionJson) != RESPONSE_SUCCESS){
352 LOG.error("JAVA API returns error # "+resource.getInfo());
353 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
354 throw new VtnServiceWebAPIException();
356 sessionId = VtnServiceCommonUtil.getSessionFromJson(resource.getInfo());
357 //acquire configure mode
358 resource.setPath(ResourcePathEnum.ACQUIRE_CONFIG_PATH.getPath());
359 resource.setSessionID(sessionId);
360 if(resource.post(VtnServiceWebUtil.prepareAquireConfigJSON()) != RESPONSE_SUCCESS){
361 LOG.error("JAVA API returns error # "+resource.getInfo());
362 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
363 throw new VtnServiceWebAPIException();
365 configId = VtnServiceCommonUtil.getConfigIdFromJson(resource.getInfo());
366 responseStr = performAbort(sessionId, configId, resource);
367 //send the api request json
368 resource.setPath(resourcePath);
369 resource.setConfigID(configId);
370 resource.setSessionID(sessionId);
371 LOG.debug("Request object passing to JAVA API #"+reqObject);
372 final int status = resource.put(reqObject);
373 LOG.debug("JAVA API returned error code #"+status);
374 if(status != RESPONSE_SUCCESS){
375 LOG.error("JAVA API returns error # "+resource.getInfo());
376 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
377 throw new VtnServiceWebAPIException();//to save the below steps execution
379 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
380 LOG.debug("JAVA API returned response # "+responseStr);
381 //responseStr = performCommit(sessionId, configId, resource);
382 String commitResponse = performCommit(sessionId, configId, resource);
383 if(commitResponse != null){
384 responseStr = commitResponse;
386 }catch (VtnServiceWebAPIException vtnException) {
387 exceptionStatus = true;
388 }catch (RuntimeException exception) {
389 exceptionStatus = true;
390 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
391 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
395 if(configId != ApplicationConstants.ZERO){
396 //release monitoring mode
397 resource.setPath(ResourcePathEnum.RELEASE_CONFIGURATION.getPath()+configId);
398 resource.setSessionID(sessionId);
399 resource.setConfigID(configId);
400 if(resource.delete() != RESPONSE_SUCCESS){
401 LOG.error("JAVA API returns error # "+resource.getInfo());
402 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
403 throw new VtnServiceWebAPIException();
406 if(sessionId != ApplicationConstants.ZERO){
408 resource.setPath(ResourcePathEnum.RELEASE_SESSION.getPath()+ sessionId);
409 resource.setSessionID(sessionId);
410 if(resource.delete() != RESPONSE_SUCCESS){
411 LOG.error("JAVA API returns error # "+resource.getInfo());
412 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
413 throw new VtnServiceWebAPIException();
417 if(null == responseStr && !exceptionStatus){
418 responseJSON = VtnServiceWebUtil.prepareErrResponseJson(ApplicationConstants.STATUS_OK, ApplicationConstants.STATUS_SUCCESS);
420 responseJSON = new JSONObject(responseStr);
422 }catch (Exception exception) {
423 exceptionStatus = true;
424 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
425 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
433 * Delete.This method will be required when we have to delete any component using Java API.
434 * This method will work in the following sequence-
436 * acquire config mode
437 * calling actual Java API which is requested
438 * release config lock
440 * @param sessionJson the session json
441 * @param reqObject the req object
442 * @param resourcePath the resource path
443 * @return the jSON object
444 * @throws VtnServiceWebAPIException the vtn service web api exception
446 public JSONObject delete(final JsonObject sessionJson, final JsonObject reqObject, final String resourcePath) throws VtnServiceWebAPIException{
447 JSONObject responseJSON = null;
448 String responseStr = null;
451 final RestResource resource = new RestResource();
453 LOG.debug("acquiring session id and config mode from java API");
455 resource.setPath(ResourcePathEnum.SESSION_PATH.getPath());
456 if(resource.post(sessionJson) != RESPONSE_SUCCESS){
457 LOG.error("JAVA API returns error # "+resource.getInfo());
458 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
459 throw new VtnServiceWebAPIException();
461 sessionId = VtnServiceCommonUtil.getSessionFromJson(resource.getInfo());
462 //acquire configure mode
463 resource.setPath(ResourcePathEnum.ACQUIRE_CONFIG_PATH.getPath());
464 resource.setSessionID(sessionId);
465 if(resource.post(VtnServiceWebUtil.prepareAquireConfigJSON()) != RESPONSE_SUCCESS){
466 LOG.error("JAVA API returns error # "+resource.getInfo());
467 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
468 throw new VtnServiceWebAPIException();
470 configId = VtnServiceCommonUtil.getConfigIdFromJson(resource.getInfo());
471 responseStr = performAbort(sessionId, configId, resource);
472 //send the api request json
473 resource.setPath(resourcePath);
474 resource.setConfigID(configId);
475 resource.setSessionID(sessionId);
476 LOG.debug("Request object passing to JAVA API #"+reqObject);
477 final int status = resource.delete(reqObject);
478 LOG.debug("JAVA API returned error code #"+status);
479 if(status != RESPONSE_SUCCESS){
480 LOG.error("JAVA API returns error # "+resource.getInfo());
481 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
482 throw new VtnServiceWebAPIException();//to save the below steps execution
484 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
485 LOG.debug("JAVA API returned response # "+responseStr);
486 //responseStr = performCommit(sessionId, configId, resource);
487 String commitResponse = performCommit(sessionId, configId, resource);
488 if(commitResponse != null){
489 responseStr = commitResponse;
491 }catch (VtnServiceWebAPIException vtnException) {
492 exceptionStatus = true;
493 }catch (Exception exception) {
494 exceptionStatus = true;
495 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
496 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
500 if(configId != ApplicationConstants.ZERO){
501 //release monitoring mode
502 resource.setPath(ResourcePathEnum.RELEASE_CONFIGURATION.getPath()+configId);
503 resource.setSessionID(sessionId);
504 resource.setConfigID(configId);
505 if(resource.delete() != RESPONSE_SUCCESS){
506 LOG.error("JAVA API returns error # "+resource.getInfo());
507 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
508 throw new VtnServiceWebAPIException();
511 if(sessionId != ApplicationConstants.ZERO){
513 resource.setPath(ResourcePathEnum.RELEASE_SESSION.getPath()+ sessionId);
514 resource.setSessionID(sessionId);
515 if(resource.delete() != RESPONSE_SUCCESS){
516 LOG.error("JAVA API returns error # "+resource.getInfo());
517 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
518 throw new VtnServiceWebAPIException();
521 if(null == responseStr && !exceptionStatus){
522 responseJSON = VtnServiceWebUtil.prepareErrResponseJson(ApplicationConstants.STATUS_OK, ApplicationConstants.STATUS_SUCCESS);
524 responseJSON = new JSONObject(responseStr);
526 }catch (Exception exception) {
527 exceptionStatus = true;
528 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
529 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
537 * Delete.This method will be required when we have to delete any component using Java API.
538 * This method will work in the following sequence-
540 * acquire config mode
541 * calling actual Java API which is requested
542 * release config lock
544 * @param sessionJson the session json
545 * @param resourcePath the resource path
546 * @return the jSON object
547 * @throws VtnServiceWebAPIException the vtn service web api exception
549 public JSONObject delete(final JsonObject sessionJson, final String resourcePath) throws VtnServiceWebAPIException {
550 JSONObject responseJSON = null;
551 String responseStr = null;
554 final RestResource resource = new RestResource();
556 LOG.debug("acquiring session id and config mode from java API");
558 resource.setPath(ResourcePathEnum.SESSION_PATH.getPath());
559 if(resource.post(sessionJson) != RESPONSE_SUCCESS){
560 LOG.error("JAVA API returns error # "+resource.getInfo());
561 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
562 throw new VtnServiceWebAPIException();
564 sessionId = VtnServiceCommonUtil.getSessionFromJson(resource.getInfo());
565 //acquire configure mode
566 resource.setPath(ResourcePathEnum.ACQUIRE_CONFIG_PATH.getPath());
567 resource.setSessionID(sessionId);
568 if(resource.post(VtnServiceWebUtil.prepareAquireConfigJSON()) != RESPONSE_SUCCESS){
569 LOG.error("JAVA API returns error # "+resource.getInfo());
570 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
571 throw new VtnServiceWebAPIException();
573 configId = VtnServiceCommonUtil.getConfigIdFromJson(resource.getInfo());
574 responseStr = performAbort(sessionId, configId, resource);
575 //send the api request json
576 resource.setPath(resourcePath);
577 resource.setConfigID(configId);
578 resource.setSessionID(sessionId);
579 final int status = resource.delete();
580 LOG.debug("JAVA API returned error code #"+status);
581 if(status != RESPONSE_SUCCESS){
582 LOG.error("JAVA API returns error # "+resource.getInfo());
583 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
584 throw new VtnServiceWebAPIException();//to save the below steps execution
586 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
587 LOG.debug("JAVA API returned response # "+responseStr);
588 //responseStr = performCommit(sessionId, configId, resource);
589 String commitResponse = performCommit(sessionId, configId, resource);
590 if(commitResponse != null){
591 responseStr = commitResponse;
593 }catch (VtnServiceWebAPIException vtnException) {
594 exceptionStatus = true;
595 }catch (RuntimeException exception) {
596 exceptionStatus = true;
597 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
598 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
602 if(configId != ApplicationConstants.ZERO){
603 //release monitoring mode
604 resource.setPath(ResourcePathEnum.RELEASE_CONFIGURATION.getPath()+configId);
605 resource.setSessionID(sessionId);
606 resource.setConfigID(configId);
607 if(resource.delete() != RESPONSE_SUCCESS){
608 LOG.error("JAVA API returns error # "+resource.getInfo());
609 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
610 throw new VtnServiceWebAPIException();
613 if(sessionId != ApplicationConstants.ZERO){
615 resource.setPath(ResourcePathEnum.RELEASE_SESSION.getPath()+ sessionId);
616 resource.setSessionID(sessionId);
617 if(resource.delete() != RESPONSE_SUCCESS){
618 LOG.error("JAVA API returns error # "+resource.getInfo());
619 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
620 throw new VtnServiceWebAPIException();
623 if(null == responseStr && !exceptionStatus){
624 responseJSON = VtnServiceWebUtil.prepareErrResponseJson(ApplicationConstants.STATUS_OK, ApplicationConstants.STATUS_SUCCESS);
626 responseJSON = new JSONObject(responseStr);
628 }catch (Exception exception) {
629 exceptionStatus = true;
630 LOG.error(VtnServiceCommonUtil.logErrorDetails(ApplicationConstants.INTERNAL_SERVER_ERROR));
631 throw new VtnServiceWebAPIException(ApplicationConstants.INTERNAL_SERVER_ERROR, ApplicationConstants.DEFAULT_ERROR_DESCRIPTION);
639 * Execute commit API for given resource, session and configuration id
644 * @return : response from JavaAPI
645 * @throws VtnServiceWebAPIException
647 private String performCommit(long sessionId, long configId, final RestResource resource)
648 throws VtnServiceWebAPIException {
649 String responseStr = null ;
650 LOG.trace("Start VtnServiceWebAPIController#performCommit()");
651 resource.setPath(ResourcePathEnum.COMMIT_CONFIGURATION.getPath());
652 resource.setSessionID(sessionId);
653 resource.setConfigID(configId);
654 final int commitStatus = resource.put(VtnServiceWebUtil.prepareConfigCommitOrSaveJSON(ApplicationConstants.OPERATION_COMMIT));
655 if (commitStatus != RESPONSE_SUCCESS) {
656 LOG.error("JAVA API returns error # "+resource.getInfo());
657 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
658 performAbort(sessionId, configId, resource);
659 throw new VtnServiceWebAPIException();
661 LOG.trace("Complete VtnServiceWebAPIController#performCommit()");
666 * Execute abort API for given resource, session and configuration id
671 * @return : response from JavaAPI
672 * @throws VtnServiceWebAPIException
674 private String performAbort(long sessionId, long configId, final RestResource resource)
675 throws VtnServiceWebAPIException {
676 String responseStr = null ;
677 LOG.trace("Start VtnServiceWebAPIController#performAbort()");
678 resource.setPath(ResourcePathEnum.ABORT_CONFIGURATION.getPath());
679 resource.setSessionID(sessionId);
680 resource.setConfigID(configId);
681 if (resource.put(VtnServiceWebUtil.prepareCandidateAbortJSON(ApplicationConstants.OPERATION_ABORT)) != RESPONSE_SUCCESS) {
682 LOG.error("JAVA API returns error # " + resource.getInfo());
683 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
684 throw new VtnServiceWebAPIException();
686 LOG.trace("Complete VtnServiceWebAPIController#performAbort()");