Bug 4528 - SFC SBRest Broken 90/28790/2
authorReinaldo Penno <[email protected]>
Mon, 26 Oct 2015 08:12:19 +0000 (01:12 -0700)
committerReinaldo Penno <[email protected]>
Mon, 26 Oct 2015 08:42:32 +0000 (01:42 -0700)
- 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 <[email protected]>
sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestAclTask.java
sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestRspTask.java
sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfTask.java
sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSffTask.java
sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfgTask.java
sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/SbRestSfstateTask.java
sfc-sb-rest/src/main/java/org/opendaylight/sfc/sbrest/provider/task/WsTask.java

index 704c2d395e96928188a42704af8bde8a8fb75928..5f3dc145bb3d78571d2e1967d1cd24e240f148d0 100644 (file)
@@ -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/";
index b401d0568a3973ff4f431a8001cb1e023aee885e..d75f4b307b3d9958b98016e29431d9c7190c4cce 100755 (executable)
@@ -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);
+                    }
                 }
             }
         }
index 9b8ca77614146023664fbe9f057e481b3f27eda1..30ab3724d06d7de3dc168d90635531a594a90295 100755 (executable)
@@ -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;
         }
index 3ecd658187f297dd804754bcd35bfa3f0c3910dd..cca5e7ed15c2d38982a9aa08d1a6b786d88b9fec 100755 (executable)
@@ -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;
         }
index 8a4097a8ea5eb7aa3fb915829f65b4cb13b56d64..78f4cf85634106a42772e1655dbfdbe20382c71d 100644 (file)
@@ -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/";
index 081d812e3085862f7be2eb080889ed01b0e02059..d4370a884b4402729b61342600d0d4596b3a7a07 100644 (file)
@@ -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;
             }
index 754cdf454effd9f893dc32602e129dff3db41890..c399c753a9cb65a7514554ffb3a38eaacaa55141 100755 (executable)
@@ -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();
                     }
-                }
+            }
         }
     }
 }