blob: 1dddc88ad5a2981b48aba14cd0e4ea80c4272765 [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());
46 formatMap.put(Criterion.Type.ETH_DST, new FormatEth());
47 formatMap.put(Criterion.Type.ETH_SRC, new FormatEth());
48 formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto());
49 formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType());
50 formatMap.put(Criterion.Type.METADATA, new FormatMetadata());
51 formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid());
52 formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp());
53 formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp());
54 formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn());
55 formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp());
56 formatMap.put(Criterion.Type.IPV4_DST, new FormatIp());
57 formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp());
58 formatMap.put(Criterion.Type.IPV6_DST, new FormatIp());
59 formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp());
60 formatMap.put(Criterion.Type.TCP_DST, new FormatTcp());
61 formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp());
62 formatMap.put(Criterion.Type.UDP_DST, new FormatUdp());
63 formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp());
64 formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp());
65 formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type());
66 formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code());
67 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());
74 formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId());
75 formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType());
76 formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel());
77
78 // Currently unimplemented
79 formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown());
80 formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown());
81 formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown());
82 formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown());
83 formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown());
84 formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown());
85 formatMap.put(Criterion.Type.MPLS_BOS, new FormatUnknown());
86 formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown());
87 formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown());
88 formatMap.put(Criterion.Type.TUNNEL_ID, new FormatUnknown());
89 formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatUnknown());
90 formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown());
91 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
115 private static class FormatEth implements CriterionTypeFormatter {
116 @Override
117 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
118 final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion;
119 return root.put("mac", ethCriterion.mac().toString());
120 }
121 }
122
123 private static class FormatIpProto implements CriterionTypeFormatter {
124 @Override
125 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
126 final Criteria.IPProtocolCriterion iPProtocolCriterion =
127 (Criteria.IPProtocolCriterion) criterion;
128 return root.put("protocol", iPProtocolCriterion.protocol());
129 }
130 }
131
132 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
141 private static class FormatMetadata implements CriterionTypeFormatter {
142 @Override
143 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
144 final Criteria.MetadataCriterion metadataCriterion =
145 (Criteria.MetadataCriterion) criterion;
146 return root.put("metadata", metadataCriterion.metadata());
147 }
148 }
149
150 private static class FormatVlanVid implements CriterionTypeFormatter {
151 @Override
152 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
153 final Criteria.VlanIdCriterion vlanIdCriterion =
154 (Criteria.VlanIdCriterion) criterion;
155 return root.put("vlanId", vlanIdCriterion.vlanId().toShort());
156 }
157 }
158
159 private static class FormatVlanPcp implements CriterionTypeFormatter {
160 @Override
161 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
162 final Criteria.VlanPcpCriterion vlanPcpCriterion =
163 (Criteria.VlanPcpCriterion) criterion;
164 return root.put("priority", vlanPcpCriterion.priority());
165 }
166 }
167
168 private static class FormatIpDscp implements CriterionTypeFormatter {
169 @Override
170 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
171 final Criteria.IPDscpCriterion ipDscpCriterion =
172 (Criteria.IPDscpCriterion) criterion;
173 return root.put("ipDscp", ipDscpCriterion.ipDscp());
174 }
175 }
176
177 private static class FormatIpEcn implements CriterionTypeFormatter {
178 @Override
179 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
180 final Criteria.IPEcnCriterion ipEcnCriterion =
181 (Criteria.IPEcnCriterion) criterion;
182 return root.put("ipEcn", ipEcnCriterion.ipEcn());
183 }
184 }
185
186 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;
199 return root.put("tcpPort", tcpPortCriterion.tcpPort().byteValue());
200 }
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;
208 return root.put("udpPort", udpPortCriterion.udpPort().byteValue());
209 }
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;
217 return root.put("sctpPort",
218 sctpPortCriterion.sctpPort().byteValue());
219 }
220 }
221
222 private static class FormatIcmpV4Type implements CriterionTypeFormatter {
223 @Override
224 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
225 final Criteria.IcmpTypeCriterion icmpTypeCriterion =
226 (Criteria.IcmpTypeCriterion) criterion;
227 return root.put("icmpType", icmpTypeCriterion.icmpType());
228 }
229 }
230
231 private static class FormatIcmpV4Code implements CriterionTypeFormatter {
232 @Override
233 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
234 final Criteria.IcmpCodeCriterion icmpCodeCriterion =
235 (Criteria.IcmpCodeCriterion) criterion;
236 return root.put("icmpCode", icmpCodeCriterion.icmpCode());
237 }
238 }
239
240 private static class FormatIpV6FLabel implements CriterionTypeFormatter {
241 @Override
242 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
243 final Criteria.IPv6FlowLabelCriterion ipv6FlowLabelCriterion =
244 (Criteria.IPv6FlowLabelCriterion) criterion;
245 return root.put("flowLabel", ipv6FlowLabelCriterion.flowLabel());
246 }
247 }
248
249 private static class FormatIcmpV6Type implements CriterionTypeFormatter {
250 @Override
251 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
252 final Criteria.Icmpv6TypeCriterion icmpv6TypeCriterion =
253 (Criteria.Icmpv6TypeCriterion) criterion;
254 return root.put("icmpv6Type", icmpv6TypeCriterion.icmpv6Type());
255 }
256 }
257
258 private static class FormatIcmpV6Code implements CriterionTypeFormatter {
259 @Override
260 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
261 final Criteria.Icmpv6CodeCriterion icmpv6CodeCriterion =
262 (Criteria.Icmpv6CodeCriterion) criterion;
263 return root.put("icmpv6Code", icmpv6CodeCriterion.icmpv6Code());
264 }
265 }
266
267 private static class FormatV6NDTarget implements CriterionTypeFormatter {
268 @Override
269 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
270 final Criteria.IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion
271 = (Criteria.IPv6NDTargetAddressCriterion) criterion;
272 return root.put("targetAddress", ipv6NDTargetAddressCriterion.targetAddress().toString());
273 }
274 }
275
276 private static class FormatV6NDTll implements CriterionTypeFormatter {
277 @Override
278 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
279 final Criteria.IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion
280 = (Criteria.IPv6NDLinkLayerAddressCriterion) criterion;
281 return root.put("mac", ipv6NDLinkLayerAddressCriterion.mac().toString());
282 }
283 }
284
285 private static class FormatMplsLabel implements CriterionTypeFormatter {
286 @Override
287 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
288 final Criteria.MplsCriterion mplsCriterion =
289 (Criteria.MplsCriterion) criterion;
290 return root.put("label", mplsCriterion.label());
291 }
292 }
293
294 private static class FormatOchSigId implements CriterionTypeFormatter {
295 @Override
296 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
297 final Criteria.LambdaCriterion lambdaCriterion =
298 (Criteria.LambdaCriterion) criterion;
299 return root.put("lambda", lambdaCriterion.lambda());
300 }
301 }
302
303 private static class FormatOchSigType implements CriterionTypeFormatter {
304 @Override
305 public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) {
306 final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
307 (Criteria.OpticalSignalTypeCriterion) criterion;
308 return root.put("signalType", opticalSignalTypeCriterion.signalType());
309 }
310 }
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800311
312 @Override
313 public ObjectNode encode(Criterion criterion, CodecContext context) {
314 checkNotNull(criterion, "Criterion cannot be null");
315
316 final ObjectNode result = context.mapper().createObjectNode()
317 .put("type", criterion.type().toString());
318
Ray Milkey73ba84a2015-02-04 17:08:41 -0800319 CriterionTypeFormatter formatter =
320 checkNotNull(
321 formatMap.get(criterion.type()),
322 "No formatter found for criterion type "
323 + criterion.type().toString());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800324
Ray Milkey73ba84a2015-02-04 17:08:41 -0800325 return formatter.formatCriterion(result, criterion);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800326 }
327}