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.util.concurrent.atomic.AtomicLong;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.binding.api.NotificationService;
13 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
29 import org.opendaylight.yangtools.concepts.Registration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.Uint64;
32 import org.opendaylight.yangtools.yang.common.Uint8;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
38 * device using dataStore strategy (FRM involved).
40 public class DropTestCommiter extends AbstractDropTest {
41 private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
42 private static final TableKey ZERO_TABLE = new TableKey(Uint8.ZERO);
43 private DataBroker dataService;
45 private static final AtomicLong ID_COUNTER = new AtomicLong();
47 private static final ThreadLocal<FlowBuilder> BUILDER = ThreadLocal.withInitial(() -> {
48 final FlowBuilder fb = new FlowBuilder();
50 fb.setPriority(PRIORITY);
51 fb.setBufferId(BUFFER_ID);
52 final FlowCookie cookie = new FlowCookie(Uint64.TEN);
54 fb.setCookieMask(cookie);
56 fb.setTableId(TABLE_ID);
57 fb.setHardTimeout(HARD_TIMEOUT);
58 fb.setIdleTimeout(IDLE_TIMEOUT);
59 fb.setFlags(new FlowModFlags(false, false, false, false, false));
63 private NotificationService notificationService;
65 private Registration notificationRegistration;
68 * start listening on packetIn.
70 @SuppressWarnings("checkstyle:IllegalCatch")
72 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
73 STARTUP_LOOP_MAX_RETRIES);
75 notificationRegistration = looper.loopUntilNoException(
76 () -> notificationService.registerListener(PacketReceived.class, this));
77 } catch (Exception e) {
78 LOG.warn("DropTest committer notification listener registration fail!");
79 LOG.debug("DropTest committer notification listener registration fail! ..", e);
80 throw new IllegalStateException("DropTest startup fail! Try again later.", e);
84 public void setDataService(final DataBroker dataService) {
85 this.dataService = dataService;
89 protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
90 final Instructions instructions) {
92 // Finally build our flow
93 final FlowBuilder fb = BUILDER.get();
95 fb.setInstructions(instructions);
96 fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + ID_COUNTER.getAndIncrement()));
98 // Construct the flow instance id
99 final InstanceIdentifier<Flow> flowInstanceId = node.builder()
100 // That is flow capable, only FlowCapableNodes have tables
101 .augmentation(FlowCapableNode.class)
102 // In the table identified by TableKey
103 .child(Table.class, ZERO_TABLE)
104 // A flow identified by flowKey
105 .child(Flow.class, new FlowKey(fb.getId()))
108 final Flow flow = fb.build();
109 final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
111 if (LOG.isDebugEnabled()) {
112 LOG.debug("onPacketReceived - About to write flow {}", flow);
114 transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow);
115 transaction.commit();
116 LOG.debug("onPacketReceived - About to write flow commited");
120 @SuppressWarnings("checkstyle:IllegalCatch")
121 public void close() {
124 LOG.debug("DropTestProvider stopped.");
125 if (notificationRegistration != null) {
126 notificationRegistration.close();
128 } catch (RuntimeException e) {
129 LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
130 LOG.debug("unregistration of notification listener failed.. ", e);
134 public void setNotificationService(final NotificationService notificationService) {
135 this.notificationService = notificationService;