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.atomic.AtomicLong;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.NotificationListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
39 * device using dataStore strategy (FRM involved)
41 public class DropTestCommiter extends AbstractDropTest {
42 private final static Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
44 private DataBroker dataService;
46 private static final AtomicLong idCounter = new AtomicLong();
48 private static final ThreadLocal<FlowBuilder> BUILDER = new ThreadLocal<FlowBuilder>() {
50 protected FlowBuilder initialValue() {
51 final FlowBuilder fb = new FlowBuilder();
55 final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
57 fb.setCookieMask(cookie);
59 fb.setTableId((short) 0);
60 fb.setHardTimeout(300);
61 fb.setIdleTimeout(240);
62 fb.setFlags(new FlowModFlags(false, false, false, false, false));
67 private NotificationProviderService notificationService;
69 private ListenerRegistration<NotificationListener> notificationRegistration;
72 * start listening on packetIn
75 notificationRegistration = notificationService.registerNotificationListener(this);
79 * @param dataService the dataService to set
81 public void setDataService(DataBroker dataService) {
82 this.dataService = dataService;
86 protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
88 // Finally build our flow
89 final FlowBuilder fb = BUILDER.get();
91 fb.setInstructions(instructions);
92 fb.setId(new FlowId(String.valueOf(fb.hashCode()) +"."+ String.valueOf(idCounter.getAndIncrement())));
94 // Construct the flow instance id
95 final InstanceIdentifier<Flow> flowInstanceId =
96 InstanceIdentifier.builder(Nodes.class) // File under nodes
97 .child(Node.class, node) // A particular node identified by nodeKey
98 .augmentation(FlowCapableNode.class) // That is flow capable, only FlowCapableNodes have tables
99 .child(Table.class, new TableKey((short) 0)) // In the table identified by TableKey
100 .child(Flow.class, new FlowKey(fb.getId())) // A flow identified by flowKey
103 final Flow flow = fb.build();
104 final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
106 if (LOG.isDebugEnabled()) {
107 LOG.debug("onPacketReceived - About to write flow {}", flow);
109 transaction.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
110 transaction.submit();
111 LOG.debug("onPacketReceived - About to write flow commited");
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);
127 * @param notificationService
129 public void setNotificationService(NotificationProviderService notificationService) {
130 this.notificationService = notificationService;