blob: 1670361cc26954c2e34cac9461d74a5b66da9d19 [file] [log] [blame]
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
Ray Milkey73ba84a2015-02-04 17:08:41 -080018import java.util.EnumMap;
19
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080020import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070022import org.onosproject.net.OchSignal;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080023import org.onosproject.net.flow.criteria.Criterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070024import 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;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070038import org.onosproject.net.flow.criteria.MetadataCriterion;
39import org.onosproject.net.flow.criteria.MplsCriterion;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070040import org.onosproject.net.flow.criteria.OchSignalCriterion;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070041import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070042import org.onosproject.net.flow.criteria.PortCriterion;
43import org.onosproject.net.flow.criteria.SctpPortCriterion;
44import org.onosproject.net.flow.criteria.TcpPortCriterion;
45import org.onosproject.net.flow.criteria.UdpPortCriterion;
46import org.onosproject.net.flow.criteria.VlanIdCriterion;
47import org.onosproject.net.flow.criteria.VlanPcpCriterion;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
51import com.fasterxml.jackson.databind.node.ObjectNode;
52
53import static com.google.common.base.Preconditions.checkNotNull;
54
55/**
56 * Criterion codec.
57 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080058public final class CriterionCodec extends JsonCodec<Criterion> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080059
Ray Milkey73ba84a2015-02-04 17:08:41 -080060 protected static final Logger log =
61 LoggerFactory.getLogger(CriterionCodec.class);
62
63 private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap;
64
65 public CriterionCodec() {
66 formatMap = new EnumMap<>(Criterion.Type.class);
67
68 formatMap.put(Criterion.Type.IN_PORT, new FormatInPort());
69 formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080070 formatMap.put(Criterion.Type.METADATA, new FormatMetadata());
Ray Milkey73ba84a2015-02-04 17:08:41 -080071 formatMap.put(Criterion.Type.ETH_DST, new FormatEth());
72 formatMap.put(Criterion.Type.ETH_SRC, new FormatEth());
Ray Milkey73ba84a2015-02-04 17:08:41 -080073 formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType());
Ray Milkey73ba84a2015-02-04 17:08:41 -080074 formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid());
75 formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp());
76 formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp());
77 formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080078 formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto());
Ray Milkey73ba84a2015-02-04 17:08:41 -080079 formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp());
80 formatMap.put(Criterion.Type.IPV4_DST, new FormatIp());
Ray Milkey73ba84a2015-02-04 17:08:41 -080081 formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp());
82 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
83 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
84 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
85 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
86 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
87 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
88 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080089 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
90 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
Ray Milkey73ba84a2015-02-04 17:08:41 -080091 formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel());
92 formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type());
93 formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code());
94 formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget());
95 formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll());
96 formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll());
97 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -080098 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr());
Ray Milkey73ba84a2015-02-04 17:08:41 -080099 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
100 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
alshabiba3a476d2015-04-10 14:35:38 -0700101 formatMap.put(Criterion.Type.DUMMY, new FormatDummyType());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800102
103 // Currently unimplemented
104 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800105 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800106 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800107 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
108 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800109 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
110 formatMap.put(Criterion.Type.MPLS_BOS, new FormatUnknown());
111 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800112 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800113 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800114 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800115 formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown());
116 formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown());
117 formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown());
118 }
119
120 private interface CriterionTypeFormatter {
Ray Milkey3078fc02015-05-06 16:14:14 -0700121 ObjectNode encodeCriterion(ObjectNode root, Criterion criterion);
Ray Milkey73ba84a2015-02-04 17:08:41 -0800122 }
123
124 private static class FormatUnknown implements CriterionTypeFormatter {
125 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700126 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Ray Milkey73ba84a2015-02-04 17:08:41 -0800127 return root;
128 }
129 }
130
131 private static class FormatInPort implements CriterionTypeFormatter {
132 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700133 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700134 final PortCriterion portCriterion = (PortCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800135 return root.put("port", portCriterion.port().toLong());
136 }
137 }
138
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800139 private static class FormatMetadata implements CriterionTypeFormatter {
140 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700141 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700142 final MetadataCriterion metadataCriterion =
143 (MetadataCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800144 return root.put("metadata", metadataCriterion.metadata());
145 }
146 }
147
Ray Milkey73ba84a2015-02-04 17:08:41 -0800148 private static class FormatEth implements CriterionTypeFormatter {
149 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700150 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700151 final EthCriterion ethCriterion = (EthCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800152 return root.put("mac", ethCriterion.mac().toString());
153 }
154 }
155
Ray Milkey73ba84a2015-02-04 17:08:41 -0800156 private static class FormatEthType implements CriterionTypeFormatter {
157 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700158 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700159 final EthTypeCriterion ethTypeCriterion =
160 (EthTypeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800161 return root.put("ethType", ethTypeCriterion.ethType());
162 }
163 }
164
Ray Milkey73ba84a2015-02-04 17:08:41 -0800165 private static class FormatVlanVid implements CriterionTypeFormatter {
166 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700167 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700168 final VlanIdCriterion vlanIdCriterion =
169 (VlanIdCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800170 return root.put("vlanId", vlanIdCriterion.vlanId().toShort());
171 }
172 }
173
174 private static class FormatVlanPcp implements CriterionTypeFormatter {
175 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700176 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700177 final VlanPcpCriterion vlanPcpCriterion =
178 (VlanPcpCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800179 return root.put("priority", vlanPcpCriterion.priority());
180 }
181 }
182
183 private static class FormatIpDscp implements CriterionTypeFormatter {
184 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700185 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700186 final IPDscpCriterion ipDscpCriterion =
187 (IPDscpCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800188 return root.put("ipDscp", ipDscpCriterion.ipDscp());
189 }
190 }
191
192 private static class FormatIpEcn implements CriterionTypeFormatter {
193 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700194 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700195 final IPEcnCriterion ipEcnCriterion =
196 (IPEcnCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800197 return root.put("ipEcn", ipEcnCriterion.ipEcn());
198 }
199 }
200
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800201 private static class FormatIpProto implements CriterionTypeFormatter {
202 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700203 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700204 final IPProtocolCriterion iPProtocolCriterion =
205 (IPProtocolCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800206 return root.put("protocol", iPProtocolCriterion.protocol());
207 }
208 }
209
Ray Milkey73ba84a2015-02-04 17:08:41 -0800210 private static class FormatIp implements CriterionTypeFormatter {
211 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700212 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700213 final IPCriterion iPCriterion = (IPCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800214 return root.put("ip", iPCriterion.ip().toString());
215 }
216 }
217
218 private static class FormatTcp implements CriterionTypeFormatter {
219 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700220 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700221 final TcpPortCriterion tcpPortCriterion =
222 (TcpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800223 return root.put("tcpPort", tcpPortCriterion.tcpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800224 }
225 }
226
227 private static class FormatUdp implements CriterionTypeFormatter {
228 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700229 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700230 final UdpPortCriterion udpPortCriterion =
231 (UdpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800232 return root.put("udpPort", udpPortCriterion.udpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800233 }
234 }
235
236 private static class FormatSctp implements CriterionTypeFormatter {
237 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700238 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700239 final SctpPortCriterion sctpPortCriterion =
240 (SctpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800241 return root.put("sctpPort", sctpPortCriterion.sctpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800242 }
243 }
244
245 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
246 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700247 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700248 final IcmpTypeCriterion icmpTypeCriterion =
249 (IcmpTypeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800250 return root.put("icmpType", icmpTypeCriterion.icmpType());
251 }
252 }
253
254 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
255 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700256 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700257 final IcmpCodeCriterion icmpCodeCriterion =
258 (IcmpCodeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800259 return root.put("icmpCode", icmpCodeCriterion.icmpCode());
260 }
261 }
262
263 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
264 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700265 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700266 final IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
267 (IPv6FlowLabelCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800268 return root.put("flowLabel", ipv6FlowLabelCriterion.flowLabel());
269 }
270 }
271
272 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
273 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700274 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700275 final Icmpv6TypeCriterion icmpv6TypeCriterion =
276 (Icmpv6TypeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800277 return root.put("icmpv6Type", icmpv6TypeCriterion.icmpv6Type());
278 }
279 }
280
281 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
282 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700283 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700284 final Icmpv6CodeCriterion icmpv6CodeCriterion =
285 (Icmpv6CodeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800286 return root.put("icmpv6Code", icmpv6CodeCriterion.icmpv6Code());
287 }
288 }
289
290 private static class FormatV6NDTarget implements CriterionTypeFormatter {
291 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700292 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700293 final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
294 = (IPv6NDTargetAddressCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800295 return root.put("targetAddress", ipv6NDTargetAddressCriterion.targetAddress().toString());
296 }
297 }
298
299 private static class FormatV6NDTll implements CriterionTypeFormatter {
300 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700301 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700302 final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
303 = (IPv6NDLinkLayerAddressCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800304 return root.put("mac", ipv6NDLinkLayerAddressCriterion.mac().toString());
305 }
306 }
307
308 private static class FormatMplsLabel implements CriterionTypeFormatter {
309 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700310 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700311 final MplsCriterion mplsCriterion =
312 (MplsCriterion) criterion;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100313 return root.put("label", mplsCriterion.label().toInt());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800314 }
315 }
316
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800317 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
318 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700319 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700320 final IPv6ExthdrFlagsCriterion exthdrCriterion =
321 (IPv6ExthdrFlagsCriterion) criterion;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800322 return root.put("exthdrFlags", exthdrCriterion.exthdrFlags());
323 }
324 }
325
Ray Milkey73ba84a2015-02-04 17:08:41 -0800326 private static class FormatOchSigId implements CriterionTypeFormatter {
327 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700328 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700329 OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda();
330 ObjectNode child = root.putObject("ochSignalId");
331
332 child.put("gridType", ochSignal.gridType().name());
333 child.put("channelSpacing", ochSignal.channelSpacing().name());
334 child.put("spacingMultiplier", ochSignal.spacingMultiplier());
335 child.put("slotGranularity", ochSignal.slotGranularity());
336
337 return root;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800338 }
339 }
340
341 private static class FormatOchSigType implements CriterionTypeFormatter {
342 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700343 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700344 final OchSignalTypeCriterion ochSignalTypeCriterion =
345 (OchSignalTypeCriterion) criterion;
346 return root.put("ochSignalType", ochSignalTypeCriterion.signalType().name());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800347 }
348 }
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800349
alshabiba3a476d2015-04-10 14:35:38 -0700350 private class FormatDummyType implements CriterionTypeFormatter {
351
352 @Override
Ray Milkey3078fc02015-05-06 16:14:14 -0700353 public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
alshabiba3a476d2015-04-10 14:35:38 -0700354 checkNotNull(criterion, "Criterion cannot be null");
355
356 return root.put("type", criterion.type().toString());
357
358 }
359 }
360
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800361 @Override
362 public ObjectNode encode(Criterion criterion, CodecContext context) {
363 checkNotNull(criterion, "Criterion cannot be null");
364
365 final ObjectNode result = context.mapper().createObjectNode()
366 .put("type", criterion.type().toString());
367
Ray Milkey73ba84a2015-02-04 17:08:41 -0800368 CriterionTypeFormatter formatter =
369 checkNotNull(
370 formatMap.get(criterion.type()),
371 "No formatter found for criterion type "
372 + criterion.type().toString());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800373
Ray Milkey3078fc02015-05-06 16:14:14 -0700374 return formatter.encodeCriterion(result, criterion);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800375 }
alshabiba3a476d2015-04-10 14:35:38 -0700376
377
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800378}