Add priority to Intents

Change-Id: Ibe63356f5b15a6aa6ca7731dba3382c3317a95ec
diff --git a/core/api/src/main/java/org/onosproject/net/intent/ConnectivityIntent.java b/core/api/src/main/java/org/onosproject/net/intent/ConnectivityIntent.java
index 305d103..23e6c02 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/ConnectivityIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/ConnectivityIntent.java
@@ -61,7 +61,8 @@
                                  Collection<NetworkResource> resources,
                                  TrafficSelector selector,
                                  TrafficTreatment treatment) {
-        this(appId, null, resources, selector, treatment, Collections.emptyList());
+        this(appId, null, resources, selector, treatment, Collections.emptyList(),
+                DEFAULT_INTENT_PRIORITY);
     }
 
     /**
@@ -83,7 +84,8 @@
                                  Collection<NetworkResource> resources,
                                  TrafficSelector selector,
                                  TrafficTreatment treatment) {
-        this(appId, key, resources, selector, treatment, Collections.emptyList());
+        this(appId, key, resources, selector, treatment, Collections.emptyList(),
+                DEFAULT_INTENT_PRIORITY);
     }
 
     /**
@@ -99,6 +101,7 @@
      * @param selector    traffic selector
      * @param treatment   treatment
      * @param constraints optional prioritized list of constraints
+     * @param priority    priority to use for flows generated by this intent
      * @throws NullPointerException if the selector or treatment is null
      */
 
@@ -107,8 +110,9 @@
                                  Collection<NetworkResource> resources,
                                  TrafficSelector selector,
                                  TrafficTreatment treatment,
-                                 List<Constraint> constraints) {
-        super(appId, key, resources);
+                                 List<Constraint> constraints,
+                                 int priority) {
+        super(appId, key, resources, priority);
         this.selector = checkNotNull(selector);
         this.treatment = checkNotNull(treatment);
         this.constraints = checkNotNull(constraints);
@@ -126,6 +130,7 @@
      * @param selector    traffic selector
      * @param treatment   treatment
      * @param constraints optional prioritized list of constraints
+     * @param priority    priority to use for flows generated by this intent
      * @throws NullPointerException if the selector or treatment is null
      */
 
@@ -133,8 +138,9 @@
                                  Collection<NetworkResource> resources,
                                  TrafficSelector selector,
                                  TrafficTreatment treatment,
-                                 List<Constraint> constraints) {
-        super(appId, null, resources);
+                                 List<Constraint> constraints,
+                                 int priority) {
+        super(appId, null, resources, priority);
         this.selector = checkNotNull(selector);
         this.treatment = checkNotNull(treatment);
         this.constraints = checkNotNull(constraints);
diff --git a/core/api/src/main/java/org/onosproject/net/intent/HostToHostIntent.java b/core/api/src/main/java/org/onosproject/net/intent/HostToHostIntent.java
index d47594a..d9a1feb 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/HostToHostIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/HostToHostIntent.java
@@ -106,7 +106,8 @@
                             TrafficSelector selector,
                             TrafficTreatment treatment,
                             List<Constraint> constraints) {
-        super(appId, key, Collections.emptyList(), selector, treatment, constraints);
+        super(appId, key, Collections.emptyList(), selector, treatment, constraints,
+                DEFAULT_INTENT_PRIORITY);
 
         // TODO: consider whether the case one and two are same is allowed
         this.one = checkNotNull(one);
@@ -146,6 +147,7 @@
                 .add("id", id())
                 .add("key", key())
                 .add("appId", appId())
+                .add("priority", priority())
                 .add("resources", resources())
                 .add("selector", selector())
                 .add("treatment", treatment())
diff --git a/core/api/src/main/java/org/onosproject/net/intent/Intent.java b/core/api/src/main/java/org/onosproject/net/intent/Intent.java
index 4cca45d..7621c7d 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/Intent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/Intent.java
@@ -22,6 +22,7 @@
 import java.util.Collection;
 import java.util.Objects;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
@@ -38,6 +39,11 @@
     private final ApplicationId appId;
     private final Key key;
 
+    private final int priority;
+    public static final int DEFAULT_INTENT_PRIORITY = 100;
+    public static final int MAX_PRIORITY = (1 << 16) - 1;
+    public static final int MIN_PRIORITY = 1;
+
     private final Collection<NetworkResource> resources;
 
     private static IdGenerator idGenerator;
@@ -50,6 +56,7 @@
         this.appId = null;
         this.key = null;
         this.resources = null;
+        this.priority = DEFAULT_INTENT_PRIORITY;
     }
 
     /**
@@ -60,7 +67,7 @@
      */
     protected Intent(ApplicationId appId,
                      Collection<NetworkResource> resources) {
-        this(appId, null, resources);
+        this(appId, null, resources, DEFAULT_INTENT_PRIORITY);
     }
 
         /**
@@ -72,11 +79,14 @@
          */
     protected Intent(ApplicationId appId,
                      Key key,
-                     Collection<NetworkResource> resources) {
+                     Collection<NetworkResource> resources,
+                     int priority) {
         checkState(idGenerator != null, "Id generator is not bound.");
+        checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY);
         this.id = IntentId.valueOf(idGenerator.getNewId());
         this.appId = checkNotNull(appId, "Application ID cannot be null");
         this.key = (key != null) ? key : Key.of(id.fingerprint(), appId);
