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; |
| 6 | import java.util.Set; |
| 7 | import java.util.concurrent.ConcurrentHashMap; |
| 8 | import java.util.concurrent.ConcurrentMap; |
| 9 | |
| 10 | import org.apache.felix.scr.annotations.Activate; |
| 11 | import org.apache.felix.scr.annotations.Component; |
| 12 | import org.apache.felix.scr.annotations.Deactivate; |
| 13 | import org.apache.felix.scr.annotations.Service; |
| 14 | import org.onlab.onos.cluster.ControllerNode; |
| 15 | import org.onlab.onos.cluster.DefaultControllerNode; |
| 16 | import org.onlab.onos.cluster.MastershipEvent; |
| 17 | import org.onlab.onos.cluster.MastershipStore; |
| 18 | import org.onlab.onos.cluster.NodeId; |
| 19 | import org.onlab.onos.net.DeviceId; |
| 20 | import org.onlab.onos.net.MastershipRole; |
| 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.slf4j.Logger; |
| 23 | |
| 24 | import static org.onlab.onos.cluster.MastershipEvent.Type.*; |
| 25 | |
| 26 | /** |
| 27 | * Manages inventory of controller mastership over devices using |
| 28 | * trivial in-memory structures implementation. |
| 29 | */ |
| 30 | @Component(immediate = true) |
| 31 | @Service |
| 32 | public class SimpleMastershipStore implements MastershipStore { |
| 33 | |
| 34 | public static final IpPrefix LOCALHOST = IpPrefix.valueOf("127.0.0.1"); |
| 35 | |
| 36 | private final Logger log = getLogger(getClass()); |
| 37 | |
| 38 | private ControllerNode instance; |
| 39 | |
| 40 | protected final ConcurrentMap<DeviceId, MastershipRole> roleMap = |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame^] | 41 | new ConcurrentHashMap<>(); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 42 | |
| 43 | @Activate |
| 44 | public void activate() { |
| 45 | instance = new DefaultControllerNode(new NodeId("local"), LOCALHOST); |
| 46 | log.info("Started"); |
| 47 | } |
| 48 | |
| 49 | @Deactivate |
| 50 | public void deactivate() { |
| 51 | log.info("Stopped"); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public MastershipEvent setRole(NodeId nodeId, DeviceId deviceId, |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame^] | 56 | MastershipRole role) { |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 57 | if (roleMap.get(deviceId) == null) { |
| 58 | return null; |
| 59 | } |
| 60 | roleMap.put(deviceId, role); |
| 61 | return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); |
| 62 | } |
| 63 | |
| 64 | @Override |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 65 | public NodeId getMaster(DeviceId deviceId) { |
| 66 | return instance.id(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Set<DeviceId> getDevices(NodeId nodeId) { |
| 71 | return Collections.unmodifiableSet(roleMap.keySet()); |
| 72 | } |
| 73 | |
| 74 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame^] | 75 | public MastershipRole requestRole(DeviceId deviceId) { |
| 76 | return getRole(instance.id(), deviceId); |
| 77 | } |
| 78 | |
| 79 | @Override |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 80 | public MastershipRole getRole(NodeId nodeId, DeviceId deviceId) { |
| 81 | MastershipRole role = roleMap.get(deviceId); |
| 82 | if (role == null) { |
| 83 | //say MASTER. If clustered, we'd figure out if anyone's got dibs here. |
| 84 | role = MastershipRole.MASTER; |
| 85 | roleMap.put(deviceId, role); |
| 86 | } |
| 87 | return role; |
| 88 | } |
| 89 | |
| 90 | } |