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.testcommon;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.math.BigInteger;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
17 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
36 * device using {@link SalFlowService} strategy.
38 public class DropTestRpcSender extends AbstractDropTest {
39 private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
41 private SalFlowService flowService;
43 public void setFlowService(final SalFlowService flowService) {
44 this.flowService = flowService;
47 private static final ThreadLocal<AddFlowInputBuilder> BUILDER = ThreadLocal.withInitial(() -> {
48 final AddFlowInputBuilder fb = new AddFlowInputBuilder();
50 fb.setPriority(PRIORITY);
51 fb.setBufferId(BUFFER_ID);
53 final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
55 fb.setCookieMask(cookie);
56 fb.setTableId(TABLE_ID);
57 fb.setHardTimeout(HARD_TIMEOUT);
58 fb.setIdleTimeout(IDLE_TIMEOUT);
59 fb.setFlags(new FlowModFlags(false, false, false, false, false));
64 private NotificationService notificationService;
66 private ListenerRegistration<DropTestRpcSender> notificationRegistration;
69 * Start listening on packetIn.
71 @SuppressWarnings("checkstyle:IllegalCatch")
73 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
74 STARTUP_LOOP_MAX_RETRIES);
76 notificationRegistration = looper.loopUntilNoException(
77 () -> notificationService.registerNotificationListener(DropTestRpcSender.this));
78 } catch (Exception e) {
79 LOG.warn("DropTest sender notification listener registration fail!");
80 LOG.debug("DropTest sender notification listener registration fail! ..", e);
81 throw new IllegalStateException("DropTest startup fail! Try again later.", e);
86 protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
87 final Instructions instructions) {
88 final AddFlowInputBuilder fb = BUILDER.get();
90 // Finally build our flow
92 fb.setInstructions(instructions);
94 // Construct the flow instance id
96 fb.setNode(new NodeRef(node));
99 final AddFlowInput flow = fb.build();
100 if (LOG.isDebugEnabled()) {
101 LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
103 ListenableFuture<RpcResult<AddFlowOutput>> result =
104 JdkFutureAdapters.listenInPoolThread(flowService.addFlow(flow));
105 Futures.addCallback(result, new FutureCallback<RpcResult<AddFlowOutput>>() {
107 public void onSuccess(final RpcResult<AddFlowOutput> result) {
108 countFutureSuccess();
112 public void onFailure(final Throwable throwable) {
115 }, MoreExecutors.directExecutor());
118 public void setNotificationService(final NotificationService notificationService) {
119 this.notificationService = notificationService;
123 @SuppressWarnings("checkstyle:IllegalCatch")
124 public void close() {
127 LOG.debug("DropTestProvider stopped.");
128 if (notificationRegistration != null) {
129 notificationRegistration.close();
131 } catch (RuntimeException e) {
132 LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
133 LOG.debug("unregistration of notification listener failed.. ", e);