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