Use String(byte[], Charset)
[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 message error message
41      * @param cause - details of reason
42      * @return RpcError
43      */
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);
46     }
47
48     AbstractRpcListener(final Object message, final String failureInfo) {
49         this.failureInfo = Preconditions.checkNotNull(failureInfo);
50         this.message = Preconditions.checkNotNull(message);
51     }
52
53     public final ListenableFuture<RpcResult<T>> getResult() {
54         return result;
55     }
56
57     @Override
58     public final void operationComplete(final Future<Void> future) {
59         if (!future.isSuccess()) {
60             LOG.debug("operation failed");
61             failedRpc(future.cause());
62         } else {
63             LOG.debug("operation complete");
64             operationSuccessful();
65         }
66     }
67
68     @Override
69     public final Object takeMessage() {
70         final Object ret = message;
71         Preconditions.checkState(ret != null, "Message has already been taken");
72         message = null;
73         return ret;
74     }
75
76     @Override
77     public final GenericFutureListener<Future<Void>> takeListener() {
78         return this;
79     }
80
81     protected abstract void operationSuccessful();
82
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());
86     }
87
88     protected final void successfulRpc(final T value) {
89         result.set(RpcResultBuilder.success(value).build());
90     }
91 }