tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cluster.impl; |
| 2 | |
| 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
| 6 | import org.apache.felix.scr.annotations.Reference; |
| 7 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 8 | import org.apache.felix.scr.annotations.Service; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 9 | import org.onlab.onos.cluster.ClusterAdminService; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 10 | import org.onlab.onos.cluster.ClusterEvent; |
| 11 | import org.onlab.onos.cluster.ClusterEventListener; |
| 12 | import org.onlab.onos.cluster.ClusterService; |
| 13 | import org.onlab.onos.cluster.ClusterStore; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame^] | 14 | import org.onlab.onos.cluster.ClusterStoreDelegate; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 15 | import org.onlab.onos.cluster.ControllerNode; |
| 16 | import org.onlab.onos.cluster.NodeId; |
| 17 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 18 | import org.onlab.onos.event.EventDeliveryService; |
| 19 | import org.slf4j.Logger; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | import static org.slf4j.LoggerFactory.getLogger; |
| 25 | |
| 26 | /** |
| 27 | * Implementation of the cluster service. |
| 28 | */ |
| 29 | @Component(immediate = true) |
| 30 | @Service |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 31 | public class ClusterManager implements ClusterService, ClusterAdminService { |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 32 | |
| 33 | public static final String INSTANCE_ID_NULL = "Instance ID cannot be null"; |
| 34 | private final Logger log = getLogger(getClass()); |
| 35 | |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame^] | 36 | private ClusterStoreDelegate delegate = new InternalStoreDelegate(); |
| 37 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 38 | protected final AbstractListenerRegistry<ClusterEvent, ClusterEventListener> |
| 39 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 40 | |
| 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 42 | protected ClusterStore store; |
| 43 | |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected EventDeliveryService eventDispatcher; |
| 46 | |
| 47 | @Activate |
| 48 | public void activate() { |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame^] | 49 | store.setDelegate(delegate); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 50 | eventDispatcher.addSink(ClusterEvent.class, listenerRegistry); |
| 51 | log.info("Started"); |
| 52 | } |
| 53 | |
| 54 | @Deactivate |
| 55 | public void deactivate() { |
| 56 | eventDispatcher.removeSink(ClusterEvent.class); |
| 57 | log.info("Stopped"); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public ControllerNode getLocalNode() { |
| 62 | return store.getLocalNode(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Set<ControllerNode> getNodes() { |
| 67 | return store.getNodes(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public ControllerNode getNode(NodeId nodeId) { |
| 72 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 73 | return store.getNode(nodeId); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public ControllerNode.State getState(NodeId nodeId) { |
| 78 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 79 | return store.getState(nodeId); |
| 80 | } |
| 81 | |
| 82 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 83 | public void removeNode(NodeId nodeId) { |
| 84 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 85 | store.removeNode(nodeId); |
| 86 | } |
| 87 | |
| 88 | @Override |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 89 | public void addListener(ClusterEventListener listener) { |
| 90 | listenerRegistry.addListener(listener); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void removeListener(ClusterEventListener listener) { |
| 95 | listenerRegistry.removeListener(listener); |
| 96 | } |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame^] | 97 | |
| 98 | // Store delegate to re-post events emitted from the store. |
| 99 | private class InternalStoreDelegate implements ClusterStoreDelegate { |
| 100 | @Override |
| 101 | public void notify(ClusterEvent event) { |
| 102 | checkNotNull(event, "Event cannot be null"); |
| 103 | eventDispatcher.post(event); |
| 104 | } |
| 105 | } |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 106 | } |