blob: 9b73c3d13a86998a29d5b0dfe70c6bf45b962ef4 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
Madan Jampani04aeb452015-05-02 16:12:24 -070016package org.onosproject.store.core.impl;
17
Thomas Vachuskad0d58542015-06-03 12:38:44 -070018import com.google.common.collect.Maps;
Madan Jampania29c6772015-08-17 13:17:07 -070019
Madan Jampani04aeb452015-05-02 16:12:24 -070020import 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;
26import org.onosproject.core.IdBlock;
27import org.onosproject.core.IdBlockStore;
28import org.onosproject.store.service.AtomicCounter;
29import org.onosproject.store.service.StorageService;
30import org.slf4j.Logger;
31
Thomas Vachuskad0d58542015-06-03 12:38:44 -070032import java.util.Map;
33
Thomas Vachuskad0d58542015-06-03 12:38:44 -070034import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani04aeb452015-05-02 16:12:24 -070035
36/**
37 * Implementation of {@code IdBlockStore} using {@code AtomicCounter}.
38 */
Sho SHIMIZU5c396e32016-08-12 15:19:12 -070039@Component(immediate = true)
Madan Jampani04aeb452015-05-02 16:12:24 -070040@Service
Madan Jampani78be2492016-06-03 23:27:07 -070041public class DistributedIdBlockStore implements IdBlockStore {
Madan Jampani04aeb452015-05-02 16:12:24 -070042
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 Jampanid5714e02016-04-19 14:15:20 -070063 AtomicCounter counter = topicCounters.computeIfAbsent(topic, storageService::getAtomicCounter);
64 return new IdBlock(counter.getAndAdd(DEFAULT_BLOCK_SIZE), DEFAULT_BLOCK_SIZE);
Madan Jampani04aeb452015-05-02 16:12:24 -070065 }
Madan Jampani04aeb452015-05-02 16:12:24 -070066}