blob: 21b7a8091823c57edf3519afc4a7e9f3c96c02d6 [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));
78 assertThat(defaultFlowEntry1.packets(), is(1L));
79 assertThat(defaultFlowEntry1.bytes(), is(1L));
80 assertThat(defaultFlowEntry1.state(), is(FlowEntry.FlowEntryState.ADDED));
81 assertThat(defaultFlowEntry1.lastSeen(),
82 greaterThan(System.currentTimeMillis() -
83 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
84 }
85
86 /**
87 * Tests the setters on a default flow entry object.
88 */
89 @Test
90 public void testSetters() {
91 final DefaultFlowEntry entry = makeFlowEntry(1);
92
93 entry.setLastSeen();
94 entry.setState(FlowEntry.FlowEntryState.PENDING_REMOVE);
95 entry.setPackets(11);
96 entry.setBytes(22);
97 entry.setLife(33);
98
99 assertThat(entry.deviceId(), is(did("id1")));
100 assertThat(entry.selector(), is(SELECTOR));
101 assertThat(entry.treatment(), is(TREATMENT));
102 assertThat(entry.timeout(), is(1));
103 assertThat(entry.life(), is(33L));
104 assertThat(entry.packets(), is(11L));
105 assertThat(entry.bytes(), is(22L));
106 assertThat(entry.state(), is(FlowEntry.FlowEntryState.PENDING_REMOVE));
107 assertThat(entry.lastSeen(),
108 greaterThan(System.currentTimeMillis() -
109 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
110 }
111
112 /**
113 * Tests a default flow rule built for an error.
114 */
115 @Test
116 public void testErrorObject() {
117 final DefaultFlowEntry errorEntry =
118 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(1),
119 111,
120 222);
121 assertThat(errorEntry.errType(), is(111));
122 assertThat(errorEntry.errCode(), is(222));
123 assertThat(errorEntry.state(), is(FlowEntry.FlowEntryState.FAILED));
124 assertThat(errorEntry.lastSeen(),
125 greaterThan(System.currentTimeMillis() -
126 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
127 }
128
129 /**
130 * Tests a default flow entry constructed from a flow rule.
131 */
132 @Test
133 public void testFlowBasedObject() {
134 final DefaultFlowEntry entry =
135 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(1));
136 assertThat(entry.priority(), is(1));
137 assertThat(entry.appId(), is((short) 0));
138 assertThat(entry.lastSeen(),
139 greaterThan(System.currentTimeMillis() -
140 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
141 }
142
143 /**
144 * Tests a default flow entry constructed from a flow rule plus extra
145 * parameters.
146 */
147 @Test
148 public void testFlowBasedObjectWithParameters() {
149 final DefaultFlowEntry entry =
150 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(33),
151 FlowEntry.FlowEntryState.REMOVED,
152 101, 102, 103);
153 assertThat(entry.state(), is(FlowEntry.FlowEntryState.REMOVED));
154 assertThat(entry.life(), is(101L));
155 assertThat(entry.packets(), is(102L));
156 assertThat(entry.bytes(), is(103L));
157 assertThat(entry.lastSeen(),
158 greaterThan(System.currentTimeMillis() -
159 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
160 }
161}