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.opendaylight.yangtools.yang.common.Uint16;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45 import org.opendaylight.yangtools.yang.common.Uint8;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseable {
50 private static final Logger LOG = LoggerFactory.getLogger(AbstractDropTest.class);
52 protected static final Uint16 PRIORITY = Uint16.valueOf(4);
53 protected static final Uint32 BUFFER_ID = Uint32.ZERO;
54 protected static final Uint16 HARD_TIMEOUT = Uint16.valueOf(300);
55 protected static final Uint16 IDLE_TIMEOUT = Uint16.valueOf(240);
56 protected static final Uint8 TABLE_ID = Uint8.ZERO;
58 static final long STARTUP_LOOP_TICK = 500L;
59 static final int STARTUP_LOOP_MAX_RETRIES = 8;
60 private static final int PROCESSING_POOL_SIZE = 10000;
62 private static final int POOL_THREAD_AMOUNT = 8;
63 private final ExecutorService executorService;
66 private static final AtomicIntegerFieldUpdater<AbstractDropTest> SENT_UPDATER = AtomicIntegerFieldUpdater
67 .newUpdater(AbstractDropTest.class, "sent");
68 private volatile int sent;
70 private static final AtomicIntegerFieldUpdater<AbstractDropTest> RCVD_UPDATER = AtomicIntegerFieldUpdater
71 .newUpdater(AbstractDropTest.class, "rcvd");
72 private volatile int rcvd;
74 private static final AtomicIntegerFieldUpdater<AbstractDropTest> EXCS_UPDATER = AtomicIntegerFieldUpdater
75 .newUpdater(AbstractDropTest.class, "excs");
76 private volatile int excs;
78 protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_SUCCESS_UPDATER =
79 AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrSuccess");
80 protected volatile int ftrSuccess;
82 protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_FAIL_UPDATER =
83 AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrFailed");
84 protected volatile int ftrFailed;
86 protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_EXECUTED = AtomicIntegerFieldUpdater
87 .newUpdater(AbstractDropTest.class, "runablesExecuted");
88 protected volatile int runablesExecuted;
90 protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_REJECTED = AtomicIntegerFieldUpdater
91 .newUpdater(AbstractDropTest.class, "runablesRejected");
92 protected volatile int runablesRejected;
94 public final DropTestStats getStats() {
95 return new DropTestStats(this.sent, this.rcvd, this.excs, this.ftrFailed, this.ftrSuccess,
96 this.runablesExecuted, this.runablesRejected);
100 final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(PROCESSING_POOL_SIZE);
101 final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(POOL_THREAD_AMOUNT, POOL_THREAD_AMOUNT, 0,
102 TimeUnit.MILLISECONDS,
104 threadPool.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("dropTest-%d").build());
105 threadPool.setRejectedExecutionHandler((rejected, executor) -> {
107 workQueue.put(rejected);
108 } catch (final InterruptedException e) {
109 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
113 executorService = threadPool;
116 public final void clearStats() {
122 this.runablesExecuted = 0;
123 this.runablesRejected = 0;
126 private void incrementRunableExecuted() {
127 RUNABLES_EXECUTED.incrementAndGet(this);
130 private void incrementRunableRejected() {
131 RUNABLES_REJECTED.incrementAndGet(this);
135 public final void onPacketReceived(final PacketReceived notification) {
136 LOG.debug("onPacketReceived - Entering - {}", notification);
138 RCVD_UPDATER.incrementAndGet(this);
141 executorService.execute(() -> {
142 incrementRunableExecuted();
143 processPacket(notification);
145 } catch (RejectedExecutionException e) {
146 incrementRunableRejected();
148 LOG.debug("onPacketReceived - {} Leaving", notification);
151 private static final Instructions DROP_INSTRUCTIONS = makeStaticDropActionInstructions();
153 private static Instructions makeStaticDropActionInstructions() {
154 // Create an DropAction
155 final DropActionCase dropAction = new DropActionCaseBuilder().setDropAction(
156 new DropActionBuilder().build()).build();
158 final Action ab = new ActionBuilder().setOrder(0).setAction(dropAction).build();
159 // Create an Apply Action
160 final ApplyActions aab = new ApplyActionsBuilder().setAction(Collections.singletonMap(ab.key(), ab)).build();
161 // Wrap our Apply Action in an Instruction
162 final Instruction ib = new InstructionBuilder()
163 .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab).build()).setOrder(0).build();
164 // Put our Instruction in a list of Instructions
165 return new InstructionsBuilder().setInstruction(Collections.singletonMap(ib.key(), ib)).build();
168 @SuppressWarnings("checkstyle:IllegalCatch")
169 private void processPacket(final PacketReceived notification) {
171 final byte[] rawPacket = notification.getPayload();
172 final byte[] srcMac = Arrays.copyOfRange(rawPacket, 6, 12);
174 final MatchBuilder match = new MatchBuilder();
175 final EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
176 final EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
178 //TODO: use HEX, use binary form
179 //Hex.decodeHex("000000000001".toCharArray());
181 ethSourceBuilder.setAddress(new MacAddress(macAddressToString(srcMac)));
182 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
183 match.setEthernetMatch(ethernetMatch.build());
185 // Get the Ingress nodeConnectorRef
186 final NodeConnectorRef ncr = notification.getIngress();
188 // Get the instance identifier for the nodeConnectorRef
189 final InstanceIdentifier<?> ncri = ncr.getValue();
191 processPacket(ncri.firstIdentifierOf(Node.class), match.build(), DROP_INSTRUCTIONS);
193 SENT_UPDATER.incrementAndGet(this);
194 } catch (RuntimeException e) {
195 LOG.warn("Failed to process packet: {}", e.getMessage());
196 LOG.debug("Failed to process packet.. ", e);
197 EXCS_UPDATER.incrementAndGet(this);
201 protected abstract void processPacket(InstanceIdentifier<Node> node, Match match, Instructions instructions);
205 public void close() {
206 executorService.shutdown();
209 public void countFutureSuccess() {
210 RPC_FUTURE_SUCCESS_UPDATER.incrementAndGet(this);
213 public void countFutureError() {
214 RPC_FUTURE_FAIL_UPDATER.incrementAndGet(this);