blob: e8056e2ee9f96805e4454d5670445a725e4b5788 [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyd43fe452015-05-29 09:35:12 -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
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());
Andreas Gilbert5e959612017-12-20 10:30:18 +010083 decoderMap.put(Criterion.Type.TCP_SRC_MASKED.name(), new TcpSrcMaskDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -070084 decoderMap.put(Criterion.Type.TCP_DST.name(), new TcpDstDecoder());
Andreas Gilbert5e959612017-12-20 10:30:18 +010085 decoderMap.put(Criterion.Type.TCP_DST_MASKED.name(), new TcpDstMaskDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -070086 decoderMap.put(Criterion.Type.UDP_SRC.name(), new UdpSrcDecoder());
Andreas Gilbert5e959612017-12-20 10:30:18 +010087 decoderMap.put(Criterion.Type.UDP_SRC_MASKED.name(), new UdpSrcMaskDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -070088 decoderMap.put(Criterion.Type.UDP_DST.name(), new UdpDstDecoder());
Andreas Gilbert5e959612017-12-20 10:30:18 +010089 decoderMap.put(Criterion.Type.UDP_DST_MASKED.name(), new UdpDstMaskDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -070090 decoderMap.put(Criterion.Type.SCTP_SRC.name(), new SctpSrcDecoder());
Andreas Gilbert5e959612017-12-20 10:30:18 +010091 decoderMap.put(Criterion.Type.SCTP_SRC_MASKED.name(), new SctpSrcMaskDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -070092 decoderMap.put(Criterion.Type.SCTP_DST.name(), new SctpDstDecoder());
Andreas Gilbert5e959612017-12-20 10:30:18 +010093 decoderMap.put(Criterion.Type.SCTP_DST_MASKED.name(), new SctpDstMaskDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -070094 decoderMap.put(Criterion.Type.ICMPV4_TYPE.name(), new IcmpV4TypeDecoder());
95 decoderMap.put(Criterion.Type.ICMPV4_CODE.name(), new IcmpV4CodeDecoder());
96 decoderMap.put(Criterion.Type.IPV6_SRC.name(), new IpV6SrcDecoder());
97 decoderMap.put(Criterion.Type.IPV6_DST.name(), new IpV6DstDecoder());
98 decoderMap.put(Criterion.Type.IPV6_FLABEL.name(), new IpV6FLabelDecoder());
99 decoderMap.put(Criterion.Type.ICMPV6_TYPE.name(), new IcmpV6TypeDecoder());
100 decoderMap.put(Criterion.Type.ICMPV6_CODE.name(), new IcmpV6CodeDecoder());
101 decoderMap.put(Criterion.Type.IPV6_ND_TARGET.name(), new V6NDTargetDecoder());
102 decoderMap.put(Criterion.Type.IPV6_ND_SLL.name(), new V6NDSllDecoder());
103 decoderMap.put(Criterion.Type.IPV6_ND_TLL.name(), new V6NDTllDecoder());
104 decoderMap.put(Criterion.Type.MPLS_LABEL.name(), new MplsLabelDecoder());
Jonathan Hartcc962d82016-08-09 16:52:22 -0700105 decoderMap.put(Criterion.Type.MPLS_BOS.name(), new MplsBosDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700106 decoderMap.put(Criterion.Type.IPV6_EXTHDR.name(), new IpV6ExthdrDecoder());
107 decoderMap.put(Criterion.Type.OCH_SIGID.name(), new OchSigIdDecoder());
108 decoderMap.put(Criterion.Type.OCH_SIGTYPE.name(), new OchSigTypeDecoder());
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700109 decoderMap.put(Criterion.Type.TUNNEL_ID.name(), new TunnelIdDecoder());
Yafit Hadar5796d972015-10-15 13:16:11 +0300110 decoderMap.put(Criterion.Type.ODU_SIGID.name(), new OduSigIdDecoder());
111 decoderMap.put(Criterion.Type.ODU_SIGTYPE.name(), new OduSigTypeDecoder());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700112 }
113
114 private class EthTypeDecoder implements CriterionDecoder {
115 @Override
116 public Criterion decodeCriterion(ObjectNode json) {
Ray Milkeyfe447c52016-02-24 14:17:54 -0800117 JsonNode ethTypeNode = nullIsIllegal(json.get(CriterionCodec.ETH_TYPE),
118 CriterionCodec.ETH_TYPE + MISSING_MEMBER_MESSAGE);
andread35f89c2015-11-23 10:02:07 -0800119 int ethType;
Ray Milkeyfe447c52016-02-24 14:17:54 -0800120 if (ethTypeNode.isInt()) {
121 ethType = ethTypeNode.asInt();
andread35f89c2015-11-23 10:02:07 -0800122 } else {
Ray Milkeyfe447c52016-02-24 14:17:54 -0800123 ethType = Integer.decode(ethTypeNode.textValue());
andread35f89c2015-11-23 10:02:07 -0800124 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700125 return Criteria.matchEthType(ethType);
126 }
127 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700128 private class EthDstDecoder implements CriterionDecoder {
129 @Override
130 public Criterion decodeCriterion(ObjectNode json) {
131 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC),
132 CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
133
134 return Criteria.matchEthDst(mac);
135 }
136 }
137
138 private class EthSrcDecoder implements CriterionDecoder {
139 @Override
140 public Criterion decodeCriterion(ObjectNode json) {
141 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC),
142 CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
143
144 return Criteria.matchEthSrc(mac);
145 }
146 }
147
148 private class InPortDecoder implements CriterionDecoder {
149 @Override
150 public Criterion decodeCriterion(ObjectNode json) {
151 PortNumber port = PortNumber.portNumber(nullIsIllegal(json.get(CriterionCodec.PORT),
alshabibfa0dc662016-01-13 11:23:53 -0800152 CriterionCodec.PORT +
153 MISSING_MEMBER_MESSAGE).asLong());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700154
155 return Criteria.matchInPort(port);
156 }
157 }
158
159 private class InPhyPortDecoder implements CriterionDecoder {
160 @Override
161 public Criterion decodeCriterion(ObjectNode json) {
162 PortNumber port = PortNumber.portNumber(nullIsIllegal(json.get(CriterionCodec.PORT),
alshabibfa0dc662016-01-13 11:23:53 -0800163 CriterionCodec.PORT +
164 MISSING_MEMBER_MESSAGE).asLong());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700165
166 return Criteria.matchInPhyPort(port);
167 }
168 }
169
170 private class MetadataDecoder implements CriterionDecoder {
171 @Override
172 public Criterion decodeCriterion(ObjectNode json) {
173 long metadata = nullIsIllegal(json.get(CriterionCodec.METADATA),
174 CriterionCodec.METADATA + MISSING_MEMBER_MESSAGE).asLong();
175
176 return Criteria.matchMetadata(metadata);
177 }
178 }
179
180 private class VlanVidDecoder implements CriterionDecoder {
181 @Override
182 public Criterion decodeCriterion(ObjectNode json) {
183 short vlanId = (short) nullIsIllegal(json.get(CriterionCodec.VLAN_ID),
184 CriterionCodec.VLAN_ID + MISSING_MEMBER_MESSAGE).asInt();
185
186 return Criteria.matchVlanId(VlanId.vlanId(vlanId));
187 }
188 }
189
190 private class VlanPcpDecoder implements CriterionDecoder {
191 @Override
192 public Criterion decodeCriterion(ObjectNode json) {
193 byte priority = (byte) nullIsIllegal(json.get(CriterionCodec.PRIORITY),
alshabibfa0dc662016-01-13 11:23:53 -0800194 CriterionCodec.PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700195
196 return Criteria.matchVlanPcp(priority);
197 }
198 }
199
alshabibfa0dc662016-01-13 11:23:53 -0800200 private class InnerVlanVidDecoder implements CriterionDecoder {
201 @Override
202 public Criterion decodeCriterion(ObjectNode json) {
203 short vlanId = (short) nullIsIllegal(json.get(CriterionCodec.INNER_VLAN_ID),
204 CriterionCodec.INNER_VLAN_ID +
205 MISSING_MEMBER_MESSAGE).asInt();
206
207 return Criteria.matchInnerVlanId(VlanId.vlanId(vlanId));
208 }
209 }
210
211 private class InnerVlanPcpDecoder implements CriterionDecoder {
212 @Override
213 public Criterion decodeCriterion(ObjectNode json) {
214 byte priority = (byte) nullIsIllegal(json.get(CriterionCodec.INNER_PRIORITY),
215 CriterionCodec.INNER_PRIORITY +
216 MISSING_MEMBER_MESSAGE).asInt();
217
218 return Criteria.matchInnerVlanPcp(priority);
219 }
220 }
221
Ray Milkeyd43fe452015-05-29 09:35:12 -0700222 private class IpDscpDecoder implements CriterionDecoder {
223 @Override
224 public Criterion decodeCriterion(ObjectNode json) {
225 byte ipDscp = (byte) nullIsIllegal(json.get(CriterionCodec.IP_DSCP),
226 CriterionCodec.IP_DSCP + MISSING_MEMBER_MESSAGE).asInt();
227 return Criteria.matchIPDscp(ipDscp);
228 }
229 }
230
231 private class IpEcnDecoder implements CriterionDecoder {
232 @Override
233 public Criterion decodeCriterion(ObjectNode json) {
234 byte ipEcn = (byte) nullIsIllegal(json.get(CriterionCodec.IP_ECN),
235 CriterionCodec.IP_ECN + MISSING_MEMBER_MESSAGE).asInt();
236 return Criteria.matchIPEcn(ipEcn);
237 }
238 }
239
240 private class IpProtoDecoder implements CriterionDecoder {
241 @Override
242 public Criterion decodeCriterion(ObjectNode json) {
243 short proto = (short) nullIsIllegal(json.get(CriterionCodec.PROTOCOL),
244 CriterionCodec.PROTOCOL + MISSING_MEMBER_MESSAGE).asInt();
245 return Criteria.matchIPProtocol(proto);
246 }
247 }
248
249 private class IpV4SrcDecoder implements CriterionDecoder {
250 @Override
251 public Criterion decodeCriterion(ObjectNode json) {
252 String ip = nullIsIllegal(json.get(CriterionCodec.IP),
253 CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText();
254 return Criteria.matchIPSrc(IpPrefix.valueOf(ip));
255 }
256 }
257
258 private class IpV4DstDecoder implements CriterionDecoder {
259 @Override
260 public Criterion decodeCriterion(ObjectNode json) {
261 String ip = nullIsIllegal(json.get(CriterionCodec.IP),
262 CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText();
263 return Criteria.matchIPDst(IpPrefix.valueOf(ip));
264 }
265 }
266
267 private class IpV6SrcDecoder implements CriterionDecoder {
268 @Override
269 public Criterion decodeCriterion(ObjectNode json) {
270 String ip = nullIsIllegal(json.get(CriterionCodec.IP),
271 CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText();
272 return Criteria.matchIPv6Src(IpPrefix.valueOf(ip));
273 }
274 }
275
276 private class IpV6DstDecoder implements CriterionDecoder {
277 @Override
278 public Criterion decodeCriterion(ObjectNode json) {
279 String ip = nullIsIllegal(json.get(CriterionCodec.IP),
280 CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText();
281 return Criteria.matchIPv6Dst(IpPrefix.valueOf(ip));
282 }
283 }
284
285 private class TcpSrcDecoder implements CriterionDecoder {
286 @Override
287 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700288 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_PORT),
289 CriterionCodec.TCP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700290 return Criteria.matchTcpSrc(tcpPort);
291 }
292 }
293
Andreas Gilbert5e959612017-12-20 10:30:18 +0100294 private class TcpSrcMaskDecoder implements CriterionDecoder {
295 @Override
296 public Criterion decodeCriterion(ObjectNode json) {
297 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_PORT),
298 CriterionCodec.TCP_PORT + MISSING_MEMBER_MESSAGE).asInt());
299
300 TpPort tcpMask = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_MASK),
301 CriterionCodec.TCP_MASK + MISSING_MEMBER_MESSAGE).asInt());
302
303 return Criteria.matchTcpSrcMasked(tcpPort, tcpMask);
304 }
305 }
306
Ray Milkeyd43fe452015-05-29 09:35:12 -0700307 private class TcpDstDecoder implements CriterionDecoder {
308 @Override
309 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700310 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_PORT),
311 CriterionCodec.TCP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700312 return Criteria.matchTcpDst(tcpPort);
313 }
314 }
315
Andreas Gilbert5e959612017-12-20 10:30:18 +0100316 private class TcpDstMaskDecoder implements CriterionDecoder {
317 @Override
318 public Criterion decodeCriterion(ObjectNode json) {
319 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_PORT),
320 CriterionCodec.TCP_PORT + MISSING_MEMBER_MESSAGE).asInt());
321
322 TpPort tcpMask = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_MASK),
323 CriterionCodec.TCP_MASK + MISSING_MEMBER_MESSAGE).asInt());
324
325 return Criteria.matchTcpDstMasked(tcpPort, tcpMask);
326 }
327 }
328
Ray Milkeyd43fe452015-05-29 09:35:12 -0700329 private class UdpSrcDecoder implements CriterionDecoder {
330 @Override
331 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700332 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_PORT),
333 CriterionCodec.UDP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700334 return Criteria.matchUdpSrc(udpPort);
335 }
336 }
337
Andreas Gilbert5e959612017-12-20 10:30:18 +0100338 private class UdpSrcMaskDecoder implements CriterionDecoder {
339 @Override
340 public Criterion decodeCriterion(ObjectNode json) {
341 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_PORT),
342 CriterionCodec.UDP_PORT + MISSING_MEMBER_MESSAGE).asInt());
343
344 TpPort udpMask = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_MASK),
345 CriterionCodec.UDP_MASK + MISSING_MEMBER_MESSAGE).asInt());
346
347 return Criteria.matchUdpSrcMasked(udpPort, udpMask);
348 }
349 }
350
Ray Milkeyd43fe452015-05-29 09:35:12 -0700351 private class UdpDstDecoder implements CriterionDecoder {
352 @Override
353 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700354 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_PORT),
355 CriterionCodec.UDP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700356 return Criteria.matchUdpDst(udpPort);
357 }
358 }
359
Andreas Gilbert5e959612017-12-20 10:30:18 +0100360 private class UdpDstMaskDecoder implements CriterionDecoder {
361 @Override
362 public Criterion decodeCriterion(ObjectNode json) {
363 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_PORT),
364 CriterionCodec.UDP_PORT + MISSING_MEMBER_MESSAGE).asInt());
365
366 TpPort udpMask = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_MASK),
367 CriterionCodec.UDP_MASK + MISSING_MEMBER_MESSAGE).asInt());
368
369 return Criteria.matchUdpDstMasked(udpPort, udpMask);
370 }
371 }
372
Ray Milkeyd43fe452015-05-29 09:35:12 -0700373 private class SctpSrcDecoder implements CriterionDecoder {
374 @Override
375 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700376 TpPort sctpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_PORT),
377 CriterionCodec.SCTP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700378 return Criteria.matchSctpSrc(sctpPort);
379 }
380 }
381
Andreas Gilbert5e959612017-12-20 10:30:18 +0100382 private class SctpSrcMaskDecoder implements CriterionDecoder {
383 @Override
384 public Criterion decodeCriterion(ObjectNode json) {
385 TpPort sctpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_PORT),
386 CriterionCodec.SCTP_PORT + MISSING_MEMBER_MESSAGE).asInt());
387
388 TpPort sctpMask = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_MASK),
389 CriterionCodec.SCTP_MASK + MISSING_MEMBER_MESSAGE).asInt());
390
391 return Criteria.matchSctpSrcMasked(sctpPort, sctpMask);
392 }
393 }
394
Ray Milkeyd43fe452015-05-29 09:35:12 -0700395 private class SctpDstDecoder implements CriterionDecoder {
396 @Override
397 public Criterion decodeCriterion(ObjectNode json) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700398 TpPort sctpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_PORT),
399 CriterionCodec.SCTP_PORT + MISSING_MEMBER_MESSAGE).asInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700400 return Criteria.matchSctpDst(sctpPort);
401 }
402 }
403
Andreas Gilbert5e959612017-12-20 10:30:18 +0100404 private class SctpDstMaskDecoder implements CriterionDecoder {
405 @Override
406 public Criterion decodeCriterion(ObjectNode json) {
407 TpPort sctpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_PORT),
408 CriterionCodec.SCTP_PORT + MISSING_MEMBER_MESSAGE).asInt());
409
410 TpPort sctpMask = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_MASK),
411 CriterionCodec.SCTP_MASK + MISSING_MEMBER_MESSAGE).asInt());
412
413 return Criteria.matchSctpDstMasked(sctpPort, sctpMask);
414 }
415 }
416
Ray Milkeyd43fe452015-05-29 09:35:12 -0700417 private class IcmpV4TypeDecoder implements CriterionDecoder {
418 @Override
419 public Criterion decodeCriterion(ObjectNode json) {
420 short type = (short) nullIsIllegal(json.get(CriterionCodec.ICMP_TYPE),
421 CriterionCodec.ICMP_TYPE + MISSING_MEMBER_MESSAGE).asInt();
422 return Criteria.matchIcmpType(type);
423 }
424 }
425
426 private class IcmpV4CodeDecoder implements CriterionDecoder {
427 @Override
428 public Criterion decodeCriterion(ObjectNode json) {
429 short code = (short) nullIsIllegal(json.get(CriterionCodec.ICMP_CODE),
430 CriterionCodec.ICMP_CODE + MISSING_MEMBER_MESSAGE).asInt();
431 return Criteria.matchIcmpCode(code);
432 }
433 }
434
435 private class IpV6FLabelDecoder implements CriterionDecoder {
436 @Override
437 public Criterion decodeCriterion(ObjectNode json) {
438 int flowLabel = nullIsIllegal(json.get(CriterionCodec.FLOW_LABEL),
439 CriterionCodec.FLOW_LABEL + MISSING_MEMBER_MESSAGE).asInt();
440 return Criteria.matchIPv6FlowLabel(flowLabel);
441 }
442 }
443
444 private class IcmpV6TypeDecoder implements CriterionDecoder {
445 @Override
446 public Criterion decodeCriterion(ObjectNode json) {
447 short type = (short) nullIsIllegal(json.get(CriterionCodec.ICMPV6_TYPE),
448 CriterionCodec.ICMPV6_TYPE + MISSING_MEMBER_MESSAGE).asInt();
449 return Criteria.matchIcmpv6Type(type);
450 }
451 }
452
453 private class IcmpV6CodeDecoder implements CriterionDecoder {
454 @Override
455 public Criterion decodeCriterion(ObjectNode json) {
456 short code = (short) nullIsIllegal(json.get(CriterionCodec.ICMPV6_CODE),
457 CriterionCodec.ICMPV6_CODE + MISSING_MEMBER_MESSAGE).asInt();
458 return Criteria.matchIcmpv6Code(code);
459 }
460 }
461
462 private class V6NDTargetDecoder implements CriterionDecoder {
463 @Override
464 public Criterion decodeCriterion(ObjectNode json) {
465 Ip6Address target = Ip6Address.valueOf(nullIsIllegal(json.get(CriterionCodec.TARGET_ADDRESS),
466 CriterionCodec.TARGET_ADDRESS + MISSING_MEMBER_MESSAGE).asText());
467 return Criteria.matchIPv6NDTargetAddress(target);
468 }
469 }
470
471 private class V6NDSllDecoder implements CriterionDecoder {
472 @Override
473 public Criterion decodeCriterion(ObjectNode json) {
474 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC),
475 CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
476 return Criteria.matchIPv6NDSourceLinkLayerAddress(mac);
477 }
478 }
479
480 private class V6NDTllDecoder implements CriterionDecoder {
481 @Override
482 public Criterion decodeCriterion(ObjectNode json) {
483 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC),
484 CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
485 return Criteria.matchIPv6NDTargetLinkLayerAddress(mac);
486 }
487 }
488
489 private class MplsLabelDecoder implements CriterionDecoder {
490 @Override
491 public Criterion decodeCriterion(ObjectNode json) {
492 int label = nullIsIllegal(json.get(CriterionCodec.LABEL),
493 CriterionCodec.LABEL + MISSING_MEMBER_MESSAGE).asInt();
494 return Criteria.matchMplsLabel(MplsLabel.mplsLabel(label));
495 }
496 }
497
Jonathan Hartcc962d82016-08-09 16:52:22 -0700498 private class MplsBosDecoder implements CriterionDecoder {
499 @Override
500 public Criterion decodeCriterion(ObjectNode json) {
501 boolean bos = nullIsIllegal(json.get(CriterionCodec.BOS),
502 CriterionCodec.BOS + MISSING_MEMBER_MESSAGE).asBoolean();
503 return Criteria.matchMplsBos(bos);
504 }
505 }
506
Ray Milkeyd43fe452015-05-29 09:35:12 -0700507 private class IpV6ExthdrDecoder implements CriterionDecoder {
508 @Override
509 public Criterion decodeCriterion(ObjectNode json) {
510 int exthdrFlags = nullIsIllegal(json.get(CriterionCodec.EXT_HDR_FLAGS),
511 CriterionCodec.EXT_HDR_FLAGS + MISSING_MEMBER_MESSAGE).asInt();
512 return Criteria.matchIPv6ExthdrFlags(exthdrFlags);
513 }
514 }
515
516 private class OchSigIdDecoder implements CriterionDecoder {
517 @Override
518 public Criterion decodeCriterion(ObjectNode json) {
Sho SHIMIZUcc137a92016-03-11 15:10:54 -0800519 JsonNode ochSignalId = nullIsIllegal(json.get(CriterionCodec.OCH_SIGNAL_ID),
520 CriterionCodec.GRID_TYPE + MISSING_MEMBER_MESSAGE);
521 GridType gridType =
522 GridType.valueOf(
523 nullIsIllegal(ochSignalId.get(CriterionCodec.GRID_TYPE),
524 CriterionCodec.GRID_TYPE + MISSING_MEMBER_MESSAGE).asText());
525 ChannelSpacing channelSpacing =
526 ChannelSpacing.valueOf(
527 nullIsIllegal(ochSignalId.get(CriterionCodec.CHANNEL_SPACING),
528 CriterionCodec.CHANNEL_SPACING + MISSING_MEMBER_MESSAGE).asText());
529 int spacingMultiplier = nullIsIllegal(ochSignalId.get(CriterionCodec.SPACING_MULIPLIER),
530 CriterionCodec.SPACING_MULIPLIER + MISSING_MEMBER_MESSAGE).asInt();
531 int slotGranularity = nullIsIllegal(ochSignalId.get(CriterionCodec.SLOT_GRANULARITY),
532 CriterionCodec.SLOT_GRANULARITY + MISSING_MEMBER_MESSAGE).asInt();
533 return Criteria.matchLambda(
534 Lambda.ochSignal(gridType, channelSpacing,
535 spacingMultiplier, slotGranularity));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700536 }
537 }
538
539 private class OchSigTypeDecoder implements CriterionDecoder {
540 @Override
541 public Criterion decodeCriterion(ObjectNode json) {
Yafit Hadar5796d972015-10-15 13:16:11 +0300542 OchSignalType ochSignalType = OchSignalType.valueOf(nullIsIllegal(json.get(CriterionCodec.OCH_SIGNAL_TYPE),
543 CriterionCodec.OCH_SIGNAL_TYPE + MISSING_MEMBER_MESSAGE).asText());
544 return Criteria.matchOchSignalType(ochSignalType);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700545 }
546 }
547
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700548 private class TunnelIdDecoder implements CriterionDecoder {
549 @Override
550 public Criterion decodeCriterion(ObjectNode json) {
551 long tunnelId = nullIsIllegal(json.get(CriterionCodec.TUNNEL_ID),
552 CriterionCodec.TUNNEL_ID + MISSING_MEMBER_MESSAGE).asLong();
553 return Criteria.matchTunnelId(tunnelId);
554 }
555 }
556
Yafit Hadar5796d972015-10-15 13:16:11 +0300557 private class OduSigIdDecoder implements CriterionDecoder {
558 @Override
559 public Criterion decodeCriterion(ObjectNode json) {
560 JsonNode oduSignalId = nullIsIllegal(json.get(CriterionCodec.ODU_SIGNAL_ID),
561 CriterionCodec.TRIBUTARY_PORT_NUMBER + MISSING_MEMBER_MESSAGE);
562
563 int tributaryPortNumber = nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_PORT_NUMBER),
564 CriterionCodec.TRIBUTARY_PORT_NUMBER + MISSING_MEMBER_MESSAGE).asInt();
565 int tributarySlotLen = nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_SLOT_LEN),
566 CriterionCodec.TRIBUTARY_SLOT_LEN + MISSING_MEMBER_MESSAGE).asInt();
567 byte[] tributarySlotBitmap = HexString.fromHexString(
568 nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_SLOT_BITMAP),
569 CriterionCodec.TRIBUTARY_SLOT_BITMAP + MISSING_MEMBER_MESSAGE).asText());
570
571 return Criteria.matchOduSignalId(
572 OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap));
573 }
574 }
575
576 private class OduSigTypeDecoder implements CriterionDecoder {
577 @Override
578 public Criterion decodeCriterion(ObjectNode json) {
579 OduSignalType oduSignalType = OduSignalType.valueOf(nullIsIllegal(json.get(CriterionCodec.ODU_SIGNAL_TYPE),
580 CriterionCodec.ODU_SIGNAL_TYPE + MISSING_MEMBER_MESSAGE).asText());
581 return Criteria.matchOduSignalType(oduSignalType);
582 }
583 }
584
Ray Milkeyd43fe452015-05-29 09:35:12 -0700585 /**
586 * Decodes the JSON into a criterion object.
587 *
588 * @return Criterion object
589 * @throws IllegalArgumentException if the JSON is invalid
590 */
591 public Criterion decode() {
Ray Milkey52075062016-02-03 17:36:48 -0800592 String type =
593 nullIsIllegal(json.get(CriterionCodec.TYPE), "Type not specified")
594 .asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700595
596 CriterionDecoder decoder = decoderMap.get(type);
597 if (decoder != null) {
598 return decoder.decodeCriterion(json);
599 }
600
601 throw new IllegalArgumentException("Type " + type + " is unknown");
602 }
603
604
605}