--- /dev/null
+/*
+ * 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
*/
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;
+ }
+ });
}
+
}
--- /dev/null
+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
+/*
+ * 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