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; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 22 | import org.onosproject.net.flow.criteria.Criterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 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.LambdaCriterion; |
| 38 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
| 39 | import org.onosproject.net.flow.criteria.MplsCriterion; |
| 40 | import org.onosproject.net.flow.criteria.OpticalSignalTypeCriterion; |
| 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; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
| 49 | |
| 50 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 51 | |
| 52 | import static com.google.common.base.Preconditions.checkNotNull; |
| 53 | |
| 54 | /** |
| 55 | * Criterion codec. |
| 56 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 57 | public final class CriterionCodec extends JsonCodec<Criterion> { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 58 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 59 | 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 Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 69 | formatMap.put(Criterion.Type.METADATA, new FormatMetadata()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 70 | formatMap.put(Criterion.Type.ETH_DST, new FormatEth()); |
| 71 | formatMap.put(Criterion.Type.ETH_SRC, new FormatEth()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 72 | formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 73 | 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 Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 77 | formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 78 | formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp()); |
| 79 | formatMap.put(Criterion.Type.IPV4_DST, new FormatIp()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 80 | 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 Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 88 | formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp()); |
| 89 | formatMap.put(Criterion.Type.IPV6_DST, new FormatIp()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 90 | 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 Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 97 | formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 98 | formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId()); |
| 99 | formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType()); |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 100 | formatMap.put(Criterion.Type.DUMMY, new FormatDummyType()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 101 | |
| 102 | // Currently unimplemented |
| 103 | formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 104 | formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 105 | formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown()); |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 106 | formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown()); |
| 107 | formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 108 | 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 Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 111 | formatMap.put(Criterion.Type.TUNNEL_ID, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 112 | formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown()); |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 113 | formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 114 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 133 | final PortCriterion portCriterion = (PortCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 134 | return root.put("port", portCriterion.port().toLong()); |
| 135 | } |
| 136 | } |
| 137 | |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 138 | private static class FormatMetadata implements CriterionTypeFormatter { |
| 139 | @Override |
| 140 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 141 | final MetadataCriterion metadataCriterion = |
| 142 | (MetadataCriterion) criterion; |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 143 | return root.put("metadata", metadataCriterion.metadata()); |
| 144 | } |
| 145 | } |
| 146 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 147 | private static class FormatEth implements CriterionTypeFormatter { |
| 148 | @Override |
| 149 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 150 | final EthCriterion ethCriterion = (EthCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 151 | return root.put("mac", ethCriterion.mac().toString()); |
| 152 | } |
| 153 | } |
| 154 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 155 | private static class FormatEthType implements CriterionTypeFormatter { |
| 156 | @Override |
| 157 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 158 | final EthTypeCriterion ethTypeCriterion = |
| 159 | (EthTypeCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 160 | return root.put("ethType", ethTypeCriterion.ethType()); |
| 161 | } |
| 162 | } |
| 163 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 164 | private static class FormatVlanVid implements CriterionTypeFormatter { |
| 165 | @Override |
| 166 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 167 | final VlanIdCriterion vlanIdCriterion = |
| 168 | (VlanIdCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 169 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 176 | final VlanPcpCriterion vlanPcpCriterion = |
| 177 | (VlanPcpCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 178 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 185 | final IPDscpCriterion ipDscpCriterion = |
| 186 | (IPDscpCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 187 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 194 | final IPEcnCriterion ipEcnCriterion = |
| 195 | (IPEcnCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 196 | return root.put("ipEcn", ipEcnCriterion.ipEcn()); |
| 197 | } |
| 198 | } |
| 199 | |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 200 | private static class FormatIpProto implements CriterionTypeFormatter { |
| 201 | @Override |
| 202 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 203 | final IPProtocolCriterion iPProtocolCriterion = |
| 204 | (IPProtocolCriterion) criterion; |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 205 | return root.put("protocol", iPProtocolCriterion.protocol()); |
| 206 | } |
| 207 | } |
| 208 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 209 | private static class FormatIp implements CriterionTypeFormatter { |
| 210 | @Override |
| 211 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 212 | final IPCriterion iPCriterion = (IPCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 213 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 220 | final TcpPortCriterion tcpPortCriterion = |
| 221 | (TcpPortCriterion) criterion; |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 222 | return root.put("tcpPort", tcpPortCriterion.tcpPort()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | private static class FormatUdp implements CriterionTypeFormatter { |
| 227 | @Override |
| 228 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 229 | final UdpPortCriterion udpPortCriterion = |
| 230 | (UdpPortCriterion) criterion; |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 231 | return root.put("udpPort", udpPortCriterion.udpPort()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | private static class FormatSctp implements CriterionTypeFormatter { |
| 236 | @Override |
| 237 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 238 | final SctpPortCriterion sctpPortCriterion = |
| 239 | (SctpPortCriterion) criterion; |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 240 | return root.put("sctpPort", sctpPortCriterion.sctpPort()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | private static class FormatIcmpV4Type implements CriterionTypeFormatter { |
| 245 | @Override |
| 246 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 247 | final IcmpTypeCriterion icmpTypeCriterion = |
| 248 | (IcmpTypeCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 249 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 256 | final IcmpCodeCriterion icmpCodeCriterion = |
| 257 | (IcmpCodeCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 258 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 265 | final IPv6FlowLabelCriterion ipv6FlowLabelCriterion = |
| 266 | (IPv6FlowLabelCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 267 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 274 | final Icmpv6TypeCriterion icmpv6TypeCriterion = |
| 275 | (Icmpv6TypeCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 276 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 283 | final Icmpv6CodeCriterion icmpv6CodeCriterion = |
| 284 | (Icmpv6CodeCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 285 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 292 | final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion |
| 293 | = (IPv6NDTargetAddressCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 294 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 301 | final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion |
| 302 | = (IPv6NDLinkLayerAddressCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 303 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 310 | final MplsCriterion mplsCriterion = |
| 311 | (MplsCriterion) criterion; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 312 | return root.put("label", mplsCriterion.label().toInt()); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 316 | private static class FormatIpV6Exthdr implements CriterionTypeFormatter { |
| 317 | @Override |
| 318 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 319 | final IPv6ExthdrFlagsCriterion exthdrCriterion = |
| 320 | (IPv6ExthdrFlagsCriterion) criterion; |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 321 | return root.put("exthdrFlags", exthdrCriterion.exthdrFlags()); |
| 322 | } |
| 323 | } |
| 324 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 325 | private static class FormatOchSigId implements CriterionTypeFormatter { |
| 326 | @Override |
| 327 | public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 328 | final LambdaCriterion lambdaCriterion = |
| 329 | (LambdaCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 330 | 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 SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 337 | final OpticalSignalTypeCriterion opticalSignalTypeCriterion = |
| 338 | (OpticalSignalTypeCriterion) criterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 339 | return root.put("signalType", opticalSignalTypeCriterion.signalType()); |
| 340 | } |
| 341 | } |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 342 | |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 343 | 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 Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 354 | @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 Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 361 | CriterionTypeFormatter formatter = |
| 362 | checkNotNull( |
| 363 | formatMap.get(criterion.type()), |
| 364 | "No formatter found for criterion type " |
| 365 | + criterion.type().toString()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 366 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 367 | return formatter.formatCriterion(result, criterion); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 368 | } |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 369 | |
| 370 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 371 | } |