blob: 744e53b5269183988979fec7dd2a3a4e2ba46456 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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 Jampani04aeb452015-05-02 16:12:24 -070019import org.onosproject.core.IdBlock;
20import org.onosproject.core.IdBlockStore;
21import org.onosproject.store.service.AtomicCounter;
22import org.onosproject.store.service.StorageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.osgi.service.component.annotations.Activate;
24import org.osgi.service.component.annotations.Component;
25import org.osgi.service.component.annotations.Deactivate;
26import org.osgi.service.component.annotations.Reference;
27import org.osgi.service.component.annotations.ReferenceCardinality;
Madan Jampani04aeb452015-05-02 16:12:24 -070028import org.slf4j.Logger;
29
Thomas Vachuskad0d58542015-06-03 12:38:44 -070030import java.util.Map;
31
Thomas Vachuskad0d58542015-06-03 12:38:44 -070032import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani04aeb452015-05-02 16:12:24 -070033
34/**
35 * Implementation of {@code IdBlockStore} using {@code AtomicCounter}.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Component(immediate = true, service = IdBlockStore.class)
Madan Jampani78be2492016-06-03 23:27:07 -070038public class DistributedIdBlockStore implements IdBlockStore {
Madan Jampani04aeb452015-05-02 16:12:24 -070039
40 private final Logger log = getLogger(getClass());
41 private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap();
42
43 private static final long DEFAULT_BLOCK_SIZE = 0x100000L;
44
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Madan Jampani04aeb452015-05-02 16:12:24 -070046 protected StorageService storageService;
47
48 @Activate
49 public void activate() {
50 log.info("Started");
51 }
52
53 @Deactivate
54 public void deactivate() {
55 log.info("Stopped");
56 }
57
58 @Override
59 public IdBlock getIdBlock(String topic) {
Madan Jampanid5714e02016-04-19 14:15:20 -070060 AtomicCounter counter = topicCounters.computeIfAbsent(topic, storageService::getAtomicCounter);
61 return new IdBlock(counter.getAndAdd(DEFAULT_BLOCK_SIZE), DEFAULT_BLOCK_SIZE);
Madan Jampani04aeb452015-05-02 16:12:24 -070062 }
Madan Jampani04aeb452015-05-02 16:12:24 -070063}