Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [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 | |
| 18 | import java.util.EnumMap; |
| 19 | |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.net.OchSignal; |
| 22 | import org.onosproject.net.flow.criteria.Criterion; |
| 23 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 24 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 25 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 26 | import org.onosproject.net.flow.criteria.IPDscpCriterion; |
| 27 | import org.onosproject.net.flow.criteria.IPEcnCriterion; |
| 28 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 29 | import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion; |
| 30 | import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion; |
| 31 | import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion; |
| 32 | import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion; |
| 33 | import org.onosproject.net.flow.criteria.IcmpCodeCriterion; |
| 34 | import org.onosproject.net.flow.criteria.IcmpTypeCriterion; |
| 35 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 36 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
| 37 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
| 38 | import org.onosproject.net.flow.criteria.MplsCriterion; |
| 39 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
| 40 | import org.onosproject.net.flow.criteria.OchSignalTypeCriterion; |
| 41 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 42 | import org.onosproject.net.flow.criteria.SctpPortCriterion; |
| 43 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
| 44 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 45 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 46 | import org.onosproject.net.flow.criteria.VlanPcpCriterion; |
| 47 | |
| 48 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 49 | |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
| 51 | |
| 52 | /** |
| 53 | * Encode portion of the criterion codec. |
| 54 | */ |
| 55 | public final class EncodeCriterionCodec { |
| 56 | |
| 57 | private final Criterion criterion; |
| 58 | private final CodecContext context; |
| 59 | |
| 60 | private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap; |
| 61 | |
| 62 | /** |
| 63 | * Creates an encoder object for a criterion. |
| 64 | * Initializes the formatter lookup map for the criterion subclasses. |
| 65 | * |
| 66 | * @param criterion Criterion to encode |
| 67 | * @param context context of the JSON encoding |
| 68 | */ |
| 69 | public EncodeCriterionCodec(Criterion criterion, CodecContext context) { |
| 70 | this.criterion = criterion; |
| 71 | this.context = context; |
| 72 | |
| 73 | formatMap = new EnumMap<>(Criterion.Type.class); |
| 74 | |
| 75 | formatMap.put(Criterion.Type.IN_PORT, new FormatInPort()); |
| 76 | formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort()); |
| 77 | formatMap.put(Criterion.Type.METADATA, new FormatMetadata()); |
| 78 | formatMap.put(Criterion.Type.ETH_DST, new FormatEth()); |
| 79 | formatMap.put(Criterion.Type.ETH_SRC, new FormatEth()); |
| 80 | formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType()); |
| 81 | formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid()); |
| 82 | formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp()); |
| 83 | formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp()); |
| 84 | formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn()); |
| 85 | formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto()); |
| 86 | formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp()); |
| 87 | formatMap.put(Criterion.Type.IPV4_DST, new FormatIp()); |
| 88 | formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp()); |
| 89 | formatMap.put(Criterion.Type.TCP_DST, new FormatTcp()); |
| 90 | formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp()); |
| 91 | formatMap.put(Criterion.Type.UDP_DST, new FormatUdp()); |
| 92 | formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp()); |
| 93 | formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp()); |
| 94 | formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type()); |
| 95 | formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code()); |
| 96 | formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp()); |
| 97 | formatMap.put(Criterion.Type.IPV6_DST, new FormatIp()); |
| 98 | formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel()); |
| 99 | formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type()); |
| 100 | formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code()); |
| 101 | formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget()); |
| 102 | formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll()); |
| 103 | formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll()); |
| 104 | formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel()); |
| 105 | formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr()); |
| 106 | formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId()); |
| 107 | formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType()); |
| 108 | formatMap.put(Criterion.Type.DUMMY, new FormatDummyType()); |
| 109 | |
| 110 | // Currently unimplemented |
| 111 | formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown()); |
| 112 | formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown()); |
| 113 | formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown()); |
| 114 | formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown()); |
| 115 | formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown()); |
| 116 | formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown()); |
| 117 | formatMap.put(Criterion.Type.MPLS_BOS, new FormatUnknown()); |
| 118 | formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown()); |
| 119 | formatMap.put(Criterion.Type.TUNNEL_ID, new FormatUnknown()); |
| 120 | formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown()); |
| 121 | formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown()); |
| 122 | formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown()); |
| 123 | formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown()); |
| 124 | formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown()); |
| 125 | } |
| 126 | |
| 127 | private interface CriterionTypeFormatter { |
| 128 | ObjectNode encodeCriterion(ObjectNode root, Criterion criterion); |
| 129 | } |
| 130 | |
| 131 | private static class FormatUnknown implements CriterionTypeFormatter { |
| 132 | @Override |
| 133 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 134 | return root; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private static class FormatInPort implements CriterionTypeFormatter { |
| 139 | @Override |
| 140 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 141 | final PortCriterion portCriterion = (PortCriterion) criterion; |
| 142 | return root.put(CriterionCodec.PORT, portCriterion.port().toLong()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private static class FormatMetadata implements CriterionTypeFormatter { |
| 147 | @Override |
| 148 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 149 | final MetadataCriterion metadataCriterion = |
| 150 | (MetadataCriterion) criterion; |
| 151 | return root.put(CriterionCodec.METADATA, metadataCriterion.metadata()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private static class FormatEth implements CriterionTypeFormatter { |
| 156 | @Override |
| 157 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 158 | final EthCriterion ethCriterion = (EthCriterion) criterion; |
| 159 | return root.put(CriterionCodec.MAC, ethCriterion.mac().toString()); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private static class FormatEthType implements CriterionTypeFormatter { |
| 164 | @Override |
| 165 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 166 | final EthTypeCriterion ethTypeCriterion = |
| 167 | (EthTypeCriterion) criterion; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 168 | return root.put(CriterionCodec.ETH_TYPE, ethTypeCriterion.ethType().toShort()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | private static class FormatVlanVid implements CriterionTypeFormatter { |
| 173 | @Override |
| 174 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 175 | final VlanIdCriterion vlanIdCriterion = |
| 176 | (VlanIdCriterion) criterion; |
| 177 | return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort()); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private static class FormatVlanPcp implements CriterionTypeFormatter { |
| 182 | @Override |
| 183 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 184 | final VlanPcpCriterion vlanPcpCriterion = |
| 185 | (VlanPcpCriterion) criterion; |
| 186 | return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority()); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private static class FormatIpDscp implements CriterionTypeFormatter { |
| 191 | @Override |
| 192 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 193 | final IPDscpCriterion ipDscpCriterion = |
| 194 | (IPDscpCriterion) criterion; |
| 195 | return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private static class FormatIpEcn implements CriterionTypeFormatter { |
| 200 | @Override |
| 201 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 202 | final IPEcnCriterion ipEcnCriterion = |
| 203 | (IPEcnCriterion) criterion; |
| 204 | return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn()); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | private static class FormatIpProto implements CriterionTypeFormatter { |
| 209 | @Override |
| 210 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 211 | final IPProtocolCriterion iPProtocolCriterion = |
| 212 | (IPProtocolCriterion) criterion; |
| 213 | return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol()); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private static class FormatIp implements CriterionTypeFormatter { |
| 218 | @Override |
| 219 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 220 | final IPCriterion iPCriterion = (IPCriterion) criterion; |
| 221 | return root.put(CriterionCodec.IP, iPCriterion.ip().toString()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | private static class FormatTcp implements CriterionTypeFormatter { |
| 226 | @Override |
| 227 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 228 | final TcpPortCriterion tcpPortCriterion = |
| 229 | (TcpPortCriterion) criterion; |
| 230 | return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort()); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private static class FormatUdp implements CriterionTypeFormatter { |
| 235 | @Override |
| 236 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 237 | final UdpPortCriterion udpPortCriterion = |
| 238 | (UdpPortCriterion) criterion; |
| 239 | return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort()); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private static class FormatSctp implements CriterionTypeFormatter { |
| 244 | @Override |
| 245 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 246 | final SctpPortCriterion sctpPortCriterion = |
| 247 | (SctpPortCriterion) criterion; |
| 248 | return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort()); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | private static class FormatIcmpV4Type implements CriterionTypeFormatter { |
| 253 | @Override |
| 254 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 255 | final IcmpTypeCriterion icmpTypeCriterion = |
| 256 | (IcmpTypeCriterion) criterion; |
| 257 | return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType()); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private static class FormatIcmpV4Code implements CriterionTypeFormatter { |
| 262 | @Override |
| 263 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 264 | final IcmpCodeCriterion icmpCodeCriterion = |
| 265 | (IcmpCodeCriterion) criterion; |
| 266 | return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode()); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | private static class FormatIpV6FLabel implements CriterionTypeFormatter { |
| 271 | @Override |
| 272 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 273 | final IPv6FlowLabelCriterion ipv6FlowLabelCriterion = |
| 274 | (IPv6FlowLabelCriterion) criterion; |
| 275 | return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel()); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | private static class FormatIcmpV6Type implements CriterionTypeFormatter { |
| 280 | @Override |
| 281 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 282 | final Icmpv6TypeCriterion icmpv6TypeCriterion = |
| 283 | (Icmpv6TypeCriterion) criterion; |
| 284 | return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type()); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | private static class FormatIcmpV6Code implements CriterionTypeFormatter { |
| 289 | @Override |
| 290 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 291 | final Icmpv6CodeCriterion icmpv6CodeCriterion = |
| 292 | (Icmpv6CodeCriterion) criterion; |
| 293 | return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code()); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | private static class FormatV6NDTarget implements CriterionTypeFormatter { |
| 298 | @Override |
| 299 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 300 | final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion |
| 301 | = (IPv6NDTargetAddressCriterion) criterion; |
| 302 | return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString()); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | private static class FormatV6NDTll implements CriterionTypeFormatter { |
| 307 | @Override |
| 308 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 309 | final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion |
| 310 | = (IPv6NDLinkLayerAddressCriterion) criterion; |
| 311 | return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString()); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | private static class FormatMplsLabel implements CriterionTypeFormatter { |
| 316 | @Override |
| 317 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 318 | final MplsCriterion mplsCriterion = |
| 319 | (MplsCriterion) criterion; |
| 320 | return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt()); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | private static class FormatIpV6Exthdr implements CriterionTypeFormatter { |
| 325 | @Override |
| 326 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 327 | final IPv6ExthdrFlagsCriterion exthdrCriterion = |
| 328 | (IPv6ExthdrFlagsCriterion) criterion; |
| 329 | return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags()); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | private static class FormatOchSigId implements CriterionTypeFormatter { |
| 334 | @Override |
| 335 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 336 | OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda(); |
| 337 | ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID); |
| 338 | |
| 339 | child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name()); |
| 340 | child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name()); |
| 341 | child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier()); |
| 342 | child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity()); |
| 343 | |
| 344 | return root; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | private static class FormatOchSigType implements CriterionTypeFormatter { |
| 349 | @Override |
| 350 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 351 | final OchSignalTypeCriterion ochSignalTypeCriterion = |
| 352 | (OchSignalTypeCriterion) criterion; |
| 353 | return root.put("ochSignalType", ochSignalTypeCriterion.signalType().name()); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | private class FormatDummyType implements CriterionTypeFormatter { |
| 358 | |
| 359 | @Override |
| 360 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 361 | checkNotNull(criterion, "Criterion cannot be null"); |
| 362 | |
| 363 | return root.put(CriterionCodec.TYPE, criterion.type().toString()); |
| 364 | |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Encodes a criterion into a JSON node. |
| 370 | * |
| 371 | * @return encoded JSON object for the given criterion |
| 372 | */ |
| 373 | public ObjectNode encode() { |
| 374 | final ObjectNode result = context.mapper().createObjectNode() |
| 375 | .put(CriterionCodec.TYPE, criterion.type().toString()); |
| 376 | |
| 377 | CriterionTypeFormatter formatter = |
| 378 | checkNotNull( |
| 379 | formatMap.get(criterion.type()), |
| 380 | "No formatter found for criterion type " |
| 381 | + criterion.type().toString()); |
| 382 | |
| 383 | return formatter.encodeCriterion(result, criterion); |
| 384 | } |
| 385 | |
| 386 | } |