blob: 095d79720e3be7891c74f16febe574d90eb8068d [file] [log] [blame]
Andrea Campanella5b240532016-02-05 09:44:41 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Andrea Campanella5b240532016-02-05 09:44:41 -08003 *
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
17package org.onosproject.net.packet.impl;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableMap;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.cluster.ClusterServiceAdapter;
24import org.onosproject.common.event.impl.TestEventDispatcher;
25import org.onosproject.core.CoreServiceAdapter;
26import org.onosproject.core.IdGenerator;
27import org.onosproject.event.TestListener;
28import org.onosproject.net.AnnotationKeys;
29import org.onosproject.net.DefaultAnnotations;
30import org.onosproject.net.DefaultDevice;
31import org.onosproject.net.Device;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.device.DeviceServiceAdapter;
34import org.onosproject.net.driver.AbstractHandlerBehaviour;
35import org.onosproject.net.driver.DefaultDriver;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070036import org.onosproject.net.driver.DriverRegistry;
Andrea Campanella5b240532016-02-05 09:44:41 -080037import org.onosproject.net.driver.impl.DriverManager;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070038import org.onosproject.net.driver.impl.DriverRegistryManager;
Andrea Campanella5b240532016-02-05 09:44:41 -080039import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.packet.DefaultOutboundPacket;
41import org.onosproject.net.packet.OutboundPacket;
42import org.onosproject.net.packet.PacketProgrammable;
43import org.onosproject.net.packet.PacketProviderRegistry;
44import org.onosproject.net.provider.ProviderId;
45import org.onosproject.net.provider.TestProvider;
46import org.onosproject.store.trivial.SimplePacketStore;
47
48import java.nio.ByteBuffer;
49import java.util.concurrent.atomic.AtomicLong;
50
51import static org.junit.Assert.assertEquals;
52import static org.onosproject.net.NetTestTools.injectEventDispatcher;
53
54/**
55 * Test packet manager activity.
56 */
57public 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 Vachuska11b99fc2017-04-27 12:51:04 -070089
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 Campanella5b240532016-02-05 09:44:41 -080096 }
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 Vachuska11b99fc2017-04-27 12:51:04 -0700147 TestDriverManager(DriverRegistry registry) {
148 this.registry = registry;
Andrea Campanella5b240532016-02-05 09:44:41 -0800149 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}