blob: efdc9ff70742af06c444992ce62af974561acecd [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;
60import org.onosproject.net.pi.runtime.PiValidFieldMatch;
Ray Milkeyd43fe452015-05-29 09:35:12 -070061
andread35f89c2015-11-23 10:02:07 -080062import java.util.EnumMap;
Ray Milkeyd43fe452015-05-29 09:35:12 -070063
64import static com.google.common.base.Preconditions.checkNotNull;
65
66/**
67 * Encode portion of the criterion codec.
68 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070069public final class EncodeCriterionCodecHelper {
Ray Milkeyd43fe452015-05-29 09:35:12 -070070
71 private final Criterion criterion;
72 private final CodecContext context;
73
74 private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap;
75
76 /**
77 * Creates an encoder object for a criterion.
78 * Initializes the formatter lookup map for the criterion subclasses.
79 *
80 * @param criterion Criterion to encode
andread35f89c2015-11-23 10:02:07 -080081 * @param context context of the JSON encoding
Ray Milkeyd43fe452015-05-29 09:35:12 -070082 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070083 public EncodeCriterionCodecHelper(Criterion criterion, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070084 this.criterion = criterion;
85 this.context = context;
86
87 formatMap = new EnumMap<>(Criterion.Type.class);
88
89 formatMap.put(Criterion.Type.IN_PORT, new FormatInPort());
90 formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort());
91 formatMap.put(Criterion.Type.METADATA, new FormatMetadata());
92 formatMap.put(Criterion.Type.ETH_DST, new FormatEth());
93 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_DST_MASKED, new FormatUnknown());
151 formatMap.put(Criterion.Type.ETH_SRC_MASKED, new FormatUnknown());
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100152 formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatUnknown());
153 formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatUnknown());
154 formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUnknown());
155 formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUnknown());
156 formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatUnknown());
157 formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800158
Ray Milkeyd43fe452015-05-29 09:35:12 -0700159 }
160
161 private interface CriterionTypeFormatter {
162 ObjectNode encodeCriterion(ObjectNode root, Criterion criterion);
163 }
164
165 private static class FormatUnknown implements CriterionTypeFormatter {
166 @Override
167 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
168 return root;
169 }
170 }
171
172 private static class FormatInPort implements CriterionTypeFormatter {
173 @Override
174 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
175 final PortCriterion portCriterion = (PortCriterion) criterion;
176 return root.put(CriterionCodec.PORT, portCriterion.port().toLong());
177 }
178 }
179
180 private static class FormatMetadata implements CriterionTypeFormatter {
181 @Override
182 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
183 final MetadataCriterion metadataCriterion =
184 (MetadataCriterion) criterion;
185 return root.put(CriterionCodec.METADATA, metadataCriterion.metadata());
186 }
187 }
188
189 private static class FormatEth implements CriterionTypeFormatter {
190 @Override
191 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
192 final EthCriterion ethCriterion = (EthCriterion) criterion;
193 return root.put(CriterionCodec.MAC, ethCriterion.mac().toString());
194 }
195 }
196
197 private static class FormatEthType implements CriterionTypeFormatter {
198 @Override
199 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
200 final EthTypeCriterion ethTypeCriterion =
201 (EthTypeCriterion) criterion;
andread35f89c2015-11-23 10:02:07 -0800202 return root.put(CriterionCodec.ETH_TYPE, "0x"
203 + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700204 }
205 }
206
207 private static class FormatVlanVid implements CriterionTypeFormatter {
208 @Override
209 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
210 final VlanIdCriterion vlanIdCriterion =
211 (VlanIdCriterion) criterion;
212 return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort());
213 }
214 }
215
216 private static class FormatVlanPcp implements CriterionTypeFormatter {
217 @Override
218 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
219 final VlanPcpCriterion vlanPcpCriterion =
220 (VlanPcpCriterion) criterion;
221 return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority());
222 }
223 }
224
alshabibfa0dc662016-01-13 11:23:53 -0800225 private static class FormatInnerVlanVid implements CriterionTypeFormatter {
226 @Override
227 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
228 final VlanIdCriterion vlanIdCriterion =
229 (VlanIdCriterion) criterion;
230 return root.put(CriterionCodec.INNER_VLAN_ID, vlanIdCriterion.vlanId().toShort());
231 }
232 }
233
234 private static class FormatInnerVlanPcp implements CriterionTypeFormatter {
235 @Override
236 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
237 final VlanPcpCriterion vlanPcpCriterion =
238 (VlanPcpCriterion) criterion;
239 return root.put(CriterionCodec.INNER_PRIORITY, vlanPcpCriterion.priority());
240 }
241 }
242
Ray Milkeyd43fe452015-05-29 09:35:12 -0700243 private static class FormatIpDscp implements CriterionTypeFormatter {
244 @Override
245 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
246 final IPDscpCriterion ipDscpCriterion =
247 (IPDscpCriterion) criterion;
248 return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp());
249 }
250 }
251
252 private static class FormatIpEcn implements CriterionTypeFormatter {
253 @Override
254 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
255 final IPEcnCriterion ipEcnCriterion =
256 (IPEcnCriterion) criterion;
257 return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn());
258 }
259 }
260
261 private static class FormatIpProto implements CriterionTypeFormatter {
262 @Override
263 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
264 final IPProtocolCriterion iPProtocolCriterion =
265 (IPProtocolCriterion) criterion;
266 return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol());
267 }
268 }
269
270 private static class FormatIp implements CriterionTypeFormatter {
271 @Override
272 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
273 final IPCriterion iPCriterion = (IPCriterion) criterion;
274 return root.put(CriterionCodec.IP, iPCriterion.ip().toString());
275 }
276 }
277
278 private static class FormatTcp implements CriterionTypeFormatter {
279 @Override
280 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
281 final TcpPortCriterion tcpPortCriterion =
282 (TcpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700283 return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700284 }
285 }
286
Andreas Gilbert5e959612017-12-20 10:30:18 +0100287 private static class FormatTcpMask implements CriterionTypeFormatter {
288 @Override
289 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
290 final TcpPortCriterion tcpPortCriterion =
291 (TcpPortCriterion) criterion;
292
293 root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
294 root.put(CriterionCodec.TCP_MASK, tcpPortCriterion.mask().toInt());
295
296 return root;
297 }
298 }
299
Ray Milkeyd43fe452015-05-29 09:35:12 -0700300 private static class FormatUdp implements CriterionTypeFormatter {
301 @Override
302 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
303 final UdpPortCriterion udpPortCriterion =
304 (UdpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700305 return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700306 }
307 }
308
Andreas Gilbert5e959612017-12-20 10:30:18 +0100309 private static class FormatUdpMask implements CriterionTypeFormatter {
310 @Override
311 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
312 final UdpPortCriterion udpPortCriterion =
313 (UdpPortCriterion) criterion;
314
315 root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
316 root.put(CriterionCodec.UDP_MASK, udpPortCriterion.mask().toInt());
317
318 return root;
319 }
320 }
321
Ray Milkeyd43fe452015-05-29 09:35:12 -0700322 private static class FormatSctp implements CriterionTypeFormatter {
323 @Override
324 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
325 final SctpPortCriterion sctpPortCriterion =
326 (SctpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700327 return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700328 }
329 }
330
Andreas Gilbert5e959612017-12-20 10:30:18 +0100331 private static class FormatSctpMask implements CriterionTypeFormatter {
332 @Override
333 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
334 final SctpPortCriterion sctpPortCriterion =
335 (SctpPortCriterion) criterion;
336
337 root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
338 root.put(CriterionCodec.SCTP_MASK, sctpPortCriterion.mask().toInt());
339
340 return root;
341 }
342 }
343
Ray Milkeyd43fe452015-05-29 09:35:12 -0700344 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
345 @Override
346 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
347 final IcmpTypeCriterion icmpTypeCriterion =
348 (IcmpTypeCriterion) criterion;
349 return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType());
350 }
351 }
352
353 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
354 @Override
355 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
356 final IcmpCodeCriterion icmpCodeCriterion =
357 (IcmpCodeCriterion) criterion;
358 return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode());
359 }
360 }
361
362 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
363 @Override
364 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
365 final IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
366 (IPv6FlowLabelCriterion) criterion;
367 return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel());
368 }
369 }
370
371 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
372 @Override
373 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
374 final Icmpv6TypeCriterion icmpv6TypeCriterion =
375 (Icmpv6TypeCriterion) criterion;
376 return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type());
377 }
378 }
379
380 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
381 @Override
382 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
383 final Icmpv6CodeCriterion icmpv6CodeCriterion =
384 (Icmpv6CodeCriterion) criterion;
385 return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code());
386 }
387 }
388
389 private static class FormatV6NDTarget implements CriterionTypeFormatter {
390 @Override
391 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
392 final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
393 = (IPv6NDTargetAddressCriterion) criterion;
394 return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString());
395 }
396 }
397
398 private static class FormatV6NDTll implements CriterionTypeFormatter {
399 @Override
400 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
401 final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
402 = (IPv6NDLinkLayerAddressCriterion) criterion;
403 return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString());
404 }
405 }
406
407 private static class FormatMplsLabel implements CriterionTypeFormatter {
408 @Override
409 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
410 final MplsCriterion mplsCriterion =
411 (MplsCriterion) criterion;
412 return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt());
413 }
414 }
415
Jonathan Hartcc962d82016-08-09 16:52:22 -0700416 private static class FormatMplsBos implements CriterionTypeFormatter {
417 @Override
418 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
419 final MplsBosCriterion mplsBosCriterion =
420 (MplsBosCriterion) criterion;
421 return root.put(CriterionCodec.BOS, mplsBosCriterion.mplsBos());
422 }
423 }
424
Ray Milkeyd43fe452015-05-29 09:35:12 -0700425 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
426 @Override
427 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
428 final IPv6ExthdrFlagsCriterion exthdrCriterion =
429 (IPv6ExthdrFlagsCriterion) criterion;
430 return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags());
431 }
432 }
433
434 private static class FormatOchSigId implements CriterionTypeFormatter {
435 @Override
436 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
437 OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda();
438 ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID);
439
440 child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name());
441 child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
442 child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier());
443 child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
444
445 return root;
446 }
447 }
448
449 private static class FormatOchSigType implements CriterionTypeFormatter {
450 @Override
451 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
452 final OchSignalTypeCriterion ochSignalTypeCriterion =
453 (OchSignalTypeCriterion) criterion;
Yafit Hadar52d81552015-10-07 12:26:52 +0300454 return root.put(CriterionCodec.OCH_SIGNAL_TYPE, ochSignalTypeCriterion.signalType().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700455 }
456 }
457
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700458 private static class FormatTunnelId implements CriterionTypeFormatter {
459 @Override
460 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
461 final TunnelIdCriterion tunnelIdCriterion =
462 (TunnelIdCriterion) criterion;
463 return root.put(CriterionCodec.TUNNEL_ID, tunnelIdCriterion.tunnelId());
464 }
465 }
466
Yafit Hadar52d81552015-10-07 12:26:52 +0300467 private static class FormatOduSignalId implements CriterionTypeFormatter {
468 @Override
469 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Yafit Hadar5796d972015-10-15 13:16:11 +0300470 OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId();
471 ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID);
472
473 child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
474 child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
475 child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap()));
476
477 return root;
Yafit Hadar52d81552015-10-07 12:26:52 +0300478 }
479 }
480
Yafit Hadar5796d972015-10-15 13:16:11 +0300481
Yafit Hadar52d81552015-10-07 12:26:52 +0300482 private static class FormatOduSignalType implements CriterionTypeFormatter {
483 @Override
484 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
485 final OduSignalTypeCriterion oduSignalTypeCriterion =
486 (OduSignalTypeCriterion) criterion;
487 return root.put(CriterionCodec.ODU_SIGNAL_TYPE, oduSignalTypeCriterion.signalType().name());
488 }
489 }
490
Frank Wang74ce2c12018-04-11 20:26:45 +0800491 private ObjectNode parsePiMatchExact(PiExactFieldMatch exactFieldMatch) {
492
493 ObjectNode matchExactNode = context.mapper().createObjectNode();
494 matchExactNode.put(CriterionCodec.PI_MATCH_FIELD_ID, exactFieldMatch.fieldId().id());
495 matchExactNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.EXACT.name().toLowerCase());
496 matchExactNode.put(CriterionCodec.PI_MATCH_VALUE,
497 HexString.toHexString(exactFieldMatch.value().asArray(),
498 null));
499 return matchExactNode;
500 }
501
502 private ObjectNode parsePiMatchLpm(PiLpmFieldMatch lpmFieldMatch) {
503
504 ObjectNode matchLpmNode = context.mapper().createObjectNode();
505 matchLpmNode.put(CriterionCodec.PI_MATCH_FIELD_ID, lpmFieldMatch.fieldId().id());
506 matchLpmNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.LPM.name().toLowerCase());
507 matchLpmNode.put(CriterionCodec.PI_MATCH_VALUE,
508 HexString.toHexString(lpmFieldMatch.value().asArray(),
509 null));
510 matchLpmNode.put(CriterionCodec.PI_MATCH_PREFIX, lpmFieldMatch.prefixLength());
511
512 return matchLpmNode;
513 }
514
515 private ObjectNode parsePiMatchTernary(PiTernaryFieldMatch ternaryFieldMatch) {
516
517 ObjectNode matchTernaryNode = context.mapper().createObjectNode();
518 matchTernaryNode.put(CriterionCodec.PI_MATCH_FIELD_ID, ternaryFieldMatch.fieldId().id());
519 matchTernaryNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.TERNARY.name().toLowerCase());
520 matchTernaryNode.put(CriterionCodec.PI_MATCH_VALUE,
521 HexString.toHexString(ternaryFieldMatch.value().asArray(),
522 null));
523 matchTernaryNode.put(CriterionCodec.PI_MATCH_MASK,
524 HexString.toHexString(ternaryFieldMatch.mask().asArray(),
525 null));
526
527 return matchTernaryNode;
528 }
529
530 private ObjectNode parsePiMatchRange(PiRangeFieldMatch rangeFieldMatch) {
531
532 ObjectNode matchRangeNode = context.mapper().createObjectNode();
533 matchRangeNode.put(CriterionCodec.PI_MATCH_FIELD_ID, rangeFieldMatch.fieldId().id());
534 matchRangeNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.RANGE.name().toLowerCase());
535 matchRangeNode.put(CriterionCodec.PI_MATCH_HIGH_VALUE,
536 HexString.toHexString(rangeFieldMatch.highValue().asArray(),
537 null));
538 matchRangeNode.put(CriterionCodec.PI_MATCH_LOW_VALUE,
539 HexString.toHexString(rangeFieldMatch.lowValue().asArray(),
540 null));
541
542 return matchRangeNode;
543 }
544
545 private ObjectNode parsePiMatchValid(PiValidFieldMatch validFieldMatch) {
546
547 ObjectNode matchValidNode = context.mapper().createObjectNode();
548 matchValidNode.put(CriterionCodec.PI_MATCH_FIELD_ID, validFieldMatch.fieldId().id());
549 matchValidNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.VALID.name().toLowerCase());
550 matchValidNode.put(CriterionCodec.PI_MATCH_VALUE, validFieldMatch.isValid());
551
552 return matchValidNode;
553 }
554
555 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:
572
573 matchNodes.add(parsePiMatchRange((PiRangeFieldMatch) fieldMatch));
574 break;
575 case VALID:
576
577 matchNodes.add(parsePiMatchValid((PiValidFieldMatch) fieldMatch));
578 break;
579 default:
580 throw new IllegalArgumentException("Type " + fieldMatch.type().name() + " is unsupported");
581 }
582 }
583 return (ObjectNode) root.set(CriterionCodec.PI_MATCHES, matchNodes);
584 }
585 }
586
Ray Milkeyd43fe452015-05-29 09:35:12 -0700587 private class FormatDummyType implements CriterionTypeFormatter {
588
589 @Override
590 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
591 checkNotNull(criterion, "Criterion cannot be null");
592
593 return root.put(CriterionCodec.TYPE, criterion.type().toString());
594
595 }
596 }
597
598 /**
599 * Encodes a criterion into a JSON node.
600 *
601 * @return encoded JSON object for the given criterion
602 */
603 public ObjectNode encode() {
604 final ObjectNode result = context.mapper().createObjectNode()
605 .put(CriterionCodec.TYPE, criterion.type().toString());
606
607 CriterionTypeFormatter formatter =
608 checkNotNull(
609 formatMap.get(criterion.type()),
610 "No formatter found for criterion type "
611 + criterion.type().toString());
612
613 return formatter.encodeCriterion(result, criterion);
614 }
615
616}