Make sure RequestContext has a constant XID
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalEchoServiceImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.JdkFutureAdapters;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.Future;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
18 import org.opendaylight.openflowplugin.impl.callback.SuccessCallback;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SalEchoService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class SalEchoServiceImpl extends CommonService implements SalEchoService {
32
33     private static final Logger LOG = LoggerFactory.getLogger(SalEchoServiceImpl.class);
34
35     public SalEchoServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
36         super(requestContextStack, deviceContext);
37     }
38
39     @Override
40     public Future<RpcResult<SendEchoOutput>> sendEcho(final SendEchoInput sendEchoInput) {
41         final RequestContext<SendEchoOutput> requestContext = getRequestContextStack().createRequestContext();
42         if (requestContext == null) {
43             getMessageSpy().spyMessage(null, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
44             return failedFuture();
45         }
46
47
48         final DeviceContext deviceContext = getDeviceContext();
49
50         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
51         deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
52
53         final EchoInputBuilder echoInputOFJavaBuilder = new EchoInputBuilder();
54         echoInputOFJavaBuilder.setVersion(getVersion());
55         echoInputOFJavaBuilder.setXid(requestContext.getXid().getValue());
56         echoInputOFJavaBuilder.setData(sendEchoInput.getData());
57         final EchoInput echoInputOFJava = echoInputOFJavaBuilder.build();
58
59         // FIXME: should be submitted via OutboundQueue
60         final Future<RpcResult<EchoOutput>> rpcEchoOutputOFJava = getPrimaryConnectionAdapter()
61                 .echo(echoInputOFJava);
62         LOG.debug("Echo with xid {} was sent from controller", requestContext.getXid());
63
64         ListenableFuture<RpcResult<EchoOutput>> listenableRpcEchoOutputOFJava = JdkFutureAdapters
65                 .listenInPoolThread(rpcEchoOutputOFJava);
66
67         // callback on OF JAVA future
68         SuccessCallback<EchoOutput, SendEchoOutput> successCallback = new SuccessCallback<EchoOutput, SendEchoOutput>(
69                 deviceContext, requestContext, listenableRpcEchoOutputOFJava) {
70
71             @Override
72             public RpcResult<SendEchoOutput> transform(final RpcResult<EchoOutput> rpcResult) {
73                 EchoOutput echoOutputOFJava = rpcResult.getResult();
74                 SendEchoOutputBuilder sendEchoOutputBuilder = new SendEchoOutputBuilder();
75                 sendEchoOutputBuilder.setData(echoOutputOFJava.getData());
76
77                 LOG.debug("Echo with xid {} was received by controller.", rpcResult.getResult().getXid());
78                 return RpcResultBuilder.success(sendEchoOutputBuilder.build()).build();
79             }
80         };
81         Futures.addCallback(listenableRpcEchoOutputOFJava, successCallback);
82
83         return requestContext.getFuture();
84     }
85 }