optical intent module

Change-Id: Ifa405ab1d883b293d349f1b66d26a05a0724691f
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 d888ed3..e44a01b 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
@@ -3,7 +3,7 @@
 import static org.slf4j.LoggerFactory.getLogger;
 
 import java.util.ArrayList;
-
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -12,24 +12,28 @@
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.onos.CoreService;
 import org.onlab.onos.net.ConnectPoint;
 import org.onlab.onos.net.DefaultEdgeLink;
-
 import org.onlab.onos.net.Link;
 import org.onlab.onos.net.Path;
 import org.onlab.onos.net.flow.TrafficSelector;
 import org.onlab.onos.net.flow.TrafficTreatment;
 
-import org.onlab.onos.net.intent.IdGenerator;
 import org.onlab.onos.net.intent.Intent;
 import org.onlab.onos.net.intent.IntentCompiler;
 import org.onlab.onos.net.intent.IntentExtensionService;
-import org.onlab.onos.net.intent.IntentId;
+
 import org.onlab.onos.net.intent.OpticalConnectivityIntent;
 import org.onlab.onos.net.intent.OpticalPathIntent;
-
 import org.onlab.onos.net.provider.ProviderId;
+import org.onlab.onos.net.resource.LinkResourceService;
+import org.onlab.onos.net.topology.LinkWeight;
 import org.onlab.onos.net.topology.PathService;
+import org.onlab.onos.net.topology.Topology;
+import org.onlab.onos.net.topology.TopologyEdge;
+
+import org.onlab.onos.net.topology.TopologyService;
 import org.slf4j.Logger;
 
 /**
@@ -49,14 +53,17 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected PathService pathService;
 
-    // protected LinkResourceService resourceService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected TopologyService topologyService;
 
-    protected IdGenerator<IntentId> intentIdGenerator;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected LinkResourceService resourceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
 
     @Activate
     public void activate() {
-        IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator();
-        intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator);
         intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
     }
 
@@ -67,29 +74,29 @@
 
     @Override
     public List<Intent> compile(OpticalConnectivityIntent intent) {
-        // TO DO: compute multiple paths using the K-shortest path algorithm
+        // TODO: compute multiple paths using the K-shortest path algorithm
+        List<Intent> retList = new ArrayList<>();
         Path path = calculatePath(intent.getSrcConnectPoint(), intent.getDst());
-        log.info("calculate the lightpath: {}.", path.toString());
+        if (path == null) {
+            return retList;
+        } else {
+            log.info("the computed lightpath is : {}.", path.toString());
+        }
 
         List<Link> links = new ArrayList<>();
         links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true));
         links.addAll(path.links());
         links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false));
 
-        // TO DO: choose a wavelength using the first-fit algorithm
         TrafficSelector opticalSelector = null;
         TrafficTreatment opticalTreatment = null;
 
-        List<Intent> retList = new ArrayList<>();
-        int wavelength = assignWavelength(path);
-        log.info("assign the wavelength: {}.", wavelength);
-
         // create a new opticalPathIntent
-        Intent newIntent = new OpticalPathIntent(intentIdGenerator.getNewId(),
-                opticalSelector,
-                opticalTreatment,
+        Intent newIntent = new OpticalPathIntent(intent.appId(),
                 path.src(),
                 path.dst(),
+                opticalSelector,
+                opticalTreatment,
                 path);
 
         retList.add(newIntent);
@@ -97,18 +104,45 @@
         return retList;
     }
 
-    private Path calculatePath(ConnectPoint one, ConnectPoint two) {
-        // TODO: K-shortest path computation algorithm
-        Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
-        if (paths.isEmpty()) {
-            throw new PathNotFoundException("No optical path from " + one + " to " + two);
-        }
-        return paths.iterator().next();
-    }
+    private Path calculatePath(ConnectPoint start, ConnectPoint end) {
+        // TODO: support user policies
+        Topology topology = topologyService.currentTopology();
+        LinkWeight weight = new LinkWeight() {
+            @Override
+            public double weight(TopologyEdge edge) {
+                boolean isOptical = false;
+                String t = edge.link().annotations().value("linkType");
+                if (t.equals("WDM")) {
+                   isOptical = true;
+                }
+                if (isOptical) {
+                    return 1; // optical links
+                } else {
+                    return 10000; // packet links
+                }
+            }
+        };
 
-    private int assignWavelength(Path path) {
-        // TODO: wavelength assignment
-        return 1;
+        Set<Path> paths = topologyService.getPaths(topology,
+                start.deviceId(),
+                end.deviceId(),
+                weight);
+
+        Iterator<Path> itr = paths.iterator();
+        while (itr.hasNext()) {
+            Path path = itr.next();
+            if (path.cost() >= 10000) {
+                itr.remove();
+            }
+        }
+
+        if (paths.isEmpty()) {
+            log.info("No optical path found from " + start + " to " + end);
+            return null;
+        } else {
+            return paths.iterator().next();
+        }
+
     }
 
 }