From e49cf3693fabc2a767a1dbc4e29ff5eecb4c9784 Mon Sep 17 00:00:00 2001 From: Reinaldo Penno Date: Mon, 26 Oct 2015 01:12:19 -0700 Subject: [PATCH] Bug 4528 - SFC SBRest Broken - Commit https://git.opendaylight.org/gerrit/#/c/28539/ introduced a new Yang model sfc-common.yang with typedefs for names used throughout other models. - Although the typedefs resolve to strings, the typedefs themselves are instantiated as objects. Therefore in order to get the actual value of, say, the SF name from a SF object, first you get a handle to the object associated with the typedef and then perform a getValue(). - Introduced try/catch around jersey client methods in order to catch future issues Change-Id: I870f3f84c707f6964aa1683b67a7c0e8ff2c847f Signed-off-by: Reinaldo Penno --- .../sbrest/provider/task/SbRestAclTask.java | 8 +- .../sbrest/provider/task/SbRestRspTask.java | 10 +- .../sbrest/provider/task/SbRestSfTask.java | 12 +- .../sbrest/provider/task/SbRestSffTask.java | 12 +- .../sbrest/provider/task/SbRestSfgTask.java | 6 +- .../provider/task/SbRestSfstateTask.java | 12 +- .../sfc/sbrest/provider/task/WsTask.java | 133 ++++++++++-------- 7 files changed, 109 insertions(+), 84 deletions(-) diff --git a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestAclTask.java b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestAclTask.java index 704c2d395..5f3dc145b 100644 --- a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestAclTask.java +++ b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestAclTask.java @@ -7,10 +7,6 @@ */ package org.opendaylight.sfc.sbrest.provider.task; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ExecutorService; - import org.opendaylight.sfc.provider.api.SfcProviderAclAPI; import org.opendaylight.sfc.provider.api.SfcProviderServiceClassifierAPI; import org.opendaylight.sfc.provider.api.SfcProviderServiceForwarderAPI; @@ -26,6 +22,10 @@ import org.opendaylight.yangtools.yang.binding.DataObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; + public class SbRestAclTask extends SbRestAbstractTask { private static final String ACL_REST_URI = "/config/ietf-acl:access-lists/access-list/"; diff --git a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestRspTask.java b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestRspTask.java index b401d0568..d75f4b307 100755 --- a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestRspTask.java +++ b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestRspTask.java @@ -9,6 +9,7 @@ package org.opendaylight.sfc.sbrest.provider.task; import org.opendaylight.sfc.provider.api.SfcProviderServiceForwarderAPI; import org.opendaylight.sfc.sbrest.json.RspExporterFactory; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.RspName; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHop; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder; @@ -50,9 +51,12 @@ public class SbRestRspTask extends SbRestAbstractTask { SfcProviderServiceForwarderAPI .readServiceFunctionForwarder(hop.getServiceFunctionForwarder()); if (sff != null && sff.getRestUri() != null) { - String restUri = sff.getRestUri().getValue() + RSP_REST_URI + obj.getName(); - this.restUriList.add(restUri); - LOG.info("RSP will be send to REST URI {}", restUri); + RspName rspName = obj.getName(); + if (rspName != null) { + String restUri = sff.getRestUri().getValue() + RSP_REST_URI + rspName.getValue(); + this.restUriList.add(restUri); + LOG.info("RSP will be send to REST URI {}", restUri); + } } } } diff --git a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfTask.java b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfTask.java index 9b8ca7761..30ab3724d 100755 --- a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfTask.java +++ b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfTask.java @@ -8,6 +8,7 @@ package org.opendaylight.sfc.sbrest.provider.task; import org.opendaylight.sfc.sbrest.json.SfExporterFactory; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction; import org.opendaylight.yangtools.yang.binding.DataObject; import org.slf4j.Logger; @@ -37,10 +38,13 @@ public class SbRestSfTask extends SbRestAbstractTask { ServiceFunction obj = (ServiceFunction) dataObject; if (obj.getRestUri() != null) { - String restUri = obj.getRestUri().getValue() + SF_REST_URI + obj.getName(); - this.restUriList = new ArrayList<>(); - this.restUriList.add(restUri); - LOG.info("SF will be send to REST URI {}", restUri); + SfName sfName = obj.getName(); + if (sfName != null) { + String restUri = obj.getRestUri().getValue() + SF_REST_URI + sfName.getValue(); + this.restUriList = new ArrayList<>(); + this.restUriList.add(restUri); + LOG.info("SF will be send to REST URI {}", restUri); + } } else { this.restUriList = null; } diff --git a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSffTask.java b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSffTask.java index 3ecd65818..cca5e7ed1 100755 --- a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSffTask.java +++ b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSffTask.java @@ -8,6 +8,7 @@ package org.opendaylight.sfc.sbrest.provider.task; import org.opendaylight.sfc.sbrest.json.SffExporterFactory; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SffName; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder; import org.opendaylight.yangtools.yang.binding.DataObject; import org.slf4j.Logger; @@ -39,10 +40,13 @@ public class SbRestSffTask extends SbRestAbstractTask { ServiceFunctionForwarder obj = (ServiceFunctionForwarder) dataObject; if (obj.getRestUri() != null) { - String restUri = obj.getRestUri().getValue() + SFF_REST_URI + obj.getName(); - this.restUriList = new ArrayList<>(); - this.restUriList.add(restUri); - LOG.info("SFF will be send to REST URI {}", restUri); + SffName sffName = obj.getName(); + if (sffName != null) { + String restUri = obj.getRestUri().getValue() + SFF_REST_URI + sffName.getValue(); + this.restUriList = new ArrayList<>(); + this.restUriList.add(restUri); + LOG.info("SFF will be send to REST URI {}", restUri); + } } else { this.restUriList = null; } diff --git a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfgTask.java b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfgTask.java index 8a4097a8e..78f4cf856 100644 --- a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfgTask.java +++ b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfgTask.java @@ -7,15 +7,15 @@ */ package org.opendaylight.sfc.sbrest.provider.task; -import java.util.ArrayList; -import java.util.concurrent.ExecutorService; - import org.opendaylight.sfc.sbrest.json.SfgExporterFactory; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfg.rev150214.service.function.groups.ServiceFunctionGroup; import org.opendaylight.yangtools.yang.binding.DataObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.concurrent.ExecutorService; + public class SbRestSfgTask extends SbRestAbstractTask { private static final String SFG_REST_URI = "/config/service-function-group:service-function-groups/service-function-group/"; diff --git a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfstateTask.java b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfstateTask.java index 081d812e3..d4370a884 100644 --- a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfstateTask.java +++ b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfstateTask.java @@ -9,6 +9,7 @@ package org.opendaylight.sfc.sbrest.provider.task; import org.opendaylight.sfc.sbrest.json.SfstateExporterFactory; import org.opendaylight.sfc.provider.api.SfcProviderServiceFunctionAPI; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.state.ServiceFunctionState; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction; import org.opendaylight.yangtools.yang.binding.DataObject; @@ -39,12 +40,15 @@ public class SbRestSfstateTask extends SbRestAbstractTask { this.restUriList = new ArrayList<>(); - if(SfcProviderServiceFunctionAPI.readServiceFunction(obj.getName())!=null) { + if (SfcProviderServiceFunctionAPI.readServiceFunction(obj.getName())!=null) { ServiceFunction serviceFunction = SfcProviderServiceFunctionAPI.readServiceFunction(obj.getName()); if (serviceFunction.getRestUri() != null) { - String restUri = serviceFunction.getRestUri().getValue() + SFSTATE_REST_URI + obj.getName(); - this.restUriList.add(restUri); - LOG.info("Service Function state will be sent to REST URI {}", restUri); + SfName sfName = obj.getName(); + if (sfName != null) { + String restUri = serviceFunction.getRestUri().getValue() + SFSTATE_REST_URI + sfName.getValue(); + this.restUriList.add(restUri); + LOG.info("Service Function state will be sent to REST URI {}", restUri); + } } else { this.restUriList = null; } diff --git a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/WsTask.java b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/WsTask.java index 754cdf454..c399c753a 100755 --- a/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/WsTask.java +++ b/sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/WsTask.java @@ -38,74 +38,83 @@ public class WsTask implements Runnable { ClientConfig clientConfig = new DefaultClientConfig(); Client client = Client.create(clientConfig); ClientResponse clientRemoteResponse = null; - WebResource.Builder wrb = client.resource(url).type(APPLICATION_JSON); - switch (restOperation) { - case PUT: - try { - clientRemoteResponse = wrb.put(ClientResponse.class, json); - } catch (UniformInterfaceException e) { - // http://stackoverflow.com/questions/12502233/jersey-uniforminterfaceexception-trying-to-proxy-to-rest-post-service - LOG.error("REST Server error. Message: {}", e.getMessage()); - } catch (ClientHandlerException e) { - if (e.getCause() instanceof ConnectException) { - LOG.error("Failed to communicate with REST Server: {} ", this.url); - } else { - LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], - e.getMessage()); - } - } finally { - if (clientRemoteResponse != null) { - if (clientRemoteResponse.getStatus() != HTTP_OK) { - throw new UniformInterfaceException(HTTP_ERROR_MSG + clientRemoteResponse.getStatus(), - clientRemoteResponse); + WebResource wr = null; + WebResource.Builder wrb = null; + try { + wrb = client.resource(url).type(APPLICATION_JSON); + } catch (Exception e) { + e.printStackTrace(); + } + + if (wrb != null) { + switch (restOperation) { + case PUT: + try { + clientRemoteResponse = wrb.put(ClientResponse.class, json); + } catch (UniformInterfaceException e) { + // http://stackoverflow.com/questions/12502233/jersey-uniforminterfaceexception-trying-to-proxy-to-rest-post-service + LOG.error("REST Server error. Message: {}", e.getMessage()); + } catch (ClientHandlerException e) { + if (e.getCause() instanceof ConnectException) { + LOG.error("Failed to communicate with REST Server: {} ", this.url); + } else { + LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], + e.getMessage()); } - clientRemoteResponse.close(); - } - } - break; - case POST: - try { - clientRemoteResponse = wrb.post(ClientResponse.class, json); - } catch (UniformInterfaceException e) { - LOG.error("REST Server error. Message: {}", e.getMessage()); - } catch (ClientHandlerException e) { - if (e.getCause() instanceof ConnectException) { - LOG.error("Failed to communicate with REST Server: {} ", this.url); - } else { - LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], - e.getMessage()); - } - } finally { - if (clientRemoteResponse != null) { - if (clientRemoteResponse.getStatus() != HTTP_OK) { - throw new UniformInterfaceException(HTTP_ERROR_MSG + clientRemoteResponse.getStatus(), - clientRemoteResponse); + } finally { + if (clientRemoteResponse != null) { + if (clientRemoteResponse.getStatus() != HTTP_OK) { + throw new UniformInterfaceException(HTTP_ERROR_MSG + clientRemoteResponse.getStatus(), + clientRemoteResponse); + } + clientRemoteResponse.close(); } - clientRemoteResponse.close(); } - } - break; - case DELETE: - try { - clientRemoteResponse = wrb.delete(ClientResponse.class); - } catch (UniformInterfaceException e) { - LOG.error("REST Server error. Message: {}", e.getMessage()); - } catch (ClientHandlerException e) { - if (e.getCause() instanceof ConnectException) { - LOG.error("Failed to communicate with REST Server: {} ", this.url); - } else { - LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], - e.getMessage()); + break; + case POST: + try { + clientRemoteResponse = wrb.post(ClientResponse.class, json); + } catch (UniformInterfaceException e) { + LOG.error("REST Server error. Message: {}", e.getMessage()); + } catch (ClientHandlerException e) { + if (e.getCause() instanceof ConnectException) { + LOG.error("Failed to communicate with REST Server: {} ", this.url); + } else { + LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], + e.getMessage()); + } + } finally { + if (clientRemoteResponse != null) { + if (clientRemoteResponse.getStatus() != HTTP_OK) { + throw new UniformInterfaceException(HTTP_ERROR_MSG + clientRemoteResponse.getStatus(), + clientRemoteResponse); + } + clientRemoteResponse.close(); + } } - } finally { - if (clientRemoteResponse != null) { - if (clientRemoteResponse.getStatus() != HTTP_OK) { - throw new UniformInterfaceException(HTTP_ERROR_MSG + clientRemoteResponse.getStatus(), - clientRemoteResponse); + break; + case DELETE: + try { + clientRemoteResponse = wrb.delete(ClientResponse.class); + } catch (UniformInterfaceException e) { + LOG.error("REST Server error. Message: {}", e.getMessage()); + } catch (ClientHandlerException e) { + if (e.getCause() instanceof ConnectException) { + LOG.error("Failed to communicate with REST Server: {} ", this.url); + } else { + LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], + e.getMessage()); + } + } finally { + if (clientRemoteResponse != null) { + if (clientRemoteResponse.getStatus() != HTTP_OK) { + throw new UniformInterfaceException(HTTP_ERROR_MSG + clientRemoteResponse.getStatus(), + clientRemoteResponse); + } + clientRemoteResponse.close(); } - clientRemoteResponse.close(); } - } + } } } } -- 2.36.6