blob: e3e449b511cc8d38703e9ec8b0de7bf75ef3fcd0 [file] [log] [blame]
Jian Lia0778172018-07-16 22:50:19 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstacknetworking.impl;
17
18import com.google.common.collect.Lists;
19import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
21import com.google.common.util.concurrent.MoreExecutors;
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.junit.TestUtils;
26import org.onosproject.cluster.ClusterServiceAdapter;
27import org.onosproject.cluster.LeadershipServiceAdapter;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreServiceAdapter;
30import org.onosproject.core.DefaultApplicationId;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.FlowRule;
36import org.onosproject.net.flow.FlowRuleOperation;
37import org.onosproject.net.flow.FlowRuleOperations;
38import org.onosproject.net.flow.FlowRuleServiceAdapter;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41
42import java.util.List;
43import java.util.Map;
44import java.util.Set;
45
46import static org.junit.Assert.assertEquals;
47import static org.onosproject.openstacknetworking.api.Constants.ACL_TABLE;
48import static org.onosproject.openstacknetworking.api.Constants.DHCP_ARP_TABLE;
49import static org.onosproject.openstacknetworking.api.Constants.FLAT_TABLE;
50import static org.onosproject.openstacknetworking.api.Constants.FORWARDING_TABLE;
51import static org.onosproject.openstacknetworking.api.Constants.JUMP_TABLE;
52import static org.onosproject.openstacknetworking.api.Constants.STAT_FLAT_OUTBOUND_TABLE;
53import static org.onosproject.openstacknetworking.api.Constants.STAT_INBOUND_TABLE;
54import static org.onosproject.openstacknetworking.api.Constants.STAT_OUTBOUND_TABLE;
55import static org.onosproject.openstacknetworking.api.Constants.VTAG_TABLE;
56import static org.onosproject.openstacknetworking.api.Constants.VTAP_FLAT_OUTBOUND_TABLE;
57import static org.onosproject.openstacknetworking.api.Constants.VTAP_INBOUND_TABLE;
58import static org.onosproject.openstacknetworking.api.Constants.VTAP_OUTBOUND_TABLE;
59
60/**
61 * Unit tests for flow rule manager.
62 */
63public class OpenstackFlowRuleManagerTest {
64
65 private static final ApplicationId TEST_APP_ID =
66 new DefaultApplicationId(1, "test");
67
68 private static final int DROP_PRIORITY = 0;
69
70 private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:000000000000000a");
71
72 private OpenstackFlowRuleManager target;
73
74 private Set<FlowRuleOperation> fros;
75
76 /**
77 * Initial setup for this unit test.
78 */
79 @Before
80 public void setUp() {
81 target = new OpenstackFlowRuleManager();
82 TestUtils.setField(target, "coreService", new TestCoreService());
83 TestUtils.setField(target, "flowRuleService", new TestFlowRuleService());
84 TestUtils.setField(target, "clusterService", new TestClusterService());
85 TestUtils.setField(target, "leadershipService", new TestLeadershipService());
86 TestUtils.setField(target, "osNodeService", new TestOpenstackNodeService());
87 TestUtils.setField(target, "deviceEventExecutor", MoreExecutors.newDirectExecutorService());
88
89 target.activate();
90 }
91
92 /**
93 * Tears down of this unit test.
94 */
95 @After
96 public void tearDown() {
97 target.deactivate();
98 target = null;
99 }
100
101 /**
102 * Tests whether the set rule method installs the flow rules properly.
103 */
104 @Test
105 public void testSetRule() {
106 int testPriority = 10;
107 int testTableType = 10;
108
109 fros = Sets.newConcurrentHashSet();
110
111 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
112 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
113
114 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder()
115 .forDevice(DEVICE_ID)
116 .withSelector(selectorBuilder.build())
117 .withTreatment(treatmentBuilder.build())
118 .withPriority(testPriority)
119 .fromApp(TEST_APP_ID)
120 .forTable(testTableType)
121 .makePermanent();
122
123 target.setRule(TEST_APP_ID, DEVICE_ID, selectorBuilder.build(),
124 treatmentBuilder.build(), testPriority, testTableType, true);
125 validateFlowRule(flowRuleBuilder.build());
126 }
127
128 /**
129 * Tests whether the connect tables method installs the flow rules properly.
130 */
131 @Test
132 public void testConnectTables() {
133 int testFromTable = 1;
134 int testToTable = 2;
135
136 fros = Sets.newConcurrentHashSet();
137
138 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
139 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
140
141 target.connectTables(DEVICE_ID, testFromTable, testToTable);
142
143 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder()
144 .forDevice(DEVICE_ID)
145 .withSelector(selectorBuilder.build())
146 .withTreatment(treatmentBuilder.transition(testToTable).build())
147 .withPriority(DROP_PRIORITY)
148 .fromApp(TEST_APP_ID)
149 .forTable(testFromTable)
150 .makePermanent();
151
152 validateFlowRule(flowRuleBuilder.build());
153 }
154
155 /**
156 * Tests whether the setup table miss entry method installs the flow rules properly.
157 */
158 @Test
159 public void testSetUpTableMissEntry() {
160 int testTable = 10;
161
162 fros = Sets.newConcurrentHashSet();
163
164 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
165 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
166
167 target.setUpTableMissEntry(DEVICE_ID, testTable);
168
169 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder()
170 .forDevice(DEVICE_ID)
171 .withSelector(selectorBuilder.build())
172 .withTreatment(treatmentBuilder.drop().build())
173 .withPriority(DROP_PRIORITY)
174 .fromApp(TEST_APP_ID)
175 .forTable(testTable)
176 .makePermanent();
177
178 validateFlowRule(flowRuleBuilder.build());
179 }
180
181 /**
182 * Tests whether initialize pipeline method installs the flow rules properly.
183 */
184 @Test
185 public void testInitializePipeline() {
186
187 fros = Sets.newConcurrentHashSet();
188
189 target.initializePipeline(DEVICE_ID);
190 assertEquals("Flow Rule size was not match", 11, fros.size());
191
192 Map<Integer, Integer> fromToTableMap = Maps.newConcurrentMap();
193 fromToTableMap.put(STAT_INBOUND_TABLE, VTAP_INBOUND_TABLE);
194 fromToTableMap.put(VTAP_INBOUND_TABLE, DHCP_ARP_TABLE);
195 fromToTableMap.put(DHCP_ARP_TABLE, VTAG_TABLE);
196 fromToTableMap.put(VTAG_TABLE, ACL_TABLE);
197 fromToTableMap.put(ACL_TABLE, JUMP_TABLE);
198 fromToTableMap.put(STAT_OUTBOUND_TABLE, VTAP_OUTBOUND_TABLE);
199 fromToTableMap.put(VTAP_OUTBOUND_TABLE, FORWARDING_TABLE);
200 fromToTableMap.put(STAT_FLAT_OUTBOUND_TABLE, VTAP_FLAT_OUTBOUND_TABLE);
201 fromToTableMap.put(VTAP_FLAT_OUTBOUND_TABLE, FLAT_TABLE);
202
203 fros.stream().map(FlowRuleOperation::rule).forEach(fr -> {
204 if (fr.tableId() != JUMP_TABLE) {
205 assertEquals("To Table did not match,",
206 fromToTableMap.get(fr.tableId()),
207 fr.treatment().tableTransition().tableId());
208 }
209 });
210 }
211
212 private void validateFlowRule(FlowRule ref) {
213 assertEquals("Flow Rule size was not match", 1, fros.size());
214 List<FlowRuleOperation> froList = Lists.newArrayList();
215 froList.addAll(fros);
216 FlowRuleOperation fro = froList.get(0);
217 FlowRule fr = fro.rule();
218
219 assertEquals("Application ID did not match", ref.appId(), fr.appId());
220 assertEquals("Device ID did not match", ref.deviceId(), fr.deviceId());
221 assertEquals("Selector did not match", ref.selector(), fr.selector());
222 assertEquals("Treatment did not match", ref.treatment(), fr.treatment());
223 assertEquals("Priority did not match", ref.priority(), fr.priority());
224 assertEquals("Table ID did not match", ref.table(), fr.table());
225 assertEquals("Permanent did not match", ref.isPermanent(), fr.isPermanent());
226 }
227
228 private class TestOpenstackNodeService extends OpenstackNodeServiceAdapter {
229 }
230
231 private class TestFlowRuleService extends FlowRuleServiceAdapter {
232 @Override
233 public void apply(FlowRuleOperations ops) {
234 fros.addAll(ops.stages().get(0));
235 }
236 }
237
238 private class TestCoreService extends CoreServiceAdapter {
239
240 @Override
241 public ApplicationId registerApplication(String name) {
242 return TEST_APP_ID;
243 }
244 }
245
246 private class TestClusterService extends ClusterServiceAdapter {
247 }
248
249 private class TestLeadershipService extends LeadershipServiceAdapter {
250 }
251}