blob: 899ade5aaad00ff47b2c9c0e377a5c33e0db2f0d [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;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080022import org.onosproject.net.flow.criteria.Criterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070023import org.onosproject.net.flow.criteria.EthCriterion;
24import org.onosproject.net.flow.criteria.EthTypeCriterion;
25import org.onosproject.net.flow.criteria.IPCriterion;
26import org.onosproject.net.flow.criteria.IPDscpCriterion;
27import org.onosproject.net.flow.criteria.IPEcnCriterion;
28import org.onosproject.net.flow.criteria.IPProtocolCriterion;
29import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
30import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
31import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
32import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
33import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
34import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
35import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
36import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
37import org.onosproject.net.flow.criteria.LambdaCriterion;
38import org.onosproject.net.flow.criteria.MetadataCriterion;
39import org.onosproject.net.flow.criteria.MplsCriterion;
40import org.onosproject.net.flow.criteria.OpticalSignalTypeCriterion;
41import org.onosproject.net.flow.criteria.PortCriterion;
42import org.onosproject.net.flow.criteria.SctpPortCriterion;
43import org.onosproject.net.flow.criteria.TcpPortCriterion;
44import org.onosproject.net.flow.criteria.UdpPortCriterion;
45import org.onosproject.net.flow.criteria.VlanIdCriterion;
46import org.onosproject.net.flow.criteria.VlanPcpCriterion;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import com.fasterxml.jackson.databind.node.ObjectNode;
51
52import static com.google.common.base.Preconditions.checkNotNull;
53
54/**
55 * Criterion codec.
56 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080057public final class CriterionCodec extends JsonCodec<Criterion> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080058
Ray Milkey73ba84a2015-02-04 17:08:41 -080059 protected static final Logger log =
60 LoggerFactory.getLogger(CriterionCodec.class);
61
62 private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap;
63
64 public CriterionCodec() {
65 formatMap = new EnumMap<>(Criterion.Type.class);
66
67 formatMap.put(Criterion.Type.IN_PORT, new FormatInPort());
68 formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080069 formatMap.put(Criterion.Type.METADATA, new FormatMetadata());
Ray Milkey73ba84a2015-02-04 17:08:41 -080070 formatMap.put(Criterion.Type.ETH_DST, new FormatEth());
71 formatMap.put(Criterion.Type.ETH_SRC, new FormatEth());
Ray Milkey73ba84a2015-02-04 17:08:41 -080072 formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType());
Ray Milkey73ba84a2015-02-04 17:08:41 -080073 formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid());
74 formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp());
75 formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp());
76 formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080077 formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto());
Ray Milkey73ba84a2015-02-04 17:08:41 -080078 formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp());
79 formatMap.put(Criterion.Type.IPV4_DST, new FormatIp());
Ray Milkey73ba84a2015-02-04 17:08:41 -080080 formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp());
81 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
82 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
83 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
84 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
85 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
86 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
87 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080088 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
89 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
Ray Milkey73ba84a2015-02-04 17:08:41 -080090 formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel());
91 formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type());
92 formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code());
93 formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget());
94 formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll());
95 formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll());
96 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -080097 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr());
Ray Milkey73ba84a2015-02-04 17:08:41 -080098 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
99 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
alshabiba3a476d2015-04-10 14:35:38 -0700100 formatMap.put(Criterion.Type.DUMMY, new FormatDummyType());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800101
102 // Currently unimplemented
103 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800104 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800105 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800106 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
107 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800108 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
109 formatMap.put(Criterion.Type.MPLS_BOS, new FormatUnknown());
110 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800111 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800112 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800113 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800114 formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown());
115 formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown());
116 formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown());
117 }
118
119 private interface CriterionTypeFormatter {
120 ObjectNode formatCriterion(ObjectNode root, Criterion criterion);
121 }
122
123 private static class FormatUnknown implements CriterionTypeFormatter {
124 @Override
125 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
126 return root;
127 }
128 }
129
130 private static class FormatInPort implements CriterionTypeFormatter {
131 @Override
132 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700133 final PortCriterion portCriterion = (PortCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800134 return root.put("port", portCriterion.port().toLong());
135 }
136 }
137
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800138 private static class FormatMetadata implements CriterionTypeFormatter {
139 @Override
140 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700141 final MetadataCriterion metadataCriterion =
142 (MetadataCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800143 return root.put("metadata", metadataCriterion.metadata());
144 }
145 }
146
Ray Milkey73ba84a2015-02-04 17:08:41 -0800147 private static class FormatEth implements CriterionTypeFormatter {
148 @Override
149 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700150 final EthCriterion ethCriterion = (EthCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800151 return root.put("mac", ethCriterion.mac().toString());
152 }
153 }
154
Ray Milkey73ba84a2015-02-04 17:08:41 -0800155 private static class FormatEthType implements CriterionTypeFormatter {
156 @Override
157 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700158 final EthTypeCriterion ethTypeCriterion =
159 (EthTypeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800160 return root.put("ethType", ethTypeCriterion.ethType());
161 }
162 }
163
Ray Milkey73ba84a2015-02-04 17:08:41 -0800164 private static class FormatVlanVid implements CriterionTypeFormatter {
165 @Override
166 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700167 final VlanIdCriterion vlanIdCriterion =
168 (VlanIdCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800169 return root.put("vlanId", vlanIdCriterion.vlanId().toShort());
170 }
171 }
172
173 private static class FormatVlanPcp implements CriterionTypeFormatter {
174 @Override
175 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700176 final VlanPcpCriterion vlanPcpCriterion =
177 (VlanPcpCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800178 return root.put("priority", vlanPcpCriterion.priority());
179 }
180 }
181
182 private static class FormatIpDscp implements CriterionTypeFormatter {
183 @Override
184 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700185 final IPDscpCriterion ipDscpCriterion =
186 (IPDscpCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800187 return root.put("ipDscp", ipDscpCriterion.ipDscp());
188 }
189 }
190
191 private static class FormatIpEcn implements CriterionTypeFormatter {
192 @Override
193 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700194 final IPEcnCriterion ipEcnCriterion =
195 (IPEcnCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800196 return root.put("ipEcn", ipEcnCriterion.ipEcn());
197 }
198 }
199
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800200 private static class FormatIpProto implements CriterionTypeFormatter {
201 @Override
202 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700203 final IPProtocolCriterion iPProtocolCriterion =
204 (IPProtocolCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800205 return root.put("protocol", iPProtocolCriterion.protocol());
206 }
207 }
208
Ray Milkey73ba84a2015-02-04 17:08:41 -0800209 private static class FormatIp implements CriterionTypeFormatter {
210 @Override
211 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700212 final IPCriterion iPCriterion = (IPCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800213 return root.put("ip", iPCriterion.ip().toString());
214 }
215 }
216
217 private static class FormatTcp implements CriterionTypeFormatter {
218 @Override
219 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700220 final TcpPortCriterion tcpPortCriterion =
221 (TcpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800222 return root.put("tcpPort", tcpPortCriterion.tcpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800223 }
224 }
225
226 private static class FormatUdp implements CriterionTypeFormatter {
227 @Override
228 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700229 final UdpPortCriterion udpPortCriterion =
230 (UdpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800231 return root.put("udpPort", udpPortCriterion.udpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800232 }
233 }
234
235 private static class FormatSctp implements CriterionTypeFormatter {
236 @Override
237 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700238 final SctpPortCriterion sctpPortCriterion =
239 (SctpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800240 return root.put("sctpPort", sctpPortCriterion.sctpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800241 }
242 }
243
244 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
245 @Override
246 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700247 final IcmpTypeCriterion icmpTypeCriterion =
248 (IcmpTypeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800249 return root.put("icmpType", icmpTypeCriterion.icmpType());
250 }
251 }
252
253 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
254 @Override
255 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700256 final IcmpCodeCriterion icmpCodeCriterion =
257 (IcmpCodeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800258 return root.put("icmpCode", icmpCodeCriterion.icmpCode());
259 }
260 }
261
262 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
263 @Override
264 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700265 final IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
266 (IPv6FlowLabelCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800267 return root.put("flowLabel", ipv6FlowLabelCriterion.flowLabel());
268 }
269 }
270
271 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
272 @Override
273 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700274 final Icmpv6TypeCriterion icmpv6TypeCriterion =
275 (Icmpv6TypeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800276 return root.put("icmpv6Type", icmpv6TypeCriterion.icmpv6Type());
277 }
278 }
279
280 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
281 @Override
282 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700283 final Icmpv6CodeCriterion icmpv6CodeCriterion =
284 (Icmpv6CodeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800285 return root.put("icmpv6Code", icmpv6CodeCriterion.icmpv6Code());
286 }
287 }
288
289 private static class FormatV6NDTarget implements CriterionTypeFormatter {
290 @Override
291 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700292 final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
293 = (IPv6NDTargetAddressCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800294 return root.put("targetAddress", ipv6NDTargetAddressCriterion.targetAddress().toString());
295 }
296 }
297
298 private static class FormatV6NDTll implements CriterionTypeFormatter {
299 @Override
300 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700301 final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
302 = (IPv6NDLinkLayerAddressCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800303 return root.put("mac", ipv6NDLinkLayerAddressCriterion.mac().toString());
304 }
305 }
306
307 private static class FormatMplsLabel implements CriterionTypeFormatter {
308 @Override
309 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700310 final MplsCriterion mplsCriterion =
311 (MplsCriterion) criterion;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100312 return root.put("label", mplsCriterion.label().toInt());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800313 }
314 }
315
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800316 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
317 @Override
318 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700319 final IPv6ExthdrFlagsCriterion exthdrCriterion =
320 (IPv6ExthdrFlagsCriterion) criterion;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800321 return root.put("exthdrFlags", exthdrCriterion.exthdrFlags());
322 }
323 }
324
Ray Milkey73ba84a2015-02-04 17:08:41 -0800325 private static class FormatOchSigId implements CriterionTypeFormatter {
326 @Override
327 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700328 final LambdaCriterion lambdaCriterion =
329 (LambdaCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800330 return root.put("lambda", lambdaCriterion.lambda());
331 }
332 }
333
334 private static class FormatOchSigType implements CriterionTypeFormatter {
335 @Override
336 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700337 final OpticalSignalTypeCriterion opticalSignalTypeCriterion =
338 (OpticalSignalTypeCriterion) criterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -0800339 return root.put("signalType", opticalSignalTypeCriterion.signalType());
340 }
341 }
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800342
alshabiba3a476d2015-04-10 14:35:38 -0700343 private class FormatDummyType implements CriterionTypeFormatter {
344
345 @Override
346 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
347 checkNotNull(criterion, "Criterion cannot be null");
348
349 return root.put("type", criterion.type().toString());
350
351 }
352 }
353
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800354 @Override
355 public ObjectNode encode(Criterion criterion, CodecContext context) {
356 checkNotNull(criterion, "Criterion cannot be null");
357
358 final ObjectNode result = context.mapper().createObjectNode()
359 .put("type", criterion.type().toString());
360
Ray Milkey73ba84a2015-02-04 17:08:41 -0800361 CriterionTypeFormatter formatter =
362 checkNotNull(
363 formatMap.get(criterion.type()),
364 "No formatter found for criterion type "
365 + criterion.type().toString());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800366
Ray Milkey73ba84a2015-02-04 17:08:41 -0800367 return formatter.formatCriterion(result, criterion);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800368 }
alshabiba3a476d2015-04-10 14:35:38 -0700369
370
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800371}