Add blueprint wiring for arp handler
[l2switch.git] / arphandler / src / main / java / org / opendaylight / l2switch / arphandler / core / ArpPacketHandler.java
1 /*
2  * Copyright (c) 2014 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.l2switch.arphandler.core;
9
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.ArpPacketListener;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.ArpPacketReceived;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.arp.packet.received.packet.chain.packet.ArpPacket;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChain;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.RawPacket;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacket;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * ArpPacketHandler listens for incoming ARP packets and processes them.
21  */
22 public class ArpPacketHandler implements ArpPacketListener {
23
24     private final static Logger LOG = LoggerFactory.getLogger(ArpPacketHandler.class);
25     private PacketDispatcher packetDispatcher;
26
27     public ArpPacketHandler(PacketDispatcher packetDispatcher) {
28         this.packetDispatcher = packetDispatcher;
29     }
30
31     /**
32      * The handler function for ARP packets.
33      *
34      * @param packetReceived
35      *            The incoming packet.
36      */
37     @Override
38     public void onArpPacketReceived(ArpPacketReceived packetReceived) {
39         if (packetReceived == null || packetReceived.getPacketChain() == null) {
40             return;
41         }
42
43         RawPacket rawPacket = null;
44         EthernetPacket ethernetPacket = null;
45         ArpPacket arpPacket = null;
46         for (PacketChain packetChain : packetReceived.getPacketChain()) {
47             if (packetChain.getPacket() instanceof RawPacket) {
48                 rawPacket = (RawPacket) packetChain.getPacket();
49             } else if (packetChain.getPacket() instanceof EthernetPacket) {
50                 ethernetPacket = (EthernetPacket) packetChain.getPacket();
51             } else if (packetChain.getPacket() instanceof ArpPacket) {
52                 arpPacket = (ArpPacket) packetChain.getPacket();
53             }
54         }
55         if (rawPacket == null || ethernetPacket == null || arpPacket == null) {
56             return;
57         }
58
59         packetDispatcher.dispatchPacket(packetReceived.getPayload(), rawPacket.getIngress(),
60                 ethernetPacket.getSourceMac(), ethernetPacket.getDestinationMac());
61     }
62
63 }