blob: 067a99aa62458ed2d0f46e110d7b3fdcd289a403 [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
Frank Wang74ce2c12018-04-11 20:26:45 +080018import com.fasterxml.jackson.databind.node.ArrayNode;
andread35f89c2015-11-23 10:02:07 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
Yafit Hadar5796d972015-10-15 13:16:11 +030020import org.onlab.util.HexString;
Ray Milkeyd43fe452015-05-29 09:35:12 -070021import org.onosproject.codec.CodecContext;
22import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030023import org.onosproject.net.OduSignalId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070024import org.onosproject.net.flow.criteria.Criterion;
25import org.onosproject.net.flow.criteria.EthCriterion;
26import org.onosproject.net.flow.criteria.EthTypeCriterion;
27import org.onosproject.net.flow.criteria.IPCriterion;
28import org.onosproject.net.flow.criteria.IPDscpCriterion;
29import org.onosproject.net.flow.criteria.IPEcnCriterion;
30import org.onosproject.net.flow.criteria.IPProtocolCriterion;
31import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
32import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
33import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
34import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
35import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
36import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
37import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
38import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
39import org.onosproject.net.flow.criteria.MetadataCriterion;
Jonathan Hartcc962d82016-08-09 16:52:22 -070040import org.onosproject.net.flow.criteria.MplsBosCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070041import org.onosproject.net.flow.criteria.MplsCriterion;
42import org.onosproject.net.flow.criteria.OchSignalCriterion;
43import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
Yafit Hadar52d81552015-10-07 12:26:52 +030044import org.onosproject.net.flow.criteria.OduSignalIdCriterion;
45import org.onosproject.net.flow.criteria.OduSignalTypeCriterion;
Frank Wang74ce2c12018-04-11 20:26:45 +080046import org.onosproject.net.flow.criteria.PiCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070047import org.onosproject.net.flow.criteria.PortCriterion;
48import org.onosproject.net.flow.criteria.SctpPortCriterion;
49import org.onosproject.net.flow.criteria.TcpPortCriterion;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -070050import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070051import org.onosproject.net.flow.criteria.UdpPortCriterion;
52import org.onosproject.net.flow.criteria.VlanIdCriterion;
53import org.onosproject.net.flow.criteria.VlanPcpCriterion;
Frank Wang74ce2c12018-04-11 20:26:45 +080054import org.onosproject.net.pi.model.PiMatchType;
55import org.onosproject.net.pi.runtime.PiExactFieldMatch;
56import org.onosproject.net.pi.runtime.PiFieldMatch;
57import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
58import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
59import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
Ray Milkeyd43fe452015-05-29 09:35:12 -070060
andread35f89c2015-11-23 10:02:07 -080061import java.util.EnumMap;
Ray Milkeyd43fe452015-05-29 09:35:12 -070062
63import static com.google.common.base.Preconditions.checkNotNull;
64
65/**
66 * Encode portion of the criterion codec.
67 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070068public final class EncodeCriterionCodecHelper {
Ray Milkeyd43fe452015-05-29 09:35:12 -070069
70 private final Criterion criterion;
71 private final CodecContext context;
72
73 private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap;
74
75 /**
76 * Creates an encoder object for a criterion.
77 * Initializes the formatter lookup map for the criterion subclasses.
78 *
79 * @param criterion Criterion to encode
andread35f89c2015-11-23 10:02:07 -080080 * @param context context of the JSON encoding
Ray Milkeyd43fe452015-05-29 09:35:12 -070081 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070082 public EncodeCriterionCodecHelper(Criterion criterion, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070083 this.criterion = criterion;
84 this.context = context;
85
86 formatMap = new EnumMap<>(Criterion.Type.class);
87
88 formatMap.put(Criterion.Type.IN_PORT, new FormatInPort());
89 formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort());
90 formatMap.put(Criterion.Type.METADATA, new FormatMetadata());
91 formatMap.put(Criterion.Type.ETH_DST, new FormatEth());
Seyeon Jeong8f014142020-02-26 12:51:03 -080092 formatMap.put(Criterion.Type.ETH_DST_MASKED, new FormatEthMasked());
Ray Milkeyd43fe452015-05-29 09:35:12 -070093 formatMap.put(Criterion.Type.ETH_SRC, new FormatEth());
94 formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType());
95 formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid());
96 formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp());
alshabibfa0dc662016-01-13 11:23:53 -080097 formatMap.put(Criterion.Type.INNER_VLAN_VID, new FormatInnerVlanVid());
98 formatMap.put(Criterion.Type.INNER_VLAN_PCP, new FormatInnerVlanPcp());
Ray Milkeyd43fe452015-05-29 09:35:12 -070099 formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp());
100 formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn());
101 formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto());
102 formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp());
103 formatMap.put(Criterion.Type.IPV4_DST, new FormatIp());
104 formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100105 formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatTcpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700106 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100107 formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatTcpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700108 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100109 formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUdpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700110 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100111 formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUdpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700112 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100113 formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatSctpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700114 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100115 formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatSctpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700116 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
117 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
118 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
119 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
120 formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel());
121 formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type());
122 formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code());
123 formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget());
124 formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll());
125 formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll());
126 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
Jonathan Hartcc962d82016-08-09 16:52:22 -0700127 formatMap.put(Criterion.Type.MPLS_BOS, new FormatMplsBos());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700128 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr());
129 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
130 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700131 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatTunnelId());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700132 formatMap.put(Criterion.Type.DUMMY, new FormatDummyType());
Yafit Hadar52d81552015-10-07 12:26:52 +0300133 formatMap.put(Criterion.Type.ODU_SIGID, new FormatOduSignalId());
134 formatMap.put(Criterion.Type.ODU_SIGTYPE, new FormatOduSignalType());
Frank Wang74ce2c12018-04-11 20:26:45 +0800135 formatMap.put(Criterion.Type.PROTOCOL_INDEPENDENT, new FormatProtocolIndependent());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700136 // Currently unimplemented
137 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
138 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
139 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
140 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
141 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
142 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700143 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700144 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
145 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
146 formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown());
147 formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown());
148 formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown());
Jonathan Hart26a8d952015-12-02 15:16:35 -0800149 formatMap.put(Criterion.Type.EXTENSION, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800150 formatMap.put(Criterion.Type.ETH_SRC_MASKED, new FormatUnknown());
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100151 formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatUnknown());
152 formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatUnknown());
153 formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUnknown());
154 formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUnknown());
155 formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatUnknown());
156 formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800157
Ray Milkeyd43fe452015-05-29 09:35:12 -0700158 }
159
160 private interface CriterionTypeFormatter {
161 ObjectNode encodeCriterion(ObjectNode root, Criterion criterion);
162 }
163
164 private static class FormatUnknown implements CriterionTypeFormatter {
165 @Override
166 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
167 return root;
168 }
169 }
170
171 private static class FormatInPort implements CriterionTypeFormatter {
172 @Override
173 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
174 final PortCriterion portCriterion = (PortCriterion) criterion;
175 return root.put(CriterionCodec.PORT, portCriterion.port().toLong());
176 }
177 }
178
179 private static class FormatMetadata implements CriterionTypeFormatter {
180 @Override
181 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
182 final MetadataCriterion metadataCriterion =
183 (MetadataCriterion) criterion;
184 return root.put(CriterionCodec.METADATA, metadataCriterion.metadata());
185 }
186 }
187
188 private static class FormatEth implements CriterionTypeFormatter {
189 @Override
190 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
191 final EthCriterion ethCriterion = (EthCriterion) criterion;
192 return root.put(CriterionCodec.MAC, ethCriterion.mac().toString());
193 }
194 }
195
Seyeon Jeong8f014142020-02-26 12:51:03 -0800196 private static class FormatEthMasked implements CriterionTypeFormatter {
197 @Override
198 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
199 final EthCriterion ethCriterion = (EthCriterion) criterion;
200 root.put(CriterionCodec.MAC, ethCriterion.mac().toString());
201 root.put(CriterionCodec.MAC_MASK, ethCriterion.mask().toString());
202
203 return root;
204 }
205 }
206
Ray Milkeyd43fe452015-05-29 09:35:12 -0700207 private static class FormatEthType implements CriterionTypeFormatter {
208 @Override
209 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
210 final EthTypeCriterion ethTypeCriterion =
211 (EthTypeCriterion) criterion;
andread35f89c2015-11-23 10:02:07 -0800212 return root.put(CriterionCodec.ETH_TYPE, "0x"
213 + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700214 }
215 }
216
217 private static class FormatVlanVid implements CriterionTypeFormatter {
218 @Override
219 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
220 final VlanIdCriterion vlanIdCriterion =
221 (VlanIdCriterion) criterion;
222 return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort());
223 }
224 }
225
226 private static class FormatVlanPcp implements CriterionTypeFormatter {
227 @Override
228 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
229 final VlanPcpCriterion vlanPcpCriterion =
230 (VlanPcpCriterion) criterion;
231 return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority());
232 }
233 }
234
alshabibfa0dc662016-01-13 11:23:53 -0800235 private static class FormatInnerVlanVid implements CriterionTypeFormatter {
236 @Override
237 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
238 final VlanIdCriterion vlanIdCriterion =
239 (VlanIdCriterion) criterion;
240 return root.put(CriterionCodec.INNER_VLAN_ID, vlanIdCriterion.vlanId().toShort());
241 }
242 }
243
244 private static class FormatInnerVlanPcp implements CriterionTypeFormatter {
245 @Override
246 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
247 final VlanPcpCriterion vlanPcpCriterion =
248 (VlanPcpCriterion) criterion;
249 return root.put(CriterionCodec.INNER_PRIORITY, vlanPcpCriterion.priority());
250 }
251 }
252
Ray Milkeyd43fe452015-05-29 09:35:12 -0700253 private static class FormatIpDscp implements CriterionTypeFormatter {
254 @Override
255 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
256 final IPDscpCriterion ipDscpCriterion =
257 (IPDscpCriterion) criterion;
258 return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp());
259 }
260 }
261
262 private static class FormatIpEcn implements CriterionTypeFormatter {
263 @Override
264 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
265 final IPEcnCriterion ipEcnCriterion =
266 (IPEcnCriterion) criterion;
267 return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn());
268 }
269 }
270
271 private static class FormatIpProto implements CriterionTypeFormatter {
272 @Override
273 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
274 final IPProtocolCriterion iPProtocolCriterion =
275 (IPProtocolCriterion) criterion;
276 return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol());
277 }
278 }
279
280 private static class FormatIp implements CriterionTypeFormatter {
281 @Override
282 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
283 final IPCriterion iPCriterion = (IPCriterion) criterion;
284 return root.put(CriterionCodec.IP, iPCriterion.ip().toString());
285 }
286 }
287
288 private static class FormatTcp implements CriterionTypeFormatter {
289 @Override
290 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
291 final TcpPortCriterion tcpPortCriterion =
292 (TcpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700293 return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700294 }
295 }
296
Andreas Gilbert5e959612017-12-20 10:30:18 +0100297 private static class FormatTcpMask implements CriterionTypeFormatter {
298 @Override
299 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
300 final TcpPortCriterion tcpPortCriterion =
301 (TcpPortCriterion) criterion;
302
303 root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
304 root.put(CriterionCodec.TCP_MASK, tcpPortCriterion.mask().toInt());
305
306 return root;
307 }
308 }
309
Ray Milkeyd43fe452015-05-29 09:35:12 -0700310 private static class FormatUdp implements CriterionTypeFormatter {
311 @Override
312 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
313 final UdpPortCriterion udpPortCriterion =
314 (UdpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700315 return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700316 }
317 }
318
Andreas Gilbert5e959612017-12-20 10:30:18 +0100319 private static class FormatUdpMask implements CriterionTypeFormatter {
320 @Override
321 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
322 final UdpPortCriterion udpPortCriterion =
323 (UdpPortCriterion) criterion;
324
325 root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
326 root.put(CriterionCodec.UDP_MASK, udpPortCriterion.mask().toInt());
327
328 return root;
329 }
330 }
331
Ray Milkeyd43fe452015-05-29 09:35:12 -0700332 private static class FormatSctp implements CriterionTypeFormatter {
333 @Override
334 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
335 final SctpPortCriterion sctpPortCriterion =
336 (SctpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700337 return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700338 }
339 }
340
Andreas Gilbert5e959612017-12-20 10:30:18 +0100341 private static class FormatSctpMask implements CriterionTypeFormatter {
342 @Override
343 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
344 final SctpPortCriterion sctpPortCriterion =
345 (SctpPortCriterion) criterion;
346
347 root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
348 root.put(CriterionCodec.SCTP_MASK, sctpPortCriterion.mask().toInt());
349
350 return root;
351 }
352 }
353
Ray Milkeyd43fe452015-05-29 09:35:12 -0700354 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
355 @Override
356 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
357 final IcmpTypeCriterion icmpTypeCriterion =
358 (IcmpTypeCriterion) criterion;
359 return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType());
360 }
361 }
362
363 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
364 @Override
365 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
366 final IcmpCodeCriterion icmpCodeCriterion =
367 (IcmpCodeCriterion) criterion;
368 return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode());
369 }
370 }
371
372 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
373 @Override
374 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
375 final IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
376 (IPv6FlowLabelCriterion) criterion;
377 return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel());
378 }
379 }
380
381 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
382 @Override
383 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
384 final Icmpv6TypeCriterion icmpv6TypeCriterion =
385 (Icmpv6TypeCriterion) criterion;
386 return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type());
387 }
388 }
389
390 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
391 @Override
392 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
393 final Icmpv6CodeCriterion icmpv6CodeCriterion =
394 (Icmpv6CodeCriterion) criterion;
395 return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code());
396 }
397 }
398
399 private static class FormatV6NDTarget implements CriterionTypeFormatter {
400 @Override
401 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
402 final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
403 = (IPv6NDTargetAddressCriterion) criterion;
404 return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString());
405 }
406 }
407
408 private static class FormatV6NDTll implements CriterionTypeFormatter {
409 @Override
410 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
411 final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
412 = (IPv6NDLinkLayerAddressCriterion) criterion;
413 return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString());
414 }
415 }
416
417 private static class FormatMplsLabel implements CriterionTypeFormatter {
418 @Override
419 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
420 final MplsCriterion mplsCriterion =
421 (MplsCriterion) criterion;
422 return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt());
423 }
424 }
425
Jonathan Hartcc962d82016-08-09 16:52:22 -0700426 private static class FormatMplsBos implements CriterionTypeFormatter {
427 @Override
428 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
429 final MplsBosCriterion mplsBosCriterion =
430 (MplsBosCriterion) criterion;
431 return root.put(CriterionCodec.BOS, mplsBosCriterion.mplsBos());
432 }
433 }
434
Ray Milkeyd43fe452015-05-29 09:35:12 -0700435 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
436 @Override
437 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
438 final IPv6ExthdrFlagsCriterion exthdrCriterion =
439 (IPv6ExthdrFlagsCriterion) criterion;
440 return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags());
441 }
442 }
443
444 private static class FormatOchSigId implements CriterionTypeFormatter {
445 @Override
446 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
447 OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda();
448 ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID);
449
450 child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name());
451 child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
452 child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier());
453 child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
454
455 return root;
456 }
457 }
458
459 private static class FormatOchSigType implements CriterionTypeFormatter {
460 @Override
461 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
462 final OchSignalTypeCriterion ochSignalTypeCriterion =
463 (OchSignalTypeCriterion) criterion;
Yafit Hadar52d81552015-10-07 12:26:52 +0300464 return root.put(CriterionCodec.OCH_SIGNAL_TYPE, ochSignalTypeCriterion.signalType().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700465 }
466 }
467
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700468 private static class FormatTunnelId implements CriterionTypeFormatter {
469 @Override
470 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
471 final TunnelIdCriterion tunnelIdCriterion =
472 (TunnelIdCriterion) criterion;
473 return root.put(CriterionCodec.TUNNEL_ID, tunnelIdCriterion.tunnelId());
474 }
475 }
476
Yafit Hadar52d81552015-10-07 12:26:52 +0300477 private static class FormatOduSignalId implements CriterionTypeFormatter {
478 @Override
479 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Yafit Hadar5796d972015-10-15 13:16:11 +0300480 OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId();
481 ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID);
482
483 child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
484 child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
485 child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap()));
486
487 return root;
Yafit Hadar52d81552015-10-07 12:26:52 +0300488 }
489 }
490
Yafit Hadar5796d972015-10-15 13:16:11 +0300491
Yafit Hadar52d81552015-10-07 12:26:52 +0300492 private static class FormatOduSignalType implements CriterionTypeFormatter {
493 @Override
494 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
495 final OduSignalTypeCriterion oduSignalTypeCriterion =
496 (OduSignalTypeCriterion) criterion;
497 return root.put(CriterionCodec.ODU_SIGNAL_TYPE, oduSignalTypeCriterion.signalType().name());
498 }
499 }
500
Frank Wang74ce2c12018-04-11 20:26:45 +0800501 private ObjectNode parsePiMatchExact(PiExactFieldMatch exactFieldMatch) {
502
503 ObjectNode matchExactNode = context.mapper().createObjectNode();
504 matchExactNode.put(CriterionCodec.PI_MATCH_FIELD_ID, exactFieldMatch.fieldId().id());
505 matchExactNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.EXACT.name().toLowerCase());
506 matchExactNode.put(CriterionCodec.PI_MATCH_VALUE,
507 HexString.toHexString(exactFieldMatch.value().asArray(),
508 null));
509 return matchExactNode;
510 }
511
512 private ObjectNode parsePiMatchLpm(PiLpmFieldMatch lpmFieldMatch) {
513
514 ObjectNode matchLpmNode = context.mapper().createObjectNode();
515 matchLpmNode.put(CriterionCodec.PI_MATCH_FIELD_ID, lpmFieldMatch.fieldId().id());
516 matchLpmNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.LPM.name().toLowerCase());
517 matchLpmNode.put(CriterionCodec.PI_MATCH_VALUE,
518 HexString.toHexString(lpmFieldMatch.value().asArray(),
519 null));
520 matchLpmNode.put(CriterionCodec.PI_MATCH_PREFIX, lpmFieldMatch.prefixLength());
521
522 return matchLpmNode;
523 }
524
525 private ObjectNode parsePiMatchTernary(PiTernaryFieldMatch ternaryFieldMatch) {
526
527 ObjectNode matchTernaryNode = context.mapper().createObjectNode();
528 matchTernaryNode.put(CriterionCodec.PI_MATCH_FIELD_ID, ternaryFieldMatch.fieldId().id());
529 matchTernaryNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.TERNARY.name().toLowerCase());
530 matchTernaryNode.put(CriterionCodec.PI_MATCH_VALUE,
531 HexString.toHexString(ternaryFieldMatch.value().asArray(),
532 null));
533 matchTernaryNode.put(CriterionCodec.PI_MATCH_MASK,
534 HexString.toHexString(ternaryFieldMatch.mask().asArray(),
535 null));
536
537 return matchTernaryNode;
538 }
539
540 private ObjectNode parsePiMatchRange(PiRangeFieldMatch rangeFieldMatch) {
541
542 ObjectNode matchRangeNode = context.mapper().createObjectNode();
543 matchRangeNode.put(CriterionCodec.PI_MATCH_FIELD_ID, rangeFieldMatch.fieldId().id());
544 matchRangeNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.RANGE.name().toLowerCase());
545 matchRangeNode.put(CriterionCodec.PI_MATCH_HIGH_VALUE,
546 HexString.toHexString(rangeFieldMatch.highValue().asArray(),
547 null));
548 matchRangeNode.put(CriterionCodec.PI_MATCH_LOW_VALUE,
549 HexString.toHexString(rangeFieldMatch.lowValue().asArray(),
550 null));
551
552 return matchRangeNode;
553 }
554
Frank Wang74ce2c12018-04-11 20:26:45 +0800555 private class FormatProtocolIndependent implements CriterionTypeFormatter {
556 @Override
557 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
558 final PiCriterion piCriterion = (PiCriterion) criterion;
559 ArrayNode matchNodes = context.mapper().createArrayNode();
560 for (PiFieldMatch fieldMatch : piCriterion.fieldMatches()) {
561 switch (fieldMatch.type()) {
562 case EXACT:
563 matchNodes.add(parsePiMatchExact((PiExactFieldMatch) fieldMatch));
564 break;
565 case LPM:
566 matchNodes.add(parsePiMatchLpm((PiLpmFieldMatch) fieldMatch));
567 break;
568 case TERNARY:
569 matchNodes.add(parsePiMatchTernary((PiTernaryFieldMatch) fieldMatch));
570 break;
571 case RANGE:
Frank Wang74ce2c12018-04-11 20:26:45 +0800572 matchNodes.add(parsePiMatchRange((PiRangeFieldMatch) fieldMatch));
573 break;
Frank Wang74ce2c12018-04-11 20:26:45 +0800574 default:
575 throw new IllegalArgumentException("Type " + fieldMatch.type().name() + " is unsupported");
576 }
577 }
578 return (ObjectNode) root.set(CriterionCodec.PI_MATCHES, matchNodes);
579 }
580 }
581
Ray Milkeyd43fe452015-05-29 09:35:12 -0700582 private class FormatDummyType implements CriterionTypeFormatter {
583
584 @Override
585 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
586 checkNotNull(criterion, "Criterion cannot be null");
587
588 return root.put(CriterionCodec.TYPE, criterion.type().toString());
589
590 }
591 }
592
593 /**
594 * Encodes a criterion into a JSON node.
595 *
596 * @return encoded JSON object for the given criterion
597 */
598 public ObjectNode encode() {
599 final ObjectNode result = context.mapper().createObjectNode()
600 .put(CriterionCodec.TYPE, criterion.type().toString());
601
602 CriterionTypeFormatter formatter =
603 checkNotNull(
604 formatMap.get(criterion.type()),
605 "No formatter found for criterion type "
606 + criterion.type().toString());
607
608 return formatter.encodeCriterion(result, criterion);
609 }
610
611}