Bug-2827: role switch proposal
[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.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
31      * @return error wrapped inside {@link RpcResult} which is wrapped inside future
32      */
33     public static <T> SettableFuture<RpcResult<T>> getRpcErrorFuture(ConnectionException e) {
34         List<RpcError> rpcErrorList = wrapConnectionErrorIntoRpcErrors(e);
35         SettableFuture<RpcResult<T>> futureWithError = SettableFuture.create();
36         futureWithError.set(RpcResultBuilder.<T>failed().withRpcErrors(rpcErrorList).build());
37         return futureWithError;
38     }
39
40     private static List<RpcError> wrapConnectionErrorIntoRpcErrors(ConnectionException e) {
41         List<RpcError> rpcErrorList = new ArrayList<>();
42         rpcErrorList.add(RpcResultBuilder.newError(
43                 RpcError.ErrorType.TRANSPORT,
44                 OFConstants.ERROR_TAG_TIMEOUT,
45                 e.getMessage(),
46                 OFConstants.APPLICATION_TAG,
47                 IMessageDispatchService.CONNECTION_ERROR_MESSAGE,
48                 e.getCause()));
49         return rpcErrorList;
50     }
51
52 }