blob: 14a09c26333d2b91e4c4084a35098a98d6fffc38 [file] [log] [blame]
Ray Milkey9ed4b962014-08-20 15:43:40 -07001package net.onrc.onos.core.matchaction;
2
3import net.onrc.onos.core.util.IdBlock;
4import net.onrc.onos.core.util.IdBlockAllocator;
5import net.onrc.onos.core.util.UnavailableIdException;
6
7import static com.google.common.base.Preconditions.checkNotNull;
8
9/**
Sho SHIMIZUafc793e2014-08-27 15:22:09 -070010 * Generates a global unique MatchActionId.
Ray Milkey9ed4b962014-08-20 15:43:40 -070011 */
12public class MatchActionIdGeneratorWithIdBlockAllocator
13 implements MatchActionIdGenerator {
14
Sho SHIMIZUd4683b62014-08-27 15:19:09 -070015 private final IdBlockAllocator allocator;
16 private IdBlock idBlock;
Ray Milkey9ed4b962014-08-20 15:43:40 -070017
Sho SHIMIZUd4683b62014-08-27 15:19:09 -070018 /**
19 * Creates a FlowId generator instance using specified ID block allocator.
20 *
21 * @param allocator the ID block allocator to be used
22 */
23 public MatchActionIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
24 this.allocator = checkNotNull(allocator);
25 this.idBlock = allocator.allocateUniqueIdBlock();
26 }
Ray Milkey9ed4b962014-08-20 15:43:40 -070027
Sho SHIMIZUd4683b62014-08-27 15:19:09 -070028 @Override
29 public synchronized MatchActionId getNewId() {
30 try {
31 return new MatchActionId(idBlock.getNextId());
32 } catch (UnavailableIdException e) {
33 idBlock = allocator.allocateUniqueIdBlock();
34 return new MatchActionId(idBlock.getNextId());
Ray Milkey9ed4b962014-08-20 15:43:40 -070035 }
Sho SHIMIZUd4683b62014-08-27 15:19:09 -070036 }
Ray Milkey9ed4b962014-08-20 15:43:40 -070037}