Update to work on Sodium SR1
[l2switch.git] / addresstracker / implementation / src / main / java / org / opendaylight / l2switch / addresstracker / addressobserver / AddressObserverUsingIpv4.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.addresstracker.addressobserver;
9
10 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChain;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.RawPacket;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacket;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.Ipv4PacketListener;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.Ipv4PacketReceived;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.ipv4.packet.received.packet.chain.packet.Ipv4Packet;
17
18
19 /**
20  * AddressObserver listens to IPv4 packets to find addresses (mac, ip) and store
21  * these address observations for each node-connector. These packets are
22  * returned to the network after the addresses are learned.
23  */
24 public class AddressObserverUsingIpv4 implements Ipv4PacketListener {
25     private static final String IPV4_IP_TO_IGNORE = "0.0.0.0";
26
27     private final AddressObservationWriter addressObservationWriter;
28
29     public AddressObserverUsingIpv4(AddressObservationWriter addressObservationWriter) {
30         this.addressObservationWriter = addressObservationWriter;
31     }
32
33     /**
34      * The handler function for IPv4 packets.
35      *
36      * @param packetReceived
37      *            The incoming packet.
38      */
39     @Override
40     public void onIpv4PacketReceived(Ipv4PacketReceived packetReceived) {
41         if (packetReceived == null || packetReceived.getPacketChain() == null) {
42             return;
43         }
44
45         RawPacket rawPacket = null;
46         EthernetPacket ethernetPacket = null;
47         Ipv4Packet ipv4Packet = null;
48         for (PacketChain packetChain : packetReceived.getPacketChain()) {
49             if (packetChain.getPacket() instanceof RawPacket) {
50                 rawPacket = (RawPacket) packetChain.getPacket();
51             } else if (packetChain.getPacket() instanceof EthernetPacket) {
52                 ethernetPacket = (EthernetPacket) packetChain.getPacket();
53             } else if (packetChain.getPacket() instanceof Ipv4Packet) {
54                 ipv4Packet = (Ipv4Packet) packetChain.getPacket();
55             }
56         }
57         if (rawPacket == null || ethernetPacket == null || ipv4Packet == null) {
58             return;
59         }
60
61         if (!IPV4_IP_TO_IGNORE.equals(ipv4Packet.getSourceIpv4().getValue())) {
62             addressObservationWriter.addAddress(ethernetPacket.getSourceMac(),
63                     IpAddressBuilder.getDefaultInstance(ipv4Packet.getSourceIpv4().getValue()), rawPacket.getIngress());
64         }
65     }
66 }