blob: fd138605b7ae4f23bc504dfb2d0cde89ddae46fd [file] [log] [blame]
Brian O'Connor520c0522014-11-23 23:50:47 -08001/*
2 * Copyright 2014 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 */
16package org.onlab.onos.store.core.impl;
17
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;
25import org.onlab.onos.core.IdBlock;
26import org.onlab.onos.core.IdBlockStore;
27import org.onlab.onos.store.hz.StoreService;
28
29import java.util.Map;
30
31/**
32 * Distributed implementation of id block store using Hazelcast.
33 */
34@Component(immediate = true)
35@Service
36public class DistributedIdBlockStore implements IdBlockStore {
37
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080038 private static final long DEFAULT_BLOCK_SIZE = 0x1000L;
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) {
54 //TODO need to persist this value across cluster failures
55 Long blockBase = theInstance.getAtomicLong(topic).getAndAdd(DEFAULT_BLOCK_SIZE);
56 return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
57 }
58}