blob: ad046025dd376810f6ac9b29148b8f379c5a6160 [file] [log] [blame]
Ray Milkey97f8f102014-11-14 14:18:23 -08001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
Ray Milkey97f8f102014-11-14 14:18:23 -080017
alshabib346b5b32015-03-06 00:42:16 -080018import com.google.common.testing.EqualsTester;
Ray Milkey97f8f102014-11-14 14:18:23 -080019import org.junit.Test;
Ray Milkey97f8f102014-11-14 14:18:23 -080020import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
alshabib346b5b32015-03-06 00:42:16 -080023import org.onosproject.net.PortNumber;
24import org.onosproject.net.flow.instructions.Instruction;
25import org.onosproject.net.flow.instructions.Instructions;
Ray Milkey97f8f102014-11-14 14:18:23 -080026
alshabib346b5b32015-03-06 00:42:16 -080027import java.util.List;
Ray Milkey97f8f102014-11-14 14:18:23 -080028
29import static org.hamcrest.CoreMatchers.equalTo;
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.hasSize;
32import static org.hamcrest.Matchers.is;
Ray Milkey97f8f102014-11-14 14:18:23 -080033
34/**
35 * Unit tests for the DefaultTrafficTreatment class.
36 */
37public class DefaultTrafficTreatmentTest {
38
39 // Tests for the nested Builder class
40
41 /**
42 * Tests that the Builder constructors return equivalent objects
43 * when given the same data.
44 */
45 @Test
46 public void testTreatmentBuilderConstructors() {
47 final TrafficTreatment treatment1 =
48 DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080049 .add(Instructions.modL0Lambda((short) 4))
Ray Milkey97f8f102014-11-14 14:18:23 -080050 .build();
51 final TrafficTreatment treatment2 =
52 DefaultTrafficTreatment.builder(treatment1).build();
53 assertThat(treatment1, is(equalTo(treatment2)));
54 }
55
56 /**
57 * Tests methods defined on the Builder.
58 */
59 @Test
60 public void testBuilderMethods() {
61 final Instruction instruction1 =
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080062 Instructions.modL0Lambda((short) 4);
Ray Milkey97f8f102014-11-14 14:18:23 -080063
64 final TrafficTreatment.Builder builder1 =
65 DefaultTrafficTreatment.builder()
66 .add(instruction1)
67 .setEthDst(MacAddress.BROADCAST)
68 .setEthSrc(MacAddress.BROADCAST)
69 .setIpDst(IpAddress.valueOf("1.1.1.1"))
70 .setIpSrc(IpAddress.valueOf("2.2.2.2"))
71 .setLambda((short) 4)
72 .setOutput(PortNumber.portNumber(2))
73 .setVlanId(VlanId.vlanId((short) 4))
74 .setVlanPcp((byte) 3);
75
76 final TrafficTreatment treatment1 = builder1.build();
77
Ray Milkey42507352015-03-20 15:16:10 -070078 final List<Instruction> instructions1 = treatment1.immediate();
Ray Milkey97f8f102014-11-14 14:18:23 -080079 assertThat(instructions1, hasSize(9));
80
81 builder1.drop();
82 builder1.add(instruction1);
83
Ray Milkey42507352015-03-20 15:16:10 -070084 final List<Instruction> instructions2 = builder1.build().immediate();
alshabib346b5b32015-03-06 00:42:16 -080085 assertThat(instructions2, hasSize(11));
Ray Milkey42507352015-03-20 15:16:10 -070086
87 builder1.deferred()
88 .popVlan()
89 .pushVlan()
90 .setVlanId(VlanId.vlanId((short) 5));
91
92 final List<Instruction> instructions3 = builder1.build().immediate();
93 assertThat(instructions3, hasSize(11));
94 final List<Instruction> instructions4 = builder1.build().deferred();
95 assertThat(instructions4, hasSize(3));
Ray Milkey97f8f102014-11-14 14:18:23 -080096 }
97
98 /**
99 * Tests equals(), hashCode() and toString() methods of
100 * DefaultTrafficTreatment.
101 */
102 @Test
103 public void testEquals() {
104 final TrafficTreatment treatment1 =
105 DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800106 .add(Instructions.modL0Lambda((short) 4))
Ray Milkey97f8f102014-11-14 14:18:23 -0800107 .build();
108 final TrafficTreatment sameAsTreatment1 =
109 DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800110 .add(Instructions.modL0Lambda((short) 4))
Ray Milkey97f8f102014-11-14 14:18:23 -0800111 .build();
112 final TrafficTreatment treatment2 =
113 DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800114 .add(Instructions.modL0Lambda((short) 2))
Ray Milkey97f8f102014-11-14 14:18:23 -0800115 .build();
116 new EqualsTester()
117 .addEqualityGroup(treatment1, sameAsTreatment1)
118 .addEqualityGroup(treatment2)
119 .testEquals();
120 }
121}