Fix drop-test-karaf service injection
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / AbstractDropTest.java
index 2f48d20eb143816f43c327e029c9a8f398aa30cc..0b00f2e782300b68e2773636abadcecbdf7fc343 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,18 +7,17 @@
  */
 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.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.RejectedExecutionException;
-import java.util.concurrent.RejectedExecutionHandler;
 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.mdsal.binding.api.NotificationService.Listener;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
 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.DropActionBuilder;
@@ -37,128 +36,132 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeCon
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseable {
+abstract class AbstractDropTest implements Listener<PacketReceived>, AutoCloseable, DropTest {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractDropTest.class);
 
-    protected static final Integer PRIORITY = 4;
-    protected static final Long BUFFER_ID = 0L;
-    protected static final Integer HARD_TIMEOUT = 300;
-    protected static final Integer IDLE_TIMEOUT = 240;
-    protected static final short TABLE_ID = 0;
+    protected static final Uint16 PRIORITY = Uint16.valueOf(4);
+    protected static final Uint32 BUFFER_ID = Uint32.ZERO;
+    protected static final Uint16 HARD_TIMEOUT = Uint16.valueOf(300);
+    protected static final Uint16 IDLE_TIMEOUT = Uint16.valueOf(240);
+    protected static final Uint8 TABLE_ID = Uint8.ZERO;
 
-    static final long STARTUP_LOOP_TICK = 500L;
-    static final int STARTUP_LOOP_MAX_RETRIES = 8;
     private static final int PROCESSING_POOL_SIZE = 10000;
 
-    private final int POOL_THREAD_AMOUNT = 8;
+    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 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 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 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 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 static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_REJECTED = AtomicIntegerFieldUpdater
+            .newUpdater(AbstractDropTest.class, "runablesRejected");
     protected volatile int runablesRejected;
 
+    @Override
     public final DropTestStats getStats() {
-        return new DropTestStats(this.sent, this.rcvd, this.excs, this.ftrFailed, this.ftrSuccess, this.runablesExecuted, this.runablesRejected);
+        return new DropTestStats(sent, rcvd, excs, ftrFailed, ftrSuccess, runablesExecuted, runablesRejected);
     }
 
-    public AbstractDropTest() {
-        final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(PROCESSING_POOL_SIZE);
+    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(new RejectedExecutionHandler() {
-            @Override
-            public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
-                try {
-                    workQueue.put(r);
-                } catch (final InterruptedException e) {
-                    throw new RejectedExecutionException("Interrupted while waiting on queue", e);
-                }
+        threadPool.setRejectedExecutionHandler((rejected, executor) -> {
+            try {
+                workQueue.put(rejected);
+            } catch (final InterruptedException e) {
+                throw new RejectedExecutionException("Interrupted while waiting on queue", e);
             }
         });
 
         executorService = threadPool;
     }
 
+    @Override
     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;
+        sent = 0;
+        rcvd = 0;
+        excs = 0;
+        ftrSuccess = 0;
+        ftrFailed = 0;
+        runablesExecuted = 0;
+        runablesRejected = 0;
     }
 
-    private final void incrementRunableExecuted() {
+    private void incrementRunableExecuted() {
         RUNABLES_EXECUTED.incrementAndGet(this);
     }
 
-    private final void incrementRunableRejected() {
+    private void incrementRunableRejected() {
         RUNABLES_REJECTED.incrementAndGet(this);
     }
 
     @Override
-    public final void onPacketReceived(final PacketReceived notification) {
+    public final void onNotification(final PacketReceived notification) {
         LOG.debug("onPacketReceived - Entering - {}", notification);
 
         RCVD_UPDATER.incrementAndGet(this);
 
         try {
-            executorService.submit(new Runnable() {
-                @Override
-                public void run() {
-                    incrementRunableExecuted();
-                    processPacket(notification);
-                }
+            executorService.execute(() -> {
+                incrementRunableExecuted();
+                processPacket(notification);
             });
-        } catch (final Exception e) {
+        } catch (RejectedExecutionException e) {
             incrementRunableRejected();
         }
-        LOG.debug("onPacketReceived - Leaving", notification);
+        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();
+        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();
+        final ApplyActions aab = new ApplyActionsBuilder().setAction(Collections.singletonMap(ab.key(), 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();
+        return new InstructionsBuilder().setInstruction(Collections.singletonMap(ib.key(), ib)).build();
     }
 
+    @SuppressWarnings("checkstyle:IllegalCatch")
     private void processPacket(final PacketReceived notification) {
         try {
             final byte[] rawPacket = notification.getPayload();
@@ -171,7 +174,7 @@ abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseab
             //TODO: use HEX, use binary form
             //Hex.decodeHex("000000000001".toCharArray());
 
-            ethSourceBuilder.setAddress(new MacAddress(macAddressToString(srcMac)));
+            ethSourceBuilder.setAddress(IetfYangUtil.macAddressFor(srcMac));
             ethernetMatch.setEthernetSource(ethSourceBuilder.build());
             match.setEthernetMatch(ethernetMatch.build());
 
@@ -184,7 +187,7 @@ abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseab
             processPacket(ncri.firstIdentifierOf(Node.class), match.build(), DROP_INSTRUCTIONS);
 
             SENT_UPDATER.incrementAndGet(this);
-        } catch (final Exception e) {
+        } catch (RuntimeException e) {
             LOG.warn("Failed to process packet: {}", e.getMessage());
             LOG.debug("Failed to process packet.. ", e);
             EXCS_UPDATER.incrementAndGet(this);