OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / AbstractDropTest.java
index 4707573098f9a86a55f53cc7d219961878e6c8be..3a7da65b0dcea7bcbb924f03a7ddf8369922c79b 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -9,13 +9,18 @@ package org.opendaylight.openflowplugin.testcommon;
 
 import static org.opendaylight.openflowjava.util.ByteBufUtils.macAddressToString;
 
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropAction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
@@ -24,12 +29,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.I
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
@@ -47,23 +52,80 @@ abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseab
     protected static final Integer IDLE_TIMEOUT = 240;
     protected static final short TABLE_ID = 0;
 
-    private static final AtomicIntegerFieldUpdater<AbstractDropTest> SENT_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "sent");
+    static final long STARTUP_LOOP_TICK = 500L;
+    static final int STARTUP_LOOP_MAX_RETRIES = 8;
+    private static final int PROCESSING_POOL_SIZE = 10000;
+
+    private static final int POOL_THREAD_AMOUNT = 8;
+    private final ExecutorService executorService;
+
+
+    private static final AtomicIntegerFieldUpdater<AbstractDropTest> SENT_UPDATER = AtomicIntegerFieldUpdater
+            .newUpdater(AbstractDropTest.class, "sent");
     private volatile int sent;
 
-    private static final AtomicIntegerFieldUpdater<AbstractDropTest> RCVD_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "rcvd");
+    private static final AtomicIntegerFieldUpdater<AbstractDropTest> RCVD_UPDATER = AtomicIntegerFieldUpdater
+            .newUpdater(AbstractDropTest.class, "rcvd");
     private volatile int rcvd;
 
-    private static final AtomicIntegerFieldUpdater<AbstractDropTest> EXCS_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "excs");
+    private static final AtomicIntegerFieldUpdater<AbstractDropTest> EXCS_UPDATER = AtomicIntegerFieldUpdater
+            .newUpdater(AbstractDropTest.class, "excs");
     private volatile int excs;
 
+    protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_SUCCESS_UPDATER =
+            AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrSuccess");
+    protected volatile int ftrSuccess;
+
+    protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_FAIL_UPDATER =
+            AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrFailed");
+    protected volatile int ftrFailed;
+
+    protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_EXECUTED = AtomicIntegerFieldUpdater
+            .newUpdater(AbstractDropTest.class, "runablesExecuted");
+    protected volatile int runablesExecuted;
+
+    protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_REJECTED = AtomicIntegerFieldUpdater
+            .newUpdater(AbstractDropTest.class, "runablesRejected");
+    protected volatile int runablesRejected;
+
     public final DropTestStats getStats() {
-        return new DropTestStats(this.sent, this.rcvd, this.excs);
+        return new DropTestStats(this.sent, this.rcvd, this.excs, this.ftrFailed, this.ftrSuccess,
+                this.runablesExecuted, this.runablesRejected);
+    }
+
+    AbstractDropTest() {
+        final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(PROCESSING_POOL_SIZE);
+        final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(POOL_THREAD_AMOUNT, POOL_THREAD_AMOUNT, 0,
+                TimeUnit.MILLISECONDS,
+                workQueue);
+        threadPool.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("dropTest-%d").build());
+        threadPool.setRejectedExecutionHandler((rejected, executor) -> {
+            try {
+                workQueue.put(rejected);
+            } catch (final InterruptedException e) {
+                throw new RejectedExecutionException("Interrupted while waiting on queue", e);
+            }
+        });
+
+        executorService = threadPool;
     }
 
