Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 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 Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 16 | package org.onosproject.store.core.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onosproject.store.Timestamp; |
| 27 | import org.onosproject.store.impl.LogicalTimestamp; |
| 28 | import org.onosproject.store.service.AtomicCounter; |
| 29 | import org.onosproject.store.service.LogicalClockService; |
| 30 | import org.onosproject.store.service.StorageService; |
| 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | /** |
| 34 | * LogicalClockService implementation based on a AtomicCounter. |
| 35 | */ |
| 36 | @Component(immediate = true, enabled = true) |
| 37 | @Service |
| 38 | public 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() |
| 53 | .build(); |
Madan Jampani | 5756c35 | 2015-04-29 00:23:58 -0700 | [diff] [blame] | 54 | log.info("Started"); |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Deactivate |
| 58 | public void deactivate() { |
Madan Jampani | 5756c35 | 2015-04-29 00:23:58 -0700 | [diff] [blame] | 59 | log.info("Stopped"); |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Timestamp getTimestamp() { |
| 64 | return new LogicalTimestamp(atomicCounter.incrementAndGet()); |
| 65 | } |
| 66 | } |