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