blob: e0baf879722331478b704d6a477da31182503122 [file] [log] [blame]
Ray Milkeydb358082015-01-13 16:34:38 -08001/*
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 */
16package org.onosproject.codec.impl;
17
18import org.hamcrest.Description;
19import org.hamcrest.TypeSafeDiagnosingMatcher;
20import org.onosproject.net.flow.criteria.Criteria;
21import org.onosproject.net.flow.criteria.Criterion;
22
23import com.fasterxml.jackson.databind.JsonNode;
24
25/**
26 * Hamcrest matcher for criterion objects.
27 */
28public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
29
30 final Criterion criterion;
31
32 private CriterionJsonMatcher(Criterion criterionValue) {
33 criterion = criterionValue;
34 }
35
36 @Override
37 public boolean matchesSafely(JsonNode jsonCriterion, Description description) {
38 final String type = criterion.type().name();
39 final String jsonType = jsonCriterion.get("type").asText();
40 if (!type.equals(jsonType)) {
41 description.appendText("type was " + type);
42 return false;
43 }
44
45 switch (criterion.type()) {
46
47 case IN_PORT:
48 final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion;
49 final long port = portCriterion.port().toLong();
50 final long jsonPort = jsonCriterion.get("port").asLong();
51 if (port != jsonPort) {
52 description.appendText("port was " + Long.toString(jsonPort));
53 return false;
54 }
55 break;
56
57 case ETH_SRC:
58 case ETH_DST:
59 final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion;
60 final String mac = ethCriterion.mac().toString();
61 final String jsonMac = jsonCriterion.get("mac").textValue();
62 if (!mac.equals(jsonMac)) {
63 description.appendText("mac was " + jsonMac);
64 return false;
65 }
66 break;
67
68 case ETH_TYPE:
69 final Criteria.EthTypeCriterion ethTypeCriterion =
70 (Criteria.EthTypeCriterion) criterion;
71 final String ethType = ethTypeCriterion.ethType().toString();
72 final String jsonEthType = jsonCriterion.get("ethType").textValue();
73 if (!ethType.equals(jsonEthType)) {
74 description.appendText("ethType was " + jsonEthType);
75 return false;
76 }
77 break;
78
79 case IPV4_SRC:
80 case IPV6_SRC:
81 case IPV4_DST:
82 case IPV6_DST:
83 final Criteria.IPCriterion ipCriterion = (Criteria.IPCriterion) criterion;
84 final String ip = ipCriterion.ip().toString();
85 final String jsonIp = jsonCriterion.get("ip").textValue();
86 if (!ip.equals(jsonIp)) {
87 description.appendText("ip was " + jsonIp);
88 return false;
89 }
90 break;
91
92 case IP_PROTO:
93 final Criteria.IPProtocolCriterion iPProtocolCriterion =
94 (Criteria.IPProtocolCriterion) criterion;
95 final byte protocol = iPProtocolCriterion.protocol();
96 final byte jsonProtocol = (byte) jsonCriterion.get("protocol").shortValue();
97 if (protocol != jsonProtocol) {
98 description.appendText("protocol was " + Byte.toString(jsonProtocol));
99 return false;
100 }
101 break;
102
103 case VLAN_PCP:
104 final Criteria.VlanPcpCriterion vlanPcpCriterion =
105 (Criteria.VlanPcpCriterion) criterion;
106 final byte priority = vlanPcpCriterion.priority();
107 final byte jsonPriority = (byte) jsonCriterion.get("protocol").shortValue();
108 if (priority != jsonPriority) {
109 description.appendText("priority was " + Byte.toString(jsonPriority));
110 return false;
111 }
112 break;
113
114 case VLAN_VID:
115 final Criteria.VlanIdCriterion vlanIdCriterion =
116 (Criteria.VlanIdCriterion) criterion;
117 final short vlanId = vlanIdCriterion.vlanId().toShort();
118 final short jsonvlanId = jsonCriterion.get("vlanId").shortValue();
119 if (vlanId != jsonvlanId) {
120 description.appendText("vlanId was " + Short.toString(jsonvlanId));
121 return false;
122 }
123 break;
124
125 case TCP_SRC:
126 case TCP_DST:
127 final Criteria.TcpPortCriterion tcpPortCriterion =
128 (Criteria.TcpPortCriterion) criterion;
129 final byte tcpPort = tcpPortCriterion.tcpPort().byteValue();
130 final byte jsonTcpPort = (byte) jsonCriterion.get("tcpPort").shortValue();
131 if (tcpPort != jsonTcpPort) {
132 description.appendText("tcp port was " + Byte.toString(jsonTcpPort));
133 return false;
134 }
135 break;
136
137 case MPLS_LABEL:
138 final Criteria.MplsCriterion mplsCriterion =
139 (Criteria.MplsCriterion) criterion;
140 final int label = mplsCriterion.label();
141 final int jsonLabel = jsonCriterion.get("label").intValue();
142 if (label != jsonLabel) {
143 description.appendText("label was " + Integer.toString(jsonLabel));
144 return false;
145 }
146 break;
147
148 case OCH_SIGID:
149 final Criteria.LambdaCriterion lambdaCriterion =
150 (Criteria.LambdaCriterion) criterion;
151 final short lambda = lambdaCriterion.lambda();
152 final short jsonLambda = jsonCriterion.get("lambda").shortValue();
153 if (lambda != jsonLambda) {
154 description.appendText("lambda was " + Short.toString(lambda));
155 return false;
156 }
157 break;
158
159 case OCH_SIGTYPE:
160 final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
161 (Criteria.OpticalSignalTypeCriterion) criterion;
162 final short signalType = opticalSignalTypeCriterion.signalType();
163 final short jsonSignalType = jsonCriterion.get("signalType").shortValue();
164 if (signalType != jsonSignalType) {
165 description.appendText("signal type was " + Short.toString(signalType));
166 return false;
167 }
168 break;
169
170 default:
171 // Don't know how to format this type
172 description.appendText("unknown criterion type " +
173 criterion.type());
174 return false;
175 }
176 return true;
177 }
178
179 @Override
180 public void describeTo(Description description) {
181 description.appendText(criterion.toString());
182 }
183
184 /**
185 * Factory to allocate an criterion matcher.
186 *
187 * @param criterion criterion object we are looking for
188 * @return matcher
189 */
190 public static CriterionJsonMatcher matchesCriterion(Criterion criterion) {
191 return new CriterionJsonMatcher(criterion);
192 }
193}