yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 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; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreServiceAdapter; |
| 27 | import org.onosproject.core.DefaultApplicationId; |
| 28 | import org.onosproject.incubator.net.virtual.DefaultVirtualDevice; |
| 29 | import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork; |
| 30 | import org.onosproject.incubator.net.virtual.DefaultVirtualPort; |
| 31 | import org.onosproject.incubator.net.virtual.NetworkId; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 32 | import org.onosproject.net.TenantId; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 33 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 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 | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 36 | import org.onosproject.incubator.net.virtual.VirtualPort; |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 37 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProviderService; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 38 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProvider; |
| 39 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProviderService; |
| 40 | import org.onosproject.net.ConnectPoint; |
| 41 | import org.onosproject.net.DefaultAnnotations; |
| 42 | import org.onosproject.net.DefaultDevice; |
| 43 | import org.onosproject.net.DefaultLink; |
| 44 | import org.onosproject.net.DefaultPort; |
| 45 | import org.onosproject.net.Device; |
| 46 | import org.onosproject.net.DeviceId; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 47 | import org.onosproject.net.Link; |
| 48 | import org.onosproject.net.Port; |
| 49 | import org.onosproject.net.PortNumber; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 50 | import org.onosproject.net.flow.DefaultTrafficSelector; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 52 | import org.onosproject.net.flow.TrafficSelector; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flow.TrafficTreatment; |
| 54 | import org.onosproject.net.flow.instructions.Instruction; |
| 55 | import org.onosproject.net.flow.instructions.Instructions; |
| 56 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 57 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 58 | import org.onosproject.net.packet.DefaultPacketContext; |
| 59 | import org.onosproject.net.packet.InboundPacket; |
| 60 | import org.onosproject.net.packet.OutboundPacket; |
| 61 | import org.onosproject.net.packet.PacketContext; |
| 62 | import org.onosproject.net.packet.PacketProcessor; |
| 63 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 64 | import org.onosproject.net.provider.ProviderId; |
| 65 | |
| 66 | import java.nio.ByteBuffer; |
| 67 | import java.util.LinkedList; |
| 68 | import java.util.List; |
| 69 | import java.util.Set; |
| 70 | |
| 71 | import static org.junit.Assert.assertEquals; |
| 72 | |
| 73 | public class DefaultVirtualPacketProviderTest { |
| 74 | private static final String SRC_MAC_ADDR = "00:00:00:00:00:00"; |
| 75 | private static final String DST_MAC_ADDR = "00:00:00:00:00:01"; |
| 76 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 77 | |
| 78 | private static final DeviceId DID1 = DeviceId.deviceId("of:001"); |
| 79 | private static final DeviceId DID2 = DeviceId.deviceId("of:002"); |
| 80 | private static final PortNumber PORT_NUM1 = PortNumber.portNumber(1); |
| 81 | private static final PortNumber PORT_NUM2 = PortNumber.portNumber(2); |
| 82 | private static final PortNumber PORT_NUM3 = PortNumber.portNumber(3); |
| 83 | private static final PortNumber PORT_NUM4 = PortNumber.portNumber(4); |
| 84 | |
| 85 | private static final DefaultAnnotations ANNOTATIONS = |
| 86 | DefaultAnnotations.builder().set("foo", "bar").build(); |
| 87 | |
| 88 | private static final Device DEV1 = |
| 89 | new DefaultDevice(PID, DID1, Device.Type.SWITCH, "", "", "", "", null); |
| 90 | private static final Device DEV2 = |
| 91 | new DefaultDevice(PID, DID2, Device.Type.SWITCH, "", "", "", "", null); |
| 92 | private static final Port PORT11 = |
| 93 | new DefaultPort(DEV1, PORT_NUM1, true, ANNOTATIONS); |
| 94 | private static final Port PORT12 = |
| 95 | new DefaultPort(DEV1, PORT_NUM2, true, ANNOTATIONS); |
| 96 | private static final Port PORT21 = |
| 97 | new DefaultPort(DEV2, PORT_NUM3, true, ANNOTATIONS); |
| 98 | private static final Port PORT22 = |
| 99 | new DefaultPort(DEV2, PORT_NUM4, true, ANNOTATIONS); |
| 100 | |
| 101 | private static final ConnectPoint CP11 = new ConnectPoint(DID1, PORT_NUM1); |
| 102 | private static final ConnectPoint CP12 = new ConnectPoint(DID1, PORT_NUM2); |
| 103 | private static final ConnectPoint CP21 = new ConnectPoint(DID2, PORT_NUM3); |
| 104 | private static final ConnectPoint CP22 = new ConnectPoint(DID2, PORT_NUM4); |
| 105 | private static final Link LINK1 = DefaultLink.builder() |
| 106 | .src(CP12).dst(CP21).providerId(PID).type(Link.Type.DIRECT).build(); |
| 107 | |
| 108 | private static final TenantId TENANT_ID = TenantId.tenantId("1"); |
| 109 | private static final NetworkId VNET_ID = NetworkId.networkId(1); |
| 110 | private static final DeviceId VDID = DeviceId.deviceId("of:100"); |
| 111 | |
| 112 | private static final PortNumber VPORT_NUM1 = PortNumber.portNumber(10); |
| 113 | private static final PortNumber VPORT_NUM2 = PortNumber.portNumber(11); |
| 114 | |
| 115 | private static final VirtualNetwork VNET = new DefaultVirtualNetwork( |
| 116 | VNET_ID, TenantId.tenantId("t1")); |
| 117 | private static final VirtualDevice VDEV = |
| 118 | new DefaultVirtualDevice(VNET_ID, VDID); |
| 119 | private static final VirtualPort VPORT1 = |
| 120 | new DefaultVirtualPort(VNET_ID, VDEV, VPORT_NUM1, CP11); |
| 121 | private static final VirtualPort VPORT2 = |
| 122 | new DefaultVirtualPort(VNET_ID, VDEV, VPORT_NUM2, CP22); |
| 123 | private static final ConnectPoint VCP11 = new ConnectPoint(VDID, VPORT_NUM1); |
| 124 | private static final ConnectPoint VCP12 = new ConnectPoint(VDID, VPORT_NUM2); |
| 125 | |
| 126 | protected DefaultVirtualPacketProvider virtualProvider; |
| 127 | protected TestPacketService testPacketService; |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 128 | protected TestVirtualPacketProviderService providerService; |
| 129 | |
| 130 | private VirtualProviderManager providerManager; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 131 | |
| 132 | private ApplicationId vAppId; |
| 133 | |
| 134 | @Before |
| 135 | public void setUp() { |
| 136 | virtualProvider = new DefaultVirtualPacketProvider(); |
| 137 | |
| 138 | virtualProvider.coreService = new CoreServiceAdapter(); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 139 | virtualProvider.vnaService = |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 140 | new TestVirtualNetworkAdminService(); |
| 141 | |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 142 | providerService = new TestVirtualPacketProviderService(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 143 | |
| 144 | testPacketService = new TestPacketService(); |
| 145 | virtualProvider.packetService = testPacketService; |
| 146 | |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 147 | providerManager = new VirtualProviderManager(); |
| 148 | virtualProvider.providerRegistryService = providerManager; |
| 149 | providerManager.registerProviderService(VNET_ID, providerService); |
| 150 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 151 | virtualProvider.activate(); |
| 152 | vAppId = new TestApplicationId(0, "Virtual App"); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 153 | |
| 154 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 155 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 156 | |
Harold Huang | e86d35f | 2017-06-14 15:15:18 +0800 | [diff] [blame] | 157 | virtualProvider.startPacketHandling(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @After |
| 161 | public void tearDown() { |
| 162 | virtualProvider.deactivate(); |
| 163 | virtualProvider.coreService = null; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 164 | virtualProvider.vnaService = null; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
| 168 | /** Test the virtual outbound packet is delivered to a proper (physical) |
| 169 | * device. |
| 170 | */ |
| 171 | @Test |
| 172 | public void devirtualizePacket() { |
| 173 | TrafficTreatment tr = DefaultTrafficTreatment.builder() |
| 174 | .setOutput(VPORT_NUM1).build(); |
| 175 | ByteBuffer data = ByteBuffer.wrap("abc".getBytes()); |
| 176 | |
| 177 | OutboundPacket vOutPacket = new DefaultOutboundPacket(VDID, tr, data); |
| 178 | |
| 179 | virtualProvider.emit(VNET_ID, vOutPacket); |
| 180 | |
| 181 | assertEquals("The count should be 1", 1, |
| 182 | testPacketService.getRequestedPacketCount()); |
| 183 | |
| 184 | OutboundPacket pOutPacket = testPacketService.getRequestedPacket(0); |
| 185 | |
| 186 | assertEquals("The packet should be requested on DEV1", DID1, |
| 187 | pOutPacket.sendThrough()); |
| 188 | |
| 189 | PortNumber outPort = pOutPacket.treatment() |
| 190 | .allInstructions() |
| 191 | .stream() |
| 192 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 193 | .map(i -> (Instructions.OutputInstruction) i) |
| 194 | .map(i -> i.port()) |
| 195 | .findFirst().get(); |
| 196 | assertEquals("The packet should be out at PORT1 of DEV1", PORT_NUM1, |
| 197 | outPort); |
| 198 | } |
| 199 | |
| 200 | /** Test the physical packet context is delivered to a proper (physical) |
| 201 | * virtual network and device. |
| 202 | */ |
| 203 | @Test |
| 204 | public void virtualizePacket() { |
| 205 | Ethernet eth = new Ethernet(); |
| 206 | eth.setSourceMACAddress(SRC_MAC_ADDR); |
| 207 | eth.setDestinationMACAddress(DST_MAC_ADDR); |
| 208 | eth.setVlanID((short) 1); |
| 209 | eth.setPayload(null); |
| 210 | |
| 211 | InboundPacket pInPacket = |
| 212 | new DefaultInboundPacket(CP22, eth, |
| 213 | ByteBuffer.wrap(eth.serialize())); |
| 214 | |
| 215 | PacketContext pContext = |
| 216 | new TestPacketContext(System.nanoTime(), pInPacket, null, false); |
| 217 | |
| 218 | testPacketService.sendTestPacketContext(pContext); |
| 219 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 220 | PacketContext vContext = providerService.getRequestedPacketContext(0); |
| 221 | InboundPacket vInPacket = vContext.inPacket(); |
| 222 | |
| 223 | assertEquals("the packet should be received from VCP12", |
| 224 | VCP12, vInPacket.receivedFrom()); |
| 225 | |
| 226 | assertEquals("VLAN tag should be excludede", VlanId.UNTAGGED, |
| 227 | vInPacket.parsed().getVlanID()); |
| 228 | } |
| 229 | |
| 230 | private class TestPacketContext extends DefaultPacketContext { |
| 231 | |
| 232 | /** |
| 233 | * Creates a new packet context. |
| 234 | * |
| 235 | * @param time creation time |
| 236 | * @param inPkt inbound packet |
| 237 | * @param outPkt outbound packet |
| 238 | * @param block whether the context is blocked or not |
| 239 | */ |
| 240 | protected TestPacketContext(long time, InboundPacket inPkt, |
| 241 | OutboundPacket outPkt, boolean block) { |
| 242 | super(time, inPkt, outPkt, block); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void send() { |
| 247 | |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private static class TestApplicationId extends DefaultApplicationId { |
| 252 | public TestApplicationId(int id, String name) { |
| 253 | super(id, name); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | private static class TestVirtualNetworkAdminService |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 258 | extends VirtualNetworkAdminServiceAdapter { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 259 | |
| 260 | @Override |
| 261 | public Set<VirtualNetwork> getVirtualNetworks(TenantId tenantId) { |
| 262 | return ImmutableSet.of(VNET); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) { |
| 267 | return ImmutableSet.of(VDEV); |
| 268 | } |
| 269 | |
| 270 | @Override |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 271 | public Set<VirtualPort> getVirtualPorts(NetworkId networkId, |
| 272 | DeviceId deviceId) { |
| 273 | return ImmutableSet.of(VPORT1, VPORT2); |
| 274 | } |
| 275 | |
| 276 | @Override |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 277 | public Set<TenantId> getTenantIds() { |
| 278 | return ImmutableSet.of(TENANT_ID); |
| 279 | } |
| 280 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 281 | } |
| 282 | |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 283 | private static class TestVirtualPacketProviderService |
| 284 | extends AbstractVirtualProviderService<VirtualPacketProvider> |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 285 | implements VirtualPacketProviderService { |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 286 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 287 | static List<PacketContext> requestedContext = new LinkedList(); |
| 288 | static List<NetworkId> requestedNetworkId = new LinkedList(); |
| 289 | |
| 290 | @Override |
| 291 | public VirtualPacketProvider provider() { |
| 292 | return null; |
| 293 | } |
| 294 | |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 295 | PacketContext getRequestedPacketContext(int index) { |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 296 | return requestedContext.get(index); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void processPacket(PacketContext context) { |
| 301 | requestedContext.add(context); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | private static class TestPacketService extends PacketServiceAdapter { |
| 306 | static List<OutboundPacket> requestedPacket = new LinkedList(); |
| 307 | static PacketProcessor processor = null; |
| 308 | |
| 309 | @Override |
| 310 | public void addProcessor(PacketProcessor processor, int priority) { |
| 311 | this.processor = processor; |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void emit(OutboundPacket packet) { |
| 316 | requestedPacket.add(packet); |
| 317 | } |
| 318 | |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 319 | OutboundPacket getRequestedPacket(int index) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 320 | return requestedPacket.get(index); |
| 321 | } |
| 322 | |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 323 | int getRequestedPacketCount() { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 324 | return requestedPacket.size(); |
| 325 | } |
| 326 | |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 327 | void sendTestPacketContext(PacketContext context) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 328 | processor.process(context); |
| 329 | } |
| 330 | } |
| 331 | } |