blob: 4b2f7808aa267ec99fea7b83c8257e0aec65783a [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 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 Jampani3e033bd2015-04-08 13:03:49 -070016package org.onosproject.store.core.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Madan Jampanif4c88502016-01-21 12:35:36 -080026import org.onosproject.store.LogicalTimestamp;
Madan Jampani3e033bd2015-04-08 13:03:49 -070027import org.onosproject.store.Timestamp;
Madan Jampani3e033bd2015-04-08 13:03:49 -070028import org.onosproject.store.service.AtomicCounter;
29import org.onosproject.store.service.LogicalClockService;
30import org.onosproject.store.service.StorageService;
31import org.slf4j.Logger;
32
33/**
34 * LogicalClockService implementation based on a AtomicCounter.
35 */
36@Component(immediate = true, enabled = true)
37@Service
38public class LogicalClockManager implements LogicalClockService {
39
40 private final Logger log = getLogger(getClass());
41
42 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
43 protected StorageService storageService;
44
45 private static final String SYSTEM_LOGICAL_CLOCK_COUNTER_NAME = "sys-clock-counter";
46 private AtomicCounter atomicCounter;
47
48 @Activate
49 public void activate() {
50 atomicCounter = storageService.atomicCounterBuilder()
51 .withName(SYSTEM_LOGICAL_CLOCK_COUNTER_NAME)
52 .withPartitionsDisabled()
Madan Jampanie17d3282016-02-03 15:30:57 -080053 .build()
54 .asAtomicCounter();
Madan Jampani5756c352015-04-29 00:23:58 -070055 log.info("Started");
Madan Jampani3e033bd2015-04-08 13:03:49 -070056 }
57
58 @Deactivate
59 public void deactivate() {
Madan Jampani5756c352015-04-29 00:23:58 -070060 log.info("Stopped");
Madan Jampani3e033bd2015-04-08 13:03:49 -070061 }
62
63 @Override
64 public Timestamp getTimestamp() {
65 return new LogicalTimestamp(atomicCounter.incrementAndGet());
66 }
67}