Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.incubator.net.virtual.impl.provider; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 25 | import org.onosproject.core.CoreServiceAdapter; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 26 | import org.onosproject.core.DefaultApplicationId; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 27 | import org.onosproject.incubator.net.virtual.DefaultVirtualDevice; |
| 28 | import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork; |
| 29 | import org.onosproject.incubator.net.virtual.DefaultVirtualPort; |
| 30 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 31 | import org.onosproject.incubator.net.virtual.TenantId; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 33 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 35 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminServiceAdapter; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 36 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 37 | import org.onosproject.net.ConnectPoint; |
| 38 | import org.onosproject.net.DefaultAnnotations; |
| 39 | import org.onosproject.net.DefaultDevice; |
| 40 | import org.onosproject.net.DefaultLink; |
| 41 | import org.onosproject.net.DefaultPath; |
| 42 | import org.onosproject.net.DefaultPort; |
| 43 | import org.onosproject.net.Device; |
| 44 | import org.onosproject.net.DeviceId; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 45 | import org.onosproject.net.Link; |
| 46 | import org.onosproject.net.Path; |
| 47 | import org.onosproject.net.Port; |
| 48 | import org.onosproject.net.PortNumber; |
| 49 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 50 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 51 | import org.onosproject.net.flow.DefaultFlowRule; |
| 52 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 53 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 54 | import org.onosproject.net.flow.FlowEntry; |
| 55 | import org.onosproject.net.flow.FlowRule; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 56 | import org.onosproject.net.flow.FlowRuleServiceAdapter; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 57 | import org.onosproject.net.flow.TrafficSelector; |
| 58 | import org.onosproject.net.flow.TrafficTreatment; |
| 59 | import org.onosproject.net.flow.instructions.Instruction; |
| 60 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 61 | import org.onosproject.net.provider.ProviderId; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 62 | import org.onosproject.net.topology.LinkWeigher; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 63 | import org.onosproject.net.topology.LinkWeight; |
| 64 | import org.onosproject.net.topology.Topology; |
Yuta HIGUCHI | 03c234e | 2017-01-23 17:21:34 -0800 | [diff] [blame] | 65 | import org.onosproject.net.topology.TopologyServiceAdapter; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 66 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 67 | import java.util.Arrays; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 68 | import java.util.HashSet; |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 69 | import java.util.Set; |
| 70 | import java.util.stream.Collectors; |
| 71 | |
| 72 | import static org.junit.Assert.assertEquals; |
| 73 | |
| 74 | public class DefaultVirtualFlowRuleProviderTest { |
| 75 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 76 | |
| 77 | private static final DeviceId DID1 = DeviceId.deviceId("of:001"); |
| 78 | private static final DeviceId DID2 = DeviceId.deviceId("of:002"); |
| 79 | private static final PortNumber PORT_NUM1 = PortNumber.portNumber(1); |
| 80 | private static final PortNumber PORT_NUM2 = PortNumber.portNumber(2); |
| 81 | |
| 82 | private static final DefaultAnnotations ANNOTATIONS = |
| 83 | DefaultAnnotations.builder().set("foo", "bar").build(); |
| 84 | |
| 85 | private static final Device DEV1 = |
| 86 | new DefaultDevice(PID, DID1, Device.Type.SWITCH, "", "", "", "", null); |
| 87 | private static final Device DEV2 = |
| 88 | new DefaultDevice(PID, DID2, Device.Type.SWITCH, "", "", "", "", null); |
| 89 | private static final Port PORT11 = |
| 90 | new DefaultPort(DEV1, PORT_NUM1, true, ANNOTATIONS); |
| 91 | private static final Port PORT12 = |
| 92 | new DefaultPort(DEV1, PORT_NUM2, true, ANNOTATIONS); |
| 93 | private static final Port PORT21 = |
| 94 | new DefaultPort(DEV2, PORT_NUM1, true, ANNOTATIONS); |
| 95 | private static final Port PORT22 = |
| 96 | new DefaultPort(DEV2, PORT_NUM2, true, ANNOTATIONS); |
| 97 | |
| 98 | private static final ConnectPoint CP11 = new ConnectPoint(DID1, PORT_NUM1); |
| 99 | private static final ConnectPoint CP12 = new ConnectPoint(DID1, PORT_NUM2); |
| 100 | private static final ConnectPoint CP21 = new ConnectPoint(DID2, PORT_NUM1); |
| 101 | private static final ConnectPoint CP22 = new ConnectPoint(DID2, PORT_NUM2); |
| 102 | private static final Link LINK1 = DefaultLink.builder() |
| 103 | .src(CP12).dst(CP21).providerId(PID).type(Link.Type.DIRECT).build(); |
| 104 | |
| 105 | private static final NetworkId VNET_ID = NetworkId.networkId(1); |
| 106 | private static final DeviceId VDID = DeviceId.deviceId("of:100"); |
| 107 | |
| 108 | private static final VirtualNetwork VNET = new DefaultVirtualNetwork( |
| 109 | VNET_ID, TenantId.tenantId("t1")); |
| 110 | private static final VirtualDevice VDEV = |
| 111 | new DefaultVirtualDevice(VNET_ID, VDID); |
| 112 | private static final VirtualPort VPORT1 = |
| 113 | new DefaultVirtualPort(VNET_ID, VDEV, PORT_NUM1, CP11); |
| 114 | private static final VirtualPort VPORT2 = |
| 115 | new DefaultVirtualPort(VNET_ID, VDEV, PORT_NUM2, CP22); |
| 116 | |
| 117 | private static final int TIMEOUT = 10; |
| 118 | |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 119 | protected DefaultVirtualFlowRuleProvider virtualProvider; |
| 120 | |
| 121 | private ApplicationId vAppId; |
| 122 | |
| 123 | @Before |
| 124 | public void setUp() { |
| 125 | virtualProvider = new DefaultVirtualFlowRuleProvider(); |
| 126 | |
| 127 | virtualProvider.deviceService = new TestDeviceService(); |
| 128 | virtualProvider.coreService = new TestCoreService(); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 129 | virtualProvider.vnService = |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 130 | new TestVirtualNetworkAdminService(); |
| 131 | virtualProvider.topologyService = new TestTopologyService(); |
| 132 | virtualProvider.flowRuleService = new TestFlowRuleService(); |
| 133 | virtualProvider.providerRegistryService = new VirtualProviderManager(); |
| 134 | |
| 135 | virtualProvider.activate(); |
| 136 | vAppId = new TestApplicationId(0, "Virtual App"); |
| 137 | } |
| 138 | |
| 139 | @After |
| 140 | public void tearDown() { |
| 141 | virtualProvider.deactivate(); |
| 142 | virtualProvider.deviceService = null; |
| 143 | virtualProvider.coreService = null; |
| 144 | } |
| 145 | |
| 146 | @Test |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 147 | public void devirtualizeFlowRuleWithInPort() { |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 148 | TrafficSelector ts = DefaultTrafficSelector.builder() |
| 149 | .matchInPort(PORT_NUM1).build(); |
| 150 | TrafficTreatment tr = DefaultTrafficTreatment.builder() |
| 151 | .setOutput(PORT_NUM2).build(); |
| 152 | |
| 153 | FlowRule r1 = DefaultFlowRule.builder() |
| 154 | .forDevice(VDID) |
| 155 | .withSelector(ts) |
| 156 | .withTreatment(tr) |
| 157 | .withPriority(10) |
| 158 | .fromApp(vAppId) |
| 159 | .makeTemporary(TIMEOUT) |
| 160 | .build(); |
| 161 | |
| 162 | virtualProvider.applyFlowRule(VNET_ID, r1); |
| 163 | |
| 164 | assertEquals("2 rules should exist", 2, |
| 165 | virtualProvider.flowRuleService.getFlowRuleCount()); |
| 166 | |
| 167 | Set<FlowEntry> phyRules = new HashSet<>(); |
| 168 | for (FlowEntry i : virtualProvider.flowRuleService.getFlowEntries(DID1)) { |
| 169 | phyRules.add(i); |
| 170 | } |
| 171 | for (FlowEntry i : virtualProvider.flowRuleService.getFlowEntries(DID2)) { |
| 172 | phyRules.add(i); |
| 173 | } |
| 174 | |
| 175 | FlowRule in = null; |
| 176 | FlowRule out = null; |
| 177 | |
| 178 | for (FlowRule rule : phyRules) { |
| 179 | |
| 180 | L2ModificationInstruction i = (L2ModificationInstruction) |
| 181 | rule.treatment().allInstructions().get(0); |
| 182 | |
| 183 | if (i.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) { |
| 184 | in = rule; |
| 185 | } else { |
| 186 | out = rule; |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | |
| 191 | assertEquals(DID1, in.deviceId()); |
| 192 | assertEquals(DID2, out.deviceId()); |
| 193 | } |
| 194 | |
| 195 | @Test |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 196 | public void devirtualizeFlowRuleWithoutInPort() { |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 197 | TrafficSelector ts = DefaultTrafficSelector.builder().build(); |
| 198 | TrafficTreatment tr = DefaultTrafficTreatment.builder() |
| 199 | .setOutput(PORT_NUM2).build(); |
| 200 | |
| 201 | FlowRule r1 = DefaultFlowRule.builder() |
| 202 | .forDevice(VDID) |
| 203 | .withSelector(ts) |
| 204 | .withTreatment(tr) |
| 205 | .withPriority(10) |
| 206 | .fromApp(vAppId) |
| 207 | .makeTemporary(TIMEOUT) |
| 208 | .build(); |
| 209 | |
| 210 | virtualProvider.applyFlowRule(VNET_ID, r1); |
| 211 | |
| 212 | assertEquals("3 rules should exist", 3, |
| 213 | virtualProvider.flowRuleService.getFlowRuleCount()); |
| 214 | |
| 215 | FlowRule inFromDID1 = null; |
| 216 | FlowRule inFromDID2 = null; |
| 217 | FlowRule out = null; |
| 218 | |
| 219 | Set<FlowEntry> phyRules = new HashSet<>(); |
| 220 | for (FlowEntry i : virtualProvider.flowRuleService.getFlowEntries(DID1)) { |
| 221 | phyRules.add(i); |
| 222 | } |
| 223 | for (FlowEntry i : virtualProvider.flowRuleService.getFlowEntries(DID2)) { |
| 224 | phyRules.add(i); |
| 225 | } |
| 226 | |
| 227 | for (FlowRule rule : phyRules) { |
| 228 | for (Instruction inst : rule.treatment().allInstructions()) { |
| 229 | if (inst.type() == Instruction.Type.L2MODIFICATION) { |
| 230 | L2ModificationInstruction i = (L2ModificationInstruction) inst; |
| 231 | if (i.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) { |
| 232 | inFromDID1 = rule; |
| 233 | break; |
| 234 | } else { |
| 235 | out = rule; |
| 236 | break; |
| 237 | } |
| 238 | } else { |
| 239 | inFromDID2 = rule; |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | assertEquals(DID1, inFromDID1.deviceId()); |
| 246 | assertEquals(DID2, inFromDID2.deviceId()); |
| 247 | assertEquals(DID2, out.deviceId()); |
| 248 | } |
| 249 | |
| 250 | @Test |
| 251 | public void removeVirtualizeFlowRule() { |
| 252 | TrafficSelector ts = DefaultTrafficSelector.builder().build(); |
| 253 | TrafficTreatment tr = DefaultTrafficTreatment.builder() |
| 254 | .setOutput(PORT_NUM2).build(); |
| 255 | |
| 256 | FlowRule r1 = DefaultFlowRule.builder() |
| 257 | .forDevice(VDID) |
| 258 | .withSelector(ts) |
| 259 | .withTreatment(tr) |
| 260 | .withPriority(10) |
| 261 | .fromApp(vAppId) |
| 262 | .makeTemporary(TIMEOUT) |
| 263 | .build(); |
| 264 | |
| 265 | virtualProvider.removeFlowRule(VNET_ID, r1); |
| 266 | |
| 267 | assertEquals("0 rules should exist", 0, |
| 268 | virtualProvider.flowRuleService.getFlowRuleCount()); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | private static class TestDeviceService extends DeviceServiceAdapter { |
| 273 | @Override |
| 274 | public int getDeviceCount() { |
| 275 | return 2; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public Iterable<Device> getDevices() { |
| 280 | return ImmutableList.of(DEV1, DEV2); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public Iterable<Device> getAvailableDevices() { |
| 285 | return getDevices(); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public Device getDevice(DeviceId deviceId) { |
| 290 | return deviceId.equals(DID2) ? DEV2 : DEV1; |
| 291 | } |
| 292 | } |
| 293 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 294 | private static class TestCoreService extends CoreServiceAdapter { |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 295 | |
| 296 | @Override |
| 297 | public ApplicationId registerApplication(String name) { |
| 298 | return new TestApplicationId(1, name); |
| 299 | } |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | private static class TestApplicationId extends DefaultApplicationId { |
| 303 | public TestApplicationId(int id, String name) { |
| 304 | super(id, name); |
| 305 | } |
| 306 | } |
| 307 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 308 | private class TestVirtualNetworkAdminService |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 309 | extends VirtualNetworkAdminServiceAdapter { |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 310 | |
| 311 | @Override |
| 312 | public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) { |
| 313 | return ImmutableSet.of(VDEV); |
| 314 | } |
| 315 | |
| 316 | @Override |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 317 | public Set<VirtualLink> getVirtualLinks(NetworkId networkId) { |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 318 | return new HashSet<>(); |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public Set<VirtualPort> getVirtualPorts(NetworkId networkId, |
| 323 | DeviceId deviceId) { |
| 324 | return ImmutableSet.of(VPORT1, VPORT2); |
| 325 | } |
| 326 | |
| 327 | @Override |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 328 | public ApplicationId getVirtualNetworkApplicationId(NetworkId networkId) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 329 | return vAppId; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 330 | } |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Yuta HIGUCHI | 03c234e | 2017-01-23 17:21:34 -0800 | [diff] [blame] | 333 | private static class TestTopologyService extends TopologyServiceAdapter { |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 334 | |
| 335 | @Override |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 336 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 337 | DefaultPath path = new DefaultPath(PID, ImmutableList.of(LINK1), |
| 338 | 100, ANNOTATIONS); |
| 339 | return ImmutableSet.of(path); |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public Set<Path> getPaths(Topology topology, DeviceId src, |
| 344 | DeviceId dst, LinkWeight weight) { |
| 345 | DefaultPath path = new DefaultPath(PID, ImmutableList.of(LINK1), |
| 346 | 100, ANNOTATIONS); |
| 347 | return ImmutableSet.of(path); |
| 348 | } |
| 349 | |
| 350 | @Override |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 351 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, |
| 352 | LinkWeigher weigher) { |
| 353 | DefaultPath path = new DefaultPath(PID, ImmutableList.of(LINK1), |
| 354 | 100, ANNOTATIONS); |
| 355 | return ImmutableSet.of(path); |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 358 | } |
| 359 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 360 | private static class TestFlowRuleService extends FlowRuleServiceAdapter { |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 361 | static Set<FlowRule> ruleCollection = new HashSet<>(); |
| 362 | |
| 363 | @Override |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 364 | public int getFlowRuleCount() { |
| 365 | return ruleCollection.size(); |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 370 | return ruleCollection.stream() |
| 371 | .filter(r -> r.deviceId().equals(deviceId)) |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 372 | .map(DefaultFlowEntry::new) |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 373 | .collect(Collectors.toSet()); |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public void applyFlowRules(FlowRule... flowRules) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 378 | ruleCollection.addAll(Arrays.asList(flowRules)); |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 382 | public void removeFlowRules(FlowRule... flowRules) { |
| 383 | Set<FlowRule> candidates = new HashSet<>(); |
| 384 | for (FlowRule rule : flowRules) { |
| 385 | ruleCollection.stream() |
| 386 | .filter(r -> r.exactMatch(rule)) |
| 387 | .forEach(candidates::add); |
| 388 | } |
| 389 | ruleCollection.removeAll(candidates); |
| 390 | } |
Yoonseon Han | c70b4e0 | 2016-10-20 15:24:33 -0700 | [diff] [blame] | 391 | } |
| 392 | } |