Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 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 java.time.Duration; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.stream.IntStream; |
| 21 | |
| 22 | import org.onlab.util.Bandwidth; |
| 23 | import org.onosproject.net.DeviceId; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.intent.Constraint; |
| 26 | import org.onosproject.net.intent.constraint.AnnotationConstraint; |
| 27 | import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; |
| 28 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 29 | import org.onosproject.net.intent.constraint.LatencyConstraint; |
| 30 | import org.onosproject.net.intent.constraint.LinkTypeConstraint; |
| 31 | import org.onosproject.net.intent.constraint.ObstacleConstraint; |
| 32 | import org.onosproject.net.intent.constraint.WaypointConstraint; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 33 | |
| 34 | import com.fasterxml.jackson.databind.JsonNode; |
| 35 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 36 | |
| 37 | import static org.onlab.util.Tools.nullIsIllegal; |
| 38 | |
| 39 | /** |
| 40 | * Constraint JSON decoder. |
| 41 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 42 | public final class DecodeConstraintCodecHelper { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 43 | private final ObjectNode json; |
| 44 | |
| 45 | /** |
| 46 | * Constructs a constraint decoder. |
| 47 | * |
| 48 | * @param json object node to decode |
| 49 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 50 | public DecodeConstraintCodecHelper(ObjectNode json) { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 51 | this.json = json; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Decodes a link type constraint. |
| 56 | * |
| 57 | * @return link type constraint object. |
| 58 | */ |
| 59 | private Constraint decodeLinkTypeConstraint() { |
| 60 | boolean inclusive = nullIsIllegal(json.get(ConstraintCodec.INCLUSIVE), |
| 61 | ConstraintCodec.INCLUSIVE + ConstraintCodec.MISSING_MEMBER_MESSAGE).asBoolean(); |
| 62 | |
| 63 | JsonNode types = nullIsIllegal(json.get(ConstraintCodec.TYPES), |
| 64 | ConstraintCodec.TYPES + ConstraintCodec.MISSING_MEMBER_MESSAGE); |
| 65 | if (types.size() < 1) { |
| 66 | throw new IllegalArgumentException( |
| 67 | "types array in link constraint must have at least one value"); |
| 68 | } |
| 69 | |
| 70 | ArrayList<Link.Type> typesEntries = new ArrayList<>(types.size()); |
| 71 | IntStream.range(0, types.size()) |
| 72 | .forEach(index -> |
| 73 | typesEntries.add(Link.Type.valueOf(types.get(index).asText()))); |
| 74 | |
| 75 | return new LinkTypeConstraint(inclusive, |
| 76 | typesEntries.toArray(new Link.Type[types.size()])); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Decodes an annotation constraint. |
| 81 | * |
| 82 | * @return annotation constraint object. |
| 83 | */ |
| 84 | private Constraint decodeAnnotationConstraint() { |
| 85 | String key = nullIsIllegal(json.get(ConstraintCodec.KEY), |
| 86 | ConstraintCodec.KEY + ConstraintCodec.MISSING_MEMBER_MESSAGE) |
| 87 | .asText(); |
| 88 | double threshold = nullIsIllegal(json.get(ConstraintCodec.THRESHOLD), |
| 89 | ConstraintCodec.THRESHOLD + ConstraintCodec.MISSING_MEMBER_MESSAGE) |
| 90 | .asDouble(); |
| 91 | |
| 92 | return new AnnotationConstraint(key, threshold); |
| 93 | } |
| 94 | |
| 95 | /** |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 96 | * Decodes a latency constraint. |
| 97 | * |
| 98 | * @return latency constraint object. |
| 99 | */ |
| 100 | private Constraint decodeLatencyConstraint() { |
| 101 | long latencyMillis = nullIsIllegal(json.get(ConstraintCodec.LATENCY_MILLIS), |
| 102 | ConstraintCodec.LATENCY_MILLIS + ConstraintCodec.MISSING_MEMBER_MESSAGE) |
| 103 | .asLong(); |
| 104 | |
| 105 | return new LatencyConstraint(Duration.ofMillis(latencyMillis)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Decodes an obstacle constraint. |
| 110 | * |
| 111 | * @return obstacle constraint object. |
| 112 | */ |
| 113 | private Constraint decodeObstacleConstraint() { |
| 114 | JsonNode obstacles = nullIsIllegal(json.get(ConstraintCodec.OBSTACLES), |
| 115 | ConstraintCodec.OBSTACLES + ConstraintCodec.MISSING_MEMBER_MESSAGE); |
| 116 | if (obstacles.size() < 1) { |
| 117 | throw new IllegalArgumentException( |
| 118 | "obstacles array in obstacles constraint must have at least one value"); |
| 119 | } |
| 120 | |
| 121 | ArrayList<DeviceId> obstacleEntries = new ArrayList<>(obstacles.size()); |
| 122 | IntStream.range(0, obstacles.size()) |
| 123 | .forEach(index -> |
| 124 | obstacleEntries.add(DeviceId.deviceId(obstacles.get(index).asText()))); |
| 125 | |
| 126 | return new ObstacleConstraint( |
| 127 | obstacleEntries.toArray(new DeviceId[obstacles.size()])); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Decodes a waypoint constraint. |
| 132 | * |
| 133 | * @return waypoint constraint object. |
| 134 | */ |
| 135 | private Constraint decodeWaypointConstraint() { |
| 136 | JsonNode waypoints = nullIsIllegal(json.get(ConstraintCodec.WAYPOINTS), |
| 137 | ConstraintCodec.WAYPOINTS + ConstraintCodec.MISSING_MEMBER_MESSAGE); |
| 138 | if (waypoints.size() < 1) { |
| 139 | throw new IllegalArgumentException( |
| 140 | "obstacles array in obstacles constraint must have at least one value"); |
| 141 | } |
| 142 | |
| 143 | ArrayList<DeviceId> waypointEntries = new ArrayList<>(waypoints.size()); |
| 144 | IntStream.range(0, waypoints.size()) |
| 145 | .forEach(index -> |
| 146 | waypointEntries.add(DeviceId.deviceId(waypoints.get(index).asText()))); |
| 147 | |
| 148 | return new WaypointConstraint( |
| 149 | waypointEntries.toArray(new DeviceId[waypoints.size()])); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Decodes an asymmetric path constraint. |
| 154 | * |
| 155 | * @return asymmetric path constraint object. |
| 156 | */ |
| 157 | private Constraint decodeAsymmetricPathConstraint() { |
| 158 | return new AsymmetricPathConstraint(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Decodes a bandwidth constraint. |
| 163 | * |
| 164 | * @return bandwidth constraint object. |
| 165 | */ |
| 166 | private Constraint decodeBandwidthConstraint() { |
| 167 | double bandwidth = nullIsIllegal(json.get(ConstraintCodec.BANDWIDTH), |
| 168 | ConstraintCodec.BANDWIDTH + ConstraintCodec.MISSING_MEMBER_MESSAGE) |
| 169 | .asDouble(); |
| 170 | |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 171 | return new BandwidthConstraint(Bandwidth.bps(bandwidth)); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Decodes the given constraint. |
| 176 | * |
| 177 | * @return constraint object. |
| 178 | */ |
| 179 | public Constraint decode() { |
| 180 | final String type = nullIsIllegal(json.get(ConstraintCodec.TYPE), |
| 181 | ConstraintCodec.TYPE + ConstraintCodec.MISSING_MEMBER_MESSAGE) |
| 182 | .asText(); |
| 183 | |
| 184 | if (type.equals(BandwidthConstraint.class.getSimpleName())) { |
| 185 | return decodeBandwidthConstraint(); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 186 | } else if (type.equals(LinkTypeConstraint.class.getSimpleName())) { |
| 187 | return decodeLinkTypeConstraint(); |
| 188 | } else if (type.equals(AnnotationConstraint.class.getSimpleName())) { |
| 189 | return decodeAnnotationConstraint(); |
| 190 | } else if (type.equals(LatencyConstraint.class.getSimpleName())) { |
| 191 | return decodeLatencyConstraint(); |
| 192 | } else if (type.equals(ObstacleConstraint.class.getSimpleName())) { |
| 193 | return decodeObstacleConstraint(); |
| 194 | } else if (type.equals(WaypointConstraint.class.getSimpleName())) { |
| 195 | return decodeWaypointConstraint(); |
| 196 | } else if (type.equals(AsymmetricPathConstraint.class.getSimpleName())) { |
| 197 | return decodeAsymmetricPathConstraint(); |
| 198 | } else if (type.equals(LinkTypeConstraint.class.getSimpleName())) { |
| 199 | return decodeLinkTypeConstraint(); |
| 200 | } else if (type.equals(AnnotationConstraint.class.getSimpleName())) { |
| 201 | return decodeAnnotationConstraint(); |
| 202 | } |
| 203 | throw new IllegalArgumentException("Instruction type " |
| 204 | + type + " is not supported"); |
| 205 | } |
| 206 | } |