blob: e0cd135b6abce0b1127c3043ed53402a8c7bc675 [file] [log] [blame]
Ray Milkey930fc662014-11-11 16:07:45 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Ray Milkey930fc662014-11-11 16:07:45 -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 Milkey930fc662014-11-11 16:07:45 -080017
18import java.util.concurrent.TimeUnit;
19
20import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.intent.IntentTestsMocks;
Ray Milkey930fc662014-11-11 16:07:45 -080022
23import com.google.common.testing.EqualsTester;
24
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.greaterThan;
27import static org.hamcrest.Matchers.is;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import static org.onosproject.net.NetTestTools.did;
Ray Milkey930fc662014-11-11 16:07:45 -080029
30/**
31 * Unit tests for the DefaultFlowEntry class.
32 */
33public class DefaultFlowEntryTest {
34 private static final IntentTestsMocks.MockSelector SELECTOR =
35 new IntentTestsMocks.MockSelector();
36 private static final IntentTestsMocks.MockTreatment TREATMENT =
37 new IntentTestsMocks.MockTreatment();
38
39 private static DefaultFlowEntry makeFlowEntry(int uniqueValue) {
Ray Milkeyd13a37b2015-06-12 11:55:17 -070040 FlowRule rule = DefaultFlowRule.builder()
41 .forDevice(did("id" + Integer.toString(uniqueValue)))
42 .withSelector(SELECTOR)
43 .withTreatment(TREATMENT)
44 .withPriority(uniqueValue)
45 .withCookie(uniqueValue)
46 .makeTemporary(uniqueValue)
47 .build();
48
49 return new DefaultFlowEntry(rule, FlowEntry.FlowEntryState.ADDED,
50 uniqueValue, uniqueValue, uniqueValue);
Ray Milkey930fc662014-11-11 16:07:45 -080051 }
52
53 final DefaultFlowEntry defaultFlowEntry1 = makeFlowEntry(1);
54 final DefaultFlowEntry sameAsDefaultFlowEntry1 = makeFlowEntry(1);
55 final DefaultFlowEntry defaultFlowEntry2 = makeFlowEntry(2);
56
57 /**
58 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
59 */
60 @Test
61 public void testEquals() {
62 new EqualsTester()
63 .addEqualityGroup(defaultFlowEntry1, sameAsDefaultFlowEntry1)
64 .addEqualityGroup(defaultFlowEntry2)
65 .testEquals();
66 }
67
68 /**
69 * Tests the construction of a default flow entry from a device id.
70 */
71 @Test
72 public void testDeviceBasedObject() {
73 assertThat(defaultFlowEntry1.deviceId(), is(did("id1")));
74 assertThat(defaultFlowEntry1.selector(), is(SELECTOR));
75 assertThat(defaultFlowEntry1.treatment(), is(TREATMENT));
76 assertThat(defaultFlowEntry1.timeout(), is(1));
77 assertThat(defaultFlowEntry1.life(), is(1L));
Thiago Santos877914d2016-07-20 18:29:29 -030078 assertThat(defaultFlowEntry1.life(TimeUnit.SECONDS), is(1L));
79 assertThat(defaultFlowEntry1.life(TimeUnit.MILLISECONDS), is(1000L));
80 assertThat(defaultFlowEntry1.life(TimeUnit.MINUTES), is(0L));
Ray Milkey930fc662014-11-11 16:07:45 -080081 assertThat(defaultFlowEntry1.packets(), is(1L));
82 assertThat(defaultFlowEntry1.bytes(), is(1L));
83 assertThat(defaultFlowEntry1.state(), is(FlowEntry.FlowEntryState.ADDED));
84 assertThat(defaultFlowEntry1.lastSeen(),
85 greaterThan(System.currentTimeMillis() -
86 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
87 }
88
89 /**
90 * Tests the setters on a default flow entry object.
91 */
92 @Test
93 public void testSetters() {
94 final DefaultFlowEntry entry = makeFlowEntry(1);
95
96 entry.setLastSeen();
97 entry.setState(FlowEntry.FlowEntryState.PENDING_REMOVE);
98 entry.setPackets(11);
99 entry.setBytes(22);
Thiago Santos877914d2016-07-20 18:29:29 -0300100 entry.setLife(33333, TimeUnit.MILLISECONDS);
Ray Milkey930fc662014-11-11 16:07:45 -0800101
102 assertThat(entry.deviceId(), is(did("id1")));
103 assertThat(entry.selector(), is(SELECTOR));
104 assertThat(entry.treatment(), is(TREATMENT));
105 assertThat(entry.timeout(), is(1));
106 assertThat(entry.life(), is(33L));
Thiago Santos877914d2016-07-20 18:29:29 -0300107 assertThat(entry.life(TimeUnit.MILLISECONDS), is(33333L));
Ray Milkey930fc662014-11-11 16:07:45 -0800108 assertThat(entry.packets(), is(11L));
109 assertThat(entry.bytes(), is(22L));
110 assertThat(entry.state(), is(FlowEntry.FlowEntryState.PENDING_REMOVE));
111 assertThat(entry.lastSeen(),
112 greaterThan(System.currentTimeMillis() -
113 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
114 }
115
116 /**
117 * Tests a default flow rule built for an error.
118 */
119 @Test
120 public void testErrorObject() {
121 final DefaultFlowEntry errorEntry =
122 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(1),
123 111,
124 222);
125 assertThat(errorEntry.errType(), is(111));
126 assertThat(errorEntry.errCode(), is(222));
127 assertThat(errorEntry.state(), is(FlowEntry.FlowEntryState.FAILED));
128 assertThat(errorEntry.lastSeen(),
129 greaterThan(System.currentTimeMillis() -
130 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
131 }
132
133 /**
134 * Tests a default flow entry constructed from a flow rule.
135 */
136 @Test
137 public void testFlowBasedObject() {
138 final DefaultFlowEntry entry =
139 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(1));
140 assertThat(entry.priority(), is(1));
141 assertThat(entry.appId(), is((short) 0));
142 assertThat(entry.lastSeen(),
143 greaterThan(System.currentTimeMillis() -
144 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
145 }
146
147 /**
148 * Tests a default flow entry constructed from a flow rule plus extra
149 * parameters.
150 */
151 @Test
152 public void testFlowBasedObjectWithParameters() {
153 final DefaultFlowEntry entry =
154 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(33),
155 FlowEntry.FlowEntryState.REMOVED,
156 101, 102, 103);
157 assertThat(entry.state(), is(FlowEntry.FlowEntryState.REMOVED));
158 assertThat(entry.life(), is(101L));
159 assertThat(entry.packets(), is(102L));
160 assertThat(entry.bytes(), is(103L));
161 assertThat(entry.lastSeen(),
162 greaterThan(System.currentTimeMillis() -
163 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
164 }
165}