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; |
| 31 | import org.onosproject.net.packet.PacketStore; |
| 32 | import org.onosproject.net.packet.PacketStoreDelegate; |
| 33 | import org.onosproject.store.AbstractStore; |
| 34 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 35 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 36 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 37 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 38 | import org.onosproject.store.serializers.KryoNamespaces; |
| 39 | import org.onosproject.store.serializers.KryoSerializer; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 42 | import java.util.concurrent.ExecutorService; |
| 43 | import java.util.concurrent.Executors; |
| 44 | |
| 45 | import static org.onlab.util.Tools.groupedThreads; |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Distributed packet store implementation allowing packets to be sent to |
| 50 | * remote instances. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | @Service |
| 54 | public class DistributedPacketStore |
| 55 | extends AbstractStore<PacketEvent, PacketStoreDelegate> |
| 56 | implements PacketStore { |
| 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 60 | // TODO: make this configurable. |
| 61 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; |
| 62 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame^] | 64 | protected MastershipService mastershipService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame^] | 67 | protected ClusterService clusterService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame^] | 70 | protected ClusterCommunicationService communicationService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 71 | |
| 72 | private static final MessageSubject PACKET_OUT_SUBJECT = |
| 73 | new MessageSubject("packet-out"); |
| 74 | |
| 75 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 76 | @Override |
| 77 | protected void setupKryoPool() { |
| 78 | serializerPool = KryoNamespace.newBuilder() |
| 79 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 80 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 81 | .build(); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 85 | private ExecutorService messageHandlingExecutor; |
| 86 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 89 | messageHandlingExecutor = Executors.newFixedThreadPool( |
| 90 | MESSAGE_HANDLER_THREAD_POOL_SIZE, |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 91 | groupedThreads("onos/store/packet", "message-handlers")); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 92 | |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame^] | 93 | communicationService.addSubscriber(PACKET_OUT_SUBJECT, |
| 94 | new InternalClusterMessageHandler(), |
| 95 | messageHandlingExecutor); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 96 | |
| 97 | log.info("Started"); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 102 | communicationService.removeSubscriber(PACKET_OUT_SUBJECT); |
| 103 | messageHandlingExecutor.shutdown(); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 104 | log.info("Stopped"); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void emit(OutboundPacket packet) { |
| 109 | NodeId myId = clusterService.getLocalNode().id(); |
| 110 | NodeId master = mastershipService.getMasterFor(packet.sendThrough()); |
| 111 | |
Jonathan Hart | 7466d61 | 2014-11-24 17:09:53 -0800 | [diff] [blame] | 112 | if (master == null) { |
| 113 | return; |
| 114 | } |
| 115 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 116 | if (myId.equals(master)) { |
| 117 | notifyDelegate(new PacketEvent(Type.EMIT, packet)); |
| 118 | return; |
| 119 | } |
| 120 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 121 | // TODO check unicast return value |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame^] | 122 | communicationService.unicast(new ClusterMessage(myId, PACKET_OUT_SUBJECT, |
| 123 | SERIALIZER.encode(packet)), |
| 124 | master); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 125 | // error log: log.warn("Failed to send packet-out to {}", master); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Handles incoming cluster messages. |
| 130 | */ |
| 131 | private class InternalClusterMessageHandler implements ClusterMessageHandler { |
| 132 | @Override |
| 133 | public void handle(ClusterMessage message) { |
| 134 | if (!message.subject().equals(PACKET_OUT_SUBJECT)) { |
| 135 | log.warn("Received message with wrong subject: {}", message); |
| 136 | } |
| 137 | |
| 138 | OutboundPacket packet = SERIALIZER.decode(message.payload()); |
| 139 | notifyDelegate(new PacketEvent(Type.EMIT, packet)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | } |