blob: 288f5f2f4bbfc97c8e3996f1628a4fc15c229ee3 [file] [log] [blame]
Ray Milkey97f8f102014-11-14 14:18:23 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Ray Milkey97f8f102014-11-14 14:18:23 -08003 *
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;
Ray Milkey85571eb2015-06-25 09:45:46 -070023import org.onosproject.net.IndexedLambda;
alshabib346b5b32015-03-06 00:42:16 -080024import org.onosproject.net.PortNumber;
25import org.onosproject.net.flow.instructions.Instruction;
26import org.onosproject.net.flow.instructions.Instructions;
Ray Milkey97f8f102014-11-14 14:18:23 -080027
alshabib346b5b32015-03-06 00:42:16 -080028import java.util.List;
Ray Milkey97f8f102014-11-14 14:18:23 -080029
30import static org.hamcrest.CoreMatchers.equalTo;
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.hasSize;
33import static org.hamcrest.Matchers.is;
Ray Milkey97f8f102014-11-14 14:18:23 -080034
35/**
36 * Unit tests for the DefaultTrafficTreatment class.
37 */
38public class DefaultTrafficTreatmentTest {
39
40 // Tests for the nested Builder class
41
42 /**
43 * Tests that the Builder constructors return equivalent objects
44 * when given the same data.
45 */
46 @Test
47 public void testTreatmentBuilderConstructors() {
48 final TrafficTreatment treatment1 =
49 DefaultTrafficTreatment.builder()
Ray Milkey85571eb2015-06-25 09:45:46 -070050 .add(Instructions.modL0Lambda(new IndexedLambda(4)))
Ray Milkey97f8f102014-11-14 14:18:23 -080051 .build();
52 final TrafficTreatment treatment2 =
53 DefaultTrafficTreatment.builder(treatment1).build();
54 assertThat(treatment1, is(equalTo(treatment2)));
55 }
56
57 /**
58 * Tests methods defined on the Builder.
59 */
60 @Test
61 public void testBuilderMethods() {
62 final Instruction instruction1 =
Ray Milkey85571eb2015-06-25 09:45:46 -070063 Instructions.modL0Lambda(new IndexedLambda(4));
Ray Milkey97f8f102014-11-14 14:18:23 -080064
65 final TrafficTreatment.Builder builder1 =
66 DefaultTrafficTreatment.builder()
67 .add(instruction1)
68 .setEthDst(MacAddress.BROADCAST)
69 .setEthSrc(MacAddress.BROADCAST)
70 .setIpDst(IpAddress.valueOf("1.1.1.1"))
71 .setIpSrc(IpAddress.valueOf("2.2.2.2"))
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070072 .add(Instructions.modL0Lambda(new IndexedLambda(4)))
Ray Milkey97f8f102014-11-14 14:18:23 -080073 .setOutput(PortNumber.portNumber(2))
74 .setVlanId(VlanId.vlanId((short) 4))
75 .setVlanPcp((byte) 3);
76
77 final TrafficTreatment treatment1 = builder1.build();
78
Ray Milkey42507352015-03-20 15:16:10 -070079 final List<Instruction> instructions1 = treatment1.immediate();
Ray Milkey97f8f102014-11-14 14:18:23 -080080 assertThat(instructions1, hasSize(9));
81
82 builder1.drop();
83 builder1.add(instruction1);
84
Ray Milkey42507352015-03-20 15:16:10 -070085 final List<Instruction> instructions2 = builder1.build().immediate();
alshabib346b5b32015-03-06 00:42:16 -080086 assertThat(instructions2, hasSize(11));
Ray Milkey42507352015-03-20 15:16:10 -070087
88 builder1.deferred()
89 .popVlan()
90 .pushVlan()
91 .setVlanId(VlanId.vlanId((short) 5));
92
93 final List<Instruction> instructions3 = builder1.build().immediate();
94 assertThat(instructions3, hasSize(11));
95 final List<Instruction> instructions4 = builder1.build().deferred();
96 assertThat(instructions4, hasSize(3));
Ray Milkey97f8f102014-11-14 14:18:23 -080097 }
98
99 /**
100 * Tests equals(), hashCode() and toString() methods of
101 * DefaultTrafficTreatment.
102 */
103 @Test
104 public void testEquals() {
Ray Milkey85571eb2015-06-25 09:45:46 -0700105 final IndexedLambda lambda1 = new IndexedLambda(4);
106 final IndexedLambda lambda2 = new IndexedLambda(5);
Ray Milkey97f8f102014-11-14 14:18:23 -0800107 final TrafficTreatment treatment1 =
108 DefaultTrafficTreatment.builder()
Ray Milkey85571eb2015-06-25 09:45:46 -0700109 .add(Instructions.modL0Lambda(lambda1))
110 .build();
Ray Milkey97f8f102014-11-14 14:18:23 -0800111 final TrafficTreatment sameAsTreatment1 =
112 DefaultTrafficTreatment.builder()
Ray Milkey85571eb2015-06-25 09:45:46 -0700113 .add(Instructions.modL0Lambda(lambda1))
Ray Milkey97f8f102014-11-14 14:18:23 -0800114 .build();
115 final TrafficTreatment treatment2 =
116 DefaultTrafficTreatment.builder()
Ray Milkey85571eb2015-06-25 09:45:46 -0700117 .add(Instructions.modL0Lambda(lambda2))
Ray Milkey97f8f102014-11-14 14:18:23 -0800118 .build();
119 new EqualsTester()
120 .addEqualityGroup(treatment1, sameAsTreatment1)
121 .addEqualityGroup(treatment2)
122 .testEquals();
123 }
124}