Committed by
Gerrit Code Review
[ONOS-3119] NSH spi ID generator for flow classifier installer.
Change-Id: Id2d7071128cd21fb126e8ee2538266ad570c4d2c
Showing
1 changed file
with
51 additions
and
0 deletions
1 | +/* | ||
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 | + */ | ||
16 | +package org.onosproject.sfc.manager; | ||
17 | + | ||
18 | +import java.util.concurrent.atomic.AtomicInteger; | ||
19 | + | ||
20 | +/** | ||
21 | + * Unique NSH SPI Id generator for NSH header. | ||
22 | + */ | ||
23 | +public 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 | +} |
-
Please register or login to post a comment