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