Fix checkstyle violations in openflow-protocol-impl - part 1
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / connection / AbstractRpcListener.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core.connection;
9
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;
21
22 /**
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.
26  */
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;
35
36     /**
37      * Create RcpError object.
38      *
39      * @param info error info
40      * @param severity - error severity
41      * @param message error message
42      * @param cause - details of reason
43      * @return RpcError
44      */
45     static RpcError buildRpcError(final String info, final String message, final Throwable cause) {
46         return RpcResultBuilder.newError(ErrorType.RPC, TAG, message, APPLICATION_TAG, info, cause);
47     }
48
49     AbstractRpcListener(final Object message, final String failureInfo) {
50         this.failureInfo = Preconditions.checkNotNull(failureInfo);
51         this.message = Preconditions.checkNotNull(message);
52     }
53
54     public final ListenableFuture<RpcResult<T>> getResult() {
55         return result;
56     }
57
58     @Override
59     public final void operationComplete(final Future<Void> future) {
60         if (!future.isSuccess()) {
61             LOG.debug("operation failed");
62             failedRpc(future.cause());
63         } else {
64             LOG.debug("operation complete");
65             operationSuccessful();
66         }
67     }
68
69     @Override
70     public final Object takeMessage() {
71         final Object ret = message;
72         Preconditions.checkState(ret != null, "Message has already been taken");
73         message = null;
74         return ret;
75     }
76
77     @Override
78     public final GenericFutureListener<Future<Void>> takeListener() {
79         return this;
80     }
81
82     protected abstract void operationSuccessful();
83
84     protected final void failedRpc(final Throwable cause) {
85         final RpcError rpcError = buildRpcError(failureInfo, "check switch connection", cause);
86         result.set(RpcResultBuilder.<T>failed().withRpcError(rpcError).build());
87     }
88
89     protected final void successfulRpc(final T value) {
90         result.set(RpcResultBuilder.success(value).build());
91     }
92 }