616c40550ad0d4f407be6f01853683482b457446
[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.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.concurrent.Future;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import org.opendaylight.openflowplugin.impl.callback.SuccessCallback;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SalEchoService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
29 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class SalEchoServiceImpl extends CommonService implements SalEchoService {
36
37     private static final Logger LOG = LoggerFactory.getLogger(SalEchoServiceImpl.class);
38
39     public SalEchoServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
40         super(requestContextStack, deviceContext);
41     }
42
43     @Override
44     public Future<RpcResult<SendEchoOutput>> sendEcho(final SendEchoInput sendEchoInput) {
45         final RequestContext<SendEchoOutput> requestContext = getRequestContextStack().createRequestContext();
46         final SettableFuture<RpcResult<SendEchoOutput>> sendEchoOutput = getRequestContextStack()
47                 .storeOrFail(requestContext);
48         if (!sendEchoOutput.isDone()) {
49             final DeviceContext deviceContext = getDeviceContext();
50             Long reserverXid = deviceContext.getReservedXid();
51             if (null == reserverXid) {
52                 if (null == reserverXid) {
53                     reserverXid = deviceContext.getReservedXid();
54                     RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
55                     return sendEchoOutput;
56                 }
57             }
58             final Xid xid = new Xid(reserverXid);
59             requestContext.setXid(xid);
60
61             LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
62             deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
63
64             final EchoInputBuilder echoInputOFJavaBuilder = new EchoInputBuilder();
65             echoInputOFJavaBuilder.setVersion(getVersion());
66             echoInputOFJavaBuilder.setXid(xid.getValue());
67             echoInputOFJavaBuilder.setData(sendEchoInput.getData());
68             final EchoInput echoInputOFJava = echoInputOFJavaBuilder.build();
69
70             final Future<RpcResult<EchoOutput>> rpcEchoOutputOFJava = getPrimaryConnectionAdapter()
71                     .echo(echoInputOFJava);
72             LOG.debug("Echo with xid {} was sent from controller", xid);
73
74             ListenableFuture<RpcResult<EchoOutput>> listenableRpcEchoOutputOFJava = JdkFutureAdapters
75                     .listenInPoolThread(rpcEchoOutputOFJava);
76
77             // callback on OF JAVA future
78             SuccessCallback<EchoOutput, SendEchoOutput> successCallback = new SuccessCallback<EchoOutput, SendEchoOutput>(
79                     deviceContext, requestContext, listenableRpcEchoOutputOFJava) {
80
81                 @Override
82                 public RpcResult<SendEchoOutput> transform(RpcResult<EchoOutput> rpcResult) {
83                     EchoOutput echoOutputOFJava = rpcResult.getResult();
84                     SendEchoOutputBuilder sendEchoOutputBuilder = new SendEchoOutputBuilder();
85                     sendEchoOutputBuilder.setData(echoOutputOFJava.getData());
86
87                     LOG.debug("Echo with xid {} was received by controller.", rpcResult.getResult().getXid());
88                     return RpcResultBuilder.success(sendEchoOutputBuilder.build()).build();
89                 }
90             };
91             Futures.addCallback(listenableRpcEchoOutputOFJava, successCallback);
92         } else {
93             getMessageSpy().spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
94         }
95
96         // callback on request context future
97         Futures.addCallback(sendEchoOutput, new FutureCallback<RpcResult<SendEchoOutput>>() {
98
99             @Override
100             public void onSuccess(RpcResult<SendEchoOutput> result) {
101             }
102
103             @Override
104             public void onFailure(Throwable t) {
105                 if (sendEchoOutput.isCancelled()) {
106                     requestContext.getFuture().set(
107                             RpcResultBuilder.<SendEchoOutput>failed()
108                                     .withError(ErrorType.APPLICATION, "Echo response wasn't obtained until barrier.")
109                                     .build());
110                     LOG.debug("Echo reply with xid {} wasn't received by controller until barrier.",
111                             requestContext.getXid());
112                 }
113             }
114         });
115
116         return sendEchoOutput;
117     }
118
119 }