yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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; |
| 18 | |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.junit.TestUtils; |
| 22 | import org.onlab.osgi.TestServiceDirectory; |
| 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 25 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.CoreServiceAdapter; |
| 28 | import org.onosproject.core.IdGenerator; |
| 29 | import org.onosproject.event.EventDeliveryService; |
| 30 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 31 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualNetworkPacketStore; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 34 | import org.onosproject.incubator.net.virtual.impl.provider.VirtualProviderManager; |
| 35 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider; |
| 36 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProvider; |
| 37 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService; |
| 38 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 39 | import org.onosproject.incubator.store.virtual.impl.SimpleVirtualPacketStore; |
| 40 | import org.onosproject.net.NetTestTools; |
| 41 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 42 | import org.onosproject.net.intent.FakeIntentManager; |
| 43 | import org.onosproject.net.intent.TestableIntentService; |
| 44 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 45 | import org.onosproject.net.packet.OutboundPacket; |
| 46 | import org.onosproject.net.packet.PacketContext; |
| 47 | import org.onosproject.net.packet.PacketProcessor; |
| 48 | import org.onosproject.net.provider.ProviderId; |
| 49 | import org.onosproject.store.service.TestStorageService; |
| 50 | |
| 51 | import java.nio.ByteBuffer; |
| 52 | import java.util.concurrent.atomic.AtomicLong; |
| 53 | |
| 54 | import static org.junit.Assert.*; |
| 55 | |
| 56 | public class VirtualNetworkPacketManagerTest extends VirtualNetworkTestUtil { |
| 57 | |
| 58 | private static final int PROCESSOR_PRIORITY = 1; |
| 59 | |
| 60 | private VirtualNetworkManager manager; |
| 61 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 62 | private CoreService coreService = new TestCoreService(); |
| 63 | private TestableIntentService intentService = new FakeIntentManager(); |
| 64 | private TestServiceDirectory testDirectory; |
| 65 | private EventDeliveryService eventDeliveryService; |
| 66 | private VirtualProviderManager providerRegistryService; |
| 67 | |
| 68 | private VirtualNetwork vnet1; |
| 69 | private VirtualNetwork vnet2; |
| 70 | |
| 71 | private VirtualPacketProvider provider = new TestPacketProvider(); |
| 72 | private VirtualNetworkPacketStore packetStore = new SimpleVirtualPacketStore(); |
| 73 | |
| 74 | private VirtualNetworkPacketManager packetManager1; |
| 75 | private VirtualNetworkPacketManager packetManager2; |
| 76 | |
| 77 | @Before |
| 78 | public void setUp() throws TestUtils.TestUtilsException { |
| 79 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 80 | |
| 81 | TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService); |
| 82 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 83 | virtualNetworkManagerStore.activate(); |
| 84 | |
| 85 | manager = new VirtualNetworkManager(); |
| 86 | manager.store = virtualNetworkManagerStore; |
| 87 | manager.coreService = coreService; |
| 88 | manager.intentService = intentService; |
| 89 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
| 90 | |
| 91 | providerRegistryService = new VirtualProviderManager(); |
| 92 | providerRegistryService.registerProvider(provider); |
| 93 | |
| 94 | testDirectory = new TestServiceDirectory() |
| 95 | .add(VirtualNetworkStore.class, virtualNetworkManagerStore) |
| 96 | .add(CoreService.class, coreService) |
| 97 | .add(VirtualProviderRegistryService.class, providerRegistryService) |
| 98 | .add(EventDeliveryService.class, eventDeliveryService) |
| 99 | .add(ClusterService.class, new ClusterServiceAdapter()) |
| 100 | .add(VirtualNetworkPacketStore.class, packetStore); |
| 101 | TestUtils.setField(manager, "serviceDirectory", testDirectory); |
| 102 | |
| 103 | eventDeliveryService = new TestEventDispatcher(); |
| 104 | NetTestTools.injectEventDispatcher(manager, eventDeliveryService); |
| 105 | |
| 106 | manager.activate(); |
| 107 | |
| 108 | vnet1 = VirtualNetworkTestUtil.setupVirtualNetworkTopology(manager, TID1); |
| 109 | vnet2 = VirtualNetworkTestUtil.setupVirtualNetworkTopology(manager, TID2); |
| 110 | |
| 111 | packetManager1 = new VirtualNetworkPacketManager(manager, vnet1.id()); |
| 112 | packetManager2 = new VirtualNetworkPacketManager(manager, vnet2.id()); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Tests the correct usage of addProcessor() for a outbound packet. |
| 117 | */ |
| 118 | @Test |
| 119 | public void addProcessorTest() { |
| 120 | PacketProcessor testProcessor = new TestProcessor(); |
| 121 | packetManager1.addProcessor(testProcessor, PROCESSOR_PRIORITY); |
| 122 | |
| 123 | assertEquals("1 processor expected", 1, |
| 124 | packetManager1.getProcessors().size()); |
| 125 | assertEquals("0 processor expected", 0, |
| 126 | packetManager2.getProcessors().size()); |
| 127 | |
| 128 | assertEquals("not equal packet processor", testProcessor, |
| 129 | packetManager1.getProcessors().get(0).processor()); |
| 130 | assertEquals("not equal packet processor priority", PROCESSOR_PRIORITY, |
| 131 | packetManager1.getProcessors().get(0).priority()); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests the correct usage of addProcessor() for a outbound packet. |
| 136 | */ |
| 137 | @Test |
| 138 | public void removeProcessorTest() { |
| 139 | PacketProcessor testProcessor = new TestProcessor(); |
| 140 | packetManager1.addProcessor(testProcessor, PROCESSOR_PRIORITY); |
| 141 | |
| 142 | assertEquals("1 processor expected", 1, |
| 143 | packetManager1.getProcessors().size()); |
| 144 | assertEquals("0 processor expected", 0, |
| 145 | packetManager2.getProcessors().size()); |
| 146 | |
| 147 | packetManager1.removeProcessor(testProcessor); |
| 148 | |
| 149 | assertEquals("0 processor expected", 0, |
| 150 | packetManager1.getProcessors().size()); |
| 151 | assertEquals("0 processor expected", 0, |
| 152 | packetManager2.getProcessors().size()); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Tests the correct usage of emit() for a outbound packet. |
| 157 | */ |
| 158 | @Test |
| 159 | public void emitTest() { |
| 160 | OutboundPacket packet = |
| 161 | new DefaultOutboundPacket(VDID1, DefaultTrafficTreatment.emptyTreatment(), ByteBuffer.allocate(5)); |
| 162 | packetManager1.emit(packet); |
| 163 | assertEquals("Packet not emitted correctly", packet, emittedPacket); |
| 164 | } |
| 165 | |
| 166 | private static OutboundPacket emittedPacket = null; |
| 167 | |
| 168 | /** |
| 169 | * Core service test class. |
| 170 | */ |
| 171 | private class TestCoreService extends CoreServiceAdapter { |
| 172 | |
| 173 | @Override |
| 174 | public IdGenerator getIdGenerator(String topic) { |
| 175 | return new IdGenerator() { |
| 176 | private AtomicLong counter = new AtomicLong(0); |
| 177 | |
| 178 | @Override |
| 179 | public long getNewId() { |
| 180 | return counter.getAndIncrement(); |
| 181 | } |
| 182 | }; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private class TestPacketProvider extends AbstractVirtualProvider |
| 187 | implements VirtualPacketProvider { |
| 188 | |
| 189 | /** |
| 190 | * Creates a provider with the supplied identifier. |
| 191 | */ |
| 192 | protected TestPacketProvider() { |
| 193 | super(new ProviderId("test-packet", |
| 194 | "org.onosproject.virtual.test-packet")); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void emit(NetworkId networkId, OutboundPacket packet) { |
| 199 | emittedPacket = packet; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private class TestProcessor implements PacketProcessor { |
| 204 | |
| 205 | @Override |
| 206 | public void process(PacketContext context) { |
| 207 | |
| 208 | } |
| 209 | } |
| 210 | } |