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 java.util.concurrent.atomic.AtomicLong;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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 static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
44 private DataBroker dataService;
46 private static final AtomicLong ID_COUNTER = new AtomicLong();
48 private static final ThreadLocal<FlowBuilder> BUILDER = new ThreadLocal<FlowBuilder>() {
50 protected FlowBuilder initialValue() {
51 final FlowBuilder fb = new FlowBuilder();
53 fb.setPriority(PRIORITY);
54 fb.setBufferId(BUFFER_ID);
55 final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
57 fb.setCookieMask(cookie);
59 fb.setTableId(TABLE_ID);
60 fb.setHardTimeout(HARD_TIMEOUT);
61 fb.setIdleTimeout(IDLE_TIMEOUT);
62 fb.setFlags(new FlowModFlags(false, false, false, false, false));
67 private NotificationService notificationService;
69 private ListenerRegistration<DropTestCommiter> notificationRegistration;
72 * start listening on packetIn
75 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
76 STARTUP_LOOP_MAX_RETRIES);
78 notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DropTestCommiter>>() {
80 public ListenerRegistration<DropTestCommiter> call() throws Exception {
81 return notificationService.registerNotificationListener(DropTestCommiter.this);
84 } catch (final Exception e) {
85 LOG.warn("DropTest committer notification listener registration fail!");
86 LOG.debug("DropTest committer notification listener registration fail! ..", e);
87 throw new IllegalStateException("DropTest startup fail! Try again later.", e);
92 * @param dataService the dataService to set
94 public void setDataService(final DataBroker dataService) {
95 this.dataService = dataService;
99 protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
101 // Finally build our flow
102 final FlowBuilder fb = BUILDER.get();
104 fb.setInstructions(instructions);
105 fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + ID_COUNTER.getAndIncrement()));
107 // Construct the flow instance id
108 final InstanceIdentifier<Flow> flowInstanceId =
110 InstanceIdentifier.builder(Nodes.class)
111 // A particular node identified by nodeKey
112 .child(Node.class, node)
113 // That is flow capable, only FlowCapableNodes have tables
114 .augmentation(FlowCapableNode.class)
115 // In the table identified by TableKey
116 .child(Table.class, new TableKey((short) 0))
117 // A flow identified by flowKey
118 .child(Flow.class, new FlowKey(fb.getId()))
121 final Flow flow = fb.build();
122 final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
124 if (LOG.isDebugEnabled()) {
125 LOG.debug("onPacketReceived - About to write flow {}", flow);
127 transaction.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
128 transaction.submit();
129 LOG.debug("onPacketReceived - About to write flow commited");
133 public void close() {
136 LOG.debug("DropTestProvider stopped.");
137 if (notificationRegistration != null) {
138 notificationRegistration.close();
140 } catch (final Exception e) {
141 LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
142 LOG.debug("unregistration of notification listener failed.. ", e);
147 * @param notificationService
149 public void setNotificationService(final NotificationService notificationService) {
150 this.notificationService = notificationService;