Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.Collections; |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 6 | import java.util.HashMap; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 7 | import java.util.HashSet; |
| 8 | import java.util.Map; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 9 | import java.util.Set; |
| 10 | import java.util.concurrent.ConcurrentHashMap; |
| 11 | import java.util.concurrent.ConcurrentMap; |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 12 | import java.util.concurrent.atomic.AtomicInteger; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 13 | |
| 14 | import org.apache.felix.scr.annotations.Activate; |
| 15 | import org.apache.felix.scr.annotations.Component; |
| 16 | import org.apache.felix.scr.annotations.Deactivate; |
| 17 | import org.apache.felix.scr.annotations.Service; |
| 18 | import org.onlab.onos.cluster.ControllerNode; |
| 19 | import org.onlab.onos.cluster.DefaultControllerNode; |
| 20 | import org.onlab.onos.cluster.MastershipEvent; |
| 21 | import org.onlab.onos.cluster.MastershipStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 22 | import org.onlab.onos.cluster.MastershipStoreDelegate; |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 23 | import org.onlab.onos.cluster.MastershipTerm; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 24 | import org.onlab.onos.cluster.NodeId; |
| 25 | import org.onlab.onos.net.DeviceId; |
| 26 | import org.onlab.onos.net.MastershipRole; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 27 | import org.onlab.onos.store.AbstractStore; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.slf4j.Logger; |
| 30 | |
| 31 | import static org.onlab.onos.cluster.MastershipEvent.Type.*; |
| 32 | |
| 33 | /** |
| 34 | * Manages inventory of controller mastership over devices using |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 35 | * trivial, non-distributed in-memory structures implementation. |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 36 | */ |
| 37 | @Component(immediate = true) |
| 38 | @Service |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 39 | public class SimpleMastershipStore |
| 40 | extends AbstractStore<MastershipEvent, MastershipStoreDelegate> |
| 41 | implements MastershipStore { |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 42 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 45 | public static final IpPrefix LOCALHOST = IpPrefix.valueOf("127.0.0.1"); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 46 | |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 47 | private ControllerNode instance = |
| 48 | new DefaultControllerNode(new NodeId("local"), LOCALHOST); |
| 49 | |
| 50 | //devices mapped to their masters, to emulate multiple nodes |
| 51 | protected final ConcurrentMap<DeviceId, NodeId> masterMap = |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 52 | new ConcurrentHashMap<>(); |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 53 | protected final Map<DeviceId, AtomicInteger> termMap = new HashMap<>(); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 54 | |
| 55 | @Activate |
| 56 | public void activate() { |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 57 | log.info("Started"); |
| 58 | } |
| 59 | |
| 60 | @Deactivate |
| 61 | public void deactivate() { |
| 62 | log.info("Stopped"); |
| 63 | } |
| 64 | |
| 65 | @Override |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 66 | public MastershipEvent setMaster(NodeId nodeId, DeviceId deviceId) { |
| 67 | |
| 68 | NodeId node = masterMap.get(deviceId); |
| 69 | if (node == null) { |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 70 | synchronized (this) { |
| 71 | masterMap.put(deviceId, nodeId); |
| 72 | termMap.put(deviceId, new AtomicInteger()); |
| 73 | } |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 74 | return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 75 | } |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 76 | |
| 77 | if (node.equals(nodeId)) { |
| 78 | return null; |
| 79 | } else { |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 80 | synchronized (this) { |
| 81 | masterMap.put(deviceId, nodeId); |
| 82 | termMap.get(deviceId).incrementAndGet(); |
| 83 | return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); |
| 84 | } |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 85 | } |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 89 | public NodeId getMaster(DeviceId deviceId) { |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 90 | return masterMap.get(deviceId); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Set<DeviceId> getDevices(NodeId nodeId) { |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 95 | Set<DeviceId> ids = new HashSet<>(); |
| 96 | for (Map.Entry<DeviceId, NodeId> d : masterMap.entrySet()) { |
| 97 | if (d.getValue().equals(nodeId)) { |
| 98 | ids.add(d.getKey()); |
| 99 | } |
| 100 | } |
| 101 | return Collections.unmodifiableSet(ids); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 105 | public MastershipRole requestRole(DeviceId deviceId) { |
| 106 | return getRole(instance.id(), deviceId); |
| 107 | } |
| 108 | |
| 109 | @Override |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 110 | public MastershipRole getRole(NodeId nodeId, DeviceId deviceId) { |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 111 | NodeId node = masterMap.get(deviceId); |
| 112 | MastershipRole role; |
| 113 | if (node != null) { |
| 114 | if (node.equals(nodeId)) { |
| 115 | role = MastershipRole.MASTER; |
| 116 | } else { |
| 117 | role = MastershipRole.STANDBY; |
| 118 | } |
| 119 | } else { |
| 120 | //masterMap doesn't contain it. |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 121 | role = MastershipRole.MASTER; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 122 | masterMap.put(deviceId, nodeId); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 123 | } |
| 124 | return role; |
| 125 | } |
| 126 | |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public MastershipTerm getTermFor(DeviceId deviceId) { |
| 129 | if (masterMap.get(deviceId) == null) { |
| 130 | return null; |
| 131 | } |
| 132 | return MastershipTerm.of( |
| 133 | masterMap.get(deviceId), termMap.get(deviceId).get()); |
| 134 | } |
| 135 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 136 | } |