ONOS-6887 Move neighbour classes from incubator to core

Change-Id: I5fa70253b833838566a3527d8938e04be4274210
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/DefaultNeighbourMessageHandler.java b/core/api/src/main/java/org/onosproject/net/neighbour/DefaultNeighbourMessageHandler.java
new file mode 100644
index 0000000..5f34e9f
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/DefaultNeighbourMessageHandler.java
@@ -0,0 +1,74 @@
+/*
+ * 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;
+
+import org.onosproject.net.Host;
+import org.onosproject.net.host.HostService;
+
+import java.util.Set;
+
+import static org.onlab.packet.VlanId.vlanId;
+import static org.onosproject.net.HostId.hostId;
+
+/**
+ * Default neighbour message handler which implements basic proxying on an
+ * L2 network (i.e. ProxyArp behaviour).
+ */
+public class DefaultNeighbourMessageHandler implements NeighbourMessageHandler {
+    @Override
+    public void handleMessage(NeighbourMessageContext context, HostService hostService) {
+        switch (context.type()) {
+        case REPLY:
+            Host h = hostService.getHost(hostId(context.packet().getDestinationMAC(),
+                    vlanId(context.packet().getVlanID())));
+
+            if (h == null) {
+                context.flood();
+            } else {
+                context.forward(h.location());
+            }
+            break;
+        case REQUEST:
+            // See if we have the target host in the host store
+            Set<Host> hosts = hostService.getHostsByIp(context.target());
+
+            Host dst = null;
+            Host src = hostService.getHost(hostId(context.srcMac(), context.vlan()));
+
+            for (Host host : hosts) {
+                if (host.vlan().equals(context.vlan())) {
+                    dst = host;
+                    break;
+                }
+            }
+
+            if (src != null && dst != null) {
+                // We know the target host so we can respond
+                context.reply(dst.mac());
+                return;
+            }
+
+            // The request couldn't be resolved.
+            // Flood the request on all ports except the incoming port.
+            context.flood();
+            break;
+        default:
+            break;
+        }
+
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourHandlerRegistration.java b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourHandlerRegistration.java
new file mode 100644
index 0000000..3186cf6
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourHandlerRegistration.java
@@ -0,0 +1,47 @@
+/*
+ * 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;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.intf.Interface;
+
+/**
+ * Information about the registration of a neighbour message handler.
+ */
+public interface NeighbourHandlerRegistration {
+
+    /**
+     * Gets the interface of the registration.
+     *
+     * @return interface
+     */
+    Interface intf();
+
+    /**
+     * Gets the neighbour message handler.
+     *
+     * @return message handler
+     */
+    NeighbourMessageHandler handler();
+
+    /**
+     * Gets the ID of the application that registered the handler.
+     *
+     * @return application ID
+     */
+    ApplicationId appId();
+}
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageActions.java b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageActions.java
new file mode 100644
index 0000000..5bbfee2
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageActions.java
@@ -0,0 +1,68 @@
+/*
+ * 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;
+
+import com.google.common.annotations.Beta;
+import org.onlab.packet.MacAddress;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.ConnectPoint;
+
+/**
+ * Performs actions on a neighbour message contexts.
+ */
+@Beta
+public interface NeighbourMessageActions {
+
+    /**
+     * Replies to an incoming request with the given MAC address.
+     *
+     * @param context incoming message context
+     * @param targetMac target MAC address.
+     */
+    void reply(NeighbourMessageContext context, MacAddress targetMac);
+
+    /**
+     * Forwards the incoming message to the given connect point.
+     *
+     * @param context incoming message context
+     * @param outPort port to send the message out
+     */
+    void forward(NeighbourMessageContext context, ConnectPoint outPort);
+
+    /**
+     * Forwards the incoming message to a given interface. The message will be
+     * modified to fit the parameters of the outgoing interface.
+     *
+     * @param context incoming message context
+     * @param outIntf interface to send the message out
+     */
+    void forward(NeighbourMessageContext context, Interface outIntf);
+
+    /**
+     * Floods the incoming message to all edge ports except the in port.
+     *
+     * @param context incoming message context
+     */
+    void flood(NeighbourMessageContext context);
+
+    /**
+     * Drops the incoming message.
+     *
+     * @param context incoming message context
+     */
+    void drop(NeighbourMessageContext context);
+}
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageContext.java b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageContext.java
new file mode 100644
index 0000000..aef37bb
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageContext.java
@@ -0,0 +1,136 @@
+/*
+ * 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;
+
+import com.google.common.annotations.Beta;
+import org.onlab.packet.Ethernet;
+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;
+
+/**
+ * Context of an incoming neighbor message (e.g. ARP, NDP).
+ *
+ * <p>This includes information about the message accessible through a
+ * protocol-agnostic interface, as well as mechanisms to perform an action in
+ * response to the incoming message.</p>
+ */
+@Beta
+public interface NeighbourMessageContext {
+    /**
+     * Gets the port where the packet came in to the network.
+     *
+     * @return connect point
+     */
+    ConnectPoint inPort();
+
+    /**
+     * Gets the full parsed representation of the packet.
+     *
+     * @return ethernet header
+     */
+    Ethernet packet();
+
+    /**
+     * Gets the protocol of the packet.
+     *
+     * @return protocol
+     */
+    NeighbourProtocol protocol();
+
+    /**
+     * Gets the message type of the packet.
+     *
+     * @return message type
+     */
+    NeighbourMessageType type();
+
+    /**
+     * Gets the vlan of the packet, if any.
+     *
+     * @return vlan
+     */
+    VlanId vlan();
+
+    /**
+     * Gets the source MAC address of the message.
+     *
+     * @return source MAC address
+     */
+    MacAddress srcMac();
+
+    /**
+     * Gets the destination MAC address of the message.
+     * <p>
+     * Only valid for reply packets, will be null for request packets.
+     * </p>
+     *
+     * @return target MAC address
+     */
+    MacAddress dstMac();
+
+    /**
+     * Gets the target IP address of the message.
+     *
+     * @return target IP address
+     */
+    IpAddress target();
+
+    /**
+     * Gets the source IP address of the message.
+     *
+     * @return source IP address
+     */
+    IpAddress sender();
+
+    /**
+     * Forwards the message to a given output port.
+     *
+     * @param outPort output port
+     */
+    void forward(ConnectPoint outPort);
+
+    /**
+     * Forwards the message to a given interface.
+     * <p>
+     * The message will be modified to fit the parameters of the outgoing
+     * interface. For example, if the interface has a VLAN configured, the
+     * outgoing packet will have that VLAN tag added.
+     * </p>
+     * @param outIntf output interface
+     */
+    void forward(Interface outIntf);
+
+    /**
+     * Replies to the request message with a given MAC address.
+     *
+     * @param targetMac target MAC address
+     */
+    void reply(MacAddress targetMac);
+
+    /**
+     * Floods the incoming message out all ports except the input port.
+     */
+    void flood();
+
+    /**
+     * Drops the incoming message.
+     */
+    void drop();
+}
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageHandler.java b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageHandler.java
new file mode 100644
index 0000000..040f4b0
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageHandler.java
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.host.HostService;
+
+/**
+ * Handler for an incoming neighbour message.
+ *
+ * <p>An application may implement this interface in order to provide their own
+ * logic for handling particular neighbour messages.</p>
+ */
+@Beta
+public interface NeighbourMessageHandler {
+
+    /**
+     * Handles a neighbour message.
+     *
+     * @param context neighbour message context
+     * @param hostService host service
+     */
+    void handleMessage(NeighbourMessageContext context, HostService hostService);
+}
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageType.java b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageType.java
new file mode 100644
index 0000000..63fd199
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourMessageType.java
@@ -0,0 +1,35 @@
+/*
+ * 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;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Neighbour message type.
+ */
+@Beta
+public enum NeighbourMessageType {
+    /**
+     * Request message.
+     */
+    REQUEST,
+
+    /**
+     * Reply message.
+     */
+    REPLY
+}
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourProtocol.java b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourProtocol.java
new file mode 100644
index 0000000..ad00dd1
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourProtocol.java
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Enumerates protocols used for neighbour discover/address resolution.
+ */
+@Beta
+public enum NeighbourProtocol {
+
+    /**
+     * Address Resolution Protocol (IPv4).
+     */
+    ARP,
+
+    /**
+     * Neighbor Discovery Protocol (IPv6).
+     */
+    NDP
+}
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourResolutionService.java b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourResolutionService.java
new file mode 100644
index 0000000..d64cc5e
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/NeighbourResolutionService.java
@@ -0,0 +1,92 @@
+/*
+ * 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;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Provides a means of registering logic for handling neighbour messages.
+ */
+@Beta
+public interface NeighbourResolutionService {
+
+    /**
+     * Registers a neighbour message handler for all neighbour messages
+     * incoming on the given connect point.
+     *
+     * @param connectPoint connect point
+     * @param handler neighbour message handler
+     * @param appId application ID
+     */
+    void registerNeighbourHandler(ConnectPoint connectPoint, NeighbourMessageHandler handler,
+                                  ApplicationId appId);
+
+    /**
+     * Registers a neighbour message handler for all neighbour messages incoming
+     * on the given interface. Neighbour packets must match the fields of the
+     * interface in order to be handled by this message handler.
+     *
+     * @param intf interface
+     * @param handler neighbour message handler
+     * @param appId application ID
+     */
+    void registerNeighbourHandler(Interface intf, NeighbourMessageHandler handler,
+                                  ApplicationId appId);
+
+    /**
+     * Unregisters a neighbour message handler that was assigned to a connect
+     * point.
+     *
+     * @param connectPoint connect point
+     * @param handler neighbour message handler
+     * @param appId application ID
+     */
+    void unregisterNeighbourHandler(ConnectPoint connectPoint, NeighbourMessageHandler handler,
+                                    ApplicationId appId);
+
+    /**
+     * Unregisters a neighbour message handler that was assigned to an interface.
+     *
+     * @param intf interface
+     * @param handler neighbour message handler
+     * @param appId application ID
+     */
+    void unregisterNeighbourHandler(Interface intf, NeighbourMessageHandler handler,
+                                    ApplicationId appId);
+
+    /**
+     * Unregisters all neighbour handlers that were registered by the given
+     * application.
+     *
+     * @param appId application ID
+     */
+    void unregisterNeighbourHandlers(ApplicationId appId);
+
+    /**
+     * Gets the neighbour message handlers that have been registered with the
+     * service.
+     *
+     * @return neighbour message handlers indexed by connect point
+     */
+    Map<ConnectPoint, Collection<NeighbourHandlerRegistration>> getHandlerRegistrations();
+}
diff --git a/core/api/src/main/java/org/onosproject/net/neighbour/package-info.java b/core/api/src/main/java/org/onosproject/net/neighbour/package-info.java
new file mode 100644
index 0000000..7503dc0
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/neighbour/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.
+ */
+
+/**
+ * Neighbour message (ARP, NDP) handling.
+ */
+package org.onosproject.net.neighbour;