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