Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.packet.impl; |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 17 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 21 | import java.util.Map; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 22 | import java.util.concurrent.ConcurrentHashMap; |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 23 | |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onlab.onos.net.Device; |
| 31 | import org.onlab.onos.net.device.DeviceService; |
| 32 | import org.onlab.onos.net.packet.OutboundPacket; |
| 33 | import org.onlab.onos.net.packet.PacketContext; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.packet.PacketEvent; |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.packet.PacketProcessor; |
| 36 | import org.onlab.onos.net.packet.PacketProvider; |
| 37 | import org.onlab.onos.net.packet.PacketProviderRegistry; |
| 38 | import org.onlab.onos.net.packet.PacketProviderService; |
| 39 | import org.onlab.onos.net.packet.PacketService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.packet.PacketStore; |
| 41 | import org.onlab.onos.net.packet.PacketStoreDelegate; |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 43 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 44 | import org.slf4j.Logger; |
| 45 | |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Provides a basic implementation of the packet SB & NB APIs. |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 51 | public class PacketManager |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 52 | extends AbstractProviderRegistry<PacketProvider, PacketProviderService> |
| 53 | implements PacketService, PacketProviderRegistry { |
| 54 | |
| 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 57 | private final PacketStoreDelegate delegate = new InternalStoreDelegate(); |
| 58 | |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | private DeviceService deviceService; |
| 61 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | private PacketStore store; |
| 64 | |
| 65 | private final Map<Integer, PacketProcessor> processors = new ConcurrentHashMap<>(); |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 66 | |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 67 | @Activate |
| 68 | public void activate() { |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 69 | store.setDelegate(delegate); |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 70 | log.info("Started"); |
| 71 | } |
| 72 | |
| 73 | @Deactivate |
| 74 | public void deactivate() { |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 75 | store.unsetDelegate(delegate); |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 76 | log.info("Stopped"); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void addProcessor(PacketProcessor processor, int priority) { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 81 | checkNotNull(processor, "Processor cannot be null"); |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 82 | processors.put(priority, processor); |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void removeProcessor(PacketProcessor processor) { |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 87 | checkNotNull(processor, "Processor cannot be null"); |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 88 | processors.values().remove(processor); |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void emit(OutboundPacket packet) { |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 93 | checkNotNull(packet, "Packet cannot be null"); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 94 | |
| 95 | store.emit(packet); |
| 96 | } |
| 97 | |
| 98 | private void localEmit(OutboundPacket packet) { |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 99 | final Device device = deviceService.getDevice(packet.sendThrough()); |
Jonathan Hart | 7466d61 | 2014-11-24 17:09:53 -0800 | [diff] [blame] | 100 | |
| 101 | if (device == null) { |
| 102 | return; |
| 103 | } |
| 104 | |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 105 | final PacketProvider packetProvider = getProvider(device.providerId()); |
Jonathan Hart | 7466d61 | 2014-11-24 17:09:53 -0800 | [diff] [blame] | 106 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 107 | if (packetProvider != null) { |
| 108 | packetProvider.emit(packet); |
| 109 | } |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 113 | protected PacketProviderService createProviderService(PacketProvider provider) { |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 114 | return new InternalPacketProviderService(provider); |
| 115 | } |
| 116 | |
| 117 | // Personalized link provider service issued to the supplied provider. |
| 118 | private class InternalPacketProviderService |
| 119 | extends AbstractProviderService<PacketProvider> |
| 120 | implements PacketProviderService { |
| 121 | |
| 122 | protected InternalPacketProviderService(PacketProvider provider) { |
| 123 | super(provider); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void processPacket(PacketContext context) { |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 128 | for (PacketProcessor processor : processors.values()) { |
alshabib | 7674db4 | 2014-09-12 23:40:46 -0700 | [diff] [blame] | 129 | processor.process(context); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Internal callback from the packet store. |
| 137 | */ |
| 138 | private class InternalStoreDelegate |
| 139 | implements PacketStoreDelegate { |
| 140 | @Override |
| 141 | public void notify(PacketEvent event) { |
| 142 | localEmit(event.subject()); |
| 143 | } |
| 144 | } |
| 145 | |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 146 | } |