2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.testcommon;
10 import static org.opendaylight.openflowjava.util.ByteBufUtils.macAddressToString;
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;
41 abstract class AbstractDropTest implements PacketProcessingListener, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(AbstractDropTest.class);
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;
50 private static final AtomicIntegerFieldUpdater<AbstractDropTest> SENT_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "sent");
51 private volatile int sent;
53 private static final AtomicIntegerFieldUpdater<AbstractDropTest> RCVD_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "rcvd");
54 private volatile int rcvd;
56 private static final AtomicIntegerFieldUpdater<AbstractDropTest> EXCS_UPDATER = AtomicIntegerFieldUpdater.newUpdater(AbstractDropTest.class, "excs");
57 private volatile int excs;
59 public final DropTestStats getStats() {
60 return new DropTestStats(this.sent, this.rcvd, this.excs);
63 public final void clearStats(){
70 public final void onPacketReceived(final PacketReceived notification) {
71 LOG.debug("onPacketReceived - Entering - {}", notification);
73 RCVD_UPDATER.incrementAndGet(this);
76 final byte[] rawPacket = notification.getPayload();
77 final byte[] srcMac = Arrays.copyOfRange(rawPacket, 6, 12);
79 final MatchBuilder match = new MatchBuilder();
80 final EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
81 final EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
83 //TODO: use HEX, use binary form
84 //Hex.decodeHex("000000000001".toCharArray());
86 ethSourceBuilder.setAddress(new MacAddress(macAddressToString(srcMac)));
87 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
88 match.setEthernetMatch(ethernetMatch.build());
90 final DropActionBuilder dab = new DropActionBuilder();
91 final DropAction dropAction = dab.build();
92 final ActionBuilder ab = new ActionBuilder();
94 ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
96 // Add our drop action to a list
97 final List<Action> actionList = Collections.singletonList(ab.build());
99 // Create an Apply Action
100 final ApplyActionsBuilder aab = new ApplyActionsBuilder();
101 aab.setAction(actionList);
103 // Wrap our Apply Action in an Instruction
104 final InstructionBuilder ib = new InstructionBuilder();
105 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build()).setOrder(0);
107 // Put our Instruction in a list of Instructions
108 final InstructionsBuilder isb = new InstructionsBuilder();
109 final List<Instruction> instructions = Collections.singletonList(ib.build());
110 isb.setInstruction(instructions);
112 // Get the Ingress nodeConnectorRef
113 final NodeConnectorRef ncr = notification.getIngress();
115 // Get the instance identifier for the nodeConnectorRef
116 final InstanceIdentifier<?> ncri = ncr.getValue();
118 // Get the instanceID for the Node in the tree above us
119 final NodeKey node = ncri.firstKeyOf(Node.class, NodeKey.class);
121 processPacket(node, match.build(), isb.build());
122 SENT_UPDATER.incrementAndGet(this);
123 } catch (Exception e) {
124 // TODO Auto-generated catch block
125 LOG.error("Failed to process packet", e);
126 EXCS_UPDATER.incrementAndGet(this);
129 LOG.debug("onPacketReceived - Leaving", notification);
132 protected abstract void processPacket(NodeKey node, Match match, Instructions instructions);