Bump MRI upstreams
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / util / RequestContextUtil.java
1 /*
2  * Copyright (c) 2015 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.impl.services.util;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
12 import org.opendaylight.yangtools.yang.common.ErrorTag;
13 import org.opendaylight.yangtools.yang.common.ErrorType;
14 import org.opendaylight.yangtools.yang.common.RpcResult;
15 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
16 import org.slf4j.Logger;
17
18 public final class RequestContextUtil {
19     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(RequestContextUtil.class);
20
21     private RequestContextUtil() {
22         // Hidden on purpose
23     }
24
25     public static <T> ListenableFuture<RpcResult<T>> closeRequestContextWithRpcError(
26                                                                                 final RequestContext<T> requestContext,
27                                                                                 final String errorMessage) {
28         RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.<T>failed().withRpcError(RpcResultBuilder
29                 .newError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, errorMessage));
30         requestContext.setResult(rpcResultBuilder.build());
31         closeRequestContext(requestContext);
32         return requestContext.getFuture();
33     }
34
35     @SuppressWarnings("checkstyle:IllegalCatch")
36     public static void closeRequestContext(final RequestContext<?> requestContext) {
37         try {
38             requestContext.close();
39         } catch (Exception e) {
40             LOG.error("Request context failed to close", e);
41         }
42     }
43 }