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