05d45d25b624512c066083d8fd09289d13b74ba6
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / 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;
9
10 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
11 import org.opendaylight.yangtools.yang.common.RpcError;
12 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
13 import org.slf4j.Logger;
14
15 public final class RequestContextUtil {
16
17     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(RequestContextUtil.class);
18
19     private RequestContextUtil() {
20         throw new UnsupportedOperationException();
21     }
22
23
24     public static void closeRequestContextWithRpcError(final RequestContext<?> requestContext, String errorMessage) {
25
26         RpcResultBuilder rpcResultBuilder = RpcResultBuilder.failed().withRpcError(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "", errorMessage));
27         requestContext.getFuture().set(rpcResultBuilder.build());
28         closeRequstContext(requestContext);
29     }
30
31     public static void closeRequstContext(final RequestContext<?> requestContext) {
32         try {
33             requestContext.close();
34         } catch (Exception e) {
35             LOG.debug("Request context wasn't closed. Exception message: {}", e.getMessage());
36         }
37     }
38 }