Showing
2 changed files
with
93 additions
and
0 deletions
... | @@ -14,6 +14,12 @@ public class DefaultControllerNode implements ControllerNode { | ... | @@ -14,6 +14,12 @@ public class DefaultControllerNode implements ControllerNode { |
14 | private final NodeId id; | 14 | private final NodeId id; |
15 | private final IpPrefix ip; | 15 | private final IpPrefix ip; |
16 | 16 | ||
17 | + // For serialization | ||
18 | + private DefaultControllerNode() { | ||
19 | + this.id = null; | ||
20 | + this.ip = null; | ||
21 | + } | ||
22 | + | ||
17 | /** | 23 | /** |
18 | * Creates a new instance with the specified id and IP address. | 24 | * Creates a new instance with the specified id and IP address. |
19 | * | 25 | * | ... | ... |
1 | +package org.onlab.onos.store.cluster.impl; | ||
2 | + | ||
3 | +import com.google.common.collect.ImmutableSet; | ||
4 | +import com.hazelcast.core.HazelcastInstance; | ||
5 | +import com.hazelcast.core.Member; | ||
6 | +import org.apache.felix.scr.annotations.Activate; | ||
7 | +import org.apache.felix.scr.annotations.Component; | ||
8 | +import org.apache.felix.scr.annotations.Deactivate; | ||
9 | +import org.apache.felix.scr.annotations.Reference; | ||
10 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
11 | +import org.apache.felix.scr.annotations.Service; | ||
12 | +import org.onlab.onos.cluster.ClusterStore; | ||
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.StoreService; | ||
17 | +import org.onlab.packet.IpPrefix; | ||
18 | +import org.slf4j.Logger; | ||
19 | + | ||
20 | +import java.util.Set; | ||
21 | + | ||
22 | +import static org.slf4j.LoggerFactory.getLogger; | ||
23 | + | ||
24 | +/** | ||
25 | + * Distributed implementation of the cluster nodes store. | ||
26 | + */ | ||
27 | +@Component(immediate = true) | ||
28 | +@Service | ||
29 | +public class DistributedClusterStore implements ClusterStore { | ||
30 | + | ||
31 | + private final Logger log = getLogger(getClass()); | ||
32 | + | ||
33 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
34 | + protected StoreService storeService; | ||
35 | + | ||
36 | + private HazelcastInstance theInstance; | ||
37 | + | ||
38 | + // FIXME: experimental implementation; enhance to assure persistence and | ||
39 | + // visibility to nodes that are not currently in the cluster | ||
40 | + | ||
41 | + @Activate | ||
42 | + public void activate() { | ||
43 | + log.info("Started"); | ||
44 | + theInstance = storeService.getHazelcastInstance(); | ||
45 | + | ||
46 | + } | ||
47 | + | ||
48 | + @Deactivate | ||
49 | + public void deactivate() { | ||
50 | + log.info("Stopped"); | ||
51 | + } | ||
52 | + | ||
53 | + @Override | ||
54 | + public ControllerNode getLocalNode() { | ||
55 | + return node(theInstance.getCluster().getLocalMember()); | ||
56 | + } | ||
57 | + | ||
58 | + @Override | ||
59 | + public Set<ControllerNode> getNodes() { | ||
60 | + ImmutableSet.Builder<ControllerNode> builder = ImmutableSet.builder(); | ||
61 | + for (Member member : theInstance.getCluster().getMembers()) { | ||
62 | + builder.add(node(member)); | ||
63 | + } | ||
64 | + return builder.build(); | ||
65 | + } | ||
66 | + | ||
67 | + @Override | ||
68 | + public ControllerNode getNode(NodeId nodeId) { | ||
69 | + for (Member member : theInstance.getCluster().getMembers()) { | ||
70 | + if (member.getUuid().equals(nodeId.toString())) { | ||
71 | + return node(member); | ||
72 | + } | ||
73 | + } | ||
74 | + return null; | ||
75 | + } | ||
76 | + | ||
77 | + @Override | ||
78 | + public ControllerNode.State getState(NodeId nodeId) { | ||
79 | + return ControllerNode.State.ACTIVE; | ||
80 | + } | ||
81 | + | ||
82 | + // Creates a controller node descriptor from the Hazelcast member. | ||
83 | + private ControllerNode node(Member member) { | ||
84 | + return new DefaultControllerNode(new NodeId(member.getUuid()), | ||
85 | + IpPrefix.valueOf(member.getSocketAddress().getAddress().getAddress())); | ||
86 | + } | ||
87 | +} |
-
Please register or login to post a comment