Phanendra Manda | 37b97fb | 2015-08-15 02:04:24 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Phanendra Manda | 37b97fb | 2015-08-15 02:04:24 +0530 | [diff] [blame] | 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 | */ |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 16 | package org.onosproject.pcep.controller; |
Phanendra Manda | 37b97fb | 2015-08-15 02:04:24 +0530 | [diff] [blame] | 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.concurrent.atomic.AtomicInteger; |
| 21 | |
| 22 | import org.slf4j.Logger; |
| 23 | |
| 24 | /** |
| 25 | * Unique Srp Id generator for pcep messages. |
| 26 | */ |
| 27 | public final class SrpIdGenerators { |
| 28 | |
| 29 | private static final Logger log = getLogger(SrpIdGenerators.class); |
| 30 | private static final AtomicInteger SRP_ID_GEN = new AtomicInteger(); |
| 31 | private static final int MAX_SRP_ID = 0x7FFFFFFF; |
| 32 | private static int srpId; |
| 33 | |
| 34 | /** |
| 35 | * Default constructor. |
| 36 | */ |
| 37 | private SrpIdGenerators() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the next srp id. |
| 42 | * |
| 43 | * @return srp id |
| 44 | */ |
| 45 | public static int create() { |
| 46 | do { |
| 47 | if (srpId >= MAX_SRP_ID) { |
| 48 | if (SRP_ID_GEN.get() >= MAX_SRP_ID) { |
| 49 | SRP_ID_GEN.set(0); |
| 50 | } |
| 51 | } |
| 52 | srpId = SRP_ID_GEN.incrementAndGet(); |
| 53 | } while (srpId > MAX_SRP_ID); |
| 54 | return srpId; |
| 55 | } |
| 56 | } |