blob: 486d41e86859924edc33e94acd042f268dc09d9b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 Jampani61056bc2014-09-27 09:07:26 -070016package org.onlab.onos.store.device.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.concurrent.ConcurrentHashMap;
21import java.util.concurrent.ConcurrentMap;
22import java.util.concurrent.atomic.AtomicInteger;
23
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;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070028import org.onlab.onos.mastership.MastershipTerm;
Madan Jampani61056bc2014-09-27 09:07:26 -070029import org.onlab.onos.net.DeviceId;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070030import org.onlab.onos.net.device.DeviceClockProviderService;
31import org.onlab.onos.net.device.DeviceClockService;
Madan Jampani61056bc2014-09-27 09:07:26 -070032import org.onlab.onos.store.Timestamp;
Yuta HIGUCHI8ce08732014-10-11 10:40:45 -070033import org.onlab.onos.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.
46 private final AtomicInteger ticker = new AtomicInteger(0);
47 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) {
65 throw new IllegalStateException("Requesting timestamp for a deviceId without mastership");
66 }
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) {
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070072 log.info("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}