c387512d382beab9ca7acd6736daf086f23d977d
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / OFJResult2RequestCtxFuture.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
9 package org.opendaylight.openflowplugin.impl.services;
10
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;
21
22 /**
23  * Created by Martin Bobak <mbobak@cisco.com> on 26.3.2015.
24  */
25 public class OFJResult2RequestCtxFuture<T> {
26
27     private static final Logger LOG = LoggerFactory.getLogger(OFJResult2RequestCtxFuture.class);
28     private final RequestContext<T> requestContext;
29     private final DeviceContext deviceContext;
30
31     public OFJResult2RequestCtxFuture(final RequestContext<T> requestContext, final DeviceContext deviceContext) {
32         this.requestContext = requestContext;
33         this.deviceContext = deviceContext;
34     }
35
36     public <F> void processResultFromOfJava(final ListenableFuture<RpcResult<F>> futureResultFromOfLib) {
37         Futures.addCallback(futureResultFromOfLib, new FutureCallback<RpcResult<F>>() {
38             @Override
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());
45                         }
46                     }
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);
51                 } else {
52                     LOG.trace("Hooking xid {} to device context.", requestContext.getXid().getValue());
53                     deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
54                 }
55             }
56
57             @Override
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.")
63                                 .build());
64                 RequestContextUtil.closeRequstContext(requestContext);
65             }
66         });
67     }
68 }