blob: 4eac49d5fde665b708050030f628d3f41d79b4d7 [file] [log] [blame]
Ray Milkeyb82c42b2015-06-30 09:42:20 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkeyb82c42b2015-06-30 09:42:20 -07003 *
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 */
16package org.onosproject.codec.impl;
17
18import java.time.Duration;
19import java.util.ArrayList;
20import java.util.stream.IntStream;
21
22import org.onlab.util.Bandwidth;
23import org.onosproject.net.DeviceId;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070024import org.onosproject.net.Link;
25import org.onosproject.net.intent.Constraint;
26import org.onosproject.net.intent.constraint.AnnotationConstraint;
27import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
28import org.onosproject.net.intent.constraint.BandwidthConstraint;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070029import org.onosproject.net.intent.constraint.LatencyConstraint;
30import org.onosproject.net.intent.constraint.LinkTypeConstraint;
31import org.onosproject.net.intent.constraint.ObstacleConstraint;
32import org.onosproject.net.intent.constraint.WaypointConstraint;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070033
34import com.fasterxml.jackson.databind.JsonNode;
35import com.fasterxml.jackson.databind.node.ObjectNode;
36
37import static org.onlab.util.Tools.nullIsIllegal;
38
39/**
40 * Constraint JSON decoder.
41 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070042public final class DecodeConstraintCodecHelper {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070043 private final ObjectNode json;
44
45 /**
46 * Constructs a constraint decoder.
47 *
48 * @param json object node to decode
49 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070050 public DecodeConstraintCodecHelper(ObjectNode json) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070051 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 Milkeyb82c42b2015-06-30 09:42:20 -070096 * 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 SHIMIZUa88db492015-11-23 13:21:04 -0800171 return new BandwidthConstraint(Bandwidth.bps(bandwidth));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700172 }
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 Milkeyb82c42b2015-06-30 09:42:20 -0700186 } 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}