ONOS-741 - JSON codecs for constraints

Change-Id: I6eebc2ef5f6c28956f0944524aa90f44cebfcc60
diff --git a/web/api/src/test/java/org/onosproject/codec/impl/IntentCodecTest.java b/web/api/src/test/java/org/onosproject/codec/impl/IntentCodecTest.java
index fd84f17..a230273 100644
--- a/web/api/src/test/java/org/onosproject/codec/impl/IntentCodecTest.java
+++ b/web/api/src/test/java/org/onosproject/codec/impl/IntentCodecTest.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.codec.impl;
 
+import java.time.Duration;
 import java.util.List;
 
 import org.junit.Test;
@@ -25,6 +26,7 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.DefaultApplicationId;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.HostId;
 import org.onosproject.net.NetTestTools;
 import org.onosproject.net.PortNumber;
@@ -36,8 +38,13 @@
 import org.onosproject.net.intent.HostToHostIntent;
 import org.onosproject.net.intent.AbstractIntentTest;
 import org.onosproject.net.intent.PointToPointIntent;
+import org.onosproject.net.intent.constraint.AnnotationConstraint;
+import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
 import org.onosproject.net.intent.constraint.BandwidthConstraint;
 import org.onosproject.net.intent.constraint.LambdaConstraint;
+import org.onosproject.net.intent.constraint.LatencyConstraint;
+import org.onosproject.net.intent.constraint.ObstacleConstraint;
+import org.onosproject.net.intent.constraint.WaypointConstraint;
 import org.onosproject.net.resource.Bandwidth;
 import org.onosproject.net.resource.Lambda;
 
@@ -45,6 +52,7 @@
 import com.google.common.collect.ImmutableList;
 
 import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
+import static org.onosproject.net.NetTestTools.did;
 import static org.onosproject.net.NetTestTools.hid;
 
 
