Add BGP configuration to config subsystem.

Change-Id: I77a5a7922387935f2142c3e74358c5498717a046
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/RoutingService.java b/apps/routing-api/src/main/java/org/onosproject/routing/RoutingService.java
index 7774082..5706211 100644
--- a/apps/routing-api/src/main/java/org/onosproject/routing/RoutingService.java
+++ b/apps/routing-api/src/main/java/org/onosproject/routing/RoutingService.java
@@ -15,21 +15,23 @@
  */
 package org.onosproject.routing;
 
-import java.util.Collection;
-
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.MacAddress;
 import org.onosproject.net.ConnectPoint;
 
+import java.util.Collection;
+
 /**
  * Provides a way of interacting with the RIB management component.
  */
 public interface RoutingService {
 
+    String ROUTER_APP_ID = "org.onosproject.router";
+
     /**
      * Specifies the type of an IP address or an IP prefix location.
      */
-    static enum LocationType {
+    enum LocationType {
         /**
          * The location of an IP address or an IP prefix is in local SDN network.
          */
diff --git a/apps/routing/src/main/java/org/onosproject/routing/cli/BgpPeersListCommand.java b/apps/routing/src/main/java/org/onosproject/routing/cli/BgpPeersListCommand.java
new file mode 100644
index 0000000..c23482f
--- /dev/null
+++ b/apps/routing/src/main/java/org/onosproject/routing/cli/BgpPeersListCommand.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2015 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.routing.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.incubator.net.config.NetworkConfigService;
+import org.onosproject.routing.RoutingService;
+import org.onosproject.routing.config.impl.BgpConfig;
+
+/**
+ * Lists the BGP peers configured in the system.
+ */
+@Command(scope = "onos", name = "bgp-peers",
+        description = "Lists all BGP peers")
+public class BgpPeersListCommand extends AbstractShellCommand {
+
+    private static final String FORMAT = "%s : %s";
+
+    @Override
+    protected void execute() {
+        NetworkConfigService configService = get(NetworkConfigService.class);
+        CoreService coreService = get(CoreService.class);
+        ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
+
+        print(appId.toString());
+
+        BgpConfig config = configService.getConfig(appId, BgpConfig.class);
+
+        if (config == null || config.bgpPeers().isEmpty()) {
+            print("No peers configured");
+        } else {
+            config.bgpPeers().forEach(
+                    p -> print(FORMAT, p.ipAddress(), p.connectPoint()));
+        }
+    }
+}
diff --git a/apps/routing/src/main/java/org/onosproject/routing/cli/BgpSpeakersListCommand.java b/apps/routing/src/main/java/org/onosproject/routing/cli/BgpSpeakersListCommand.java
new file mode 100644
index 0000000..2ec1bbd
--- /dev/null
+++ b/apps/routing/src/main/java/org/onosproject/routing/cli/BgpSpeakersListCommand.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2015 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.routing.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.incubator.net.config.NetworkConfigService;
+import org.onosproject.routing.RoutingService;
+import org.onosproject.routing.config.impl.BgpConfig;
+
+/**
+ * Lists the BGP speakers configured in the system.
+ */
+@Command(scope = "onos", name = "bgp-speakers",
+        description = "Lists all BGP speakers")
+public class BgpSpeakersListCommand extends AbstractShellCommand {
+
+    private static final String FORMAT = "%s : %s";
+
+    @Override
+    protected void execute() {
+        NetworkConfigService configService = get(NetworkConfigService.class);
+        CoreService coreService = get(CoreService.class);
+        ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
+
+        print(appId.toString());
+
+        BgpConfig config = configService.getConfig(appId, BgpConfig.class);
+
+        if (config == null || config.bgpSpeakers().isEmpty()) {
+            print("No speakers configured");
+        } else {
+            config.bgpSpeakers().forEach(
+                    s -> print(FORMAT, s.connectPoint(), s.listenAddresses()));
+        }
+    }
+}
diff --git a/apps/routing/src/main/java/org/onosproject/routing/config/impl/BgpConfig.java b/apps/routing/src/main/java/org/onosproject/routing/config/impl/BgpConfig.java
new file mode 100644
index 0000000..4e421bf
--- /dev/null
+++ b/apps/routing/src/main/java/org/onosproject/routing/config/impl/BgpConfig.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2015 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.routing.config.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.Sets;
+import org.onlab.packet.IpAddress;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.incubator.net.config.Config;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Configuration object for BGP config.
+ */
+public class BgpConfig extends Config<ApplicationId> {
+
+    public static final String PEERS = "bgpPeers";
+    public static final String SPEAKERS = "bgpSpeakers";
+    public static final String CONNECT_POINT = "connectPoint";
+    public static final String IP_ADDRESS = "ipAddress";
+    public static final String LISTEN_ADDRESSES = "listenAddresses";
+
+    /**
+     * Gets the set of configured BGP peers.
+     *
+     * @return BGP peers
+     */
+    public Set<BgpPeerConfig> bgpPeers() {
+        Set<BgpPeerConfig> peers = Sets.newHashSet();
+
+        JsonNode peersNode = node.get(PEERS);
+        peersNode.forEach(jsonNode -> peers.add(
+                new BgpPeerConfig(ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()),
+                        IpAddress.valueOf(jsonNode.path(IP_ADDRESS).asText()))));
+
+        return peers;
+    }
+
+    /**
+     * Gets the set of configured BGP speakers.
+     *
+     * @return BGP speakers
+     */
+    public Set<BgpSpeakerConfig> bgpSpeakers() {
+        Set<BgpSpeakerConfig> speakers = Sets.newHashSet();
+
+        JsonNode speakersNode = node.get(SPEAKERS);
+        speakersNode.forEach(jsonNode -> {
+            Set<IpAddress> listenAddresses = Sets.newHashSet();
+            jsonNode.path(LISTEN_ADDRESSES).forEach(addressNode ->
+                    listenAddresses.add(IpAddress.valueOf(addressNode.asText()))
+            );
+            speakers.add(new BgpSpeakerConfig(
+                    ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()),
+                    listenAddresses));
+        });
+
+        return speakers;
+    }
+
+    /**
+     * Configuration for a BGP peer.
+     */
+    public class BgpPeerConfig {
+        private ConnectPoint connectPoint;
+        private IpAddress ipAddress;
+
+        public BgpPeerConfig(ConnectPoint connectPoint, IpAddress ipAddress) {
+            this.connectPoint = connectPoint;
+            this.ipAddress = ipAddress;
+        }
+
+        public ConnectPoint connectPoint() {
+            return connectPoint;
+        }
+
+        public IpAddress ipAddress() {
+            return ipAddress;
+        }
+
+    }
+
+    /**
+     * Configuration for a BGP speaker.
+     */
+    public class BgpSpeakerConfig {
+
+        private ConnectPoint connectPoint;
+        private Set<IpAddress> listenAddresses;
+
+        public BgpSpeakerConfig(ConnectPoint connectPoint, Set<IpAddress> listenAddresses) {
+            this.connectPoint = checkNotNull(connectPoint);
+            this.listenAddresses = checkNotNull(listenAddresses);
+        }
+
+        public ConnectPoint connectPoint() {
+            return connectPoint;
+        }
+
+        public Set<IpAddress> listenAddresses() {
+            return listenAddresses;
+        }
+    }
+}
diff --git a/apps/routing/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java b/apps/routing/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
index 60e53a1..2667594 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
@@ -19,9 +19,9 @@
 import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
 import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
 import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
-
 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.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
@@ -30,6 +30,9 @@
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
+import org.onosproject.incubator.net.config.ConfigFactory;
+import org.onosproject.incubator.net.config.NetworkConfigRegistry;
+import org.onosproject.incubator.net.config.basics.SubjectFactories;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.host.HostService;
 import org.onosproject.routing.config.BgpPeer;
@@ -68,6 +71,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected HostService hostService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigRegistry registry;
+
     private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>();
     private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>();
     private Set<IpAddress> gatewayIpAddresses = new HashSet<>();
@@ -83,13 +89,28 @@
     private MacAddress virtualGatewayMacAddress;
     private HostToInterfaceAdaptor hostAdaptor;
 
+    private ConfigFactory configFactory =
+            new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, BgpConfig.class, "bgp") {
+        @Override
+        public BgpConfig createConfig() {
+            return new BgpConfig();
+        }
+    };
+
     @Activate
     public void activate() {
+        registry.registerConfigFactory(configFactory);
         readConfiguration();
         hostAdaptor = new HostToInterfaceAdaptor(hostService);
         log.info("Routing configuration service started");
     }
 
