BUG-4715: SalEchoService return value fix 20/30920/8
authorJozef Bacigal <jbacigal@cisco.com>
Mon, 7 Dec 2015 16:46:45 +0000 (17:46 +0100)
committerJozef Bacigal <jbacigal@cisco.com>
Fri, 11 Dec 2015 15:40:42 +0000 (16:40 +0100)
Change-Id: I6e288652efcadfd0a21b47372039aed5a55cc207
Signed-off-by: Jozef Bacigal <jbacigal@cisco.com>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/EchoService.java [new file with mode: 0644]
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalEchoServiceImpl.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/services/EchoServiceTest.java [new file with mode: 0644]
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/services/SalEchoServiceImplTest.java

diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/EchoService.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/EchoService.java
new file mode 100644 (file)
index 0000000..8afe07e
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.openflowplugin.impl.services;
+
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
+import org.opendaylight.openflowplugin.api.openflow.device.Xid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+
+public class EchoService extends AbstractSimpleService<EchoInputBuilder, EchoOutput> {
+    public EchoService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
+        super(requestContextStack, deviceContext, EchoOutput.class);
+    }
+
+    @Override
+    protected OfHeader buildRequest(final Xid xid, final EchoInputBuilder input) throws Exception {
+        return input
+                .setXid(xid.getValue())
+                .setVersion(getVersion())
+                .build();
+    }
+}
\ No newline at end of file
index ce952bf1d1bc165d39ad422d8570418b9da6655b..750a63a77269be2fb044e21f4921c6e5786d8728 100644 (file)
@@ -7,33 +7,58 @@
  */
 package org.opendaylight.openflowplugin.impl.services;
 
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.concurrent.Future;
+import javax.annotation.Nullable;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
-import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SalEchoService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+
+public final class SalEchoServiceImpl implements SalEchoService {
+
+    private final EchoService echoService;
 
-public final class SalEchoServiceImpl extends AbstractSimpleService<SendEchoInput, SendEchoOutput> implements SalEchoService {
     public SalEchoServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
-        super(requestContextStack, deviceContext, SendEchoOutput.class);
+        echoService = new EchoService(requestContextStack, deviceContext);
     }
 
     @Override
     public Future<RpcResult<SendEchoOutput>> sendEcho(final SendEchoInput sendEchoInput) {
-        return handleServiceCall(sendEchoInput);
+        final EchoInputBuilder echoInputBld = new EchoInputBuilder()
+                .setData(sendEchoInput.getData());
+        return transform(echoService.handleServiceCall(echoInputBld));
     }
 
-    @Override
-    protected OfHeader buildRequest(final Xid xid, final SendEchoInput input) {
-        final EchoInputBuilder echoInputOFJavaBuilder = new EchoInputBuilder();
-        echoInputOFJavaBuilder.setVersion(getVersion());
-        echoInputOFJavaBuilder.setXid(xid.getValue());
-        echoInputOFJavaBuilder.setData(input.getData());
-        return echoInputOFJavaBuilder.build();
+    private Future<RpcResult<SendEchoOutput>> transform(final ListenableFuture<RpcResult<EchoOutput>> rpcResultListenableFuture) {
+        return Futures.transform(rpcResultListenableFuture, new Function<RpcResult<EchoOutput>, RpcResult<SendEchoOutput>>() {
+            @Nullable
+            @Override
+            public RpcResult<SendEchoOutput> apply(@Nullable final RpcResult<EchoOutput> input) {
+                Preconditions.checkNotNull(input, "echoOutput value is never expected to be NULL");
+                final RpcResult<SendEchoOutput> rpcOutput;
+                if (input.isSuccessful()) {
+                    final SendEchoOutput sendEchoOutput = new SendEchoOutputBuilder()
+                            .setData(input.getResult().getData())
+                            .build();
+                    rpcOutput = RpcResultBuilder.success(sendEchoOutput).build();
+                } else {
+                    rpcOutput = RpcResultBuilder.<SendEchoOutput>failed()
+                            .withRpcErrors(input.getErrors())
+                            .build();
+                }
+                return rpcOutput;
+            }
+        });
     }
+
 }
diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/services/EchoServiceTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/services/EchoServiceTest.java
new file mode 100644 (file)
index 0000000..3e453b7
--- /dev/null
@@ -0,0 +1,43 @@
+package org.opendaylight.openflowplugin.impl.services;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Test;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.device.Xid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+
+public class EchoServiceTest extends ServiceMocking {
+
+    private static final Long DUMMY_XID_VALUE = 100L;
+    private static final byte[] DUMMY_DATA = "DUMMY DATA".getBytes();
+    EchoService echoService;
+
+    @Override
+    public void setup() {
+        echoService = new EchoService(mockedRequestContextStack, mockedDeviceContext);
+    }
+
+    @Test
+    public void testSendEcho() throws Exception {
+        EchoInputBuilder sendEchoInput = new EchoInputBuilder();
+        echoService.handleServiceCall(sendEchoInput);
+        verify(mockedRequestContextStack).createRequestContext();
+    }
+
+    @Test
+    public void testBuildRequest() throws Exception {
+        EchoInputBuilder sendEchoInput = new EchoInputBuilder().setData(DUMMY_DATA);
+        final OfHeader request = this.echoService.buildRequest(new Xid(DUMMY_XID_VALUE), sendEchoInput);
+        assertEquals(DUMMY_XID_VALUE, request.getXid());
+        assertTrue(request instanceof EchoInput);
+        final byte[] data = ((EchoInput) request).getData();
+        assertArrayEquals(DUMMY_DATA, data);
+        assertEquals(OFConstants.OFP_VERSION_1_3, request.getVersion().shortValue());
+    }
+}
\ No newline at end of file
index 60d3c1747915b066825f8bf913bad9ec789c82db..3564d2836a4544f66a09a508e8f3a31f294057b4 100644 (file)
@@ -1,38 +1,60 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.openflowplugin.impl.services;
 
+import static org.mockito.Mockito.verify;
+
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import java.util.concurrent.Future;
+import org.junit.Assert;
 import org.junit.Test;
-import org.opendaylight.openflowplugin.api.openflow.device.Xid;
+import org.mockito.Matchers;
+import org.mockito.Mockito;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
-
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.verify;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
 public class SalEchoServiceImplTest extends ServiceMocking {
 
-    private static final Long DUMMY_XID_VALUE = 100L;
     private static final byte[] DUMMY_DATA = "DUMMY DATA".getBytes();
     SalEchoServiceImpl salEchoService;
 
-    @Test
-    public void testSendEcho() throws Exception {
+    @Override
+    protected void setup() {
         salEchoService = new SalEchoServiceImpl(mockedRequestContextStack, mockedDeviceContext);
-        SendEchoInput sendEchoInput = new SendEchoInputBuilder().build();
-        salEchoService.sendEcho(sendEchoInput);
-        verify(mockedRequestContextStack).createRequestContext();;
     }
 
     @Test
-    public void testBuildRequest() throws Exception {
-        salEchoService = new SalEchoServiceImpl(mockedRequestContextStack, mockedDeviceContext);
-        SendEchoInput sendEchoInput = new SendEchoInputBuilder().setData(DUMMY_DATA).build();
-        final OfHeader request = this.salEchoService.buildRequest(new Xid(DUMMY_XID_VALUE), sendEchoInput);
-        assertEquals(DUMMY_XID_VALUE, request.getXid());
-        assertTrue(request instanceof EchoInput);
-        final byte[] data = ((EchoInput) request).getData();
-        assertArrayEquals(DUMMY_DATA, data);
+    public void testSendEcho() throws Exception {
+        final EchoOutput echoOut = new EchoOutputBuilder()
+                .setData(DUMMY_DATA)
+                .build();
+        final RpcResult<EchoOutput> replyRpcResult = RpcResultBuilder.success(echoOut).build();
+        final ListenableFuture<RpcResult<EchoOutput>> replyFt = Futures.immediateFuture(replyRpcResult);
+        Mockito.when(mockedRequestContext.getFuture()).thenReturn(replyFt);
+        SendEchoInput sendEchoInput = new SendEchoInputBuilder()
+                .setData(DUMMY_DATA)
+                .build();
+
+        final Future<RpcResult<SendEchoOutput>> echoOutput = salEchoService.sendEcho(sendEchoInput);
+
+        Assert.assertNotNull(echoOutput);
+        Assert.assertTrue(echoOutput.isDone());
+        Assert.assertTrue(echoOutput.get().isSuccessful());
+        verify(mockedRequestContextStack).createRequestContext();
+        verify(mockedOutboundQueue).commitEntry(Matchers.eq(2121L), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
     }
 }
\ No newline at end of file