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