API for core-based unicast route service.

Change-Id: I5d20c02f90b74ef8013898da2520bfb19a54b5c0
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/ResolvedRoute.java b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/ResolvedRoute.java
new file mode 100644
index 0000000..0f07282
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/ResolvedRoute.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.incubator.net.routing;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+
+/**
+ * Represents a route with the next hop MAC address resolved.
+ */
+public class ResolvedRoute {
+
+    private final IpPrefix prefix;
+    private final IpAddress nextHop;
+    private final MacAddress nextHopMac;
+
+    /**
+     * Creates a new resolved route.
+     *
+     * @param route input route
+     * @param nextHopMac next hop MAC address
+     */
+    public ResolvedRoute(Route route, MacAddress nextHopMac) {
+        this.prefix = route.prefix();
+        this.nextHop = route.nextHop();
+        this.nextHopMac = nextHopMac;
+    }
+
+    /**
+     * Creates a new resolved route.
+     *
+     * @param prefix route prefix
+     * @param nextHop route next hop IP address
+     * @param nextHopMac next hop MAC address
+     */
+    public ResolvedRoute(IpPrefix prefix, IpAddress nextHop, MacAddress nextHopMac) {
+        this.prefix = prefix;
+        this.nextHop = nextHop;
+        this.nextHopMac = nextHopMac;
+    }
+
+    /**
+     * Returns the IP prefix.
+     *
+     * @return IP prefix
+     */
+    public IpPrefix prefix() {
+        return prefix;
+    }
+
+    /**
+     * Returns the next hop IP address.
+     *
+     * @return IP address
+     */
+    public IpAddress nextHop() {
+        return nextHop;
+    }
+
+    /**
+     * Returns the next hop MAC address.
+     *
+     * @return MAC address
+     */
+    public MacAddress nextHopMac() {
+        return nextHopMac;
+    }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/Route.java b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/Route.java
new file mode 100644
index 0000000..c4647e1
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/Route.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.incubator.net.routing;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Represents a route.
+ */
+public class Route {
+
+    private static final String VERSION_MISMATCH =
+            "Prefix and next hop must be in the same address family";
+
+    /**
+     * Source of the route.
+     */
+    public enum Source {
+        /**
+         * Route came from the iBGP route source.
+         */
+        BGP,
+
+        /**
+         * Route came from the FPM route source.
+         */
+        FPM,
+
+        /**
+         * Route can from the static route source.
+         */
+        STATIC,
+
+        /**
+         * Route source was not defined.
+         */
+        UNDEFINED
+    }
+
+    private final Source source;
+    private final IpPrefix prefix;
+    private final IpAddress nextHop;
+
+    /**
+     * Creates a route.
+     *
+     * @param source route source
+     * @param prefix IP prefix
+     * @param nextHop
+     */
+    public Route(Source source, IpPrefix prefix, IpAddress nextHop) {
+        checkNotNull(prefix);
+        checkNotNull(nextHop);
+        checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH);
+
+        this.source = checkNotNull(source);
+        this.prefix = prefix;
+        this.nextHop = nextHop;
+    }
+
+    /**
+     * Returns the route source.
+     *
+     * @return route source
+     */
+    public Source source() {
+        return source;
+    }
+
+    /**
+     * Returns the IP prefix of the route.
+     *
+     * @return IP prefix
+     */
+    public IpPrefix prefix() {
+        return prefix;
+    }
+
+    /**
+     * Returns the next hop IP address.
+     *
+     * @return next hop
+     */
+    public IpAddress nextHop() {
+        return nextHop;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(prefix, nextHop);
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+
+        if (!(other instanceof Route)) {
+            return false;
+        }
+
+        Route that = (Route) other;
+
+        return Objects.equals(this.prefix, that.prefix) &&
+                Objects.equals(this.nextHop, that.nextHop);
+    }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteEvent.java b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteEvent.java
new file mode 100644
index 0000000..e81fb84
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteEvent.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.incubator.net.routing;
+
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Describes an event about a route.
+ */
+public class RouteEvent extends AbstractEvent<RouteEvent.Type, ResolvedRoute> {
+
+    /**
+     * Route event type.
+     */
+    public enum Type {
+
+        /**
+         * Route is new.
+         */
+        ROUTE_ADDED,
+
+        /**
+         * Route has updated information.
+         */
+        ROUTE_UPDATED,
+
+        /**
+         * Route was removed.
+         */
+        ROUTE_REMOVED
+    }
+
+    /**
+     * Creates a new route event.
+     *
+     * @param type event type
+     * @param subject event subject
+     */
+    public RouteEvent(Type type, ResolvedRoute subject) {
+        super(type, subject);
+    }
+
+    /**
+     * Creates a new route event.
+     *
+     * @param type event type
+     * @param subject event subject
+     * @param time event time
+     */
+    protected RouteEvent(Type type, ResolvedRoute subject, long time) {
+        super(type, subject, time);
+    }
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteListener.java b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteListener.java
new file mode 100644
index 0000000..ad56240
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteListener.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.incubator.net.routing;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Listener for route events.
+ */
+public interface RouteListener extends EventListener<RouteEvent> {
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteService.java
new file mode 100644
index 0000000..3fd9e69
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteService.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.incubator.net.routing;
+
+import org.onlab.packet.IpAddress;
+import org.onosproject.event.ListenerService;
+
+import java.util.Collection;
+
+/**
+ * IP unicast routing service.
+ */
+public interface RouteService extends ListenerService<RouteEvent, RouteListener> {
+
+    /**
+     * Gets all routes.
+     *
+     * @return collection of all routes
+     */
+    Collection<Route> getRoutes();
+
+    /**
+     * Gets routes learnt from a particular source.
+     *
+     * @param source route source
+     * @return collection of routes
+     */
+    Collection<Route> getRoutesFromSource(Route.Source source);
+
+    /**
+     * Gets the longest prefix route that matches the given IP address.
+     *
+     * @param ip IP addres
+     * @return longest prefix matched route
+     */
+    Route longestPrefixMatch(IpAddress ip);
+
+    //TODO should mutation methods be pushed to a different interface?
+    /**
+     * Updates the given routes in the route service.
+     *
+     * @param routes collection of routes to update
+     */
+    void update(Collection<Route> routes);
+
+    /**
+     * Withdraws the given routes from the route service.
+     *
+     * @param routes collection of routes to withdraw
+     */
+    void withdraw(Collection<Route> routes);
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/package-info.java b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/package-info.java
new file mode 100644
index 0000000..e37dcdb
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * Unicast routing service.
+ */
+package org.onosproject.incubator.net.routing;