blob: d192184b32fcecf56c955133ff20c579f0484721 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -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.core.impl;
Brian O'Connor520c0522014-11-23 23:50:47 -080017
Madan Jampani66feabf2015-06-05 12:24:20 -070018import java.util.concurrent.atomic.AtomicBoolean;
19
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.core.IdBlock;
21import org.onosproject.core.IdGenerator;
22import org.onosproject.core.UnavailableIdException;
Brian O'Connor520c0522014-11-23 23:50:47 -080023
Brian O'Connor390c9972015-06-24 16:08:09 -040024import static com.google.common.base.Preconditions.checkNotNull;
25
Brian O'Connor520c0522014-11-23 23:50:47 -080026/**
27 * Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as
28 * backend.
29 */
30public class BlockAllocatorBasedIdGenerator implements IdGenerator {
31 protected final IdBlockAllocator allocator;
Madan Jampanic80da082015-06-03 00:26:21 -070032 protected IdBlock idBlock;
Madan Jampani66feabf2015-06-05 12:24:20 -070033 protected AtomicBoolean initialized;
34
Brian O'Connor520c0522014-11-23 23:50:47 -080035
36 /**
37 * Constructs an ID generator which use {@link IdBlockAllocator} as backend.
38 *
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -080039 * @param allocator the ID block allocator to use
Brian O'Connor520c0522014-11-23 23:50:47 -080040 */
41 protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
Brian O'Connor390c9972015-06-24 16:08:09 -040042 this.allocator = checkNotNull(allocator, "allocator cannot be null");
Madan Jampani66feabf2015-06-05 12:24:20 -070043 this.initialized = new AtomicBoolean(false);
Brian O'Connor520c0522014-11-23 23:50:47 -080044 }
45
46 @Override
47 public long getNewId() {
48 try {
alshabib5f5d2812015-07-02 18:43:19 -070049 if (!initialized.get()) {
50 synchronized (allocator) {
51 if (!initialized.get()) {
52 idBlock = allocator.allocateUniqueIdBlock();
53 initialized.set(true);
54 }
55 }
56 }
Madan Jampanic80da082015-06-03 00:26:21 -070057 return idBlock.getNextId();
Brian O'Connor520c0522014-11-23 23:50:47 -080058 } catch (UnavailableIdException e) {
59 synchronized (allocator) {
Madan Jampanic80da082015-06-03 00:26:21 -070060 idBlock = allocator.allocateUniqueIdBlock();
Brian O'Connor520c0522014-11-23 23:50:47 -080061 }
Madan Jampani66feabf2015-06-05 12:24:20 -070062 return idBlock.getNextId();
Brian O'Connor520c0522014-11-23 23:50:47 -080063 }
64 }
65}