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 java.math.BigInteger;
11 import java.util.concurrent.Callable;
12 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
13 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.binding.NotificationListener;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
33 * device using {@link SalFlowService} strategy
35 public class DropTestRpcSender extends AbstractDropTest {
36 private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
38 private SalFlowService flowService;
41 * @param flowService the flowService to set
43 public void setFlowService(SalFlowService flowService) {
44 this.flowService = flowService;
47 private static final ThreadLocal<AddFlowInputBuilder> BUILDER = new ThreadLocal<AddFlowInputBuilder>() {
49 protected AddFlowInputBuilder initialValue() {
50 final AddFlowInputBuilder fb = new AddFlowInputBuilder();
52 fb.setPriority(PRIORITY);
53 fb.setBufferId(BUFFER_ID);
55 final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
57 fb.setCookieMask(cookie);
58 fb.setTableId(TABLE_ID);
59 fb.setHardTimeout(HARD_TIMEOUT);
60 fb.setIdleTimeout(IDLE_TIMEOUT);
61 fb.setFlags(new FlowModFlags(false, false, false, false, false));
67 private NotificationProviderService notificationService;
69 private ListenerRegistration<NotificationListener> notificationRegistration;
72 * start listening on packetIn
75 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
76 STARTUP_LOOP_MAX_RETRIES);
78 notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<NotificationListener>>() {
80 public ListenerRegistration<NotificationListener> call() throws Exception {
81 return notificationService.registerNotificationListener(DropTestRpcSender.this);
84 } catch (Exception e) {
85 LOG.warn("DropTest sender notification listener registration fail!");
86 LOG.debug("DropTest sender notification listener registration fail! ..", e);
87 throw new IllegalStateException("DropTest startup fail! Try again later.", e);
92 protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
93 final AddFlowInputBuilder fb = BUILDER.get();
95 // Finally build our flow
97 fb.setInstructions(instructions);
98 //fb.setId(new FlowId(Long.toString(fb.hashCode)));
100 // Construct the flow instance id
101 final InstanceIdentifier<Node> flowInstanceId = InstanceIdentifier
103 .builder(Nodes.class)
104 // A particular node identified by nodeKey
105 .child(Node.class, node).build();
106 fb.setNode(new NodeRef(flowInstanceId));
109 AddFlowInput flow = fb.build();
110 if (LOG.isDebugEnabled()) {
111 LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
113 flowService.addFlow(flow);
117 * @param notificationService
119 public void setNotificationService(NotificationProviderService notificationService) {
120 this.notificationService = notificationService;
124 public void close() {
126 LOG.debug("DropTestProvider stopped.");
127 if (notificationRegistration != null) {
128 notificationRegistration.close();
130 } catch (Exception e) {
131 LOG.error("unregistration of notification listener failed", e);