Ray Milkey | db35808 | 2015-01-13 16:34:38 -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 | |
| 18 | import org.hamcrest.Description; |
| 19 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 20 | import org.onosproject.net.flow.criteria.Criteria; |
| 21 | import org.onosproject.net.flow.criteria.Criterion; |
| 22 | |
| 23 | import com.fasterxml.jackson.databind.JsonNode; |
| 24 | |
| 25 | /** |
| 26 | * Hamcrest matcher for criterion objects. |
| 27 | */ |
| 28 | public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 29 | |
| 30 | final Criterion criterion; |
| 31 | |
| 32 | private CriterionJsonMatcher(Criterion criterionValue) { |
| 33 | criterion = criterionValue; |
| 34 | } |
| 35 | |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame^] | 36 | // CHECKSTYLE IGNORE MethodLength FOR NEXT 300 LINES |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 37 | @Override |
| 38 | public boolean matchesSafely(JsonNode jsonCriterion, Description description) { |
| 39 | final String type = criterion.type().name(); |
| 40 | final String jsonType = jsonCriterion.get("type").asText(); |
| 41 | if (!type.equals(jsonType)) { |
| 42 | description.appendText("type was " + type); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | switch (criterion.type()) { |
| 47 | |
| 48 | case IN_PORT: |
| 49 | final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion; |
| 50 | final long port = portCriterion.port().toLong(); |
| 51 | final long jsonPort = jsonCriterion.get("port").asLong(); |
| 52 | if (port != jsonPort) { |
| 53 | description.appendText("port was " + Long.toString(jsonPort)); |
| 54 | return false; |
| 55 | } |
| 56 | break; |
| 57 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 58 | case ETH_DST: |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame^] | 59 | case ETH_SRC: |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 60 | final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion; |
| 61 | final String mac = ethCriterion.mac().toString(); |
| 62 | final String jsonMac = jsonCriterion.get("mac").textValue(); |
| 63 | if (!mac.equals(jsonMac)) { |
| 64 | description.appendText("mac was " + jsonMac); |
| 65 | return false; |
| 66 | } |
| 67 | break; |
| 68 | |
| 69 | case ETH_TYPE: |
| 70 | final Criteria.EthTypeCriterion ethTypeCriterion = |
| 71 | (Criteria.EthTypeCriterion) criterion; |
| 72 | final String ethType = ethTypeCriterion.ethType().toString(); |
| 73 | final String jsonEthType = jsonCriterion.get("ethType").textValue(); |
| 74 | if (!ethType.equals(jsonEthType)) { |
| 75 | description.appendText("ethType was " + jsonEthType); |
| 76 | return false; |
| 77 | } |
| 78 | break; |
| 79 | |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame^] | 80 | case VLAN_VID: |
| 81 | final Criteria.VlanIdCriterion vlanIdCriterion = |
| 82 | (Criteria.VlanIdCriterion) criterion; |
| 83 | final short vlanId = vlanIdCriterion.vlanId().toShort(); |
| 84 | final short jsonVlanId = jsonCriterion.get("vlanId").shortValue(); |
| 85 | if (vlanId != jsonVlanId) { |
| 86 | description.appendText("vlanId was " + Short.toString(jsonVlanId)); |
| 87 | return false; |
| 88 | } |
| 89 | break; |
| 90 | |
| 91 | case VLAN_PCP: |
| 92 | final Criteria.VlanPcpCriterion vlanPcpCriterion = |
| 93 | (Criteria.VlanPcpCriterion) criterion; |
| 94 | final byte priority = vlanPcpCriterion.priority(); |
| 95 | final byte jsonPriority = (byte) jsonCriterion.get("priority").shortValue(); |
| 96 | if (priority != jsonPriority) { |
| 97 | description.appendText("priority was " + Byte.toString(jsonPriority)); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | break; |
| 101 | |
| 102 | case IP_PROTO: |
| 103 | final Criteria.IPProtocolCriterion iPProtocolCriterion = |
| 104 | (Criteria.IPProtocolCriterion) criterion; |
| 105 | final byte protocol = iPProtocolCriterion.protocol(); |
| 106 | final byte jsonProtocol = (byte) jsonCriterion.get("protocol").shortValue(); |
| 107 | if (protocol != jsonProtocol) { |
| 108 | description.appendText("protocol was " + Byte.toString(jsonProtocol)); |
| 109 | return false; |
| 110 | } |
| 111 | break; |
| 112 | |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame^] | 113 | case IPV4_SRC: |
| 114 | case IPV4_DST: |
| 115 | case IPV6_SRC: |
| 116 | case IPV6_DST: |
| 117 | final Criteria.IPCriterion ipCriterion = (Criteria.IPCriterion) criterion; |
| 118 | final String ip = ipCriterion.ip().toString(); |
| 119 | final String jsonIp = jsonCriterion.get("ip").textValue(); |
| 120 | if (!ip.equals(jsonIp)) { |
| 121 | description.appendText("ip was " + jsonIp); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | break; |
| 125 | |
| 126 | case TCP_SRC: |
| 127 | case TCP_DST: |
| 128 | final Criteria.TcpPortCriterion tcpPortCriterion = |
| 129 | (Criteria.TcpPortCriterion) criterion; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame^] | 130 | final short tcpPort = tcpPortCriterion.tcpPort(); |
| 131 | final short jsonTcpPort = jsonCriterion.get("tcpPort").shortValue(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 132 | if (tcpPort != jsonTcpPort) { |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame^] | 133 | description.appendText("tcp port was " + Short.toString(jsonTcpPort)); |
| 134 | return false; |
| 135 | } |
| 136 | break; |
| 137 | |
| 138 | case UDP_SRC: |
| 139 | case UDP_DST: |
| 140 | final Criteria.UdpPortCriterion udpPortCriterion = |
| 141 | (Criteria.UdpPortCriterion) criterion; |
| 142 | final short udpPort = udpPortCriterion.udpPort(); |
| 143 | final short jsonUdpPort = jsonCriterion.get("udpPort").shortValue(); |
| 144 | if (udpPort != jsonUdpPort) { |
| 145 | description.appendText("udp port was " + Short.toString(jsonUdpPort)); |
| 146 | return false; |
| 147 | } |
| 148 | break; |
| 149 | |
| 150 | case SCTP_SRC: |
| 151 | case SCTP_DST: |
| 152 | final Criteria.SctpPortCriterion sctpPortCriterion = |
| 153 | (Criteria.SctpPortCriterion) criterion; |
| 154 | final short sctpPort = sctpPortCriterion.sctpPort(); |
| 155 | final short jsonSctpPort = jsonCriterion.get("sctpPort").shortValue(); |
| 156 | if (sctpPort != jsonSctpPort) { |
| 157 | description.appendText("sctp port was " + Short.toString(jsonSctpPort)); |
| 158 | return false; |
| 159 | } |
| 160 | break; |
| 161 | |
| 162 | case ICMPV4_TYPE: |
| 163 | final Criteria.IcmpTypeCriterion icmpTypeCriterion = |
| 164 | (Criteria.IcmpTypeCriterion) criterion; |
| 165 | final byte icmpType = icmpTypeCriterion.icmpType(); |
| 166 | final byte jsonIcmpType = (byte) jsonCriterion.get("icmpType").shortValue(); |
| 167 | if (icmpType != jsonIcmpType) { |
| 168 | description.appendText("icmp type was " + Byte.toString(jsonIcmpType)); |
| 169 | return false; |
| 170 | } |
| 171 | break; |
| 172 | |
| 173 | case ICMPV4_CODE: |
| 174 | final Criteria.IcmpCodeCriterion icmpCodeCriterion = |
| 175 | (Criteria.IcmpCodeCriterion) criterion; |
| 176 | final byte icmpCode = icmpCodeCriterion.icmpCode(); |
| 177 | final byte jsonIcmpCode = (byte) jsonCriterion.get("icmpCode").shortValue(); |
| 178 | if (icmpCode != jsonIcmpCode) { |
| 179 | description.appendText("icmp code was " + Byte.toString(jsonIcmpCode)); |
| 180 | return false; |
| 181 | } |
| 182 | break; |
| 183 | |
| 184 | case IPV6_FLABEL: |
| 185 | final Criteria.IPv6FlowLabelCriterion ipv6FlowLabelCriterion = |
| 186 | (Criteria.IPv6FlowLabelCriterion) criterion; |
| 187 | final int flowLabel = ipv6FlowLabelCriterion.flowLabel(); |
| 188 | final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue(); |
| 189 | if (flowLabel != jsonFlowLabel) { |
| 190 | description.appendText("IPv6 flow label was " + Integer.toString(jsonFlowLabel)); |
| 191 | return false; |
| 192 | } |
| 193 | break; |
| 194 | |
| 195 | case ICMPV6_TYPE: |
| 196 | final Criteria.Icmpv6TypeCriterion icmpv6TypeCriterion = |
| 197 | (Criteria.Icmpv6TypeCriterion) criterion; |
| 198 | final byte icmpv6Type = icmpv6TypeCriterion.icmpv6Type(); |
| 199 | final byte jsonIcmpv6Type = (byte) jsonCriterion.get("icmpv6Type").shortValue(); |
| 200 | if (icmpv6Type != jsonIcmpv6Type) { |
| 201 | description.appendText("icmpv6 type was " + Byte.toString(jsonIcmpv6Type)); |
| 202 | return false; |
| 203 | } |
| 204 | break; |
| 205 | |
| 206 | case ICMPV6_CODE: |
| 207 | final Criteria.Icmpv6CodeCriterion icmpv6CodeCriterion = |
| 208 | (Criteria.Icmpv6CodeCriterion) criterion; |
| 209 | final byte icmpv6Code = icmpv6CodeCriterion.icmpv6Code(); |
| 210 | final byte jsonIcmpv6Code = (byte) jsonCriterion.get("icmpv6Code").shortValue(); |
| 211 | if (icmpv6Code != jsonIcmpv6Code) { |
| 212 | description.appendText("icmpv6 code was " + Byte.toString(jsonIcmpv6Code)); |
| 213 | return false; |
| 214 | } |
| 215 | break; |
| 216 | |
| 217 | case IPV6_ND_TARGET: |
| 218 | final Criteria.IPv6NDTargetAddressCriterion |
| 219 | ipv6NDTargetAddressCriterion = |
| 220 | (Criteria.IPv6NDTargetAddressCriterion) criterion; |
| 221 | final String targetAddress = |
| 222 | ipv6NDTargetAddressCriterion.targetAddress().toString(); |
| 223 | final String jsonTargetAddress = |
| 224 | jsonCriterion.get("targetAddress").textValue(); |
| 225 | if (!targetAddress.equals(jsonTargetAddress)) { |
| 226 | description.appendText("target address was " + |
| 227 | jsonTargetAddress); |
| 228 | return false; |
| 229 | } |
| 230 | break; |
| 231 | |
| 232 | case IPV6_ND_SLL: |
| 233 | case IPV6_ND_TLL: |
| 234 | final Criteria.IPv6NDLinkLayerAddressCriterion |
| 235 | ipv6NDLinkLayerAddressCriterion = |
| 236 | (Criteria.IPv6NDLinkLayerAddressCriterion) criterion; |
| 237 | final String llAddress = |
| 238 | ipv6NDLinkLayerAddressCriterion.mac().toString(); |
| 239 | final String jsonLlAddress = |
| 240 | jsonCriterion.get("mac").textValue(); |
| 241 | if (!llAddress.equals(jsonLlAddress)) { |
| 242 | description.appendText("mac was " + jsonLlAddress); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | break; |
| 246 | |
| 247 | case MPLS_LABEL: |
| 248 | final Criteria.MplsCriterion mplsCriterion = |
| 249 | (Criteria.MplsCriterion) criterion; |
| 250 | final int label = mplsCriterion.label(); |
| 251 | final int jsonLabel = jsonCriterion.get("label").intValue(); |
| 252 | if (label != jsonLabel) { |
| 253 | description.appendText("label was " + Integer.toString(jsonLabel)); |
| 254 | return false; |
| 255 | } |
| 256 | break; |
| 257 | |
| 258 | case OCH_SIGID: |
| 259 | final Criteria.LambdaCriterion lambdaCriterion = |
| 260 | (Criteria.LambdaCriterion) criterion; |
| 261 | final short lambda = lambdaCriterion.lambda(); |
| 262 | final short jsonLambda = jsonCriterion.get("lambda").shortValue(); |
| 263 | if (lambda != jsonLambda) { |
| 264 | description.appendText("lambda was " + Short.toString(lambda)); |
| 265 | return false; |
| 266 | } |
| 267 | break; |
| 268 | |
| 269 | case OCH_SIGTYPE: |
| 270 | final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion = |
| 271 | (Criteria.OpticalSignalTypeCriterion) criterion; |
| 272 | final short signalType = opticalSignalTypeCriterion.signalType(); |
| 273 | final short jsonSignalType = jsonCriterion.get("signalType").shortValue(); |
| 274 | if (signalType != jsonSignalType) { |
| 275 | description.appendText("signal type was " + Short.toString(signalType)); |
| 276 | return false; |
| 277 | } |
| 278 | break; |
| 279 | |
| 280 | default: |
| 281 | // Don't know how to format this type |
| 282 | description.appendText("unknown criterion type " + |
| 283 | criterion.type()); |
| 284 | return false; |
| 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public void describeTo(Description description) { |
| 291 | description.appendText(criterion.toString()); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Factory to allocate an criterion matcher. |
| 296 | * |
| 297 | * @param criterion criterion object we are looking for |
| 298 | * @return matcher |
| 299 | */ |
| 300 | public static CriterionJsonMatcher matchesCriterion(Criterion criterion) { |
| 301 | return new CriterionJsonMatcher(criterion); |
| 302 | } |
| 303 | } |