Upgrade ietf-{inet,yang}-types to 2013-07-15
[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 static org.opendaylight.openflowjava.util.ByteBufUtils.macAddressToString;
11 import com.google.common.util.concurrent.ThreadFactoryBuilder;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.concurrent.ArrayBlockingQueue;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.RejectedExecutionException;
17 import java.util.concurrent.RejectedExecutionHandler;
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;
45
46 abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseable {
47     private static final Logger LOG = LoggerFactory.getLogger(AbstractDropTest.class);
48
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;
54
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;
58
59     private final int POOL_THREAD_AMOUNT = 8;
60     private final ExecutorService executorService;
61
62
63     private static final AtomicIntegerFieldUpdater<AbstractDropTest> SENT_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "sent");
64     private volatile int sent;
65
66     private static final AtomicIntegerFieldUpdater<AbstractDropTest> RCVD_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "rcvd");
67     private volatile int rcvd;
68
69     private static final AtomicIntegerFieldUpdater<AbstractDropTest> EXCS_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "excs");
70     private volatile int excs;
71
72     protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_SUCCESS_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrSuccess");
73     protected volatile int ftrSuccess;
74
75     protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RPC_FUTURE_FAIL_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "ftrFailed");
76     protected volatile int ftrFailed;
77
78     protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_EXECUTED = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "runablesExecuted");
79     protected volatile int runablesExecuted;
80
81     protected static final AtomicIntegerFieldUpdater<AbstractDropTest> RUNABLES_REJECTED = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "runablesRejected");
82     protected volatile int runablesRejected;
83
84     public final DropTestStats getStats() {
85         return new DropTestStats(this.sent, this.rcvd, this.excs, this.ftrFailed, this.ftrSuccess, this.runablesExecuted, this.runablesRejected);
86     }
87
88     public AbstractDropTest() {
89         final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(PROCESSING_POOL_SIZE);
90         final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(POOL_THREAD_AMOUNT, POOL_THREAD_AMOUNT, 0,
91                 TimeUnit.MILLISECONDS,
92                 workQueue);
93         threadPool.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("dropTest-%d").build());
94         threadPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
95             @Override
96             public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
97                 try {
98                     workQueue.put(r);
99                 } catch (final InterruptedException e) {
100                     throw new RejectedExecutionException("Interrupted while waiting on queue", e);
101                 }
102             }
103         });
104
105         executorService = threadPool;
106     }
107
108     public final void clearStats() {
109         this.sent = 0;
110         this.rcvd = 0;
111         this.excs = 0;
112         this.ftrSuccess = 0;
113         this.ftrFailed = 0;
114         this.runablesExecuted = 0;
115         this.runablesRejected = 0;
116     }
117
118     private final void incrementRunableExecuted() {
119         RUNABLES_EXECUTED.incrementAndGet(this);
120     }
121
122     private final void incrementRunableRejected() {
123         RUNABLES_REJECTED.incrementAndGet(this);
124     }
125
126     @Override
127     public final void onPacketReceived(final PacketReceived notification) {
128         LOG.debug("onPacketReceived - Entering - {}", notification);
129
130         RCVD_UPDATER.incrementAndGet(this);
131
132         try {
133             executorService.submit(new Runnable() {
134                 @Override
135                 public void run() {
136                     incrementRunableExecuted();
137                     processPacket(notification);
138                 }
139             });
140         } catch (final Exception e) {
141             incrementRunableRejected();
142         }
143         LOG.debug("onPacketReceived - Leaving", notification);
144     }
145
146     private static final Instructions DROP_INSTRUCTIONS = makeStaticDropActionInstructions();
147
148     private static Instructions makeStaticDropActionInstructions() {
149         // Create an DropAction
150         final DropActionCase dropAction = new DropActionCaseBuilder().setDropAction(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.singletonList(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.singletonList(ib)).build();
160     }
161
162     private void processPacket(final PacketReceived notification) {
163         try {
164             final byte[] rawPacket = notification.getPayload();
165             final byte[] srcMac = Arrays.copyOfRange(rawPacket, 6, 12);
166
167             final MatchBuilder match = new MatchBuilder();
168             final EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
169             final EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
170
171             //TODO: use HEX, use binary form
172             //Hex.decodeHex("000000000001".toCharArray());
173
174             ethSourceBuilder.setAddress(new MacAddress(macAddressToString(srcMac)));
175             ethernetMatch.setEthernetSource(ethSourceBuilder.build());
176             match.setEthernetMatch(ethernetMatch.build());
177
178             // Get the Ingress nodeConnectorRef
179             final NodeConnectorRef ncr = notification.getIngress();
180
181             // Get the instance identifier for the nodeConnectorRef
182             final InstanceIdentifier<?> ncri = ncr.getValue();
183
184             processPacket(ncri.firstIdentifierOf(Node.class), match.build(), DROP_INSTRUCTIONS);
185
186             SENT_UPDATER.incrementAndGet(this);
187         } catch (final Exception e) {
188             LOG.warn("Failed to process packet: {}", e.getMessage());
189             LOG.debug("Failed to process packet.. ", e);
190             EXCS_UPDATER.incrementAndGet(this);
191         }
192     }
193
194     protected abstract void processPacket(InstanceIdentifier<Node> node, Match match, Instructions instructions);
195
196
197     @Override
198     public void close() {
199         executorService.shutdown();
200     }
201
202     public void countFutureSuccess() {
203         RPC_FUTURE_SUCCESS_UPDATER.incrementAndGet(this);
204     }
205
206     public void countFutureError() {
207         RPC_FUTURE_FAIL_UPDATER.incrementAndGet(this);
208     }
209 }