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