Andrea Campanella | 5b24053 | 2016-02-05 09:44:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andrea Campanella | 5b24053 | 2016-02-05 09:44:41 -0800 | [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.net.packet.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 24 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 25 | import org.onosproject.core.CoreServiceAdapter; |
| 26 | import org.onosproject.core.IdGenerator; |
| 27 | import org.onosproject.event.TestListener; |
| 28 | import org.onosproject.net.AnnotationKeys; |
| 29 | import org.onosproject.net.DefaultAnnotations; |
| 30 | import org.onosproject.net.DefaultDevice; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 34 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 35 | import org.onosproject.net.driver.DefaultDriver; |
| 36 | import org.onosproject.net.driver.impl.DriverManager; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 39 | import org.onosproject.net.packet.OutboundPacket; |
| 40 | import org.onosproject.net.packet.PacketProgrammable; |
| 41 | import org.onosproject.net.packet.PacketProviderRegistry; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.onosproject.net.provider.TestProvider; |
| 44 | import org.onosproject.store.trivial.SimplePacketStore; |
| 45 | |
| 46 | import java.nio.ByteBuffer; |
| 47 | import java.util.concurrent.atomic.AtomicLong; |
| 48 | |
| 49 | import static org.junit.Assert.assertEquals; |
| 50 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 51 | |
| 52 | /** |
| 53 | * Test packet manager activity. |
| 54 | */ |
| 55 | public class PacketManagerTest { |
| 56 | |
| 57 | private static final ProviderId FOO_PID = new ProviderId("foo", "foo"); |
| 58 | |
| 59 | private static final DeviceId FOO_DID = DeviceId.deviceId("foo:002"); |
| 60 | |
| 61 | private static final DefaultAnnotations ANNOTATIONS = |
| 62 | DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "foo").build(); |
| 63 | |
| 64 | private static final Device FOO_DEV = |
| 65 | new DefaultDevice(FOO_PID, FOO_DID, Device.Type.SWITCH, "", "", "", "", null, ANNOTATIONS); |
| 66 | |
| 67 | private PacketManager mgr; |
| 68 | |
| 69 | protected TestProvider provider; |
| 70 | protected TestListener listener = new TestListener(); |
| 71 | |
| 72 | private PacketProviderRegistry providerRegistry; |
| 73 | |
| 74 | private TestDriverManager driverService; |
| 75 | |
| 76 | @Before |
| 77 | public void setUp() { |
| 78 | mgr = new PacketManager(); |
| 79 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
| 80 | mgr.store = new SimplePacketStore(); |
| 81 | mgr.clusterService = new ClusterServiceAdapter(); |
| 82 | mgr.deviceService = new TestDeviceService(); |
| 83 | mgr.deviceService = new TestDeviceService(); |
| 84 | mgr.coreService = new TestCoreService(); |
| 85 | providerRegistry = mgr; |
| 86 | mgr.activate(); |
| 87 | driverService = new TestDriverManager(); |
| 88 | driverService.addDriver(new DefaultDriver("foo", ImmutableList.of(), "", "", "", |
| 89 | ImmutableMap.of(PacketProgrammable.class, |
| 90 | TestPacketProgrammable.class), |
| 91 | ImmutableMap.of())); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests the correct usage of fallback driver provider for packets. |
| 96 | */ |
| 97 | @Test |
| 98 | public void packetProviderfallbackBasics() { |
| 99 | OutboundPacket packet = |
| 100 | new DefaultOutboundPacket(FOO_DID, DefaultTrafficTreatment.emptyTreatment(), ByteBuffer.allocate(5)); |
| 101 | mgr.emit(packet); |
| 102 | assertEquals("Packet not emitted correctly", packet, emittedPacket); |
| 103 | } |
| 104 | |
| 105 | private static class TestDeviceService extends DeviceServiceAdapter { |
| 106 | @Override |
| 107 | public int getDeviceCount() { |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public Iterable<Device> getDevices() { |
| 113 | return ImmutableList.of(FOO_DEV); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public Iterable<Device> getAvailableDevices() { |
| 118 | return getDevices(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Device getDevice(DeviceId deviceId) { |
| 123 | return FOO_DEV; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private class TestCoreService extends CoreServiceAdapter { |
| 128 | |
| 129 | @Override |
| 130 | public IdGenerator getIdGenerator(String topic) { |
| 131 | return new IdGenerator() { |
| 132 | private AtomicLong counter = new AtomicLong(0); |
| 133 | |
| 134 | @Override |
| 135 | public long getNewId() { |
| 136 | return counter.getAndIncrement(); |
| 137 | } |
| 138 | }; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private class TestDriverManager extends DriverManager { |
| 143 | TestDriverManager() { |
| 144 | this.deviceService = mgr.deviceService; |
| 145 | activate(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private static OutboundPacket emittedPacket = null; |
| 150 | |
| 151 | public static class TestPacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable { |
| 152 | |
| 153 | @Override |
| 154 | public void emit(OutboundPacket packet) { |
| 155 | emittedPacket = packet; |
| 156 | } |
| 157 | } |
| 158 | } |