blob: 4e8f8e3a65110aa0d74bffc4cccf70a87e519953 [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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());
96 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
97 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
98 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
99 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
100 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
101 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
102 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
103 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
104 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
105 formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel());
106 formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type());
107 formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code());
108 formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget());
109 formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll());
110 formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll());
111 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
Jonathan Hartcc962d82016-08-09 16:52:22 -0700112 formatMap.put(Criterion.Type.MPLS_BOS, new FormatMplsBos());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700113 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr());
114 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
115 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700116 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatTunnelId());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700117 formatMap.put(Criterion.Type.DUMMY, new FormatDummyType());
Yafit Hadar52d81552015-10-07 12:26:52 +0300118 formatMap.put(Criterion.Type.ODU_SIGID, new FormatOduSignalId());
119 formatMap.put(Criterion.Type.ODU_SIGTYPE, new FormatOduSignalType());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700120 // Currently unimplemented
121 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
122 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
123 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
124 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
125 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
126 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700127 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700128 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
129 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
130 formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown());
131 formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown());
132 formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown());
Jonathan Hart26a8d952015-12-02 15:16:35 -0800133 formatMap.put(Criterion.Type.EXTENSION, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800134 formatMap.put(Criterion.Type.ETH_DST_MASKED, new FormatUnknown());
135 formatMap.put(Criterion.Type.ETH_SRC_MASKED, new FormatUnknown());
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100136 formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatUnknown());
137 formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatUnknown());
138 formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUnknown());
139 formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUnknown());
140 formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatUnknown());
141 formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800142
Ray Milkeyd43fe452015-05-29 09:35:12 -0700143 }
144
145 private interface CriterionTypeFormatter {
146 ObjectNode encodeCriterion(ObjectNode root, Criterion criterion);
147 }
148
149 private static class FormatUnknown implements CriterionTypeFormatter {
150 @Override
151 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
152 return root;
153 }
154 }
155
156 private static class FormatInPort implements CriterionTypeFormatter {
157 @Override
158 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
159 final PortCriterion portCriterion = (PortCriterion) criterion;
160 return root.put(CriterionCodec.PORT, portCriterion.port().toLong());
161 }
162 }
163
164 private static class FormatMetadata implements CriterionTypeFormatter {
165 @Override
166 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
167 final MetadataCriterion metadataCriterion =
168 (MetadataCriterion) criterion;
169 return root.put(CriterionCodec.METADATA, metadataCriterion.metadata());
170 }
171 }
172
173 private static class FormatEth implements CriterionTypeFormatter {
174 @Override
175 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
176 final EthCriterion ethCriterion = (EthCriterion) criterion;
177 return root.put(CriterionCodec.MAC, ethCriterion.mac().toString());
178 }
179 }
180
181 private static class FormatEthType implements CriterionTypeFormatter {
182 @Override
183 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
184 final EthTypeCriterion ethTypeCriterion =
185 (EthTypeCriterion) criterion;
andread35f89c2015-11-23 10:02:07 -0800186 return root.put(CriterionCodec.ETH_TYPE, "0x"
187 + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700188 }
189 }
190
191 private static class FormatVlanVid implements CriterionTypeFormatter {
192 @Override
193 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
194 final VlanIdCriterion vlanIdCriterion =
195 (VlanIdCriterion) criterion;
196 return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort());
197 }
198 }
199
200 private static class FormatVlanPcp implements CriterionTypeFormatter {
201 @Override
202 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
203 final VlanPcpCriterion vlanPcpCriterion =
204 (VlanPcpCriterion) criterion;
205 return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority());
206 }
207 }
208
alshabibfa0dc662016-01-13 11:23:53 -0800209 private static class FormatInnerVlanVid implements CriterionTypeFormatter {
210 @Override
211 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
212 final VlanIdCriterion vlanIdCriterion =
213 (VlanIdCriterion) criterion;
214 return root.put(CriterionCodec.INNER_VLAN_ID, vlanIdCriterion.vlanId().toShort());
215 }
216 }
217
218 private static class FormatInnerVlanPcp implements CriterionTypeFormatter {
219 @Override
220 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
221 final VlanPcpCriterion vlanPcpCriterion =
222 (VlanPcpCriterion) criterion;
223 return root.put(CriterionCodec.INNER_PRIORITY, vlanPcpCriterion.priority());
224 }
225 }
226
Ray Milkeyd43fe452015-05-29 09:35:12 -0700227 private static class FormatIpDscp implements CriterionTypeFormatter {
228 @Override
229 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
230 final IPDscpCriterion ipDscpCriterion =
231 (IPDscpCriterion) criterion;
232 return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp());
233 }
234 }
235
236 private static class FormatIpEcn implements CriterionTypeFormatter {
237 @Override
238 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
239 final IPEcnCriterion ipEcnCriterion =
240 (IPEcnCriterion) criterion;
241 return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn());
242 }
243 }
244
245 private static class FormatIpProto implements CriterionTypeFormatter {
246 @Override
247 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
248 final IPProtocolCriterion iPProtocolCriterion =
249 (IPProtocolCriterion) criterion;
250 return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol());
251 }
252 }
253
254 private static class FormatIp implements CriterionTypeFormatter {
255 @Override
256 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
257 final IPCriterion iPCriterion = (IPCriterion) criterion;
258 return root.put(CriterionCodec.IP, iPCriterion.ip().toString());
259 }
260 }
261
262 private static class FormatTcp implements CriterionTypeFormatter {
263 @Override
264 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
265 final TcpPortCriterion tcpPortCriterion =
266 (TcpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700267 return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700268 }
269 }
270
271 private static class FormatUdp implements CriterionTypeFormatter {
272 @Override
273 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
274 final UdpPortCriterion udpPortCriterion =
275 (UdpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700276 return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700277 }
278 }
279
280 private static class FormatSctp implements CriterionTypeFormatter {
281 @Override
282 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
283 final SctpPortCriterion sctpPortCriterion =
284 (SctpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700285 return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700286 }
287 }
288
289 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
290 @Override
291 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
292 final IcmpTypeCriterion icmpTypeCriterion =
293 (IcmpTypeCriterion) criterion;
294 return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType());
295 }
296 }
297
298 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
299 @Override
300 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
301 final IcmpCodeCriterion icmpCodeCriterion =
302 (IcmpCodeCriterion) criterion;
303 return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode());
304 }
305 }
306
307 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
308 @Override
309 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
310 final IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
311 (IPv6FlowLabelCriterion) criterion;
312 return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel());
313 }
314 }
315
316 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
317 @Override
318 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
319 final Icmpv6TypeCriterion icmpv6TypeCriterion =
320 (Icmpv6TypeCriterion) criterion;
321 return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type());
322 }
323 }
324
325 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
326 @Override
327 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
328 final Icmpv6CodeCriterion icmpv6CodeCriterion =
329 (Icmpv6CodeCriterion) criterion;
330 return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code());
331 }
332 }
333
334 private static class FormatV6NDTarget implements CriterionTypeFormatter {
335 @Override
336 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
337 final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
338 = (IPv6NDTargetAddressCriterion) criterion;
339 return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString());
340 }
341 }
342
343 private static class FormatV6NDTll implements CriterionTypeFormatter {
344 @Override
345 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
346 final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
347 = (IPv6NDLinkLayerAddressCriterion) criterion;
348 return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString());
349 }
350 }
351
352 private static class FormatMplsLabel implements CriterionTypeFormatter {
353 @Override
354 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
355 final MplsCriterion mplsCriterion =
356 (MplsCriterion) criterion;
357 return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt());
358 }
359 }
360
Jonathan Hartcc962d82016-08-09 16:52:22 -0700361 private static class FormatMplsBos implements CriterionTypeFormatter {
362 @Override
363 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
364 final MplsBosCriterion mplsBosCriterion =
365 (MplsBosCriterion) criterion;
366 return root.put(CriterionCodec.BOS, mplsBosCriterion.mplsBos());
367 }
368 }
369
Ray Milkeyd43fe452015-05-29 09:35:12 -0700370 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
371 @Override
372 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
373 final IPv6ExthdrFlagsCriterion exthdrCriterion =
374 (IPv6ExthdrFlagsCriterion) criterion;
375 return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags());
376 }
377 }
378
379 private static class FormatOchSigId implements CriterionTypeFormatter {
380 @Override
381 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
382 OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda();
383 ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID);
384
385 child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name());
386 child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
387 child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier());
388 child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
389
390 return root;
391 }
392 }
393
394 private static class FormatOchSigType implements CriterionTypeFormatter {
395 @Override
396 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
397 final OchSignalTypeCriterion ochSignalTypeCriterion =
398 (OchSignalTypeCriterion) criterion;
Yafit Hadar52d81552015-10-07 12:26:52 +0300399 return root.put(CriterionCodec.OCH_SIGNAL_TYPE, ochSignalTypeCriterion.signalType().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700400 }
401 }
402
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700403 private static class FormatTunnelId implements CriterionTypeFormatter {
404 @Override
405 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
406 final TunnelIdCriterion tunnelIdCriterion =
407 (TunnelIdCriterion) criterion;
408 return root.put(CriterionCodec.TUNNEL_ID, tunnelIdCriterion.tunnelId());
409 }
410 }
411
Yafit Hadar52d81552015-10-07 12:26:52 +0300412 private static class FormatOduSignalId implements CriterionTypeFormatter {
413 @Override
414 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Yafit Hadar5796d972015-10-15 13:16:11 +0300415 OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId();
416 ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID);
417
418 child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
419 child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
420 child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap()));
421
422 return root;
Yafit Hadar52d81552015-10-07 12:26:52 +0300423 }
424 }
425
Yafit Hadar5796d972015-10-15 13:16:11 +0300426
Yafit Hadar52d81552015-10-07 12:26:52 +0300427 private static class FormatOduSignalType implements CriterionTypeFormatter {
428 @Override
429 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
430 final OduSignalTypeCriterion oduSignalTypeCriterion =
431 (OduSignalTypeCriterion) criterion;
432 return root.put(CriterionCodec.ODU_SIGNAL_TYPE, oduSignalTypeCriterion.signalType().name());
433 }
434 }
435
Ray Milkeyd43fe452015-05-29 09:35:12 -0700436 private class FormatDummyType implements CriterionTypeFormatter {
437
438 @Override
439 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
440 checkNotNull(criterion, "Criterion cannot be null");
441
442 return root.put(CriterionCodec.TYPE, criterion.type().toString());
443
444 }
445 }
446
447 /**
448 * Encodes a criterion into a JSON node.
449 *
450 * @return encoded JSON object for the given criterion
451 */
452 public ObjectNode encode() {
453 final ObjectNode result = context.mapper().createObjectNode()
454 .put(CriterionCodec.TYPE, criterion.type().toString());
455
456 CriterionTypeFormatter formatter =
457 checkNotNull(
458 formatMap.get(criterion.type()),
459 "No formatter found for criterion type "
460 + criterion.type().toString());
461
462 return formatter.encodeCriterion(result, criterion);
463 }
464
465}