Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 18 | import java.util.EnumMap; |
| 19 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.net.flow.criteria.Criteria; |
| 23 | import org.onosproject.net.flow.criteria.Criterion; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Criterion codec. |
| 33 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 34 | public final class CriterionCodec extends JsonCodec<Criterion> { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 35 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 36 | 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 Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 46 | formatMap.put(Criterion.Type.METADATA, new FormatMetadata()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 47 | formatMap.put(Criterion.Type.ETH_DST, new FormatEth()); |
| 48 | formatMap.put(Criterion.Type.ETH_SRC, new FormatEth()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 49 | formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 50 | 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 Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 54 | formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 55 | formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp()); |
| 56 | formatMap.put(Criterion.Type.IPV4_DST, new FormatIp()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 57 | 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 Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 65 | formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp()); |
| 66 | formatMap.put(Criterion.Type.IPV6_DST, new FormatIp()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 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()); |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 74 | formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 75 | formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId()); |
| 76 | formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 77 | |
| 78 | // Currently unimplemented |
| 79 | formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 80 | formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 81 | formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown()); |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 82 | formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown()); |
| 83 | formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 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()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 87 | formatMap.put(Criterion.Type.TUNNEL_ID, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 88 | formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown()); |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 89 | formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 90 | formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown()); |
| 91 | formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown()); |
| 92 | formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown()); |
| 93 | } |
| 94 | |
| 95 | private interface CriterionTypeFormatter { |
| 96 | ObjectNode formatCriterion(ObjectNode root, Criterion criterion); |
| 97 | } |
| 98 | |
| 99 | private static class FormatUnknown implements CriterionTypeFormatter { |
| 100 | @Override |
| 101 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 102 | return root; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private static class FormatInPort implements CriterionTypeFormatter { |
| 107 | @Override |
| 108 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 109 | final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion; |
| 110 | return root.put("port", portCriterion.port().toLong()); |
| 111 | } |
| 112 | } |
| 113 | |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 114 | private static class FormatMetadata implements CriterionTypeFormatter { |
| 115 | @Override |
| 116 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 117 | final Criteria.MetadataCriterion metadataCriterion = |
| 118 | (Criteria.MetadataCriterion) criterion; |
| 119 | return root.put("metadata", metadataCriterion.metadata()); |
| 120 | } |
| 121 | } |
| 122 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 123 | private static class FormatEth implements CriterionTypeFormatter { |
| 124 | @Override |
| 125 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 126 | final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion; |
| 127 | return root.put("mac", ethCriterion.mac().toString()); |
| 128 | } |
| 129 | } |
| 130 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 131 | private static class FormatEthType implements CriterionTypeFormatter { |
| 132 | @Override |
| 133 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 134 | final Criteria.EthTypeCriterion ethTypeCriterion = |
| 135 | (Criteria.EthTypeCriterion) criterion; |
| 136 | return root.put("ethType", ethTypeCriterion.ethType()); |
| 137 | } |
| 138 | } |
| 139 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 140 | private static class FormatVlanVid implements CriterionTypeFormatter { |
| 141 | @Override |
| 142 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 143 | final Criteria.VlanIdCriterion vlanIdCriterion = |
| 144 | (Criteria.VlanIdCriterion) criterion; |
| 145 | return root.put("vlanId", vlanIdCriterion.vlanId().toShort()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private static class FormatVlanPcp implements CriterionTypeFormatter { |
| 150 | @Override |
| 151 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 152 | final Criteria.VlanPcpCriterion vlanPcpCriterion = |
| 153 | (Criteria.VlanPcpCriterion) criterion; |
| 154 | return root.put("priority", vlanPcpCriterion.priority()); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | private static class FormatIpDscp implements CriterionTypeFormatter { |
| 159 | @Override |
| 160 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 161 | final Criteria.IPDscpCriterion ipDscpCriterion = |
| 162 | (Criteria.IPDscpCriterion) criterion; |
| 163 | return root.put("ipDscp", ipDscpCriterion.ipDscp()); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private static class FormatIpEcn implements CriterionTypeFormatter { |
| 168 | @Override |
| 169 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 170 | final Criteria.IPEcnCriterion ipEcnCriterion = |
| 171 | (Criteria.IPEcnCriterion) criterion; |
| 172 | return root.put("ipEcn", ipEcnCriterion.ipEcn()); |
| 173 | } |
| 174 | } |
| 175 | |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 176 | private static class FormatIpProto implements CriterionTypeFormatter { |
| 177 | @Override |
| 178 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 179 | final Criteria.IPProtocolCriterion iPProtocolCriterion = |
| 180 | (Criteria.IPProtocolCriterion) criterion; |
| 181 | return root.put("protocol", iPProtocolCriterion.protocol()); |
| 182 | } |
| 183 | } |
| 184 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 185 | private static class FormatIp implements CriterionTypeFormatter { |
| 186 | @Override |
| 187 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 188 | final Criteria.IPCriterion iPCriterion = (Criteria.IPCriterion) criterion; |
| 189 | return root.put("ip", iPCriterion.ip().toString()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private static class FormatTcp implements CriterionTypeFormatter { |
| 194 | @Override |
| 195 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 196 | final Criteria.TcpPortCriterion tcpPortCriterion = |
| 197 | (Criteria.TcpPortCriterion) criterion; |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 198 | return root.put("tcpPort", tcpPortCriterion.tcpPort()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | private static class FormatUdp implements CriterionTypeFormatter { |
| 203 | @Override |
| 204 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 205 | final Criteria.UdpPortCriterion udpPortCriterion = |
| 206 | (Criteria.UdpPortCriterion) criterion; |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 207 | return root.put("udpPort", udpPortCriterion.udpPort()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | private static class FormatSctp implements CriterionTypeFormatter { |
| 212 | @Override |
| 213 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 214 | final Criteria.SctpPortCriterion sctpPortCriterion = |
| 215 | (Criteria.SctpPortCriterion) criterion; |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 216 | return root.put("sctpPort", sctpPortCriterion.sctpPort()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | private static class FormatIcmpV4Type implements CriterionTypeFormatter { |
| 221 | @Override |
| 222 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 223 | final Criteria.IcmpTypeCriterion icmpTypeCriterion = |
| 224 | (Criteria.IcmpTypeCriterion) criterion; |
| 225 | return root.put("icmpType", icmpTypeCriterion.icmpType()); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private static class FormatIcmpV4Code implements CriterionTypeFormatter { |
| 230 | @Override |
| 231 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 232 | final Criteria.IcmpCodeCriterion icmpCodeCriterion = |
| 233 | (Criteria.IcmpCodeCriterion) criterion; |
| 234 | return root.put("icmpCode", icmpCodeCriterion.icmpCode()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private static class FormatIpV6FLabel implements CriterionTypeFormatter { |
| 239 | @Override |
| 240 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 241 | final Criteria.IPv6FlowLabelCriterion ipv6FlowLabelCriterion = |
| 242 | (Criteria.IPv6FlowLabelCriterion) criterion; |
| 243 | return root.put("flowLabel", ipv6FlowLabelCriterion.flowLabel()); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private static class FormatIcmpV6Type implements CriterionTypeFormatter { |
| 248 | @Override |
| 249 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 250 | final Criteria.Icmpv6TypeCriterion icmpv6TypeCriterion = |
| 251 | (Criteria.Icmpv6TypeCriterion) criterion; |
| 252 | return root.put("icmpv6Type", icmpv6TypeCriterion.icmpv6Type()); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | private static class FormatIcmpV6Code implements CriterionTypeFormatter { |
| 257 | @Override |
| 258 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 259 | final Criteria.Icmpv6CodeCriterion icmpv6CodeCriterion = |
| 260 | (Criteria.Icmpv6CodeCriterion) criterion; |
| 261 | return root.put("icmpv6Code", icmpv6CodeCriterion.icmpv6Code()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private static class FormatV6NDTarget implements CriterionTypeFormatter { |
| 266 | @Override |
| 267 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 268 | final Criteria.IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion |
| 269 | = (Criteria.IPv6NDTargetAddressCriterion) criterion; |
| 270 | return root.put("targetAddress", ipv6NDTargetAddressCriterion.targetAddress().toString()); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | private static class FormatV6NDTll implements CriterionTypeFormatter { |
| 275 | @Override |
| 276 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 277 | final Criteria.IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion |
| 278 | = (Criteria.IPv6NDLinkLayerAddressCriterion) criterion; |
| 279 | return root.put("mac", ipv6NDLinkLayerAddressCriterion.mac().toString()); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private static class FormatMplsLabel implements CriterionTypeFormatter { |
| 284 | @Override |
| 285 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 286 | final Criteria.MplsCriterion mplsCriterion = |
| 287 | (Criteria.MplsCriterion) criterion; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 288 | return root.put("label", mplsCriterion.label().toInt()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 292 | private static class FormatIpV6Exthdr implements CriterionTypeFormatter { |
| 293 | @Override |
| 294 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 295 | final Criteria.IPv6ExthdrFlagsCriterion exthdrCriterion = |
| 296 | (Criteria.IPv6ExthdrFlagsCriterion) criterion; |
| 297 | return root.put("exthdrFlags", exthdrCriterion.exthdrFlags()); |
| 298 | } |
| 299 | } |
| 300 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 301 | private static class FormatOchSigId implements CriterionTypeFormatter { |
| 302 | @Override |
| 303 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 304 | final Criteria.LambdaCriterion lambdaCriterion = |
| 305 | (Criteria.LambdaCriterion) criterion; |
| 306 | return root.put("lambda", lambdaCriterion.lambda()); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private static class FormatOchSigType implements CriterionTypeFormatter { |
| 311 | @Override |
| 312 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
| 313 | final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion = |
| 314 | (Criteria.OpticalSignalTypeCriterion) criterion; |
| 315 | return root.put("signalType", opticalSignalTypeCriterion.signalType()); |
| 316 | } |
| 317 | } |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 318 | |
| 319 | @Override |
| 320 | public ObjectNode encode(Criterion criterion, CodecContext context) { |
| 321 | checkNotNull(criterion, "Criterion cannot be null"); |
| 322 | |
| 323 | final ObjectNode result = context.mapper().createObjectNode() |
| 324 | .put("type", criterion.type().toString()); |
| 325 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 326 | CriterionTypeFormatter formatter = |
| 327 | checkNotNull( |
| 328 | formatMap.get(criterion.type()), |
| 329 | "No formatter found for criterion type " |
| 330 | + criterion.type().toString()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 331 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 332 | return formatter.formatCriterion(result, criterion); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 333 | } |
| 334 | } |