Move BGP listen port configuration to BgpSessionManager component

Change-Id: I445bff180740fededacaa59441fc53332cc3d482
diff --git a/apps/routing-api/src/main/java/org/onosproject/routingapi/BgpService.java b/apps/routing-api/src/main/java/org/onosproject/routingapi/BgpService.java
index c7e58d4..3901b22 100644
--- a/apps/routing-api/src/main/java/org/onosproject/routingapi/BgpService.java
+++ b/apps/routing-api/src/main/java/org/onosproject/routingapi/BgpService.java
@@ -24,9 +24,8 @@
      * Starts the BGP service.
      *
      * @param routeListener listener to send route updates to
-     * @param bgpPort port number to listen on
      */
-    void start(RouteListener routeListener, int bgpPort);
+    void start(RouteListener routeListener);
 
     /**
      * Stops the BGP service.
diff --git a/apps/routing/pom.xml b/apps/routing/pom.xml
index 8656ebb..055d0b8 100644
--- a/apps/routing/pom.xml
+++ b/apps/routing/pom.xml
@@ -55,6 +55,11 @@
         </dependency>
 
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.karaf.shell</groupId>
             <artifactId>org.apache.karaf.shell.console</artifactId>
         </dependency>
diff --git a/apps/routing/src/main/java/org/onosproject/routing/Router.java b/apps/routing/src/main/java/org/onosproject/routing/Router.java
index 494c190..1db8e0b 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/Router.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/Router.java
@@ -124,7 +124,7 @@
         this.fibComponent = checkNotNull(listener);
         this.hostService.addListener(hostListener);
 
-        bgpService.start(new InternalRouteListener(), 2000);
+        bgpService.start(new InternalRouteListener());
 
         bgpUpdatesExecutor.execute(new Runnable() {
             @Override
diff --git a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSessionManager.java b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSessionManager.java
index cfd0081..5a16313 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSessionManager.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSessionManager.java
@@ -15,7 +15,11 @@
  */
 package org.onosproject.routing.bgp;
 
+import org.osgi.service.component.ComponentContext;
+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.Service;
 import org.jboss.netty.bootstrap.ServerBootstrap;
 import org.jboss.netty.channel.Channel;
@@ -40,6 +44,7 @@
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.util.Collection;
+import java.util.Dictionary;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -72,6 +77,47 @@
 
     private RouteListener routeListener;
 
