### Question:
TransitVlanPool implements EncapsulationResourcesProvider<TransitVlanEncapsulation> { @Override public TransitVlanEncapsulation allocate(Flow flow, PathId pathId, PathId oppositePathId) { return get(oppositePathId, null) .orElseGet(() -> allocate(flow, pathId)); } TransitVlanPool(PersistenceManager persistenceManager, int minTransitVlan, int maxTransitVlan,
                           int poolSize); @Override TransitVlanEncapsulation allocate(Flow flow, PathId pathId, PathId oppositePathId); @Override void deallocate(PathId pathId); @Override Optional<TransitVlanEncapsulation> get(PathId pathId, PathId oppositePathId);  }### Answer:
@Test public void vlanPoolTest() { transactionManager.doInTransaction(() -> { Set<Integer> transitVlans = new HashSet<>(); for (int i = MIN_TRANSIT_VLAN; i <= MAX_TRANSIT_VLAN; i++) { Flow flow = Flow.builder() .flowId(format("flow_%d", i)).srcSwitch(SWITCH_A).destSwitch(SWITCH_B).build(); transitVlans.add(transitVlanPool.allocate( flow, new PathId(format("path_%d", i)), new PathId(format("opposite_dummy_%d", i))).getTransitVlan().getVlan()); } assertEquals(MAX_TRANSIT_VLAN - MIN_TRANSIT_VLAN + 1, transitVlans.size()); transitVlans.forEach(vlan -> assertTrue(vlan >= MIN_TRANSIT_VLAN && vlan <= MAX_TRANSIT_VLAN)); }); }
@Test(expected = ResourceNotAvailableException.class) public void vlanPoolFullTest() { transactionManager.doInTransaction(() -> { for (int i = MIN_TRANSIT_VLAN; i <= MAX_TRANSIT_VLAN + 1; i++) { Flow flow = Flow.builder() .flowId(format("flow_%d", i)).srcSwitch(SWITCH_A).destSwitch(SWITCH_B).build(); assertTrue(transitVlanPool.allocate( flow, new PathId(format("path_%d", i)), new PathId(format("opposite_dummy_%d", i))).getTransitVlan().getVlan() > 0); } }); }
@Test public void gotSameVlanForOppositePath() { Flow flow = Flow.builder().flowId("flow_1").srcSwitch(SWITCH_A).destSwitch(SWITCH_B).build(); final PathId forwardPathId = new PathId("forward"); final PathId reversePathId = new PathId("reverse"); transactionManager.doInTransaction(() -> { TransitVlan forward = transitVlanPool.allocate(flow, forwardPathId, reversePathId) .getTransitVlan(); TransitVlan reverse = transitVlanPool.allocate(flow, reversePathId, forwardPathId) .getTransitVlan(); Assert.assertEquals(forward, reverse); }); }