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.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.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.opendaylight.yangtools.yang.binding.NotificationListener;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
40 * device using dataStore strategy (FRM involved)
42 public class DropTestCommiter extends AbstractDropTest {
43 private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
45 private DataBroker dataService;
47 private static final AtomicLong ID_COUNTER = new AtomicLong();
49 private static final ThreadLocal<FlowBuilder> BUILDER = new ThreadLocal<FlowBuilder>() {
51 protected FlowBuilder initialValue() {
52 final FlowBuilder fb = new FlowBuilder();
54 fb.setPriority(PRIORITY);
55 fb.setBufferId(BUFFER_ID);
56 final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
58 fb.setCookieMask(cookie);
60 fb.setTableId(TABLE_ID);
61 fb.setHardTimeout(HARD_TIMEOUT);
62 fb.setIdleTimeout(IDLE_TIMEOUT);
63 fb.setFlags(new FlowModFlags(false, false, false, false, false));
68 private NotificationProviderService notificationService;
70 private ListenerRegistration<NotificationListener> notificationRegistration;
73 * start listening on packetIn
76 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
77 STARTUP_LOOP_MAX_RETRIES);
79 notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<NotificationListener>>() {
81 public ListenerRegistration<NotificationListener> call() throws Exception {
82 return notificationService.registerNotificationListener(DropTestCommiter.this);
85 } catch (Exception e) {
86 LOG.warn("DropTest committer notification listener registration fail!");
87 LOG.debug("DropTest committer notification listener registration fail! ..", e);
88 throw new IllegalStateException("DropTest startup fail! Try again later.", e);
93 * @param dataService the dataService to set
95 public void setDataService(DataBroker dataService) {
96 this.dataService = dataService;
100 protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
102 // Finally build our flow
103 final FlowBuilder fb = BUILDER.get();
105 fb.setInstructions(instructions);
106 fb.setId(new FlowId(String.valueOf(fb.hashCode()) +"."+ ID_COUNTER.getAndIncrement()));
108 // Construct the flow instance id
109 final InstanceIdentifier<Flow> flowInstanceId =
111 InstanceIdentifier.builder(Nodes.class)
112 // A particular node identified by nodeKey
113 .child(Node.class, node)
114 // That is flow capable, only FlowCapableNodes have tables
115 .augmentation(FlowCapableNode.class)
116 // In the table identified by TableKey
117 .child(Table.class, new TableKey((short) 0))
118 // A flow identified by flowKey
119 .child(Flow.class, new FlowKey(fb.getId()))
122 final Flow flow = fb.build();
123 final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
125 if (LOG.isDebugEnabled()) {
126 LOG.debug("onPacketReceived - About to write flow {}", flow);
128 transaction.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
129 transaction.submit();
130 LOG.debug("onPacketReceived - About to write flow commited");
134 public void close() {
136 LOG.debug("DropTestProvider stopped.");
137 if (notificationRegistration != null) {
138 notificationRegistration.close();
140 } catch (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(NotificationProviderService notificationService) {
150 this.notificationService = notificationService;