ONOS-1438: Improved the routing rule population process for link add and failure; computes the routes changed from the link changes and populates the rules only for the routes.

Change-Id: Id4dbd80da37b333f2c19bc97333472dc8031481b
diff --git a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
index b5e1181..08d6220 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -151,8 +151,7 @@
                 groupHandler.createGroups();
                 groupHandlerMap.put(device.id(), groupHandler);
                 log.debug("Initiating default group handling for {}", device.id());
-
-                defaultRoutingHandler.startPopulationProcess();
+                defaultRoutingHandler.populateTtpRules(device.id());
             } else {
                 log.debug("Activate: Local role {} "
                                 + "is not MASTER for device {}",
@@ -162,6 +161,8 @@
             }
         }
 
+        defaultRoutingHandler.startPopulationProcess();
+
         log.info("Started");
     }
 
@@ -239,6 +240,8 @@
             switch (event.type()) {
             case DEVICE_ADDED:
             case PORT_REMOVED:
+            case DEVICE_UPDATED:
+            case DEVICE_AVAILABILITY_CHANGED:
                 scheduleEventHandlerIfNotScheduled(event);
                 break;
             default:
@@ -294,8 +297,12 @@
                     processLinkRemoved((Link) event.subject());
                 } else if (event.type() == GroupEvent.Type.GROUP_ADDED) {
                     processGroupAdded((Group) event.subject());
-                } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
-                    processDeviceAdded((Device) event.subject());
+                } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
+                        event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
+                        event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
+                    if (deviceService.isAvailable(((Device) event.subject()).id())) {
+                        processDeviceAdded((Device) event.subject());
+                    }
                 } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) {
                     processPortRemoved((Device) event.subject(),
                             ((DeviceEvent) event).port());
@@ -321,12 +328,12 @@
                 groupHandler.linkUp(link);
             }
         }
-        defaultRoutingHandler.startPopulationProcess();
+        defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
     }
 
     private void processLinkRemoved(Link link) {
         log.debug("A link {} was removed", link.toString());
-        defaultRoutingHandler.startPopulationProcess();
+        defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
     }