Andrea Campanella
Committed by Gerrit Code Review

ONOS-3759 Packet fallback driver provider

Change-Id: I8d526f66200b68d21aad3bd88575f6021d10c9a1
1 +/*
2 + * Copyright 2016 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.net.packet;
18 +
19 +import org.onosproject.net.driver.HandlerBehaviour;
20 +
21 +/**
22 + * Packet programmable device behaviour.
23 + */
24 +public interface PacketProgrammable extends HandlerBehaviour {
25 +
26 + /**
27 + * Emits the specified outbound packet onto the network from the device.
28 + * @param packet outbound packet
29 + */
30 + void emit(OutboundPacket packet);
31 +}
1 +/*
2 + * Copyright 2016 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.net.packet.impl;
18 +
19 +import org.onosproject.net.DeviceId;
20 +import org.onosproject.net.device.DeviceService;
21 +import org.onosproject.net.packet.OutboundPacket;
22 +import org.onosproject.net.packet.PacketProgrammable;
23 +import org.onosproject.net.packet.PacketProvider;
24 +import org.onosproject.net.provider.AbstractProvider;
25 +import org.onosproject.net.provider.ProviderId;
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +/**
30 + * Driver-based packet rule provider.
31 + */
32 +public class PacketDriverProvider extends AbstractProvider implements PacketProvider {
33 +
34 + private final Logger log = LoggerFactory.getLogger(getClass());
35 +
36 + // To be extracted for reuse as we deal with other.
37 + private static final String SCHEME = "default";
38 + private static final String PROVIDER_NAME = "org.onosproject.provider";
39 + protected DeviceService deviceService;
40 +
41 + public PacketDriverProvider() {
42 + super(new ProviderId(SCHEME, PROVIDER_NAME));
43 + }
44 +
45 + /**
46 + * Initializes the provider with the necessary device service.
47 + *
48 + * @param deviceService device service
49 + */
50 + void init(DeviceService deviceService) {
51 + this.deviceService = deviceService;
52 + }
53 +
54 + @Override
55 + public void emit(OutboundPacket packet) {
56 + PacketProgrammable programmable = getPacketProgrammable(packet.sendThrough());
57 + if (programmable != null) {
58 + programmable.emit(packet);
59 + }
60 + }
61 +
62 + private PacketProgrammable getPacketProgrammable(DeviceId deviceId) {
63 + PacketProgrammable programmable = deviceService.getDevice(deviceId).as(PacketProgrammable.class);
64 + if (programmable == null) {
65 + log.warn("Device {} is not packet programmable");
66 + }
67 + return programmable;
68 + }
69 +}
...@@ -89,19 +89,19 @@ public class PacketManager ...@@ -89,19 +89,19 @@ public class PacketManager
89 private final PacketStoreDelegate delegate = new InternalStoreDelegate(); 89 private final PacketStoreDelegate delegate = new InternalStoreDelegate();
90 90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 - private CoreService coreService; 92 + protected CoreService coreService;
93 93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 - private ClusterService clusterService; 95 + protected ClusterService clusterService;
96 96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 - private DeviceService deviceService; 98 + protected DeviceService deviceService;
99 99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 - private FlowRuleService flowService; 101 + protected FlowRuleService flowService;
102 102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 - private PacketStore store; 104 + protected PacketStore store;
105 105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 private FlowObjectiveService objectiveService; 107 private FlowObjectiveService objectiveService;
...@@ -112,6 +112,8 @@ public class PacketManager ...@@ -112,6 +112,8 @@ public class PacketManager
112 112
113 private final List<ProcessorEntry> processors = Lists.newCopyOnWriteArrayList(); 113 private final List<ProcessorEntry> processors = Lists.newCopyOnWriteArrayList();
114 114
115 + private final PacketDriverProvider defaultProvider = new PacketDriverProvider();
116 +
115 private ApplicationId appId; 117 private ApplicationId appId;
116 private NodeId localNodeId; 118 private NodeId localNodeId;
117 119
...@@ -124,6 +126,7 @@ public class PacketManager ...@@ -124,6 +126,7 @@ public class PacketManager
124 store.setDelegate(delegate); 126 store.setDelegate(delegate);
125 deviceService.addListener(deviceListener); 127 deviceService.addListener(deviceListener);
126 store.existingRequests().forEach(this::pushToAllDevices); 128 store.existingRequests().forEach(this::pushToAllDevices);
129 + defaultProvider.init(deviceService);
127 log.info("Started"); 130 log.info("Started");
128 } 131 }
129 132
...@@ -136,6 +139,11 @@ public class PacketManager ...@@ -136,6 +139,11 @@ public class PacketManager
136 } 139 }
137 140
138 @Override 141 @Override
142 + protected PacketProvider defaultProvider() {
143 + return defaultProvider;
144 + }
145 +
146 + @Override
139 public void addProcessor(PacketProcessor processor, int priority) { 147 public void addProcessor(PacketProcessor processor, int priority) {
140 checkPermission(PACKET_EVENT); 148 checkPermission(PACKET_EVENT);
141 checkNotNull(processor, "Processor cannot be null"); 149 checkNotNull(processor, "Processor cannot be null");
...@@ -374,7 +382,7 @@ public class PacketManager ...@@ -374,7 +382,7 @@ public class PacketManager
374 /** 382 /**
375 * Internal callback from the packet store. 383 * Internal callback from the packet store.
376 */ 384 */
377 - private class InternalStoreDelegate implements PacketStoreDelegate { 385 + protected class InternalStoreDelegate implements PacketStoreDelegate {
378 @Override 386 @Override
379 public void notify(PacketEvent event) { 387 public void notify(PacketEvent event) {
380 localEmit(event.subject()); 388 localEmit(event.subject());
......
1 +/*
2 + * Copyright 2016 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.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 +}