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 java.util.Collections;
16 import org.opendaylight.controller.sal.common.util.RpcErrors;
17 import org.opendaylight.controller.sal.common.util.Rpcs;
18 import org.opendaylight.yangtools.yang.common.RpcError;
19 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
20 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * This class holds all the context we need for sending a single message down the tube.
28 * A MessageHolder (used in queue) and the actual listener. It is not a thing of beauty,
29 * but it keeps us from allocating unnecessary objects in the egress path.
31 abstract class AbstractRpcListener<T> implements GenericFutureListener<Future<Void>>, ChannelOutboundQueue.MessageHolder<Object> {
32 private static final Logger LOG = LoggerFactory.getLogger(AbstractRpcListener.class);
33 private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
34 private static final String TAG = "OPENFLOW";
35 private final SettableFuture<RpcResult<T>> result = SettableFuture.create();
36 private final String failureInfo;
37 private Object message;
40 * Create RcpError object
42 * @param severity - error severity
44 * @param cause - details of reason
47 static RpcError buildRpcError(final String info, final ErrorSeverity severity, final String message,
48 final Throwable cause) {
49 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
50 ErrorType.RPC, cause);
54 AbstractRpcListener(final Object message, final String failureInfo) {
55 this.failureInfo = Preconditions.checkNotNull(failureInfo);
56 this.message = Preconditions.checkNotNull(message);
59 public final ListenableFuture<RpcResult<T>> getResult() {
64 public final void operationComplete(final Future<Void> future) {
65 if (!future.isSuccess()) {
66 LOG.debug("operation failed");
67 failedRpc(future.cause());
69 LOG.debug("operation complete");
70 operationSuccessful();
75 public final Object takeMessage() {
76 final Object ret = message;
77 Preconditions.checkState(ret != null, "Message has already been taken");
83 public final GenericFutureListener<Future<Void>> takeListener() {
87 protected abstract void operationSuccessful();
89 protected final void failedRpc(final Throwable cause) {
90 final RpcError rpcError = buildRpcError(
91 failureInfo, ErrorSeverity.ERROR, "check switch connection", cause);
92 result.set(Rpcs.getRpcResult(
95 Collections.singletonList(rpcError)));
98 protected final void successfulRpc(final T value) {
99 result.set(RpcResultBuilder.success(value).build());