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