tidy up addFlow futures
[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.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
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;
22
23 /**
24  * Created by Martin Bobak <mbobak@cisco.com> on 26.3.2015.
25  */
26 public class OFJResult2RequestCtxFuture<T> {
27
28     private static final Logger LOG = LoggerFactory.getLogger(OFJResult2RequestCtxFuture.class);
29     private final RequestContext<T> requestContext;
30     private final DeviceContext deviceContext;
31
32     public OFJResult2RequestCtxFuture(final RequestContext<T> requestContext, final DeviceContext deviceContext) {
33         this.requestContext = requestContext;
34         this.deviceContext = deviceContext;
35     }
36
37     public <F> void processResultFromOfJava(final ListenableFuture<RpcResult<F>> futureResultFromOfLib) {
38         Futures.addCallback(futureResultFromOfLib, new FutureCallback<RpcResult<F>>() {
39             @Override
40             public void onSuccess(final RpcResult<F> fRpcResult) {
41                 if (!fRpcResult.isSuccessful()) {
42                     deviceContext.getMessageSpy().spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
43
44                     // remove current request from request cache in deviceContext
45                     deviceContext.unhookRequestCtx(requestContext.getXid());
46                     // handle requestContext failure
47                     StringBuilder rpcErrors = new StringBuilder();
48                     if (null != fRpcResult.getErrors() && fRpcResult.getErrors().size() > 0) {
49                         for (RpcError error : fRpcResult.getErrors()) {
50                             rpcErrors.append(error.getMessage());
51                         }
52                     }
53                     LOG.trace("OF Java result for XID {} was not successful. Errors : {}", requestContext.getXid().getValue(), rpcErrors.toString());
54                     requestContext.getFuture().set(
55                             RpcResultBuilder.<T>failed().withRpcErrors(fRpcResult.getErrors()).build());
56                     RequestContextUtil.closeRequstContext(requestContext);
57                 }
58                 // else: message was successfully sent - waiting for callback on requestContext.future to get invoked
59             }
60
61             @Override
62             public void onFailure(final Throwable throwable) {
63                 if (futureResultFromOfLib.isCancelled()) {
64                     deviceContext.getMessageSpy().spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
65
66                     LOG.trace("Asymmetric message - no response from OF Java expected for XID {}. Closing as successful.", requestContext.getXid().getValue());
67                     requestContext.getFuture().set(RpcResultBuilder.<T>success().build());
68                 } else {
69                     deviceContext.getMessageSpy().spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
70                     deviceContext.unhookRequestCtx(requestContext.getXid());
71                     LOG.trace("Exception occured while processing OF Java response for XID {}.", requestContext.getXid().getValue(), throwable);
72                     requestContext.getFuture().set(
73                             RpcResultBuilder.<T>failed()
74                                     .withError(RpcError.ErrorType.APPLICATION, "", "Flow translation to OF JAVA failed.")
75                                     .build());
76                 }
77
78                 RequestContextUtil.closeRequstContext(requestContext);
79             }
80         });
81     }
82 }