Fix checkstyle violations
[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
17 /**
18  * ArpPacketHandler listens for incoming ARP packets and processes them.
19  */
20 public class ArpPacketHandler implements ArpPacketListener {
21
22     private final PacketDispatcher packetDispatcher;
23
24     public ArpPacketHandler(PacketDispatcher packetDispatcher) {
25         this.packetDispatcher = packetDispatcher;
26     }
27
28     /**
29      * The handler function for ARP packets.
30      *
31      * @param packetReceived
32      *            The incoming packet.
33      */
34     @Override
35     public void onArpPacketReceived(ArpPacketReceived packetReceived) {
36         if (packetReceived == null || packetReceived.getPacketChain() == null) {
37             return;
38         }
39
40         RawPacket rawPacket = null;
41         EthernetPacket ethernetPacket = null;
42         ArpPacket arpPacket = null;
43         for (PacketChain packetChain : packetReceived.getPacketChain()) {
44             if (packetChain.getPacket() instanceof RawPacket) {
45                 rawPacket = (RawPacket) packetChain.getPacket();
46             } else if (packetChain.getPacket() instanceof EthernetPacket) {
47                 ethernetPacket = (EthernetPacket) packetChain.getPacket();
48             } else if (packetChain.getPacket() instanceof ArpPacket) {
49                 arpPacket = (ArpPacket) packetChain.getPacket();
50             }
51         }
52         if (rawPacket == null || ethernetPacket == null || arpPacket == null) {
53             return;
54         }
55
56         packetDispatcher.dispatchPacket(packetReceived.getPayload(), rawPacket.getIngress(),
57                 ethernetPacket.getSourceMac(), ethernetPacket.getDestinationMac());
58     }
59
60 }