-    public final void clearStats(){
+    public final void clearStats() {
         this.sent = 0;
         this.rcvd = 0;
         this.excs = 0;
+        this.ftrSuccess = 0;
+        this.ftrFailed = 0;
+        this.runablesExecuted = 0;
+        this.runablesRejected = 0;
+    }
+
+    private void incrementRunableExecuted() {
+        RUNABLES_EXECUTED.incrementAndGet(this);
+    }
+
+    private void incrementRunableRejected() {
+        RUNABLES_REJECTED.incrementAndGet(this);
     }
 
     @Override
@@ -72,6 +134,36 @@ abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseab
 
         RCVD_UPDATER.incrementAndGet(this);
 
+        try {
+            executorService.execute(() -> {
+                incrementRunableExecuted();
+                processPacket(notification);
+            });
+        } catch (RejectedExecutionException e) {
+            incrementRunableRejected();
+        }
+        LOG.debug("onPacketReceived - {} Leaving", notification);
+    }
+
+    private static final Instructions DROP_INSTRUCTIONS = makeStaticDropActionInstructions();
+
+    private static Instructions makeStaticDropActionInstructions() {
+        // Create an DropAction
+        final DropActionCase dropAction = new DropActionCaseBuilder().setDropAction(
+                new DropActionBuilder().build()).build();
+        // Create an Action
+        final Action ab = new ActionBuilder().setOrder(0).setAction(dropAction).build();
+        // Create an Apply Action
+        final ApplyActions aab = new ApplyActionsBuilder().setAction(Collections.singletonList(ab)).build();
+        // Wrap our Apply Action in an Instruction
+        final Instruction ib = new InstructionBuilder()
+                .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab).build()).setOrder(0).build();
+        // Put our Instruction in a list of Instructions
+        return new InstructionsBuilder().setInstruction(Collections.singletonList(ib)).build();
+    }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    private void processPacket(final PacketReceived notification) {
         try {
             final byte[] rawPacket = notification.getPayload();
             final byte[] srcMac = Arrays.copyOfRange(rawPacket, 6, 12);
@@ -87,47 +179,35 @@ abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseab
             ethernetMatch.setEthernetSource(ethSourceBuilder.build());
             match.setEthernetMatch(ethernetMatch.build());
 
-            final DropActionBuilder dab = new DropActionBuilder();
-            final DropAction dropAction = dab.build();
-            final ActionBuilder ab = new ActionBuilder();
-            ab.setOrder(0);
-            ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
-
-            // Add our drop action to a list
-            final List<Action> actionList = Collections.singletonList(ab.build());
-
-            // Create an Apply Action
-            final ApplyActionsBuilder aab = new ApplyActionsBuilder();
-            aab.setAction(actionList);
-
-            // Wrap our Apply Action in an Instruction
-            final InstructionBuilder ib = new InstructionBuilder();
-            ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build()).setOrder(0);
-
-            // Put our Instruction in a list of Instructions
-            final InstructionsBuilder isb = new InstructionsBuilder();
-            final List<Instruction> instructions = Collections.singletonList(ib.build());
-            isb.setInstruction(instructions);
-
             // Get the Ingress nodeConnectorRef
             final NodeConnectorRef ncr = notification.getIngress();
 
             // Get the instance identifier for the nodeConnectorRef
             final InstanceIdentifier<?> ncri = ncr.getValue();
 
-            // Get the instanceID for the Node in the tree above us
-            final NodeKey node = ncri.firstKeyOf(Node.class, NodeKey.class);
+            processPacket(ncri.firstIdentifierOf(Node.class), match.build(), DROP_INSTRUCTIONS);
 
-            processPacket(node, match.build(), isb.build());
             SENT_UPDATER.incrementAndGet(this);
-        } catch (Exception e) {
-            // TODO Auto-generated catch block
-            LOG.error("Failed to process packet", e);
+        } catch (RuntimeException e) {
+            LOG.warn("Failed to process packet: {}", e.getMessage());
+            LOG.debug("Failed to process packet.. ", e);
             EXCS_UPDATER.incrementAndGet(this);
         }
+    }
+
+    protected abstract void processPacket(InstanceIdentifier<Node> node, Match match, Instructions instructions);
 
-        LOG.debug("onPacketReceived - Leaving", notification);
+
+    @Override
+    public void close() {
+        executorService.shutdown();
     }
 
-    protected abstract void processPacket(NodeKey node, Match match, Instructions instructions);
+    public void countFutureSuccess() {
+        RPC_FUTURE_SUCCESS_UPDATER.incrementAndGet(this);
+    }
+
+    public void countFutureError() {
+        RPC_FUTURE_FAIL_UPDATER.incrementAndGet(this);
+    }
 }