AddressObserver implements ArpPacketListener, Ipv4PacketListener, Ipv6PacketListener { @Override public void onIpv6PacketReceived(Ipv6PacketReceived packetReceived) { if (packetReceived == null || packetReceived.getPacketChain() == null) { return; } RawPacket rawPacket = null; EthernetPacket ethernetPacket = null; Ipv6Packet ipv6Packet = null; for (PacketChain packetChain : packetReceived.getPacketChain()) { if (packetChain.getPacket() instanceof RawPacket) { rawPacket = (RawPacket) packetChain.getPacket(); } else if (packetChain.getPacket() instanceof EthernetPacket) { ethernetPacket = (EthernetPacket) packetChain.getPacket(); } else if (packetChain.getPacket() instanceof Ipv6Packet) { ipv6Packet = (Ipv6Packet) packetChain.getPacket(); } } if (rawPacket == null || ethernetPacket == null || ipv6Packet == null) { return; } if (IPV6_IP_TO_IGNORE.equals(ipv6Packet.getSourceIpv6().getValue())) { return; } VlanId vlanId = null; if (ethernetPacket.getEthertype().equals(KnownEtherType.VlanTagged)) { vlanId = ethernetPacket.getHeader8021q().get(0).getVlan(); } MacAddress sourceMac = ethernetPacket.getSourceMac(); IpAddress ipAddress = new IpAddress(ipv6Packet.getSourceIpv6()); ConnectorAddress addrs = createAddresses(sourceMac, vlanId, ipAddress, ethernetPacket.getEthertype()); if (addrs == null) { return; } NodeConnectorRef ingress = rawPacket.getIngress(); hostMonitor.packetReceived(addrs, ingress.getValue()); } AddressObserver(HostMonitor hostTrackerImpl, NotificationService notificationService); @Override void onArpPacketReceived(ArpPacketReceived packetReceived); @Override void onIpv4PacketReceived(Ipv4PacketReceived packetReceived); @Override void onIpv6PacketReceived(Ipv6PacketReceived packetReceived);  }
@Test public void testOnIpv6PacketReceived() { Ipv6PacketReceived packetReceived = mock(Ipv6PacketReceived.class); List<PacketChain> packetChains = new ArrayList<PacketChain>(); PacketChain packetChain1 = mock(PacketChain.class); PacketChain packetChain2 = mock(PacketChain.class); PacketChain packetChain3 = mock(PacketChain.class); packetChains.add(packetChain1); packetChains.add(packetChain2); packetChains.add(packetChain3); RawPacket rawPacket = mock(RawPacket.class); EthernetPacket ethernetPacket = mock(EthernetPacket.class); Ipv6Packet ipv6Packet = mock(Ipv6Packet.class); Ipv6Address ipv6Address = Ipv6Address.getDefaultInstance("2000::1"); MacAddress srcMac = MacAddress.getDefaultInstance("aa:bb:cc:dd:ee:f0"); List<Header8021q> headers = mock(ArrayList.class); Header8021q header = mock(Header8021q.class); headers.add(0, header); VlanId vlanId = VlanId.getDefaultInstance("3001"); InstanceIdentifier<NodeConnector> ncRefInstance = mock(InstanceIdentifier.class); NodeConnectorRef ncRef = new NodeConnectorRef(ncRefInstance); when(packetReceived.getPacketChain()).thenReturn(packetChains); when(packetChain1.getPacket()).thenReturn(rawPacket); when(packetChain2.getPacket()).thenReturn(ethernetPacket); when(packetChain3.getPacket()).thenReturn(ipv6Packet); when(ipv6Packet.getSourceIpv6()).thenReturn(ipv6Address); when(ethernetPacket.getEthertype()).thenReturn(KnownEtherType.VlanTagged); when(ethernetPacket.getHeader8021q()).thenReturn(headers); when(headers.get(anyInt())).thenReturn(header); when(header.getVlan()).thenReturn(vlanId); when(ethernetPacket.getSourceMac()).thenReturn(srcMac); when(rawPacket.getIngress()).thenReturn(ncRef); addressObserver.onIpv6PacketReceived(packetReceived); verify(hostMonitor, times(1)).packetReceived(any(ConnectorAddress.class), any(InstanceIdentifier.class)); }