Add single layer deserialization support
[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.RpcError;
13 import org.opendaylight.yangtools.yang.common.RpcResult;
14 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
15 import org.slf4j.Logger;
16
17 public final class RequestContextUtil {
18
19     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(RequestContextUtil.class);
20
21     private RequestContextUtil() {
22         throw new UnsupportedOperationException();
23     }
24
25
26     public static <T> ListenableFuture<RpcResult<T>> closeRequestContextWithRpcError(final RequestContext<T> requestContext, final String errorMessage) {
27         RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.<T>failed().withRpcError(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "", errorMessage));
28         requestContext.setResult(rpcResultBuilder.build());
29         closeRequestContext(requestContext);
30         return requestContext.getFuture();
31     }
32
33     public static void closeRequestContext(final RequestContext<?> requestContext) {
34         try {
35             requestContext.close();
36         } catch (Exception e) {
37             LOG.error("Request context failed to close", e);
38         }
39     }
40 }