ONOS-741 - JSON codecs for constraints
Change-Id: I6eebc2ef5f6c28956f0944524aa90f44cebfcc60
Showing
5 changed files
with
240 additions
and
9 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, | ... | ... |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment