Pushes flow rules to the control switch when the switch is connected and available.

Change-Id: I7cc03a44e21b9dd67d40139ceea7a9b44201d7a8
diff --git a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
index 0d564f1..1c02578 100644
--- a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
+++ b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
@@ -381,15 +381,21 @@
                 case DEVICE_AVAILABILITY_CHANGED:
                     if (deviceService.isAvailable(event.subject().id())) {
                         log.info("Device connected {}", event.subject().id());
-                        processIntfFilters(true, configService.getInterfaces());
+                        if (event.subject().id().equals(deviceId)) {
+                            processIntfFilters(true, configService.getInterfaces());
 
-                        /* For test only - will be removed before Cardinal release */
-                        delay(1000);
-                        FibEntry fibEntry = new FibEntry(Ip4Prefix.valueOf("10.1.0.0/16"),
-                                                         Ip4Address.valueOf("192.168.10.1"),
-                                                         MacAddress.valueOf("DE:AD:BE:EF:FE:ED"));
-                        FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry);
-                        updateFibEntry(Collections.singletonList(fibUpdate));
+                            /* For test only - will be removed before Cardinal release */
+                            delay(1000);
+                            FibEntry fibEntry = new FibEntry(Ip4Prefix.valueOf("10.1.0.0/16"),
+                                    Ip4Address.valueOf("192.168.10.1"),
+                                    MacAddress.valueOf("DE:AD:BE:EF:FE:ED"));
+                            FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry);
+                            updateFibEntry(Collections.singletonList(fibUpdate));
+                        }
+
+                        if (event.subject().id().equals(ctrlDeviceId)) {
+                            connectivityManager.notifySwitchAvailable();
+                        }
                     }
                     break;
 
diff --git a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/TunnellingConnectivityManager.java b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/TunnellingConnectivityManager.java
index 7d8ca7c..39b248c 100644
--- a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/TunnellingConnectivityManager.java
+++ b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/TunnellingConnectivityManager.java
@@ -81,6 +81,23 @@
 
         this.bgpSpeaker = bgpSpeaker;
 
+    }
+
+    public void start() {
+        packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 3);
+    }
+
+    public void stop() {
+        packetService.removeProcessor(processor);
+        // Should revoke packet requests in the future
+    }
+
+    /**
+     * Pushes the flow rules for forwarding BGP TCP packets to controller.
+     * It is called when switches are connected and available.
+     */
+    public void notifySwitchAvailable() {
+        // control plane OVS is available, push default flows
         TrafficSelector selectorDst = DefaultTrafficSelector.builder()
                 .matchEthType(Ethernet.TYPE_IPV4)
                 .matchIPProtocol(IPv4.PROTOCOL_TCP)
@@ -105,7 +122,7 @@
                 .withFlag(ForwardingObjective.Flag.VERSATILE)
                 .add();
         flowObjectiveService.forward(bgpSpeaker.connectPoint().deviceId(),
-                                     puntSrc);
+                puntSrc);
 
         ForwardingObjective puntDst = DefaultForwardingObjective.builder()
                 .fromApp(appId)
@@ -115,18 +132,8 @@
                 .withFlag(ForwardingObjective.Flag.VERSATILE)
                 .add();
         flowObjectiveService.forward(bgpSpeaker.connectPoint().deviceId(),
-                                     puntDst);
+                puntDst);
         log.info("Sent punt forwarding objective to {}", bgpSpeaker.connectPoint().deviceId());
-
-    }
-
-    public void start() {
-        packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 3);
-    }
-
-    public void stop() {
-        packetService.removeProcessor(processor);
-        // Should revoke packet requests in the future
     }
 
     /**