blob: cd28afcaf12327f974cbaae85bac2d2d3ac617ba [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
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 */
16package org.onosproject.codec.impl;
17
andread35f89c2015-11-23 10:02:07 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyd43fe452015-05-29 09:35:12 -070020import org.onlab.packet.Ip6Address;
21import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070024import org.onlab.packet.TpPort;
Ray Milkeyd43fe452015-05-29 09:35:12 -070025import org.onlab.packet.VlanId;
Yafit Hadar5796d972015-10-15 13:16:11 +030026import org.onlab.util.HexString;
Ray Milkeyd43fe452015-05-29 09:35:12 -070027import org.onosproject.net.ChannelSpacing;
28import org.onosproject.net.GridType;
29import org.onosproject.net.Lambda;
Yafit Hadar5796d972015-10-15 13:16:11 +030030import org.onosproject.net.OchSignalType;
31import org.onosproject.net.OduSignalId;
32import org.onosproject.net.OduSignalType;
Ray Milkeyd43fe452015-05-29 09:35:12 -070033import org.onosproject.net.PortNumber;
34import org.onosproject.net.flow.criteria.Criteria;
35import org.onosproject.net.flow.criteria.Criterion;
36
andread35f89c2015-11-23 10:02:07 -080037import java.util.HashMap;
38import java.util.Map;
39
40import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkeyd43fe452015-05-29 09:35:12 -070041
Ray Milkeyd43fe452015-05-29 09:35:12 -070042/**
43 * Decode portion of the criterion codec.
44 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070045public final class DecodeCriterionCodecHelper {
Ray Milkeyd43fe452015-05-29 09:35:12 -070046
47 private final ObjectNode json;
48
49 protected static final String MISSING_MEMBER_MESSAGE =
50 " member is required in Criterion";
51
52 private interface CriterionDecoder {
53 Criterion decodeCriterion(ObjectNode json);
54 }
55 private final Map<String, CriterionDecoder> decoderMap;
56
57 /**
58 * Creates a decode criterion codec object.
59 * Initializes the lookup map for criterion subclass decoders.
60 *
61 * @param json JSON object to decode
62 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070063 public DecodeCriterionCodecHelper(ObjectNode json) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070064 this.json = json;
65 decoderMap = new HashMap<>();
66
67 decoderMap.put(Criterion.Type.IN_PORT.name(), new InPortDecoder());
68 decoderMap.put(Criterion.Type.IN_PHY_PORT.name(), new InPhyPortDecoder());
69 decoderMap.put(Criterion.Type.METADATA.name(), new MetadataDecoder());
70 decoderMap.put(Criterion.Type.ETH_DST.name(), new EthDstDecoder());
71 decoderMap.put(Criterion.Type.ETH_SRC.name(), new EthSrcDecoder());
72 decoderMap.put(Criterion.Type.ETH_TYPE.name(), new EthTypeDecoder());
73 decoderMap.put(Criterion.Type.VLAN_VID.name(), new VlanVidDecoder());
74 decoderMap.put(Criterion.Type.VLAN_PCP.name(), new VlanPcpDecoder());
alshabibfa0dc662016-01-13 11:23:53 -080075 decoderMap.put(Criterion.Type.INNER_VLAN_VID.name(), new InnerVlanVidDecoder());
76 decoderMap.put(Criterion.Type.INNER_VLAN_PCP.name(), new InnerVlanPcpDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -070077 decoderMap.put(Criterion.Type.IP_DSCP.name(), new IpDscpDecoder());
78 decoderMap.put(Criterion.Type.IP_ECN.name(), new IpEcnDecoder());
79 decoderMap.put(Criterion.Type.IP_PROTO.name(), new IpProtoDecoder());
80 decoderMap.put(Criterion.Type.IPV4_SRC.name(), new IpV4SrcDecoder());
81 decoderMap.put(Criterion.Type.IPV4_DST.name(), new IpV4DstDecoder());
82 decoderMap.put(Criterion.Type.TCP_SRC.name(), new TcpSrcDecoder());
83 decoderMap.put(Criterion.Type.TCP_DST.name(), new TcpDstDecoder());
84 decoderMap.put(Criterion.Type.UDP_SRC.name(), new UdpSrcDecoder());
85 decoderMap.put(Criterion.Type.UDP_DST.name(), new UdpDstDecoder());
86 decoderMap.put(Criterion.Type.SCTP_SRC.name(), new SctpSrcDecoder());
87 decoderMap.put(Criterion.Type.SCTP_DST.name(), new SctpDstDecoder());
88 decoderMap.put(Criterion.Type.ICMPV4_TYPE.name(), new IcmpV4TypeDecoder());
89 decoderMap.put(Criterion.Type.ICMPV4_CODE.name(), new IcmpV4CodeDecoder());
90 decoderMap.put(Criterion.Type.IPV6_SRC.name(), new IpV6SrcDecoder());
91 decoderMap.put(Criterion.Type.IPV6_DST.name(), new IpV6DstDecoder());
92 decoderMap.put(Criterion.Type.IPV6_FLABEL.name(), new IpV6FLabelDecoder());
93 decoderMap.put(Criterion.Type.ICMPV6_TYPE.name(), new IcmpV6TypeDecoder());
94 decoderMap.put(Criterion.Type.ICMPV6_CODE.name(), new IcmpV6CodeDecoder());
95 decoderMap.put(Criterion.Type.IPV6_ND_TARGET.name(), new V6NDTargetDecoder());
96 decoderMap.put(Criterion.Type.IPV6_ND_SLL.name(), new V6NDSllDecoder());
97 decoderMap.put(Criterion.Type.IPV6_ND_TLL.name(), new V6NDTllDecoder());
98 decoderMap.put(Criterion.Type.MPLS_LABEL.name(), new MplsLabelDecoder());
99 decoderMap.put(Criterion.Type.IPV6_EXTHDR.name(), new IpV6ExthdrDecoder());
100 decoderMap.put(Criterion.Type.OCH_SIGID.name(), new OchSigIdDecoder());
101 decoderMap.put(Criterion.Type.OCH_SIGTYPE.name(), new OchSigTypeDecoder());
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700102 decoderMap.put(Criterion.Type.TUNNEL_ID.name(), new TunnelIdDecoder());
Yafit Hadar5796d972015-10-15 13:16:11 +0300103 decoderMap.put(Criterion.Type.ODU_SIGID.name(), new OduSigIdDecoder());
104 decoderMap.put(Criterion.Type.ODU_SIGTYPE.name(), new OduSigTypeDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700105 }
106
107 private class EthTypeDecoder implements CriterionDecoder {
108 @Override
109 public Criterion decodeCriterion(ObjectNode json) {
andread35f89c2015-11-23 10:02:07 -0800110 int ethType;
111 if (json.get(CriterionCodec.ETH_TYPE).isInt()) {
112 ethType = nullIsIllegal(json.get(CriterionCodec.ETH_TYPE),
113 CriterionCodec.ETH_TYPE + MISSING_MEMBER_MESSAGE).asInt();
114 } else {
115 ethType = Integer.decode(json.get(CriterionCodec.ETH_TYPE).textValue());
116 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700117 return Criteria.matchEthType(ethType);
118 }
119 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700120 private class EthDstDecoder implements CriterionDecoder {
121 @Override
122 public Criterion decodeCriterion(ObjectNode json) {
123 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC),
124 CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
125
126 return Criteria.matchEthDst(mac);
127 }
128 }
129
130 private class EthSrcDecoder implements CriterionDecoder {
131 @Override
132 public Criterion decodeCriterion(ObjectNode json) {
133 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC),
134 CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
135
136 return Criteria.matchEthSrc(mac);
137 }
138 }
139
140 private class InPortDecoder implements CriterionDecoder {
141 @Override
142 public Criterion decodeCriterion(ObjectNode json) {
143 PortNumber port = PortNumber.portNumber(nullIsIllegal(json.get(CriterionCodec.PORT),
alshabibfa0dc662016-01-13 11:23:53 -0800144 CriterionCodec.PORT +
145 MISSING_MEMBER_MESSAGE).asLong());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700146
147 return Criteria.matchInPort(port);
148 }
149 }
150
151 private class InPhyPortDecoder implements CriterionDecoder {
152 @Override
153 public Criterion decodeCriterion(ObjectNode json) {
154 PortNumber port = PortNumber.portNumber(nullIsIllegal(json.get(CriterionCodec.PORT),
alshabibfa0dc662016-01-13 11:23:53 -0800155 CriterionCodec.PORT +
156 MISSING_MEMBER_MESSAGE).asLong());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700157
158 return Criteria.matchInPhyPort(port);
159 }
160 }
161
162 private class MetadataDecoder implements CriterionDecoder {
163 @Override
164 public Criterion decodeCriterion(ObjectNode json) {
165 long metadata = nullIsIllegal(json.get(CriterionCodec.METADATA),
166 CriterionCodec.METADATA + MISSING_MEMBER_MESSAGE).asLong();
167
168 return Criteria.matchMetadata(metadata);
169 }
170 }
171
172 private class VlanVidDecoder implements CriterionDecoder {
173 @Override
174 public Criterion decodeCriterion(ObjectNode json) {
175 short vlanId = (short) nullIsIllegal(json.get(CriterionCodec.VLAN_ID),
176 CriterionCodec.VLAN_ID + MISSING_MEMBER_MESSAGE).asInt();
177
178 return Criteria.matchVlanId(VlanId.vlanId(vlanId));
179 }
180 }
181
182 private class VlanPcpDecoder implements CriterionDecoder {
183 @Override
184 public Criterion decodeCriterion(ObjectNode json) {
185 byte priority = (byte) nullIsIllegal(json.get(CriterionCodec.PRIORITY),
alshabibfa0dc662016-01-13 11:23:53 -0800186 CriterionCodec.PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700187
188 return Criteria.matchVlanPcp(priority);
189 }
190 }
191
alshabibfa0dc662016-01-13 11:23:53 -0800192 private class InnerVlanVidDecoder implements CriterionDecoder {
193 @Override
194 public Criterion decodeCriterion(ObjectNode json) {
195 short vlanId = (short) nullIsIllegal(json.get(CriterionCodec.INNER_VLAN_ID),
196 CriterionCodec.INNER_VLAN_ID +
197 MISSING_MEMBER_MESSAGE).asInt();
198
199 return Criteria.matchInnerVlanId(VlanId.vlanId(vlanId));
200 }
201 }
202
203 private class InnerVlanPcpDecoder implements CriterionDecoder {
204 @Override
205 public Criterion decodeCriterion(ObjectNode json) {
206 byte priority = (byte) nullIsIllegal(json.get(CriterionCodec.INNER_PRIORITY),
207 CriterionCodec.INNER_PRIORITY +
208 MISSING_MEMBER_MESSAGE).asInt();
209
210 return Criteria.matchInnerVlanPcp(priority);
211 }
212 }
213
Ray Milkeyd43fe452015-05-29 09:35:12 -0700214 private class IpDscpDecoder implements CriterionDecoder {
215 @Override
216 public Criterion decodeCriterion(ObjectNode json) {
217 byte ipDscp = (byte) nullIsIllegal(json.get(CriterionCodec.IP_DSCP),
218 CriterionCodec.IP_DSCP + MISSING_MEMBER_MESSAGE).asInt();
219 return Criteria.matchIPDscp(ipDscp);
220 }
221 }
222
223 private class IpEcnDecoder implements CriterionDecoder {
224 @Override
225 public Criterion decodeCriterion(ObjectNode json) {
226 byte ipEcn = (byte) nullIsIllegal(json.get(CriterionCodec.IP_ECN),
227 CriterionCodec.IP_ECN + MISSING_MEMBER_MESSAGE).asInt();
228 return Criteria.matchIPEcn(ipEcn);
229 }
230 }
231
232 private class IpProtoDecoder implements CriterionDecoder {
233 @Override
234 public Criterion decodeCriterion(ObjectNode json) {
235 short proto = (short) nullIsIllegal(json.get(CriterionCodec.PROTOCOL),
236 CriterionCodec.PROTOCOL + MISSING_MEMBER_MESSAGE).asInt();
237 return Criteria.matchIPProtocol(proto);
238 }
239 }
240
241 private class IpV4SrcDecoder implements CriterionDecoder {
242 @Override
243 public Criterion decodeCriterion(ObjectNode json) {
244 String ip = nullIsIllegal(json.get(CriterionCodec.IP),
245 CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText();
246 return Criteria.matchIPSrc(IpPrefix.valueOf(ip));
247 }
248 }
249
250 private class IpV4DstDecoder implements CriterionDecoder {
251 @Override
252 public Criterion decodeCriterion(ObjectNode json) {
253 String ip = nullIsIllegal(json.get(CriterionCodec.IP),
254 CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText();
255 return Criteria.matchIPDst(IpPrefix.valueOf(ip));
256 }
257 }
258
259 private class IpV6SrcDecoder implements CriterionDecoder {
260 @Override
261 public Criterion decodeCriterion(ObjectNode json) {
262 String ip = nullIsIllegal(json.get(CriterionCodec.IP),
263 CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText();
264 return Criteria.matchIPv6Src(IpPrefix.valueOf(ip));
265 }
266 }
267
268 private class IpV6DstDecoder implements CriterionDecoder {
269 @Override
270 public Criterion decodeCriterion(ObjectNode json) {
271 String ip = nullIsIllegal(json.get(CriterionCodec.IP),
272 CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText();
273 return Criteria.matchIPv6Dst(IpPrefix.valueOf(ip));
274 }
275 }
276
277 private class TcpSrcDecoder implements CriterionDecoder {
278 @Override
279 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700280 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_PORT),
281 CriterionCodec.TCP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700282 return Criteria.matchTcpSrc(tcpPort);
283 }
284 }
285
286 private class TcpDstDecoder implements CriterionDecoder {
287 @Override
288 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700289 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_PORT),
290 CriterionCodec.TCP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700291 return Criteria.matchTcpDst(tcpPort);
292 }
293 }
294
295 private class UdpSrcDecoder implements CriterionDecoder {
296 @Override
297 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700298 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_PORT),
299 CriterionCodec.UDP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700300 return Criteria.matchUdpSrc(udpPort);
301 }
302 }
303
304 private class UdpDstDecoder implements CriterionDecoder {
305 @Override
306 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700307 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_PORT),
308 CriterionCodec.UDP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700309 return Criteria.matchUdpDst(udpPort);
310 }
311 }
312
313 private class SctpSrcDecoder implements CriterionDecoder {
314 @Override
315 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700316 TpPort sctpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_PORT),
317 CriterionCodec.SCTP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700318 return Criteria.matchSctpSrc(sctpPort);
319 }
320 }
321
322 private class SctpDstDecoder implements CriterionDecoder {
323 @Override
324 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700325 TpPort sctpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_PORT),
326 CriterionCodec.SCTP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700327 return Criteria.matchSctpDst(sctpPort);
328 }
329 }
330
331 private class IcmpV4TypeDecoder implements CriterionDecoder {
332 @Override
333 public Criterion decodeCriterion(ObjectNode json) {
334 short type = (short) nullIsIllegal(json.get(CriterionCodec.ICMP_TYPE),
335 CriterionCodec.ICMP_TYPE + MISSING_MEMBER_MESSAGE).asInt();
336 return Criteria.matchIcmpType(type);
337 }
338 }
339
340 private class IcmpV4CodeDecoder implements CriterionDecoder {
341 @Override
342 public Criterion decodeCriterion(ObjectNode json) {
343 short code = (short) nullIsIllegal(json.get(CriterionCodec.ICMP_CODE),
344 CriterionCodec.ICMP_CODE + MISSING_MEMBER_MESSAGE).asInt();
345 return Criteria.matchIcmpCode(code);
346 }
347 }
348
349 private class IpV6FLabelDecoder implements CriterionDecoder {
350 @Override
351 public Criterion decodeCriterion(ObjectNode json) {
352 int flowLabel = nullIsIllegal(json.get(CriterionCodec.FLOW_LABEL),
353 CriterionCodec.FLOW_LABEL + MISSING_MEMBER_MESSAGE).asInt();
354 return Criteria.matchIPv6FlowLabel(flowLabel);
355 }
356 }
357
358 private class IcmpV6TypeDecoder implements CriterionDecoder {
359 @Override
360 public Criterion decodeCriterion(ObjectNode json) {
361 short type = (short) nullIsIllegal(json.get(CriterionCodec.ICMPV6_TYPE),
362 CriterionCodec.ICMPV6_TYPE + MISSING_MEMBER_MESSAGE).asInt();
363 return Criteria.matchIcmpv6Type(type);
364 }
365 }
366
367 private class IcmpV6CodeDecoder implements CriterionDecoder {
368 @Override
369 public Criterion decodeCriterion(ObjectNode json) {
370 short code = (short) nullIsIllegal(json.get(CriterionCodec.ICMPV6_CODE),
371 CriterionCodec.ICMPV6_CODE + MISSING_MEMBER_MESSAGE).asInt();
372 return Criteria.matchIcmpv6Code(code);
373 }
374 }
375
376 private class V6NDTargetDecoder implements CriterionDecoder {
377 @Override
378 public Criterion decodeCriterion(ObjectNode json) {
379 Ip6Address target = Ip6Address.valueOf(nullIsIllegal(json.get(CriterionCodec.TARGET_ADDRESS),
380 CriterionCodec.TARGET_ADDRESS + MISSING_MEMBER_MESSAGE).asText());
381 return Criteria.matchIPv6NDTargetAddress(target);
382 }
383 }
384
385 private class V6NDSllDecoder implements CriterionDecoder {
386 @Override
387 public Criterion decodeCriterion(ObjectNode json) {
388 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC),
389 CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
390 return Criteria.matchIPv6NDSourceLinkLayerAddress(mac);
391 }
392 }
393
394 private class V6NDTllDecoder implements CriterionDecoder {
395 @Override
396 public Criterion decodeCriterion(ObjectNode json) {
397 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC),
398 CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
399 return Criteria.matchIPv6NDTargetLinkLayerAddress(mac);
400 }
401 }
402
403 private class MplsLabelDecoder implements CriterionDecoder {
404 @Override
405 public Criterion decodeCriterion(ObjectNode json) {
406 int label = nullIsIllegal(json.get(CriterionCodec.LABEL),
407 CriterionCodec.LABEL + MISSING_MEMBER_MESSAGE).asInt();
408 return Criteria.matchMplsLabel(MplsLabel.mplsLabel(label));
409 }
410 }
411
412 private class IpV6ExthdrDecoder implements CriterionDecoder {
413 @Override
414 public Criterion decodeCriterion(ObjectNode json) {
415 int exthdrFlags = nullIsIllegal(json.get(CriterionCodec.EXT_HDR_FLAGS),
416 CriterionCodec.EXT_HDR_FLAGS + MISSING_MEMBER_MESSAGE).asInt();
417 return Criteria.matchIPv6ExthdrFlags(exthdrFlags);
418 }
419 }
420
421 private class OchSigIdDecoder implements CriterionDecoder {
422 @Override
423 public Criterion decodeCriterion(ObjectNode json) {
424 if (json.get(CriterionCodec.LAMBDA) != null) {
425 Lambda lambda = Lambda.indexedLambda(nullIsIllegal(json.get(CriterionCodec.LAMBDA),
426 CriterionCodec.LAMBDA + MISSING_MEMBER_MESSAGE).asInt());
427 return Criteria.matchLambda(lambda);
428 } else {
429 JsonNode ochSignalId = nullIsIllegal(json.get(CriterionCodec.OCH_SIGNAL_ID),
430 CriterionCodec.GRID_TYPE + MISSING_MEMBER_MESSAGE);
431 GridType gridType =
432 GridType.valueOf(
433 nullIsIllegal(ochSignalId.get(CriterionCodec.GRID_TYPE),
434 CriterionCodec.GRID_TYPE + MISSING_MEMBER_MESSAGE).asText());
435 ChannelSpacing channelSpacing =
436 ChannelSpacing.valueOf(
437 nullIsIllegal(ochSignalId.get(CriterionCodec.CHANNEL_SPACING),
438 CriterionCodec.CHANNEL_SPACING + MISSING_MEMBER_MESSAGE).asText());
439 int spacingMultiplier = nullIsIllegal(ochSignalId.get(CriterionCodec.SPACING_MULIPLIER),
440 CriterionCodec.SPACING_MULIPLIER + MISSING_MEMBER_MESSAGE).asInt();
441 int slotGranularity = nullIsIllegal(ochSignalId.get(CriterionCodec.SLOT_GRANULARITY),
442 CriterionCodec.SLOT_GRANULARITY + MISSING_MEMBER_MESSAGE).asInt();
443 return Criteria.matchLambda(
444 Lambda.ochSignal(gridType, channelSpacing,
445 spacingMultiplier, slotGranularity));
446 }
447 }
448 }
449
450 private class OchSigTypeDecoder implements CriterionDecoder {
451 @Override
452 public Criterion decodeCriterion(ObjectNode json) {
Yafit Hadar5796d972015-10-15 13:16:11 +0300453 OchSignalType ochSignalType = OchSignalType.valueOf(nullIsIllegal(json.get(CriterionCodec.OCH_SIGNAL_TYPE),
454 CriterionCodec.OCH_SIGNAL_TYPE + MISSING_MEMBER_MESSAGE).asText());
455 return Criteria.matchOchSignalType(ochSignalType);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700456 }
457 }
458
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700459 private class TunnelIdDecoder implements CriterionDecoder {
460 @Override
461 public Criterion decodeCriterion(ObjectNode json) {
462 long tunnelId = nullIsIllegal(json.get(CriterionCodec.TUNNEL_ID),
463 CriterionCodec.TUNNEL_ID + MISSING_MEMBER_MESSAGE).asLong();
464 return Criteria.matchTunnelId(tunnelId);
465 }
466 }
467
Yafit Hadar5796d972015-10-15 13:16:11 +0300468 private class OduSigIdDecoder implements CriterionDecoder {
469 @Override
470 public Criterion decodeCriterion(ObjectNode json) {
471 JsonNode oduSignalId = nullIsIllegal(json.get(CriterionCodec.ODU_SIGNAL_ID),
472 CriterionCodec.TRIBUTARY_PORT_NUMBER + MISSING_MEMBER_MESSAGE);
473
474 int tributaryPortNumber = nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_PORT_NUMBER),
475 CriterionCodec.TRIBUTARY_PORT_NUMBER + MISSING_MEMBER_MESSAGE).asInt();
476 int tributarySlotLen = nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_SLOT_LEN),
477 CriterionCodec.TRIBUTARY_SLOT_LEN + MISSING_MEMBER_MESSAGE).asInt();
478 byte[] tributarySlotBitmap = HexString.fromHexString(
479 nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_SLOT_BITMAP),
480 CriterionCodec.TRIBUTARY_SLOT_BITMAP + MISSING_MEMBER_MESSAGE).asText());
481
482 return Criteria.matchOduSignalId(
483 OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap));
484 }
485 }
486
487 private class OduSigTypeDecoder implements CriterionDecoder {
488 @Override
489 public Criterion decodeCriterion(ObjectNode json) {
490 OduSignalType oduSignalType = OduSignalType.valueOf(nullIsIllegal(json.get(CriterionCodec.ODU_SIGNAL_TYPE),
491 CriterionCodec.ODU_SIGNAL_TYPE + MISSING_MEMBER_MESSAGE).asText());
492 return Criteria.matchOduSignalType(oduSignalType);
493 }
494 }
495
Ray Milkeyd43fe452015-05-29 09:35:12 -0700496 /**
497 * Decodes the JSON into a criterion object.
498 *
499 * @return Criterion object
500 * @throws IllegalArgumentException if the JSON is invalid
501 */
502 public Criterion decode() {
503 String type = json.get(CriterionCodec.TYPE).asText();
504
505 CriterionDecoder decoder = decoderMap.get(type);
506 if (decoder != null) {
507 return decoder.decodeCriterion(json);
508 }
509
510 throw new IllegalArgumentException("Type " + type + " is unknown");
511 }
512
513
514}