2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.services;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.yangtools.yang.common.RpcError;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Created by Martin Bobak <mbobak@cisco.com> on 26.3.2015.
26 public class OFJResult2RequestCtxFuture<T> {
28 private static final Logger LOG = LoggerFactory.getLogger(OFJResult2RequestCtxFuture.class);
29 private final RequestContext<T> requestContext;
30 private final DeviceContext deviceContext;
32 public OFJResult2RequestCtxFuture(final RequestContext<T> requestContext, final DeviceContext deviceContext) {
33 this.requestContext = requestContext;
34 this.deviceContext = deviceContext;
37 public <F> void processResultFromOfJava(final Future<RpcResult<F>> futureResultFromOfLib) {
39 final RpcResult<F> rpcResult = futureResultFromOfLib.get(requestContext.getWaitTimeout(), TimeUnit.MILLISECONDS);
40 if (!rpcResult.isSuccessful()) {
41 requestContext.getFuture().set(
42 RpcResultBuilder.<T>failed().withRpcErrors(rpcResult.getErrors()).build());
43 RequestContextUtil.closeRequstContext(requestContext);
45 LOG.trace("Hooking xid {} to device context.", requestContext.getXid().getValue());
46 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
48 } catch (InterruptedException | ExecutionException | TimeoutException e) {
49 requestContext.getFuture().set(
52 .withError(RpcError.ErrorType.APPLICATION, "",
53 "Flow modification on device wasn't successfull.").build());
54 RequestContextUtil.closeRequstContext(requestContext);
55 } catch (final Exception e) {
56 requestContext.getFuture().set(
57 RpcResultBuilder.<T>failed()
58 .withError(RpcError.ErrorType.APPLICATION, "", "Flow translation to OF JAVA failed.")
60 RequestContextUtil.closeRequstContext(requestContext);