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