diff --git a/api/src/com/cloud/deploy/DeployDestination.java b/api/src/com/cloud/deploy/DeployDestination.java index 9cc4946b717..c3f91880327 100644 --- a/api/src/com/cloud/deploy/DeployDestination.java +++ b/api/src/com/cloud/deploy/DeployDestination.java @@ -57,7 +57,28 @@ public class DeployDestination { @Override public boolean equals(Object obj) { - assert false : "Not implemented correctly yet."; - return false; + DeployDestination that = (DeployDestination)obj; + if (this._dc == null || that._dc == null) { + return false; + } + if (this._dc.getId() != that._dc.getId()) { + return false; + } + if (this._pod == null || that._pod == null) { + return false; + } + if (this._pod.getId() != that._pod.getId()) { + return false; + } + if (this._cluster == null || that._cluster == null) { + return false; + } + if (this._cluster.getId() != that._cluster.getId()) { + return false; + } + if (this._host == null || that._host == null) { + return false; + } + return this._host.getId() == that._host.getId(); } } diff --git a/api/src/com/cloud/network/configuration/NetworkGuru.java b/api/src/com/cloud/network/configuration/NetworkGuru.java index 7f01ac5bb36..2cb0a5a1764 100644 --- a/api/src/com/cloud/network/configuration/NetworkGuru.java +++ b/api/src/com/cloud/network/configuration/NetworkGuru.java @@ -27,9 +27,9 @@ public interface NetworkGuru extends Adapter { NicProfile allocate(NetworkConfiguration config, NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException; - boolean create(NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException; +// NicProfile create(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException; - String reserve(NicProfile nic, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException; + String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException; boolean release(String uniqueId); diff --git a/api/src/com/cloud/resource/Resource.java b/api/src/com/cloud/resource/Resource.java index 0ab66082ee4..310507061ae 100644 --- a/api/src/com/cloud/resource/Resource.java +++ b/api/src/com/cloud/resource/Resource.java @@ -4,6 +4,11 @@ package com.cloud.resource; import java.util.Date; +import java.util.List; +import java.util.Set; + +import com.cloud.utils.fsm.FiniteState; +import com.cloud.utils.fsm.StateMachine; /** * Indicates a resource in CloudStack. @@ -12,11 +17,50 @@ import java.util.Date; * */ public interface Resource { - enum State { - Allocated, // The resource is allocated but not re - Reserving, - Reserved, - Releasing, + enum Event { + ReservationRequested, + ReleaseRequested, + CancelRequested, + OperationCompleted, + OperationFailed, + } + + enum State implements FiniteState { + Allocated, // Resource is allocated + Reserving, // Resource is being reserved right now. + Reserved, // Resource is reserved + Releasing, // Resource is being released. + Ready; // Resource is ready which means it does not need to go through reservation. + + @Override + public StateMachine getStateMachine() { + + // TODO Auto-generated method stub + return null; + } + + @Override + public State getNextState(Event event) { + return s_fsm.getNextState(this, event); + } + + @Override + public List getFromStates(Event event) { + return s_fsm.getFromStates(this, event); + } + + @Override + public Set getPossibleEvents() { + return s_fsm.getPossibleEvents(this); + } + + final static private StateMachine s_fsm = new StateMachine(); + static { + s_fsm.addTransition(State.Allocated, Event.ReservationRequested, State.Reserving); + s_fsm.addTransition(State.Reserving, Event.CancelRequested, State.Allocated); + s_fsm.addTransition(State.Reserving, Event.OperationCompleted, State.Reserved); + s_fsm.addTransition(State.Reserving, Event.OperationFailed, State.Allocated); + } } enum ReservationStrategy { diff --git a/api/src/com/cloud/vm/NicProfile.java b/api/src/com/cloud/vm/NicProfile.java index fa669ccc1c4..621472723ea 100644 --- a/api/src/com/cloud/vm/NicProfile.java +++ b/api/src/com/cloud/vm/NicProfile.java @@ -10,15 +10,15 @@ import com.cloud.network.Network.BroadcastDomainType; import com.cloud.network.Network.Mode; import com.cloud.network.Network.TrafficType; import com.cloud.network.NetworkConfiguration; +import com.cloud.resource.Resource; +import com.cloud.resource.Resource.ReservationStrategy; public class NicProfile { long id; BroadcastDomainType broadcastType; - String cidr; Mode mode; long vmId; String gateway; - int deviceId; AddressFormat format; TrafficType trafficType; String ip4Address; @@ -27,6 +27,8 @@ public class NicProfile { URI isolationUri; String netmask; URI broadcastUri; + ReservationStrategy strategy; + String reservationId; public String getNetmask() { return netmask; @@ -44,7 +46,7 @@ public class NicProfile { return broadcastUri; } - public void setIsolationUril(URI isolationUri) { + public void setIsolationUri(URI isolationUri) { this.isolationUri = isolationUri; } @@ -60,10 +62,6 @@ public class NicProfile { this.broadcastType = broadcastType; } - public void setCidr(String cidr) { - this.cidr = cidr; - } - public void setMode(Mode mode) { this.mode = mode; } @@ -76,10 +74,6 @@ public class NicProfile { this.gateway = gateway; } - public void setDeviceId(int deviceId) { - this.deviceId = deviceId; - } - public void setFormat(AddressFormat format) { this.format = format; } @@ -112,10 +106,6 @@ public class NicProfile { return broadcastType; } - public String getCidr() { - return cidr; - } - public void setMacAddress(String macAddress) { this.macAddress = macAddress; } @@ -128,10 +118,6 @@ public class NicProfile { return gateway; } - public int getDeviceId() { - return deviceId; - } - public AddressFormat getFormat() { return format; } @@ -158,8 +144,6 @@ public class NicProfile { public NicProfile(Nic nic, NetworkConfiguration network) { this.id = nic.getId(); - this.deviceId = nic.getDeviceId(); - this.cidr = network.getCidr(); this.gateway = network.getGateway(); this.mode = network.getMode(); this.format = null; @@ -168,19 +152,35 @@ public class NicProfile { this.ip4Address = nic.getIp4Address(); this.ip6Address = null; this.macAddress = nic.getMacAddress(); + this.reservationId = nic.getReservationId(); + this.strategy = nic.getReservationStrategy(); } - public NicProfile(long id, BroadcastDomainType type, String cidr, Mode mode, long vmId) { + public NicProfile(long id, BroadcastDomainType type, Mode mode, long vmId) { this.id = id; this.broadcastType = type; - this.cidr = cidr; this.mode = mode; this.vmId = vmId; } - public NicProfile(String ip4Address, String macAddress, String gateway) { + public NicProfile(Resource.ReservationStrategy strategy, String ip4Address, String macAddress, String gateway, String netmask) { + this.format = AddressFormat.Ip4; this.ip4Address = ip4Address; this.macAddress = macAddress; this.gateway = gateway; + this.netmask = netmask; + this.strategy = strategy; + } + + public ReservationStrategy getReservationStrategy() { + return strategy; + } + + public String getReservationId() { + return reservationId; + } + + public void setReservationId(String reservationId) { + this.reservationId = reservationId; } } diff --git a/core/src/com/cloud/vm/VirtualNetwork.java b/core/src/com/cloud/vm/VirtualNetwork.java index fd882a0b7e9..44651f65b3b 100644 --- a/core/src/com/cloud/vm/VirtualNetwork.java +++ b/core/src/com/cloud/vm/VirtualNetwork.java @@ -17,25 +17,15 @@ */ package com.cloud.vm; +import com.cloud.network.Network.IsolationType; +import com.cloud.network.Network.Mode; + /** * VirtualNetwork describes from a management level the * things needed to provide the network to the virtual * machine. */ public class VirtualNetwork { - public enum Mode { - None, - Local, - Static, - Dhcp; - } - - public enum Isolation { - VNET, - VLAN, - OSWITCH, - } - /** * The gateway for this network. */ @@ -74,7 +64,7 @@ public class VirtualNetwork { /** * Isolation method for networking. */ - public Isolation method; + public IsolationType method; public boolean firewalled; diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index db7e49f53c2..504aba68dd1 100644 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -2488,7 +2488,7 @@ public class NetworkManagerImpl implements NetworkManager, VirtualMachineManager nic.setState(Resource.State.Reserving); _nicDao.update(nic.getId(), nic); NicProfile profile = toNicProfile(nic); - String reservationId = concierge.reserve(profile, vmProfile, dest); + String reservationId = concierge.reserve(profile, config, vmProfile, dest); nic.setIp4Address(profile.getIp4Address()); nic.setIp6Address(profile.getIp6Address()); nic.setMacAddress(profile.getMacAddress()); @@ -2497,6 +2497,9 @@ public class NetworkManagerImpl implements NetworkManager, VirtualMachineManager nic.setReservationId(reservationId); nic.setReserver(concierge.getName()); nic.setState(Resource.State.Reserved); + nic.setNetmask(profile.getNetmask()); + nic.setGateway(profile.getGateway()); + nic.setAddressFormat(profile.getFormat()); _nicDao.update(nic.getId(), nic); for (NetworkElement element : _networkElements) { if (!element.prepare(config, profile, vmProfile, null)) { diff --git a/server/src/com/cloud/network/configuration/ControlNetworkGuru.java b/server/src/com/cloud/network/configuration/ControlNetworkGuru.java index 0ad56a1dece..59bf7d3a83f 100644 --- a/server/src/com/cloud/network/configuration/ControlNetworkGuru.java +++ b/server/src/com/cloud/network/configuration/ControlNetworkGuru.java @@ -17,12 +17,14 @@ import com.cloud.deploy.DeployDestination; import com.cloud.deploy.DeploymentPlan; import com.cloud.exception.InsufficientAddressCapacityException; import com.cloud.exception.InsufficientVirtualNetworkCapcityException; +import com.cloud.network.Network.AddressFormat; import com.cloud.network.Network.BroadcastDomainType; import com.cloud.network.Network.Mode; import com.cloud.network.Network.TrafficType; import com.cloud.network.NetworkConfiguration; import com.cloud.network.NetworkConfigurationVO; import com.cloud.offering.NetworkOffering; +import com.cloud.resource.Resource.ReservationStrategy; import com.cloud.user.Account; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.component.ComponentLocator; @@ -90,21 +92,18 @@ public class ControlNetworkGuru extends AdapterBase implements NetworkGuru { throw new CloudRuntimeException("Does not support nic specification at this time: " + nic); } - return new NicProfile(null, null, null); + return new NicProfile(ReservationStrategy.Start, null, null, null, null); } @Override - public boolean create(NicProfile nic, VirtualMachineProfile profile) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { - return true; - } - - @Override - public String reserve(NicProfile nic, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, + public String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { String ip = _dcDao.allocateLinkLocalPrivateIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), vm.getId()); nic.setIp4Address(ip); nic.setMacAddress("FE:FF:FF:FF:FF:FF"); nic.setNetmask("255.255.0.0"); + nic.setFormat(AddressFormat.Ip4); + return Long.toString(nic.getId()); } diff --git a/server/src/com/cloud/network/configuration/GuestNetworkGuru.java b/server/src/com/cloud/network/configuration/GuestNetworkGuru.java index 14304b20d2a..e15f3d8cc68 100644 --- a/server/src/com/cloud/network/configuration/GuestNetworkGuru.java +++ b/server/src/com/cloud/network/configuration/GuestNetworkGuru.java @@ -73,14 +73,7 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru { } @Override - public boolean create(NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapcityException, - InsufficientAddressCapacityException { - // TODO Auto-generated method stub - return false; - } - - @Override - public String reserve(NicProfile nic, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, + public String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { // TODO Auto-generated method stub return null; diff --git a/server/src/com/cloud/network/configuration/PodBasedNetworkGuru.java b/server/src/com/cloud/network/configuration/PodBasedNetworkGuru.java index 869f051612c..cf72ccd8aa7 100644 --- a/server/src/com/cloud/network/configuration/PodBasedNetworkGuru.java +++ b/server/src/com/cloud/network/configuration/PodBasedNetworkGuru.java @@ -14,12 +14,14 @@ import com.cloud.deploy.DeployDestination; import com.cloud.deploy.DeploymentPlan; import com.cloud.exception.InsufficientAddressCapacityException; import com.cloud.exception.InsufficientVirtualNetworkCapcityException; +import com.cloud.network.Network.AddressFormat; import com.cloud.network.Network.BroadcastDomainType; import com.cloud.network.Network.Mode; import com.cloud.network.Network.TrafficType; import com.cloud.network.NetworkConfiguration; import com.cloud.network.NetworkConfigurationVO; import com.cloud.offering.NetworkOffering; +import com.cloud.resource.Resource.ReservationStrategy; import com.cloud.user.Account; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.component.Inject; @@ -62,17 +64,12 @@ public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru { throw new CloudRuntimeException("Does not support nic configuration"); } - NicProfile profile = new NicProfile(null, null, null); + NicProfile profile = new NicProfile(ReservationStrategy.Start, null, null, null, null); return profile; } @Override - public boolean create(NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { - return true; - } - - @Override - public String reserve(NicProfile nic, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, + public String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { DataCenter dc = dest.getDataCenter(); Pod pod = dest.getPod(); @@ -81,9 +78,9 @@ public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru { String[] macs = _dcDao.getNextAvailableMacAddressPair(dc.getId()); nic.setIp4Address(ip); - nic.setCidr(pod.getCidrAddress() + "/" + pod.getCidrSize()); nic.setGateway(pod.getGateway()); nic.setMacAddress(macs[0]); + nic.setFormat(AddressFormat.Ip4); String netmask = NetUtils.getCidrSubNet(pod.getCidrAddress(), pod.getCidrSize()); nic.setNetmask(netmask); diff --git a/server/src/com/cloud/network/configuration/PublicNetworkGuru.java b/server/src/com/cloud/network/configuration/PublicNetworkGuru.java index 4ae8a353ce4..9e28c4c62fc 100644 --- a/server/src/com/cloud/network/configuration/PublicNetworkGuru.java +++ b/server/src/com/cloud/network/configuration/PublicNetworkGuru.java @@ -3,9 +3,6 @@ */ package com.cloud.network.configuration; -import java.net.URI; -import java.net.URISyntaxException; - import javax.ejb.Local; import org.apache.log4j.Logger; @@ -18,12 +15,15 @@ import com.cloud.deploy.DeployDestination; import com.cloud.deploy.DeploymentPlan; import com.cloud.exception.InsufficientAddressCapacityException; import com.cloud.exception.InsufficientVirtualNetworkCapcityException; +import com.cloud.network.Network.AddressFormat; import com.cloud.network.Network.BroadcastDomainType; +import com.cloud.network.Network.IsolationType; import com.cloud.network.Network.Mode; import com.cloud.network.Network.TrafficType; import com.cloud.network.NetworkConfiguration; import com.cloud.network.NetworkConfigurationVO; import com.cloud.offering.NetworkOffering; +import com.cloud.resource.Resource.ReservationStrategy; import com.cloud.user.Account; import com.cloud.utils.Pair; import com.cloud.utils.component.AdapterBase; @@ -62,18 +62,19 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { if (nic != null) { throw new CloudRuntimeException("Unsupported nic settings"); } - - return new NicProfile(null, null, null); + + return new NicProfile(ReservationStrategy.Create, null, null, null, null); } @Override - public boolean create(NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { - return true; - } - - @Override - public String reserve(NicProfile ch, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { + public String reserve(NicProfile ch, NetworkConfiguration configuration, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { + if (ch.getReservationId() != null) { + return ch.getReservationId(); + } + long dcId = dest.getDataCenter().getId(); + + String[] macs = _dcDao.getNextAvailableMacAddressPair(dcId); Pair ipAndVlan = _vlanDao.assignIpAddress(dcId, vm.getVm().getAccountId(), vm.getVm().getDomainId(), VlanType.VirtualNetwork, true); if (ipAndVlan == null) { @@ -83,12 +84,12 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { ch.setIp4Address(ipAndVlan.first()); ch.setGateway(vlan.getVlanGateway()); ch.setNetmask(vlan.getVlanNetmask()); - try { - ch.setIsolationUril(new URI("vlan://" + vlan.getVlanId())); - } catch (URISyntaxException e) { - throw new CloudRuntimeException("URI Syntax: " + "vlan://" + vlan.getVlanId(), e); - } + ch.setIsolationUri(IsolationType.Vlan.toUri(vlan.getVlanId())); ch.setBroadcastType(BroadcastDomainType.Vlan); + ch.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlan.getVlanId())); + ch.setMacAddress(macs[1]); + ch.setFormat(AddressFormat.Ip4); + ch.setReservationId(Long.toString(vlan.getId())); return Long.toString(vlan.getId()); } diff --git a/server/src/com/cloud/vm/NicVO.java b/server/src/com/cloud/vm/NicVO.java index 844ea3495e9..fdc9f3a730f 100644 --- a/server/src/com/cloud/vm/NicVO.java +++ b/server/src/com/cloud/vm/NicVO.java @@ -29,6 +29,7 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; +import com.cloud.network.Network.AddressFormat; import com.cloud.network.Network.Mode; @Entity @@ -57,6 +58,9 @@ public class NicVO implements Nic { @Column(name="isolation_uri") URI isolationUri; + @Column(name="ip_type") + AddressFormat addressFormat; + @Column(name="broadcast_uri") URI broadcastUri; @@ -138,6 +142,14 @@ public class NicVO implements Nic { public void setGateway(String gateway) { this.gateway = gateway; } + + public AddressFormat getAddressFormat() { + return addressFormat; + } + + public void setAddressFormat(AddressFormat format) { + this.addressFormat = format; + } public void setNetmask(String netmask) { this.netmask = netmask;