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.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;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
38 * device using {@link SalFlowService} strategy
40 public class DropTestRpcSender extends AbstractDropTest {
41 private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
42 private static final InstanceIdentifier<Nodes> NODES_IDENTIFIER = InstanceIdentifier.create(Nodes.class);
44 private SalFlowService flowService;
47 * @param flowService the flowService to set
49 public void setFlowService(final SalFlowService flowService) {
50 this.flowService = flowService;
53 private static final ThreadLocal<AddFlowInputBuilder> BUILDER = new ThreadLocal<AddFlowInputBuilder>() {
55 protected AddFlowInputBuilder initialValue() {
56 final AddFlowInputBuilder fb = new AddFlowInputBuilder();
58 fb.setPriority(PRIORITY);
59 fb.setBufferId(BUFFER_ID);
61 final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
63 fb.setCookieMask(cookie);
64 fb.setTableId(TABLE_ID);
65 fb.setHardTimeout(HARD_TIMEOUT);
66 fb.setIdleTimeout(IDLE_TIMEOUT);
67 fb.setFlags(new FlowModFlags(false, false, false, false, false));
73 private NotificationService notificationService;
75 private ListenerRegistration<DropTestRpcSender> notificationRegistration;
78 * start listening on packetIn
81 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
82 STARTUP_LOOP_MAX_RETRIES);
84 notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DropTestRpcSender>>() {
86 public ListenerRegistration<DropTestRpcSender> call() throws Exception {
87 return notificationService.registerNotificationListener(DropTestRpcSender.this);
90 } catch (final Exception e) {
91 LOG.warn("DropTest sender notification listener registration fail!");
92 LOG.debug("DropTest sender notification listener registration fail! ..", e);
93 throw new IllegalStateException("DropTest startup fail! Try again later.", e);
98 protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
99 final AddFlowInputBuilder fb = BUILDER.get();
101 // Finally build our flow
103 fb.setInstructions(instructions);
105 // Construct the flow instance id
106 final InstanceIdentifier<Node> flowInstanceId = NODES_IDENTIFIER.child(Node.class, node);
107 fb.setNode(new NodeRef(flowInstanceId));
110 final AddFlowInput flow = fb.build();
111 if (LOG.isDebugEnabled()) {
112 LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
114 ListenableFuture<RpcResult<AddFlowOutput>> result = JdkFutureAdapters.listenInPoolThread(flowService.addFlow(flow));
115 Futures.addCallback(result, new FutureCallback<RpcResult<AddFlowOutput>>() {
117 public void onSuccess(final RpcResult<AddFlowOutput> o) {
118 countFutureSuccess();
122 public void onFailure(final Throwable throwable) {
129 * @param notificationService
131 public void setNotificationService(final NotificationService notificationService) {
132 this.notificationService = notificationService;
136 public void close() {
139 LOG.debug("DropTestProvider stopped.");
140 if (notificationRegistration != null) {
141 notificationRegistration.close();
143 } catch (final Exception e) {
144 LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
145 LOG.debug("unregistration of notification listener failed.. ", e);