Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.device.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.concurrent.ConcurrentHashMap; |
| 6 | import java.util.concurrent.ConcurrentMap; |
| 7 | import java.util.concurrent.atomic.AtomicInteger; |
| 8 | |
| 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
| 12 | import org.apache.felix.scr.annotations.Service; |
| 13 | import org.onlab.onos.cluster.MastershipTerm; |
| 14 | import org.onlab.onos.net.DeviceId; |
Yuta HIGUCHI | 68b3494 | 2014-10-02 23:23:21 -0700 | [diff] [blame] | 15 | import org.onlab.onos.store.ClockProviderService; |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 16 | import org.onlab.onos.store.ClockService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 17 | import org.onlab.onos.store.Timestamp; |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 18 | import org.onlab.onos.store.common.impl.MastershipBasedTimestamp; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Clock service to issue Timestamp based on Device Mastership. |
| 23 | */ |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 24 | @Component(immediate = true) |
| 25 | @Service |
Yuta HIGUCHI | 68b3494 | 2014-10-02 23:23:21 -0700 | [diff] [blame] | 26 | public class DeviceClockManager implements ClockService, ClockProviderService { |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 27 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 28 | private final Logger log = getLogger(getClass()); |
| 29 | |
| 30 | // TODO: Implement per device ticker that is reset to 0 at the beginning of a new term. |
| 31 | private final AtomicInteger ticker = new AtomicInteger(0); |
| 32 | private ConcurrentMap<DeviceId, MastershipTerm> deviceMastershipTerms = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 33 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 34 | @Activate |
| 35 | public void activate() { |
| 36 | log.info("Started"); |
| 37 | } |
| 38 | |
| 39 | @Deactivate |
| 40 | public void deactivate() { |
| 41 | log.info("Stopped"); |
| 42 | } |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 43 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 44 | @Override |
| 45 | public Timestamp getTimestamp(DeviceId deviceId) { |
| 46 | MastershipTerm term = deviceMastershipTerms.get(deviceId); |
| 47 | if (term == null) { |
| 48 | throw new IllegalStateException("Requesting timestamp for a deviceId without mastership"); |
| 49 | } |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 50 | return new MastershipBasedTimestamp(term.termNumber(), ticker.incrementAndGet()); |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void setMastershipTerm(DeviceId deviceId, MastershipTerm term) { |
| 55 | deviceMastershipTerms.put(deviceId, term); |
| 56 | } |
| 57 | } |