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