[ONOS-4166] Use device capapbility set using network config for path computation

Change-Id: I5f904f3838aafd5d1ab21d335043d9cfcdd2bce2
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java
index 89a685b..aa16f80 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java
@@ -60,6 +60,7 @@
 import org.onosproject.incubator.net.tunnel.TunnelName;
 import org.onosproject.incubator.net.tunnel.TunnelService;
 import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.DefaultAnnotations.Builder;
 import org.onosproject.net.Device;
@@ -92,6 +93,7 @@
 import org.onosproject.pce.pcestore.PcePathInfo;
 import org.onosproject.pce.pcestore.PceccTunnelInfo;
 import org.onosproject.pce.pcestore.api.PceStore;
+import org.onosproject.pcep.api.DeviceCapability;
 import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.DistributedSet;
 import org.onosproject.store.service.Serializer;
@@ -176,6 +178,9 @@
     protected DeviceService deviceService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigService netCfgService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected LabelResourceAdminService labelRsrcAdminService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -304,6 +309,14 @@
             return false;
         }
 
+        // Get device config from netconfig, to ascertain that session with ingress is present.
+        DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(srcLsrId), DeviceCapability.class);
+        if (cfg == null) {
+            log.debug("No session to ingress.");
+            pceStore.addFailedPathInfo(new PcePathInfo(src, dst, tunnelName, constraints, lspType));
+            return false;
+        }
+
         TunnelEndPoint srcEndPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(srcLsrId));
         TunnelEndPoint dstEndPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dstLsrId));
 
@@ -441,7 +454,7 @@
                     bwConstraintValue = bwConstraint.bandwidth().bps();
                 } else if (constraint instanceof CostConstraint) {
                     costConstraint = (CostConstraint) constraint;
-                costType = costConstraint.type().name();
+                    costType = costConstraint.type().name();
                 }
             }
 
