[CORD-438] Fix Segment Routing application

Changes:
- Discards Link events if one of the switch is down;
- Triggers the paths optimization after device-down event;

Change-Id: I4114f2450401e0d1ff5658d592daf319d977f20e
diff --git a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/DefaultRoutingHandler.java b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/DefaultRoutingHandler.java
index 870ea06..d7e4c3c 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/DefaultRoutingHandler.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/DefaultRoutingHandler.java
@@ -662,6 +662,7 @@
         if (updatedEcmpSpgMap != null) {
             updatedEcmpSpgMap.remove(deviceId);
         }
+        this.populateRoutingRulesForLinkStatusChange(null);
     }
 
     private final class RetryFilters implements Runnable {
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 fce63fe..72b6401 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -693,6 +693,15 @@
                     if (event.type() == LinkEvent.Type.LINK_ADDED) {
                         processLinkAdded((Link) event.subject());
                     } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
+                        Link linkRemoved = (Link) event.subject();
+                        if (linkRemoved.src().elementId() instanceof DeviceId &&
+                                !deviceService.isAvailable(linkRemoved.src().deviceId())) {
+                            continue;
+                        }
+                        if (linkRemoved.dst().elementId() instanceof DeviceId &&
+                                !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
+                            continue;
+                        }
                         processLinkRemoved((Link) event.subject());
                     } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
                             event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||