blob: 8d72e51061ff9f9a65f230a11405946d5c2e3f41 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.device.impl;
Madan Jampani61056bc2014-09-27 09:07:26 -070017
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.concurrent.ConcurrentHashMap;
21import java.util.concurrent.ConcurrentMap;
Madan Jampani15d773c2015-02-25 15:31:55 -080022import java.util.concurrent.atomic.AtomicLong;
Madan Jampani61056bc2014-09-27 09:07:26 -070023
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.mastership.MastershipTerm;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.device.DeviceClockProviderService;
31import org.onosproject.net.device.DeviceClockService;
32import org.onosproject.store.Timestamp;
33import org.onosproject.store.impl.MastershipBasedTimestamp;
Madan Jampani61056bc2014-09-27 09:07:26 -070034import org.slf4j.Logger;
35
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070036/**
37 * Clock service to issue Timestamp based on Device Mastership.
38 */
Madan Jampani61056bc2014-09-27 09:07:26 -070039@Component(immediate = true)
40@Service
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070041public class DeviceClockManager implements DeviceClockService, DeviceClockProviderService {
Yuta HIGUCHI2e963892014-09-27 13:00:39 -070042
Madan Jampani61056bc2014-09-27 09:07:26 -070043 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.
Madan Jampani15d773c2015-02-25 15:31:55 -080046 private final AtomicLong ticker = new AtomicLong(0);
Madan Jampani61056bc2014-09-27 09:07:26 -070047 private ConcurrentMap<DeviceId, MastershipTerm> deviceMastershipTerms = new ConcurrentHashMap<>();
Yuta HIGUCHI2e963892014-09-27 13:00:39 -070048
Madan Jampani61056bc2014-09-27 09:07:26 -070049 @Activate
50 public void activate() {
51 log.info("Started");
52 }
53
54 @Deactivate
55 public void deactivate() {
56 log.info("Stopped");
57 }
Yuta HIGUCHI2e963892014-09-27 13:00:39 -070058
Madan Jampani61056bc2014-09-27 09:07:26 -070059 @Override
60 public Timestamp getTimestamp(DeviceId deviceId) {
61 MastershipTerm term = deviceMastershipTerms.get(deviceId);
Yuta HIGUCHIc2996f92014-10-19 01:19:20 -070062 log.trace("term info for {} is: {}", deviceId, term);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070063
Madan Jampani61056bc2014-09-27 09:07:26 -070064 if (term == null) {
HIGUCHI Yuta3a6f7cd2015-02-25 18:51:25 -080065 throw new IllegalStateException("Requesting timestamp for " + deviceId + " without mastership");
Madan Jampani61056bc2014-09-27 09:07:26 -070066 }
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070067 return new MastershipBasedTimestamp(term.termNumber(), ticker.incrementAndGet());
Madan Jampani61056bc2014-09-27 09:07:26 -070068 }
69
70 @Override
71 public void setMastershipTerm(DeviceId deviceId, MastershipTerm term) {
Madan Jampanif2af7712015-05-29 18:43:52 -070072 log.debug("adding term info {} {}", deviceId, term.master());
Madan Jampani61056bc2014-09-27 09:07:26 -070073 deviceMastershipTerms.put(deviceId, term);
74 }
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -070075
76 @Override
77 public boolean isTimestampAvailable(DeviceId deviceId) {
78 return deviceMastershipTerms.containsKey(deviceId);
79 }
Madan Jampani61056bc2014-09-27 09:07:26 -070080}