ReservationStep { public void execute() throws EnvironmentCreationException, PeerException { Set<Peer> peers = peerManager.resolve( topology.getAllPeers() ); final Map<Peer, UsedNetworkResources> reservedNetResources = Maps.newConcurrentMap(); for ( final Peer peer : peers ) { peerUtil.addPeerTask( new PeerUtil.PeerTask<>( peer, new Callable<Object>() { @Override public Object call() throws Exception { reservedNetResources.put( peer, peer.getUsedNetworkResources() ); return null; } } ) ); } PeerUtil.PeerTaskResults<Object> netQueryResults = peerUtil.executeParallel(); for ( PeerUtil.PeerTaskResult netQueryResult : netQueryResults.getResults() ) { if ( netQueryResult.hasSucceeded() ) { trackerOperation.addLog( String.format( "Obtained reserved network resources from peer %s", netQueryResult.getPeer().getName() ) ); } else { trackerOperation.addLog( String.format( "Failed to obtain reserved network resources from peer %s. Reason: %s", netQueryResult.getPeer().getName(), netQueryResult.getFailureReason() ) ); } } if ( netQueryResults.hasFailures() ) { throw new EnvironmentCreationException( "Failed to obtain reserved network resources from all peers" ); } Set<String> allP2pSubnets = Sets.newHashSet(); Set<String> allContainerSubnets = Sets.newHashSet(); Set<Long> allVnis = Sets.newHashSet(); for ( UsedNetworkResources netResources : reservedNetResources.values() ) { allContainerSubnets.addAll( netResources.getContainerSubnets() ); allP2pSubnets.addAll( netResources.getP2pSubnets() ); allVnis.addAll( netResources.getVnis() ); } final String freeContainerSubnet = P2PUtil.generateContainerSubnet( allContainerSubnets ); if ( freeContainerSubnet == null ) { throw new EnvironmentCreationException( "Free container subnet not found" ); } final String freeP2pSubnet = P2PUtil.generateP2PSubnet( allP2pSubnets ); if ( freeP2pSubnet == null ) { throw new EnvironmentCreationException( "Free p2p subnet not found" ); } final Long freeVni = generateRandomVni( allVnis ); if ( freeVni == -1 ) { throw new EnvironmentCreationException( "Free VNI not found" ); } for ( final Peer peer : peers ) { peerUtil.addPeerTask( new PeerUtil.PeerTask<>( peer, new Callable<Object>() { @Override public Integer call() throws Exception { NetworkResourceImpl networkResource = new NetworkResourceImpl( environment.getId(), freeVni, freeP2pSubnet, freeContainerSubnet, peerManager.getLocalPeer().getId(), identityManager.getActiveUser().getUserName(), identityManager.getActiveUser().getSecurityKeyId()); return peer.reserveNetworkResource( networkResource ); } } ) ); } PeerUtil.PeerTaskResults<Object> netReservationResults = peerUtil.executeParallel(); for ( PeerUtil.PeerTaskResult<Object> netReservationResult : netReservationResults.getResults() ) { if ( netReservationResult.hasSucceeded() ) { trackerOperation.addLog( String.format( "Reserved network resources on peer %s", netReservationResult.getPeer().getName() ) ); environment.addEnvironmentPeer( new EnvironmentPeerImpl( netReservationResult.getPeer().getId(), ( Integer ) netReservationResult.getResult() ) ); } else { trackerOperation.addLog( String.format( "Failed to reserve network resources on peer %s. Reason: %s", netReservationResult.getPeer().getName(), netReservationResult.getFailureReason() ) ); } } if ( netReservationResults.hasFailures() ) { throw new EnvironmentCreationException( "Failed to reserve network resources on all peers" ); } environment.setVni( freeVni ); environment.setP2PSubnet( freeP2pSubnet ); environment.setSubnetCidr( String.format( "%s/24", freeContainerSubnet ) ); } ReservationStep( final Topology topology, final LocalEnvironment environment, final PeerManager peerManager,
                            final IdentityManager identityManager, final TrackerOperation trackerOperation ); void execute();  }
@Test( expected = EnvironmentCreationException.class ) public void testExecute() throws Exception { doReturn( 123 ).when( peerTaskResult ).getResult(); doReturn( true ).when( peerTaskResult ).hasSucceeded(); step.execute(); verify( environment ).setP2PSubnet( anyString() ); doReturn( false ).when( peerTaskResult ).hasSucceeded(); doReturn( true ).when( peerTaskResults ).hasFailures(); step.execute(); }