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 java.math.BigInteger;
15 import java.util.concurrent.Callable;
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.SalFlowService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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);
40 private static final InstanceIdentifier<Nodes> NODES_IDENTIFIER = InstanceIdentifier.create(Nodes.class);
42 private SalFlowService flowService;
45 * @param flowService the flowService to set
47 public void setFlowService(final SalFlowService flowService) {
48 this.flowService = flowService;
51 private static final ThreadLocal<AddFlowInputBuilder> BUILDER = new ThreadLocal<AddFlowInputBuilder>() {
53 protected AddFlowInputBuilder initialValue() {
54 final AddFlowInputBuilder fb = new AddFlowInputBuilder();
56 fb.setPriority(PRIORITY);
57 fb.setBufferId(BUFFER_ID);
59 final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
61 fb.setCookieMask(cookie);
62 fb.setTableId(TABLE_ID);
63 fb.setHardTimeout(HARD_TIMEOUT);
64 fb.setIdleTimeout(IDLE_TIMEOUT);
65 fb.setFlags(new FlowModFlags(false, false, false, false, false));
71 private NotificationService notificationService;
73 private ListenerRegistration<DropTestRpcSender> notificationRegistration;
76 * start listening on packetIn
79 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
80 STARTUP_LOOP_MAX_RETRIES);
82 notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DropTestRpcSender>>() {
84 public ListenerRegistration<DropTestRpcSender> call() throws Exception {
85 return notificationService.registerNotificationListener(DropTestRpcSender.this);
88 } catch (final Exception e) {
89 LOG.warn("DropTest sender notification listener registration fail!");
90 LOG.debug("DropTest sender notification listener registration fail! ..", e);
91 throw new IllegalStateException("DropTest startup fail! Try again later.", e);
96 protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
97 final AddFlowInputBuilder fb = BUILDER.get();
99 // Finally build our flow
101 fb.setInstructions(instructions);
103 // Construct the flow instance id
104 final InstanceIdentifier<Node> flowInstanceId = NODES_IDENTIFIER.child(Node.class, node);
105 fb.setNode(new NodeRef(flowInstanceId));
108 final AddFlowInput flow = fb.build();
109 if (LOG.isDebugEnabled()) {
110 LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
112 ListenableFuture result = JdkFutureAdapters.listenInPoolThread(flowService.addFlow(flow));
113 Futures.addCallback(result, new FutureCallback() {
115 public void onSuccess(final Object o) {
116 countFutureSuccess();
120 public void onFailure(final Throwable throwable) {
127 * @param notificationService
129 public void setNotificationService(final NotificationService notificationService) {
130 this.notificationService = notificationService;
134 public void close() {
137 LOG.debug("DropTestProvider stopped.");
138 if (notificationRegistration != null) {
139 notificationRegistration.close();
141 } catch (final Exception e) {
142 LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
143 LOG.debug("unregistration of notification listener failed.. ", e);