Merge "TxChaingManager logging changed to warning"
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / AbstractDropTest.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  * <p/>
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
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropAction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseable {
42     private static final Logger LOG = LoggerFactory.getLogger(AbstractDropTest.class);
43
44     protected static final Integer PRIORITY = 4;
45     protected static final Long BUFFER_ID = 0L;
46     protected static final Integer HARD_TIMEOUT = 300;
47     protected static final Integer IDLE_TIMEOUT = 240;
48     protected static final short TABLE_ID = 0;
49
50     static final long STARTUP_LOOP_TICK = 500L;
51     static final int STARTUP_LOOP_MAX_RETRIES = 8;
52
53     private static final AtomicIntegerFieldUpdater<AbstractDropTest> SENT_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "sent");
54     private volatile int sent;
55
56     private static final AtomicIntegerFieldUpdater<AbstractDropTest> RCVD_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "rcvd");
57     private volatile int rcvd;
58
59     private static final AtomicIntegerFieldUpdater<AbstractDropTest> EXCS_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "excs");
60     private volatile int excs;
61
62     public final DropTestStats getStats() {
63         return new DropTestStats(this.sent, this.rcvd, this.excs);
64     }
65
66     public final void clearStats() {
67         this.sent = 0;
68         this.rcvd = 0;
69         this.excs = 0;
70     }
71
72     @Override
73     public final void onPacketReceived(final PacketReceived notification) {
74         LOG.debug("onPacketReceived - Entering - {}", notification);
75
76         RCVD_UPDATER.incrementAndGet(this);
77
78         try {
79             final byte[] rawPacket = notification.getPayload();
80             final byte[] srcMac = Arrays.copyOfRange(rawPacket, 6, 12);
81
82             final MatchBuilder match = new MatchBuilder();
83             final EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
84             final EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
85
86             //TODO: use HEX, use binary form
87             //Hex.decodeHex("000000000001".toCharArray());
88
89             ethSourceBuilder.setAddress(new MacAddress(macAddressToString(srcMac)));
90             ethernetMatch.setEthernetSource(ethSourceBuilder.build());
91             match.setEthernetMatch(ethernetMatch.build());
92
93             final DropActionBuilder dab = new DropActionBuilder();
94             final DropAction dropAction = dab.build();
95             final ActionBuilder ab = new ActionBuilder();
96             ab.setOrder(0);
97             ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
98
99             // Add our drop action to a list
100             final List<Action> actionList = Collections.singletonList(ab.build());
101
102             // Create an Apply Action
103             final ApplyActionsBuilder aab = new ApplyActionsBuilder();
104             aab.setAction(actionList);
105
106             // Wrap our Apply Action in an Instruction
107             final InstructionBuilder ib = new InstructionBuilder();
108             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build()).setOrder(0);
109
110             // Put our Instruction in a list of Instructions
111             final InstructionsBuilder isb = new InstructionsBuilder();
112             final List<Instruction> instructions = Collections.singletonList(ib.build());
113             isb.setInstruction(instructions);
114
115             // Get the Ingress nodeConnectorRef
116             final NodeConnectorRef ncr = notification.getIngress();
117
118             // Get the instance identifier for the nodeConnectorRef
119             final InstanceIdentifier<?> ncri = ncr.getValue();
120
121             // Get the instanceID for the Node in the tree above us
122             final NodeKey node = ncri.firstKeyOf(Node.class, NodeKey.class);
123
124             processPacket(node, match.build(), isb.build());
125             SENT_UPDATER.incrementAndGet(this);
126         } catch (Exception e) {
127             // TODO Auto-generated catch block
128             LOG.error("Failed to process packet", e);
129             EXCS_UPDATER.incrementAndGet(this);
130         }
131
132         LOG.debug("onPacketReceived - Leaving", notification);
133     }
134
135     protected abstract void processPacket(NodeKey node, Match match, Instructions instructions);
136 }