yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -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.ImmutableSet; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
| 30 | import org.onosproject.core.DefaultApplicationId; |
| 31 | import org.onosproject.incubator.net.virtual.DefaultVirtualDevice; |
| 32 | import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork; |
| 33 | import org.onosproject.incubator.net.virtual.DefaultVirtualPort; |
| 34 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 35 | import org.onosproject.incubator.net.virtual.TenantId; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 37 | import org.onosproject.incubator.net.virtual.VirtualHost; |
| 38 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 39 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 40 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
Yoonseon Han | 33821fb | 2017-02-24 13:40:44 +0900 | [diff] [blame] | 41 | import org.onosproject.incubator.net.virtual.VirtualNetworkListener; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 42 | import org.onosproject.incubator.net.virtual.VirtualPort; |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 43 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProviderService; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 44 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProvider; |
| 45 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProviderService; |
| 46 | import org.onosproject.net.ConnectPoint; |
| 47 | import org.onosproject.net.DefaultAnnotations; |
| 48 | import org.onosproject.net.DefaultDevice; |
| 49 | import org.onosproject.net.DefaultLink; |
| 50 | import org.onosproject.net.DefaultPort; |
| 51 | import org.onosproject.net.Device; |
| 52 | import org.onosproject.net.DeviceId; |
| 53 | import org.onosproject.net.HostId; |
| 54 | import org.onosproject.net.HostLocation; |
| 55 | import org.onosproject.net.Link; |
| 56 | import org.onosproject.net.Port; |
| 57 | import org.onosproject.net.PortNumber; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 58 | import org.onosproject.net.flow.DefaultTrafficSelector; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 59 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 60 | import org.onosproject.net.flow.TrafficSelector; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flow.TrafficTreatment; |
| 62 | import org.onosproject.net.flow.instructions.Instruction; |
| 63 | import org.onosproject.net.flow.instructions.Instructions; |
| 64 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 65 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 66 | import org.onosproject.net.packet.DefaultPacketContext; |
| 67 | import org.onosproject.net.packet.InboundPacket; |
| 68 | import org.onosproject.net.packet.OutboundPacket; |
| 69 | import org.onosproject.net.packet.PacketContext; |
| 70 | import org.onosproject.net.packet.PacketProcessor; |
| 71 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 72 | import org.onosproject.net.provider.ProviderId; |
| 73 | |
| 74 | import java.nio.ByteBuffer; |
| 75 | import java.util.LinkedList; |
| 76 | import java.util.List; |
| 77 | import java.util.Set; |
| 78 | |
| 79 | import static org.junit.Assert.assertEquals; |
| 80 | |
| 81 | public class DefaultVirtualPacketProviderTest { |
| 82 | private static final String SRC_MAC_ADDR = "00:00:00:00:00:00"; |
| 83 | private static final String DST_MAC_ADDR = "00:00:00:00:00:01"; |
| 84 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 85 | |
| 86 | private static final DeviceId DID1 = DeviceId.deviceId("of:001"); |
| 87 | private static final DeviceId DID2 = DeviceId.deviceId("of:002"); |
| 88 | private static final PortNumber PORT_NUM1 = PortNumber.portNumber(1); |
| 89 | private static final PortNumber PORT_NUM2 = PortNumber.portNumber(2); |
| 90 | private static final PortNumber PORT_NUM3 = PortNumber.portNumber(3); |
| 91 | private static final PortNumber PORT_NUM4 = PortNumber.portNumber(4); |
| 92 | |
| 93 | private static final DefaultAnnotations ANNOTATIONS = |
| 94 | DefaultAnnotations.builder().set("foo", "bar").build(); |
| 95 | |
| 96 | private static final Device DEV1 = |
| 97 | new DefaultDevice(PID, DID1, Device.Type.SWITCH, "", "", "", "", null); |
| 98 | private static final Device DEV2 = |
| 99 | new DefaultDevice(PID, DID2, Device.Type.SWITCH, "", "", "", "", null); |
| 100 | private static final Port PORT11 = |
| 101 | new DefaultPort(DEV1, PORT_NUM1, true, ANNOTATIONS); |
| 102 | private static final Port PORT12 = |
| 103 | new DefaultPort(DEV1, PORT_NUM2, true, ANNOTATIONS); |
| 104 | private static final Port PORT21 = |
| 105 | new DefaultPort(DEV2, PORT_NUM3, true, ANNOTATIONS); |
| 106 | private static final Port PORT22 = |
| 107 | new DefaultPort(DEV2, PORT_NUM4, true, ANNOTATIONS); |
| 108 | |
| 109 | private static final ConnectPoint CP11 = new ConnectPoint(DID1, PORT_NUM1); |
| 110 | private static final ConnectPoint CP12 = new ConnectPoint(DID1, PORT_NUM2); |
| 111 | private static final ConnectPoint CP21 = new ConnectPoint(DID2, PORT_NUM3); |
| 112 | private static final ConnectPoint CP22 = new ConnectPoint(DID2, PORT_NUM4); |
| 113 | private static final Link LINK1 = DefaultLink.builder() |
| 114 | .src(CP12).dst(CP21).providerId(PID).type(Link.Type.DIRECT).build(); |
| 115 | |
| 116 | private static final TenantId TENANT_ID = TenantId.tenantId("1"); |
| 117 | private static final NetworkId VNET_ID = NetworkId.networkId(1); |
| 118 | private static final DeviceId VDID = DeviceId.deviceId("of:100"); |
| 119 | |
| 120 | private static final PortNumber VPORT_NUM1 = PortNumber.portNumber(10); |
| 121 | private static final PortNumber VPORT_NUM2 = PortNumber.portNumber(11); |
| 122 | |
| 123 | private static final VirtualNetwork VNET = new DefaultVirtualNetwork( |
| 124 | VNET_ID, TenantId.tenantId("t1")); |
| 125 | private static final VirtualDevice VDEV = |
| 126 | new DefaultVirtualDevice(VNET_ID, VDID); |
| 127 | private static final VirtualPort VPORT1 = |
| 128 | new DefaultVirtualPort(VNET_ID, VDEV, VPORT_NUM1, CP11); |
| 129 | private static final VirtualPort VPORT2 = |
| 130 | new DefaultVirtualPort(VNET_ID, VDEV, VPORT_NUM2, CP22); |
| 131 | private static final ConnectPoint VCP11 = new ConnectPoint(VDID, VPORT_NUM1); |
| 132 | private static final ConnectPoint VCP12 = new ConnectPoint(VDID, VPORT_NUM2); |
| 133 | |
| 134 | protected DefaultVirtualPacketProvider virtualProvider; |
| 135 | protected TestPacketService testPacketService; |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 136 | protected TestVirtualPacketProviderService providerService; |
| 137 | |
| 138 | private VirtualProviderManager providerManager; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 139 | |
| 140 | private ApplicationId vAppId; |
| 141 | |
| 142 | @Before |
| 143 | public void setUp() { |
| 144 | virtualProvider = new DefaultVirtualPacketProvider(); |
| 145 | |
| 146 | virtualProvider.coreService = new CoreServiceAdapter(); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 147 | virtualProvider.vnaService = |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 148 | new TestVirtualNetworkAdminService(); |
| 149 | |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 150 | providerService = new TestVirtualPacketProviderService(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 151 | |
| 152 | testPacketService = new TestPacketService(); |
| 153 | virtualProvider.packetService = testPacketService; |
| 154 | |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 155 | providerManager = new VirtualProviderManager(); |
| 156 | virtualProvider.providerRegistryService = providerManager; |
| 157 | providerManager.registerProviderService(VNET_ID, providerService); |
| 158 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 159 | virtualProvider.activate(); |
| 160 | vAppId = new TestApplicationId(0, "Virtual App"); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 161 | |
| 162 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 163 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 164 | |
| 165 | virtualProvider.startPacketHandling(VNET_ID); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @After |
| 169 | public void tearDown() { |
| 170 | virtualProvider.deactivate(); |
| 171 | virtualProvider.coreService = null; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 172 | virtualProvider.vnaService = null; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
| 176 | /** Test the virtual outbound packet is delivered to a proper (physical) |
| 177 | * device. |
| 178 | */ |
| 179 | @Test |
| 180 | public void devirtualizePacket() { |
| 181 | TrafficTreatment tr = DefaultTrafficTreatment.builder() |
| 182 | .setOutput(VPORT_NUM1).build(); |
| 183 | ByteBuffer data = ByteBuffer.wrap("abc".getBytes()); |
| 184 | |
| 185 | OutboundPacket vOutPacket = new DefaultOutboundPacket(VDID, tr, data); |
| 186 | |
| 187 | virtualProvider.emit(VNET_ID, vOutPacket); |
| 188 | |
| 189 | assertEquals("The count should be 1", 1, |
| 190 | testPacketService.getRequestedPacketCount()); |
| 191 | |
| 192 | OutboundPacket pOutPacket = testPacketService.getRequestedPacket(0); |
| 193 | |
| 194 | assertEquals("The packet should be requested on DEV1", DID1, |
| 195 | pOutPacket.sendThrough()); |
| 196 | |
| 197 | PortNumber outPort = pOutPacket.treatment() |
| 198 | .allInstructions() |
| 199 | .stream() |
| 200 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 201 | .map(i -> (Instructions.OutputInstruction) i) |
| 202 | .map(i -> i.port()) |
| 203 | .findFirst().get(); |
| 204 | assertEquals("The packet should be out at PORT1 of DEV1", PORT_NUM1, |
| 205 | outPort); |
| 206 | } |
| 207 | |
| 208 | /** Test the physical packet context is delivered to a proper (physical) |
| 209 | * virtual network and device. |
| 210 | */ |
| 211 | @Test |
| 212 | public void virtualizePacket() { |
| 213 | Ethernet eth = new Ethernet(); |
| 214 | eth.setSourceMACAddress(SRC_MAC_ADDR); |
| 215 | eth.setDestinationMACAddress(DST_MAC_ADDR); |
| 216 | eth.setVlanID((short) 1); |
| 217 | eth.setPayload(null); |
| 218 | |
| 219 | InboundPacket pInPacket = |
| 220 | new DefaultInboundPacket(CP22, eth, |
| 221 | ByteBuffer.wrap(eth.serialize())); |
| 222 | |
| 223 | PacketContext pContext = |
| 224 | new TestPacketContext(System.nanoTime(), pInPacket, null, false); |
| 225 | |
| 226 | testPacketService.sendTestPacketContext(pContext); |
| 227 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 228 | PacketContext vContext = providerService.getRequestedPacketContext(0); |
| 229 | InboundPacket vInPacket = vContext.inPacket(); |
| 230 | |
| 231 | assertEquals("the packet should be received from VCP12", |
| 232 | VCP12, vInPacket.receivedFrom()); |
| 233 | |
| 234 | assertEquals("VLAN tag should be excludede", VlanId.UNTAGGED, |
| 235 | vInPacket.parsed().getVlanID()); |
| 236 | } |
| 237 | |
| 238 | private class TestPacketContext extends DefaultPacketContext { |
| 239 | |
| 240 | /** |
| 241 | * Creates a new packet context. |
| 242 | * |
| 243 | * @param time creation time |
| 244 | * @param inPkt inbound packet |
| 245 | * @param outPkt outbound packet |
| 246 | * @param block whether the context is blocked or not |
| 247 | */ |
| 248 | protected TestPacketContext(long time, InboundPacket inPkt, |
| 249 | OutboundPacket outPkt, boolean block) { |
| 250 | super(time, inPkt, outPkt, block); |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void send() { |
| 255 | |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private static class TestApplicationId extends DefaultApplicationId { |
| 260 | public TestApplicationId(int id, String name) { |
| 261 | super(id, name); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private static class TestVirtualNetworkAdminService |
| 266 | implements VirtualNetworkAdminService { |
| 267 | |
| 268 | @Override |
| 269 | public Set<VirtualNetwork> getVirtualNetworks(TenantId tenantId) { |
| 270 | return ImmutableSet.of(VNET); |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) { |
| 275 | return ImmutableSet.of(VDEV); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public Set<VirtualHost> getVirtualHosts(NetworkId networkId) { |
| 280 | return null; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public Set<VirtualLink> getVirtualLinks(NetworkId networkId) { |
| 285 | return null; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public Set<VirtualPort> getVirtualPorts(NetworkId networkId, |
| 290 | DeviceId deviceId) { |
| 291 | return ImmutableSet.of(VPORT1, VPORT2); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public <T> T get(NetworkId networkId, Class<T> serviceClass) { |
| 296 | return null; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public ServiceDirectory getServiceDirectory() { |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | @Override |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 305 | public ApplicationId getVirtualNetworkApplicationId(NetworkId networkId) { |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | @Override |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 310 | public void registerTenantId(TenantId tenantId) { |
| 311 | |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void unregisterTenantId(TenantId tenantId) { |
| 316 | |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public Set<TenantId> getTenantIds() { |
| 321 | return ImmutableSet.of(TENANT_ID); |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public VirtualNetwork createVirtualNetwork(TenantId tenantId) { |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void removeVirtualNetwork(NetworkId networkId) { |
| 331 | |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public VirtualDevice createVirtualDevice(NetworkId networkId, |
| 336 | DeviceId deviceId) { |
| 337 | return null; |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public void removeVirtualDevice(NetworkId networkId, DeviceId deviceId) { |
| 342 | |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public VirtualHost createVirtualHost(NetworkId networkId, HostId hostId, |
| 347 | MacAddress mac, VlanId vlan, |
| 348 | HostLocation location, |
| 349 | Set<IpAddress> ips) { |
| 350 | return null; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public void removeVirtualHost(NetworkId networkId, HostId hostId) { |
| 355 | |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public VirtualLink createVirtualLink(NetworkId networkId, |
| 360 | ConnectPoint src, ConnectPoint dst) { |
| 361 | return null; |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public void removeVirtualLink(NetworkId networkId, |
| 366 | ConnectPoint src, ConnectPoint dst) { |
| 367 | |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public VirtualPort createVirtualPort(NetworkId networkId, |
| 372 | DeviceId deviceId, |
| 373 | PortNumber portNumber, |
| 374 | ConnectPoint realizedBy) { |
| 375 | return null; |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public void bindVirtualPort(NetworkId networkId, |
| 380 | DeviceId deviceId, |
| 381 | PortNumber portNumber, |
| 382 | ConnectPoint realizedBy) { |
| 383 | |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public void removeVirtualPort(NetworkId networkId, DeviceId deviceId, |
| 388 | PortNumber portNumber) { |
| 389 | |
| 390 | } |
Yoonseon Han | 33821fb | 2017-02-24 13:40:44 +0900 | [diff] [blame] | 391 | |
| 392 | @Override |
| 393 | public void addListener(VirtualNetworkListener listener) { |
| 394 | |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public void removeListener(VirtualNetworkListener listener) { |
| 399 | |
| 400 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 401 | } |
| 402 | |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 403 | private static class TestVirtualPacketProviderService |
| 404 | extends AbstractVirtualProviderService<VirtualPacketProvider> |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 405 | implements VirtualPacketProviderService { |
| 406 | static List<PacketContext> requestedContext = new LinkedList(); |
| 407 | static List<NetworkId> requestedNetworkId = new LinkedList(); |
| 408 | |
| 409 | @Override |
| 410 | public VirtualPacketProvider provider() { |
| 411 | return null; |
| 412 | } |
| 413 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 414 | public NetworkId getRequestedNetworkId(int index) { |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 415 | return requestedNetworkId.get(index); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | public PacketContext getRequestedPacketContext(int index) { |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 419 | return requestedContext.get(index); |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public void processPacket(PacketContext context) { |
| 424 | requestedContext.add(context); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | private static class TestPacketService extends PacketServiceAdapter { |
| 429 | static List<OutboundPacket> requestedPacket = new LinkedList(); |
| 430 | static PacketProcessor processor = null; |
| 431 | |
| 432 | @Override |
| 433 | public void addProcessor(PacketProcessor processor, int priority) { |
| 434 | this.processor = processor; |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public void emit(OutboundPacket packet) { |
| 439 | requestedPacket.add(packet); |
| 440 | } |
| 441 | |
| 442 | public OutboundPacket getRequestedPacket(int index) { |
| 443 | return requestedPacket.get(index); |
| 444 | } |
| 445 | |
| 446 | public int getRequestedPacketCount() { |
| 447 | return requestedPacket.size(); |
| 448 | } |
| 449 | |
| 450 | public void sendTestPacketContext(PacketContext context) { |
| 451 | processor.process(context); |
| 452 | } |
| 453 | } |
| 454 | } |