b1d645a612fafac7781b846e953827e41cbad904
[vtn.git] /
1 /*
2  * Copyright (c) 2012-2013 NEC Corporation
3  * All rights reserved.
4  * 
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
8  */
9 package org.opendaylight.vtn.webapi.services;
10
11 import org.json.JSONObject;
12
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;
21
22 /**
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. 
25  */
26 public class VtnServiceWebAPIController {
27
28         /** The Constant LOG. */
29         private static final Logger LOG = Logger.getLogger(VtnServiceWebAPIController.class.getName());
30
31         /** The response success. */
32         private final int RESPONSE_SUCCESS = 200;
33         
34         /** The exception status. */
35         private boolean exceptionStatus = false;
36         
37         /**
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 . 
42          *
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
47          */
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;
51                 long sessionId = 0;
52                 boolean isReadlock = true;
53                 String responseStr = null;
54                 final RestResource resource = new RestResource();
55                 try{
56                         LOG.debug("acquiring session id and readlock from java API");
57                         //acquire session id
58                         resource.setPath(ResourcePathEnum.SESSION_PATH.getPath());
59                         if(resource.post(sessionJson) != RESPONSE_SUCCESS){
60                                 isReadlock=false;
61                                 LOG.error("JAVA API returns error # "+resource.getInfo());
62                                 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
63                                 throw new VtnServiceWebAPIException();
64                         }
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){
70                                 isReadlock=false;
71                                 LOG.error("JAVA API returns error # "+resource.getInfo());
72                                 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
73                                 throw new VtnServiceWebAPIException();
74                         }
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();
84                         }
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);
93                 }
94                 finally{
95                         try{
96                                 //release monitoring mode
97                         if(isReadlock){
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();
104                                         }
105                         }
106                         if(sessionId != ApplicationConstants.ZERO){
107                                         //release session
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();
114                                         }
115                         }
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);
121                         }
122                         
123                 }
124                 return responseJSON;
125         }
126         
127         /**
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
137          */
138         public JSONObject get(final JsonObject sessionJson, final JsonObject reqObject, final String resourcePath) throws VtnServiceWebAPIException {
139                 JSONObject responseJSON = null;
140                 long sessionId = 0;
141                 boolean isReadlock = true;
142                 String responseStr = null;
143                 final RestResource resource = new RestResource();
144                 try{
145                         LOG.debug("acquiring session id and readlock from java API");
146                         //acquire session id
147                         resource.setPath(ResourcePathEnum.SESSION_PATH.getPath());
148                         if(resource.post(sessionJson) != RESPONSE_SUCCESS){
149                                 isReadlock=false;
150                                 LOG.error("JAVA API returns error # "+resource.getInfo());
151                                 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
152                                 throw new VtnServiceWebAPIException();
153                         }
154                         sessionId = VtnServiceCommonUtil.getSessionFromJson(resource.getInfo());
155                         //acquire monitoring mode
156                 
157                         resource.setPath(ResourcePathEnum.ACQUIRE_RELEASE_MONITORING_PATH.getPath());
158                         resource.setSessionID(sessionId);
159                         if(resource.put(VtnServiceWebUtil.prepareAquireReadLockJSON()) != RESPONSE_SUCCESS){
160                                 isReadlock = false;
161                                 LOG.error("JAVA API returns error # "+resource.getInfo());
162                                 responseStr = resource.getInfo() != null? resource.getInfo().toString():null;
163                                 throw new VtnServiceWebAPIException();
164                         }
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();
175                         }
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);
184                 }
185                 finally{
186                         try{
187                                 //release monitoring mode
188                                 if(isReadlock){
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();
195                                         }
196                                 }
197                                 
198                                 if(sessionId != ApplicationConstants.ZERO){
199                                         //release session
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();
206                                         }
207                                 }
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);
213                         }
214                 
215                 }
216                 return responseJSON;
217         }
218
219         
220         /**
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-
223          * Create session 
224          * acquire config mode
225          * calling actual Java API which is requested
226          * release config lock
227          * release session
228          *
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
234          */
235         public JSONObject post(final JsonObject sessionJson, final JsonObject reqObject, final String resourcePath) throws VtnServiceWebAPIException{
236                 JSONObject responseJSON = null; 
237                 String responseStr = null;
238                 long sessionId = 0;
239                 long configId = 0;
240                 final RestResource resource = new RestResource();
241                 try{
242                         LOG.debug("acquiring session id and config mode from java API");
243                         //acquire session id
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();
249                         }
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();
258                         }
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
273                         }
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;
280                         }
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);
287                 }
288                 finally{
289                         try{
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();
299                                         }
300                                 }
301                                 if(sessionId != ApplicationConstants.ZERO){
302                                         //release session
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();
309                                         }
310                                 }
311
312                                 if(null == responseStr && !exceptionStatus){
313                                         responseJSON = VtnServiceWebUtil.prepareErrResponseJson(ApplicationConstants.STATUS_OK, ApplicationConstants.STATUS_SUCCESS);
314                                 }else{
315                                         responseJSON = new JSONObject(responseStr);
316                                 }
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);
320                         }
321
322
323                 }
324                 return responseJSON;
325         }
326
327         /**
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-
330          * Create session 
331          * acquire config mode
332          * calling actual Java API which is requested
333          * release config lock
334          * release session
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
340          */
341         public JSONObject put(final JsonObject sessionJson, final JsonObject reqObject, final String resourcePath) throws VtnServiceWebAPIException {
342                 JSONObject responseJSON = null;
343                 String responseStr = null;
344                 long sessionId = 0;
345                 long configId = 0;
346                 final RestResource resource = new RestResource();
347                 try{
348                         LOG.debug("acquiring session id and config mode from java API");
349                         //acquire session id
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();
355                         }
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();
364                         }
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
378                         }
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;
385                         }
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);
392                 }
393                 finally{
394                         try{
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();
404                                         }
405                                 }
406                                 if(sessionId != ApplicationConstants.ZERO){
407                                         //release session
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();
414                                         }
415                                 }
416
417                                 if(null == responseStr && !exceptionStatus){
418                                         responseJSON = VtnServiceWebUtil.prepareErrResponseJson(ApplicationConstants.STATUS_OK, ApplicationConstants.STATUS_SUCCESS);
419                                 }else{
420                                         responseJSON = new JSONObject(responseStr);
421                                 }
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);
426                         }
427
428                 }
429                 return responseJSON;
430         }
431
432         /**
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-
435          * Create session 
436          * acquire config mode
437          * calling actual Java API which is requested
438          * release config lock
439          * release session
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
445          */
446         public JSONObject delete(final JsonObject sessionJson, final JsonObject reqObject, final String resourcePath) throws VtnServiceWebAPIException{
447                 JSONObject responseJSON = null;
448                 String responseStr = null;
449                 long sessionId = 0;
450                 long configId = 0;
451                 final RestResource resource = new RestResource();
452                 try{
453                         LOG.debug("acquiring session id and config mode from java API");
454                         //acquire session id
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();
460                         }
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();
469                         }
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
483                         }
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;
490                         }
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);
497                 }
498                 finally{
499                         try{
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();
509                                         }
510                                 }
511                                 if(sessionId != ApplicationConstants.ZERO){
512                                         //release session
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();
519                                         }
520                                 }
521                                 if(null == responseStr && !exceptionStatus){
522                                         responseJSON = VtnServiceWebUtil.prepareErrResponseJson(ApplicationConstants.STATUS_OK, ApplicationConstants.STATUS_SUCCESS);
523                                 }else{
524                                         responseJSON = new JSONObject(responseStr);
525                                 }
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);
530                         }
531                         
532                 }
533                 return responseJSON;
534         }
535         
536         /**
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-
539          * Create session 
540          * acquire config mode
541          * calling actual Java API which is requested
542          * release config lock
543          * release session
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
548          */
549         public JSONObject delete(final JsonObject sessionJson,  final String resourcePath) throws VtnServiceWebAPIException {
550                 JSONObject responseJSON = null;
551                 String responseStr = null;
552                 long sessionId = 0;
553                 long configId = 0;
554                 final RestResource resource = new RestResource();
555                 try{
556                         LOG.debug("acquiring session id and config mode from java API");
557                         //acquire session id
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();
563                         }
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();
572                         }
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
585                         }
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;
592                         }
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);
599                 }
600                 finally{
601                         try{
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();
611                                         }
612                                 }
613                                 if(sessionId != ApplicationConstants.ZERO){
614                                         //release session
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();
621                                         }
622                                 }
623                                 if(null == responseStr && !exceptionStatus){
624                                         responseJSON = VtnServiceWebUtil.prepareErrResponseJson(ApplicationConstants.STATUS_OK, ApplicationConstants.STATUS_SUCCESS);
625                                 }else{
626                                         responseJSON = new JSONObject(responseStr);
627                                 }
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);
632                         }
633                         
634                 }
635                 return responseJSON;
636         }
637
638         /**
639          * Execute commit API for given resource, session and configuration id
640          * @param responseStr
641          * @param sessionId
642          * @param configId
643          * @param resource
644          * @return : response from JavaAPI
645          * @throws VtnServiceWebAPIException
646          */
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();
660                 }
661                 LOG.trace("Complete VtnServiceWebAPIController#performCommit()");
662                 return responseStr;
663         }
664         
665         /**
666          * Execute abort API for given resource, session and configuration id
667          * @param responseStr
668          * @param sessionId
669          * @param configId
670          * @param resource
671          * @return : response from JavaAPI
672          * @throws VtnServiceWebAPIException
673          */
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();
685                 }
686                 LOG.trace("Complete VtnServiceWebAPIController#performAbort()");
687                 return responseStr;
688         }       
689 }