Migrate addresstracker notification integration
[l2switch.git] / addresstracker / implementation / src / main / java / org / opendaylight / l2switch / addresstracker / addressobserver / AddressObserverUsingIpv6.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.mdsal.binding.api.NotificationService.Listener;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChain;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.RawPacket;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.raw.packet.RawPacketFields;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacket;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv6.rev140528.Ipv6PacketReceived;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv6.rev140528.ipv6.packet.received.packet.chain.packet.Ipv6Packet;
18
19 /**
20  * AddressObserver listens to IPv6 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 AddressObserverUsingIpv6 implements Listener<Ipv6PacketReceived> {
25     private static final String IPV6_IP_TO_IGNORE = "0:0:0:0:0:0:0:0";
26
27     private final AddressObservationWriter addressObservationWriter;
28
29     public AddressObserverUsingIpv6(AddressObservationWriter addressObservationWriter) {
30         this.addressObservationWriter = addressObservationWriter;
31     }
32
33     /**
34      * The handler function for IPv6 packets.
35      *
36      * @param packetReceived
37      *            The incoming packet.
38      */
39     @Override
40     public void onNotification(Ipv6PacketReceived packetReceived) {
41         if (packetReceived == null || packetReceived.getPacketChain() == null) {
42             return;
43         }
44
45         RawPacketFields rawPacket = null;
46         EthernetPacket ethernetPacket = null;
47         Ipv6Packet ipv6Packet = null;
48         for (PacketChain packetChain : packetReceived.getPacketChain()) {
49             if (packetChain.getPacket() instanceof RawPacket) {
50                 rawPacket = ((RawPacket) packetChain.getPacket()).getRawPacketFields();
51             } else if (packetChain.getPacket() instanceof EthernetPacket) {
52                 ethernetPacket = (EthernetPacket) packetChain.getPacket();
53             } else if (packetChain.getPacket() instanceof Ipv6Packet) {
54                 ipv6Packet = (Ipv6Packet) packetChain.getPacket();
55             }
56         }
57         if (rawPacket == null || ethernetPacket == null || ipv6Packet == null) {
58             return;
59         }
60
61         if (!IPV6_IP_TO_IGNORE.equals(ipv6Packet.getSourceIpv6().getValue())) {
62             addressObservationWriter.addAddress(ethernetPacket.getSourceMac(),
63                 IetfInetUtil.ipAddressFor(ipv6Packet.getSourceIpv6().getValue()), rawPacket.getIngress());
64         }
65     }
66 }