blob: 83b25a772a6f3da8333c6670e3b66efe471c242d [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());
92 formatMap.put(Criterion.Type.ETH_SRC, new FormatEth());
93 formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType());
94 formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid());
95 formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp());
alshabibfa0dc662016-01-13 11:23:53 -080096 formatMap.put(Criterion.Type.INNER_VLAN_VID, new FormatInnerVlanVid());
97 formatMap.put(Criterion.Type.INNER_VLAN_PCP, new FormatInnerVlanPcp());
Ray Milkeyd43fe452015-05-29 09:35:12 -070098 formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp());
99 formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn());
100 formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto());
101 formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp());
102 formatMap.put(Criterion.Type.IPV4_DST, new FormatIp());
103 formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100104 formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatTcpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700105 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100106 formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatTcpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700107 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100108 formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUdpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700109 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100110 formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUdpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700111 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100112 formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatSctpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700113 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100114 formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatSctpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700115 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
116 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
117 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
118 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
119 formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel());
120 formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type());
121 formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code());
122 formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget());
123 formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll());
124 formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll());
125 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
Jonathan Hartcc962d82016-08-09 16:52:22 -0700126 formatMap.put(Criterion.Type.MPLS_BOS, new FormatMplsBos());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700127 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr());
128 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
129 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700130 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatTunnelId());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700131 formatMap.put(Criterion.Type.DUMMY, new FormatDummyType());
Yafit Hadar52d81552015-10-07 12:26:52 +0300132 formatMap.put(Criterion.Type.ODU_SIGID, new FormatOduSignalId());
133 formatMap.put(Criterion.Type.ODU_SIGTYPE, new FormatOduSignalType());
Frank Wang74ce2c12018-04-11 20:26:45 +0800134 formatMap.put(Criterion.Type.PROTOCOL_INDEPENDENT, new FormatProtocolIndependent());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700135 // Currently unimplemented
136 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
137 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
138 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
139 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
140 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
141 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700142 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700143 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
144 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
145 formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown());
146 formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown());
147 formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown());
Jonathan Hart26a8d952015-12-02 15:16:35 -0800148 formatMap.put(Criterion.Type.EXTENSION, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800149 formatMap.put(Criterion.Type.ETH_DST_MASKED, new FormatUnknown());
150 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
196 private static class FormatEthType implements CriterionTypeFormatter {
197 @Override
198 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
199 final EthTypeCriterion ethTypeCriterion =
200 (EthTypeCriterion) criterion;
andread35f89c2015-11-23 10:02:07 -0800201 return root.put(CriterionCodec.ETH_TYPE, "0x"
202 + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700203 }
204 }
205
206 private static class FormatVlanVid implements CriterionTypeFormatter {
207 @Override
208 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
209 final VlanIdCriterion vlanIdCriterion =
210 (VlanIdCriterion) criterion;
211 return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort());
212 }
213 }
214
215 private static class FormatVlanPcp implements CriterionTypeFormatter {
216 @Override
217 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
218 final VlanPcpCriterion vlanPcpCriterion =
219 (VlanPcpCriterion) criterion;
220 return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority());
221 }
222 }
223
alshabibfa0dc662016-01-13 11:23:53 -0800224 private static class FormatInnerVlanVid implements CriterionTypeFormatter {
225 @Override
226 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
227 final VlanIdCriterion vlanIdCriterion =
228 (VlanIdCriterion) criterion;
229 return root.put(CriterionCodec.INNER_VLAN_ID, vlanIdCriterion.vlanId().toShort());
230 }
231 }
232
233 private static class FormatInnerVlanPcp implements CriterionTypeFormatter {
234 @Override
235 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
236 final VlanPcpCriterion vlanPcpCriterion =
237 (VlanPcpCriterion) criterion;
238 return root.put(CriterionCodec.INNER_PRIORITY, vlanPcpCriterion.priority());
239 }
240 }
241
Ray Milkeyd43fe452015-05-29 09:35:12 -0700242 private static class FormatIpDscp implements CriterionTypeFormatter {
243 @Override
244 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
245 final IPDscpCriterion ipDscpCriterion =
246 (IPDscpCriterion) criterion;
247 return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp());
248 }
249 }
250
251 private static class FormatIpEcn implements CriterionTypeFormatter {
252 @Override
253 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
254 final IPEcnCriterion ipEcnCriterion =
255 (IPEcnCriterion) criterion;
256 return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn());
257 }
258 }
259
260 private static class FormatIpProto implements CriterionTypeFormatter {
261 @Override
262 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
263 final IPProtocolCriterion iPProtocolCriterion =
264 (IPProtocolCriterion) criterion;
265 return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol());
266 }
267 }
268
269 private static class FormatIp implements CriterionTypeFormatter {
270 @Override
271 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
272 final IPCriterion iPCriterion = (IPCriterion) criterion;
273 return root.put(CriterionCodec.IP, iPCriterion.ip().toString());
274 }
275 }
276
277 private static class FormatTcp implements CriterionTypeFormatter {
278 @Override
279 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
280 final TcpPortCriterion tcpPortCriterion =
281 (TcpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700282 return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700283 }
284 }
285
Andreas Gilbert5e959612017-12-20 10:30:18 +0100286 private static class FormatTcpMask implements CriterionTypeFormatter {
287 @Override
288 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
289 final TcpPortCriterion tcpPortCriterion =
290 (TcpPortCriterion) criterion;
291
292 root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
293 root.put(CriterionCodec.TCP_MASK, tcpPortCriterion.mask().toInt());
294
295 return root;
296 }
297 }
298
Ray Milkeyd43fe452015-05-29 09:35:12 -0700299 private static class FormatUdp implements CriterionTypeFormatter {
300 @Override
301 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
302 final UdpPortCriterion udpPortCriterion =
303 (UdpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700304 return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700305 }
306 }
307
Andreas Gilbert5e959612017-12-20 10:30:18 +0100308 private static class FormatUdpMask implements CriterionTypeFormatter {
309 @Override
310 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
311 final UdpPortCriterion udpPortCriterion =
312 (UdpPortCriterion) criterion;
313
314 root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
315 root.put(CriterionCodec.UDP_MASK, udpPortCriterion.mask().toInt());
316
317 return root;
318 }
319 }
320
Ray Milkeyd43fe452015-05-29 09:35:12 -0700321 private static class FormatSctp implements CriterionTypeFormatter {
322 @Override
323 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
324 final SctpPortCriterion sctpPortCriterion =
325 (SctpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700326 return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700327 }
328 }
329
Andreas Gilbert5e959612017-12-20 10:30:18 +0100330 private static class FormatSctpMask implements CriterionTypeFormatter {
331 @Override
332 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
333 final SctpPortCriterion sctpPortCriterion =
334 (SctpPortCriterion) criterion;
335
336 root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
337 root.put(CriterionCodec.SCTP_MASK, sctpPortCriterion.mask().toInt());
338
339 return root;
340 }
341 }
342
Ray Milkeyd43fe452015-05-29 09:35:12 -0700343 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
344 @Override
345 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
346 final IcmpTypeCriterion icmpTypeCriterion =
347 (IcmpTypeCriterion) criterion;
348 return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType());
349 }
350 }
351
352 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
353 @Override
354 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
355 final IcmpCodeCriterion icmpCodeCriterion =
356 (IcmpCodeCriterion) criterion;
357 return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode());
358 }
359 }
360
361 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
362 @Override
363 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
364 final IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
365 (IPv6FlowLabelCriterion) criterion;
366 return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel());
367 }
368 }
369
370 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
371 @Override
372 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
373 final Icmpv6TypeCriterion icmpv6TypeCriterion =
374 (Icmpv6TypeCriterion) criterion;
375 return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type());
376 }
377 }
378
379 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
380 @Override
381 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
382 final Icmpv6CodeCriterion icmpv6CodeCriterion =
383 (Icmpv6CodeCriterion) criterion;
384 return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code());
385 }
386 }
387
388 private static class FormatV6NDTarget implements CriterionTypeFormatter {
389 @Override
390 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
391 final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
392 = (IPv6NDTargetAddressCriterion) criterion;
393 return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString());
394 }
395 }
396
397 private static class FormatV6NDTll implements CriterionTypeFormatter {
398 @Override
399 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
400 final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
401 = (IPv6NDLinkLayerAddressCriterion) criterion;
402 return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString());
403 }
404 }
405
406 private static class FormatMplsLabel implements CriterionTypeFormatter {
407 @Override
408 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
409 final MplsCriterion mplsCriterion =
410 (MplsCriterion) criterion;
411 return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt());
412 }
413 }
414
Jonathan Hartcc962d82016-08-09 16:52:22 -0700415 private static class FormatMplsBos implements CriterionTypeFormatter {
416 @Override
417 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
418 final MplsBosCriterion mplsBosCriterion =
419 (MplsBosCriterion) criterion;
420 return root.put(CriterionCodec.BOS, mplsBosCriterion.mplsBos());
421 }
422 }
423
Ray Milkeyd43fe452015-05-29 09:35:12 -0700424 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
425 @Override
426 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
427 final IPv6ExthdrFlagsCriterion exthdrCriterion =
428 (IPv6ExthdrFlagsCriterion) criterion;
429 return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags());
430 }
431 }
432
433 private static class FormatOchSigId implements CriterionTypeFormatter {
434 @Override
435 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
436 OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda();
437 ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID);
438
439 child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name());
440 child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
441 child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier());
442 child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
443
444 return root;
445 }
446 }
447
448 private static class FormatOchSigType implements CriterionTypeFormatter {
449 @Override
450 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
451 final OchSignalTypeCriterion ochSignalTypeCriterion =
452 (OchSignalTypeCriterion) criterion;
Yafit Hadar52d81552015-10-07 12:26:52 +0300453 return root.put(CriterionCodec.OCH_SIGNAL_TYPE, ochSignalTypeCriterion.signalType().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700454 }
455 }
456
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700457 private static class FormatTunnelId implements CriterionTypeFormatter {
458 @Override
459 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
460 final TunnelIdCriterion tunnelIdCriterion =
461 (TunnelIdCriterion) criterion;
462 return root.put(CriterionCodec.TUNNEL_ID, tunnelIdCriterion.tunnelId());
463 }
464 }
465
Yafit Hadar52d81552015-10-07 12:26:52 +0300466 private static class FormatOduSignalId implements CriterionTypeFormatter {
467 @Override
468 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Yafit Hadar5796d972015-10-15 13:16:11 +0300469 OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId();
470 ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID);
471
472 child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
473 child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
474 child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap()));
475
476 return root;
Yafit Hadar52d81552015-10-07 12:26:52 +0300477 }
478 }
479
Yafit Hadar5796d972015-10-15 13:16:11 +0300480
Yafit Hadar52d81552015-10-07 12:26:52 +0300481 private static class FormatOduSignalType implements CriterionTypeFormatter {
482 @Override
483 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
484 final OduSignalTypeCriterion oduSignalTypeCriterion =
485 (OduSignalTypeCriterion) criterion;
486 return root.put(CriterionCodec.ODU_SIGNAL_TYPE, oduSignalTypeCriterion.signalType().name());
487 }
488 }
489
Frank Wang74ce2c12018-04-11 20:26:45 +0800490 private ObjectNode parsePiMatchExact(PiExactFieldMatch exactFieldMatch) {
491
492 ObjectNode matchExactNode = context.mapper().createObjectNode();
493 matchExactNode.put(CriterionCodec.PI_MATCH_FIELD_ID, exactFieldMatch.fieldId().id());
494 matchExactNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.EXACT.name().toLowerCase());
495 matchExactNode.put(CriterionCodec.PI_MATCH_VALUE,
496 HexString.toHexString(exactFieldMatch.value().asArray(),
497 null));
498 return matchExactNode;
499 }
500
501 private ObjectNode parsePiMatchLpm(PiLpmFieldMatch lpmFieldMatch) {
502
503 ObjectNode matchLpmNode = context.mapper().createObjectNode();
504 matchLpmNode.put(CriterionCodec.PI_MATCH_FIELD_ID, lpmFieldMatch.fieldId().id());
505 matchLpmNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.LPM.name().toLowerCase());
506 matchLpmNode.put(CriterionCodec.PI_MATCH_VALUE,
507 HexString.toHexString(lpmFieldMatch.value().asArray(),
508 null));
509 matchLpmNode.put(CriterionCodec.PI_MATCH_PREFIX, lpmFieldMatch.prefixLength());
510
511 return matchLpmNode;
512 }
513
514 private ObjectNode parsePiMatchTernary(PiTernaryFieldMatch ternaryFieldMatch) {
515
516 ObjectNode matchTernaryNode = context.mapper().createObjectNode();
517 matchTernaryNode.put(CriterionCodec.PI_MATCH_FIELD_ID, ternaryFieldMatch.fieldId().id());
518 matchTernaryNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.TERNARY.name().toLowerCase());
519 matchTernaryNode.put(CriterionCodec.PI_MATCH_VALUE,
520 HexString.toHexString(ternaryFieldMatch.value().asArray(),
521 null));
522 matchTernaryNode.put(CriterionCodec.PI_MATCH_MASK,
523 HexString.toHexString(ternaryFieldMatch.mask().asArray(),
524 null));
525
526 return matchTernaryNode;
527 }
528
529 private ObjectNode parsePiMatchRange(PiRangeFieldMatch rangeFieldMatch) {
530
531 ObjectNode matchRangeNode = context.mapper().createObjectNode();
532 matchRangeNode.put(CriterionCodec.PI_MATCH_FIELD_ID, rangeFieldMatch.fieldId().id());
533 matchRangeNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.RANGE.name().toLowerCase());
534 matchRangeNode.put(CriterionCodec.PI_MATCH_HIGH_VALUE,
535 HexString.toHexString(rangeFieldMatch.highValue().asArray(),
536 null));
537 matchRangeNode.put(CriterionCodec.PI_MATCH_LOW_VALUE,
538 HexString.toHexString(rangeFieldMatch.lowValue().asArray(),
539 null));
540
541 return matchRangeNode;
542 }
543
Frank Wang74ce2c12018-04-11 20:26:45 +0800544 private class FormatProtocolIndependent implements CriterionTypeFormatter {
545 @Override
546 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
547 final PiCriterion piCriterion = (PiCriterion) criterion;
548 ArrayNode matchNodes = context.mapper().createArrayNode();
549 for (PiFieldMatch fieldMatch : piCriterion.fieldMatches()) {
550 switch (fieldMatch.type()) {
551 case EXACT:
552 matchNodes.add(parsePiMatchExact((PiExactFieldMatch) fieldMatch));
553 break;
554 case LPM:
555 matchNodes.add(parsePiMatchLpm((PiLpmFieldMatch) fieldMatch));
556 break;
557 case TERNARY:
558 matchNodes.add(parsePiMatchTernary((PiTernaryFieldMatch) fieldMatch));
559 break;
560 case RANGE:
Frank Wang74ce2c12018-04-11 20:26:45 +0800561 matchNodes.add(parsePiMatchRange((PiRangeFieldMatch) fieldMatch));
562 break;
Frank Wang74ce2c12018-04-11 20:26:45 +0800563 default:
564 throw new IllegalArgumentException("Type " + fieldMatch.type().name() + " is unsupported");
565 }
566 }
567 return (ObjectNode) root.set(CriterionCodec.PI_MATCHES, matchNodes);
568 }
569 }
570
Ray Milkeyd43fe452015-05-29 09:35:12 -0700571 private class FormatDummyType implements CriterionTypeFormatter {
572
573 @Override
574 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
575 checkNotNull(criterion, "Criterion cannot be null");
576
577 return root.put(CriterionCodec.TYPE, criterion.type().toString());
578
579 }
580 }
581
582 /**
583 * Encodes a criterion into a JSON node.
584 *
585 * @return encoded JSON object for the given criterion
586 */
587 public ObjectNode encode() {
588 final ObjectNode result = context.mapper().createObjectNode()
589 .put(CriterionCodec.TYPE, criterion.type().toString());
590
591 CriterionTypeFormatter formatter =
592 checkNotNull(
593 formatMap.get(criterion.type()),
594 "No formatter found for criterion type "
595 + criterion.type().toString());
596
597 return formatter.encodeCriterion(result, criterion);
598 }
599
600}