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 static java.util.Objects.requireNonNull;
12 import java.util.concurrent.atomic.AtomicLong;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.NotificationService;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
32 import org.opendaylight.yangtools.concepts.Registration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.Uint64;
35 import org.opendaylight.yangtools.yang.common.Uint8;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Deactivate;
39 import org.osgi.service.component.annotations.Reference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to device using dataStore
45 * strategy (FRM involved).
48 @Component(service = DropTestCommiter.class, immediate = true)
49 public final class DropTestCommiterImpl extends AbstractDropTest implements DropTestCommiter {
50 private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiterImpl.class);
51 private static final TableKey ZERO_TABLE = new TableKey(Uint8.ZERO);
52 private static final ThreadLocal<FlowBuilder> BUILDER = ThreadLocal.withInitial(() -> {
53 final var cookie = new FlowCookie(Uint64.TEN);
54 return new FlowBuilder()
55 .setPriority(PRIORITY)
56 .setBufferId(BUFFER_ID)
58 .setCookieMask(cookie)
60 .setHardTimeout(HARD_TIMEOUT)
61 .setIdleTimeout(IDLE_TIMEOUT)
62 .setFlags(new FlowModFlags(false, false, false, false, false));
65 private final AtomicLong idCounter = new AtomicLong();
66 private final DataBroker dataBroker;
67 private final NotificationService notificationService;
69 private Registration reg = null;
73 public DropTestCommiterImpl(@Reference final DataBroker dataBroker,
74 @Reference final NotificationService notificationService) {
75 this.dataBroker = requireNonNull(dataBroker);
76 this.notificationService = requireNonNull(notificationService);
85 LOG.debug("DropTestProvider terminated");
89 * Start listening on packetIn.
91 * @return {@code false} if already started
94 public synchronized boolean start() {
98 reg = notificationService.registerListener(PacketReceived.class, this);
99 LOG.debug("DropTestProvider started");
104 * Stop listening on packetIn.
106 * @return {@code false} if already stopped
109 public synchronized boolean stop() {
115 LOG.debug("DropTestProvider stopped");
120 protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
121 final Instructions instructions) {
123 // Finally build our flow
124 final FlowBuilder fb = BUILDER.get();
126 fb.setInstructions(instructions);
127 fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + idCounter.getAndIncrement()));
129 // Construct the flow instance id
130 final var flowInstanceId = node.builder()
131 // That is flow capable, only FlowCapableNodes have tables
132 .augmentation(FlowCapableNode.class)
133 // In the table identified by TableKey
134 .child(Table.class, ZERO_TABLE)
135 // A flow identified by flowKey
136 .child(Flow.class, new FlowKey(fb.getId()))
139 final var flow = fb.build();
140 final var transaction = dataBroker.newReadWriteTransaction();
142 LOG.debug("onPacketReceived - About to write flow {}", flow);
143 transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow);
144 transaction.commit();
145 LOG.debug("onPacketReceived - About to write flow commited");