Fix jvpp coverity issues #2 36/3536/2
authorMarek Gradzki <mgradzki@cisco.com>
Mon, 24 Oct 2016 06:06:52 +0000 (08:06 +0200)
committerDamjan Marion <dmarion.lists@gmail.com>
Mon, 24 Oct 2016 10:05:53 +0000 (10:05 +0000)
- synchronize AbstractFutureJvppInvoker.getRequests
- handle registry & jvpp close in API usage examples

Change-Id: I918bf864b8212fde04f0d9194037f1c6a810fc3f
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
12 files changed:
plugins/snat-plugin/snat/jvpp/io/fd/vpp/jvpp/snat/test/CallbackApiTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/CallbackApiTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/CallbackJVppFacadeNotificationTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/CallbackJVppFacadeTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/CallbackNotificationApiTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/ControlPingTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/CreateSubInterfaceTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/FutureApiNotificationTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/FutureApiTest.java
vpp-api/java/jvpp-core/io/fd/vpp/jvpp/core/test/L2AclTest.java
vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java
vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java

index fd462bd..e168d05 100644 (file)
@@ -48,23 +48,22 @@ public class CallbackApiTest {
 
     private static void testCallbackApi() throws Exception {
         System.out.println("Testing Java callback API for snat plugin");
-        JVppRegistry registry = new JVppRegistryImpl("SnatCallbackApiTest");
-        JVpp jvpp = new JVppSnatImpl();
+        try (final JVppRegistry registry = new JVppRegistryImpl("SnatCallbackApiTest");
+             final JVpp jvpp = new JVppSnatImpl()) {
+            registry.register(jvpp, new TestCallback());
 
-        registry.register(jvpp, new TestCallback());
+            System.out.println("Sending SnatInterfaceAddDelFeature request...");
+            SnatInterfaceAddDelFeature request = new SnatInterfaceAddDelFeature();
+            request.isAdd = 1;
+            request.isInside = 1;
+            request.swIfIndex = 1;
+            final int result = jvpp.send(request);
+            System.out.printf("SnatInterfaceAddDelFeature send result = %d%n", result);
 
-        System.out.println("Sending SnatInterfaceAddDelFeature request...");
-        SnatInterfaceAddDelFeature request = new SnatInterfaceAddDelFeature();
-        request.isAdd = 1;
-        request.isInside = 1;
-        request.swIfIndex = 1;
-        final int result = jvpp.send(request);
-        System.out.printf("SnatInterfaceAddDelFeature send result = %d%n", result);
+            Thread.sleep(1000);
 
-        Thread.sleep(1000);
-
-        System.out.println("Disconnecting...");
-        registry.close();
+            System.out.println("Disconnecting...");
+        }
         Thread.sleep(1000);
     }
 }
index 3e1eb0a..986993b 100644 (file)
@@ -33,6 +33,37 @@ import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
 
 public class CallbackApiTest {
 
+    public static void main(String[] args) throws Exception {
+        testCallbackApi();
+    }
+
+    private static void testCallbackApi() throws Exception {
+        System.out.println("Testing Java callback API with JVppRegistry");
+        try (final JVppRegistry registry = new JVppRegistryImpl("CallbackApiTest");
+             final JVpp jvpp = new JVppCoreImpl()) {
+            registry.register(jvpp, new TestCallback());
+
+            System.out.println("Sending ShowVersion request...");
+            final int result = jvpp.send(new ShowVersion());
+            System.out.printf("ShowVersion send result = %d%n", result);
+
+            System.out.println("Sending GetNodeIndex request...");
+            GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
+            getNodeIndexRequest.nodeName = "non-existing-node".getBytes();
+            jvpp.send(getNodeIndexRequest);
+
+            System.out.println("Sending SwInterfaceDump request...");
+            SwInterfaceDump swInterfaceDumpRequest = new SwInterfaceDump();
+            swInterfaceDumpRequest.nameFilterValid = 0;
+            swInterfaceDumpRequest.nameFilter = "".getBytes();
+            jvpp.send(swInterfaceDumpRequest);
+
+            Thread.sleep(1000);
+            System.out.println("Disconnecting...");
+        }
+        Thread.sleep(1000);
+    }
+
     static class TestCallback implements GetNodeIndexCallback, ShowVersionCallback, SwInterfaceCallback {
 
         @Override
@@ -43,56 +74,23 @@ public class CallbackApiTest {
         @Override
         public void onShowVersionReply(final ShowVersionReply msg) {
             System.out.printf("Received ShowVersionReply: context=%d, program=%s, version=%s, "
-                            + "buildDate=%s, buildDirectory=%s%n",
-                    msg.context, new String(msg.program), new String(msg.version),
-                    new String(msg.buildDate), new String(msg.buildDirectory));
+                    + "buildDate=%s, buildDirectory=%s%n",
+                msg.context, new String(msg.program), new String(msg.version),
+                new String(msg.buildDate), new String(msg.buildDirectory));
         }
 
         @Override
         public void onSwInterfaceDetails(final SwInterfaceDetails msg) {
             System.out.printf("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
-                            + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
-                    new String(msg.interfaceName), msg.l2AddressLength, msg.adminUpDown,
-                    msg.linkUpDown, msg.linkSpeed, (int) msg.linkMtu);
+                    + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
+                new String(msg.interfaceName), msg.l2AddressLength, msg.adminUpDown,
+                msg.linkUpDown, msg.linkSpeed, (int) msg.linkMtu);
         }
 
         @Override
         public void onError(VppCallbackException ex) {
             System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
-                    ex.getCtxId(), ex.getErrorCode());
+                ex.getCtxId(), ex.getErrorCode());
         }
     }
