Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 16 | package org.onosproject.store.core.impl; |
| 17 | |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 19 | |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 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.core.IdBlock; |
| 27 | import org.onosproject.core.IdBlockStore; |
| 28 | import org.onosproject.store.service.AtomicCounter; |
| 29 | import org.onosproject.store.service.StorageService; |
| 30 | import org.slf4j.Logger; |
| 31 | |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 32 | import java.util.Map; |
| 33 | |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 34 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Implementation of {@code IdBlockStore} using {@code AtomicCounter}. |
| 38 | */ |
Sho SHIMIZU | 5c396e3 | 2016-08-12 15:19:12 -0700 | [diff] [blame] | 39 | @Component(immediate = true) |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 40 | @Service |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 41 | public class DistributedIdBlockStore implements IdBlockStore { |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap(); |
| 45 | |
| 46 | private static final long DEFAULT_BLOCK_SIZE = 0x100000L; |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected StorageService storageService; |
| 50 | |
| 51 | @Activate |
| 52 | public void activate() { |
| 53 | log.info("Started"); |
| 54 | } |
| 55 | |
| 56 | @Deactivate |
| 57 | public void deactivate() { |
| 58 | log.info("Stopped"); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public IdBlock getIdBlock(String topic) { |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 63 | AtomicCounter counter = topicCounters.computeIfAbsent(topic, storageService::getAtomicCounter); |
| 64 | return new IdBlock(counter.getAndAdd(DEFAULT_BLOCK_SIZE), DEFAULT_BLOCK_SIZE); |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 65 | } |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 66 | } |