Made SDN-IP's BGP listen port configurable

Change-Id: I98bb6bb2d500087757b57bf0ed9f5c709d459aef
diff --git a/apps/sdnip/pom.xml b/apps/sdnip/pom.xml
index 0cb35d2..1a4899e 100644
--- a/apps/sdnip/pom.xml
+++ b/apps/sdnip/pom.xml
@@ -55,6 +55,11 @@
     </dependency>
 
     <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+    </dependency>
+
+    <dependency>
       <groupId>org.onosproject</groupId>
       <artifactId>onlab-thirdparty</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 e3defdc3..6db770d 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
@@ -18,10 +18,12 @@
 import static org.slf4j.LoggerFactory.getLogger;
 
 import java.util.Collection;
+import java.util.Dictionary;
 
 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;
@@ -38,6 +40,7 @@
 import org.onosproject.sdnip.bgp.BgpSession;
 import org.onosproject.sdnip.bgp.BgpSessionManager;
 import org.onosproject.sdnip.config.SdnIpConfigurationReader;
+import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 
 /**
@@ -65,6 +68,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected LeadershipService leadershipService;
 
+    private static final int DEFAULT_BGP_PORT = 2000;
+    private int bgpPort;
+
     private IntentSynchronizer intentSynchronizer;
     private SdnIpConfigurationReader config;
     private PeerConnectivityManager peerConnectivity;
@@ -76,8 +82,9 @@
     private ControllerNode localControllerNode;
 
     @Activate
-    protected void activate() {
+    protected void activate(ComponentContext context) {
         log.info("SDN-IP started");
+        readComponentConfiguration(context);
 
         appId = coreService.registerApplication(SDN_IP_APP);
         config = new SdnIpConfigurationReader();
@@ -104,9 +111,10 @@
         leadershipService.addListener(leadershipEventListener);
         leadershipService.runForLeadership(appId.name());
 
+        log.info("Starting BGP with port {}", bgpPort);
+
         bgpSessionManager = new BgpSessionManager(router);
-        // TODO: the local BGP listen port number should be configurable
-        bgpSessionManager.start(2000);
+        bgpSessionManager.start(bgpPort);
 
         // TODO need to disable link discovery on external ports
     }
@@ -125,6 +133,33 @@
         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 Collection<BgpSession> getBgpSessions() {
         return bgpSessionManager.getBgpSessions();