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 static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.BUFFER_ID;
11 import static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.HARD_TIMEOUT;
12 import static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.IDLE_TIMEOUT;
13 import static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.PRIORITY;
14 import static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.TABLE_ID;
16 import java.math.BigInteger;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
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.opendaylight.yangtools.yang.binding.NotificationListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
37 * device using {@link SalFlowService} strategy
39 public class DropTestRpcSender extends AbstractDropTest {
40 private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
42 private SalFlowService flowService;
45 * @param flowService the flowService to set
47 public void setFlowService(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 NotificationProviderService notificationService;
73 private ListenerRegistration<NotificationListener> notificationRegistration;
76 * start listening on packetIn
79 notificationRegistration = notificationService.registerNotificationListener(this);
83 protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
84 final AddFlowInputBuilder fb = BUILDER.get();
86 // Finally build our flow
88 fb.setInstructions(instructions);
89 //fb.setId(new FlowId(Long.toString(fb.hashCode)));
91 // Construct the flow instance id
92 final InstanceIdentifier<Node> flowInstanceId = InstanceIdentifier
95 // A particular node identified by nodeKey
96 .child(Node.class, node).build();
97 fb.setNode(new NodeRef(flowInstanceId));
100 AddFlowInput flow = fb.build();
101 if (LOG.isDebugEnabled()) {
102 LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
104 flowService.addFlow(flow);
108 * @param notificationService
110 public void setNotificationService(NotificationProviderService notificationService) {
111 this.notificationService = notificationService;
115 public void close() {
117 LOG.debug("DropTestProvider stopped.");
118 if (notificationRegistration != null) {
119 notificationRegistration.close();
121 } catch (Exception e) {
122 LOG.error("unregistration of notification listener failed", e);