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
8 package org.opendaylight.openflowplugin.impl.callback;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
15 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
16 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
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;
26 public class BaseCallback<I, O> implements FutureCallback<RpcResult<I>> {
28 private static final Logger LOG = LoggerFactory.getLogger(BaseCallback.class);
30 protected DeviceContext deviceContext;
31 protected RequestContext<O> requestContext;
32 private ListenableFuture<RpcResult<I>> futureResultFromOfLib;
34 public BaseCallback(final DeviceContext deviceContext, final RequestContext<O> requestContext, final ListenableFuture<RpcResult<I>> futureResultFromOfLib) {
35 this.deviceContext = deviceContext;
36 this.requestContext = requestContext;
37 this.futureResultFromOfLib = futureResultFromOfLib;
41 public void onSuccess(final RpcResult<I> fRpcResult) {
42 if (!fRpcResult.isSuccessful()) {
43 synchronized (deviceContext) {
44 deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
46 // remove current request from request cache in deviceContext
47 deviceContext.unhookRequestCtx(requestContext.getXid());
48 // handle requestContext failure
49 StringBuilder rpcErrors = new StringBuilder();
50 if (null != fRpcResult.getErrors() && fRpcResult.getErrors().size() > 0) {
51 for (RpcError error : fRpcResult.getErrors()) {
52 rpcErrors.append(error.getMessage());
55 LOG.trace("OF Java result for XID {} was not successful. Errors : {}", requestContext.getXid().getValue(), rpcErrors.toString());
56 requestContext.getFuture().set(
57 RpcResultBuilder.<O>failed().withRpcErrors(fRpcResult.getErrors()).build());
58 RequestContextUtil.closeRequstContext(requestContext);
61 // else: message was successfully sent - waiting for callback on requestContext.future to get invoked
62 // or can be implemented specific processing via processSuccess() method
63 processSuccess(fRpcResult);
69 public void onFailure(final Throwable throwable) {
70 deviceContext.unhookRequestCtx(requestContext.getXid());
72 if (futureResultFromOfLib.isCancelled()) {
73 deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
75 LOG.trace("Asymmetric message - no response from OF Java expected for XID {}. Closing as successful.", requestContext.getXid().getValue());
76 requestContext.getFuture().set(RpcResultBuilder.<O>success().build());
78 deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
79 LOG.trace("Exception occured while processing OF Java response for XID {}.", requestContext.getXid().getValue(), throwable);
80 requestContext.getFuture().set(
81 RpcResultBuilder.<O>failed()
82 .withError(RpcError.ErrorType.APPLICATION, "OF JAVA operation failed.", throwable)
86 RequestContextUtil.closeRequstContext(requestContext);
89 protected void processSuccess(final RpcResult<I> fRpcResult) {
90 //should be override in child class where is awaited processing of
91 //successfull future (e. g. transformation)