TunnellingConnectivityManager implements Ipv4PacketListener { @Override public void onIpv4PacketReceived(Ipv4PacketReceived packetReceived) { if (packetReceived == null || packetReceived.getPacketChain() == null) { return; } RawPacket rawPacket = null; EthernetPacket ethernetPacket = null; Ipv4Packet ipv4Packet = 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 Ipv4Packet) { ipv4Packet = (Ipv4Packet) packetChain.getPacket(); } } if (rawPacket == null || ethernetPacket == null || ipv4Packet == null) { LOG.info("Ipv4 somehting null"); return; } List<Header8021q> list = ethernetPacket.getHeader8021q(); int offset = TCP_OFFSET; if (list != null && !list.isEmpty()) { offset = TCP_OFFSET + 4; } byte[] payload = packetReceived.getPayload(); try { AtriumTCPHeader header = AtriumTCPHeader.decodeTCPHeader(payload, offset); if (header.getSourcePort() == BGP_PORT || header.getDestinationPort() == BGP_PORT || ipv4Packet.getProtocol() == KnownIpProtocols.Icmp) { NodeConnectorRef ingressNCRef = rawPacket.getIngress(); NodeConnector ingressNC = null; ingressNC = AtriumUtils.getNodeConnector(dataBroker, ingressNCRef); if (ingressNC == null) { return; } NodeId ingressNodeId = AtriumUtils.getNodeIdFromNodeConnectorId(ingressNC.getId()); String portNum = AtriumUtils.getPortNoFromNodeConnectorId(ingressNC.getId()); Long ingressPort = Long.valueOf(portNum); Ipv4Address dstAddress = ipv4Packet.getDestinationIpv4(); NodeConnectorRef egressNodeConnectorRef = null; if (ingressNodeId.equals(bgpSpeaker.getAttachmentDpId()) && ingressPort.equals(bgpSpeaker.getAttachmentPort())) { BgpPeer bgpPeer = configService.getBgpPeerByIpAddress(new IpAddress((dstAddress))); if(bgpPeer != null) { egressNodeConnectorRef = AtriumUtils.getNodeConnRef(bgpPeer.getPeerDpId(), bgpPeer.getPeerPort()); } } for (InterfaceAddresses addr : bgpSpeaker.getInterfaceAddresses()) { InterfaceAddressesBuilder builder = new InterfaceAddressesBuilder(addr); if (builder.getIpAddress().equals(new IpAddress(dstAddress))) { egressNodeConnectorRef = AtriumUtils.getNodeConnRef(bgpSpeaker.getAttachmentDpId(), bgpSpeaker.getAttachmentPort()); } } sendPacketOut(payload, egressNodeConnectorRef); } } catch (AtriumTCPDecodeException e) { e.printStackTrace(); } } TunnellingConnectivityManager(DataBroker dataBroker, RoutingConfigService configService,
			PacketProcessingService packetService, AtriumFlowObjectiveService flowObjectives); void start(); void stop(); void notifySwitchAvailable(); @Override void onIpv4PacketReceived(Ipv4PacketReceived packetReceived);  }
@Test public void testOnIpv4PacketReceived() throws InterruptedException, ExecutionException { Ipv4PacketReceived packetReceived = mock(Ipv4PacketReceived.class); List<PacketChain> packetChains = new ArrayList<PacketChain>(); PacketChain packetChain1 = mock(PacketChain.class); PacketChain packetChain2 = mock(PacketChain.class); PacketChain packetChain3 = mock(PacketChain.class); RawPacket rawPacket = mock(RawPacket.class); EthernetPacket ethernetPacket = mock(EthernetPacket.class); Ipv4Packet ipv4Packet = mock(Ipv4Packet.class); packetChains.add(packetChain1); packetChains.add(packetChain2); packetChains.add(packetChain3); List<Header8021q> headers = mock(ArrayList.class); byte[] payload = new byte[100]; for (byte index = 1; payload.length <= index; index++) { payload[index] = index; } NodeConnectorRef ncRef = mock(NodeConnectorRef.class); ReadOnlyTransaction readTx = mock(ReadOnlyTransaction.class); CheckedFuture<Optional<NodeConnector>, ReadFailedException> checkedNodes = mock(CheckedFuture.class); Optional<NodeConnector> nc = mock(Optional.class); NodeConnector nodeConnector = mock(NodeConnector.class); NodeConnectorId ncId = new NodeConnectorId("openflow:20:17"); Ipv4Address destIpAddress = new Ipv4Address("192.168.10.1"); BgpPeer bgpPeer = mock(BgpPeer.class); Future<RpcResult<Void>> future = mock(Future.class); when(packetReceived.getPacketChain()).thenReturn(packetChains); when(packetChain1.getPacket()).thenReturn(rawPacket); when(packetChain2.getPacket()).thenReturn(ethernetPacket); when(packetChain3.getPacket()).thenReturn(ipv4Packet); when(ipv4Packet.getProtocol()).thenReturn(KnownIpProtocols.Icmp); when(ethernetPacket.getHeader8021q()).thenReturn(headers); when(packetReceived.getPayload()).thenReturn(payload); when(rawPacket.getIngress()).thenReturn(ncRef); when(dataBroker.newReadOnlyTransaction()).thenReturn(readTx); when(readTx.read(any(LogicalDatastoreType.class), (InstanceIdentifier<NodeConnector>) any(InstanceIdentifier.class))).thenReturn(checkedNodes); when(checkedNodes.get()).thenReturn(nc); when(nc.isPresent()).thenReturn(Boolean.valueOf(true)); when(nc.get()).thenReturn(nodeConnector); when(nodeConnector.getId()).thenReturn(ncId); when(ipv4Packet.getDestinationIpv4()).thenReturn(destIpAddress); when(routingConfigService.getBgpPeerByIpAddress(any(IpAddress.class))).thenReturn(bgpPeer); when(bgpPeer.getPeerDpId()).thenReturn(NodeId.getDefaultInstance("21")); when(bgpPeer.getPeerPort()).thenReturn(Long.valueOf(18)); when(packetService.transmitPacket(any(TransmitPacketInput.class))).thenReturn(future); connectivityManager.onIpv4PacketReceived(packetReceived); verify(packetService, times(1)).transmitPacket(any(TransmitPacketInput.class)); }