Clean up DropTest{Committer,RpcSender}
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / AbstractDropTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.testcommon;
9
10 import com.google.common.util.concurrent.ThreadFactoryBuilder;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.concurrent.ArrayBlockingQueue;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.RejectedExecutionException;
16 import java.util.concurrent.ThreadPoolExecutor;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 import org.opendaylight.mdsal.binding.api.NotificationService.Listener;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.Uint16;
42 import org.opendaylight.yangtools.yang.common.Uint32;
43 import org.opendaylight.yangtools.yang.common.Uint8;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 abstract class AbstractDropTest implements Listener<PacketReceived>, AutoCloseable {
48     private static final Logger LOG = LoggerFactory.getLogger(AbstractDropTest.class);
49
50     protected static final Uint16 PRIORITY = Uint16.valueOf(4);
51     protected static final Uint32 BUFFER_ID = Uint32.ZERO;
52     protected static final Uint16 HARD_TIMEOUT = Uint16.valueOf(300);
53     protected static final Uint16 IDLE_TIMEOUT = Uint16.valueOf(240);
54     protected static final Uint8 TABLE_ID = Uint8.ZERO;
55
56     private static final int PROCESSING_POOL_SIZE = 10000;
57
58     private static final int POOL_THREAD_AMOUNT = 8;
59     private final ExecutorService executorService;
60
61     private static final AtomicIntegerFieldUpdater<AbstractDropTest> SENT_UPDATER = AtomicIntegerFieldUpdater
62             .newUpdater(AbstractDropTest.class, "sent");
63     private volatile int sent;
64
65     private static final AtomicIntegerFieldUpdater<AbstractDropTest> RCVD_UPDATER = AtomicIntegerFieldUpdater
66             .newUpdater(AbstractDropTest.class, "rcvd");
67     private volatile int rcvd;
68
69     private static final AtomicIntegerFieldUpdater<AbstractDropTest> EXCS_UPDATER = AtomicIntegerFieldUpdater
70             .newUpdater(AbstractDropTest.class, "excs");
71     private volatile int excs;
72
73     protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_SUCCESS_UPDATER =
74             AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrSuccess");
75     protected volatile int ftrSuccess;
76
77     protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_FAIL_UPDATER =
78             AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrFailed");
79     protected volatile int ftrFailed;
80
81     protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_EXECUTED = AtomicIntegerFieldUpdater
82             .newUpdater(AbstractDropTest.class, "runablesExecuted");
83     protected volatile int runablesExecuted;
84
85     protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_REJECTED = AtomicIntegerFieldUpdater
86             .newUpdater(AbstractDropTest.class, "runablesRejected");
87     protected volatile int runablesRejected;
88
89     public final DropTestStats getStats() {
90         return new DropTestStats(sent, rcvd, excs, ftrFailed, ftrSuccess, runablesExecuted, runablesRejected);
91     }
92
93     AbstractDropTest() {
94         final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(PROCESSING_POOL_SIZE);
95         final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(POOL_THREAD_AMOUNT, POOL_THREAD_AMOUNT, 0,
96                 TimeUnit.MILLISECONDS,
97                 workQueue);
98         threadPool.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("dropTest-%d").build());
99         threadPool.setRejectedExecutionHandler((rejected, executor) -> {
100             try {
101                 workQueue.put(rejected);
102             } catch (final InterruptedException e) {
103                 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
104             }
105         });
106
107         executorService = threadPool;
108     }
109
110     public final void clearStats() {
111         sent = 0;
112         rcvd = 0;
113         excs = 0;
114         ftrSuccess = 0;
115         ftrFailed = 0;
116         runablesExecuted = 0;
117         runablesRejected = 0;
118     }
119
120     private void incrementRunableExecuted() {
121         RUNABLES_EXECUTED.incrementAndGet(this);
122     }
123
124     private void incrementRunableRejected() {
125         RUNABLES_REJECTED.incrementAndGet(this);
126     }
127
128     @Override
129     public final void onNotification(final PacketReceived notification) {
130         LOG.debug("onPacketReceived - Entering - {}", notification);
131
132         RCVD_UPDATER.incrementAndGet(this);
133
134         try {
135             executorService.execute(() -> {
136                 incrementRunableExecuted();
137                 processPacket(notification);
138             });
139         } catch (RejectedExecutionException e) {
140             incrementRunableRejected();
141         }
142         LOG.debug("onPacketReceived - {} Leaving", notification);
143     }
144
145     private static final Instructions DROP_INSTRUCTIONS = makeStaticDropActionInstructions();
146
147     private static Instructions makeStaticDropActionInstructions() {
148         // Create an DropAction
149         final DropActionCase dropAction = new DropActionCaseBuilder().setDropAction(
150                 new DropActionBuilder().build()).build();
151         // Create an Action
152         final Action ab = new ActionBuilder().setOrder(0).setAction(dropAction).build();
153         // Create an Apply Action
154         final ApplyActions aab = new ApplyActionsBuilder().setAction(Collections.singletonMap(ab.key(), ab)).build();
155         // Wrap our Apply Action in an Instruction
156         final Instruction ib = new InstructionBuilder()
157                 .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab).build()).setOrder(0).build();
158         // Put our Instruction in a list of Instructions
159         return new InstructionsBuilder().setInstruction(Collections.singletonMap(ib.key(), ib)).build();
160     }
161
162     @SuppressWarnings("checkstyle:IllegalCatch")
163     private void processPacket(final PacketReceived notification) {
164         try {
165             final byte[] rawPacket = notification.getPayload();
166             final byte[] srcMac = Arrays.copyOfRange(rawPacket, 6, 12);
167
168             final MatchBuilder match = new MatchBuilder();
169             final EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
170             final EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
171
172             //TODO: use HEX, use binary form
173             //Hex.decodeHex("000000000001".toCharArray());
174
175             ethSourceBuilder.setAddress(IetfYangUtil.INSTANCE.macAddressFor(srcMac));
176             ethernetMatch.setEthernetSource(ethSourceBuilder.build());
177             match.setEthernetMatch(ethernetMatch.build());
178
179             // Get the Ingress nodeConnectorRef
180             final NodeConnectorRef ncr = notification.getIngress();
181
182             // Get the instance identifier for the nodeConnectorRef
183             final InstanceIdentifier<?> ncri = ncr.getValue();
184
185             processPacket(ncri.firstIdentifierOf(Node.class), match.build(), DROP_INSTRUCTIONS);
186
187             SENT_UPDATER.incrementAndGet(this);
188         } catch (RuntimeException e) {
189             LOG.warn("Failed to process packet: {}", e.getMessage());
190             LOG.debug("Failed to process packet.. ", e);
191             EXCS_UPDATER.incrementAndGet(this);
192         }
193     }
194
195     protected abstract void processPacket(InstanceIdentifier<Node> node, Match match, Instructions instructions);
196
197
198     @Override
199     public void close() {
200         executorService.shutdown();
201     }
202
203     public void countFutureSuccess() {
204         RPC_FUTURE_SUCCESS_UPDATER.incrementAndGet(this);
205     }
206
207     public void countFutureError() {
208         RPC_FUTURE_FAIL_UPDATER.incrementAndGet(this);
209     }
210 }