blob: 3b10907612e5bd08df1dccd4377ee7eba7ff5cbc [file] [log] [blame]
Ray Milkeyb82c42b2015-06-30 09:42:20 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Thomas Szyrkowiec7c5f9c92017-07-12 12:42:02 +020029import org.onosproject.net.intent.constraint.DomainConstraint;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070030import org.onosproject.net.intent.constraint.LatencyConstraint;
31import org.onosproject.net.intent.constraint.LinkTypeConstraint;
32import org.onosproject.net.intent.constraint.ObstacleConstraint;
33import org.onosproject.net.intent.constraint.WaypointConstraint;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070034
35import com.fasterxml.jackson.databind.JsonNode;
36import com.fasterxml.jackson.databind.node.ObjectNode;
37
38import static org.onlab.util.Tools.nullIsIllegal;
39
40/**
41 * Constraint JSON decoder.
42 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070043public final class DecodeConstraintCodecHelper {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070044 private final ObjectNode json;
45
46 /**
47 * Constructs a constraint decoder.
48 *
49 * @param json object node to decode
50 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070051 public DecodeConstraintCodecHelper(ObjectNode json) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070052 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 Milkeyb82c42b2015-06-30 09:42:20 -070097 * 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 Szyrkowiec7c5f9c92017-07-12 12:42:02 +0200163 * Decodes a domain constraint.
164 *
165 * @return domain constraint object.
166 */
167 private Constraint decodeDomainConstraint() {
168 return DomainConstraint.domain();
169 }
170
171
172 /**
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700173 * 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 SHIMIZUa88db492015-11-23 13:21:04 -0800182 return new BandwidthConstraint(Bandwidth.bps(bandwidth));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700183 }
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 Milkeyb82c42b2015-06-30 09:42:20 -0700197 } 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 Szyrkowiec7c5f9c92017-07-12 12:42:02 +0200209 } else if (type.equals(DomainConstraint.class.getSimpleName())) {
210 return decodeDomainConstraint();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700211 }
212 throw new IllegalArgumentException("Instruction type "
213 + type + " is not supported");
214 }
215}