2 * Copyright (c) 2013 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.openflow.md.core.sal;
10 import com.google.common.base.Function;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.collect.Lists;
13 import com.google.common.util.concurrent.AsyncFunction;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.concurrent.Future;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.openflowplugin.api.OFConstants;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.sal.NotificationComposer;
26 import org.opendaylight.openflowplugin.api.openflow.statistics.MessageSpy;
27 import org.opendaylight.openflowplugin.openflow.md.util.RpcInputOutputTuple;
28 import org.opendaylight.openflowplugin.openflow.md.util.TaskUtil;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionAware;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
32 import org.opendaylight.yangtools.yang.binding.DataContainer;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34 import org.opendaylight.yangtools.yang.common.RpcError;
35 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
44 public abstract class OFRpcTaskUtil {
45 protected static final Logger LOG = LoggerFactory.getLogger(OFRpcTaskUtil.class);
50 * @return rpcResult of given type, containing wrapped errors of barrier sending (if any) or success
52 private OFRpcTaskUtil() {
53 //hiding implicit constructor
56 public static Collection<RpcError> manageBarrier(final OFRpcTaskContext taskContext, final Boolean isBarrier,
57 final SwitchConnectionDistinguisher cookie) {
58 Collection<RpcError> errors = null;
59 if (MoreObjects.firstNonNull(isBarrier, Boolean.FALSE)) {
60 RpcInputOutputTuple<BarrierInput, ListenableFuture<RpcResult<BarrierOutput>>> sendBarrierRpc =
61 TaskUtil.sendBarrier(taskContext.getSession(), cookie, taskContext.getMessageService());
62 Future<RpcResult<BarrierOutput>> barrierFuture = sendBarrierRpc.getOutput();
64 RpcResult<BarrierOutput> barrierResult = barrierFuture.get(
65 taskContext.getMaxTimeout(), taskContext.getMaxTimeoutUnit());
66 if (!barrierResult.isSuccessful()) {
67 errors = barrierResult.getErrors();
69 } catch (Exception e) {
70 RpcError rpcError = RpcResultBuilder.newWarning(
72 OFConstants.ERROR_TAG_TIMEOUT,
73 "barrier sending failed",
74 OFConstants.APPLICATION_TAG,
75 "switch failed to respond on barrier request - message ordering is not preserved",
77 errors = Lists.newArrayList(rpcError);
82 errors = Collections.emptyList();
90 * @param originalResult
91 * @param notificationProviderService
92 * @param notificationComposer lazy notification composer
94 public static <R extends RpcResult<? extends TransactionAware>, N extends Notification, I extends DataContainer>
95 void hookFutureNotification(
96 final OFRpcTask<I, R> task,
97 final ListenableFuture<R> originalResult,
98 final NotificationProviderService notificationProviderService,
99 final NotificationComposer<N> notificationComposer) {
101 class FutureCallbackImpl implements FutureCallback<R> {
103 public void onSuccess(final R result) {
104 if(null == notificationProviderService) {
105 LOG.warn("onSuccess(): notificationServiceProvider is null, could not publish result {}",result);
106 } else if (notificationComposer == null) {
107 LOG.warn("onSuccess(): notificationComposer is null, could not publish result {}",result);
108 } else if(result == null) {
109 LOG.warn("onSuccess(): result is null, could not publish result {}",result);
110 } else if (result.getResult() == null) {
111 LOG.warn("onSuccess(): result.getResult() is null, could not publish result {}",result);
112 } else if (result.getResult().getTransactionId() == null) {
113 LOG.warn("onSuccess(): result.getResult().getTransactionId() is null, could not publish result {}",result);
115 notificationProviderService.publish(notificationComposer.compose(result.getResult().getTransactionId()));
116 task.getTaskContext().getMessageSpy().spyMessage(
117 task.getInput(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_SUCCESS);
122 public void onFailure(final Throwable t) {
123 //TODO: good place to notify MD-SAL about errors
124 task.getTaskContext().getMessageSpy().spyMessage(
125 task.getInput(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);
129 Futures.addCallback(originalResult, new FutureCallbackImpl());
133 * @param task of rpcl
134 * @param originalResult
135 * @return chained result with barrier
137 public static <T extends TransactionAware, I extends DataContainer>
138 ListenableFuture<RpcResult<T>> chainFutureBarrier(
139 final OFRpcTask<I, RpcResult<T>> task,
140 final ListenableFuture<RpcResult<T>> originalResult) {
142 ListenableFuture<RpcResult<T>> chainResult = originalResult;
143 if (MoreObjects.firstNonNull(task.isBarrier(), Boolean.FALSE)) {
145 chainResult = Futures.transform(originalResult, new AsyncFunction<RpcResult<T>, RpcResult<T>>() {
148 public ListenableFuture<RpcResult<T>> apply(final RpcResult<T> input) throws Exception {
149 if (input.isSuccessful()) {
150 RpcInputOutputTuple<BarrierInput, ListenableFuture<RpcResult<BarrierOutput>>> sendBarrierRpc = TaskUtil.sendBarrier(
151 task.getSession(), task.getCookie(), task.getMessageService());
152 ListenableFuture<RpcResult<T>> barrierTxResult = Futures.transform(
153 sendBarrierRpc.getOutput(),
154 transformBarrierToTransactionAware(input, sendBarrierRpc.getInput()));
155 return barrierTxResult;
157 return Futures.immediateFuture(input);
168 * @param originalInput
171 protected static <T extends TransactionAware> Function<RpcResult<BarrierOutput>, RpcResult<T>> transformBarrierToTransactionAware(
172 final RpcResult<T> originalInput, final BarrierInput barrierInput) {
174 class FunctionImpl implements Function<RpcResult<BarrierOutput>, RpcResult<T>> {
177 public RpcResult<T> apply(final RpcResult<BarrierOutput> barrierResult) {
178 RpcResultBuilder<T> rpcBuilder = null;
179 if (barrierResult.isSuccessful()) {
180 rpcBuilder = RpcResultBuilder.<T>success();
182 rpcBuilder = RpcResultBuilder.<T>failed();
183 RpcError rpcError = RpcResultBuilder.newWarning(
185 OFConstants.ERROR_TAG_TIMEOUT,
186 "barrier sending failed",
187 OFConstants.APPLICATION_TAG,
188 "switch failed to respond on barrier request, barrier.xid = "+barrierInput.getXid(),
190 List<RpcError> chainedErrors = new ArrayList<>();
191 chainedErrors.add(rpcError);
192 chainedErrors.addAll(barrierResult.getErrors());
193 rpcBuilder.withRpcErrors(chainedErrors);
196 rpcBuilder.withResult(originalInput.getResult());
198 return rpcBuilder.build();
202 return new FunctionImpl();