2 * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core.connection;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import io.netty.util.concurrent.Future;
14 import io.netty.util.concurrent.GenericFutureListener;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * This class holds all the context we need for sending a single message down the tube.
24 * A MessageHolder (used in queue) and the actual listener. It is not a thing of beauty,
25 * but it keeps us from allocating unnecessary objects in the egress path.
27 abstract class AbstractRpcListener<T> implements GenericFutureListener<Future<Void>>,
28 ChannelOutboundQueue.MessageHolder<Object> {
29 private static final Logger LOG = LoggerFactory.getLogger(AbstractRpcListener.class);
30 private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
31 private static final String TAG = "OPENFLOW";
32 private final SettableFuture<RpcResult<T>> result = SettableFuture.create();
33 private final String failureInfo;
34 private Object message;
37 * Create RcpError object.
39 * @param info error info
40 * @param message error message
41 * @param cause - details of reason
44 static RpcError buildRpcError(final String info, final String message, final Throwable cause) {
45 return RpcResultBuilder.newError(ErrorType.RPC, TAG, message, APPLICATION_TAG, info, cause);
48 AbstractRpcListener(final Object message, final String failureInfo) {
49 this.failureInfo = Preconditions.checkNotNull(failureInfo);
50 this.message = Preconditions.checkNotNull(message);
53 public final ListenableFuture<RpcResult<T>> getResult() {
58 public final void operationComplete(final Future<Void> future) {
59 if (!future.isSuccess()) {
60 LOG.debug("operation failed");
61 failedRpc(future.cause());
63 LOG.debug("operation complete");
64 operationSuccessful();
69 public final Object takeMessage() {
70 final Object ret = message;
71 Preconditions.checkState(ret != null, "Message has already been taken");
77 public final GenericFutureListener<Future<Void>> takeListener() {
81 protected abstract void operationSuccessful();
83 protected final void failedRpc(final Throwable cause) {
84 final RpcError rpcError = buildRpcError(failureInfo, "check switch connection", cause);
85 result.set(RpcResultBuilder.<T>failed().withRpcError(rpcError).build());
88 protected final void successfulRpc(final T value) {
89 result.set(RpcResultBuilder.success(value).build());