tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.cluster.impl; |
| 2 | |
| 3 | import com.google.common.collect.ImmutableSet; |
| 4 | import org.apache.felix.scr.annotations.Activate; |
| 5 | import org.apache.felix.scr.annotations.Component; |
| 6 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 7 | import org.apache.felix.scr.annotations.Reference; |
| 8 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 9 | import org.apache.felix.scr.annotations.Service; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 10 | import org.onlab.onos.cluster.ClusterEvent; |
| 11 | import org.onlab.onos.cluster.ClusterStore; |
| 12 | import org.onlab.onos.cluster.ClusterStoreDelegate; |
| 13 | import org.onlab.onos.cluster.ControllerNode; |
| 14 | import org.onlab.onos.cluster.DefaultControllerNode; |
| 15 | import org.onlab.onos.cluster.NodeId; |
| 16 | import org.onlab.onos.store.AbstractStore; |
| 17 | import org.onlab.packet.IpPrefix; |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import java.io.IOException; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 22 | import java.util.Map; |
| 23 | import java.util.Set; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 24 | import java.util.concurrent.ConcurrentHashMap; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 25 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 26 | import static org.onlab.onos.cluster.ControllerNode.State; |
| 27 | import static org.onlab.packet.IpPrefix.valueOf; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Distributed implementation of the cluster nodes store. |
| 31 | */ |
| 32 | @Component(immediate = true) |
| 33 | @Service |
| 34 | public class DistributedClusterStore |
| 35 | extends AbstractStore<ClusterEvent, ClusterStoreDelegate> |
| 36 | implements ClusterStore { |
| 37 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 38 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 39 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 40 | private DefaultControllerNode localNode; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 41 | private final Map<NodeId, DefaultControllerNode> nodes = new ConcurrentHashMap<>(); |
| 42 | private final Map<NodeId, State> states = new ConcurrentHashMap<>(); |
| 43 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 45 | private ClusterCommunicationAdminService communicationAdminService; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 46 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 47 | private final ClusterNodesDelegate nodesDelegate = new InnerNodesDelegate(); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 48 | |
| 49 | @Activate |
| 50 | public void activate() { |
| 51 | loadClusterDefinition(); |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 52 | establishSelfIdentity(); |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 53 | |
| 54 | // Start-up the comm service and prime it with the loaded nodes. |
| 55 | communicationAdminService.startUp(localNode, nodesDelegate); |
| 56 | for (DefaultControllerNode node : nodes.values()) { |
| 57 | communicationAdminService.addNode(node); |
| 58 | } |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 59 | log.info("Started"); |
| 60 | } |
| 61 | |
| 62 | @Deactivate |
| 63 | public void deactivate() { |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 64 | log.info("Stopped"); |
| 65 | } |
| 66 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Loads the cluster definition file. |
| 69 | */ |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 70 | private void loadClusterDefinition() { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 71 | ClusterDefinitionStore cds = new ClusterDefinitionStore("../config/cluster.json"); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 72 | try { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 73 | Set<DefaultControllerNode> storedNodes = cds.read(); |
| 74 | for (DefaultControllerNode node : storedNodes) { |
| 75 | nodes.put(node.id(), node); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 76 | } |
| 77 | } catch (IOException e) { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 78 | log.error("Unable to read cluster definitions", e); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 83 | * Determines who the local controller node is. |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 84 | */ |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 85 | private void establishSelfIdentity() { |
| 86 | // Establishes the controller's own identity. |
| 87 | IpPrefix ip = valueOf(System.getProperty("onos.ip", "127.0.1.1")); |
| 88 | localNode = nodes.get(new NodeId(ip.toString())); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 89 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 90 | // As a fall-back, let's make sure we at least know who we are. |
| 91 | if (localNode == null) { |
| 92 | localNode = new DefaultControllerNode(new NodeId(ip.toString()), ip); |
| 93 | nodes.put(localNode.id(), localNode); |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 94 | } |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 95 | states.put(localNode.id(), State.ACTIVE); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public ControllerNode getLocalNode() { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 100 | return localNode; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Set<ControllerNode> getNodes() { |
| 105 | ImmutableSet.Builder<ControllerNode> builder = ImmutableSet.builder(); |
| 106 | return builder.addAll(nodes.values()).build(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public ControllerNode getNode(NodeId nodeId) { |
| 111 | return nodes.get(nodeId); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public State getState(NodeId nodeId) { |
| 116 | State state = states.get(nodeId); |
| 117 | return state == null ? State.INACTIVE : state; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public ControllerNode addNode(NodeId nodeId, IpPrefix ip, int tcpPort) { |
| 122 | DefaultControllerNode node = new DefaultControllerNode(nodeId, ip, tcpPort); |
| 123 | nodes.put(nodeId, node); |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 124 | communicationAdminService.addNode(node); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 125 | return node; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void removeNode(NodeId nodeId) { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 130 | DefaultControllerNode node = nodes.remove(nodeId); |
| 131 | if (node != null) { |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 132 | communicationAdminService.removeNode(node); |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame] | 133 | } |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 134 | } |
| 135 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 136 | // Entity to handle back calls from the connection manager. |
| 137 | private class InnerNodesDelegate implements ClusterNodesDelegate { |
| 138 | @Override |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 139 | public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) { |
| 140 | DefaultControllerNode node = nodes.get(nodeId); |
| 141 | if (node == null) { |
| 142 | node = (DefaultControllerNode) addNode(nodeId, ip, tcpPort); |
| 143 | } |
| 144 | states.put(nodeId, State.ACTIVE); |
| 145 | return node; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 146 | } |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 147 | @Override |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 148 | public void nodeVanished(NodeId nodeId) { |
| 149 | states.put(nodeId, State.INACTIVE); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 152 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 153 | } |