+    private static final int DEFAULT_BGP_PORT = 2000;
+    private int bgpPort;
+
+    @Activate
+    protected void activate(ComponentContext context) {
+        readComponentConfiguration(context);
+        log.info("BgpSessionManager started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        log.info("BgpSessionManager stopped");
+    }
+
+    /**
+     * Extracts properties from the component configuration context.
+     *
+     * @param context the component context
+     */
+    private void readComponentConfiguration(ComponentContext context) {
+        Dictionary<?, ?> properties = context.getProperties();
+        try {
+            String strPort = (String) properties.get("bgpPort");
+            if (strPort != null) {
+                bgpPort = Integer.parseInt(strPort);
+            } else {
+                bgpPort = DEFAULT_BGP_PORT;
+            }
+        } catch (Exception e) {
+            bgpPort = DEFAULT_BGP_PORT;
+        }
+        log.debug("BGP port is set to {}", bgpPort);
+    }
+
+    @Modified
+    public void modified(ComponentContext context) {
+        // Blank @Modified method to catch modifications to the context.
+        // If no @Modified method exists, it seems @Activate is called again
+        // when the context is modified.
+    }
+
     /**
      * Checks whether the BGP Session Manager is shutdown.
      *
@@ -245,7 +291,7 @@
     }
 
     @Override
-    public void start(RouteListener routeListener, int listenPortNumber) {
+    public void start(RouteListener routeListener) {
         log.debug("BGP Session Manager start.");
         isShutdown = false;
 
@@ -271,7 +317,7 @@
             }
         };
         InetSocketAddress listenAddress =
-                new InetSocketAddress(listenPortNumber);
+                new InetSocketAddress(bgpPort);
 
         serverBootstrap = new ServerBootstrap(channelFactory);
         // serverBootstrap.setOptions("reuseAddr", true);
diff --git a/apps/routing/src/test/java/org/onosproject/routing/RouterAsyncArpTest.java b/apps/routing/src/test/java/org/onosproject/routing/RouterAsyncArpTest.java
index d9a5ce9..54a21ee 100644
--- a/apps/routing/src/test/java/org/onosproject/routing/RouterAsyncArpTest.java
+++ b/apps/routing/src/test/java/org/onosproject/routing/RouterAsyncArpTest.java
@@ -78,7 +78,7 @@
         hostService = createMock(HostService.class);
 
         BgpService bgpService = createMock(BgpService.class);
-        bgpService.start(anyObject(RouteListener.class), anyInt());
+        bgpService.start(anyObject(RouteListener.class));
         bgpService.stop();
         replay(bgpService);
 
diff --git a/apps/routing/src/test/java/org/onosproject/routing/RouterTest.java b/apps/routing/src/test/java/org/onosproject/routing/RouterTest.java
index 176c159..26ed60a 100644
--- a/apps/routing/src/test/java/org/onosproject/routing/RouterTest.java
+++ b/apps/routing/src/test/java/org/onosproject/routing/RouterTest.java
@@ -84,7 +84,7 @@
         setUpHostService();
 
         BgpService bgpService = createMock(BgpService.class);
-        bgpService.start(anyObject(RouteListener.class), anyInt());
+        bgpService.start(anyObject(RouteListener.class));
         bgpService.stop();
         replay(bgpService);
 
diff --git a/apps/routing/src/test/java/org/onosproject/routing/bgp/BgpSessionManagerTest.java b/apps/routing/src/test/java/org/onosproject/routing/bgp/BgpSessionManagerTest.java
index c73eaff..c9c52b1 100644
--- a/apps/routing/src/test/java/org/onosproject/routing/bgp/BgpSessionManagerTest.java
+++ b/apps/routing/src/test/java/org/onosproject/routing/bgp/BgpSessionManagerTest.java
@@ -35,16 +35,21 @@
 import org.onlab.packet.Ip4Prefix;
 import org.onosproject.routingapi.RouteListener;
 import org.onosproject.routingapi.RouteUpdate;
+import org.osgi.service.component.ComponentContext;
 
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Dictionary;
 import java.util.LinkedList;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
 import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
@@ -252,7 +257,14 @@
         //
         bgpSessionManager = new BgpSessionManager();
         // NOTE: We use port 0 to bind on any available port
-        bgpSessionManager.start(dummyRouteListener, 0);
+        ComponentContext componentContext = createMock(ComponentContext.class);
+        Dictionary<String, String> dictionary = createMock(Dictionary.class);
+        expect(dictionary.get("bgpPort")).andReturn("0");
+        replay(dictionary);
+        expect(componentContext.getProperties()).andReturn(dictionary);
+        replay(componentContext);
+        bgpSessionManager.activate(componentContext);
+        bgpSessionManager.start(dummyRouteListener);
 
         // Get the port number the BGP Session Manager is listening on
         Channel serverChannel = TestUtils.getField(bgpSessionManager,
diff --git a/apps/sdnip/pom.xml b/apps/sdnip/pom.xml
index 925bc58..48a486e 100644
--- a/apps/sdnip/pom.xml
+++ b/apps/sdnip/pom.xml
@@ -55,11 +55,6 @@
     </dependency>
 
     <dependency>
-      <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.compendium</artifactId>
-    </dependency>
-
-    <dependency>
       <groupId>org.onosproject</groupId>
       <artifactId>onlab-misc</artifactId>
     </dependency>
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
index b375852..2f24e15 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
@@ -18,7 +18,6 @@
 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.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
@@ -34,11 +33,8 @@
 import org.onosproject.net.intent.IntentService;
 import org.onosproject.routingapi.RoutingService;
 import org.onosproject.sdnip.config.SdnIpConfigurationReader;
-import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 
-import java.util.Dictionary;
-
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -77,9 +73,6 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigService networkConfigService;
 
-    private static final int DEFAULT_BGP_PORT = 2000;
-    private int bgpPort;
-
     private IntentSynchronizer intentSynchronizer;
     private SdnIpConfigurationReader config;
     private PeerConnectivityManager peerConnectivity;
@@ -90,9 +83,8 @@
     private ControllerNode localControllerNode;
 
     @Activate
-    protected void activate(ComponentContext context) {
+    protected void activate() {
         log.info("SDN-IP started");
-        readComponentConfiguration(context);
 
         appId = coreService.registerApplication(SDN_IP_APP);
         config = new SdnIpConfigurationReader();
@@ -117,9 +109,6 @@
 
         leadershipService.addListener(leadershipEventListener);
         leadershipService.runForLeadership(appId.name());
-
-        log.info("Starting BGP with port {}", bgpPort);
-        // TODO feed port information through to the BgpService
     }
 
     @Deactivate
@@ -134,33 +123,6 @@
         log.info("SDN-IP Stopped");
     }
 
-    /**
-     * Extracts properties from the component configuration context.
-     *
-     * @param context the component context
-     */
-    private void readComponentConfiguration(ComponentContext context) {
-        Dictionary<?, ?> properties = context.getProperties();
-        try {
-            String strPort = (String) properties.get("bgpPort");
-            if (strPort != null) {
-                bgpPort = Integer.parseInt(strPort);
-            } else {
-                bgpPort = DEFAULT_BGP_PORT;
-            }
-        } catch (Exception e) {
-            bgpPort = DEFAULT_BGP_PORT;
-        }
-        log.debug("BGP port is set to {}", bgpPort);
-    }
-
-    @Modified
-    public void modified(ComponentContext context) {
-        // Blank @Modified method to catch modifications to the context.
-        // If no @Modified method exists, it seems @Activate is called again
-        // when the context is modified.
-    }
-
     @Override
     public void modifyPrimary(boolean isPrimary) {
         intentSynchronizer.leaderChanged(isPrimary);