Fix drop-test-karaf service injection
[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, DropTest {
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     @Override
90     public final DropTestStats getStats() {
91         return new DropTestStats(sent, rcvd, excs, ftrFailed, ftrSuccess, runablesExecuted, runablesRejected);
92     }
93
94     AbstractDropTest() {
95         final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(PROCESSING_POOL_SIZE);
96         final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(POOL_THREAD_AMOUNT, POOL_THREAD_AMOUNT, 0,
97                 TimeUnit.MILLISECONDS,
98                 workQueue);
99         threadPool.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("dropTest-%d").build());
100         threadPool.setRejectedExecutionHandler((rejected, executor) -> {
101             try {
102                 workQueue.put(rejected);
103             } catch (final InterruptedException e) {
104                 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
105             }
106         });
107
108         executorService = threadPool;
109     }
110
111     @Override
112     public final void clearStats() {
113         sent = 0;
114         rcvd = 0;
115         excs = 0;
116         ftrSuccess = 0;
117         ftrFailed = 0;
118         runablesExecuted = 0;
119         runablesRejected = 0;
120     }
121
122     private void incrementRunableExecuted() {
123         RUNABLES_EXECUTED.incrementAndGet(this);
124     }
125
126     private void incrementRunableRejected() {
127         RUNABLES_REJECTED.incrementAndGet(this);
128     }
129
130     @Override
131     public final void onNotification(final PacketReceived notification) {
132         LOG.debug("onPacketReceived - Entering - {}", notification);
133
134         RCVD_UPDATER.incrementAndGet(this);
135
136         try {
137             executorService.execute(() -> {
138                 incrementRunableExecuted();
139                 processPacket(notification);
140             });
141         } catch (RejectedExecutionException e) {
142             incrementRunableRejected();
143         }
144         LOG.debug("onPacketReceived - {} Leaving", notification);
145     }
146
147     private static final Instructions DROP_INSTRUCTIONS = makeStaticDropActionInstructions();
148
149     private static Instructions makeStaticDropActionInstructions() {
150         // Create an DropAction
151         final DropActionCase dropAction = new DropActionCaseBuilder().setDropAction(
152                 new DropActionBuilder().build()).build();
153         // Create an Action
154         final Action ab = new ActionBuilder().setOrder(0).setAction(dropAction).build();
155         // Create an Apply Action
156         final ApplyActions aab = new ApplyActionsBuilder().setAction(Collections.singletonMap(ab.key(), ab)).build();
157         // Wrap our Apply Action in an Instruction
158         final Instruction ib = new InstructionBuilder()
159                 .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab).build()).setOrder(0).build();
160         // Put our Instruction in a list of Instructions
161         return new InstructionsBuilder().setInstruction(Collections.singletonMap(ib.key(), ib)).build();
162     }
163
164     @SuppressWarnings("checkstyle:IllegalCatch")
165     private void processPacket(final PacketReceived notification) {
166         try {
167             final byte[] rawPacket = notification.getPayload();
168             final byte[] srcMac = Arrays.copyOfRange(rawPacket, 6, 12);
169
170             final MatchBuilder match = new MatchBuilder();
171             final EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
172             final EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
173
174             //TODO: use HEX, use binary form
175             //Hex.decodeHex("000000000001".toCharArray());
176
177             ethSourceBuilder.setAddress(IetfYangUtil.macAddressFor(srcMac));
178             ethernetMatch.setEthernetSource(ethSourceBuilder.build());
179             match.setEthernetMatch(ethernetMatch.build());
180
181             // Get the Ingress nodeConnectorRef
182             final NodeConnectorRef ncr = notification.getIngress();
183
184             // Get the instance identifier for the nodeConnectorRef
185             final InstanceIdentifier<?> ncri = ncr.getValue();
186
187             processPacket(ncri.firstIdentifierOf(Node.class), match.build(), DROP_INSTRUCTIONS);
188
189             SENT_UPDATER.incrementAndGet(this);
190         } catch (RuntimeException e) {
191             LOG.warn("Failed to process packet: {}", e.getMessage());
192             LOG.debug("Failed to process packet.. ", e);
193             EXCS_UPDATER.incrementAndGet(this);
194         }
195     }
196
197     protected abstract void processPacket(InstanceIdentifier<Node> node, Match match, Instructions instructions);
198
199
200     @Override
201     public void close() {
202         executorService.shutdown();
203     }
204
205     public void countFutureSuccess() {
206         RPC_FUTURE_SUCCESS_UPDATER.incrementAndGet(this);
207     }
208
209     public void countFutureError() {
210         RPC_FUTURE_FAIL_UPDATER.incrementAndGet(this);
211     }
212 }