-
-    public static void main(String[] args) throws Exception {
-        testCallbackApi();
-    }
-
-    private static void testCallbackApi() throws Exception {
-        System.out.println("Testing Java callback API with JVppRegistry");
-        JVppRegistry registry = new JVppRegistryImpl("CallbackApiTest");
-        JVpp jvpp = new JVppCoreImpl();
-
-        registry.register(jvpp, new TestCallback());
-
-        System.out.println("Sending ShowVersion request...");
-        final int result = jvpp.send(new ShowVersion());
-        System.out.printf("ShowVersion send result = %d%n", result);
-
-        System.out.println("Sending GetNodeIndex request...");
-        GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
-        getNodeIndexRequest.nodeName = "non-existing-node".getBytes();
-        jvpp.send(getNodeIndexRequest);
-
-        System.out.println("Sending SwInterfaceDump request...");
-        SwInterfaceDump swInterfaceDumpRequest = new SwInterfaceDump();
-        swInterfaceDumpRequest.nameFilterValid = 0;
-        swInterfaceDumpRequest.nameFilter = "".getBytes();
-        jvpp.send(swInterfaceDumpRequest);
-
-        Thread.sleep(1000);
-
-        System.out.println("Disconnecting...");
-        registry.close();
-        Thread.sleep(1000);
-    }
 }
