blob: 1dbe8c8fbfed76068cc4216abfb7bdc7655aa73e [file] [log] [blame]
Bharat saraswal7997bb92015-11-29 16:27:07 +05301/*
2 * Copyright 2015 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.onosproject.sfc.manager;
17
18import java.util.concurrent.atomic.AtomicInteger;
19
20/**
21 * Unique NSH SPI Id generator for NSH header.
22 */
23public final class NshSpiIdGenerators {
24
25 private static final AtomicInteger NSH_SPI_ID_GEN = new AtomicInteger();
26 private static final int MAX_NSH_SPI_ID = 0x7FFFFFFF;
27 private static int nshSpiId;
28
29 /**
30 * Default constructor.
31 */
32 private NshSpiIdGenerators() {
33 }
34
35 /**
36 * Get the next NSH SPI id.
37 *
38 * @return NSH SPI id
39 */
40 public static int create() {
41 do {
42 if (nshSpiId >= MAX_NSH_SPI_ID) {
43 if (NSH_SPI_ID_GEN.get() >= MAX_NSH_SPI_ID) {
44 NSH_SPI_ID_GEN.set(0);
45 }
46 }
47 nshSpiId = NSH_SPI_ID_GEN.incrementAndGet();
48 } while (nshSpiId > MAX_NSH_SPI_ID);
49 return nshSpiId;
50 }
51}