ONOS-741 - JSON codecs for constraints
Change-Id: I6eebc2ef5f6c28956f0944524aa90f44cebfcc60
Showing
5 changed files
with
454 additions
and
12 deletions
| ... | @@ -15,7 +15,10 @@ | ... | @@ -15,7 +15,10 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.codec.impl; | 16 | package org.onosproject.codec.impl; |
| 17 | 17 | ||
| 18 | -import com.google.common.collect.ImmutableSet; | 18 | +import java.util.Map; |
| 19 | +import java.util.Set; | ||
| 20 | +import java.util.concurrent.ConcurrentHashMap; | ||
| 21 | + | ||
| 19 | import org.apache.felix.scr.annotations.Activate; | 22 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; | 23 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; | 24 | import org.apache.felix.scr.annotations.Deactivate; |
| ... | @@ -36,15 +39,14 @@ import org.onosproject.net.flow.TrafficTreatment; | ... | @@ -36,15 +39,14 @@ import org.onosproject.net.flow.TrafficTreatment; |
| 36 | import org.onosproject.net.flow.criteria.Criterion; | 39 | import org.onosproject.net.flow.criteria.Criterion; |
| 37 | import org.onosproject.net.flow.instructions.Instruction; | 40 | import org.onosproject.net.flow.instructions.Instruction; |
| 38 | import org.onosproject.net.intent.ConnectivityIntent; | 41 | import org.onosproject.net.intent.ConnectivityIntent; |
| 42 | +import org.onosproject.net.intent.Constraint; | ||
| 39 | import org.onosproject.net.intent.HostToHostIntent; | 43 | import org.onosproject.net.intent.HostToHostIntent; |
| 40 | import org.onosproject.net.intent.Intent; | 44 | import org.onosproject.net.intent.Intent; |
| 41 | import org.onosproject.net.intent.PointToPointIntent; | 45 | import org.onosproject.net.intent.PointToPointIntent; |
| 42 | import org.slf4j.Logger; | 46 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; | 47 | import org.slf4j.LoggerFactory; |
| 44 | 48 | ||
| 45 | -import java.util.Map; | 49 | +import com.google.common.collect.ImmutableSet; |
| 46 | -import java.util.Set; | ||
| 47 | -import java.util.concurrent.ConcurrentHashMap; | ||
| 48 | 50 | ||
| 49 | /** | 51 | /** |
| 50 | * Implementation of the JSON codec brokering service. | 52 | * Implementation of the JSON codec brokering service. |
| ... | @@ -77,6 +79,7 @@ public class CodecManager implements CodecService { | ... | @@ -77,6 +79,7 @@ public class CodecManager implements CodecService { |
| 77 | registerCodec(Instruction.class, new InstructionCodec()); | 79 | registerCodec(Instruction.class, new InstructionCodec()); |
| 78 | registerCodec(Criterion.class, new CriterionCodec()); | 80 | registerCodec(Criterion.class, new CriterionCodec()); |
| 79 | registerCodec(Ethernet.class, new EthernetCodec()); | 81 | registerCodec(Ethernet.class, new EthernetCodec()); |
| 82 | + registerCodec(Constraint.class, new ConstraintCodec()); | ||
| 80 | log.info("Started"); | 83 | log.info("Started"); |
| 81 | } | 84 | } |
| 82 | 85 | ... | ... |
| ... | @@ -56,9 +56,12 @@ public class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> { | ... | @@ -56,9 +56,12 @@ public class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> { |
| 56 | final ArrayNode jsonConstraints = result.putArray("constraints"); | 56 | final ArrayNode jsonConstraints = result.putArray("constraints"); |
| 57 | 57 | ||
| 58 | if (intent.constraints() != null) { | 58 | if (intent.constraints() != null) { |
| 59 | + final JsonCodec<Constraint> constraintCodec = | ||
| 60 | + context.codec(Constraint.class); | ||
| 59 | for (final Constraint constraint : intent.constraints()) { | 61 | for (final Constraint constraint : intent.constraints()) { |
| 60 | - // TODO: constraint should have its own codec | 62 | + final ObjectNode constraintNode = |
| 61 | - jsonConstraints.add(constraint.toString()); | 63 | + constraintCodec.encode(constraint, context); |
| 64 | + jsonConstraints.add(constraintNode); | ||
| 62 | } | 65 | } |
| 63 | } | 66 | } |
| 64 | } | 67 | } | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.codec.CodecContext; | ||
| 19 | +import org.onosproject.codec.JsonCodec; | ||
| 20 | +import org.onosproject.net.DeviceId; | ||
| 21 | +import org.onosproject.net.Link; | ||
| 22 | +import org.onosproject.net.intent.Constraint; | ||
| 23 | +import org.onosproject.net.intent.constraint.AnnotationConstraint; | ||
| 24 | +import org.onosproject.net.intent.constraint.BandwidthConstraint; | ||
| 25 | +import org.onosproject.net.intent.constraint.LambdaConstraint; | ||
| 26 | +import org.onosproject.net.intent.constraint.LatencyConstraint; | ||
| 27 | +import org.onosproject.net.intent.constraint.LinkTypeConstraint; | ||
| 28 | +import org.onosproject.net.intent.constraint.ObstacleConstraint; | ||
| 29 | +import org.onosproject.net.intent.constraint.WaypointConstraint; | ||
| 30 | + | ||
| 31 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 32 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 33 | + | ||
| 34 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 35 | + | ||
| 36 | +/** | ||
| 37 | + * Constraint JSON codec. | ||
| 38 | + */ | ||
| 39 | +public class ConstraintCodec extends JsonCodec<Constraint> { | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * Encodes a latency constraint. | ||
| 43 | + * | ||
| 44 | + * @param constraint latency constraint to encode | ||
| 45 | + * @param context code context | ||
| 46 | + * @return JSON ObjectNode representing the constraint | ||
| 47 | + */ | ||
| 48 | + private ObjectNode encodeLatencyConstraint(Constraint constraint, | ||
| 49 | + CodecContext context) { | ||
| 50 | + checkNotNull(constraint, "Duration constraint cannot be null"); | ||
| 51 | + final LatencyConstraint latencyConstraint = | ||
| 52 | + (LatencyConstraint) constraint; | ||
| 53 | + return context.mapper().createObjectNode() | ||
| 54 | + .put("latencyMillis", latencyConstraint.latency().toMillis()); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * Encodes an obstacle constraint. | ||
| 59 | + * | ||
| 60 | + * @param constraint obstacle constraint to encode | ||
| 61 | + * @param context code context | ||
| 62 | + * @return JSON ObjectNode representing the constraint | ||
| 63 | + */ | ||
| 64 | + private ObjectNode encodeObstacleConstraint(Constraint constraint, | ||
| 65 | + CodecContext context) { | ||
| 66 | + checkNotNull(constraint, "Obstacle constraint cannot be null"); | ||
| 67 | + final ObstacleConstraint obstacleConstraint = | ||
| 68 | + (ObstacleConstraint) constraint; | ||
| 69 | + | ||
| 70 | + final ObjectNode result = context.mapper().createObjectNode(); | ||
| 71 | + final ArrayNode jsonObstacles = result.putArray("obstacles"); | ||
| 72 | + | ||
| 73 | + for (DeviceId did : obstacleConstraint.obstacles()) { | ||
| 74 | + jsonObstacles.add(did.toString()); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + return result; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + /** | ||
| 81 | + * Encodes a waypoint constraint. | ||
| 82 | + * | ||
| 83 | + * @param constraint waypoint constraint to encode | ||
| 84 | + * @param context code context | ||
| 85 | + * @return JSON ObjectNode representing the constraint | ||
| 86 | + */ | ||
| 87 | + private ObjectNode encodeWaypointConstraint(Constraint constraint, | ||
| 88 | + CodecContext context) { | ||
| 89 | + checkNotNull(constraint, "Waypoint constraint cannot be null"); | ||
| 90 | + final WaypointConstraint waypointConstraint = | ||
| 91 | + (WaypointConstraint) constraint; | ||
| 92 | + | ||
| 93 | + final ObjectNode result = context.mapper().createObjectNode(); | ||
| 94 | + final ArrayNode jsonWaypoints = result.putArray("waypoints"); | ||
| 95 | + | ||
| 96 | + for (DeviceId did : waypointConstraint.waypoints()) { | ||
| 97 | + jsonWaypoints.add(did.toString()); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + return result; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + /** | ||
| 104 | + * Encodes a annotation constraint. | ||
| 105 | + * | ||
| 106 | + * @param constraint annotation constraint to encode | ||
| 107 | + * @param context code context | ||
| 108 | + * @return JSON ObjectNode representing the constraint | ||
| 109 | + */ | ||
| 110 | + private ObjectNode encodeAnnotationConstraint(Constraint constraint, | ||
| 111 | + CodecContext context) { | ||
| 112 | + checkNotNull(constraint, "Annotation constraint cannot be null"); | ||
| 113 | + final AnnotationConstraint annotationConstraint = | ||
| 114 | + (AnnotationConstraint) constraint; | ||
| 115 | + return context.mapper().createObjectNode() | ||
| 116 | + .put("key", annotationConstraint.key()) | ||
| 117 | + .put("threshold", annotationConstraint.threshold()); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * Encodes a bandwidth constraint. | ||
| 122 | + * | ||
| 123 | + * @param constraint bandwidth constraint to encode | ||
| 124 | + * @param context code context | ||
| 125 | + * @return JSON ObjectNode representing the constraint | ||
| 126 | + */ | ||
| 127 | + private ObjectNode encodeBandwidthConstraint(Constraint constraint, | ||
| 128 | + CodecContext context) { | ||
| 129 | + checkNotNull(constraint, "Bandwidth constraint cannot be null"); | ||
| 130 | + final BandwidthConstraint bandwidthConstraint = | ||
| 131 | + (BandwidthConstraint) constraint; | ||
| 132 | + return context.mapper().createObjectNode() | ||
| 133 | + .put("bandwidth", bandwidthConstraint.bandwidth().toDouble()); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + /** | ||
| 137 | + * Encodes a lambda constraint. | ||
| 138 | + * | ||
| 139 | + * @param constraint lambda constraint to encode | ||
| 140 | + * @param context code context | ||
| 141 | + * @return JSON ObjectNode representing the constraint | ||
| 142 | + */ | ||
| 143 | + private ObjectNode encodeLambdaConstraint(Constraint constraint, | ||
| 144 | + CodecContext context) { | ||
| 145 | + checkNotNull(constraint, "Lambda constraint cannot be null"); | ||
| 146 | + final LambdaConstraint lambdaConstraint = | ||
| 147 | + (LambdaConstraint) constraint; | ||
| 148 | + | ||
| 149 | + return context.mapper().createObjectNode() | ||
| 150 | + .put("lambda", lambdaConstraint.lambda().toInt()); | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + /** | ||
| 154 | + * Encodes a link type constraint. | ||
| 155 | + * | ||
| 156 | + * @param constraint link type constraint to encode | ||
| 157 | + * @param context code context | ||
| 158 | + * @return JSON ObjectNode representing the constraint | ||
| 159 | + */ | ||
| 160 | + private ObjectNode encodeLinkTypeConstraint(Constraint constraint, | ||
| 161 | + CodecContext context) { | ||
| 162 | + checkNotNull(constraint, "Link type constraint cannot be null"); | ||
| 163 | + | ||
| 164 | + final LinkTypeConstraint linkTypeConstraint = | ||
| 165 | + (LinkTypeConstraint) constraint; | ||
| 166 | + | ||
| 167 | + final ObjectNode result = context.mapper().createObjectNode() | ||
| 168 | + .put("inclusive", linkTypeConstraint.isInclusive()); | ||
| 169 | + | ||
| 170 | + final ArrayNode jsonTypes = result.putArray("types"); | ||
| 171 | + | ||
| 172 | + if (linkTypeConstraint.types() != null) { | ||
| 173 | + for (Link.Type type : linkTypeConstraint.types()) { | ||
| 174 | + jsonTypes.add(type.name()); | ||
| 175 | + } | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + return result; | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + @Override | ||
| 182 | + public ObjectNode encode(Constraint constraint, CodecContext context) { | ||
| 183 | + checkNotNull(constraint, "Constraint cannot be null"); | ||
| 184 | + | ||
| 185 | + final ObjectNode result; | ||
| 186 | + if (constraint instanceof BandwidthConstraint) { | ||
| 187 | + result = encodeBandwidthConstraint(constraint, context); | ||
| 188 | + } else if (constraint instanceof LambdaConstraint) { | ||
| 189 | + result = encodeLambdaConstraint(constraint, context); | ||
| 190 | + } else if (constraint instanceof LinkTypeConstraint) { | ||
| 191 | + result = encodeLinkTypeConstraint(constraint, context); | ||
| 192 | + } else if (constraint instanceof AnnotationConstraint) { | ||
| 193 | + result = encodeAnnotationConstraint(constraint, context); | ||
| 194 | + } else if (constraint instanceof LatencyConstraint) { | ||
| 195 | + result = encodeLatencyConstraint(constraint, context); | ||
| 196 | + } else if (constraint instanceof ObstacleConstraint) { | ||
| 197 | + result = encodeObstacleConstraint(constraint, context); | ||
| 198 | + } else if (constraint instanceof WaypointConstraint) { | ||
| 199 | + result = encodeWaypointConstraint(constraint, context); | ||
| 200 | + } else { | ||
| 201 | + result = context.mapper().createObjectNode(); | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + result.put("type", constraint.getClass().getSimpleName()); | ||
| 205 | + return result; | ||
| 206 | + } | ||
| 207 | +} |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.codec.impl; | 16 | package org.onosproject.codec.impl; |
| 17 | 17 | ||
| 18 | +import java.time.Duration; | ||
| 18 | import java.util.List; | 19 | import java.util.List; |
| 19 | 20 | ||
| 20 | import org.junit.Test; | 21 | import org.junit.Test; |
| ... | @@ -25,6 +26,7 @@ import org.onosproject.codec.JsonCodec; | ... | @@ -25,6 +26,7 @@ import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.core.ApplicationId; | 26 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.DefaultApplicationId; | 27 | import org.onosproject.core.DefaultApplicationId; |
| 27 | import org.onosproject.net.ConnectPoint; | 28 | import org.onosproject.net.ConnectPoint; |
| 29 | +import org.onosproject.net.DeviceId; | ||
| 28 | import org.onosproject.net.HostId; | 30 | import org.onosproject.net.HostId; |
| 29 | import org.onosproject.net.NetTestTools; | 31 | import org.onosproject.net.NetTestTools; |
| 30 | import org.onosproject.net.PortNumber; | 32 | import org.onosproject.net.PortNumber; |
| ... | @@ -36,8 +38,13 @@ import org.onosproject.net.intent.Constraint; | ... | @@ -36,8 +38,13 @@ import org.onosproject.net.intent.Constraint; |
| 36 | import org.onosproject.net.intent.HostToHostIntent; | 38 | import org.onosproject.net.intent.HostToHostIntent; |
| 37 | import org.onosproject.net.intent.AbstractIntentTest; | 39 | import org.onosproject.net.intent.AbstractIntentTest; |
| 38 | import org.onosproject.net.intent.PointToPointIntent; | 40 | import org.onosproject.net.intent.PointToPointIntent; |
| 41 | +import org.onosproject.net.intent.constraint.AnnotationConstraint; | ||
| 42 | +import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; | ||
| 39 | import org.onosproject.net.intent.constraint.BandwidthConstraint; | 43 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 40 | import org.onosproject.net.intent.constraint.LambdaConstraint; | 44 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
| 45 | +import org.onosproject.net.intent.constraint.LatencyConstraint; | ||
| 46 | +import org.onosproject.net.intent.constraint.ObstacleConstraint; | ||
| 47 | +import org.onosproject.net.intent.constraint.WaypointConstraint; | ||
| 41 | import org.onosproject.net.resource.Bandwidth; | 48 | import org.onosproject.net.resource.Bandwidth; |
| 42 | import org.onosproject.net.resource.Lambda; | 49 | import org.onosproject.net.resource.Lambda; |
| 43 | 50 | ||
| ... | @@ -45,6 +52,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; | ... | @@ -45,6 +52,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; |
| 45 | import com.google.common.collect.ImmutableList; | 52 | import com.google.common.collect.ImmutableList; |
| 46 | 53 | ||
| 47 | import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent; | 54 | import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent; |
| 55 | +import static org.onosproject.net.NetTestTools.did; | ||
| 48 | import static org.onosproject.net.NetTestTools.hid; | 56 | import static org.onosproject.net.NetTestTools.hid; |
| 49 | 57 | ||
| 50 | 58 | ||
| ... | @@ -111,6 +119,9 @@ public class IntentCodecTest extends AbstractIntentTest { | ... | @@ -111,6 +119,9 @@ public class IntentCodecTest extends AbstractIntentTest { |
| 111 | public void intentWithTreatmentSelectorAndConstraints() { | 119 | public void intentWithTreatmentSelectorAndConstraints() { |
| 112 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); | 120 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 113 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); | 121 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
| 122 | + DeviceId did1 = did("device1"); | ||
| 123 | + DeviceId did2 = did("device2"); | ||
| 124 | + DeviceId did3 = did("device3"); | ||
| 114 | final TrafficSelector selector = DefaultTrafficSelector.builder() | 125 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 115 | .matchIPProtocol((byte) 3) | 126 | .matchIPProtocol((byte) 3) |
| 116 | .matchMplsLabel(4) | 127 | .matchMplsLabel(4) |
| ... | @@ -125,9 +136,16 @@ public class IntentCodecTest extends AbstractIntentTest { | ... | @@ -125,9 +136,16 @@ public class IntentCodecTest extends AbstractIntentTest { |
| 125 | .setOutput(PortNumber.CONTROLLER) | 136 | .setOutput(PortNumber.CONTROLLER) |
| 126 | .setEthDst(MacAddress.BROADCAST) | 137 | .setEthDst(MacAddress.BROADCAST) |
| 127 | .build(); | 138 | .build(); |
| 128 | - final Constraint constraint1 = new BandwidthConstraint(Bandwidth.valueOf(1.0)); | 139 | + |
| 129 | - final Constraint constraint2 = new LambdaConstraint(Lambda.valueOf(3)); | 140 | + final List<Constraint> constraints = |
| 130 | - final List<Constraint> constraints = ImmutableList.of(constraint1, constraint2); | 141 | + ImmutableList.of( |
| 142 | + new BandwidthConstraint(Bandwidth.valueOf(1.0)), | ||
| 143 | + new LambdaConstraint(Lambda.valueOf(3)), | ||
| 144 | + new AnnotationConstraint("key", 33.0), | ||
| 145 | + new AsymmetricPathConstraint(), | ||
| 146 | + new LatencyConstraint(Duration.ofSeconds(2)), | ||
| 147 | + new ObstacleConstraint(did1, did2), | ||
| 148 | + new WaypointConstraint(did3)); | ||
| 131 | 149 | ||
| 132 | final PointToPointIntent intent = | 150 | final PointToPointIntent intent = |
| 133 | new PointToPointIntent(appId, selector, treatment, | 151 | new PointToPointIntent(appId, selector, treatment, | ... | ... |
| ... | @@ -21,6 +21,8 @@ import java.util.Set; | ... | @@ -21,6 +21,8 @@ import java.util.Set; |
| 21 | import org.hamcrest.Description; | 21 | import org.hamcrest.Description; |
| 22 | import org.hamcrest.TypeSafeDiagnosingMatcher; | 22 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 23 | import org.onosproject.net.ConnectPoint; | 23 | import org.onosproject.net.ConnectPoint; |
| 24 | +import org.onosproject.net.DeviceId; | ||
| 25 | +import org.onosproject.net.Link; | ||
| 24 | import org.onosproject.net.NetworkResource; | 26 | import org.onosproject.net.NetworkResource; |
| 25 | import org.onosproject.net.flow.TrafficSelector; | 27 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; | 28 | import org.onosproject.net.flow.TrafficTreatment; |
| ... | @@ -31,6 +33,13 @@ import org.onosproject.net.intent.Constraint; | ... | @@ -31,6 +33,13 @@ import org.onosproject.net.intent.Constraint; |
| 31 | import org.onosproject.net.intent.HostToHostIntent; | 33 | import org.onosproject.net.intent.HostToHostIntent; |
| 32 | import org.onosproject.net.intent.Intent; | 34 | import org.onosproject.net.intent.Intent; |
| 33 | import org.onosproject.net.intent.PointToPointIntent; | 35 | import org.onosproject.net.intent.PointToPointIntent; |
| 36 | +import org.onosproject.net.intent.constraint.AnnotationConstraint; | ||
| 37 | +import org.onosproject.net.intent.constraint.BandwidthConstraint; | ||
| 38 | +import org.onosproject.net.intent.constraint.LambdaConstraint; | ||
| 39 | +import org.onosproject.net.intent.constraint.LatencyConstraint; | ||
| 40 | +import org.onosproject.net.intent.constraint.LinkTypeConstraint; | ||
| 41 | +import org.onosproject.net.intent.constraint.ObstacleConstraint; | ||
| 42 | +import org.onosproject.net.intent.constraint.WaypointConstraint; | ||
| 34 | 43 | ||
| 35 | import com.fasterxml.jackson.databind.JsonNode; | 44 | import com.fasterxml.jackson.databind.JsonNode; |
| 36 | 45 | ||
| ... | @@ -118,6 +127,208 @@ public final class IntentJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> | ... | @@ -118,6 +127,208 @@ public final class IntentJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> |
| 118 | return true; | 127 | return true; |
| 119 | } | 128 | } |
| 120 | 129 | ||
| 130 | + | ||
| 131 | + /** | ||
| 132 | + * Matches a bandwidth constraint against a JSON representation of the | ||
| 133 | + * constraint. | ||
| 134 | + * | ||
| 135 | + * @param bandwidthConstraint constraint object to match | ||
| 136 | + * @param constraintJson JSON representation of the constraint | ||
| 137 | + * @return true if the constraint and JSON match, false otherwise. | ||
| 138 | + */ | ||
| 139 | + private boolean matchBandwidthConstraint(BandwidthConstraint bandwidthConstraint, | ||
| 140 | + JsonNode constraintJson) { | ||
| 141 | + final JsonNode bandwidthJson = constraintJson.get("bandwidth"); | ||
| 142 | + return bandwidthJson != null | ||
| 143 | + && constraintJson.get("bandwidth").asDouble() | ||
| 144 | + == bandwidthConstraint.bandwidth().toDouble(); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + /** | ||
| 148 | + * Matches a lamdba constraint against a JSON representation of the | ||
| 149 | + * constraint. | ||
| 150 | + * | ||
| 151 | + * @param lambdaConstraint constraint object to match | ||
| 152 | + * @param constraintJson JSON representation of the constraint | ||
| 153 | + * @return true if the constraint and JSON match, false otherwise. | ||
| 154 | + */ | ||
| 155 | + private boolean matchLambdaConstraint(LambdaConstraint lambdaConstraint, | ||
| 156 | + JsonNode constraintJson) { | ||
| 157 | + final JsonNode lambdaJson = constraintJson.get("lambda"); | ||
| 158 | + return lambdaJson != null | ||
| 159 | + && constraintJson.get("lambda").asInt() | ||
| 160 | + == lambdaConstraint.lambda().toInt(); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * Matches a link type constraint against a JSON representation of the | ||
| 165 | + * constraint. | ||
| 166 | + * | ||
| 167 | + * @param linkTypeConstraint constraint object to match | ||
| 168 | + * @param constraintJson JSON representation of the constraint | ||
| 169 | + * @return true if the constraint and JSON match, false otherwise. | ||
| 170 | + */ | ||
| 171 | + private boolean matchLinkTypeConstraint(LinkTypeConstraint linkTypeConstraint, | ||
| 172 | + JsonNode constraintJson) { | ||
| 173 | + final JsonNode inclusiveJson = constraintJson.get("inclusive"); | ||
| 174 | + final JsonNode typesJson = constraintJson.get("types"); | ||
| 175 | + | ||
| 176 | + if (typesJson.size() != linkTypeConstraint.types().size()) { | ||
| 177 | + return false; | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + int foundType = 0; | ||
| 181 | + for (Link.Type type : linkTypeConstraint.types()) { | ||
| 182 | + for (int jsonIndex = 0; jsonIndex < typesJson.size(); jsonIndex++) { | ||
| 183 | + if (type.name().equals(typesJson.get(jsonIndex).asText())) { | ||
| 184 | + foundType++; | ||
| 185 | + break; | ||
| 186 | + } | ||
| 187 | + } | ||
| 188 | + } | ||
| 189 | + return (inclusiveJson != null && | ||
| 190 | + inclusiveJson.asBoolean() == linkTypeConstraint.isInclusive()) && | ||
| 191 | + foundType == typesJson.size(); | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + /** | ||
| 195 | + * Matches an annotation constraint against a JSON representation of the | ||
| 196 | + * constraint. | ||
| 197 | + * | ||
| 198 | + * @param annotationConstraint constraint object to match | ||
| 199 | + * @param constraintJson JSON representation of the constraint | ||
| 200 | + * @return true if the constraint and JSON match, false otherwise. | ||
| 201 | + */ | ||
| 202 | + private boolean matchAnnotationConstraint(AnnotationConstraint annotationConstraint, | ||
| 203 | + JsonNode constraintJson) { | ||
| 204 | + final JsonNode keyJson = constraintJson.get("key"); | ||
| 205 | + final JsonNode thresholdJson = constraintJson.get("threshold"); | ||
| 206 | + return (keyJson != null | ||
| 207 | + && keyJson.asText().equals(annotationConstraint.key())) && | ||
| 208 | + (thresholdJson != null | ||
| 209 | + && thresholdJson.asDouble() == annotationConstraint.threshold()); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + /** | ||
| 213 | + * Matches a latency constraint against a JSON representation of the | ||
| 214 | + * constraint. | ||
| 215 | + * | ||
| 216 | + * @param latencyConstraint constraint object to match | ||
| 217 | + * @param constraintJson JSON representation of the constraint | ||
| 218 | + * @return true if the constraint and JSON match, false otherwise. | ||
| 219 | + */ | ||
| 220 | + private boolean matchLatencyConstraint(LatencyConstraint latencyConstraint, | ||
| 221 | + JsonNode constraintJson) { | ||
| 222 | + final JsonNode latencyJson = constraintJson.get("latencyMillis"); | ||
| 223 | + return (latencyJson != null | ||
| 224 | + && latencyJson.asInt() == latencyConstraint.latency().toMillis()); | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + /** | ||
| 228 | + * Matches an obstacle constraint against a JSON representation of the | ||
| 229 | + * constraint. | ||
| 230 | + * | ||
| 231 | + * @param obstacleConstraint constraint object to match | ||
| 232 | + * @param constraintJson JSON representation of the constraint | ||
| 233 | + * @return true if the constraint and JSON match, false otherwise. | ||
| 234 | + */ | ||
| 235 | + private boolean matchObstacleConstraint(ObstacleConstraint obstacleConstraint, | ||
| 236 | + JsonNode constraintJson) { | ||
| 237 | + final JsonNode obstaclesJson = constraintJson.get("obstacles"); | ||
| 238 | + | ||
| 239 | + if (obstaclesJson.size() != obstacleConstraint.obstacles().size()) { | ||
| 240 | + return false; | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + for (int obstaclesIndex = 0; obstaclesIndex < obstaclesJson.size(); | ||
| 244 | + obstaclesIndex++) { | ||
| 245 | + boolean obstacleFound = false; | ||
| 246 | + final String obstacleJson = obstaclesJson.get(obstaclesIndex) | ||
| 247 | + .asText(); | ||
| 248 | + for (DeviceId obstacle : obstacleConstraint.obstacles()) { | ||
| 249 | + if (obstacle.toString().equals(obstacleJson)) { | ||
| 250 | + obstacleFound = true; | ||
| 251 | + } | ||
| 252 | + } | ||
| 253 | + if (!obstacleFound) { | ||
| 254 | + return false; | ||
| 255 | + } | ||
| 256 | + } | ||
| 257 | + return true; | ||
| 258 | + } | ||
| 259 | + | ||
| 260 | + /** | ||
| 261 | + * Matches a waypoint constraint against a JSON representation of the | ||
| 262 | + * constraint. | ||
| 263 | + * | ||
| 264 | + * @param waypointConstraint constraint object to match | ||
| 265 | + * @param constraintJson JSON representation of the constraint | ||
| 266 | + * @return true if the constraint and JSON match, false otherwise. | ||
| 267 | + */ | ||
| 268 | + private boolean matchWaypointConstraint(WaypointConstraint waypointConstraint, | ||
| 269 | + JsonNode constraintJson) { | ||
| 270 | + final JsonNode waypointsJson = constraintJson.get("waypoints"); | ||
| 271 | + | ||
| 272 | + if (waypointsJson.size() != waypointConstraint.waypoints().size()) { | ||
| 273 | + return false; | ||
| 274 | + } | ||
| 275 | + | ||
| 276 | + for (int waypointsIndex = 0; waypointsIndex < waypointsJson.size(); | ||
| 277 | + waypointsIndex++) { | ||
| 278 | + boolean waypointFound = false; | ||
| 279 | + final String waypointJson = waypointsJson.get(waypointsIndex) | ||
| 280 | + .asText(); | ||
| 281 | + for (DeviceId waypoint : waypointConstraint.waypoints()) { | ||
| 282 | + if (waypoint.toString().equals(waypointJson)) { | ||
| 283 | + waypointFound = true; | ||
| 284 | + } | ||
| 285 | + } | ||
| 286 | + if (!waypointFound) { | ||
| 287 | + return false; | ||
| 288 | + } | ||
| 289 | + } | ||
| 290 | + return true; | ||
| 291 | + } | ||
| 292 | + | ||
| 293 | + | ||
| 294 | + /** | ||
| 295 | + * Matches a constraint against a JSON representation of the | ||
| 296 | + * constraint. | ||
| 297 | + * | ||
| 298 | + * @param constraint constraint object to match | ||
| 299 | + * @param constraintJson JSON representation of the constraint | ||
| 300 | + * @return true if the constraint and JSON match, false otherwise. | ||
| 301 | + */ | ||
| 302 | + private boolean matchConstraint(Constraint constraint, JsonNode constraintJson) { | ||
| 303 | + final JsonNode typeJson = constraintJson.get("type"); | ||
| 304 | + if (!typeJson.asText().equals(constraint.getClass().getSimpleName())) { | ||
| 305 | + return false; | ||
| 306 | + } | ||
| 307 | + if (constraint instanceof BandwidthConstraint) { | ||
| 308 | + return matchBandwidthConstraint((BandwidthConstraint) constraint, | ||
| 309 | + constraintJson); | ||
| 310 | + } else if (constraint instanceof LambdaConstraint) { | ||
| 311 | + return matchLambdaConstraint((LambdaConstraint) constraint, | ||
| 312 | + constraintJson); | ||
| 313 | + } else if (constraint instanceof LinkTypeConstraint) { | ||
| 314 | + return matchLinkTypeConstraint((LinkTypeConstraint) constraint, | ||
| 315 | + constraintJson); | ||
| 316 | + } else if (constraint instanceof AnnotationConstraint) { | ||
| 317 | + return matchAnnotationConstraint((AnnotationConstraint) constraint, | ||
| 318 | + constraintJson); | ||
| 319 | + } else if (constraint instanceof LatencyConstraint) { | ||
| 320 | + return matchLatencyConstraint((LatencyConstraint) constraint, | ||
| 321 | + constraintJson); | ||
| 322 | + } else if (constraint instanceof ObstacleConstraint) { | ||
| 323 | + return matchObstacleConstraint((ObstacleConstraint) constraint, | ||
| 324 | + constraintJson); | ||
| 325 | + } else if (constraint instanceof WaypointConstraint) { | ||
| 326 | + return matchWaypointConstraint((WaypointConstraint) constraint, | ||
| 327 | + constraintJson); | ||
| 328 | + } | ||
| 329 | + return true; | ||
| 330 | + } | ||
| 331 | + | ||
| 121 | /** | 332 | /** |
| 122 | * Matches the JSON representation of a connectivity intent. Calls the | 333 | * Matches the JSON representation of a connectivity intent. Calls the |
| 123 | * matcher for the connectivity intent subtype. | 334 | * matcher for the connectivity intent subtype. |
| ... | @@ -193,16 +404,16 @@ public final class IntentJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> | ... | @@ -193,16 +404,16 @@ public final class IntentJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> |
| 193 | } | 404 | } |
| 194 | for (final Constraint constraint : connectivityIntent.constraints()) { | 405 | for (final Constraint constraint : connectivityIntent.constraints()) { |
| 195 | boolean constraintFound = false; | 406 | boolean constraintFound = false; |
| 196 | - final String constraintString = constraint.toString(); | ||
| 197 | for (int constraintIndex = 0; constraintIndex < jsonConstraints.size(); | 407 | for (int constraintIndex = 0; constraintIndex < jsonConstraints.size(); |
| 198 | constraintIndex++) { | 408 | constraintIndex++) { |
| 199 | final JsonNode value = jsonConstraints.get(constraintIndex); | 409 | final JsonNode value = jsonConstraints.get(constraintIndex); |
| 200 | - if (value.asText().equals(constraintString)) { | 410 | + if (matchConstraint(constraint, value)) { |
| 201 | constraintFound = true; | 411 | constraintFound = true; |
| 202 | } | 412 | } |
| 203 | } | 413 | } |
| 204 | if (!constraintFound) { | 414 | if (!constraintFound) { |
| 205 | - description.appendText("resource missing " + constraintString); | 415 | + final String constraintString = constraint.toString(); |
| 416 | + description.appendText("constraint missing " + constraintString); | ||
| 206 | return false; | 417 | return false; |
| 207 | } | 418 | } |
| 208 | } | 419 | } | ... | ... |
-
Please register or login to post a comment