Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.packet.impl; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 24 | import org.onlab.util.KryoNamespace; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.mastership.MastershipService; |
| 28 | import org.onosproject.net.packet.OutboundPacket; |
| 29 | import org.onosproject.net.packet.PacketEvent; |
| 30 | import org.onosproject.net.packet.PacketEvent.Type; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame^] | 31 | import org.onosproject.net.packet.PacketRequest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.packet.PacketStore; |
| 33 | import org.onosproject.net.packet.PacketStoreDelegate; |
| 34 | import org.onosproject.store.AbstractStore; |
| 35 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 36 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 37 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 38 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 39 | import org.onosproject.store.serializers.KryoNamespaces; |
| 40 | import org.onosproject.store.serializers.KryoSerializer; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame^] | 41 | import org.onosproject.store.service.ConsistentMap; |
| 42 | import org.onosproject.store.service.Serializer; |
| 43 | import org.onosproject.store.service.StorageService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame^] | 46 | import java.util.Set; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 47 | import java.util.concurrent.ExecutorService; |
| 48 | import java.util.concurrent.Executors; |
| 49 | |
| 50 | import static org.onlab.util.Tools.groupedThreads; |
| 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Distributed packet store implementation allowing packets to be sent to |
| 55 | * remote instances. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | @Service |
| 59 | public class DistributedPacketStore |
| 60 | extends AbstractStore<PacketEvent, PacketStoreDelegate> |
| 61 | implements PacketStore { |
| 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 65 | // TODO: make this configurable. |
| 66 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; |
| 67 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 69 | protected MastershipService mastershipService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 72 | protected ClusterService clusterService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 75 | protected ClusterCommunicationService communicationService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 76 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame^] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected StorageService storageService; |
| 79 | |
| 80 | private PacketRequestTracker tracker; |
| 81 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 82 | private static final MessageSubject PACKET_OUT_SUBJECT = |
| 83 | new MessageSubject("packet-out"); |
| 84 | |
| 85 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 86 | @Override |
| 87 | protected void setupKryoPool() { |
| 88 | serializerPool = KryoNamespace.newBuilder() |
| 89 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 90 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 91 | .build(); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 92 | } |
| 93 | }; |
| 94 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 95 | private ExecutorService messageHandlingExecutor; |
| 96 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 97 | @Activate |
| 98 | public void activate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 99 | messageHandlingExecutor = Executors.newFixedThreadPool( |
| 100 | MESSAGE_HANDLER_THREAD_POOL_SIZE, |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 101 | groupedThreads("onos/store/packet", "message-handlers")); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 102 | |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 103 | communicationService.addSubscriber(PACKET_OUT_SUBJECT, |
| 104 | new InternalClusterMessageHandler(), |
| 105 | messageHandlingExecutor); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 106 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame^] | 107 | tracker = new PacketRequestTracker(); |
| 108 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 109 | log.info("Started"); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Deactivate |
| 113 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 114 | communicationService.removeSubscriber(PACKET_OUT_SUBJECT); |
| 115 | messageHandlingExecutor.shutdown(); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 116 | log.info("Stopped"); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void emit(OutboundPacket packet) { |
| 121 | NodeId myId = clusterService.getLocalNode().id(); |
| 122 | NodeId master = mastershipService.getMasterFor(packet.sendThrough()); |
| 123 | |
Jonathan Hart | 7466d61 | 2014-11-24 17:09:53 -0800 | [diff] [blame] | 124 | if (master == null) { |
| 125 | return; |
| 126 | } |
| 127 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 128 | if (myId.equals(master)) { |
| 129 | notifyDelegate(new PacketEvent(Type.EMIT, packet)); |
| 130 | return; |
| 131 | } |
| 132 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 133 | // TODO check unicast return value |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 134 | communicationService.unicast(new ClusterMessage(myId, PACKET_OUT_SUBJECT, |
| 135 | SERIALIZER.encode(packet)), |
| 136 | master); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 137 | // error log: log.warn("Failed to send packet-out to {}", master); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 138 | } |
| 139 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame^] | 140 | @Override |
| 141 | public boolean requestPackets(PacketRequest request) { |
| 142 | return tracker.add(request); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Set<PacketRequest> existingRequests() { |
| 147 | return tracker.requests(); |
| 148 | } |
| 149 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Handles incoming cluster messages. |
| 152 | */ |
| 153 | private class InternalClusterMessageHandler implements ClusterMessageHandler { |
| 154 | @Override |
| 155 | public void handle(ClusterMessage message) { |
| 156 | if (!message.subject().equals(PACKET_OUT_SUBJECT)) { |
| 157 | log.warn("Received message with wrong subject: {}", message); |
| 158 | } |
| 159 | |
| 160 | OutboundPacket packet = SERIALIZER.decode(message.payload()); |
| 161 | notifyDelegate(new PacketEvent(Type.EMIT, packet)); |
| 162 | } |
| 163 | } |
| 164 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame^] | 165 | private class PacketRequestTracker { |
| 166 | |
| 167 | private ConsistentMap<PacketRequest, Boolean> requests; |
| 168 | |
| 169 | public PacketRequestTracker() { |
| 170 | requests = storageService.<PacketRequest, Boolean>consistentMapBuilder() |
| 171 | .withName("packet-requests") |
| 172 | .withSerializer(new Serializer() { |
| 173 | KryoNamespace kryo = new KryoNamespace.Builder() |
| 174 | .register(KryoNamespaces.API) |
| 175 | .build(); |
| 176 | @Override |
| 177 | public <T> byte[] encode(T object) { |
| 178 | return kryo.serialize(object); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public <T> T decode(byte[] bytes) { |
| 183 | return kryo.deserialize(bytes); |
| 184 | } |
| 185 | }).build(); |
| 186 | } |
| 187 | |
| 188 | public boolean add(PacketRequest request) { |
| 189 | if (requests.putIfAbsent(request, true) == null) { |
| 190 | return true; |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | public boolean remove(PacketRequest request) { |
| 196 | if (requests.remove(request) == null) { |
| 197 | return false; |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | public Set<PacketRequest> requests() { |
| 203 | return requests.keySet(); |
| 204 | } |
| 205 | |
| 206 | } |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 207 | } |