@@ -621,7 +634,8 @@
             while (it.hasNext() && cost > 0) {
                 Constraint constraint = it.next();
                 if (constraint instanceof CapabilityConstraint) {
-                    cost = ((CapabilityConstraint) constraint).isValidLink(edge.link(), deviceService) ? 1 : -1;
+                    cost = ((CapabilityConstraint) constraint).isValidLink(edge.link(), deviceService,
+                                                                           netCfgService) ? 1 : -1;
                 } else {
                     cost = constraint.cost(edge.link(), resourceService::isAvailable);
                 }
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceccSrTeBeHandler.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceccSrTeBeHandler.java
index dee4747..24413c1 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceccSrTeBeHandler.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceccSrTeBeHandler.java
@@ -406,6 +406,7 @@
 
     /**
      * Install a rule for pushing unique global labels to the device.
+     *
      * @param deviceId device to which flow should be pushed
      * @param labelId label for the device
      * @param type type of operation
@@ -437,6 +438,7 @@
 
     /**
      * Install a rule for pushing node labels to the device of other nodes.
+     *
      * @param deviceId device to which flow should be pushed
      * @param labelId label for the device
      * @param ipPrefix device for which label is pushed
@@ -474,7 +476,8 @@
     }
 
     /**
-     *  Install a rule for pushing Adjacency labels to the device.
+     * Install a rule for pushing Adjacency labels to the device.
+     *
      * @param deviceId device to which flow should be pushed
      * @param labelId label for the adjacency
      * @param srcPortNum local port of the adjacency
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/constraint/CapabilityConstraint.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/constraint/CapabilityConstraint.java
index a479c38..e123eea 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/constraint/CapabilityConstraint.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/constraint/CapabilityConstraint.java
@@ -15,12 +15,14 @@
  */
 package org.onosproject.pce.pceservice.constraint;
 
-import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
+import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.intent.ResourceContext;
 import org.onosproject.net.intent.constraint.BooleanConstraint;
+import org.onosproject.pcep.api.DeviceCapability;
 
 import java.util.Objects;
 
@@ -32,11 +34,7 @@
 public final class CapabilityConstraint extends BooleanConstraint {
 
     private final CapabilityType capabilityType;
-    public static final String PCECC_CAPABILITY = "pceccCapability";
-    public static final String SR_CAPABILITY = "srCapability";
-    public static final String LABEL_STACK_CAPABILITY = "labelStackCapability";
     public static final String LSRID = "lsrId";
-    public static final String L3 = "L3";
     public static final String TRUE = "true";
 
     /**
@@ -117,45 +115,33 @@
      *
      * @param link to validate source and destination based on capability constraint
      * @param deviceService instance of DeviceService
+     * @param netCfgService instance of NetworkConfigService
      * @return true if link satisfies capability constraint otherwise false
      */
-    public boolean isValidLink(Link link, DeviceService deviceService) {
-        if (deviceService == null) {
+    public boolean isValidLink(Link link, DeviceService deviceService, NetworkConfigService netCfgService) {
+        if (deviceService == null || netCfgService == null) {
             return false;
         }
 
         Device srcDevice = deviceService.getDevice(link.src().deviceId());
         Device dstDevice = deviceService.getDevice(link.dst().deviceId());
 
-        //TODO: Usage of annotations are for transient solution. In future will be replaces with the
+        //TODO: Usage of annotations are for transient solution. In future will be replaced with the
         // network config service / Projection model.
         // L3 device
-        if (srcDevice == null
-                || dstDevice == null
-                || srcDevice.annotations().value(AnnotationKeys.TYPE) == null
-                || dstDevice.annotations().value(AnnotationKeys.TYPE) == null
-                || !srcDevice.annotations().value(AnnotationKeys.TYPE).equals(L3)
-                || !dstDevice.annotations().value(AnnotationKeys.TYPE).equals(L3)) {
+        if (srcDevice == null || dstDevice == null) {
             return false;
         }
 
-        String scrLsrId = srcDevice.annotations().value(LSRID);
+        String srcLsrId = srcDevice.annotations().value(LSRID);
         String dstLsrId = dstDevice.annotations().value(LSRID);
 
-        Device srcCapDevice = null;
-        Device dstCapDevice = null;
+        DeviceCapability srcDeviceConfig = netCfgService.getConfig(DeviceId.deviceId(srcLsrId),
+                                                                       DeviceCapability.class);
+        DeviceCapability dstDeviceConfig = netCfgService.getConfig(DeviceId.deviceId(dstLsrId),
+                                                                       DeviceCapability.class);
 
-        // Get Capability device
-        Iterable<Device> devices = deviceService.getAvailableDevices();
-        for (Device dev : devices) {
-            if (dev.annotations().value(LSRID).equals(scrLsrId)) {
-                srcCapDevice = dev;
-            } else if (dev.annotations().value(LSRID).equals(dstLsrId)) {
-                dstCapDevice = dev;
-            }
-        }
-
-        if (srcCapDevice == null || dstCapDevice == null) {
+        if (srcDeviceConfig == null || dstDeviceConfig == null) {
             return false;
         }
 
@@ -163,23 +149,11 @@
         case WITH_SIGNALLING:
             return true;
         case WITHOUT_SIGNALLING_AND_WITHOUT_SR:
-            if (srcCapDevice.annotations().value(PCECC_CAPABILITY) != null
-                    && dstCapDevice.annotations().value(PCECC_CAPABILITY) != null) {
-                return srcCapDevice.annotations().value(PCECC_CAPABILITY).equals(TRUE)
-                        && dstCapDevice.annotations().value(PCECC_CAPABILITY).equals(TRUE);
-            }
-            return false;
+            return srcDeviceConfig.localLabelCap() && dstDeviceConfig.localLabelCap();
+
         case SR_WITHOUT_SIGNALLING:
-            if (srcCapDevice.annotations().value(LABEL_STACK_CAPABILITY) != null
-                    && dstCapDevice.annotations().value(LABEL_STACK_CAPABILITY) != null
-                    && srcCapDevice.annotations().value(SR_CAPABILITY) != null
-                    && dstCapDevice.annotations().value(SR_CAPABILITY) != null) {
-                return srcCapDevice.annotations().value(LABEL_STACK_CAPABILITY).equals(TRUE)
-                        && dstCapDevice.annotations().value(LABEL_STACK_CAPABILITY).equals(TRUE)
-                        && srcCapDevice.annotations().value(SR_CAPABILITY).equals(TRUE)
-                        && dstCapDevice.annotations().value(SR_CAPABILITY).equals(TRUE);
-            }
-            return false;
+            return srcDeviceConfig.srCap() && dstDeviceConfig.srCap()
+                        && srcDeviceConfig.labelStackCap() && dstDeviceConfig.labelStackCap();
         default:
             return false;
         }