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.l2switch.addresstracker.addressobserver;
10 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
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.basepacket.rev140528.packet.chain.grp.packet.chain.packet.raw.packet.RawPacketFields;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacket;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.Ipv4PacketListener;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.Ipv4PacketReceived;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.ipv4.packet.received.packet.chain.packet.Ipv4Packet;
21 * AddressObserver listens to IPv4 packets to find addresses (mac, ip) and store
22 * these address observations for each node-connector. These packets are
23 * returned to the network after the addresses are learned.
25 public class AddressObserverUsingIpv4 implements Ipv4PacketListener {
26 private static final String IPV4_IP_TO_IGNORE = "0.0.0.0";
28 private final AddressObservationWriter addressObservationWriter;
30 public AddressObserverUsingIpv4(AddressObservationWriter addressObservationWriter) {
31 this.addressObservationWriter = addressObservationWriter;
35 * The handler function for IPv4 packets.
37 * @param packetReceived
38 * The incoming packet.
41 public void onIpv4PacketReceived(Ipv4PacketReceived packetReceived) {
42 if (packetReceived == null || packetReceived.getPacketChain() == null) {
46 RawPacketFields rawPacket = null;
47 EthernetPacket ethernetPacket = null;
48 Ipv4Packet ipv4Packet = null;
49 for (PacketChain packetChain : packetReceived.getPacketChain()) {
50 if (packetChain.getPacket() instanceof RawPacket) {
51 rawPacket = ((RawPacket) packetChain.getPacket()).getRawPacketFields();
52 } else if (packetChain.getPacket() instanceof EthernetPacket) {
53 ethernetPacket = (EthernetPacket) packetChain.getPacket();
54 } else if (packetChain.getPacket() instanceof Ipv4Packet) {
55 ipv4Packet = (Ipv4Packet) packetChain.getPacket();
58 if (rawPacket == null || ethernetPacket == null || ipv4Packet == null) {
62 if (!IPV4_IP_TO_IGNORE.equals(ipv4Packet.getSourceIpv4().getValue())) {
63 addressObservationWriter.addAddress(ethernetPacket.getSourceMac(),
64 IetfInetUtil.ipAddressFor(ipv4Packet.getSourceIpv4().getValue()), rawPacket.getIngress());