finalized guru design

This commit is contained in:
root 2010-09-22 10:43:03 -07:00 committed by Alex Huang
parent 0be5053c56
commit 979fcf8b78
11 changed files with 147 additions and 87 deletions

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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<State, Event> {
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<State, Event> getStateMachine() {
// TODO Auto-generated method stub
return null;
}
@Override
public State getNextState(Event event) {
return s_fsm.getNextState(this, event);
}
@Override
public List<State> getFromStates(Event event) {
return s_fsm.getFromStates(this, event);
}
@Override
public Set<Event> getPossibleEvents() {
return s_fsm.getPossibleEvents(this);
}
final static private StateMachine<State, Event> s_fsm = new StateMachine<State, Event>();
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 {

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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)) {

View File

@ -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());
}

View File

@ -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;

View File

@ -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);

View File

@ -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<String, VlanVO> 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());
}

View File

@ -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;