blob: 9d8ac65d1bf990b98eedd42631acbbe000526b4c [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.node.ObjectNode;
Yafit Hadar5796d972015-10-15 13:16:11 +030019import org.onlab.util.HexString;
Ray Milkeyd43fe452015-05-29 09:35:12 -070020import org.onosproject.codec.CodecContext;
21import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030022import org.onosproject.net.OduSignalId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import org.onosproject.net.flow.criteria.Criterion;
24import org.onosproject.net.flow.criteria.EthCriterion;
25import org.onosproject.net.flow.criteria.EthTypeCriterion;
26import org.onosproject.net.flow.criteria.IPCriterion;
27import org.onosproject.net.flow.criteria.IPDscpCriterion;
28import org.onosproject.net.flow.criteria.IPEcnCriterion;
29import org.onosproject.net.flow.criteria.IPProtocolCriterion;
30import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
31import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
32import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
33import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
34import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
35import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
36import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
37import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
38import org.onosproject.net.flow.criteria.MetadataCriterion;
Jonathan Hartcc962d82016-08-09 16:52:22 -070039import org.onosproject.net.flow.criteria.MplsBosCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070040import org.onosproject.net.flow.criteria.MplsCriterion;
41import org.onosproject.net.flow.criteria.OchSignalCriterion;
42import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
Yafit Hadar52d81552015-10-07 12:26:52 +030043import org.onosproject.net.flow.criteria.OduSignalIdCriterion;
44import org.onosproject.net.flow.criteria.OduSignalTypeCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070045import org.onosproject.net.flow.criteria.PortCriterion;
46import org.onosproject.net.flow.criteria.SctpPortCriterion;
47import org.onosproject.net.flow.criteria.TcpPortCriterion;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -070048import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070049import org.onosproject.net.flow.criteria.UdpPortCriterion;
50import org.onosproject.net.flow.criteria.VlanIdCriterion;
51import org.onosproject.net.flow.criteria.VlanPcpCriterion;
52
andread35f89c2015-11-23 10:02:07 -080053import java.util.EnumMap;
Ray Milkeyd43fe452015-05-29 09:35:12 -070054
55import static com.google.common.base.Preconditions.checkNotNull;
56
57/**
58 * Encode portion of the criterion codec.
59 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070060public final class EncodeCriterionCodecHelper {
Ray Milkeyd43fe452015-05-29 09:35:12 -070061
62 private final Criterion criterion;
63 private final CodecContext context;
64
65 private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap;
66
67 /**
68 * Creates an encoder object for a criterion.
69 * Initializes the formatter lookup map for the criterion subclasses.
70 *
71 * @param criterion Criterion to encode
andread35f89c2015-11-23 10:02:07 -080072 * @param context context of the JSON encoding
Ray Milkeyd43fe452015-05-29 09:35:12 -070073 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070074 public EncodeCriterionCodecHelper(Criterion criterion, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070075 this.criterion = criterion;
76 this.context = context;
77
78 formatMap = new EnumMap<>(Criterion.Type.class);
79
80 formatMap.put(Criterion.Type.IN_PORT, new FormatInPort());
81 formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort());
82 formatMap.put(Criterion.Type.METADATA, new FormatMetadata());
83 formatMap.put(Criterion.Type.ETH_DST, new FormatEth());
84 formatMap.put(Criterion.Type.ETH_SRC, new FormatEth());
85 formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType());
86 formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid());
87 formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp());
alshabibfa0dc662016-01-13 11:23:53 -080088 formatMap.put(Criterion.Type.INNER_VLAN_VID, new FormatInnerVlanVid());
89 formatMap.put(Criterion.Type.INNER_VLAN_PCP, new FormatInnerVlanPcp());
Ray Milkeyd43fe452015-05-29 09:35:12 -070090 formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp());
91 formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn());
92 formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto());
93 formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp());
94 formatMap.put(Criterion.Type.IPV4_DST, new FormatIp());
95 formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp());
Andreas Gilbert5e959612017-12-20 10:30:18 +010096 formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatTcpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -070097 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
Andreas Gilbert5e959612017-12-20 10:30:18 +010098 formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatTcpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -070099 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100100 formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUdpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700101 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100102 formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUdpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700103 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100104 formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatSctpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700105 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
Andreas Gilbert5e959612017-12-20 10:30:18 +0100106 formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatSctpMask());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700107 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
108 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
109 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
110 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
111 formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel());
112 formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type());
113 formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code());
114 formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget());
115 formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll());
116 formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll());
117 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
Jonathan Hartcc962d82016-08-09 16:52:22 -0700118 formatMap.put(Criterion.Type.MPLS_BOS, new FormatMplsBos());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700119 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr());
120 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
121 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700122 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatTunnelId());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700123 formatMap.put(Criterion.Type.DUMMY, new FormatDummyType());
Yafit Hadar52d81552015-10-07 12:26:52 +0300124 formatMap.put(Criterion.Type.ODU_SIGID, new FormatOduSignalId());
125 formatMap.put(Criterion.Type.ODU_SIGTYPE, new FormatOduSignalType());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700126 // Currently unimplemented
127 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
128 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
129 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
130 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
131 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
132 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700133 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700134 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
135 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
136 formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown());
137 formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown());
138 formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown());
Jonathan Hart26a8d952015-12-02 15:16:35 -0800139 formatMap.put(Criterion.Type.EXTENSION, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800140 formatMap.put(Criterion.Type.ETH_DST_MASKED, new FormatUnknown());
141 formatMap.put(Criterion.Type.ETH_SRC_MASKED, new FormatUnknown());
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100142 formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatUnknown());
143 formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatUnknown());
144 formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUnknown());
145 formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUnknown());
146 formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatUnknown());
147 formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatUnknown());
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400148 formatMap.put(Criterion.Type.PROTOCOL_INDEPENDENT, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800149
Ray Milkeyd43fe452015-05-29 09:35:12 -0700150 }
151
152 private interface CriterionTypeFormatter {
153 ObjectNode encodeCriterion(ObjectNode root, Criterion criterion);
154 }
155
156 private static class FormatUnknown implements CriterionTypeFormatter {
157 @Override
158 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
159 return root;
160 }
161 }
162
163 private static class FormatInPort implements CriterionTypeFormatter {
164 @Override
165 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
166 final PortCriterion portCriterion = (PortCriterion) criterion;
167 return root.put(CriterionCodec.PORT, portCriterion.port().toLong());
168 }
169 }
170
171 private static class FormatMetadata implements CriterionTypeFormatter {
172 @Override
173 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
174 final MetadataCriterion metadataCriterion =
175 (MetadataCriterion) criterion;
176 return root.put(CriterionCodec.METADATA, metadataCriterion.metadata());
177 }
178 }
179
180 private static class FormatEth implements CriterionTypeFormatter {
181 @Override
182 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
183 final EthCriterion ethCriterion = (EthCriterion) criterion;
184 return root.put(CriterionCodec.MAC, ethCriterion.mac().toString());
185 }
186 }
187
188 private static class FormatEthType implements CriterionTypeFormatter {
189 @Override
190 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
191 final EthTypeCriterion ethTypeCriterion =
192 (EthTypeCriterion) criterion;
andread35f89c2015-11-23 10:02:07 -0800193 return root.put(CriterionCodec.ETH_TYPE, "0x"
194 + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700195 }
196 }
197
198 private static class FormatVlanVid implements CriterionTypeFormatter {
199 @Override
200 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
201 final VlanIdCriterion vlanIdCriterion =
202 (VlanIdCriterion) criterion;
203 return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort());
204 }
205 }
206
207 private static class FormatVlanPcp implements CriterionTypeFormatter {
208 @Override
209 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
210 final VlanPcpCriterion vlanPcpCriterion =
211 (VlanPcpCriterion) criterion;
212 return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority());
213 }
214 }
215
alshabibfa0dc662016-01-13 11:23:53 -0800216 private static class FormatInnerVlanVid implements CriterionTypeFormatter {
217 @Override
218 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
219 final VlanIdCriterion vlanIdCriterion =
220 (VlanIdCriterion) criterion;
221 return root.put(CriterionCodec.INNER_VLAN_ID, vlanIdCriterion.vlanId().toShort());
222 }
223 }
224
225 private static class FormatInnerVlanPcp implements CriterionTypeFormatter {
226 @Override
227 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
228 final VlanPcpCriterion vlanPcpCriterion =
229 (VlanPcpCriterion) criterion;
230 return root.put(CriterionCodec.INNER_PRIORITY, vlanPcpCriterion.priority());
231 }
232 }
233
Ray Milkeyd43fe452015-05-29 09:35:12 -0700234 private static class FormatIpDscp implements CriterionTypeFormatter {
235 @Override
236 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
237 final IPDscpCriterion ipDscpCriterion =
238 (IPDscpCriterion) criterion;
239 return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp());
240 }
241 }
242
243 private static class FormatIpEcn implements CriterionTypeFormatter {
244 @Override
245 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
246 final IPEcnCriterion ipEcnCriterion =
247 (IPEcnCriterion) criterion;
248 return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn());
249 }
250 }
251
252 private static class FormatIpProto implements CriterionTypeFormatter {
253 @Override
254 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
255 final IPProtocolCriterion iPProtocolCriterion =
256 (IPProtocolCriterion) criterion;
257 return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol());
258 }
259 }
260
261 private static class FormatIp implements CriterionTypeFormatter {
262 @Override
263 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
264 final IPCriterion iPCriterion = (IPCriterion) criterion;
265 return root.put(CriterionCodec.IP, iPCriterion.ip().toString());
266 }
267 }
268
269 private static class FormatTcp implements CriterionTypeFormatter {
270 @Override
271 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
272 final TcpPortCriterion tcpPortCriterion =
273 (TcpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700274 return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700275 }
276 }
277
Andreas Gilbert5e959612017-12-20 10:30:18 +0100278 private static class FormatTcpMask implements CriterionTypeFormatter {
279 @Override
280 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
281 final TcpPortCriterion tcpPortCriterion =
282 (TcpPortCriterion) criterion;
283
284 root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
285 root.put(CriterionCodec.TCP_MASK, tcpPortCriterion.mask().toInt());
286
287 return root;
288 }
289 }
290
Ray Milkeyd43fe452015-05-29 09:35:12 -0700291 private static class FormatUdp implements CriterionTypeFormatter {
292 @Override
293 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
294 final UdpPortCriterion udpPortCriterion =
295 (UdpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700296 return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700297 }
298 }
299
Andreas Gilbert5e959612017-12-20 10:30:18 +0100300 private static class FormatUdpMask implements CriterionTypeFormatter {
301 @Override
302 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
303 final UdpPortCriterion udpPortCriterion =
304 (UdpPortCriterion) criterion;
305
306 root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
307 root.put(CriterionCodec.UDP_MASK, udpPortCriterion.mask().toInt());
308
309 return root;
310 }
311 }
312
Ray Milkeyd43fe452015-05-29 09:35:12 -0700313 private static class FormatSctp implements CriterionTypeFormatter {
314 @Override
315 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
316 final SctpPortCriterion sctpPortCriterion =
317 (SctpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700318 return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700319 }
320 }
321
Andreas Gilbert5e959612017-12-20 10:30:18 +0100322 private static class FormatSctpMask implements CriterionTypeFormatter {
323 @Override
324 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
325 final SctpPortCriterion sctpPortCriterion =
326 (SctpPortCriterion) criterion;
327
328 root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
329 root.put(CriterionCodec.SCTP_MASK, sctpPortCriterion.mask().toInt());
330
331 return root;
332 }
333 }
334
Ray Milkeyd43fe452015-05-29 09:35:12 -0700335 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
336 @Override
337 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
338 final IcmpTypeCriterion icmpTypeCriterion =
339 (IcmpTypeCriterion) criterion;
340 return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType());
341 }
342 }
343
344 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
345 @Override
346 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
347 final IcmpCodeCriterion icmpCodeCriterion =
348 (IcmpCodeCriterion) criterion;
349 return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode());
350 }
351 }
352
353 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
354 @Override
355 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
356 final IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
357 (IPv6FlowLabelCriterion) criterion;
358 return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel());
359 }
360 }
361
362 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
363 @Override
364 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
365 final Icmpv6TypeCriterion icmpv6TypeCriterion =
366 (Icmpv6TypeCriterion) criterion;
367 return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type());
368 }
369 }
370
371 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
372 @Override
373 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
374 final Icmpv6CodeCriterion icmpv6CodeCriterion =
375 (Icmpv6CodeCriterion) criterion;
376 return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code());
377 }
378 }
379
380 private static class FormatV6NDTarget implements CriterionTypeFormatter {
381 @Override
382 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
383 final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
384 = (IPv6NDTargetAddressCriterion) criterion;
385 return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString());
386 }
387 }
388
389 private static class FormatV6NDTll implements CriterionTypeFormatter {
390 @Override
391 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
392 final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
393 = (IPv6NDLinkLayerAddressCriterion) criterion;
394 return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString());
395 }
396 }
397
398 private static class FormatMplsLabel implements CriterionTypeFormatter {
399 @Override
400 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
401 final MplsCriterion mplsCriterion =
402 (MplsCriterion) criterion;
403 return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt());
404 }
405 }
406
Jonathan Hartcc962d82016-08-09 16:52:22 -0700407 private static class FormatMplsBos implements CriterionTypeFormatter {
408 @Override
409 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
410 final MplsBosCriterion mplsBosCriterion =
411 (MplsBosCriterion) criterion;
412 return root.put(CriterionCodec.BOS, mplsBosCriterion.mplsBos());
413 }
414 }
415
Ray Milkeyd43fe452015-05-29 09:35:12 -0700416 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
417 @Override
418 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
419 final IPv6ExthdrFlagsCriterion exthdrCriterion =
420 (IPv6ExthdrFlagsCriterion) criterion;
421 return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags());
422 }
423 }
424
425 private static class FormatOchSigId implements CriterionTypeFormatter {
426 @Override
427 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
428 OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda();
429 ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID);
430
431 child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name());
432 child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
433 child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier());
434 child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
435
436 return root;
437 }
438 }
439
440 private static class FormatOchSigType implements CriterionTypeFormatter {
441 @Override
442 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
443 final OchSignalTypeCriterion ochSignalTypeCriterion =
444 (OchSignalTypeCriterion) criterion;
Yafit Hadar52d81552015-10-07 12:26:52 +0300445 return root.put(CriterionCodec.OCH_SIGNAL_TYPE, ochSignalTypeCriterion.signalType().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700446 }
447 }
448
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700449 private static class FormatTunnelId implements CriterionTypeFormatter {
450 @Override
451 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
452 final TunnelIdCriterion tunnelIdCriterion =
453 (TunnelIdCriterion) criterion;
454 return root.put(CriterionCodec.TUNNEL_ID, tunnelIdCriterion.tunnelId());
455 }
456 }
457
Yafit Hadar52d81552015-10-07 12:26:52 +0300458 private static class FormatOduSignalId implements CriterionTypeFormatter {
459 @Override
460 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Yafit Hadar5796d972015-10-15 13:16:11 +0300461 OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId();
462 ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID);
463
464 child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
465 child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
466 child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap()));
467
468 return root;
Yafit Hadar52d81552015-10-07 12:26:52 +0300469 }
470 }
471
Yafit Hadar5796d972015-10-15 13:16:11 +0300472
Yafit Hadar52d81552015-10-07 12:26:52 +0300473 private static class FormatOduSignalType implements CriterionTypeFormatter {
474 @Override
475 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
476 final OduSignalTypeCriterion oduSignalTypeCriterion =
477 (OduSignalTypeCriterion) criterion;
478 return root.put(CriterionCodec.ODU_SIGNAL_TYPE, oduSignalTypeCriterion.signalType().name());
479 }
480 }
481
Ray Milkeyd43fe452015-05-29 09:35:12 -0700482 private class FormatDummyType implements CriterionTypeFormatter {
483
484 @Override
485 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
486 checkNotNull(criterion, "Criterion cannot be null");
487
488 return root.put(CriterionCodec.TYPE, criterion.type().toString());
489
490 }
491 }
492
493 /**
494 * Encodes a criterion into a JSON node.
495 *
496 * @return encoded JSON object for the given criterion
497 */
498 public ObjectNode encode() {
499 final ObjectNode result = context.mapper().createObjectNode()
500 .put(CriterionCodec.TYPE, criterion.type().toString());
501
502 CriterionTypeFormatter formatter =
503 checkNotNull(
504 formatMap.get(criterion.type()),
505 "No formatter found for criterion type "
506 + criterion.type().toString());
507
508 return formatter.encodeCriterion(result, criterion);
509 }
510
511}