cloudstack/server/src/com/cloud/network/configuration/PodBasedNetworkGuru.java
2010-09-22 10:44:00 -07:00

105 lines
3.8 KiB
Java

/**
*
*/
package com.cloud.network.configuration;
import javax.ejb.Local;
import org.apache.log4j.Logger;
import com.cloud.dc.DataCenter;
import com.cloud.dc.Pod;
import com.cloud.dc.dao.DataCenterDao;
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;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.NetUtils;
import com.cloud.vm.NicProfile;
import com.cloud.vm.VirtualMachineProfile;
@Local(value={NetworkGuru.class})
public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru {
private static final Logger s_logger = Logger.getLogger(PodBasedNetworkGuru.class);
@Inject DataCenterDao _dcDao;
@Override
public NetworkConfiguration design(NetworkOffering offering, DeploymentPlan plan, NetworkConfiguration userSpecified, Account owner) {
TrafficType type = offering.getTrafficType();
if (type != TrafficType.Management && type != TrafficType.Storage) {
return null;
}
NetworkConfigurationVO config = new NetworkConfigurationVO(type, Mode.Static, BroadcastDomainType.Native, offering.getId(), plan.getDataCenterId());
return config;
}
protected PodBasedNetworkGuru() {
super();
}
@Override
public NicProfile allocate(NetworkConfiguration config, NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
TrafficType trafficType = config.getTrafficType();
if (trafficType != TrafficType.Storage && trafficType != TrafficType.Management) {
return null;
}
if (nic != null) {
throw new CloudRuntimeException("Does not support nic configuration");
}
NicProfile profile = new NicProfile(ReservationStrategy.Start, null, null, null, null);
return profile;
}
@Override
public String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile vm, DeployDestination dest) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
DataCenter dc = dest.getDataCenter();
Pod pod = dest.getPod();
String ip = _dcDao.allocatePrivateIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), nic.getId());
String[] macs = _dcDao.getNextAvailableMacAddressPair(dc.getId());
nic.setIp4Address(ip);
nic.setGateway(pod.getGateway());
nic.setMacAddress(macs[0]);
nic.setFormat(AddressFormat.Ip4);
String netmask = NetUtils.getCidrSubNet(pod.getCidrAddress(), pod.getCidrSize());
nic.setNetmask(netmask);
return Long.toString(nic.getId());
}
@Override
public boolean release(String uniqueId) {
_dcDao.releasePrivateIpAddress(Long.parseLong(uniqueId));
return true;
}
@Override
public NetworkConfiguration implement(NetworkConfiguration config, NetworkOffering offering, DeployDestination destination) {
return config;
}
@Override
public void destroy(NetworkConfiguration config, NetworkOffering offering) {
}
}