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 java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.Future;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
19 import org.opendaylight.openflowplugin.api.openflow.md.core.sal.NotificationComposer;
20 import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
21 import org.opendaylight.openflowplugin.openflow.md.core.MessageFactory;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
23 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
24 import org.opendaylight.openflowplugin.openflow.md.util.RpcInputOutputTuple;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
28 import org.opendaylight.yangtools.yang.binding.DataContainer;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import com.google.common.base.Function;
36 import com.google.common.base.Objects;
37 import com.google.common.collect.Lists;
38 import com.google.common.util.concurrent.AsyncFunction;
39 import com.google.common.util.concurrent.FutureCallback;
40 import com.google.common.util.concurrent.Futures;
41 import com.google.common.util.concurrent.JdkFutureAdapters;
42 import com.google.common.util.concurrent.ListenableFuture;
43 import com.google.common.util.concurrent.SettableFuture;
48 public abstract class OFRpcTaskUtil {
54 * @return rpcResult of given type, containing wrapped errors of barrier sending (if any) or success
56 public static Collection<RpcError> manageBarrier(OFRpcTaskContext taskContext, Boolean isBarrier,
57 SwitchConnectionDistinguisher cookie) {
58 Collection<RpcError> errors = null;
59 if (Objects.firstNonNull(isBarrier, Boolean.FALSE)) {
60 RpcInputOutputTuple<BarrierInput, ListenableFuture<RpcResult<BarrierOutput>>> sendBarrierRpc =
61 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();
91 * @param messageService
92 * @return barrier response
94 protected static RpcInputOutputTuple<BarrierInput, ListenableFuture<RpcResult<BarrierOutput>>> sendBarrier(SessionContext session,
95 SwitchConnectionDistinguisher cookie, IMessageDispatchService messageService) {
96 BarrierInput barrierInput = MessageFactory.createBarrier(
97 session.getFeatures().getVersion(), session.getNextXid());
98 Future<RpcResult<BarrierOutput>> barrierResult = messageService.barrier(barrierInput, cookie);
99 ListenableFuture<RpcResult<BarrierOutput>> output = JdkFutureAdapters.listenInPoolThread(barrierResult);
101 return new RpcInputOutputTuple<>(barrierInput, output);
105 * @param result rpcResult with success = false, errors = given collection
106 * @param barrierErrors
108 public static <T> void wrapBarrierErrors(SettableFuture<RpcResult<T>> result,
109 Collection<RpcError> barrierErrors) {
110 result.set(RpcResultBuilder.<T>failed().withRpcErrors(barrierErrors).build());
115 * @param originalResult
116 * @param notificationProviderService
117 * @param notificationComposer lazy notification composer
119 public static <R extends RpcResult<? extends TransactionAware>, N extends Notification, INPUT extends DataContainer>
120 void hookFutureNotification(
121 final OFRpcTask<INPUT, R> task,
122 ListenableFuture<R> originalResult,
123 final NotificationProviderService notificationProviderService,
124 final NotificationComposer<N> notificationComposer) {
125 Futures.addCallback(originalResult, new FutureCallback<R>() {
127 public void onSuccess(R result) {
128 if (null != notificationProviderService) {
129 notificationProviderService.publish(notificationComposer.compose(result.getResult().getTransactionId()));
131 task.getTaskContext().getMessageSpy().spyMessage(
132 task.getInput(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_SUCCESS);
136 public void onFailure(Throwable t) {
137 //TODO: good place to notify MD-SAL about errors
138 task.getTaskContext().getMessageSpy().spyMessage(
139 task.getInput(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);
146 * @param originalResult
147 * @param notificationProviderService
148 * @param notificationComposer lazy notification composer
149 * @return chained result with barrier
151 public static <TX extends TransactionAware, INPUT extends DataContainer>
152 ListenableFuture<RpcResult<TX>> chainFutureBarrier(
153 final OFRpcTask<INPUT, RpcResult<TX>> task,
154 final ListenableFuture<RpcResult<TX>> originalResult) {
156 ListenableFuture<RpcResult<TX>> chainResult = originalResult;
157 if (Objects.firstNonNull(task.isBarrier(), Boolean.FALSE)) {
159 chainResult = Futures.transform(originalResult, new AsyncFunction<RpcResult<TX>, RpcResult<TX>>() {
162 public ListenableFuture<RpcResult<TX>> apply(final RpcResult<TX> input) throws Exception {
163 if (input.isSuccessful()) {
164 RpcInputOutputTuple<BarrierInput, ListenableFuture<RpcResult<BarrierOutput>>> sendBarrierRpc = sendBarrier(
165 task.getSession(), task.getCookie(), task.getMessageService());
166 ListenableFuture<RpcResult<TX>> barrierTxResult = Futures.transform(
167 sendBarrierRpc.getOutput(),
168 transformBarrierToTransactionAware(input, sendBarrierRpc.getInput()));
169 return barrierTxResult;
171 return Futures.immediateFuture(input);
182 * @param originalInput
185 protected static <TX extends TransactionAware> Function<RpcResult<BarrierOutput>, RpcResult<TX>> transformBarrierToTransactionAware(
186 final RpcResult<TX> originalInput, final BarrierInput barrierInput) {
187 return new Function<RpcResult<BarrierOutput>, RpcResult<TX>>() {
190 public RpcResult<TX> apply(final RpcResult<BarrierOutput> barrierResult) {
191 RpcResultBuilder<TX> rpcBuilder = null;
192 if (barrierResult.isSuccessful()) {
193 rpcBuilder = RpcResultBuilder.<TX>success();
195 rpcBuilder = RpcResultBuilder.<TX>failed();
196 RpcError rpcError = RpcResultBuilder.newWarning(
198 OFConstants.ERROR_TAG_TIMEOUT,
199 "barrier sending failed",
200 OFConstants.APPLICATION_TAG,
201 "switch failed to respond on barrier request, barrier.xid = "+barrierInput.getXid(),
203 List<RpcError> chainedErrors = new ArrayList<>();
204 chainedErrors.add(rpcError);
205 chainedErrors.addAll(barrierResult.getErrors());
206 rpcBuilder.withRpcErrors(chainedErrors);
209 rpcBuilder.withResult(originalInput.getResult());
211 return rpcBuilder.build();