Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.device.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.concurrent.ConcurrentHashMap; |
| 21 | import java.util.concurrent.ConcurrentMap; |
| 22 | import java.util.concurrent.atomic.AtomicInteger; |
| 23 | |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 28 | import org.onlab.onos.mastership.MastershipTerm; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.DeviceId; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.device.DeviceClockProviderService; |
| 31 | import org.onlab.onos.net.device.DeviceClockService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 32 | import org.onlab.onos.store.Timestamp; |
Yuta HIGUCHI | 8ce0873 | 2014-10-11 10:40:45 -0700 | [diff] [blame] | 33 | import org.onlab.onos.store.impl.MastershipBasedTimestamp; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Clock service to issue Timestamp based on Device Mastership. |
| 38 | */ |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 39 | @Component(immediate = true) |
| 40 | @Service |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 41 | public class DeviceClockManager implements DeviceClockService, DeviceClockProviderService { |
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 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | // TODO: Implement per device ticker that is reset to 0 at the beginning of a new term. |
| 46 | private final AtomicInteger ticker = new AtomicInteger(0); |
| 47 | private ConcurrentMap<DeviceId, MastershipTerm> deviceMastershipTerms = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 48 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 49 | @Activate |
| 50 | public void activate() { |
| 51 | log.info("Started"); |
| 52 | } |
| 53 | |
| 54 | @Deactivate |
| 55 | public void deactivate() { |
| 56 | log.info("Stopped"); |
| 57 | } |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 58 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 59 | @Override |
| 60 | public Timestamp getTimestamp(DeviceId deviceId) { |
| 61 | MastershipTerm term = deviceMastershipTerms.get(deviceId); |
Yuta HIGUCHI | c2996f9 | 2014-10-19 01:19:20 -0700 | [diff] [blame] | 62 | log.trace("term info for {} is: {}", deviceId, term); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 63 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 64 | if (term == null) { |
| 65 | throw new IllegalStateException("Requesting timestamp for a deviceId without mastership"); |
| 66 | } |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 67 | return new MastershipBasedTimestamp(term.termNumber(), ticker.incrementAndGet()); |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void setMastershipTerm(DeviceId deviceId, MastershipTerm term) { |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 72 | log.info("adding term info {} {}", deviceId, term.master()); |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 73 | deviceMastershipTerms.put(deviceId, term); |
| 74 | } |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public boolean isTimestampAvailable(DeviceId deviceId) { |
| 78 | return deviceMastershipTerms.containsKey(deviceId); |
| 79 | } |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 80 | } |