AddressObserver implements ArpPacketListener, Ipv4PacketListener, Ipv6PacketListener { @Override public void onArpPacketReceived(ArpPacketReceived packetReceived) { if (packetReceived == null || packetReceived.getPacketChain() == null) { return; } RawPacket rawPacket = null; EthernetPacket ethernetPacket = null; ArpPacket arpPacket = 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 ArpPacket) { arpPacket = (ArpPacket) packetChain.getPacket(); } } if (rawPacket == null || ethernetPacket == null || arpPacket == null) { return; } VlanId vlanId = null; if (ethernetPacket.getEthertype().equals(KnownEtherType.VlanTagged)) { vlanId = ethernetPacket.getHeader8021q().get(0).getVlan(); } MacAddress sourceMac = ethernetPacket.getSourceMac(); IpAddress ipAddress = null; if (arpPacket.getProtocolType().equals(KnownEtherType.Ipv4)) { ipAddress = new IpAddress(new Ipv4Address(arpPacket.getSourceProtocolAddress())); } 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 testOnArpPacketReceived() { ArpPacketReceived packetReceived = mock(ArpPacketReceived.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); ArpPacket arpPacket = mock(ArpPacket.class); List<Header8021q> headers = mock(ArrayList.class); Header8021q header = mock(Header8021q.class); headers.add(0, header); VlanId vlanId = VlanId.getDefaultInstance("4001"); MacAddress srcMac = MacAddress.getDefaultInstance("aa:bb:cc:dd:ee:ff"); 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(arpPacket); when(arpPacket.getProtocolType()).thenReturn(KnownEtherType.Ipv4); when(arpPacket.getSourceProtocolAddress()).thenReturn("192.168.20.1"); 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.onArpPacketReceived(packetReceived); verify(hostMonitor, times(1)).packetReceived(any(ConnectorAddress.class), any(InstanceIdentifier.class)); }