blob: 57cff6682aff12ee26faf2a9c03332ea709738c6 [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;
22import org.onosproject.net.flow.criteria.Criteria;
23import org.onosproject.net.flow.criteria.Criterion;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import com.fasterxml.jackson.databind.node.ObjectNode;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Criterion codec.
33 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080034public final class CriterionCodec extends JsonCodec<Criterion> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080035
Ray Milkey73ba84a2015-02-04 17:08:41 -080036 protected static final Logger log =
37 LoggerFactory.getLogger(CriterionCodec.class);
38
39 private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap;
40
41 public CriterionCodec() {
42 formatMap = new EnumMap<>(Criterion.Type.class);
43
44 formatMap.put(Criterion.Type.IN_PORT, new FormatInPort());
45 formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080046 formatMap.put(Criterion.Type.METADATA, new FormatMetadata());
Ray Milkey73ba84a2015-02-04 17:08:41 -080047 formatMap.put(Criterion.Type.ETH_DST, new FormatEth());
48 formatMap.put(Criterion.Type.ETH_SRC, new FormatEth());
Ray Milkey73ba84a2015-02-04 17:08:41 -080049 formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType());
Ray Milkey73ba84a2015-02-04 17:08:41 -080050 formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid());
51 formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp());
52 formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp());
53 formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080054 formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto());
Ray Milkey73ba84a2015-02-04 17:08:41 -080055 formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp());
56 formatMap.put(Criterion.Type.IPV4_DST, new FormatIp());
Ray Milkey73ba84a2015-02-04 17:08:41 -080057 formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp());
58 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
59 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
60 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
61 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
62 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
63 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
64 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080065 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
66 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
Ray Milkey73ba84a2015-02-04 17:08:41 -080067 formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel());
68 formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type());
69 formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code());
70 formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget());
71 formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll());
72 formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll());
73 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -080074 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr());
Ray Milkey73ba84a2015-02-04 17:08:41 -080075 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
76 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
alshabiba3a476d2015-04-10 14:35:38 -070077 formatMap.put(Criterion.Type.DUMMY, new FormatDummyType());
Ray Milkey73ba84a2015-02-04 17:08:41 -080078
79 // Currently unimplemented
80 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -080081 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -080082 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080083 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
84 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -080085 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
86 formatMap.put(Criterion.Type.MPLS_BOS, new FormatUnknown());
87 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -080088 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -080089 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -080090 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
Ray Milkey73ba84a2015-02-04 17:08:41 -080091 formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown());
92 formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown());
93 formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown());
94 }
95
96 private interface CriterionTypeFormatter {
97 ObjectNode formatCriterion(ObjectNode root, Criterion criterion);
98 }
99
100 private static class FormatUnknown implements CriterionTypeFormatter {
101 @Override
102 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
103 return root;
104 }
105 }
106
107 private static class FormatInPort implements CriterionTypeFormatter {
108 @Override
109 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
110 final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion;
111 return root.put("port", portCriterion.port().toLong());
112 }
113 }
114
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800115 private static class FormatMetadata implements CriterionTypeFormatter {
116 @Override
117 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
118 final Criteria.MetadataCriterion metadataCriterion =
119 (Criteria.MetadataCriterion) criterion;
120 return root.put("metadata", metadataCriterion.metadata());
121 }
122 }
123
Ray Milkey73ba84a2015-02-04 17:08:41 -0800124 private static class FormatEth implements CriterionTypeFormatter {
125 @Override
126 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
127 final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion;
128 return root.put("mac", ethCriterion.mac().toString());
129 }
130 }
131
Ray Milkey73ba84a2015-02-04 17:08:41 -0800132 private static class FormatEthType implements CriterionTypeFormatter {
133 @Override
134 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
135 final Criteria.EthTypeCriterion ethTypeCriterion =
136 (Criteria.EthTypeCriterion) criterion;
137 return root.put("ethType", ethTypeCriterion.ethType());
138 }
139 }
140
Ray Milkey73ba84a2015-02-04 17:08:41 -0800141 private static class FormatVlanVid implements CriterionTypeFormatter {
142 @Override
143 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
144 final Criteria.VlanIdCriterion vlanIdCriterion =
145 (Criteria.VlanIdCriterion) criterion;
146 return root.put("vlanId", vlanIdCriterion.vlanId().toShort());
147 }
148 }
149
150 private static class FormatVlanPcp implements CriterionTypeFormatter {
151 @Override
152 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
153 final Criteria.VlanPcpCriterion vlanPcpCriterion =
154 (Criteria.VlanPcpCriterion) criterion;
155 return root.put("priority", vlanPcpCriterion.priority());
156 }
157 }
158
159 private static class FormatIpDscp implements CriterionTypeFormatter {
160 @Override
161 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
162 final Criteria.IPDscpCriterion ipDscpCriterion =
163 (Criteria.IPDscpCriterion) criterion;
164 return root.put("ipDscp", ipDscpCriterion.ipDscp());
165 }
166 }
167
168 private static class FormatIpEcn implements CriterionTypeFormatter {
169 @Override
170 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
171 final Criteria.IPEcnCriterion ipEcnCriterion =
172 (Criteria.IPEcnCriterion) criterion;
173 return root.put("ipEcn", ipEcnCriterion.ipEcn());
174 }
175 }
176
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800177 private static class FormatIpProto implements CriterionTypeFormatter {
178 @Override
179 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
180 final Criteria.IPProtocolCriterion iPProtocolCriterion =
181 (Criteria.IPProtocolCriterion) criterion;
182 return root.put("protocol", iPProtocolCriterion.protocol());
183 }
184 }
185
Ray Milkey73ba84a2015-02-04 17:08:41 -0800186 private static class FormatIp implements CriterionTypeFormatter {
187 @Override
188 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
189 final Criteria.IPCriterion iPCriterion = (Criteria.IPCriterion) criterion;
190 return root.put("ip", iPCriterion.ip().toString());
191 }
192 }
193
194 private static class FormatTcp implements CriterionTypeFormatter {
195 @Override
196 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
197 final Criteria.TcpPortCriterion tcpPortCriterion =
198 (Criteria.TcpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800199 return root.put("tcpPort", tcpPortCriterion.tcpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800200 }
201 }
202
203 private static class FormatUdp implements CriterionTypeFormatter {
204 @Override
205 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
206 final Criteria.UdpPortCriterion udpPortCriterion =
207 (Criteria.UdpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800208 return root.put("udpPort", udpPortCriterion.udpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800209 }
210 }
211
212 private static class FormatSctp implements CriterionTypeFormatter {
213 @Override
214 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
215 final Criteria.SctpPortCriterion sctpPortCriterion =
216 (Criteria.SctpPortCriterion) criterion;
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800217 return root.put("sctpPort", sctpPortCriterion.sctpPort());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800218 }
219 }
220
221 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
222 @Override
223 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
224 final Criteria.IcmpTypeCriterion icmpTypeCriterion =
225 (Criteria.IcmpTypeCriterion) criterion;
226 return root.put("icmpType", icmpTypeCriterion.icmpType());
227 }
228 }
229
230 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
231 @Override
232 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
233 final Criteria.IcmpCodeCriterion icmpCodeCriterion =
234 (Criteria.IcmpCodeCriterion) criterion;
235 return root.put("icmpCode", icmpCodeCriterion.icmpCode());
236 }
237 }
238
239 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
240 @Override
241 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
242 final Criteria.IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
243 (Criteria.IPv6FlowLabelCriterion) criterion;
244 return root.put("flowLabel", ipv6FlowLabelCriterion.flowLabel());
245 }
246 }
247
248 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
249 @Override
250 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
251 final Criteria.Icmpv6TypeCriterion icmpv6TypeCriterion =
252 (Criteria.Icmpv6TypeCriterion) criterion;
253 return root.put("icmpv6Type", icmpv6TypeCriterion.icmpv6Type());
254 }
255 }
256
257 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
258 @Override
259 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
260 final Criteria.Icmpv6CodeCriterion icmpv6CodeCriterion =
261 (Criteria.Icmpv6CodeCriterion) criterion;
262 return root.put("icmpv6Code", icmpv6CodeCriterion.icmpv6Code());
263 }
264 }
265
266 private static class FormatV6NDTarget implements CriterionTypeFormatter {
267 @Override
268 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
269 final Criteria.IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
270 = (Criteria.IPv6NDTargetAddressCriterion) criterion;
271 return root.put("targetAddress", ipv6NDTargetAddressCriterion.targetAddress().toString());
272 }
273 }
274
275 private static class FormatV6NDTll implements CriterionTypeFormatter {
276 @Override
277 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
278 final Criteria.IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
279 = (Criteria.IPv6NDLinkLayerAddressCriterion) criterion;
280 return root.put("mac", ipv6NDLinkLayerAddressCriterion.mac().toString());
281 }
282 }
283
284 private static class FormatMplsLabel implements CriterionTypeFormatter {
285 @Override
286 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
287 final Criteria.MplsCriterion mplsCriterion =
288 (Criteria.MplsCriterion) criterion;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100289 return root.put("label", mplsCriterion.label().toInt());
Ray Milkey73ba84a2015-02-04 17:08:41 -0800290 }
291 }
292
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800293 private static class FormatIpV6Exthdr implements CriterionTypeFormatter {
294 @Override
295 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
296 final Criteria.IPv6ExthdrFlagsCriterion exthdrCriterion =
297 (Criteria.IPv6ExthdrFlagsCriterion) criterion;
298 return root.put("exthdrFlags", exthdrCriterion.exthdrFlags());
299 }
300 }
301
Ray Milkey73ba84a2015-02-04 17:08:41 -0800302 private static class FormatOchSigId implements CriterionTypeFormatter {
303 @Override
304 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
305 final Criteria.LambdaCriterion lambdaCriterion =
306 (Criteria.LambdaCriterion) criterion;
307 return root.put("lambda", lambdaCriterion.lambda());
308 }
309 }
310
311 private static class FormatOchSigType implements CriterionTypeFormatter {
312 @Override
313 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
314 final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
315 (Criteria.OpticalSignalTypeCriterion) criterion;
316 return root.put("signalType", opticalSignalTypeCriterion.signalType());
317 }
318 }
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800319
alshabiba3a476d2015-04-10 14:35:38 -0700320 private class FormatDummyType implements CriterionTypeFormatter {
321
322 @Override
323 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
324 checkNotNull(criterion, "Criterion cannot be null");
325
326 return root.put("type", criterion.type().toString());
327
328 }
329 }
330
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800331 @Override
332 public ObjectNode encode(Criterion criterion, CodecContext context) {
333 checkNotNull(criterion, "Criterion cannot be null");
334
335 final ObjectNode result = context.mapper().createObjectNode()
336 .put("type", criterion.type().toString());
337
Ray Milkey73ba84a2015-02-04 17:08:41 -0800338 CriterionTypeFormatter formatter =
339 checkNotNull(
340 formatMap.get(criterion.type()),
341 "No formatter found for criterion type "
342 + criterion.type().toString());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800343
Ray Milkey73ba84a2015-02-04 17:08:41 -0800344 return formatter.formatCriterion(result, criterion);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800345 }
alshabiba3a476d2015-04-10 14:35:38 -0700346
347
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800348}