alena f8a687ab23 1) Implemented create/list/deleteNetworkOffering create/delete/listNetwork APIs.
2) Added networkIds parameter to deployVMCmd - accepts list of networks ids separated by coma.
3) Changed domainRouter/systemVm/userVm response to return list of Nics associated with the vm.
2010-12-02 19:30:17 -08:00

138 lines
5.2 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.Vlan.VlanType;
import com.cloud.dc.VlanVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.dc.dao.VlanDao;
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;
import com.cloud.network.NetworkManager;
import com.cloud.network.NetworkVO;
import com.cloud.network.Networks.AddressFormat;
import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.Networks.IsolationType;
import com.cloud.network.Networks.Mode;
import com.cloud.network.Networks.TrafficType;
import com.cloud.network.dao.IPAddressDao;
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;
import com.cloud.utils.component.Inject;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
@Local(value={NetworkGuru.class})
public class PublicNetworkGuru extends AdapterBase implements NetworkGuru {
private static final Logger s_logger = Logger.getLogger(PublicNetworkGuru.class);
@Inject DataCenterDao _dcDao;
@Inject VlanDao _vlanDao;
@Inject NetworkManager _networkMgr;
@Inject IPAddressDao _ipAddressDao;
@Override
public Network design(NetworkOffering offering, DeploymentPlan plan, Network config, Account owner) {
if (offering.getTrafficType() != TrafficType.Public) {
return null;
}
return new NetworkVO(offering.getTrafficType(), offering.getGuestIpType(), Mode.Static, BroadcastDomainType.Vlan, offering.getId(), plan.getDataCenterId());
}
protected PublicNetworkGuru() {
super();
}
protected void getIp(NicProfile nic, DataCenter dc, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException {
if (nic.getIp4Address() == null) {
Pair<String, VlanVO> ipAndVlan = _vlanDao.assignIpAddress(dc.getId(), vm.getVirtualMachine().getAccountId(), vm.getVirtualMachine().getDomainId(), VlanType.VirtualNetwork, true);
if (ipAndVlan == null) {
throw new InsufficientVirtualNetworkCapcityException("Unable to get public ip address in " + dc.getId(), DataCenter.class, dc.getId());
}
VlanVO vlan = ipAndVlan.second();
nic.setIp4Address(ipAndVlan.first());
nic.setGateway(vlan.getVlanGateway());
nic.setNetmask(vlan.getVlanNetmask());
nic.setIsolationUri(IsolationType.Vlan.toUri(vlan.getVlanId()));
nic.setBroadcastType(BroadcastDomainType.Vlan);
nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlan.getVlanId()));
nic.setFormat(AddressFormat.Ip4);
nic.setReservationId(Long.toString(vlan.getId()));
}
nic.setDns1(dc.getDns1());
nic.setDns2(dc.getDns2());
}
@Override
public NicProfile allocate(Network config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
if (config.getTrafficType() != TrafficType.Public) {
return null;
}
if (nic == null) {
nic = new NicProfile(ReservationStrategy.Create, null, null, null, null);
}
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId());
nic.setMacAddress(mac);
DataCenter dc = _dcDao.findById(config.getDataCenterId());
getIp(nic, dc, vm);
if (nic.getIp4Address() == null) {
nic.setStrategy(ReservationStrategy.Start);
} else {
nic.setStrategy(ReservationStrategy.Create);
}
return nic;
}
@Override
public void reserve(NicProfile nic, Network configuration, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException {
if (nic.getIp4Address() == null) {
getIp(nic, dest.getDataCenter(), vm);
}
}
@Override
public boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) {
_ipAddressDao.unassignIpAddress(reservationId);
return true;
}
@Override
public Network implement(Network config, NetworkOffering offering, DeployDestination destination, ReservationContext context) {
return config;
}
@Override
public void deallocate(Network config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) {
}
@Override
public void destroy(Network config, NetworkOffering offering) {
}
@Override
public boolean trash(Network config, NetworkOffering offering, Account owner) {
return true;
}
}