Merge "BUG-4117: add support of Old Notif. for Statistics"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / util / RpcResultUtil.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.openflowplugin.openflow.md.util;
9
10 import com.google.common.util.concurrent.SettableFuture;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.openflowplugin.api.ConnectionException;
14 import org.opendaylight.openflowplugin.api.OFConstants;
15 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
19
20 /**
21  *
22  */
23 public final class RpcResultUtil {
24
25     private RpcResultUtil() {
26         throw new UnsupportedOperationException("RpcResultUtil is not expected to be instantiated.");
27     }
28
29     /**
30      * @param e connection exception
31      * @param <T> rpc result return type
32      * @return error wrapped inside {@link RpcResult} which is wrapped inside future
33      */
34     public static <T> SettableFuture<RpcResult<T>> getRpcErrorFuture(ConnectionException e) {
35         List<RpcError> rpcErrorList = wrapConnectionErrorIntoRpcErrors(e);
36         SettableFuture<RpcResult<T>> futureWithError = SettableFuture.create();
37         futureWithError.set(RpcResultBuilder.<T>failed().withRpcErrors(rpcErrorList).build());
38         return futureWithError;
39     }
40
41     private static List<RpcError> wrapConnectionErrorIntoRpcErrors(ConnectionException e) {
42         List<RpcError> rpcErrorList = new ArrayList<>();
43         rpcErrorList.add(RpcResultBuilder.newError(
44                 RpcError.ErrorType.TRANSPORT,
45                 OFConstants.ERROR_TAG_TIMEOUT,
46                 e.getMessage(),
47                 OFConstants.APPLICATION_TAG,
48                 IMessageDispatchService.CONNECTION_ERROR_MESSAGE,
49                 e.getCause()));
50         return rpcErrorList;
51     }
52
53 }