+        this.priority = priority;
         this.resources = checkNotNull(resources);
     }
 
@@ -99,6 +109,15 @@
     }
 
     /**
+     * Returns the priority of the intent.
+     *
+     * @return intent priority
+     */
+    public int priority() {
+        return priority;
+    }
+
+    /**
      * Returns the collection of resources required for this intent.
      *
      * @return collection of resources; may be null
diff --git a/core/api/src/main/java/org/onosproject/net/intent/LinkCollectionIntent.java b/core/api/src/main/java/org/onosproject/net/intent/LinkCollectionIntent.java
index 55c0cfb..17451cb 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/LinkCollectionIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/LinkCollectionIntent.java
@@ -59,7 +59,7 @@
                                 ConnectPoint ingressPoint,
                                 ConnectPoint egressPoint) {
         this(appId, selector, treatment, links, ingressPoint, egressPoint,
-             Collections.emptyList());
+             Collections.emptyList(), DEFAULT_INTENT_PRIORITY);
     }
 
     /**
@@ -74,6 +74,7 @@
      * @param ingressPoint ingress point
      * @param egressPoint egress point
      * @param constraints optional list of constraints
+     * @param priority    priority to use for the flows generated by this intent
      * @throws NullPointerException {@code path} is null
      */
     public LinkCollectionIntent(ApplicationId appId,
@@ -82,8 +83,9 @@
                                 Set<Link> links,
                                 ConnectPoint ingressPoint,
                                 ConnectPoint egressPoint,
-                                List<Constraint> constraints) {
-        super(appId, resources(links), selector, treatment, constraints);
+                                List<Constraint> constraints,
+                                int priority) {
+        super(appId, resources(links), selector, treatment, constraints, priority);
         this.links = links;
         this.ingressPoints = ImmutableSet.of(ingressPoint);
         this.egressPoints = ImmutableSet.of(egressPoint);
@@ -101,6 +103,7 @@
      * @param ingressPoints Set of ingress points
      * @param egressPoints Set of egress points
      * @param constraints  the constraints
+     * @param priority     priority to use for the flows generated by this intent
      * @throws NullPointerException {@code path} is null
      */
     public LinkCollectionIntent(ApplicationId appId,
@@ -109,8 +112,9 @@
                                 Set<Link> links,
                                 Set<ConnectPoint> ingressPoints,
                                 Set<ConnectPoint> egressPoints,
-                                List<Constraint> constraints) {
-        super(appId, resources(links), selector, treatment, constraints);
+                                List<Constraint> constraints,
+                                int priority) {
+        super(appId, resources(links), selector, treatment, constraints, priority);
 
         this.links = links;
         this.ingressPoints = ImmutableSet.copyOf(ingressPoints);
@@ -166,6 +170,7 @@
                 .add("id", id())
                 .add("key", key())
                 .add("appId", appId())
+                .add("priority", priority())
                 .add("resources", resources())
                 .add("selector", selector())
                 .add("treatment", treatment())
diff --git a/core/api/src/main/java/org/onosproject/net/intent/MplsIntent.java b/core/api/src/main/java/org/onosproject/net/intent/MplsIntent.java
index 0c13e3f..d7f8535 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/MplsIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/MplsIntent.java
@@ -74,7 +74,8 @@
                               Optional<MplsLabel> egressLabel,
                               List<Constraint> constraints) {
 
-        super(appId, Collections.emptyList(), selector, treatment, constraints);
+        super(appId, Collections.emptyList(), selector, treatment, constraints,
+                DEFAULT_INTENT_PRIORITY);
 
         checkNotNull(ingressPoint);
         checkNotNull(egressPoint);
@@ -144,6 +145,7 @@
         return MoreObjects.toStringHelper(getClass())
                 .add("id", id())
                 .add("appId", appId())
+                .add("priority", priority())
                 .add("selector", selector())
                 .add("treatment", treatment())
                 .add("ingressPoint", ingressPoint)
diff --git a/core/api/src/main/java/org/onosproject/net/intent/MplsPathIntent.java b/core/api/src/main/java/org/onosproject/net/intent/MplsPathIntent.java
index f6602c9..2102dba 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/MplsPathIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/MplsPathIntent.java
@@ -59,7 +59,8 @@
     public MplsPathIntent(ApplicationId appId, TrafficSelector selector,
             TrafficTreatment treatment, Path path, Optional<MplsLabel> ingressLabel,
             Optional<MplsLabel> egressLabel, List<Constraint> constraints) {
-        super(appId, selector, treatment, path, constraints);
+        super(appId, selector, treatment, path, constraints,
+                DEFAULT_INTENT_PRIORITY);
 
         checkNotNull(ingressLabel);
         checkNotNull(egressLabel);
diff --git a/core/api/src/main/java/org/onosproject/net/intent/MultiPointToSinglePointIntent.java b/core/api/src/main/java/org/onosproject/net/intent/MultiPointToSinglePointIntent.java
index 19fcc7c..721ff17 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/MultiPointToSinglePointIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/MultiPointToSinglePointIntent.java
@@ -56,7 +56,8 @@
                                          TrafficTreatment treatment,
                                          Set<ConnectPoint> ingressPoints,
                                          ConnectPoint egressPoint) {
-        this(appId, selector, treatment, ingressPoints, egressPoint, Collections.emptyList());
+        this(appId, selector, treatment, ingressPoints, egressPoint,
+                Collections.emptyList(), DEFAULT_INTENT_PRIORITY);
     }
 
     /**
@@ -70,6 +71,7 @@
      * @param ingressPoints set of ports from which ingress traffic originates
      * @param egressPoint   port to which traffic will egress
      * @param constraints   constraints to apply to the intent
+     * @param priority      priority to use for flows generated by this intent
      * @throws NullPointerException     if {@code ingressPoints} or
      *                                  {@code egressPoint} is null.
      * @throws IllegalArgumentException if the size of {@code ingressPoints} is
@@ -81,8 +83,10 @@
                                          TrafficTreatment treatment,
                                          Set<ConnectPoint> ingressPoints,
                                          ConnectPoint egressPoint,
-                                         List<Constraint> constraints) {
-        super(appId, key, Collections.emptyList(), selector, treatment, constraints);
+                                         List<Constraint> constraints,
+                                         int priority) {
+        super(appId, key, Collections.emptyList(), selector, treatment, constraints,
+                priority);
 
         checkNotNull(ingressPoints);
         checkArgument(!ingressPoints.isEmpty(), "Ingress point set cannot be empty");
@@ -104,6 +108,7 @@
      * @param ingressPoints set of ports from which ingress traffic originates
      * @param egressPoint   port to which traffic will egress
      * @param constraints   constraints to apply to the intent
+     * @param priority      priority to use for flows generated by this intent
      * @throws NullPointerException     if {@code ingressPoints} or
      *                                  {@code egressPoint} is null.
      * @throws IllegalArgumentException if the size of {@code ingressPoints} is
@@ -114,8 +119,10 @@
                                          TrafficTreatment treatment,
                                          Set<ConnectPoint> ingressPoints,
                                          ConnectPoint egressPoint,
-                                         List<Constraint> constraints) {
-        this(appId, null, selector, treatment, ingressPoints, egressPoint, constraints);
+                                         List<Constraint> constraints,
+                                         int priority) {
+        this(appId, null, selector, treatment, ingressPoints, egressPoint,
+                constraints, priority);
     }
 
     /**
@@ -152,6 +159,7 @@
                 .add("id", id())
                 .add("key", key())
                 .add("appId", appId())
+                .add("priority", priority())
                 .add("resources", resources())
                 .add("selector", selector())
                 .add("treatment", treatment())
diff --git a/core/api/src/main/java/org/onosproject/net/intent/OpticalConnectivityIntent.java b/core/api/src/main/java/org/onosproject/net/intent/OpticalConnectivityIntent.java
index d51abbb..091ebc5 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/OpticalConnectivityIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/OpticalConnectivityIntent.java
@@ -54,7 +54,7 @@
     public OpticalConnectivityIntent(ApplicationId appId,
                                      Key key,
                                      ConnectPoint src, ConnectPoint dst) {
-        super(appId, key, Collections.emptyList());
+        super(appId, key, Collections.emptyList(), DEFAULT_INTENT_PRIORITY);
         this.src = src;
         this.dst = dst;
     }
diff --git a/core/api/src/main/java/org/onosproject/net/intent/PathIntent.java b/core/api/src/main/java/org/onosproject/net/intent/PathIntent.java
index e307e2e..13fa61e 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/PathIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/PathIntent.java
@@ -48,7 +48,8 @@
      */
     public PathIntent(ApplicationId appId, TrafficSelector selector,
                       TrafficTreatment treatment, Path path) {
-        this(appId, selector, treatment, path, Collections.emptyList());
+        this(appId, selector, treatment, path, Collections.emptyList(),
+                DEFAULT_INTENT_PRIORITY);
     }
 
     /**
@@ -60,11 +61,14 @@
      * @param treatment treatment
      * @param path      traversed links
      * @param constraints  optional list of constraints
+     * @param priority  priority to use for the generated flows
      * @throws NullPointerException {@code path} is null
      */
     public PathIntent(ApplicationId appId, TrafficSelector selector,
-                      TrafficTreatment treatment, Path path, List<Constraint> constraints) {
-        super(appId, resources(path.links()), selector, treatment, constraints);
+                      TrafficTreatment treatment, Path path, List<Constraint> constraints,
+                      int priority) {
+        super(appId, resources(path.links()), selector, treatment, constraints,
+                priority);
         PathIntent.validate(path.links());
         this.path = path;
     }
@@ -123,6 +127,7 @@
         return MoreObjects.toStringHelper(getClass())
                 .add("id", id())
                 .add("appId", appId())
+                .add("priority", priority())
                 .add("resources", resources())
                 .add("selector", selector())
                 .add("treatment", treatment())
diff --git a/core/api/src/main/java/org/onosproject/net/intent/PointToPointIntent.java b/core/api/src/main/java/org/onosproject/net/intent/PointToPointIntent.java
index d8434ad..2df7b8b 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/PointToPointIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/PointToPointIntent.java
@@ -49,6 +49,7 @@
      * @param ingressPoint ingress port
      * @param egressPoint  egress port
      * @param constraints  optional list of constraints
+     * @param priority     priority to use for flows generated by this intent
      * @throws NullPointerException if {@code ingressPoint} or {@code egressPoints} is null.
      */
     public PointToPointIntent(ApplicationId appId,
@@ -57,8 +58,10 @@
                               TrafficTreatment treatment,
                               ConnectPoint ingressPoint,
                               ConnectPoint egressPoint,
-                              List<Constraint> constraints) {
-        super(appId, key, Collections.emptyList(), selector, treatment, constraints);
+                              List<Constraint> constraints,
+                              int priority) {
+        super(appId, key, Collections.emptyList(), selector, treatment, constraints,
+                priority);
 
         checkNotNull(ingressPoint);
         checkNotNull(egressPoint);
@@ -85,7 +88,8 @@
                               ConnectPoint ingressPoint,
                               ConnectPoint egressPoint) {
         this(appId, null, selector, treatment, ingressPoint, egressPoint,
-             ImmutableList.of(new LinkTypeConstraint(false, Link.Type.OPTICAL)));
+             ImmutableList.of(new LinkTypeConstraint(false, Link.Type.OPTICAL)),
+                DEFAULT_INTENT_PRIORITY);
     }
 
     /**
@@ -98,14 +102,17 @@
      * @param ingressPoint ingress port
      * @param egressPoint  egress port
      * @param constraints  optional list of constraints
+     * @param priority     priority to use for flows generated by this intent
      * @throws NullPointerException if {@code ingressPoint} or {@code egressPoints} is null.
      */
     public PointToPointIntent(ApplicationId appId, TrafficSelector selector,
                               TrafficTreatment treatment,
                               ConnectPoint ingressPoint,
                               ConnectPoint egressPoint,
-                              List<Constraint> constraints) {
-        super(appId, null, Collections.emptyList(), selector, treatment, constraints);
+                              List<Constraint> constraints,
+                              int priority) {
+        super(appId, null, Collections.emptyList(), selector, treatment,
+                constraints, priority);
 
         checkNotNull(ingressPoint);
         checkNotNull(egressPoint);
@@ -150,6 +157,7 @@
                 .add("id", id())
                 .add("key", key())
                 .add("appId", appId())
+                .add("priority", priority())
                 .add("resources", resources())
                 .add("selector", selector())
                 .add("treatment", treatment())
diff --git a/core/api/src/main/java/org/onosproject/net/intent/SinglePointToMultiPointIntent.java b/core/api/src/main/java/org/onosproject/net/intent/SinglePointToMultiPointIntent.java
index 2531fae..129c4a6 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/SinglePointToMultiPointIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/SinglePointToMultiPointIntent.java
@@ -54,7 +54,9 @@
     public SinglePointToMultiPointIntent(ApplicationId appId,
             TrafficSelector selector, TrafficTreatment treatment,
             ConnectPoint ingressPoint, Set<ConnectPoint> egressPoints) {
-        this(appId, null, selector, treatment, ingressPoint, egressPoints, Collections.emptyList());
+        this(appId, null, selector, treatment, ingressPoint, egressPoints,
+                Collections.emptyList(),
+                DEFAULT_INTENT_PRIORITY);
     }
 
     /**
@@ -67,6 +69,7 @@
      * @param ingressPoint port on which traffic will ingress
      * @param egressPoints set of ports on which traffic will egress
      * @param constraints constraints to apply to the intent
+     * @param priority priority to use for flows generated by this intent
      * @throws NullPointerException if {@code ingressPoint} or
      *             {@code egressPoints} is null
      * @throws IllegalArgumentException if the size of {@code egressPoints} is
@@ -76,8 +79,10 @@
             Key key,
             TrafficSelector selector, TrafficTreatment treatment,
             ConnectPoint ingressPoint, Set<ConnectPoint> egressPoints,
-            List<Constraint> constraints) {
-        super(appId, key, Collections.emptyList(), selector, treatment, constraints);
+            List<Constraint> constraints,
+            int priority) {
+        super(appId, key, Collections.emptyList(), selector, treatment, constraints,
+                priority);
         checkNotNull(egressPoints);
         checkNotNull(ingressPoint);
         checkArgument(!egressPoints.isEmpty(), "Egress point set cannot be empty");
@@ -122,6 +127,7 @@
                 .add("id", id())
                 .add("key", key())
                 .add("appId", appId())
+                .add("priority", priority())
                 .add("resources", resources())
                 .add("selector", selector())
                 .add("treatment", treatment())
diff --git a/core/api/src/main/java/org/onosproject/net/intent/TwoWayP2PIntent.java b/core/api/src/main/java/org/onosproject/net/intent/TwoWayP2PIntent.java
index b6d0246..118e137 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/TwoWayP2PIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/TwoWayP2PIntent.java
@@ -107,7 +107,8 @@
                            TrafficSelector selector,
                            TrafficTreatment treatment,
                            List<Constraint> constraints) {
-        super(appId, key, Collections.emptyList(), selector, treatment, constraints);
+        super(appId, key, Collections.emptyList(), selector, treatment, constraints,
+                DEFAULT_INTENT_PRIORITY);
 
         // TODO: consider whether the case one and two are same is allowed
         this.one = checkNotNull(one);
@@ -147,6 +148,7 @@
                 .add("id", id())
                 .add("key", key())
                 .add("appId", appId())
+                .add("priority", priority())
                 .add("resources", resources())
                 .add("selector", selector())
                 .add("treatment", treatment())
diff --git a/core/api/src/test/java/org/onosproject/net/intent/LinkCollectionIntentTest.java b/core/api/src/test/java/org/onosproject/net/intent/LinkCollectionIntentTest.java
index 0327390..a9ae782 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/LinkCollectionIntentTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/LinkCollectionIntentTest.java
@@ -134,7 +134,8 @@
                         links1,
                         ingress,
                         egress,
-                        constraints);
+                        constraints,
+                        8888);
 
         final Set<Link> createdLinks = collectionIntent.links();
         assertThat(createdLinks, hasSize(1));