[ONOS-5903] Intents always installs first path. Traffic path load is not distributed.
            + Adding HashPathSelectionConstraint
            + Adding '--hashed' option in add-point-intent cli for intent hashcode based path selection
            + hashed path selection % size bug fix

Change-Id: I2e867934a0bbed66301118973d7e1d1483d7cb0e
diff --git a/cli/src/main/java/org/onosproject/cli/net/ConnectivityIntentCommand.java b/cli/src/main/java/org/onosproject/cli/net/ConnectivityIntentCommand.java
index e9d975b..5d5d18b 100644
--- a/cli/src/main/java/org/onosproject/cli/net/ConnectivityIntentCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/ConnectivityIntentCommand.java
@@ -37,6 +37,7 @@
 import org.onosproject.net.intent.Key;
 import org.onosproject.net.intent.constraint.BandwidthConstraint;
 import org.onosproject.net.intent.constraint.EncapsulationConstraint;
+import org.onosproject.net.intent.constraint.HashedPathSelectionConstraint;
 import org.onosproject.net.intent.constraint.PartialFailureConstraint;
 
 import java.util.LinkedList;
@@ -176,6 +177,10 @@
             required = false, multiValued = false)
     private String encapsulationString = null;
 
+    @Option(name = "--hashed", description = "Hashed path selection",
+            required = false, multiValued = false)
+    private boolean hashedPathSelection = false;
+
 
     /**
      * Constructs a traffic selector based on the command line arguments
@@ -385,6 +390,10 @@
             constraints.add(new EncapsulationConstraint(encapType));
         }
 
+        // Check for hashed path selection
+        if (hashedPathSelection) {
+            constraints.add(new HashedPathSelectionConstraint());
+        }
         return constraints;
     }
 
diff --git a/core/api/src/main/java/org/onosproject/net/intent/constraint/HashedPathSelectionConstraint.java b/core/api/src/main/java/org/onosproject/net/intent/constraint/HashedPathSelectionConstraint.java
new file mode 100644
index 0000000..3cb120c
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/constraint/HashedPathSelectionConstraint.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2014-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.constraint;
+
+import org.onosproject.net.Link;
+import org.onosproject.net.Path;
+import org.onosproject.net.intent.Constraint;
+import org.onosproject.net.intent.ResourceContext;
+
+/**
+ * A constraint for intent.hashCode() based path selection.
+ */
+public class HashedPathSelectionConstraint implements Constraint {
+
+    @Override
+    public double cost(Link link, ResourceContext context) {
+        return 1;
+    }
+
+    @Override
+    public boolean validate(Path path, ResourceContext context) {
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        return 1;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        return (obj != null && getClass().equals(obj.getClass()));
+    }
+
+    @Override
+    public String toString() {
+        return "HashedPathSelectionConstraint";
+    }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/ConnectivityIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/ConnectivityIntentCompiler.java
index 81e0fcc8..43923b5 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/ConnectivityIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/ConnectivityIntentCompiler.java
@@ -30,6 +30,7 @@
 import org.onosproject.net.intent.Constraint;
 import org.onosproject.net.intent.IntentCompiler;
 import org.onosproject.net.intent.IntentExtensionService;
+import org.onosproject.net.intent.constraint.HashedPathSelectionConstraint;
 import org.onosproject.net.intent.impl.PathNotFoundException;
 import org.onosproject.net.resource.ResourceQueryService;
 import org.onosproject.net.provider.ProviderId;
@@ -127,7 +128,11 @@
         if (filtered.isEmpty()) {
             return null;
         }
-        // TODO: let's be more intelligent about this eventually
+
+        if (constraints.stream().anyMatch(c -> c instanceof HashedPathSelectionConstraint)) {
+            return filtered.get(intent.hashCode() % filtered.size());
+        }
+
         return filtered.iterator().next();
     }
 
@@ -150,7 +155,11 @@
         if (filtered.isEmpty()) {
             throw new PathNotFoundException(one, two);
         }
-        // TODO: let's be more intelligent about this eventually
+
+        if (constraints.stream().anyMatch(c -> c instanceof HashedPathSelectionConstraint)) {
+            return filtered.get(intent.hashCode() % filtered.size());
+        }
+
         return filtered.iterator().next();
     }
 
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index 3480b41..1ea18a0 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -190,6 +190,7 @@
 import org.onosproject.net.intent.constraint.BandwidthConstraint;
 import org.onosproject.net.intent.constraint.BooleanConstraint;
 import org.onosproject.net.intent.constraint.EncapsulationConstraint;
+import org.onosproject.net.intent.constraint.HashedPathSelectionConstraint;
 import org.onosproject.net.intent.constraint.LatencyConstraint;
 import org.onosproject.net.intent.constraint.LinkTypeConstraint;
 import org.onosproject.net.intent.constraint.ObstacleConstraint;
@@ -487,6 +488,7 @@
                     DefaultTableStatisticsEntry.class,
                     EncapsulationConstraint.class,
                     EncapsulationType.class,
+                    HashedPathSelectionConstraint.class,
                     // Flow Objectives
                     DefaultForwardingObjective.class,
                     ForwardingObjective.Flag.class,