index f8845d6..d84cb03 100644 (file)
@@ -30,18 +30,17 @@ public class CallbackJVppFacadeNotificationTest {
     private static void testCallbackFacade() throws Exception {
         System.out.println("Testing CallbackJVppFacade for notifications");
 
-        final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeTest");
-        final JVppCore jvpp = new JVppCoreImpl();
+        try (final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeTest");
+             final JVppCore jvpp = new JVppCoreImpl()) {
+            final CallbackJVppCoreFacade jvppCallbackFacade = new CallbackJVppCoreFacade(registry, jvpp);
+            System.out.println("Successfully connected to VPP");
 
-        CallbackJVppCoreFacade jvppCallbackFacade = new CallbackJVppCoreFacade(registry, jvpp);
-        System.out.println("Successfully connected to VPP");
-
-        final AutoCloseable notificationListenerReg =
+            final AutoCloseable notificationListenerReg =
                 jvppCallbackFacade.getNotificationRegistry().registerSwInterfaceSetFlagsNotificationCallback(
-                        NotificationUtils::printNotification
+                    NotificationUtils::printNotification
                 );
 
-        jvppCallbackFacade.wantInterfaceEvents(NotificationUtils.getEnableInterfaceNotificationsReq(),
+            jvppCallbackFacade.wantInterfaceEvents(NotificationUtils.getEnableInterfaceNotificationsReq(),
                 new WantInterfaceEventsCallback() {
                     @Override
                     public void onWantInterfaceEventsReply(final WantInterfaceEventsReply reply) {
@@ -51,16 +50,16 @@ public class CallbackJVppFacadeNotificationTest {
                     @Override
                     public void onError(final VppCallbackException ex) {
                         System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n",
-                                ex.getMethodName(), ex.getCtxId(), ex.getErrorCode());
+                            ex.getMethodName(), ex.getCtxId(), ex.getErrorCode());
                     }
                 });
 
-        System.out.println("Changing interface configuration");
-        NotificationUtils.getChangeInterfaceState().send(jvpp);
+            System.out.println("Changing interface configuration");
+            NotificationUtils.getChangeInterfaceState().send(jvpp);
 
-        Thread.sleep(1000);
+            Thread.sleep(1000);
 
-        jvppCallbackFacade.wantInterfaceEvents(NotificationUtils.getDisableInterfaceNotificationsReq(),
+            jvppCallbackFacade.wantInterfaceEvents(NotificationUtils.getDisableInterfaceNotificationsReq(),
                 new WantInterfaceEventsCallback() {
                     @Override
                     public void onWantInterfaceEventsReply(final WantInterfaceEventsReply reply) {
@@ -70,16 +69,15 @@ public class CallbackJVppFacadeNotificationTest {
                     @Override
                     public void onError(final VppCallbackException ex) {
                         System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n",
-                                ex.getMethodName(), ex.getCtxId(), ex.getErrorCode());
+                            ex.getMethodName(), ex.getCtxId(), ex.getErrorCode());
                     }
                 });
 
-        notificationListenerReg.close();
-
-        Thread.sleep(2000);
+            notificationListenerReg.close();
 
-        System.out.println("Disconnecting...");
-        registry.close();
+            Thread.sleep(2000);
+            System.out.println("Disconnecting...");
+        }
         Thread.sleep(1000);
     }
 
index ac79b09..9f7cb8d 100644 (file)
@@ -19,7 +19,6 @@ package io.fd.vpp.jvpp.core.test;
 import io.fd.vpp.jvpp.JVppRegistry;
 import io.fd.vpp.jvpp.JVppRegistryImpl;
 import io.fd.vpp.jvpp.VppCallbackException;
-import io.fd.vpp.jvpp.core.JVppCore;
 import io.fd.vpp.jvpp.core.JVppCoreImpl;
 import io.fd.vpp.jvpp.core.callback.GetNodeIndexCallback;
 import io.fd.vpp.jvpp.core.callback.ShowVersionCallback;
@@ -38,14 +37,14 @@ public class CallbackJVppFacadeTest {
         @Override
         public void onShowVersionReply(final ShowVersionReply msg) {
             System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, program=%s,"
-                            + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context, new String(msg.program),
-                    new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
+                    + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context, new String(msg.program),
+                new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
         }
 
         @Override
         public void onError(VppCallbackException ex) {
             System.out.printf("Received onError exception in showVersionCallback1: call=%s, reply=%d, context=%d%n",
-                    ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
+                ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
         }
     };
 
@@ -53,14 +52,14 @@ public class CallbackJVppFacadeTest {
         @Override
         public void onShowVersionReply(final ShowVersionReply msg) {
             System.out.printf("ShowVersionCallback2 received ShowVersionReply: context=%d, program=%s,"
-                            + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context, new String(msg.program),
-                    new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
+                    + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context, new String(msg.program),
+                new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
         }
 
         @Override
         public void onError(VppCallbackException ex) {
             System.out.printf("Received onError exception in showVersionCallback2: call=%s, reply=%d, context=%d%n",
-                    ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
+                ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
         }
 
     };
@@ -74,30 +73,27 @@ public class CallbackJVppFacadeTest {
         @Override
         public void onError(VppCallbackException ex) {
             System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d%n",
-                    ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
+                ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
         }
     };
 
     private static void testCallbackFacade() throws Exception {
         System.out.println("Testing CallbackJVppFacade");
 
-        final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeTest");
-        final JVppCore jvpp = new JVppCoreImpl();
+        try (final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeTest");
+             final CallbackJVppCoreFacade callbackFacade = new CallbackJVppCoreFacade(registry, new JVppCoreImpl())) {
+            System.out.println("Successfully connected to VPP");
 
-        CallbackJVppCoreFacade jvppCallbackFacade = new CallbackJVppCoreFacade(registry, jvpp);
-        System.out.println("Successfully connected to VPP");
+            callbackFacade.showVersion(showVersionCallback1);
+            callbackFacade.showVersion(showVersionCallback2);
 
-        jvppCallbackFacade.showVersion(showVersionCallback1);
-        jvppCallbackFacade.showVersion(showVersionCallback2);
+            GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
+            getNodeIndexRequest.nodeName = "dummyNode".getBytes();
+            callbackFacade.getNodeIndex(getNodeIndexRequest, getNodeIndexCallback);
 
-        GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
-        getNodeIndexRequest.nodeName = "dummyNode".getBytes();
-        jvppCallbackFacade.getNodeIndex(getNodeIndexRequest, getNodeIndexCallback);
-
-        Thread.sleep(2000);
-
-        System.out.println("Disconnecting...");
-        registry.close();
+            Thread.sleep(2000);
+            System.out.println("Disconnecting...");
+        }
         Thread.sleep(1000);
     }
 
index a4b8702..a9f71f1 100644 (file)
@@ -35,12 +35,42 @@ import io.fd.vpp.jvpp.core.dto.WantInterfaceEventsReply;
 
 public class CallbackNotificationApiTest {
 
+    private static void testCallbackApi() throws Exception {
+        System.out.println("Testing Java callback API for notifications");
+        try (final JVppRegistry registry = new JVppRegistryImpl("CallbackNotificationTest");
+             final JVpp jvpp = new JVppCoreImpl()) {
+            registry.register(jvpp, new TestCallback());
+            System.out.println("Successfully connected to VPP");
+
+            getEnableInterfaceNotificationsReq().send(jvpp);
+            System.out.println("Interface notifications started");
+            // TODO test ifc dump which also triggers interface flags send
+
+            System.out.println("Changing interface configuration");
+            getChangeInterfaceState().send(jvpp);
+
+            // Notifications are received
+            Thread.sleep(500);
+
+            getDisableInterfaceNotificationsReq().send(jvpp);
+            System.out.println("Interface events stopped");
+
+            Thread.sleep(2000);
+            System.out.println("Disconnecting...");
+        }
+        Thread.sleep(1000);
+    }
+
+    public static void main(String[] args) throws Exception {
+        testCallbackApi();
+    }
+
     private static class TestCallback implements SwInterfaceSetFlagsNotificationCallback,
-            WantInterfaceEventsCallback, SwInterfaceSetFlagsCallback {
+        WantInterfaceEventsCallback, SwInterfaceSetFlagsCallback {
 
         @Override
         public void onSwInterfaceSetFlagsNotification(
-                final SwInterfaceSetFlagsNotification msg) {
+            final SwInterfaceSetFlagsNotification msg) {
             printNotification(msg);
         }
 
@@ -57,40 +87,8 @@ public class CallbackNotificationApiTest {
         @Override
         public void onError(VppCallbackException ex) {
             System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d%n",
-                    ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
+                ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
 
         }
     }
-
-    private static void testCallbackApi() throws Exception {
-        System.out.println("Testing Java callback API for notifications");
-        JVppRegistry registry = new JVppRegistryImpl("CallbackNotificationTest");
-        JVpp jvpp = new JVppCoreImpl();
-
-        registry.register(jvpp, new TestCallback());
-        System.out.println("Successfully connected to VPP");
-
-        getEnableInterfaceNotificationsReq().send(jvpp);
-        System.out.println("Interface notifications started");
-        // TODO test ifc dump which also triggers interface flags send
-
-        System.out.println("Changing interface configuration");
-        getChangeInterfaceState().send(jvpp);
-
-        // Notifications are received
-        Thread.sleep(500);
-
-        getDisableInterfaceNotificationsReq().send(jvpp);
-        System.out.println("Interface events stopped");
-
-        Thread.sleep(2000);
-
-        System.out.println("Disconnecting...");
-        registry.close();
-        Thread.sleep(1000);
-    }
-
-    public static void main(String[] args) throws Exception {
-        testCallbackApi();
-    }
 }
index ecf61a5..e97f4e3 100644 (file)
@@ -20,8 +20,8 @@ import io.fd.vpp.jvpp.JVpp;
 import io.fd.vpp.jvpp.JVppRegistry;
 import io.fd.vpp.jvpp.JVppRegistryImpl;
 import io.fd.vpp.jvpp.VppCallbackException;
-import io.fd.vpp.jvpp.core.JVppCoreImpl;
 import io.fd.vpp.jvpp.callback.ControlPingCallback;
+import io.fd.vpp.jvpp.core.JVppCoreImpl;
 import io.fd.vpp.jvpp.dto.ControlPing;
 import io.fd.vpp.jvpp.dto.ControlPingReply;
 
@@ -29,37 +29,36 @@ public class ControlPingTest {
 
     private static void testControlPing() throws Exception {
         System.out.println("Testing ControlPing using Java callback API");
-        JVppRegistry registry = new JVppRegistryImpl("ControlPingTest");
-        JVpp jvpp = new JVppCoreImpl();
+        try (JVppRegistry registry = new JVppRegistryImpl("ControlPingTest");
+             JVpp jvpp = new JVppCoreImpl()) {
 
-        registry.register(jvpp, new ControlPingCallback() {
-            @Override
-            public void onControlPingReply(final ControlPingReply reply) {
-                System.out.printf("Received ControlPingReply: %s%n", reply);
-            }
+            registry.register(jvpp, new ControlPingCallback() {
+                @Override
+                public void onControlPingReply(final ControlPingReply reply) {
+                    System.out.printf("Received ControlPingReply: %s%n", reply);
+                }
 
-            @Override
-            public void onError(VppCallbackException ex) {
-                System.out.printf("Received onError exception: call=%s, reply=%d, context=%d ", ex.getMethodName(),
+                @Override
+                public void onError(VppCallbackException ex) {
+                    System.out.printf("Received onError exception: call=%s, reply=%d, context=%d ", ex.getMethodName(),
                         ex.getErrorCode(), ex.getCtxId());
-            }
-
-        });
-        System.out.println("Successfully connected to VPP");
-        Thread.sleep(1000);
+                }
 
-        System.out.println("Sending control ping using JVppRegistry");
-        registry.controlPing(jvpp.getClass());
+            });
+            System.out.println("Successfully connected to VPP");
+            Thread.sleep(1000);
 
-        Thread.sleep(2000);
+            System.out.println("Sending control ping using JVppRegistry");
+            registry.controlPing(jvpp.getClass());
 
-        System.out.println("Sending control ping using JVpp plugin");
-        jvpp.send(new ControlPing());
+            Thread.sleep(2000);
 
-        Thread.sleep(2000);
+            System.out.println("Sending control ping using JVpp plugin");
+            jvpp.send(new ControlPing());
 
-        System.out.println("Disconnecting...");
-        registry.close();
+            Thread.sleep(2000);
+            System.out.println("Disconnecting...");
+        }
         Thread.sleep(1000);
     }
 
index 4360584..a96258f 100644 (file)
@@ -18,7 +18,6 @@ package io.fd.vpp.jvpp.core.test;
 
 import static java.util.Objects.requireNonNull;
 
-import io.fd.vpp.jvpp.JVpp;
 import io.fd.vpp.jvpp.JVppRegistry;
 import io.fd.vpp.jvpp.JVppRegistryImpl;
 import io.fd.vpp.jvpp.core.JVppCoreImpl;
@@ -54,8 +53,8 @@ public class CreateSubInterfaceTest {
     private static void requireSingleIface(final SwInterfaceDetailsReplyDump response, final String ifaceName) {
         if (response.swInterfaceDetails.size() != 1) {
             throw new IllegalStateException(
-                    String.format("Expected one interface matching filter %s but was %d", ifaceName,
-                            response.swInterfaceDetails.size()));
+                String.format("Expected one interface matching filter %s but was %d", ifaceName,
+                    response.swInterfaceDetails.size()));
         }
     }
 
@@ -82,38 +81,36 @@ public class CreateSubInterfaceTest {
 
     private static void testCreateSubInterface() throws Exception {
         System.out.println("Testing sub-interface creation using Java callback API");
-        final JVppRegistry registry = new JVppRegistryImpl("CreateSubInterface");
-        final JVpp jvpp = new JVppCoreImpl();
-        final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
+        try (final JVppRegistry registry = new JVppRegistryImpl("CreateSubInterface");
+             final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
+            System.out.println("Successfully connected to VPP");
+            Thread.sleep(1000);
 
-        System.out.println("Successfully connected to VPP");
-        Thread.sleep(1000);
-
-        final String ifaceName = "GigabitEthernet0/8/0";
+            final String ifaceName = "GigabitEthernet0/8/0";
 
-        final SwInterfaceDetailsReplyDump swInterfaceDetails =
+            final SwInterfaceDetailsReplyDump swInterfaceDetails =
                 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(ifaceName)).toCompletableFuture().get();
 
-        requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
-        requireNonNull(swInterfaceDetails.swInterfaceDetails, "swInterfaceDetails is null");
-        requireSingleIface(swInterfaceDetails, ifaceName);
+            requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
+            requireNonNull(swInterfaceDetails.swInterfaceDetails, "swInterfaceDetails is null");
+            requireSingleIface(swInterfaceDetails, ifaceName);
 
-        final int swIfIndex = swInterfaceDetails.swInterfaceDetails.get(0).swIfIndex;
-        final int subId = 1;
+            final int swIfIndex = swInterfaceDetails.swInterfaceDetails.get(0).swIfIndex;
+            final int subId = 1;
 
-        final CreateSubifReply createSubifReply =
+            final CreateSubifReply createSubifReply =
                 jvppFacade.createSubif(createSubifRequest(swIfIndex, subId)).toCompletableFuture().get();
-        print(createSubifReply);
+            print(createSubifReply);
 
-        final String subIfaceName = "GigabitEthernet0/8/0." + subId;
-        final SwInterfaceDetailsReplyDump subIface =
+            final String subIfaceName = "GigabitEthernet0/8/0." + subId;
+            final SwInterfaceDetailsReplyDump subIface =
                 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(subIfaceName)).toCompletableFuture().get();
-        requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
-        requireNonNull(subIface.swInterfaceDetails, "swInterfaceDump returned null");
-        requireSingleIface(swInterfaceDetails, ifaceName);
+            requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
+            requireNonNull(subIface.swInterfaceDetails, "swInterfaceDump returned null");
+            requireSingleIface(swInterfaceDetails, ifaceName);
 
-        System.out.println("Disconnecting...");
-        registry.close();
+            System.out.println("Disconnecting...");
+        }
         Thread.sleep(1000);
     }
 
index 2ba1b09..9efeae1 100644 (file)
@@ -20,7 +20,6 @@ import static io.fd.vpp.jvpp.core.test.NotificationUtils.getChangeInterfaceState
 import static io.fd.vpp.jvpp.core.test.NotificationUtils.getDisableInterfaceNotificationsReq;
 import static io.fd.vpp.jvpp.core.test.NotificationUtils.getEnableInterfaceNotificationsReq;
 
-import io.fd.vpp.jvpp.JVpp;
 import io.fd.vpp.jvpp.JVppRegistry;
 import io.fd.vpp.jvpp.JVppRegistryImpl;
 import io.fd.vpp.jvpp.core.JVppCoreImpl;
@@ -30,32 +29,24 @@ public class FutureApiNotificationTest {
 
     private static void testFutureApi() throws Exception {
         System.out.println("Testing Java future API for notifications");
-
-        final JVppRegistry registry = new JVppRegistryImpl("FutureApiNotificationTest");
-        final JVpp jvpp = new JVppCoreImpl();
-        final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
-
-        System.out.println("Successfully connected to VPP");
-
-        final AutoCloseable notificationListenerReg =
-                jvppFacade.getNotificationRegistry()
-                        .registerSwInterfaceSetFlagsNotificationCallback(NotificationUtils::printNotification);
-
-        jvppFacade.wantInterfaceEvents(getEnableInterfaceNotificationsReq()).toCompletableFuture().get();
-        System.out.println("Interface events started");
-
-        System.out.println("Changing interface configuration");
-        jvppFacade.swInterfaceSetFlags(getChangeInterfaceState()).toCompletableFuture().get();
-
-        Thread.sleep(1000);
-
-        jvppFacade.wantInterfaceEvents(getDisableInterfaceNotificationsReq()).toCompletableFuture().get();
-        System.out.println("Interface events stopped");
-
-        notificationListenerReg.close();
-
-        System.out.println("Disconnecting...");
-        registry.close();
+        try (final JVppRegistry registry = new JVppRegistryImpl("FutureApiNotificationTest");
+             final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl());
+             final AutoCloseable notificationListenerReg =
+                 jvppFacade.getNotificationRegistry()
+                     .registerSwInterfaceSetFlagsNotificationCallback(NotificationUtils::printNotification)) {
+            System.out.println("Successfully connected to VPP");
+            jvppFacade.wantInterfaceEvents(getEnableInterfaceNotificationsReq()).toCompletableFuture().get();
+            System.out.println("Interface events started");
+
+            System.out.println("Changing interface configuration");
+            jvppFacade.swInterfaceSetFlags(getChangeInterfaceState()).toCompletableFuture().get();
+
+            Thread.sleep(1000);
+
+            jvppFacade.wantInterfaceEvents(getDisableInterfaceNotificationsReq()).toCompletableFuture().get();
+            System.out.println("Interface events stopped");
+            System.out.println("Disconnecting...");
+        }
     }
 
     public static void main(String[] args) throws Exception {
index 80c1108..f478bab 100644 (file)
 
 package io.fd.vpp.jvpp.core.test;
 
-import java.util.Objects;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.Future;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import io.fd.vpp.jvpp.JVpp;
 import io.fd.vpp.jvpp.JVppRegistry;
 import io.fd.vpp.jvpp.JVppRegistryImpl;
 import io.fd.vpp.jvpp.core.JVppCoreImpl;
@@ -35,6 +29,11 @@ import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
+import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 public class FutureApiTest {
 
@@ -45,10 +44,10 @@ public class FutureApiTest {
         final Future<ShowVersionReply> replyFuture = jvpp.showVersion(new ShowVersion()).toCompletableFuture();
         final ShowVersionReply reply = replyFuture.get();
         LOG.info(
-                String.format(
-                        "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s%n",
-                        reply.context, new String(reply.program), new String(reply.version), new String(reply.buildDate),
-                        new String(reply.buildDirectory)));
+            String.format(
+                "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s%n",
+                reply.context, new String(reply.program), new String(reply.version), new String(reply.buildDate),
+                new String(reply.buildDirectory)));
     }
 
     private static void testEmptyBridgeDomainDump(final FutureJVppCoreFacade jvpp) throws Exception {
@@ -57,16 +56,16 @@ public class FutureApiTest {
         request.bdId = -1; // dump call
 
         final CompletableFuture<BridgeDomainDetailsReplyDump>
-                replyFuture = jvpp.bridgeDomainDump(request).toCompletableFuture();
+            replyFuture = jvpp.bridgeDomainDump(request).toCompletableFuture();
         final BridgeDomainDetailsReplyDump reply = replyFuture.get();
 
         if (reply == null || reply.bridgeDomainDetails == null) {
             LOG.severe("Received null response for empty dump: " + reply);
         } else {
             LOG.info(
-                    String.format(
-                            "Received empty bridge-domain dump reply with list of bridge-domains: %s, %s",
-                            reply.bridgeDomainDetails, reply.bridgeDomainSwIfDetails));
+                String.format(
+                    "Received empty bridge-domain dump reply with list of bridge-domains: %s, %s",
+                    reply.bridgeDomainDetails, reply.bridgeDomainSwIfDetails));
         }
     }
 
@@ -78,8 +77,8 @@ public class FutureApiTest {
         try {
             final GetNodeIndexReply reply = replyFuture.get();
             LOG.info(
-                    String.format(
-                            "Received GetNodeIndexReply: context=%d, nodeIndex=%d%n", reply.context, reply.nodeIndex));
+                String.format(
+                    "Received GetNodeIndexReply: context=%d, nodeIndex=%d%n", reply.context, reply.nodeIndex));
         } catch (Exception e) {
             LOG.log(Level.SEVERE, "GetNodeIndex request failed", e);
         }
@@ -96,28 +95,26 @@ public class FutureApiTest {
         for (SwInterfaceDetails details : reply.swInterfaceDetails) {
             Objects.requireNonNull(details, "reply.swInterfaceDetails contains null element!");
             LOG.info(
-                    String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
-                                    + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
-                            new String(details.interfaceName), details.l2AddressLength, details.adminUpDown,
-                            details.linkUpDown, details.linkSpeed, (int) details.linkMtu));
+                String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
+                        + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
+                    new String(details.interfaceName), details.l2AddressLength, details.adminUpDown,
+                    details.linkUpDown, details.linkSpeed, (int) details.linkMtu));
         }
     }
 
     private static void testFutureApi() throws Exception {
         LOG.info("Testing Java future API");
+        try (final JVppRegistry registry = new JVppRegistryImpl("FutureApiTest");
+             final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
+            LOG.info("Successfully connected to VPP");
 
-        final JVppRegistry registry = new JVppRegistryImpl("FutureApiTest");
-        final JVpp jvpp = new JVppCoreImpl();
-        final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
-        LOG.info("Successfully connected to VPP");
-
-        testEmptyBridgeDomainDump(jvppFacade);
-        testShowVersion(jvppFacade);
-        testGetNodeIndex(jvppFacade);
-        testSwInterfaceDump(jvppFacade);
+            testEmptyBridgeDomainDump(jvppFacade);
+            testShowVersion(jvppFacade);
+            testGetNodeIndex(jvppFacade);
+            testSwInterfaceDump(jvppFacade);
 
-        LOG.info("Disconnecting...");
-        registry.close();
+            LOG.info("Disconnecting...");
+        }
     }
 
     public static void main(String[] args) throws Exception {
index d7da5ec..6b3fa99 100644 (file)
@@ -16,8 +16,6 @@
 
 package io.fd.vpp.jvpp.core.test;
 
-import javax.xml.bind.DatatypeConverter;
-import io.fd.vpp.jvpp.JVpp;
 import io.fd.vpp.jvpp.JVppRegistry;
 import io.fd.vpp.jvpp.JVppRegistryImpl;
 import io.fd.vpp.jvpp.core.JVppCoreImpl;
@@ -36,6 +34,7 @@ import io.fd.vpp.jvpp.core.dto.ClassifyTableInfoReply;
 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
+import javax.xml.bind.DatatypeConverter;
 
 /**
  * <p>Tests L2 ACL creation and read.<br> Equivalent to the following vppctl commands:<br>
@@ -63,8 +62,8 @@ public class L2AclTest {
         request.skipNVectors = 0;
         request.matchNVectors = 1;
         request.mask =
-                new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
-                        (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00};
+            new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00};
         return request;
     }
 
@@ -83,8 +82,8 @@ public class L2AclTest {
         request.advance = 0; // default
         // match 01:02:03:04:05:06 mac address
         request.match =
-                new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
-                        (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
+            new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
+                (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
         return request;
     }
 
@@ -147,46 +146,46 @@ public class L2AclTest {
 
     private static void testL2Acl() throws Exception {
         System.out.println("Testing L2 ACLs using Java callback API");
-        final JVppRegistry registry = new JVppRegistryImpl("L2AclTest");
-        final JVpp jvpp = new JVppCoreImpl();
-        final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
+        try (final JVppRegistry registry = new JVppRegistryImpl("L2AclTest");
+             final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
 
-        System.out.println("Successfully connected to VPP");
-        Thread.sleep(1000);
+            System.out.println("Successfully connected to VPP");
+            Thread.sleep(1000);
 
-        final ClassifyAddDelTableReply classifyAddDelTableReply =
+            final ClassifyAddDelTableReply classifyAddDelTableReply =
                 jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get();
-        print(classifyAddDelTableReply);
+            print(classifyAddDelTableReply);
 
-        final ClassifyTableIdsReply classifyTableIdsReply =
+            final ClassifyTableIdsReply classifyTableIdsReply =
                 jvppFacade.classifyTableIds(new ClassifyTableIds()).toCompletableFuture().get();
-        print(classifyTableIdsReply);
+            print(classifyTableIdsReply);
 
-        final ClassifyTableInfoReply classifyTableInfoReply =
+            final ClassifyTableInfoReply classifyTableInfoReply =
                 jvppFacade.classifyTableInfo(createClassifyTableInfoRequest(classifyAddDelTableReply.newTableIndex))
-                        .toCompletableFuture().get();
-        print(classifyTableInfoReply);
+                    .toCompletableFuture().get();
+            print(classifyTableInfoReply);
 
-        final ClassifyAddDelSessionReply classifyAddDelSessionReply =
+            final ClassifyAddDelSessionReply classifyAddDelSessionReply =
                 jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex))
-                        .toCompletableFuture().get();
-        print(classifyAddDelSessionReply);
+                    .toCompletableFuture().get();
+            print(classifyAddDelSessionReply);
 
-        final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump =
+            final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump =
                 jvppFacade.classifySessionDump(createClassifySessionDumpRequest(classifyAddDelTableReply.newTableIndex))
-                        .toCompletableFuture().get();
-        print(classifySessionDetailsReplyDump);
+                    .toCompletableFuture().get();
+            print(classifySessionDetailsReplyDump);
 
-        final InputAclSetInterfaceReply inputAclSetInterfaceReply =
+            final InputAclSetInterfaceReply inputAclSetInterfaceReply =
                 jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get();
-        print(inputAclSetInterfaceReply);
+            print(inputAclSetInterfaceReply);
 
-        final ClassifyTableByInterfaceReply classifyTableByInterfaceReply =
-                jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture().get();
-        print(classifyTableByInterfaceReply);
+            final ClassifyTableByInterfaceReply classifyTableByInterfaceReply =
+                jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture()
+                    .get();
+            print(classifyTableByInterfaceReply);
 
-        System.out.println("Disconnecting...");
-        registry.close();
+            System.out.println("Disconnecting...");
+        }
         Thread.sleep(1000);
     }
 
index ed8b8db..98e140e 100644 (file)
@@ -51,7 +51,7 @@ public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker {
         this.requests = Objects.requireNonNull(requestMap, "Null requestMap");
     }
 
-    protected final Map<Integer, CompletableFuture<? extends JVppReply<?>>> getRequests() {
+    protected synchronized final Map<Integer, CompletableFuture<? extends JVppReply<?>>> getRequests() {
         return this.requests;
     }
 
index ea1d864..27b4d29 100644 (file)
@@ -27,14 +27,15 @@ public class ConnectionTest {
 
     private static void testConnect() throws Exception {
         System.out.println("Testing JNI connection with JVppRegistry");
-        JVppRegistry registry = new JVppRegistryImpl("ConnectionTest");
-        System.out.println("Successfully connected to vpp");
-
-        Thread.sleep(5000);
-
-        System.out.println("Disconnecting...");
-        registry.close();
-        Thread.sleep(1000);
+        final JVppRegistry registry = new JVppRegistryImpl("ConnectionTest");
+        try {
+            System.out.println("Successfully connected to vpp");
+            Thread.sleep(5000);
+            System.out.println("Disconnecting...");
+            Thread.sleep(1000);
+        } finally {
+            registry.close();
+        }
     }
 
     public static void main(String[] args) throws Exception {