@@ -111,6 +119,9 @@
     public void intentWithTreatmentSelectorAndConstraints() {
         ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
         ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
+        DeviceId did1 = did("device1");
+        DeviceId did2 = did("device2");
+        DeviceId did3 = did("device3");
         final TrafficSelector selector = DefaultTrafficSelector.builder()
                 .matchIPProtocol((byte) 3)
                 .matchMplsLabel(4)
@@ -125,9 +136,16 @@
                 .setOutput(PortNumber.CONTROLLER)
                 .setEthDst(MacAddress.BROADCAST)
                 .build();
-        final Constraint constraint1 = new BandwidthConstraint(Bandwidth.valueOf(1.0));
-        final Constraint constraint2 = new LambdaConstraint(Lambda.valueOf(3));
-        final List<Constraint> constraints = ImmutableList.of(constraint1, constraint2);
+
+        final List<Constraint> constraints =
+                ImmutableList.of(
+                    new BandwidthConstraint(Bandwidth.valueOf(1.0)),
+                    new LambdaConstraint(Lambda.valueOf(3)),
+                    new AnnotationConstraint("key", 33.0),
+                    new AsymmetricPathConstraint(),
+                    new LatencyConstraint(Duration.ofSeconds(2)),
+                    new ObstacleConstraint(did1, did2),
+                    new WaypointConstraint(did3));
 
         final PointToPointIntent intent =
                 new PointToPointIntent(appId, selector, treatment,
diff --git a/web/api/src/test/java/org/onosproject/codec/impl/IntentJsonMatcher.java b/web/api/src/test/java/org/onosproject/codec/impl/IntentJsonMatcher.java
index a49307b..9dddd4d 100644
--- a/web/api/src/test/java/org/onosproject/codec/impl/IntentJsonMatcher.java
+++ b/web/api/src/test/java/org/onosproject/codec/impl/IntentJsonMatcher.java
@@ -21,6 +21,8 @@
 import org.hamcrest.Description;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
 import org.onosproject.net.NetworkResource;
 import org.onosproject.net.flow.TrafficSelector;
 import org.onosproject.net.flow.TrafficTreatment;
@@ -31,6 +33,13 @@
 import org.onosproject.net.intent.HostToHostIntent;
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.PointToPointIntent;
+import org.onosproject.net.intent.constraint.AnnotationConstraint;
+import org.onosproject.net.intent.constraint.BandwidthConstraint;
+import org.onosproject.net.intent.constraint.LambdaConstraint;
+import org.onosproject.net.intent.constraint.LatencyConstraint;
+import org.onosproject.net.intent.constraint.LinkTypeConstraint;
+import org.onosproject.net.intent.constraint.ObstacleConstraint;
+import org.onosproject.net.intent.constraint.WaypointConstraint;
 
 import com.fasterxml.jackson.databind.JsonNode;
 
@@ -118,6 +127,208 @@
         return true;
     }
 
+
+    /**
+     * Matches a bandwidth constraint against a JSON representation of the
+     * constraint.
+     *
+     * @param bandwidthConstraint constraint object to match
+     * @param constraintJson JSON representation of the constraint
+     * @return true if the constraint and JSON match, false otherwise.
+     */
+    private boolean matchBandwidthConstraint(BandwidthConstraint bandwidthConstraint,
+                                             JsonNode constraintJson) {
+        final JsonNode bandwidthJson = constraintJson.get("bandwidth");
+        return bandwidthJson != null
+                && constraintJson.get("bandwidth").asDouble()
+                == bandwidthConstraint.bandwidth().toDouble();
+    }
+
+    /**
+     * Matches a lamdba constraint against a JSON representation of the
+     * constraint.
+     *
+     * @param lambdaConstraint constraint object to match
+     * @param constraintJson JSON representation of the constraint
+     * @return true if the constraint and JSON match, false otherwise.
+     */
+    private boolean matchLambdaConstraint(LambdaConstraint lambdaConstraint,
+                                          JsonNode constraintJson) {
+        final JsonNode lambdaJson = constraintJson.get("lambda");
+        return lambdaJson != null
+                && constraintJson.get("lambda").asInt()
+                == lambdaConstraint.lambda().toInt();
+    }
+
+    /**
+     * Matches a link type constraint against a JSON representation of the
+     * constraint.
+     *
+     * @param linkTypeConstraint constraint object to match
+     * @param constraintJson JSON representation of the constraint
+     * @return true if the constraint and JSON match, false otherwise.
+     */
+    private boolean matchLinkTypeConstraint(LinkTypeConstraint linkTypeConstraint,
+                                            JsonNode constraintJson) {
+        final JsonNode inclusiveJson = constraintJson.get("inclusive");
+        final JsonNode typesJson = constraintJson.get("types");
+
+        if (typesJson.size() != linkTypeConstraint.types().size()) {
+            return false;
+        }
+
+        int foundType = 0;
+        for (Link.Type type : linkTypeConstraint.types()) {
+            for (int jsonIndex = 0; jsonIndex < typesJson.size(); jsonIndex++) {
+                if (type.name().equals(typesJson.get(jsonIndex).asText())) {
+                    foundType++;
+                    break;
+                }
+            }
+        }
+        return (inclusiveJson != null &&
+                inclusiveJson.asBoolean() == linkTypeConstraint.isInclusive()) &&
+                foundType == typesJson.size();
+    }
+
+    /**
+     * Matches an annotation constraint against a JSON representation of the
+     * constraint.
+     *
+     * @param annotationConstraint constraint object to match
+     * @param constraintJson JSON representation of the constraint
+     * @return true if the constraint and JSON match, false otherwise.
+     */
+    private boolean matchAnnotationConstraint(AnnotationConstraint annotationConstraint,
+                                              JsonNode constraintJson) {
+        final JsonNode keyJson = constraintJson.get("key");
+        final JsonNode thresholdJson = constraintJson.get("threshold");
+        return (keyJson != null
+                && keyJson.asText().equals(annotationConstraint.key())) &&
+               (thresholdJson != null
+                && thresholdJson.asDouble() == annotationConstraint.threshold());
+    }
+
+    /**
+     * Matches a latency constraint against a JSON representation of the
+     * constraint.
+     *
+     * @param latencyConstraint constraint object to match
+     * @param constraintJson JSON representation of the constraint
+     * @return true if the constraint and JSON match, false otherwise.
+     */
+    private boolean matchLatencyConstraint(LatencyConstraint latencyConstraint,
+                                           JsonNode constraintJson) {
+        final JsonNode latencyJson = constraintJson.get("latencyMillis");
+        return (latencyJson != null
+                && latencyJson.asInt() == latencyConstraint.latency().toMillis());
+    }
+
+    /**
+     * Matches an obstacle constraint against a JSON representation of the
+     * constraint.
+     *
+     * @param obstacleConstraint constraint object to match
+     * @param constraintJson JSON representation of the constraint
+     * @return true if the constraint and JSON match, false otherwise.
+     */
+    private boolean matchObstacleConstraint(ObstacleConstraint obstacleConstraint,
+                                            JsonNode constraintJson) {
+        final JsonNode obstaclesJson = constraintJson.get("obstacles");
+
+        if (obstaclesJson.size() != obstacleConstraint.obstacles().size()) {
+            return false;
+        }
+
+        for (int obstaclesIndex = 0; obstaclesIndex < obstaclesJson.size();
+                 obstaclesIndex++) {
+            boolean obstacleFound = false;
+            final String obstacleJson = obstaclesJson.get(obstaclesIndex)
+                    .asText();
+            for (DeviceId obstacle : obstacleConstraint.obstacles()) {
+                if (obstacle.toString().equals(obstacleJson)) {
+                    obstacleFound = true;
+                }
+            }
+            if (!obstacleFound) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Matches a waypoint constraint against a JSON representation of the
+     * constraint.
+     *
+     * @param waypointConstraint constraint object to match
+     * @param constraintJson JSON representation of the constraint
+     * @return true if the constraint and JSON match, false otherwise.
+     */
+    private boolean matchWaypointConstraint(WaypointConstraint waypointConstraint,
+                                            JsonNode constraintJson) {
+        final JsonNode waypointsJson = constraintJson.get("waypoints");
+
+        if (waypointsJson.size() != waypointConstraint.waypoints().size()) {
+            return false;
+        }
+
+        for (int waypointsIndex = 0; waypointsIndex < waypointsJson.size();
+             waypointsIndex++) {
+            boolean waypointFound = false;
+            final String waypointJson = waypointsJson.get(waypointsIndex)
+                    .asText();
+            for (DeviceId waypoint : waypointConstraint.waypoints()) {
+                if (waypoint.toString().equals(waypointJson)) {
+                    waypointFound = true;
+                }
+            }
+            if (!waypointFound) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+
+    /**
+     * Matches a constraint against a JSON representation of the
+     * constraint.
+     *
+     * @param constraint constraint object to match
+     * @param constraintJson JSON representation of the constraint
+     * @return true if the constraint and JSON match, false otherwise.
+     */
+    private boolean matchConstraint(Constraint constraint, JsonNode constraintJson) {
+        final JsonNode typeJson = constraintJson.get("type");
+        if (!typeJson.asText().equals(constraint.getClass().getSimpleName())) {
+            return false;
+        }
+        if (constraint instanceof BandwidthConstraint) {
+            return matchBandwidthConstraint((BandwidthConstraint) constraint,
+                    constraintJson);
+        } else if (constraint instanceof LambdaConstraint) {
+            return matchLambdaConstraint((LambdaConstraint) constraint,
+                    constraintJson);
+        } else if (constraint instanceof LinkTypeConstraint) {
+            return matchLinkTypeConstraint((LinkTypeConstraint) constraint,
+                    constraintJson);
+        } else if (constraint instanceof AnnotationConstraint) {
+            return matchAnnotationConstraint((AnnotationConstraint) constraint,
+                    constraintJson);
+        } else if (constraint instanceof LatencyConstraint) {
+            return matchLatencyConstraint((LatencyConstraint) constraint,
+                    constraintJson);
+        } else if (constraint instanceof ObstacleConstraint) {
+            return matchObstacleConstraint((ObstacleConstraint) constraint,
+                    constraintJson);
+        } else if (constraint instanceof WaypointConstraint) {
+            return matchWaypointConstraint((WaypointConstraint) constraint,
+                    constraintJson);
+        }
+        return true;
+    }
+
     /**
      * Matches the JSON representation of a connectivity intent. Calls the
      * matcher for the connectivity intent subtype.
@@ -193,16 +404,16 @@
             }
             for (final Constraint constraint : connectivityIntent.constraints()) {
                 boolean constraintFound = false;
-                final String constraintString = constraint.toString();
                 for (int constraintIndex = 0; constraintIndex < jsonConstraints.size();
                      constraintIndex++) {
                     final JsonNode value = jsonConstraints.get(constraintIndex);
-                    if (value.asText().equals(constraintString)) {
+                    if (matchConstraint(constraint, value)) {
                         constraintFound = true;
                     }
                 }
                 if (!constraintFound) {
-                    description.appendText("resource missing " + constraintString);
+                    final String constraintString = constraint.toString();
+                    description.appendText("constraint missing " + constraintString);
                     return false;
                 }
             }