2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services;
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;
30 public class SalEchoServiceImpl extends CommonService implements SalEchoService {
32 private static final Logger LOG = LoggerFactory.getLogger(SalEchoServiceImpl.class);
34 public SalEchoServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
35 super(requestContextStack, deviceContext);
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();
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();
56 LOG.debug("Echo with xid {} was sent from controller", xid);
58 final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
59 outboundQueue.commitEntry(xid.getValue(), echoInputOFJava, new FutureCallback<OfHeader>() {
61 RpcResultBuilder<SendEchoOutput> rpcResultBuilder;
63 public void onSuccess(final OfHeader ofHeader) {
64 rpcResultBuilder = RpcResultBuilder.<SendEchoOutput>success();
65 requestContext.setResult(rpcResultBuilder.build());
66 RequestContextUtil.closeRequstContext(requestContext);
68 getMessageSpy().spyMessage(echoInputOFJava.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
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);
77 getMessageSpy().spyMessage(echoInputOFJava.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
80 return requestContext.getFuture();