ONOS-6887 Move neighbour classes from incubator to core
Change-Id: I5fa70253b833838566a3527d8938e04be4274210
diff --git a/core/net/src/main/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageActions.java b/core/net/src/main/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageActions.java
new file mode 100644
index 0000000..ed78212
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageActions.java
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.neighbour.impl;
+
+import org.onlab.packet.ARP;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.ICMP6;
+import org.onlab.packet.IPv6;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip6Address;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onlab.packet.ndp.NeighborAdvertisement;
+import org.onlab.packet.ndp.NeighborDiscoveryOptions;
+import org.onlab.util.Tools;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.neighbour.NeighbourMessageActions;
+import org.onosproject.net.neighbour.NeighbourMessageContext;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.edge.EdgePortService;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.packet.DefaultOutboundPacket;
+import org.onosproject.net.packet.PacketService;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Implementation of neighbour message actions.
+ */
+public class DefaultNeighbourMessageActions implements NeighbourMessageActions {
+
+ private final EdgePortService edgeService;
+ private final PacketService packetService;
+
+ public DefaultNeighbourMessageActions(PacketService packetService,
+ EdgePortService edgeService) {
+ this.packetService = packetService;
+ this.edgeService = edgeService;
+ }
+
+ @Override
+ public void reply(NeighbourMessageContext context, MacAddress targetMac) {
+ replyInternal(context, targetMac);
+ }
+
+ @Override
+ public void forward(NeighbourMessageContext context, ConnectPoint outPort) {
+ sendTo(context.packet(), outPort);
+ }
+
+ @Override
+ public void forward(NeighbourMessageContext context, Interface outIntf) {
+ Ethernet packetOut = (Ethernet) context.packet().clone();
+ if (outIntf.vlan().equals(VlanId.NONE)) {
+ // The egress interface has no VLAN Id. Send out an untagged
+ // packet
+ packetOut.setVlanID(Ethernet.VLAN_UNTAGGED);
+ } else {
+ // The egress interface has a VLAN set. Send out a tagged packet
+ packetOut.setVlanID(outIntf.vlan().toShort());
+ }
+ sendTo(packetOut, outIntf.connectPoint());
+ }
+
+ @Override
+ public void flood(NeighbourMessageContext context) {
+ Tools.stream(edgeService.getEdgePoints())
+ .filter(cp -> !cp.equals(context.inPort()))
+ .forEach(cp -> sendTo(context.packet(), cp));
+ }
+
+ @Override
+ public void drop(NeighbourMessageContext context) {
+
+ }
+
+ private void replyInternal(NeighbourMessageContext context, MacAddress targetMac) {
+ switch (context.protocol()) {
+ case ARP:
+ sendTo(ARP.buildArpReply((Ip4Address) context.target(),
+ targetMac, context.packet()), context.inPort());
+ break;
+ case NDP:
+ sendTo(buildNdpReply((Ip6Address) context.target(), targetMac,
+ context.packet()), context.inPort());
+ break;
+ default:
+ break;
+ }
+ }
+
+ /**
+ * Outputs a packet out a specific port.
+ *
+ * @param packet the packet to send
+ * @param outPort the port to send it out
+ */
+ private void sendTo(Ethernet packet, ConnectPoint outPort) {
+ sendTo(ByteBuffer.wrap(packet.serialize()), outPort);
+ }
+
+ /**
+ * Outputs a packet out a specific port.
+ *
+ * @param packet packet to send
+ * @param outPort port to send it out
+ */
+ private void sendTo(ByteBuffer packet, ConnectPoint outPort) {
+ if (!edgeService.isEdgePoint(outPort)) {
+ // Sanity check to make sure we don't send the packet out an
+ // internal port and create a loop (could happen due to
+ // misconfiguration).
+ return;
+ }
+
+ TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
+ builder.setOutput(outPort.port());
+ packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
+ builder.build(), packet));
+ }
+
+ /**
+ * Builds an NDP reply based on a request.
+ *
+ * @param srcIp the IP address to use as the reply source
+ * @param srcMac the MAC address to use as the reply source
+ * @param request the Neighbor Solicitation request we got
+ * @return an Ethernet frame containing the Neighbor Advertisement reply
+ */
+ private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac,
+ Ethernet request) {
+ Ethernet eth = new Ethernet();
+ eth.setDestinationMACAddress(request.getSourceMAC());
+ eth.setSourceMACAddress(srcMac);
+ eth.setEtherType(Ethernet.TYPE_IPV6);
+ eth.setVlanID(request.getVlanID());
+
+ IPv6 requestIp = (IPv6) request.getPayload();
+ IPv6 ipv6 = new IPv6();
+ ipv6.setSourceAddress(srcIp.toOctets());
+ ipv6.setDestinationAddress(requestIp.getSourceAddress());
+ ipv6.setHopLimit((byte) 255);
+
+ ICMP6 icmp6 = new ICMP6();
+ icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
+ icmp6.setIcmpCode((byte) 0);
+
+ NeighborAdvertisement nadv = new NeighborAdvertisement();
+ nadv.setTargetAddress(srcIp.toOctets());
+ nadv.setSolicitedFlag((byte) 1);
+ nadv.setOverrideFlag((byte) 1);
+ nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ srcMac.toBytes());
+
+ icmp6.setPayload(nadv);
+ ipv6.setPayload(icmp6);
+ eth.setPayload(ipv6);
+ return eth;
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageContext.java b/core/net/src/main/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageContext.java
new file mode 100644
index 0000000..2e013af
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageContext.java
@@ -0,0 +1,276 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.neighbour.impl;
+
+import java.util.Objects;
+
+import org.onlab.packet.ARP;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.ICMP6;
+import org.onlab.packet.IPv6;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip6Address;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onlab.packet.ndp.NeighborSolicitation;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.neighbour.NeighbourMessageActions;
+import org.onosproject.net.neighbour.NeighbourMessageContext;
+import org.onosproject.net.neighbour.NeighbourMessageType;
+import org.onosproject.net.neighbour.NeighbourProtocol;
+
+import static com.google.common.base.Preconditions.checkState;
+
+/**
+ * Default implementation of a neighbour message context.
+ */
+
+public class DefaultNeighbourMessageContext implements NeighbourMessageContext {
+
+ private final NeighbourProtocol protocol;
+ private final NeighbourMessageType type;
+
+ private final IpAddress target;
+ private final IpAddress sender;
+
+ private final Ethernet eth;
+ private final ConnectPoint inPort;
+
+ private final NeighbourMessageActions actions;
+
+ /**
+ * Creates a new neighbour message context.
+ *
+ * @param actions actions
+ * @param eth ethernet frame
+ * @param inPort incoming port
+ * @param protocol message protocol
+ * @param type message type
+ * @param target target IP address
+ * @param sender sender IP address
+ */
+ DefaultNeighbourMessageContext(NeighbourMessageActions actions,
+ Ethernet eth, ConnectPoint inPort,
+ NeighbourProtocol protocol,
+ NeighbourMessageType type,
+ IpAddress target, IpAddress sender) {
+ this.actions = actions;
+ this.eth = eth;
+ this.inPort = inPort;
+ this.protocol = protocol;
+ this.type = type;
+ this.target = target;
+ this.sender = sender;
+ }
+
+ @Override
+ public ConnectPoint inPort() {
+ return inPort;
+ }
+
+ @Override
+ public Ethernet packet() {
+ return eth;
+ }
+
+ @Override
+ public NeighbourProtocol protocol() {
+ return protocol;
+ }
+
+ @Override
+ public NeighbourMessageType type() {
+ return type;
+ }
+
+ @Override
+ public VlanId vlan() {
+ return VlanId.vlanId(eth.getVlanID());
+ }
+
+ @Override
+ public MacAddress srcMac() {
+ return eth.getSourceMAC();
+ }
+
+ @Override
+ public MacAddress dstMac() {
+ return eth.getDestinationMAC();
+ }
+
+ @Override
+ public IpAddress target() {
+ return target;
+ }
+
+ @Override
+ public IpAddress sender() {
+ return sender;
+ }
+
+ @Override
+ public void forward(ConnectPoint outPort) {
+ actions.forward(this, outPort);
+ }
+
+ @Override
+ public void forward(Interface outIntf) {
+ actions.forward(this, outIntf);
+ }
+
+ @Override
+ public void reply(MacAddress targetMac) {
+ checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests");
+
+ actions.reply(this, targetMac);
+ }
+
+ @Override
+ public void flood() {
+ actions.flood(this);
+ }
+
+ @Override
+ public void drop() {
+ actions.drop(this);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(protocol, type, target, sender, eth, inPort);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof DefaultNeighbourMessageContext)) {
+ return false;
+ }
+
+ DefaultNeighbourMessageContext that = (DefaultNeighbourMessageContext) obj;
+
+ return Objects.equals(protocol, that.protocol) &&
+ Objects.equals(type, that.type) &&
+ Objects.equals(target, that.target) &&
+ Objects.equals(sender, that.sender) &&
+ Objects.equals(eth, that.eth) &&
+ Objects.equals(inPort, that.inPort);
+ }
+
+ /**
+ * Attempts to create a MessageContext for the given Ethernet frame. If the
+ * frame is a valid ARP or NDP request or response, a context will be
+ * created.
+ *
+ * @param eth input Ethernet frame
+ * @param inPort in port
+ * @param actions actions to take
+ * @return MessageContext if the packet was ARP or NDP, otherwise null
+ */
+ public static NeighbourMessageContext createContext(Ethernet eth,
+ ConnectPoint inPort,
+ NeighbourMessageActions actions) {
+ if (eth.getEtherType() == Ethernet.TYPE_ARP) {
+ return createArpContext(eth, inPort, actions);
+ } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
+ return createNdpContext(eth, inPort, actions);
+ }
+
+ return null;
+ }
+
+ /**
+ * Extracts context information from ARP packets.
+ *
+ * @param eth input Ethernet frame that is thought to be ARP
+ * @param inPort in port
+ * @param actions actions to take
+ * @return MessageContext object if the packet was a valid ARP packet,
+ * otherwise null
+ */
+ private static NeighbourMessageContext createArpContext(Ethernet eth,
+ ConnectPoint inPort,
+ NeighbourMessageActions actions) {
+ if (eth.getEtherType() != Ethernet.TYPE_ARP) {
+ return null;
+ }
+
+ ARP arp = (ARP) eth.getPayload();
+
+ IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress());
+ IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress());
+
+ NeighbourMessageType type;
+ if (arp.getOpCode() == ARP.OP_REQUEST) {
+ type = NeighbourMessageType.REQUEST;
+ } else if (arp.getOpCode() == ARP.OP_REPLY) {
+ type = NeighbourMessageType.REPLY;
+ } else {
+ return null;
+ }
+
+ return new DefaultNeighbourMessageContext(actions, eth, inPort,
+ NeighbourProtocol.ARP, type, target, sender);
+ }
+
+ /**
+ * Extracts context information from NDP packets.
+ *
+ * @param eth input Ethernet frame that is thought to be NDP
+ * @param inPort in port
+ * @param actions actions to take
+ * @return MessageContext object if the packet was a valid NDP packet,
+ * otherwise null
+ */
+ private static NeighbourMessageContext createNdpContext(Ethernet eth,
+ ConnectPoint inPort,
+ NeighbourMessageActions actions) {
+ if (eth.getEtherType() != Ethernet.TYPE_IPV6) {
+ return null;
+ }
+ IPv6 ipv6 = (IPv6) eth.getPayload();
+
+ if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
+ return null;
+ }
+ ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
+
+ IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress());
+ IpAddress target;
+
+ NeighbourMessageType type;
+ if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
+ type = NeighbourMessageType.REQUEST;
+ NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
+ target = Ip6Address.valueOf(nsol.getTargetAddress());
+ } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
+ type = NeighbourMessageType.REPLY;
+ /*
+ * sender and target are the same in the reply.
+ * We use as target the destination ip.
+ */
+ target = Ip6Address.valueOf(ipv6.getDestinationAddress());
+ } else {
+ return null;
+ }
+
+ return new DefaultNeighbourMessageContext(actions, eth, inPort,
+ NeighbourProtocol.NDP, type, target, sender);
+ }
+
+}
diff --git a/core/net/src/main/java/org/onosproject/net/neighbour/impl/NeighbourResolutionManager.java b/core/net/src/main/java/org/onosproject/net/neighbour/impl/NeighbourResolutionManager.java
new file mode 100644
index 0000000..2a9a187
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/neighbour/impl/NeighbourResolutionManager.java
@@ -0,0 +1,477 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.neighbour.impl;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Multimaps;
+import com.google.common.collect.SetMultimap;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.ICMP6;
+import org.onlab.packet.IPv6;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onlab.util.Tools;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.neighbour.NeighbourHandlerRegistration;
+import org.onosproject.net.neighbour.NeighbourMessageActions;
+import org.onosproject.net.neighbour.NeighbourMessageContext;
+import org.onosproject.net.neighbour.NeighbourMessageHandler;
+import org.onosproject.net.neighbour.NeighbourResolutionService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.edge.EdgePortService;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.host.HostService;
+import org.onosproject.net.packet.InboundPacket;
+import org.onosproject.net.packet.PacketContext;
+import org.onosproject.net.packet.PacketProcessor;
+import org.onosproject.net.packet.PacketService;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.packet.Ethernet.TYPE_ARP;
+import static org.onlab.packet.Ethernet.TYPE_IPV6;
+import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
+import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
+import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
+import static org.onosproject.net.packet.PacketPriority.CONTROL;
+
+/**
+ * Manages handlers for neighbour messages.
+ */
+@Service
+@Component(immediate = true)
+public class NeighbourResolutionManager implements NeighbourResolutionService {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected HostService hostService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected EdgePortService edgeService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected PacketService packetService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ComponentConfigService componentConfigService;
+
+ @Property(name = "arpEnabled", boolValue = true,
+ label = "Enable Address resolution protocol")
+ protected boolean arpEnabled = true;
+
+ @Property(name = "ndpEnabled", boolValue = false,
+ label = "Enable IPv6 neighbour discovery")
+ protected boolean ndpEnabled = false;
+
+ @Property(name = "requestInterceptsEnabled", boolValue = true,
+ label = "Enable requesting packet intercepts")
+ private boolean requestInterceptsEnabled = true;
+
+ private static final String APP_NAME = "org.onosproject.neighbour";
+ private ApplicationId appId;
+
+ private final SetMultimap<ConnectPoint, NeighbourHandlerRegistration> packetHandlers =
+ Multimaps.synchronizedSetMultimap(HashMultimap.create());
+
+ private final InternalPacketProcessor processor = new InternalPacketProcessor();
+ private NeighbourMessageActions actions;
+
+ @Activate
+ protected void activate(ComponentContext context) {
+ appId = coreService.registerApplication(APP_NAME);
+
+ componentConfigService.registerProperties(getClass());
+ modified(context);
+
+ actions = new DefaultNeighbourMessageActions(packetService, edgeService);
+
+ packetService.addProcessor(processor, PacketProcessor.director(1));
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ cancelPackets();
+ packetService.removeProcessor(processor);
+ componentConfigService.unregisterProperties(getClass(), false);
+ }
+
+ @Modified
+ protected void modified(ComponentContext context) {
+ Dictionary<?, ?> properties = context.getProperties();
+ Boolean flag;
+
+ flag = Tools.isPropertyEnabled(properties, "ndpEnabled");
+ if (flag != null) {
+ ndpEnabled = flag;
+ log.info("IPv6 neighbor discovery is {}",
+ ndpEnabled ? "enabled" : "disabled");
+ }
+
+ flag = Tools.isPropertyEnabled(properties, "arpEnabled");
+ if (flag != null) {
+ arpEnabled = flag;
+ log.info("Address resolution protocol is {}",
+ arpEnabled ? "enabled" : "disabled");
+ }
+
+ flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
+ if (flag == null) {
+ log.info("Request intercepts is not configured, " +
+ "using current value of {}", requestInterceptsEnabled);
+ } else {
+ requestInterceptsEnabled = flag;
+ log.info("Configured. Request intercepts is {}",
+ requestInterceptsEnabled ? "enabled" : "disabled");
+ }
+
+ synchronized (packetHandlers) {
+ if (!packetHandlers.isEmpty() && requestInterceptsEnabled) {
+ requestPackets();
+ } else {
+ cancelPackets();
+ }
+ }
+ }
+
+ private void requestPackets() {
+ if (arpEnabled) {
+ packetService.requestPackets(buildArpSelector(), CONTROL, appId);
+ } else {
+ packetService.cancelPackets(buildArpSelector(), CONTROL, appId);
+ }
+
+ if (ndpEnabled) {
+ packetService.requestPackets(buildNeighborSolicitationSelector(),
+ CONTROL, appId);
+ packetService.requestPackets(buildNeighborAdvertisementSelector(),
+ CONTROL, appId);
+ } else {
+ packetService.cancelPackets(buildNeighborSolicitationSelector(),
+ CONTROL, appId);
+ packetService.cancelPackets(buildNeighborAdvertisementSelector(),
+ CONTROL, appId);
+ }
+ }
+
+ private void cancelPackets() {
+ packetService.cancelPackets(buildArpSelector(), CONTROL, appId);
+ packetService.cancelPackets(buildNeighborSolicitationSelector(),
+ CONTROL, appId);
+ packetService.cancelPackets(buildNeighborAdvertisementSelector(),
+ CONTROL, appId);
+ }
+
+ private TrafficSelector buildArpSelector() {
+ return DefaultTrafficSelector.builder()
+ .matchEthType(TYPE_ARP)
+ .build();
+ }
+
+ private TrafficSelector buildNeighborSolicitationSelector() {
+ return DefaultTrafficSelector.builder()
+ .matchEthType(TYPE_IPV6)
+ .matchIPProtocol(PROTOCOL_ICMP6)
+ .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
+ .build();
+ }
+
+ private TrafficSelector buildNeighborAdvertisementSelector() {
+ return DefaultTrafficSelector.builder()
+ .matchEthType(TYPE_IPV6)
+ .matchIPProtocol(PROTOCOL_ICMP6)
+ .matchIcmpv6Type(NEIGHBOR_ADVERTISEMENT)
+ .build();
+ }
+
+ @Override
+ public void registerNeighbourHandler(ConnectPoint connectPoint,
+ NeighbourMessageHandler handler,
+ ApplicationId appId) {
+ register(connectPoint, new HandlerRegistration(handler, appId));
+ }
+
+ @Override
+ public void registerNeighbourHandler(Interface intf,
+ NeighbourMessageHandler handler,
+ ApplicationId appId) {
+ register(intf.connectPoint(), new HandlerRegistration(handler, intf, appId));
+ }
+
+ private void register(ConnectPoint connectPoint, HandlerRegistration registration) {
+ synchronized (packetHandlers) {
+ if (packetHandlers.isEmpty() && requestInterceptsEnabled) {
+ requestPackets();
+ }
+ packetHandlers.put(connectPoint, registration);
+ }
+ }
+
+ @Override
+ public void unregisterNeighbourHandler(ConnectPoint connectPoint,
+ NeighbourMessageHandler handler,
+ ApplicationId appId) {
+ unregister(connectPoint, new HandlerRegistration(handler, appId));
+ }
+
+ @Override
+ public void unregisterNeighbourHandler(Interface intf,
+ NeighbourMessageHandler handler,
+ ApplicationId appId) {
+ unregister(intf.connectPoint(), new HandlerRegistration(handler, intf, appId));
+ }
+
+ private void unregister(ConnectPoint connectPoint, HandlerRegistration registration) {
+ synchronized (packetHandlers) {
+ packetHandlers.remove(connectPoint, registration);
+
+ if (packetHandlers.isEmpty()) {
+ cancelPackets();
+ }
+ }
+ }
+
+ @Override
+ public void unregisterNeighbourHandlers(ApplicationId appId) {
+ synchronized (packetHandlers) {
+ Iterator<NeighbourHandlerRegistration> it = packetHandlers.values().iterator();
+
+ while (it.hasNext()) {
+ NeighbourHandlerRegistration registration = it.next();
+ if (registration.appId().equals(appId)) {
+ it.remove();
+ }
+ }
+
+ if (packetHandlers.isEmpty()) {
+ cancelPackets();
+ }
+ }
+ }
+
+ @Override
+ public Map<ConnectPoint, Collection<NeighbourHandlerRegistration>> getHandlerRegistrations() {
+ return ImmutableMap.copyOf(Multimaps.asMap(packetHandlers));
+ }
+
+ private void handlePacket(PacketContext context) {
+ InboundPacket pkt = context.inPacket();
+ Ethernet ethPkt = pkt.parsed();
+
+ NeighbourMessageContext msgContext =
+ DefaultNeighbourMessageContext.createContext(ethPkt, pkt.receivedFrom(), actions);
+
+ if (msgContext == null) {
+ return;
+ }
+
+ if (handleMessage(msgContext)) {
+ context.block();
+ }
+
+ }
+
+ private boolean handleMessage(NeighbourMessageContext context) {
+ Collection<NeighbourHandlerRegistration> handlers = packetHandlers.get(context.inPort());
+
+ Collection<NeighbourHandlerRegistration> handled = handlers
+ .stream()
+ .filter(registration -> registration.intf() == null || matches(context, registration.intf()))
+ .collect(Collectors.toSet());
+
+ handled.forEach(registration -> registration.handler().handleMessage(context, hostService));
+
+ return !handled.isEmpty();
+
+ }
+
+ /**
+ * Checks that incoming packet matches the parameters of the interface.
+ * This means that if the interface specifies a particular parameter
+ * (VLAN, IP address, etc.) then the incoming packet should match those
+ * parameters.
+ *
+ * @param context incoming message context
+ * @param intf interface to check
+ * @return true if the incoming message matches the interface, otherwise false
+ */
+ private boolean matches(NeighbourMessageContext context, Interface intf) {
+ checkNotNull(context);
+ checkNotNull(intf);
+
+ boolean matches = true;
+ // For non-broadcast packets, if the interface has a MAC address check that
+ // the destination MAC address of the packet matches the interface MAC
+ if (!context.dstMac().isBroadcast() &&
+ !intf.mac().equals(MacAddress.NONE) &&
+ !intf.mac().equals(context.dstMac())) {
+ matches = false;
+ }
+ // If the interface has a VLAN, check that the packet's VLAN matches
+ if (!intf.vlan().equals(VlanId.NONE) && !intf.vlan().equals(context.vlan())) {
+ matches = false;
+ }
+ // If the interface has IP addresses, check that the packet's target IP
+ // address matches one of the interface IP addresses
+ if (!intf.ipAddressesList().isEmpty() && !hasIp(intf, context.target())) {
+ matches = false;
+ }
+
+ return matches;
+ }
+
+ /**
+ * Returns true if the interface has the given IP address.
+ *
+ * @param intf interface to check
+ * @param ip IP address
+ * @return true if the IP is configured on the interface, otherwise false
+ */
+ private boolean hasIp(Interface intf, IpAddress ip) {
+ return intf.ipAddressesList().stream()
+ .anyMatch(intfAddress -> intfAddress.ipAddress().equals(ip));
+ }
+
+ /**
+ * Stores a neighbour message handler registration.
+ */
+ private class HandlerRegistration implements NeighbourHandlerRegistration {
+ private final Interface intf;
+ private final NeighbourMessageHandler handler;
+ private final ApplicationId appId;
+
+ /**
+ * Creates a new handler registration.
+ *
+ * @param handler neighbour message handler
+ */
+ public HandlerRegistration(NeighbourMessageHandler handler, ApplicationId appId) {
+ this(handler, null, appId);
+ }
+
+ /**
+ * Creates a new handler registration.
+ *
+ * @param handler neighbour message handler
+ * @param intf interface
+ */
+ public HandlerRegistration(NeighbourMessageHandler handler, Interface intf, ApplicationId appId) {
+ this.intf = intf;
+ this.handler = handler;
+ this.appId = appId;
+ }
+
+ @Override
+ public Interface intf() {
+ return intf;
+ }
+
+ @Override
+ public NeighbourMessageHandler handler() {
+ return handler;
+ }
+
+ @Override
+ public ApplicationId appId() {
+ return appId;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (this == other) {
+ return true;
+ }
+
+ if (!(other instanceof HandlerRegistration)) {
+ return false;
+ }
+
+ HandlerRegistration that = (HandlerRegistration) other;
+
+ return Objects.equals(intf, that.intf) &&
+ Objects.equals(handler, that.handler) &&
+ Objects.equals(appId, that.appId);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(intf, handler, appId);
+ }
+ }
+
+ /**
+ * Packet processor for incoming packets.
+ */
+ private class InternalPacketProcessor implements PacketProcessor {
+
+ @Override
+ public void process(PacketContext context) {
+ // Stop processing if the packet has been handled, since we
+ // can't do any more to it.
+ if (context.isHandled()) {
+ return;
+ }
+
+ InboundPacket pkt = context.inPacket();
+ Ethernet ethPkt = pkt.parsed();
+ if (ethPkt == null) {
+ return;
+ }
+
+ if (ethPkt.getEtherType() == TYPE_ARP) {
+ // handle ARP packets
+ handlePacket(context);
+ } else if (ethPkt.getEtherType() == TYPE_IPV6) {
+ IPv6 ipv6 = (IPv6) ethPkt.getPayload();
+ if (ipv6.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
+ ICMP6 icmp6 = (ICMP6) ipv6.getPayload();
+ if (icmp6.getIcmpType() == NEIGHBOR_SOLICITATION ||
+ icmp6.getIcmpType() == NEIGHBOR_ADVERTISEMENT) {
+ // handle ICMPv6 solicitations and advertisements (NDP)
+ handlePacket(context);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/neighbour/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/neighbour/impl/package-info.java
new file mode 100644
index 0000000..a044c67
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/neighbour/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of neighbour resolution service.
+ */
+package org.onosproject.net.neighbour.impl;
diff --git a/core/net/src/test/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageActionsTest.java b/core/net/src/test/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageActionsTest.java
new file mode 100644
index 0000000..52b5227
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/neighbour/impl/DefaultNeighbourMessageActionsTest.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.neighbour.impl;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Sets;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.ARP;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.edge.EdgePortServiceAdapter;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.packet.DefaultOutboundPacket;
+import org.onosproject.net.packet.OutboundPacket;
+import org.onosproject.net.packet.PacketService;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.onosproject.net.neighbour.impl.DefaultNeighbourMessageContext.createContext;
+
+/**
+ * Unit tests for DefaultNeighbourMessageActions.
+ */
+public class DefaultNeighbourMessageActionsTest {
+
+ private static final ConnectPoint CP1 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1");
+ private static final ConnectPoint CP2 = ConnectPoint.deviceConnectPoint("of:0000000000000001/2");
+ private static final ConnectPoint CP3 = ConnectPoint.deviceConnectPoint("of:0000000000000002/1");
+
+ private static final List<ConnectPoint> EDGE_PORTS =
+ ImmutableList.<ConnectPoint>builder()
+ .add(CP1)
+ .add(CP2)
+ .add(CP3)
+ .build();
+
+ private static final MacAddress MAC1 = MacAddress.valueOf(1);
+ private static final MacAddress MAC2 = MacAddress.valueOf(2);
+ private static final IpAddress IP1 = IpAddress.valueOf(1);
+ private static final IpAddress IP2 = IpAddress.valueOf(2);
+
+ private static final VlanId VLAN1 = VlanId.vlanId((short) 1);
+
+ private static final Interface INTF1 = NeighbourTestUtils.intf(CP1, IP1, MAC1, VLAN1);
+ private static final Interface INTF2 = NeighbourTestUtils.intf(CP2, IP2, MAC2, VLAN1);
+
+ private DefaultNeighbourMessageActions actions;
+ private PacketService packetService;
+
+ @Before
+ public void setUp() throws Exception {
+ packetService = createMock(PacketService.class);
+ actions = new DefaultNeighbourMessageActions(packetService, new TestEdgeService());
+ }
+
+ @Test
+ public void reply() throws Exception {
+ Ethernet request = NeighbourTestUtils.createArpRequest(IP1);
+
+ Ip4Address ip4Address = INTF1.ipAddressesList().get(0).ipAddress().getIp4Address();
+ Ethernet response = ARP.buildArpReply(ip4Address, MAC2, request);
+
+ packetService.emit(outbound(response, CP1));
+ expectLastCall().once();
+ replay(packetService);
+
+ actions.reply(createContext(request, CP1, null), MAC2);
+
+ verify(packetService);
+ }
+
+ @Test
+ public void forwardToConnectPoint() {
+ Ethernet request = NeighbourTestUtils.createArpRequest(IP1);
+
+ packetService.emit(outbound(request, CP2));
+ expectLastCall().once();
+ replay(packetService);
+
+ actions.forward(createContext(request, CP1, null), CP2);
+
+ verify(packetService);
+ }
+
+ @Test
+ public void forwardToInterface() {
+ Ethernet request = NeighbourTestUtils.createArpRequest(IP1);
+
+ Ethernet forwardedRequest = (Ethernet) request.clone();
+ forwardedRequest.setSourceMACAddress(INTF2.mac());
+ forwardedRequest.setVlanID(INTF2.vlan().toShort());
+
+ packetService.emit(outbound(forwardedRequest, CP2));
+ expectLastCall().once();
+ replay(packetService);
+
+ actions.forward(createContext(request, CP1, null), INTF2);
+
+ verify(packetService);
+ }
+
+ @Test
+ public void flood() {
+ Ethernet request = NeighbourTestUtils.createArpRequest(IP1);
+
+ // Expect the packet to be emitted out all ports apart from the in port
+ Sets.difference(Sets.newLinkedHashSet(EDGE_PORTS), Collections.singleton(CP1))
+ .forEach(cp -> {
+ packetService.emit(outbound(request, cp));
+ expectLastCall().once();
+ });
+ replay(packetService);
+
+ actions.flood(createContext(request, CP1, null));
+
+ verify(packetService);
+ }
+
+ private static OutboundPacket outbound(Ethernet packet, ConnectPoint outPort) {
+ TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(outPort.port()).build();
+ return new DefaultOutboundPacket(outPort.deviceId(),
+ treatment, ByteBuffer.wrap(packet.serialize()));
+ }
+
+ private class TestEdgeService extends EdgePortServiceAdapter {
+
+ @Override
+ public boolean isEdgePoint(ConnectPoint point) {
+ return true;
+ }
+
+ @Override
+ public Iterable<ConnectPoint> getEdgePoints() {
+ return EDGE_PORTS;
+ }
+
+ }
+
+}
diff --git a/core/net/src/test/java/org/onosproject/net/neighbour/impl/NeighbourResolutionManagerTest.java b/core/net/src/test/java/org/onosproject/net/neighbour/impl/NeighbourResolutionManagerTest.java
new file mode 100644
index 0000000..030d496
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/neighbour/impl/NeighbourResolutionManagerTest.java
@@ -0,0 +1,321 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.neighbour.impl;
+
+import java.util.Collection;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ComponentContextAdapter;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.TestApplicationId;
+import org.onosproject.cfg.ComponentConfigAdapter;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.host.HostService;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.neighbour.NeighbourHandlerRegistration;
+import org.onosproject.net.neighbour.NeighbourMessageContext;
+import org.onosproject.net.neighbour.NeighbourMessageHandler;
+import org.onosproject.net.packet.DefaultInboundPacket;
+import org.onosproject.net.packet.DefaultOutboundPacket;
+import org.onosproject.net.packet.InboundPacket;
+import org.onosproject.net.packet.OutboundPacket;
+import org.onosproject.net.packet.PacketContext;
+import org.onosproject.net.packet.PacketContextAdapter;
+import org.onosproject.net.packet.PacketPriority;
+import org.onosproject.net.packet.PacketProcessor;
+import org.onosproject.net.packet.PacketService;
+import org.onosproject.net.packet.PacketServiceAdapter;
+
+import static org.easymock.EasyMock.anyInt;
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.reset;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.net.neighbour.impl.DefaultNeighbourMessageContext.createContext;
+import static org.onosproject.net.neighbour.impl.NeighbourTestUtils.createArpRequest;
+import static org.onosproject.net.neighbour.impl.NeighbourTestUtils.intf;
+
+/**
+ * Unit tests for the NeighbourResolutionManager.
+ */
+public class NeighbourResolutionManagerTest {
+
+ private NeighbourResolutionManager neighbourManager;
+
+ private PacketService packetService;
+ private PacketProcessor packetProcessor;
+
+ private static final NeighbourMessageHandler HANDLER = new TestNeighbourHandler();
+
+ private static final ConnectPoint CP1 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1");
+ private static final ConnectPoint CP2 = ConnectPoint.deviceConnectPoint("of:0000000000000001/2");
+
+ private static final MacAddress MAC1 = MacAddress.valueOf(1);
+ private static final IpAddress IP1 = IpAddress.valueOf(1);
+ private static final IpAddress IP2 = IpAddress.valueOf(2);
+
+ private static final Interface INTF1 = intf(CP1, IP1, MAC1, VlanId.NONE);
+
+ private static final ApplicationId APP_ID = TestApplicationId.create("app");
+
+ @Before
+ public void setUp() throws Exception {
+ neighbourManager = new NeighbourResolutionManager();
+
+ packetService = createMock(PacketService.class);
+ packetService.requestPackets(anyObject(TrafficSelector.class),
+ anyObject(PacketPriority.class), anyObject(ApplicationId.class));
+ expectLastCall().anyTimes();
+ packetService.addProcessor(anyObject(PacketProcessor.class), anyInt());
+ expectLastCall().andDelegateTo(new TestPacketService()).once();
+ packetService.cancelPackets(anyObject(TrafficSelector.class),
+ anyObject(PacketPriority.class), anyObject(ApplicationId.class));
+ expectLastCall().anyTimes();
+ replay(packetService);
+
+ neighbourManager.packetService = packetService;
+
+ CoreService coreService = createNiceMock(CoreService.class);
+ replay(coreService);
+ neighbourManager.coreService = coreService;
+
+ neighbourManager.componentConfigService = new ComponentConfigAdapter();
+
+ neighbourManager.activate(new ComponentContextAdapter());
+ }
+
+ @Test
+ public void testRegistration() throws Exception {
+ neighbourManager.registerNeighbourHandler(CP1, HANDLER, APP_ID);
+
+ assertTrue(verifyRegistration(CP1, HANDLER, APP_ID));
+ }
+
+ @Test
+ public void testUnregister() {
+ // Register a handler and verify the registration is there
+ neighbourManager.registerNeighbourHandler(CP1, HANDLER, APP_ID);
+ assertTrue(verifyRegistration(CP1, HANDLER, APP_ID));
+
+ // Unregister the handler but supply a different connect point
+ neighbourManager.unregisterNeighbourHandler(CP2, HANDLER, APP_ID);
+
+ // Verify the original registration is still there on the original
+ // connect point
+ assertTrue(verifyRegistration(CP1, HANDLER, APP_ID));
+
+ assertTrue(verifyNoRegistration(CP2));
+
+ // Unregister the handler from the original connect point
+ neighbourManager.unregisterNeighbourHandler(CP1, HANDLER, APP_ID);
+
+ // Verify that it is gone
+ assertTrue(verifyNoRegistration(CP1));
+ }
+
+ @Test
+ public void testRegisterInterface() {
+ neighbourManager.registerNeighbourHandler(INTF1, HANDLER, APP_ID);
+
+ assertTrue(verifyRegistration(INTF1, HANDLER, APP_ID));
+ }
+
+ @Test
+ public void testUnregisterInterface() {
+ // Register a handler for an interface and verify it is there
+ neighbourManager.registerNeighbourHandler(INTF1, HANDLER, APP_ID);
+ assertTrue(verifyRegistration(INTF1, HANDLER, APP_ID));
+
+ // Unregister the handler but use the connect point rather than the interface
+ neighbourManager.unregisterNeighbourHandler(CP1, HANDLER, APP_ID);
+
+ // Verify the interface registration is still there
+ assertTrue(verifyRegistration(INTF1, HANDLER, APP_ID));
+
+ // Unregister the handler from the interface
+ neighbourManager.unregisterNeighbourHandler(INTF1, HANDLER, APP_ID);
+
+ // Verify the registration is gone
+ assertTrue(verifyNoRegistration(INTF1));
+ }
+
+ @Test
+ public void testUnregisterByAppId() {
+ // Register some handlers and verify they are there
+ neighbourManager.registerNeighbourHandler(CP1, HANDLER, APP_ID);
+ neighbourManager.registerNeighbourHandler(CP2, HANDLER, APP_ID);
+
+ assertEquals(2, neighbourManager.getHandlerRegistrations().size());
+
+ // Unregister all handlers for the given app ID
+ neighbourManager.unregisterNeighbourHandlers(APP_ID);
+
+ // Verify the handlers are gone
+ assertEquals(0, neighbourManager.getHandlerRegistrations().size());
+ }
+
+ @Test
+ public void testPacketDistribution() {
+ Ethernet arpRequest = createArpRequest(IP1);
+
+ NeighbourMessageHandler handler = createMock(NeighbourMessageHandler.class);
+ handler.handleMessage(eq(createContext(arpRequest, CP1, null)), anyObject(HostService.class));
+ expectLastCall().once();
+ replay(handler);
+ neighbourManager.registerNeighbourHandler(CP1, handler, APP_ID);
+
+ // Incoming packet on the connect point where the handler is registered
+ packetProcessor.process(context(arpRequest, CP1));
+
+ // Send a packet from a different connect point that should not be
+ // delivered to the handler
+ packetProcessor.process(context(arpRequest, CP2));
+
+ verify(handler);
+ }
+
+ @Test
+ public void testPacketDistributionToInterface() {
+ Ethernet arpRequest = createArpRequest(IP1);
+
+ NeighbourMessageHandler handler = createMock(NeighbourMessageHandler.class);
+ handler.handleMessage(eq(createContext(arpRequest, CP1, null)), anyObject(HostService.class));
+ expectLastCall().once();
+ replay(handler);
+ neighbourManager.registerNeighbourHandler(INTF1, handler, APP_ID);
+
+ // Incoming packet matching the interface where the handler is registered
+ packetProcessor.process(context(arpRequest, CP1));
+
+ verify(handler);
+
+ reset(handler);
+ replay(handler);
+
+ // Incoming packet on same connect point but not matching the interface
+ packetProcessor.process(context(createArpRequest(IP2), CP1));
+
+ verify(handler);
+ }
+
+ /**
+ * Verifies that there is one registration for the given connect point and
+ * that the registration matches the given handler and appId.
+ *
+ * @param cp connect point to verify registration for
+ * @param handler neighbour message handler
+ * @param appId application ID
+ * @return true if the registration is the only registration present for
+ * this connect point, otherwise false
+ */
+ private boolean verifyRegistration(ConnectPoint cp, NeighbourMessageHandler handler, ApplicationId appId) {
+ Collection<NeighbourHandlerRegistration> registrations =
+ neighbourManager.getHandlerRegistrations().get(cp);
+
+ if (registrations == null) {
+ return false;
+ }
+
+ if (registrations.size() != 1) {
+ return false;
+ }
+
+ NeighbourHandlerRegistration reg = registrations.stream().findFirst().get();
+
+ return reg.appId().equals(appId) &&
+ reg.handler().equals(handler);
+ }
+
+ /**
+ * Verifies that there is one registration for the given interface and
+ * that the registration matches the given handler and appId.
+ *
+ * @param intf interface to verify registration for
+ * @param handler neighbour message handler
+ * @param appId application ID
+ * @return true if the registration is the only registration present for
+ * this interface, otherwise false
+ */
+ private boolean verifyRegistration(Interface intf, NeighbourMessageHandler handler, ApplicationId appId) {
+ return verifyRegistration(intf.connectPoint(), handler, appId);
+ }
+
+ /**
+ * Verifies that there are no registrations for the given connect point.
+ *
+ * @param cp connect point
+ * @return true if there are no registrations for this connect point,
+ * otherwise false
+ */
+ private boolean verifyNoRegistration(ConnectPoint cp) {
+ return neighbourManager.getHandlerRegistrations().get(cp) == null;
+ }
+
+ /**
+ * Verifies that there are no registrations for the given interface.
+ *
+ * @param intf interface
+ * @return true if there are no registrations for this interface,
+ * otherwise false
+ */
+ private boolean verifyNoRegistration(Interface intf) {
+ return verifyNoRegistration(intf.connectPoint());
+ }
+
+ /**
+ * Creates a packet context for the given packet coming in the given port.
+ *
+ * @param packet packet to wrap in a packet context
+ * @param inPort input port of the packet
+ * @return packet context
+ */
+ private static PacketContext context(Ethernet packet, ConnectPoint inPort) {
+ InboundPacket inboundPacket = new DefaultInboundPacket(inPort, packet, null);
+ OutboundPacket outboundPacket = new DefaultOutboundPacket(null, null, null);
+ return new PacketContextAdapter(0, inboundPacket, outboundPacket, false);
+ }
+
+ private class TestPacketService extends PacketServiceAdapter {
+
+ @Override
+ public void addProcessor(PacketProcessor processor, int priority) {
+ NeighbourResolutionManagerTest.this.packetProcessor = processor;
+ }
+ }
+
+ private static class TestNeighbourHandler implements NeighbourMessageHandler {
+
+ @Override
+ public void handleMessage(NeighbourMessageContext context, HostService hostService) {
+
+ }
+ }
+
+}
diff --git a/core/net/src/test/java/org/onosproject/net/neighbour/impl/NeighbourTestUtils.java b/core/net/src/test/java/org/onosproject/net/neighbour/impl/NeighbourTestUtils.java
new file mode 100644
index 0000000..ed5ed0c
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/neighbour/impl/NeighbourTestUtils.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.neighbour.impl;
+
+import org.onlab.packet.ARP;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.host.InterfaceIpAddress;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Test utilities.
+ */
+public final class NeighbourTestUtils {
+
+ private static final MacAddress MAC1 = MacAddress.valueOf(1);
+ private static final MacAddress MAC2 = MacAddress.valueOf(2);
+ private static final IpAddress IP2 = IpAddress.valueOf(2);
+
+ private NeighbourTestUtils() {
+
+ }
+
+ /**
+ * Creates an ARP request for the given target IP.
+ *
+ * @param targetIp IP address
+ * @return ARP request packet
+ */
+ public static Ethernet createArpRequest(IpAddress targetIp) {
+ Ethernet eth = new Ethernet();
+ eth.setDestinationMACAddress(MAC1);
+ eth.setSourceMACAddress(MAC2);
+ eth.setEtherType(Ethernet.TYPE_ARP);
+
+ ARP arp = new ARP();
+ arp.setOpCode(ARP.OP_REPLY);
+ arp.setProtocolType(ARP.PROTO_TYPE_IP);
+ arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
+
+ arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
+ arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
+ arp.setSenderHardwareAddress(MAC2.toBytes());
+ arp.setTargetHardwareAddress(MacAddress.ZERO.toBytes());
+
+ arp.setTargetProtocolAddress(targetIp.toOctets());
+ arp.setSenderProtocolAddress(IP2.toOctets());
+
+ eth.setPayload(arp);
+ return eth;
+ }
+
+ /**
+ * Creates an interface with the given parameters. The IP prefix is assumed
+ * to be a /24.
+ *
+ * @param cp connect point
+ * @param ip IP address
+ * @param mac MAC address
+ * @param vlan VLAN ID
+ * @return interface
+ */
+ public static Interface intf(ConnectPoint cp, IpAddress ip, MacAddress mac, VlanId vlan) {
+ List<InterfaceIpAddress> ips =
+ Collections.singletonList(InterfaceIpAddress.valueOf(ip.toString() + "/24"));
+ return new Interface("foo", cp, ips, mac, vlan);
+ }
+}