blob: 260cef1e6b547256a28fd75dc2be2a4432f19347 [file] [log] [blame]
Brian O'Connor520c0522014-11-23 23:50:47 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Brian O'Connor520c0522014-11-23 23:50:47 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.core.impl;
Brian O'Connor520c0522014-11-23 23:50:47 -080017
18import com.hazelcast.core.HazelcastInstance;
19import com.hazelcast.core.IAtomicLong;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.IdBlock;
26import org.onosproject.core.IdBlockStore;
27import org.onosproject.store.hz.StoreService;
Brian O'Connor520c0522014-11-23 23:50:47 -080028
29import java.util.Map;
30
31/**
32 * Distributed implementation of id block store using Hazelcast.
33 */
Madan Jampani04aeb452015-05-02 16:12:24 -070034@Component(immediate = false, enabled = false)
Brian O'Connor520c0522014-11-23 23:50:47 -080035@Service
36public class DistributedIdBlockStore implements IdBlockStore {
37
Brian O'Connorfd079912015-02-18 20:50:59 -080038 private static final long DEFAULT_BLOCK_SIZE = 0x100000L;
Brian O'Connor520c0522014-11-23 23:50:47 -080039
40 protected Map<String, IAtomicLong> topicBlocks;
41
42 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
43 protected StoreService storeService;
44
45 protected HazelcastInstance theInstance;
46
47 @Activate
48 public void activate() {
49 theInstance = storeService.getHazelcastInstance();
50 }
51
52 @Override
53 public IdBlock getIdBlock(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -080054 Long blockBase = theInstance.getAtomicLong(topic).getAndAdd(DEFAULT_BLOCK_SIZE);
55 return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
56 }
57}