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 com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
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 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Created by Martin Bobak <mbobak@cisco.com> on 26.3.2015.
25 public class OFJResult2RequestCtxFuture<T> {
27 private static final Logger LOG = LoggerFactory.getLogger(OFJResult2RequestCtxFuture.class);
28 private final RequestContext<T> requestContext;
29 private final DeviceContext deviceContext;
31 public OFJResult2RequestCtxFuture(final RequestContext<T> requestContext, final DeviceContext deviceContext) {
32 this.requestContext = requestContext;
33 this.deviceContext = deviceContext;
36 public <F> void processResultFromOfJava(final ListenableFuture<RpcResult<F>> futureResultFromOfLib) {
37 Futures.addCallback(futureResultFromOfLib, new FutureCallback<RpcResult<F>>() {
39 public void onSuccess(final RpcResult<F> fRpcResult) {
40 if (!fRpcResult.isSuccessful()) {
41 StringBuilder rpcErrors = new StringBuilder();
42 if (null != fRpcResult.getErrors() && fRpcResult.getErrors().size() > 0) {
43 for (RpcError error : fRpcResult.getErrors()) {
44 rpcErrors.append(error.getMessage());
47 LOG.trace("OF Java result for XID {} was not successful. Errors : {}", requestContext.getXid().getValue(), rpcErrors.toString());
48 requestContext.getFuture().set(
49 RpcResultBuilder.<T>failed().withRpcErrors(fRpcResult.getErrors()).build());
50 RequestContextUtil.closeRequstContext(requestContext);
52 LOG.trace("Hooking xid {} to device context.", requestContext.getXid().getValue());
53 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
58 public void onFailure(final Throwable throwable) {
59 LOG.trace("Exception occured while processing OF Java response for XID {}.", requestContext.getXid().getValue(), throwable);
60 requestContext.getFuture().set(
61 RpcResultBuilder.<T>failed()
62 .withError(RpcError.ErrorType.APPLICATION, "", "Flow translation to OF JAVA failed.")
64 RequestContextUtil.closeRequstContext(requestContext);