Moved routing/bgp config into common routing bundle.

This allows the configuration to be used from multiple applications.

 * The class that reads the configuration file is now a service so that
   config can be consumed by components in other bundles.
 * Name of config reader classes has been generalized to RoutingConfigService
 * All config has been added to RoutingConfigService, instead of having
   two service interfaces like we did previously

Change-Id: Iaec9daf0f5b72abe2d6709fb75188d6d81947478
diff --git a/apps/routing-api/src/main/java/org/onosproject/routingapi/config/BgpPeer.java b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/BgpPeer.java
new file mode 100644
index 0000000..47a3520
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/BgpPeer.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2014 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.routingapi.config;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.NetTools;
+import org.onosproject.net.PortNumber;
+
+import java.util.Objects;
+
+/**
+ * Configuration details for a BGP peer.
+ */
+public class BgpPeer {
+    private final ConnectPoint connectPoint;
+    private final IpAddress ipAddress;
+
+    /**
+     * Creates a new BgpPeer.
+     *
+     * @param dpid the DPID of the switch the peer is attached at, as a String
+     * @param port the port the peer is attached at
+     * @param ipAddress the IP address of the peer as a String
+     */
+    public BgpPeer(@JsonProperty("attachmentDpid") String dpid,
+                   @JsonProperty("attachmentPort") long port,
+                   @JsonProperty("ipAddress") String ipAddress) {
+        this.connectPoint = new ConnectPoint(
+                DeviceId.deviceId(NetTools.dpidToUri(dpid)),
+                PortNumber.portNumber(port));
+        this.ipAddress = IpAddress.valueOf(ipAddress);
+    }
+
+    /**
+     * Gets the connection point of the peer.
+     *
+     * @return the connection point
+     */
+    public ConnectPoint connectPoint() {
+        return connectPoint;
+    }
+
+    /**
+     * Gets the IP address of the peer.
+     *
+     * @return the IP address
+     */
+    public IpAddress ipAddress() {
+        return ipAddress;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(connectPoint, ipAddress);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+
+        if (!(obj instanceof BgpPeer)) {
+            return false;
+        }
+
+        BgpPeer that = (BgpPeer) obj;
+        return Objects.equals(this.connectPoint, that.connectPoint)
+                && Objects.equals(this.ipAddress, that.ipAddress);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("connectPoint", connectPoint)
+                .add("ipAddress", ipAddress)
+                .toString();
+    }
+}
diff --git a/apps/routing-api/src/main/java/org/onosproject/routingapi/config/BgpSpeaker.java b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/BgpSpeaker.java
new file mode 100644
index 0000000..c19ceb6
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/BgpSpeaker.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2014 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.routingapi.config;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.MacAddress;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.NetTools;
+import org.onosproject.net.PortNumber;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Represents a BGP daemon in SDN network.
+ * <p>
+ * Each BGP speaker has a attachment point, which includes a switch DPID and a
+ * switch port. Each BGP speaker has one MAC address and several IP addresses,
+ * which are used to peer with BGP peers outside the SDN network. For each
+ * peer outside the SDN network, we configure a different IP address to BGP
+ * speaker inside the SDN network.
+ * </p>
+ * <p>
+ * Each BGP speaker has a name, which is a unique identifying String that is
+ * used to reference this speaker in the configuration.
+ * </p>
+ */
+public class BgpSpeaker {
+    private final String name;
+    private final ConnectPoint connectPoint;
+    private final MacAddress macAddress;
+    private List<InterfaceAddress> interfaceAddresses;
+
+    /**
+     * Class constructor used by the JSON library to create an object.
+     *
+     * @param name the name of the BGP speaker inside SDN network
+     * @param attachmentDpid the DPID where the BGP speaker is attached to
+     * @param attachmentPort the port where the BGP speaker is attached to
+     * @param macAddress the MAC address of the BGP speaker
+     */
+    @JsonCreator
+    public BgpSpeaker(@JsonProperty("name") String name,
+            @JsonProperty("attachmentDpid") String attachmentDpid,
+            @JsonProperty("attachmentPort") long attachmentPort,
+            @JsonProperty("macAddress") String macAddress) {
+
+        this.name = name;
+        this.macAddress = MacAddress.valueOf(macAddress);
+        this.connectPoint = new ConnectPoint(
+                DeviceId.deviceId(NetTools.dpidToUri(attachmentDpid)),
+                PortNumber.portNumber(attachmentPort));
+    }
+
+    /**
+     * Sets the addresses we configured for the BGP speaker on all virtual
+     * {@link Interface}s.
+     *
+     * @param interfaceAddresses a list of IP addresses of the BGP speaker
+     * configured on all virtual interfaces
+     */
+    @JsonProperty("interfaceAddresses")
+    public void setInterfaceAddresses(
+            List<InterfaceAddress> interfaceAddresses) {
+        this.interfaceAddresses = interfaceAddresses;
+    }
+
+    /**
+     * Gets the BGP speaker name.
+     *
+     * @return the BGP speaker name
+     */
+    public String name() {
+        return name;
+    }
+
+    /**
+     * Gets the connect point where the BGP speaker is attached.
+     *
+     * @return the connect point
+     */
+    public ConnectPoint connectPoint() {
+        return connectPoint;
+    }
+
+    /**
+     * Gets the MAC address of the BGP speaker.
+     *
+     * @return the MAC address
+     */
+    public MacAddress macAddress() {
+        return macAddress;
+    }
+
+    /**
+     * Gets all IP addresses configured on all {@link Interface}s of the
+     * BGP speaker.
+     *
+     * @return a list of IP addresses of the BGP speaker configured on all
+     * virtual interfaces
+     */
+    public List<InterfaceAddress> interfaceAddresses() {
+        return interfaceAddresses;
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (!(other instanceof BgpSpeaker)) {
+            return false;
+        }
+
+        BgpSpeaker otherBgpSpeaker = (BgpSpeaker) other;
+
+        return  name.equals(otherBgpSpeaker.name) &&
+                connectPoint.equals(
+                        otherBgpSpeaker.connectPoint) &&
+                macAddress.equals(otherBgpSpeaker.macAddress) &&
+                interfaceAddresses.equals(otherBgpSpeaker.interfaceAddresses);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(name, connectPoint, macAddress,
+                interfaceAddresses);
+
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("speakerName", name)
+                .add("connectPoint", connectPoint)
+                .add("macAddress", macAddress)
+                .add("interfaceAddresses", interfaceAddresses)
+                .toString();
+    }
+}
diff --git a/apps/routing-api/src/main/java/org/onosproject/routingapi/config/Interface.java b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/Interface.java
new file mode 100644
index 0000000..7f7547d
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/Interface.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2014 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.routingapi.config;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.Sets;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.host.InterfaceIpAddress;
+import org.onosproject.net.host.PortAddresses;
+
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * An Interface is a set of addresses that are logically mapped to a switch
+ * port in the network.
+ */
+public class Interface {
+    private final ConnectPoint connectPoint;
+    private final Set<InterfaceIpAddress> ipAddresses;
+    private final MacAddress macAddress;
+    private final VlanId vlan;
+
+    /**
+     * Creates an Interface based on a connection point, a set of interface
+     * IP addresses, and a MAC address.
+     *
+     * @param connectPoint the connect point this interface is mapped to
+     * @param ipAddresses the IP addresses for the interface
+     * @param macAddress the MAC address of the interface
+     * @param vlan VLAN identifier
+     */
+    public Interface(ConnectPoint connectPoint,
+                     Set<InterfaceIpAddress> ipAddresses,
+                     MacAddress macAddress, VlanId vlan) {
+        this.connectPoint = connectPoint;
+        this.ipAddresses = Sets.newHashSet(ipAddresses);
+        this.macAddress = macAddress;
+        this.vlan = vlan;
+    }
+
+    /**
+     * Creates an Interface based on a PortAddresses object.
+     *
+     * @param portAddresses the PortAddresses object to turn into an Interface
+     */
+    public Interface(PortAddresses portAddresses) {
+        connectPoint = portAddresses.connectPoint();
+        ipAddresses = Sets.newHashSet(portAddresses.ipAddresses());
+        macAddress = portAddresses.mac();
+        vlan = portAddresses.vlan();
+    }
+
+    /**
+     * Retrieves the connection point that this interface maps to.
+     *
+     * @return the connection point
+     */
+    public ConnectPoint connectPoint() {
+        return connectPoint;
+    }
+
+    /**
+     * Retrieves the set of IP addresses that are assigned to the interface.
+     *
+     * @return the set of interface IP addresses
+     */
+    public Set<InterfaceIpAddress> ipAddresses() {
+        return ipAddresses;
+    }
+
+    /**
+     * Retrieves the MAC address that is assigned to the interface.
+     *
+     * @return the MAC address
+     */
+    public MacAddress mac() {
+        return macAddress;
+    }
+
+    /**
+     * Retrieves the VLAN ID that is assigned to the interface.
+     *
+     * @return the VLAN ID
+     */
+    public VlanId vlan() {
+        return vlan;
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (!(other instanceof Interface)) {
+            return false;
+        }
+
+        Interface otherInterface = (Interface) other;
+
+        return  connectPoint.equals(otherInterface.connectPoint) &&
+                ipAddresses.equals(otherInterface.ipAddresses) &&
+                macAddress.equals(otherInterface.macAddress) &&
+                vlan.equals(otherInterface.vlan);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(connectPoint, ipAddresses, macAddress, vlan);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("connectPoint", connectPoint)
+                .add("ipAddresses", ipAddresses)
+                .add("macAddress", macAddress)
+                .add("vlan", vlan)
+                .toString();
+    }
+}
diff --git a/apps/routing-api/src/main/java/org/onosproject/routingapi/config/InterfaceAddress.java b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/InterfaceAddress.java
new file mode 100644
index 0000000..5fa4d64
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/InterfaceAddress.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2014 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.routingapi.config;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.NetTools;
+import org.onosproject.net.PortNumber;
+
+import java.util.Objects;
+
+/**
+ * Represents an address of a {@link BgpSpeaker} configured on an
+ * {@link Interface}.
+ * <p>
+ * Each InterfaceAddress includes the interface name and an IP address.
+ * </p>
+ */
+public class InterfaceAddress {
+    private final ConnectPoint connectPoint;
+    private final IpAddress ipAddress;
+
+    /**
+     * Creates an InterfaceAddress object.
+     *
+     * @param dpid the DPID of the interface as a String
+     * @param port the port of the interface
+     * @param ipAddress the IP address of a {@link BgpSpeaker} configured on
+     * the interface
+     */
+    public InterfaceAddress(@JsonProperty("interfaceDpid") String dpid,
+                            @JsonProperty("interfacePort") int port,
+                            @JsonProperty("ipAddress") String ipAddress) {
+        this.connectPoint = new ConnectPoint(
+                DeviceId.deviceId(NetTools.dpidToUri(dpid)),
+                PortNumber.portNumber(port));
+        this.ipAddress = IpAddress.valueOf(ipAddress);
+    }
+
+    /**
+     * Gets the connection point of the peer.
+     *
+     * @return the connection point
+     */
+    public ConnectPoint connectPoint() {
+        return connectPoint;
+    }
+
+    /**
+     * Gets the IP address of a BGP speaker configured on an {@link Interface}.
+     *
+     * @return the IP address
+     */
+    public IpAddress ipAddress() {
+        return ipAddress;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(connectPoint, ipAddress);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+
+        if (!(obj instanceof InterfaceAddress)) {
+            return false;
+        }
+
+        InterfaceAddress that = (InterfaceAddress) obj;
+        return Objects.equals(this.connectPoint, that.connectPoint)
+                && Objects.equals(this.ipAddress, that.ipAddress);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("connectPoint", connectPoint)
+                .add("ipAddress", ipAddress)
+                .toString();
+    }
+}
diff --git a/apps/routing-api/src/main/java/org/onosproject/routingapi/config/RoutingConfigurationService.java b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/RoutingConfigurationService.java
new file mode 100644
index 0000000..385e99d
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/RoutingConfigurationService.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2014 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.routingapi.config;
+
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Provides information about the routing configuration.
+ */
+public interface RoutingConfigurationService {
+
+    /**
+     * Gets the list of BGP speakers inside the SDN network.
+     *
+     * @return the map of BGP speaker names to BGP speaker objects
+     */
+    public Map<String, BgpSpeaker> getBgpSpeakers();
+
+    /**
+     * Gets the list of configured BGP peers.
+     *
+     * @return the map from peer IP address to BgpPeer object
+     */
+    public Map<IpAddress, BgpPeer> getBgpPeers();
+
+    /**
+     * Retrieves the entire set of interfaces in the network.
+     *
+     * @return the set of interfaces
+     */
+    Set<Interface> getInterfaces();
+
+    /**
+     * Retrieves the interface associated with the given connect point.
+     *
+     * @param connectPoint the connect point to retrieve interface information
+     * for
+     * @return the interface
+     */
+    Interface getInterface(ConnectPoint connectPoint);
+
+    /**
+     * Retrieves the interface that matches the given IP address. Matching
+     * means that the IP address is in one of the interface's assigned subnets.
+     *
+     * @param ipAddress IP address to match
+     * @return the matching interface
+     */
+    Interface getMatchingInterface(IpAddress ipAddress);
+
+}
diff --git a/apps/routing-api/src/main/java/org/onosproject/routingapi/config/package-info.java b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/package-info.java
new file mode 100644
index 0000000..7e464da
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routingapi/config/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014 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.
+ */
+
+/**
+ * SDN-IP configuration services.
+ */
+package org.onosproject.routingapi.config;