+    @Deactivate
+    public void deactivate() {
+        registry.unregisterConfigFactory(configFactory);
+        log.info("Routing configuration service stopped");
+    }
+
     /**
      * Reads SDN-IP related information contained in the configuration file.
      *
diff --git a/apps/routing/src/main/java/org/onosproject/routing/impl/Router.java b/apps/routing/src/main/java/org/onosproject/routing/impl/Router.java
index f110993..c5c7741 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/impl/Router.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/impl/Router.java
@@ -15,21 +15,14 @@
  */
 package org.onosproject.routing.impl;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.LinkedBlockingQueue;
-
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimaps;
+import com.google.common.collect.SetMultimap;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import com.googlecode.concurrenttrees.common.KeyValuePair;
+import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
+import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
+import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -41,6 +34,7 @@
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
+import org.onosproject.core.CoreService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Host;
 import org.onosproject.net.host.HostEvent;
@@ -60,15 +54,20 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Multimaps;
-import com.google.common.collect.SetMultimap;
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import com.googlecode.concurrenttrees.common.KeyValuePair;
-import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
-import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
-import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onosproject.routing.RouteEntry.createBinaryString;
 
 /**
@@ -102,6 +101,9 @@
     private IntentRequestListener intentRequestListener;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected HostService hostService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -123,6 +125,8 @@
         routesWaitingOnArp = Multimaps.synchronizedSetMultimap(
                 HashMultimap.<IpAddress, RouteEntry>create());
 
+        coreService.registerApplication(ROUTER_APP_ID);
+
         bgpUpdatesExecutor = Executors.newSingleThreadExecutor(
                 new ThreadFactoryBuilder()
                 .setNameFormat("sdnip-bgp-updates-%d").build());
diff --git a/apps/routing/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/routing/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 2026943..379eaac 100644
--- a/apps/routing/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/routing/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -31,5 +31,11 @@
     <command>
       <action class="org.onosproject.routing.cli.RemoveRouteCommand"/>
     </command>
+    <command>
+      <action class="org.onosproject.routing.cli.BgpPeersListCommand"/>
+    </command>
+    <command>
+      <action class="org.onosproject.routing.cli.BgpSpeakersListCommand"/>
+    </command>
   </command-bundle>
 </blueprint>
diff --git a/apps/routing/src/test/java/org/onosproject/routing/impl/RouterAsyncArpTest.java b/apps/routing/src/test/java/org/onosproject/routing/impl/RouterAsyncArpTest.java
index 272d26c..100c13d 100644
--- a/apps/routing/src/test/java/org/onosproject/routing/impl/RouterAsyncArpTest.java
+++ b/apps/routing/src/test/java/org/onosproject/routing/impl/RouterAsyncArpTest.java
@@ -26,6 +26,7 @@
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
+import org.onosproject.core.CoreService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DefaultHost;
 import org.onosproject.net.DeviceId;
@@ -90,6 +91,7 @@
         fibListener = createMock(FibListener.class);
 
         router = new Router();
+        router.coreService = createNiceMock(CoreService.class);
         router.hostService = hostService;
         router.routingConfigurationService = routingConfigurationService;
         router.bgpService = bgpService;
diff --git a/apps/routing/src/test/java/org/onosproject/routing/impl/RouterTest.java b/apps/routing/src/test/java/org/onosproject/routing/impl/RouterTest.java
index d17e493..45bc309 100644
--- a/apps/routing/src/test/java/org/onosproject/routing/impl/RouterTest.java
+++ b/apps/routing/src/test/java/org/onosproject/routing/impl/RouterTest.java
@@ -26,6 +26,7 @@
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
+import org.onosproject.core.CoreService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DefaultHost;
 import org.onosproject.net.DeviceId;
@@ -96,6 +97,7 @@
         fibListener = createMock(FibListener.class);
 
         router = new Router();
+        router.coreService = createNiceMock(CoreService.class);
         router.hostService = hostService;
         router.routingConfigurationService = routingConfigurationService;
         router.bgpService = bgpService;