changed approach to synchronization of DeviceContext
[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 = requestContextStack.createRequestContext();
46         final SettableFuture<RpcResult<SendEchoOutput>> sendEchoOutput = requestContextStack
47                 .storeOrFail(requestContext);
48         if (!sendEchoOutput.isDone()) {
49             final Xid xid = deviceContext.getNextXid();
50             requestContext.setXid(xid);
51
52             LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
53             deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
54
55             final EchoInputBuilder echoInputOFJavaBuilder = new EchoInputBuilder();
56             echoInputOFJavaBuilder.setVersion(version);
57             echoInputOFJavaBuilder.setXid(xid.getValue());
58             echoInputOFJavaBuilder.setData(sendEchoInput.getData());
59             final EchoInput echoInputOFJava = echoInputOFJavaBuilder.build();
60
61             final Future<RpcResult<EchoOutput>> rpcEchoOutputOFJava = provideConnectionAdapter(PRIMARY_CONNECTION)
62                     .echo(echoInputOFJava);
63             LOG.debug("Echo with xid {} was sent from controller", xid);
64
65             ListenableFuture<RpcResult<EchoOutput>> listenableRpcEchoOutputOFJava = JdkFutureAdapters
66                     .listenInPoolThread(rpcEchoOutputOFJava);
67
68             // callback on OF JAVA future
69             SuccessCallback<EchoOutput, SendEchoOutput> successCallback = new SuccessCallback<EchoOutput, SendEchoOutput>(
70                     deviceContext, requestContext, listenableRpcEchoOutputOFJava) {
71
72                 @Override
73                 public RpcResult<SendEchoOutput> transform(RpcResult<EchoOutput> rpcResult) {
74                     EchoOutput echoOutputOFJava = rpcResult.getResult();
75                     SendEchoOutputBuilder sendEchoOutputBuilder = new SendEchoOutputBuilder();
76                     sendEchoOutputBuilder.setData(echoOutputOFJava.getData());
77
78                     LOG.debug("Echo with xid {} was received by controller.", rpcResult.getResult().getXid());
79                     return RpcResultBuilder.success(sendEchoOutputBuilder.build()).build();
80                 }
81             };
82             Futures.addCallback(listenableRpcEchoOutputOFJava, successCallback);
83         } else {
84             messageSpy.spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);
85         }
86
87         // callback on request context future
88         Futures.addCallback(sendEchoOutput, new FutureCallback<RpcResult<SendEchoOutput>>() {
89
90             @Override
91             public void onSuccess(RpcResult<SendEchoOutput> result) {
92             }
93
94             @Override
95             public void onFailure(Throwable t) {
96                 if (sendEchoOutput.isCancelled()) {
97                     requestContext.getFuture().set(
98                             RpcResultBuilder.<SendEchoOutput>failed()
99                                     .withError(ErrorType.APPLICATION, "Echo response wasn't obtained until barrier.")
100                                     .build());
101                     LOG.debug("Echo reply with xid {} wasn't received by controller until barrier.",
102                             requestContext.getXid());
103                 }
104             }
105         });
106
107         return sendEchoOutput;
108     }
109
110 }