2 * Copyright (c) 2014, 2015 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 org.opendaylight.openflowjava.util.ByteBufUtils.macAddressToString;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.concurrent.ArrayBlockingQueue;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.RejectedExecutionException;
18 import java.util.concurrent.ThreadPoolExecutor;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
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.InstructionsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseable {
47 private static final Logger LOG = LoggerFactory.getLogger(AbstractDropTest.class);
49 protected static final Integer PRIORITY = 4;
50 protected static final Long BUFFER_ID = 0L;
51 protected static final Integer HARD_TIMEOUT = 300;
52 protected static final Integer IDLE_TIMEOUT = 240;
53 protected static final short TABLE_ID = 0;
55 static final long STARTUP_LOOP_TICK = 500L;
56 static final int STARTUP_LOOP_MAX_RETRIES = 8;
57 private static final int PROCESSING_POOL_SIZE = 10000;
59 private static final int POOL_THREAD_AMOUNT = 8;
60 private final ExecutorService executorService;
63 private static final AtomicIntegerFieldUpdater<AbstractDropTest> SENT_UPDATER = AtomicIntegerFieldUpdater
64 .newUpdater(AbstractDropTest.class, "sent");
65 private volatile int sent;
67 private static final AtomicIntegerFieldUpdater<AbstractDropTest> RCVD_UPDATER = AtomicIntegerFieldUpdater
68 .newUpdater(AbstractDropTest.class, "rcvd");
69 private volatile int rcvd;
71 private static final AtomicIntegerFieldUpdater<AbstractDropTest> EXCS_UPDATER = AtomicIntegerFieldUpdater
72 .newUpdater(AbstractDropTest.class, "excs");
73 private volatile int excs;
75 protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_SUCCESS_UPDATER =
76 AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrSuccess");
77 protected volatile int ftrSuccess;
79 protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_FAIL_UPDATER =
80 AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrFailed");
81 protected volatile int ftrFailed;
83 protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_EXECUTED = AtomicIntegerFieldUpdater
84 .newUpdater(AbstractDropTest.class, "runablesExecuted");
85 protected volatile int runablesExecuted;
87 protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_REJECTED = AtomicIntegerFieldUpdater
88 .newUpdater(AbstractDropTest.class, "runablesRejected");
89 protected volatile int runablesRejected;
91 public final DropTestStats getStats() {
92 return new DropTestStats(this.sent, this.rcvd, this.excs, this.ftrFailed, this.ftrSuccess,
93 this.runablesExecuted, this.runablesRejected);
97 final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(PROCESSING_POOL_SIZE);
98 final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(POOL_THREAD_AMOUNT, POOL_THREAD_AMOUNT, 0,
99 TimeUnit.MILLISECONDS,
101 threadPool.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("dropTest-%d").build());
102 threadPool.setRejectedExecutionHandler((rejected, executor) -> {
104 workQueue.put(rejected);
105 } catch (final InterruptedException e) {
106 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
110 executorService = threadPool;
113 public final void clearStats() {
119 this.runablesExecuted = 0;
120 this.runablesRejected = 0;
123 private void incrementRunableExecuted() {
124 RUNABLES_EXECUTED.incrementAndGet(this);
127 private void incrementRunableRejected() {
128 RUNABLES_REJECTED.incrementAndGet(this);
132 public final void onPacketReceived(final PacketReceived notification) {
133 LOG.debug("onPacketReceived - Entering - {}", notification);
135 RCVD_UPDATER.incrementAndGet(this);
138 executorService.execute(() -> {
139 incrementRunableExecuted();
140 processPacket(notification);
142 } catch (RejectedExecutionException e) {
143 incrementRunableRejected();
145 LOG.debug("onPacketReceived - {} Leaving", notification);
148 private static final Instructions DROP_INSTRUCTIONS = makeStaticDropActionInstructions();
150 private static Instructions makeStaticDropActionInstructions() {
151 // Create an DropAction
152 final DropActionCase dropAction = new DropActionCaseBuilder().setDropAction(
153 new DropActionBuilder().build()).build();
155 final Action ab = new ActionBuilder().setOrder(0).setAction(dropAction).build();
156 // Create an Apply Action
157 final ApplyActions aab = new ApplyActionsBuilder().setAction(Collections.singletonList(ab)).build();
158 // Wrap our Apply Action in an Instruction
159 final Instruction ib = new InstructionBuilder()
160 .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab).build()).setOrder(0).build();
161 // Put our Instruction in a list of Instructions
162 return new InstructionsBuilder().setInstruction(Collections.singletonList(ib)).build();
165 @SuppressWarnings("checkstyle:IllegalCatch")
166 private void processPacket(final PacketReceived notification) {
168 final byte[] rawPacket = notification.getPayload();
169 final byte[] srcMac = Arrays.copyOfRange(rawPacket, 6, 12);
171 final MatchBuilder match = new MatchBuilder();
172 final EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
173 final EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
175 //TODO: use HEX, use binary form
176 //Hex.decodeHex("000000000001".toCharArray());
178 ethSourceBuilder.setAddress(new MacAddress(macAddressToString(srcMac)));
179 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
180 match.setEthernetMatch(ethernetMatch.build());
182 // Get the Ingress nodeConnectorRef
183 final NodeConnectorRef ncr = notification.getIngress();
185 // Get the instance identifier for the nodeConnectorRef
186 final InstanceIdentifier<?> ncri = ncr.getValue();
188 processPacket(ncri.firstIdentifierOf(Node.class), match.build(), DROP_INSTRUCTIONS);
190 SENT_UPDATER.incrementAndGet(this);
191 } catch (RuntimeException e) {
192 LOG.warn("Failed to process packet: {}", e.getMessage());
193 LOG.debug("Failed to process packet.. ", e);
194 EXCS_UPDATER.incrementAndGet(this);
198 protected abstract void processPacket(InstanceIdentifier<Node> node, Match match, Instructions instructions);
202 public void close() {
203 executorService.shutdown();
206 public void countFutureSuccess() {
207 RPC_FUTURE_SUCCESS_UPDATER.incrementAndGet(this);
210 public void countFutureError() {
211 RPC_FUTURE_FAIL_UPDATER.incrementAndGet(this);