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;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
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.openflowplugin.common.wait.SimpleTaskRetryLooper;
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.Node;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
36 * device using dataStore strategy (FRM involved).
38 public class DropTestCommiter extends AbstractDropTest {
39 private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
40 private static final TableKey ZERO_TABLE = new TableKey((short) 0);
41 private DataBroker dataService;
43 private static final AtomicLong ID_COUNTER = new AtomicLong();
45 private static final ThreadLocal<FlowBuilder> BUILDER = ThreadLocal.withInitial(() -> {
46 final FlowBuilder fb = new FlowBuilder();
48 fb.setPriority(PRIORITY);
49 fb.setBufferId(BUFFER_ID);
50 final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
52 fb.setCookieMask(cookie);
54 fb.setTableId(TABLE_ID);
55 fb.setHardTimeout(HARD_TIMEOUT);
56 fb.setIdleTimeout(IDLE_TIMEOUT);
57 fb.setFlags(new FlowModFlags(false, false, false, false, false));
61 private NotificationService notificationService;
63 private ListenerRegistration<DropTestCommiter> notificationRegistration;
66 * start listening on packetIn.
68 @SuppressWarnings("checkstyle:IllegalCatch")
70 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
71 STARTUP_LOOP_MAX_RETRIES);
73 notificationRegistration = looper.loopUntilNoException(() ->
74 notificationService.registerNotificationListener(DropTestCommiter.this));
75 } catch (Exception e) {
76 LOG.warn("DropTest committer notification listener registration fail!");
77 LOG.debug("DropTest committer notification listener registration fail! ..", e);
78 throw new IllegalStateException("DropTest startup fail! Try again later.", e);
82 public void setDataService(final DataBroker dataService) {
83 this.dataService = dataService;
87 protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
88 final Instructions instructions) {
90 // Finally build our flow
91 final FlowBuilder fb = BUILDER.get();
93 fb.setInstructions(instructions);
94 fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + ID_COUNTER.getAndIncrement()));
96 // Construct the flow instance id
97 final InstanceIdentifier<Flow> flowInstanceId = node.builder()
98 // That is flow capable, only FlowCapableNodes have tables
99 .augmentation(FlowCapableNode.class)
100 // In the table identified by TableKey
101 .child(Table.class, ZERO_TABLE)
102 // A flow identified by flowKey
103 .child(Flow.class, new FlowKey(fb.getId()))
106 final Flow flow = fb.build();
107 final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
109 if (LOG.isDebugEnabled()) {
110 LOG.debug("onPacketReceived - About to write flow {}", flow);
112 transaction.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
113 transaction.submit();
114 LOG.debug("onPacketReceived - About to write flow commited");
118 @SuppressWarnings("checkstyle:IllegalCatch")
119 public void close() {
122 LOG.debug("DropTestProvider stopped.");
123 if (notificationRegistration != null) {
124 notificationRegistration.close();
126 } catch (RuntimeException e) {
127 LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
128 LOG.debug("unregistration of notification listener failed.. ", e);
132 public void setNotificationService(final NotificationService notificationService) {
133 this.notificationService = notificationService;