Jian Li

Check TrafficTreatment and TrafficSelector in FlowRuleCodecTest

Change-Id: I1694dd9103899731c4e0fdbb1ee531ef36ed6a86
...@@ -39,7 +39,12 @@ import org.onosproject.net.OchSignalType; ...@@ -39,7 +39,12 @@ import org.onosproject.net.OchSignalType;
39 import org.onosproject.net.OduSignalType; 39 import org.onosproject.net.OduSignalType;
40 import org.onosproject.net.PortNumber; 40 import org.onosproject.net.PortNumber;
41 import org.onosproject.net.flow.DefaultFlowRule; 41 import org.onosproject.net.flow.DefaultFlowRule;
42 +import org.onosproject.net.flow.DefaultTrafficSelector;
43 +import org.onosproject.net.flow.DefaultTrafficTreatment;
42 import org.onosproject.net.flow.FlowRule; 44 import org.onosproject.net.flow.FlowRule;
45 +import org.onosproject.net.flow.TrafficSelector;
46 +import org.onosproject.net.flow.TrafficTreatment;
47 +import org.onosproject.net.flow.criteria.Criteria;
43 import org.onosproject.net.flow.criteria.Criterion; 48 import org.onosproject.net.flow.criteria.Criterion;
44 import org.onosproject.net.flow.criteria.EthCriterion; 49 import org.onosproject.net.flow.criteria.EthCriterion;
45 import org.onosproject.net.flow.criteria.EthTypeCriterion; 50 import org.onosproject.net.flow.criteria.EthTypeCriterion;
...@@ -178,11 +183,37 @@ public class FlowRuleCodecTest { ...@@ -178,11 +183,37 @@ public class FlowRuleCodecTest {
178 public void testFlowRuleEncode() { 183 public void testFlowRuleEncode() {
179 184
180 DeviceId deviceId = DeviceId.deviceId("of:000000000000000a"); 185 DeviceId deviceId = DeviceId.deviceId("of:000000000000000a");
186 +
187 + Instruction output = Instructions.createOutput(PortNumber.portNumber(0));
188 + Instruction modL2Src = Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
189 + Instruction modL2Dst = Instructions.modL2Dst(MacAddress.valueOf("44:55:66:77:88:99"));
190 + TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
191 + TrafficTreatment treatment = tBuilder
192 + .add(output)
193 + .add(modL2Src)
194 + .add(modL2Dst)
195 + .build();
196 +
197 + Criterion inPort = Criteria.matchInPort(PortNumber.portNumber(0));
198 + Criterion ethSrc = Criteria.matchEthSrc(MacAddress.valueOf("11:22:33:44:55:66"));
199 + Criterion ethDst = Criteria.matchEthDst(MacAddress.valueOf("44:55:66:77:88:99"));
200 + Criterion ethType = Criteria.matchEthType(Ethernet.TYPE_IPV4);
201 +
202 + TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
203 + TrafficSelector selector = sBuilder
204 + .add(inPort)
205 + .add(ethSrc)
206 + .add(ethDst)
207 + .add(ethType)
208 + .build();
209 +
181 FlowRule permFlowRule = DefaultFlowRule.builder() 210 FlowRule permFlowRule = DefaultFlowRule.builder()
182 .withCookie(1) 211 .withCookie(1)
183 .forTable(1) 212 .forTable(1)
184 .withPriority(1) 213 .withPriority(1)
185 .makePermanent() 214 .makePermanent()
215 + .withTreatment(treatment)
216 + .withSelector(selector)
186 .forDevice(deviceId).build(); 217 .forDevice(deviceId).build();
187 218
188 FlowRule tempFlowRule = DefaultFlowRule.builder() 219 FlowRule tempFlowRule = DefaultFlowRule.builder()
...@@ -190,6 +221,8 @@ public class FlowRuleCodecTest { ...@@ -190,6 +221,8 @@ public class FlowRuleCodecTest {
190 .forTable(1) 221 .forTable(1)
191 .withPriority(1) 222 .withPriority(1)
192 .makeTemporary(1000) 223 .makeTemporary(1000)
224 + .withTreatment(treatment)
225 + .withSelector(selector)
193 .forDevice(deviceId).build(); 226 .forDevice(deviceId).build();
194 227
195 ObjectNode permFlowRuleJson = flowRuleCodec.encode(permFlowRule, context); 228 ObjectNode permFlowRuleJson = flowRuleCodec.encode(permFlowRule, context);
...@@ -260,9 +293,27 @@ public class FlowRuleCodecTest { ...@@ -260,9 +293,27 @@ public class FlowRuleCodecTest {
260 return false; 293 return false;
261 } 294 }
262 295
263 - // TODO: need to check traffic treatment 296 + // check traffic treatment
297 + JsonNode jsonTreatment = jsonNode.get("treatment");
298 + TrafficTreatmentCodecTest.TrafficTreatmentJsonMatcher treatmentMatcher =
299 + TrafficTreatmentCodecTest.TrafficTreatmentJsonMatcher
300 + .matchesTrafficTreatment(flowRule.treatment());
301 +
302 + if (!treatmentMatcher.matches(jsonTreatment)) {
303 + description.appendText("treatment is not matched");
304 + return false;
305 + }
306 +
307 + // check traffic selector
308 + JsonNode jsonSelector = jsonNode.get("selector");
309 + TrafficSelectorCodecTest.TrafficSelectorJsonMatcher selectorMatcher =
310 + TrafficSelectorCodecTest.TrafficSelectorJsonMatcher
311 + .matchesTrafficSelector(flowRule.selector());
264 312
265 - // TODO: need to check selector 313 + if (!selectorMatcher.matches(jsonSelector)) {
314 + description.appendText("selector is not matched");
315 + return false;
316 + }
266 317
267 return true; 318 return true;
268 } 319 }
......
...@@ -95,7 +95,7 @@ public class TrafficTreatmentCodecTest { ...@@ -95,7 +95,7 @@ public class TrafficTreatmentCodecTest {
95 assertThat(types.contains(insts.get(1).type().name()), is(true)); 95 assertThat(types.contains(insts.get(1).type().name()), is(true));
96 } 96 }
97 97
98 - private static final class TrafficTreatmentJsonMatcher 98 + public static final class TrafficTreatmentJsonMatcher
99 extends TypeSafeDiagnosingMatcher<JsonNode> { 99 extends TypeSafeDiagnosingMatcher<JsonNode> {
100 100
101 private final TrafficTreatment trafficTreatment; 101 private final TrafficTreatment trafficTreatment;
...@@ -163,19 +163,23 @@ public class TrafficTreatmentCodecTest { ...@@ -163,19 +163,23 @@ public class TrafficTreatmentCodecTest {
163 // check metered 163 // check metered
164 JsonNode meterNode = getInstNode(jsonInstructions, "METER"); 164 JsonNode meterNode = getInstNode(jsonInstructions, "METER");
165 String jsonMeterId = meterNode != null ? meterNode.get("meterId").asText() : null; 165 String jsonMeterId = meterNode != null ? meterNode.get("meterId").asText() : null;
166 - String meterId = trafficTreatment.metered().meterId().toString(); 166 + if (trafficTreatment.metered() != null) {
167 - if (!StringUtils.equals(jsonMeterId, meterId)) { 167 + String meterId = trafficTreatment.metered().meterId().toString();
168 - description.appendText("meter id was " + jsonMeterId); 168 + if (!StringUtils.equals(jsonMeterId, meterId)) {
169 - return false; 169 + description.appendText("meter id was " + jsonMeterId);
170 + return false;
171 + }
170 } 172 }
171 173
172 // check table transition 174 // check table transition
173 JsonNode tableNode = getInstNode(jsonInstructions, "TABLE"); 175 JsonNode tableNode = getInstNode(jsonInstructions, "TABLE");
174 String jsonTableId = tableNode != null ? tableNode.get("tableId").asText() : null; 176 String jsonTableId = tableNode != null ? tableNode.get("tableId").asText() : null;
175 - String tableId = trafficTreatment.tableTransition().tableId().toString(); 177 + if (trafficTreatment.tableTransition() != null) {
176 - if (!StringUtils.equals(jsonTableId, tableId)) { 178 + String tableId = trafficTreatment.tableTransition().tableId().toString();
177 - description.appendText("table id was " + jsonMeterId); 179 + if (!StringUtils.equals(jsonTableId, tableId)) {
178 - return false; 180 + description.appendText("table id was " + jsonMeterId);
181 + return false;
182 + }
179 } 183 }
180 184
181 // TODO: check deferred 185 // TODO: check deferred
......