issue 384: NetworkSpec refactored - Adrian comments addressed

This commit is contained in:
andreaturli 2012-03-02 18:19:59 +00:00
parent edd16d0e26
commit 01fa802dd7
6 changed files with 35 additions and 43 deletions

View File

@ -29,8 +29,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
public class CloneSpec { public class CloneSpec {
private VmSpec vmSpec; private final VmSpec vmSpec;
private NetworkSpec networkSpec; private final NetworkSpec networkSpec;
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();

View File

@ -19,16 +19,16 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import org.virtualbox_4_1.NetworkAttachmentType; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Objects;
import com.google.common.collect.Sets;
import org.virtualbox_4_1.NATProtocol;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull; import org.virtualbox_4_1.NATProtocol;
import org.virtualbox_4_1.NetworkAttachmentType;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
/** /**
* Represents a network adapter in VirtualBox. * Represents a network adapter in VirtualBox.
@ -39,14 +39,14 @@ public class NetworkAdapter {
private final NetworkAttachmentType networkAttachmentType; private final NetworkAttachmentType networkAttachmentType;
private final String macAddress; private final String macAddress;
private final Set<RedirectRule> redirectRules = Sets.newLinkedHashSet(); private final Set<RedirectRule> redirectRules;
public NetworkAdapter(NetworkAttachmentType networkAttachmentType, public NetworkAdapter(NetworkAttachmentType networkAttachmentType,
String macAddress, Set<RedirectRule> redirectRules) { String macAddress, Set<RedirectRule> redirectRules) {
this.networkAttachmentType = checkNotNull(networkAttachmentType, this.networkAttachmentType = checkNotNull(networkAttachmentType,
"networkAttachmentType"); "networkAttachmentType");
this.macAddress = macAddress; this.macAddress = macAddress;
this.redirectRules.addAll(redirectRules); this.redirectRules = ImmutableSet.<RedirectRule>copyOf(checkNotNull(redirectRules, "redirectRules"));
} }
public static Builder builder() { public static Builder builder() {
@ -57,7 +57,7 @@ public class NetworkAdapter {
private NetworkAttachmentType networkAttachmentType; private NetworkAttachmentType networkAttachmentType;
private String macAddress; private String macAddress;
private Set<RedirectRule> redirectRules = Sets.newLinkedHashSet(); private Set<RedirectRule> redirectRules;
/** /**
* *

View File

@ -18,6 +18,8 @@
*/ */
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Objects; import com.google.common.base.Objects;
public class NetworkInterfaceCard { public class NetworkInterfaceCard {
@ -28,9 +30,9 @@ public class NetworkInterfaceCard {
public NetworkInterfaceCard(long slot, NetworkAdapter networkAdapter, String hostInterfaceName) { public NetworkInterfaceCard(long slot, NetworkAdapter networkAdapter, String hostInterfaceName) {
this.slot = slot; this.slot = checkNotNull(slot, "slot");
this.networkAdapter = networkAdapter; this.networkAdapter = checkNotNull(networkAdapter, "networkAdapter");
this.hostInterfaceName = hostInterfaceName; this.hostInterfaceName = checkNotNull(hostInterfaceName, "hostInterfaceName");
} }
public static Builder builder() { public static Builder builder() {

View File

@ -18,12 +18,14 @@
*/ */
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
/** /**
* Describes the network configuration for a VirtualBox machine. * Describes the network configuration for a VirtualBox machine.
@ -33,7 +35,7 @@ public class NetworkSpec {
private final List<NetworkInterfaceCard> networkInterfaceCards; private final List<NetworkInterfaceCard> networkInterfaceCards;
public NetworkSpec(final List<NetworkInterfaceCard> networkInterfaceCards) { public NetworkSpec(final List<NetworkInterfaceCard> networkInterfaceCards) {
this.networkInterfaceCards = checkNotNull(networkInterfaceCards, "networkInterfaceCards"); this.networkInterfaceCards = ImmutableList.copyOf(checkNotNull(networkInterfaceCards, "networkInterfaceCards"));
} }
public static Builder builder() { public static Builder builder() {
@ -44,26 +46,9 @@ public class NetworkSpec {
private List<NetworkInterfaceCard> networkInterfaceCards = new ArrayList<NetworkInterfaceCard>(); private List<NetworkInterfaceCard> networkInterfaceCards = new ArrayList<NetworkInterfaceCard>();
public Builder addNIC1(NetworkInterfaceCard networkInterfaceCard) { public Builder addNIC(long slot, NetworkInterfaceCard networkInterfaceCard) {
NetworkInterfaceCard nic = NetworkInterfaceCard.builder().slot(0L).addNetworkAdapter(networkInterfaceCard.getNetworkAdapter()).build(); checkArgument(slot >= 0 && slot < 4, "must be 0, 1, 2, 3: %s", slot);
this.networkInterfaceCards.add(nic); NetworkInterfaceCard nic = NetworkInterfaceCard.builder().slot(slot).addNetworkAdapter(networkInterfaceCard.getNetworkAdapter()).build();
return this;
}
public Builder addNIC2(NetworkInterfaceCard networkInterfaceCard) {
NetworkInterfaceCard nic = NetworkInterfaceCard.builder().slot(1L).addNetworkAdapter(networkInterfaceCard.getNetworkAdapter()).build();
this.networkInterfaceCards.add(nic);
return this;
}
public Builder addNIC3(NetworkInterfaceCard networkInterfaceCard) {
NetworkInterfaceCard nic = NetworkInterfaceCard.builder().slot(2L).addNetworkAdapter(networkInterfaceCard.getNetworkAdapter()).build();
this.networkInterfaceCards.add(nic);
return this;
}
public Builder addNIC4(NetworkInterfaceCard networkInterfaceCard) {
NetworkInterfaceCard nic = NetworkInterfaceCard.builder().slot(3L).addNetworkAdapter(networkInterfaceCard.getNetworkAdapter()).build();
this.networkInterfaceCards.add(nic); this.networkInterfaceCards.add(nic);
return this; return this;
} }
@ -73,14 +58,14 @@ public class NetworkSpec {
} }
} }
public List<NetworkInterfaceCard> getNetworkInterfaceCards() { public List<NetworkInterfaceCard> getNetworkInterfaceCards() {
return networkInterfaceCards; return networkInterfaceCards;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
return true;
if (o instanceof VmSpec) { if (o instanceof VmSpec) {
NetworkSpec other = (NetworkSpec) o; NetworkSpec other = (NetworkSpec) o;
return Objects.equal(networkInterfaceCards, other.networkInterfaceCards); return Objects.equal(networkInterfaceCards, other.networkInterfaceCards);
@ -95,8 +80,6 @@ public class NetworkSpec {
@Override @Override
public String toString() { public String toString() {
return "NetworkSpec{" + return "NetworkSpec{" + "networkInterfaceCards= " + networkInterfaceCards + '}';
"networkInterfaceCards= " + networkInterfaceCards +
'}';
} }
} }

View File

@ -18,6 +18,8 @@
*/ */
package org.jclouds.virtualbox.functions; package org.jclouds.virtualbox.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.jclouds.virtualbox.domain.NetworkInterfaceCard; import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
@ -36,8 +38,8 @@ public class AttachNicToMachine implements Function<NetworkInterfaceCard, Void>
public AttachNicToMachine(String vmName, MachineUtils machineUtils) { public AttachNicToMachine(String vmName, MachineUtils machineUtils) {
this.vmName = vmName; this.vmName = checkNotNull(vmName, "vmName");
this.machineUtils = machineUtils; this.machineUtils = checkNotNull(machineUtils, "machineUtils");
} }
@Override @Override

View File

@ -139,8 +139,13 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
} }
} }
/**
* ensureMachineHasPowerDown needs to have this delay just to ensure that the machine is completely powered off
*
* @param vmName
*/
private void ensureMachineHasPowerDown(String vmName) { private void ensureMachineHasPowerDown(String vmName) {
while(manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.RUNNING)) { while(!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.POWERED_OFF)) {
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() { machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
@Override @Override
public Void apply(ISession session) { public Void apply(ISession session) {