blob: 0010848405f184f0b04f334275b4022d97f9c0e8 [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;
39import org.onosproject.net.flow.criteria.MplsCriterion;
40import org.onosproject.net.flow.criteria.OchSignalCriterion;
41import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
Yafit Hadar52d81552015-10-07 12:26:52 +030042import org.onosproject.net.flow.criteria.OduSignalIdCriterion;
43import org.onosproject.net.flow.criteria.OduSignalTypeCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070044import org.onosproject.net.flow.criteria.PortCriterion;
45import org.onosproject.net.flow.criteria.SctpPortCriterion;
46import org.onosproject.net.flow.criteria.TcpPortCriterion;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -070047import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070048import org.onosproject.net.flow.criteria.UdpPortCriterion;
49import org.onosproject.net.flow.criteria.VlanIdCriterion;
50import org.onosproject.net.flow.criteria.VlanPcpCriterion;
51
andread35f89c2015-11-23 10:02:07 -080052import java.util.EnumMap;
Ray Milkeyd43fe452015-05-29 09:35:12 -070053
54import static com.google.common.base.Preconditions.checkNotNull;
55
56/**
57 * Encode portion of the criterion codec.
58 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070059public final class EncodeCriterionCodecHelper {
Ray Milkeyd43fe452015-05-29 09:35:12 -070060
61 private final Criterion criterion;
62 private final CodecContext context;
63
64 private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap;
65
66 /**
67 * Creates an encoder object for a criterion.
68 * Initializes the formatter lookup map for the criterion subclasses.
69 *
70 * @param criterion Criterion to encode
andread35f89c2015-11-23 10:02:07 -080071 * @param context context of the JSON encoding
Ray Milkeyd43fe452015-05-29 09:35:12 -070072 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070073 public EncodeCriterionCodecHelper(Criterion criterion, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070074 this.criterion = criterion;
75 this.context = context;
76
77 formatMap = new EnumMap<>(Criterion.Type.class);
78
79 formatMap.put(Criterion.Type.IN_PORT, new FormatInPort());
80 formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort());
81 formatMap.put(Criterion.Type.METADATA, new FormatMetadata());
82 formatMap.put(Criterion.Type.ETH_DST, new FormatEth());
83 formatMap.put(Criterion.Type.ETH_SRC, new FormatEth());
84 formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType());
85 formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid());
86 formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp());
alshabibfa0dc662016-01-13 11:23:53 -080087 formatMap.put(Criterion.Type.INNER_VLAN_VID, new FormatInnerVlanVid());
88 formatMap.put(Criterion.Type.INNER_VLAN_PCP, new FormatInnerVlanPcp());
Ray Milkeyd43fe452015-05-29 09:35:12 -070089 formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp());
90 formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn());
91 formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto());
92 formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp());
93 formatMap.put(Criterion.Type.IPV4_DST, new FormatIp());
94 formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp());
95 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
96 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
97 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
98 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
99 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
100 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
101 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
102 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
103 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
104 formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel());
105 formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type());
106 formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code());
107 formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget());
108 formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll());
109 formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll());
110 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
111 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr());
112 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
113 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700114 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatTunnelId());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700115 formatMap.put(Criterion.Type.DUMMY, new FormatDummyType());
Yafit Hadar52d81552015-10-07 12:26:52 +0300116 formatMap.put(Criterion.Type.ODU_SIGID, new FormatOduSignalId());
117 formatMap.put(Criterion.Type.ODU_SIGTYPE, new FormatOduSignalType());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700118 // Currently unimplemented
119 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
120 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
121 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
122 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
123 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
124 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
125 formatMap.put(Criterion.Type.MPLS_BOS, new FormatUnknown());
126 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700127 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
128 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
129 formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown());
130 formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown());
131 formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown());
Jonathan Hart26a8d952015-12-02 15:16:35 -0800132 formatMap.put(Criterion.Type.EXTENSION, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800133 formatMap.put(Criterion.Type.ETH_DST_MASKED, new FormatUnknown());
134 formatMap.put(Criterion.Type.ETH_SRC_MASKED, new FormatUnknown());
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100135 formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatUnknown());
136 formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatUnknown());
137 formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUnknown());
138 formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUnknown());
139 formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatUnknown());
140 formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatUnknown());
Saurav Das9d6c86b2016-02-19 09:01:07 -0800141
Ray Milkeyd43fe452015-05-29 09:35:12 -0700142 }
143
144 private interface CriterionTypeFormatter {
145 ObjectNode encodeCriterion(ObjectNode root, Criterion criterion);
146 }
147
148 private static class FormatUnknown implements CriterionTypeFormatter {
149 @Override
150 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
151 return root;
152 }
153 }
154
155 private static class FormatInPort implements CriterionTypeFormatter {
156 @Override
157 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
158 final PortCriterion portCriterion = (PortCriterion) criterion;
159 return root.put(CriterionCodec.PORT, portCriterion.port().toLong());
160 }
161 }
162
163 private static class FormatMetadata implements CriterionTypeFormatter {
164 @Override
165 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
166 final MetadataCriterion metadataCriterion =
167 (MetadataCriterion) criterion;
168 return root.put(CriterionCodec.METADATA, metadataCriterion.metadata());
169 }
170 }
171
172 private static class FormatEth implements CriterionTypeFormatter {
173 @Override
174 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
175 final EthCriterion ethCriterion = (EthCriterion) criterion;
176 return root.put(CriterionCodec.MAC, ethCriterion.mac().toString());
177 }
178 }
179
180 private static class FormatEthType implements CriterionTypeFormatter {
181 @Override
182 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
183 final EthTypeCriterion ethTypeCriterion =
184 (EthTypeCriterion) criterion;
andread35f89c2015-11-23 10:02:07 -0800185 return root.put(CriterionCodec.ETH_TYPE, "0x"
186 + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700187 }
188 }
189
190 private static class FormatVlanVid implements CriterionTypeFormatter {
191 @Override
192 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
193 final VlanIdCriterion vlanIdCriterion =
194 (VlanIdCriterion) criterion;
195 return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort());
196 }
197 }
198
199 private static class FormatVlanPcp implements CriterionTypeFormatter {
200 @Override
201 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
202 final VlanPcpCriterion vlanPcpCriterion =
203 (VlanPcpCriterion) criterion;
204 return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority());
205 }
206 }
207
alshabibfa0dc662016-01-13 11:23:53 -0800208 private static class FormatInnerVlanVid implements CriterionTypeFormatter {
209 @Override
210 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
211 final VlanIdCriterion vlanIdCriterion =
212 (VlanIdCriterion) criterion;
213 return root.put(CriterionCodec.INNER_VLAN_ID, vlanIdCriterion.vlanId().toShort());
214 }
215 }
216
217 private static class FormatInnerVlanPcp implements CriterionTypeFormatter {
218 @Override
219 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
220 final VlanPcpCriterion vlanPcpCriterion =
221 (VlanPcpCriterion) criterion;
222 return root.put(CriterionCodec.INNER_PRIORITY, vlanPcpCriterion.priority());
223 }
224 }
225
Ray Milkeyd43fe452015-05-29 09:35:12 -0700226 private static class FormatIpDscp implements CriterionTypeFormatter {
227 @Override
228 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
229 final IPDscpCriterion ipDscpCriterion =
230 (IPDscpCriterion) criterion;
231 return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp());
232 }
233 }
234
235 private static class FormatIpEcn implements CriterionTypeFormatter {
236 @Override
237 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
238 final IPEcnCriterion ipEcnCriterion =
239 (IPEcnCriterion) criterion;
240 return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn());
241 }
242 }
243
244 private static class FormatIpProto implements CriterionTypeFormatter {
245 @Override
246 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
247 final IPProtocolCriterion iPProtocolCriterion =
248 (IPProtocolCriterion) criterion;
249 return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol());
250 }
251 }
252
253 private static class FormatIp implements CriterionTypeFormatter {
254 @Override
255 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
256 final IPCriterion iPCriterion = (IPCriterion) criterion;
257 return root.put(CriterionCodec.IP, iPCriterion.ip().toString());
258 }
259 }
260
261 private static class FormatTcp implements CriterionTypeFormatter {
262 @Override
263 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
264 final TcpPortCriterion tcpPortCriterion =
265 (TcpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700266 return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700267 }
268 }
269
270 private static class FormatUdp implements CriterionTypeFormatter {
271 @Override
272 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
273 final UdpPortCriterion udpPortCriterion =
274 (UdpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700275 return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700276 }
277 }
278
279 private static class FormatSctp implements CriterionTypeFormatter {
280 @Override
281 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
282 final SctpPortCriterion sctpPortCriterion =
283 (SctpPortCriterion) criterion;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700284 return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700285 }
286 }
287
288 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
289 @Override
290 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
291 final IcmpTypeCriterion icmpTypeCriterion =
292 (IcmpTypeCriterion) criterion;
293 return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType());
294 }
295 }
296
297 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
298 @Override
299 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
300 final IcmpCodeCriterion icmpCodeCriterion =
301 (IcmpCodeCriterion) criterion;
302 return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode());
303 }
304 }
305
306 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
307 @Override
308 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
309 final IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
310 (IPv6FlowLabelCriterion) criterion;
311 return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel());
312 }
313 }
314
315 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
316 @Override
317 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
318 final Icmpv6TypeCriterion icmpv6TypeCriterion =
319 (Icmpv6TypeCriterion) criterion;
320 return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type());
321 }
322 }
323
324 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
325 @Override
326 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
327 final Icmpv6CodeCriterion icmpv6CodeCriterion =
328 (Icmpv6CodeCriterion) criterion;
329 return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code());
330 }
331 }
332
333 private static class FormatV6NDTarget implements CriterionTypeFormatter {
334 @Override
335 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
336 final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
337 = (IPv6NDTargetAddressCriterion) criterion;
338 return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString());
339 }
340 }
341
342 private static class FormatV6NDTll implements CriterionTypeFormatter {
343 @Override
344 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
345 final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
346 = (IPv6NDLinkLayerAddressCriterion) criterion;
347 return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString());
348 }
349 }
350
351 private static class FormatMplsLabel implements CriterionTypeFormatter {
352 @Override
353 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
354 final MplsCriterion mplsCriterion =
355 (MplsCriterion) criterion;
356 return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt());
357 }
358 }
359
360 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
361 @Override
362 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
363 final IPv6ExthdrFlagsCriterion exthdrCriterion =
364 (IPv6ExthdrFlagsCriterion) criterion;
365 return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags());
366 }
367 }
368
369 private static class FormatOchSigId implements CriterionTypeFormatter {
370 @Override
371 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
372 OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda();
373 ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID);
374
375 child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name());
376 child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
377 child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier());
378 child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
379
380 return root;
381 }
382 }
383
384 private static class FormatOchSigType implements CriterionTypeFormatter {
385 @Override
386 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
387 final OchSignalTypeCriterion ochSignalTypeCriterion =
388 (OchSignalTypeCriterion) criterion;
Yafit Hadar52d81552015-10-07 12:26:52 +0300389 return root.put(CriterionCodec.OCH_SIGNAL_TYPE, ochSignalTypeCriterion.signalType().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700390 }
391 }
392
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700393 private static class FormatTunnelId implements CriterionTypeFormatter {
394 @Override
395 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
396 final TunnelIdCriterion tunnelIdCriterion =
397 (TunnelIdCriterion) criterion;
398 return root.put(CriterionCodec.TUNNEL_ID, tunnelIdCriterion.tunnelId());
399 }
400 }
401
Yafit Hadar52d81552015-10-07 12:26:52 +0300402 private static class FormatOduSignalId implements CriterionTypeFormatter {
403 @Override
404 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Yafit Hadar5796d972015-10-15 13:16:11 +0300405 OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId();
406 ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID);
407
408 child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
409 child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
410 child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap()));
411
412 return root;
Yafit Hadar52d81552015-10-07 12:26:52 +0300413 }
414 }
415
Yafit Hadar5796d972015-10-15 13:16:11 +0300416
Yafit Hadar52d81552015-10-07 12:26:52 +0300417 private static class FormatOduSignalType implements CriterionTypeFormatter {
418 @Override
419 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
420 final OduSignalTypeCriterion oduSignalTypeCriterion =
421 (OduSignalTypeCriterion) criterion;
422 return root.put(CriterionCodec.ODU_SIGNAL_TYPE, oduSignalTypeCriterion.signalType().name());
423 }
424 }
425
Ray Milkeyd43fe452015-05-29 09:35:12 -0700426 private class FormatDummyType implements CriterionTypeFormatter {
427
428 @Override
429 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
430 checkNotNull(criterion, "Criterion cannot be null");
431
432 return root.put(CriterionCodec.TYPE, criterion.type().toString());
433
434 }
435 }
436
437 /**
438 * Encodes a criterion into a JSON node.
439 *
440 * @return encoded JSON object for the given criterion
441 */
442 public ObjectNode encode() {
443 final ObjectNode result = context.mapper().createObjectNode()
444 .put(CriterionCodec.TYPE, criterion.type().toString());
445
446 CriterionTypeFormatter formatter =
447 checkNotNull(
448 formatMap.get(criterion.type()),
449 "No formatter found for criterion type "
450 + criterion.type().toString());
451
452 return formatter.encodeCriterion(result, criterion);
453 }
454
455}