Returning of future from request context - SalEchoService.
[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 java.util.concurrent.Future;
12 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
16 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SalEchoService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
24 import org.opendaylight.yangtools.yang.common.RpcError;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class SalEchoServiceImpl extends CommonService implements SalEchoService {
31
32     private static final Logger LOG = LoggerFactory.getLogger(SalEchoServiceImpl.class);
33
34     public SalEchoServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
35         super(requestContextStack, deviceContext);
36     }
37
38     @Override
39     public Future<RpcResult<SendEchoOutput>> sendEcho(final SendEchoInput sendEchoInput) {
40         final RequestContext<SendEchoOutput> requestContext = getRequestContextStack().createRequestContext();
41         if (requestContext == null) {
42             getMessageSpy().spyMessage(null, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
43             return failedFuture();
44         }
45
46
47
48         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
49         final Xid xid = requestContext.getXid();
50         final EchoInputBuilder echoInputOFJavaBuilder = new EchoInputBuilder();
51         echoInputOFJavaBuilder.setVersion(getVersion());
52         echoInputOFJavaBuilder.setXid(requestContext.getXid().getValue());
53         echoInputOFJavaBuilder.setData(sendEchoInput.getData());
54         final EchoInput echoInputOFJava = echoInputOFJavaBuilder.build();
55
56         LOG.debug("Echo with xid {} was sent from controller", xid);
57
58         final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
59         outboundQueue.commitEntry(xid.getValue(), echoInputOFJava, new FutureCallback<OfHeader>() {
60
61             RpcResultBuilder<SendEchoOutput> rpcResultBuilder;
62             @Override
63             public void onSuccess(final OfHeader ofHeader) {
64                 rpcResultBuilder = RpcResultBuilder.<SendEchoOutput>success();
65                 requestContext.setResult(rpcResultBuilder.build());
66                 RequestContextUtil.closeRequstContext(requestContext);
67
68                 getMessageSpy().spyMessage(echoInputOFJava.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
69             }
70
71             @Override
72             public void onFailure(final Throwable throwable) {
73                 rpcResultBuilder = RpcResultBuilder.<SendEchoOutput>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
74                 requestContext.setResult(rpcResultBuilder.build());
75                 RequestContextUtil.closeRequstContext(requestContext);
76
77                 getMessageSpy().spyMessage(echoInputOFJava.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
78             }
79         });
80         return requestContext.getFuture();
81     }
82 }