Fix for optical re-reoute

Change-Id: Iad3ca0e175cb76f66ac276981f4e36bb580566c8
diff --git a/apps/optical/src/main/java/org/onlab/onos/optical/OpticalPathProvisioner.java b/apps/optical/src/main/java/org/onlab/onos/optical/OpticalPathProvisioner.java
index 1ad55ae..5aaa359 100644
--- a/apps/optical/src/main/java/org/onlab/onos/optical/OpticalPathProvisioner.java
+++ b/apps/optical/src/main/java/org/onlab/onos/optical/OpticalPathProvisioner.java
@@ -206,6 +206,9 @@
     private static class OpticalLinkWeight implements LinkWeight {
         @Override
         public double weight(TopologyEdge edge) {
+            if (edge.link().state() == Link.State.INACTIVE) {
+                return -1; // ignore inactive links
+            }
             if (isOpticalLink(edge.link())) {
                 return 1000.0;  // optical links
             } else {
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/OpticalPathIntent.java b/core/api/src/main/java/org/onlab/onos/net/intent/OpticalPathIntent.java
index 92017f1..71a9187 100644
--- a/core/api/src/main/java/org/onlab/onos/net/intent/OpticalPathIntent.java
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/OpticalPathIntent.java
@@ -37,7 +37,7 @@
             ConnectPoint src,
             ConnectPoint dst,
             Path path) {
-        super(id(OpticalPathIntent.class, src, dst),
+        super(id(OpticalPathIntent.class, src, dst, path),
               appId,
               ImmutableSet.<NetworkResource>copyOf(path.links()));
         this.src = src;
@@ -78,6 +78,7 @@
                 .toString();
     }
 
+
     public Collection<Link> requiredLinks() {
         return path.links();
     }
diff --git a/core/net/src/main/java/org/onlab/onos/net/intent/impl/ObjectiveTracker.java b/core/net/src/main/java/org/onlab/onos/net/intent/impl/ObjectiveTracker.java
index 53b1239..c984632 100644
--- a/core/net/src/main/java/org/onlab/onos/net/intent/impl/ObjectiveTracker.java
+++ b/core/net/src/main/java/org/onlab/onos/net/intent/impl/ObjectiveTracker.java
@@ -45,6 +45,7 @@
 import static java.util.concurrent.Executors.newSingleThreadExecutor;
 import static org.onlab.onos.net.LinkKey.linkKey;
 import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED;
+import static org.onlab.onos.net.link.LinkEvent.Type.LINK_UPDATED;
 import static org.onlab.util.Tools.namedThreads;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -152,13 +153,18 @@
                 for (Event reason : event.reasons()) {
                     if (reason instanceof LinkEvent) {
                         LinkEvent linkEvent = (LinkEvent) reason;
-                        if (linkEvent.type() == LINK_REMOVED) {
+                        if (linkEvent.type() == LINK_REMOVED
+                                || (linkEvent.type() == LINK_UPDATED &&
+                                        linkEvent.subject().isDurable())) {
                             final LinkKey linkKey = linkKey(linkEvent.subject());
                             Set<IntentId> intentIds = intentsByLink.get(linkKey);
                             log.debug("recompile triggered by LinkDown {} {}", linkKey, intentIds);
                             toBeRecompiled.addAll(intentIds);
                         }
-                        recompileOnly = recompileOnly && linkEvent.type() == LINK_REMOVED;
+                        recompileOnly = recompileOnly &&
+                                (linkEvent.type() == LINK_REMOVED ||
+                                (linkEvent.type() == LINK_UPDATED &&
+                                linkEvent.subject().isDurable()));
                     }
                 }
 
diff --git a/core/net/src/main/java/org/onlab/onos/net/intent/impl/OpticalConnectivityIntentCompiler.java b/core/net/src/main/java/org/onlab/onos/net/intent/impl/OpticalConnectivityIntentCompiler.java
index e5a1203..85c9f29 100644
--- a/core/net/src/main/java/org/onlab/onos/net/intent/impl/OpticalConnectivityIntentCompiler.java
+++ b/core/net/src/main/java/org/onlab/onos/net/intent/impl/OpticalConnectivityIntentCompiler.java
@@ -79,6 +79,9 @@
         LinkWeight weight = new LinkWeight() {
             @Override
             public double weight(TopologyEdge edge) {
+                if (edge.link().state() == Link.State.INACTIVE) {
+                    return -1;
+                }
                 return edge.link().type() == Link.Type.OPTICAL ? +1 : -1;
             }
         };
@@ -86,7 +89,8 @@
         Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
                                                    end.deviceId(), weight);
         if (paths.isEmpty()) {
-            throw new PathNotFoundException("No fiber path from " + start + " to " + end);
+            throw new PathNotFoundException("No Optical path found from " +
+                                                    start + " to " + end);
         }
 
         // TODO: let's be more intelligent about this eventually
diff --git a/core/net/src/main/java/org/onlab/onos/net/link/impl/LinkManager.java b/core/net/src/main/java/org/onlab/onos/net/link/impl/LinkManager.java
index 15fc4a1..76ce64a 100644
--- a/core/net/src/main/java/org/onlab/onos/net/link/impl/LinkManager.java
+++ b/core/net/src/main/java/org/onlab/onos/net/link/impl/LinkManager.java
@@ -256,8 +256,19 @@
     }
 
     // Removes all links in the specified set and emits appropriate events.
-    private void removeLinks(Set<Link> links, boolean isSoftRemove) {
+    private void  removeLinks(Set<Link> links, boolean isSoftRemove) {
         for (Link link : links) {
+            if (!deviceService.getDevice(link.src().deviceId()).type().equals(
+                    deviceService.getDevice(link.dst().deviceId()).type())) {
+                //TODO this is aweful. need to be fixed so that we don't down
+                // configured links. perhaps add a mechanism to figure out the
+                // state of this link
+                log.info("Ignoring removal of link as device types are " +
+                                 "different {} {} ",
+                         link.src() ,
+                         link.dst());
+                continue;
+            }
             LinkEvent event = isSoftRemove ?
                     store.removeOrDownLink(link.src(), link.dst()) :
                     store.removeLink(link.src(), link.dst());
diff --git a/core/net/src/test/java/org/onlab/onos/net/link/impl/LinkManagerTest.java b/core/net/src/test/java/org/onlab/onos/net/link/impl/LinkManagerTest.java
index 2cab8fe..665a550 100644
--- a/core/net/src/test/java/org/onlab/onos/net/link/impl/LinkManagerTest.java
+++ b/core/net/src/test/java/org/onlab/onos/net/link/impl/LinkManagerTest.java
@@ -21,6 +21,7 @@
 import org.junit.Test;
 import org.onlab.onos.event.Event;
 import org.onlab.onos.net.ConnectPoint;
+import org.onlab.onos.net.DefaultDevice;
 import org.onlab.onos.net.Device;
 import org.onlab.onos.net.DeviceId;
 import org.onlab.onos.net.Link;
@@ -41,8 +42,10 @@
 import org.onlab.onos.store.trivial.impl.SimpleLinkStore;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import static org.junit.Assert.*;
@@ -60,11 +63,17 @@
     private static final DeviceId DID1 = deviceId("of:foo");
     private static final DeviceId DID2 = deviceId("of:bar");
     private static final DeviceId DID3 = deviceId("of:goo");
+    private static final Device DEV1 = new DefaultDevice(
+            PID, DID1, Device.Type.SWITCH, "", "", "", "", null);
+    private static final Device DEV2 = new DefaultDevice(
+            PID, DID2, Device.Type.SWITCH, "", "", "", "", null);
+    private static final Device DEV3 = new DefaultDevice(
+            PID, DID2, Device.Type.SWITCH, "", "", "", "", null);
 
     private static final PortNumber P1 = PortNumber.portNumber(1);
     private static final PortNumber P2 = PortNumber.portNumber(2);
     private static final PortNumber P3 = PortNumber.portNumber(3);
-
+    private static final Map<DeviceId, Device> DEVICEIDMAP = new HashMap<>();
 
     private LinkManager mgr;
 
@@ -76,6 +85,7 @@
     protected TestListener listener = new TestListener();
     protected DeviceManager devmgr = new TestDeviceManager();
 
+
     @Before
     public void setUp() {
         mgr = new LinkManager();
@@ -87,6 +97,10 @@
         mgr.deviceService = devmgr;
         mgr.activate();
 
+        DEVICEIDMAP.put(DID1, DEV1);
+        DEVICEIDMAP.put(DID2, DEV2);
+        DEVICEIDMAP.put(DID3, DEV3);
+
         service.addListener(listener);
 
         provider = new TestProvider();
@@ -276,10 +290,17 @@
     }
 
     private static class TestDeviceManager extends DeviceManager {
+
         @Override
         public MastershipRole getRole(DeviceId deviceId) {
             return MastershipRole.MASTER;
         }
+
+        @Override
+        public Device getDevice(DeviceId deviceId) {
+            return DEVICEIDMAP.get(deviceId);
+        }
+
     }
 
 }