use of org.apache.cloudstack.network.contrail.model.VMInterfaceModel in project cloudstack by apache.
the class ContrailGuru method reserve.
/**
* Allocate the ip address (and mac) for the specified VM device.
*/
@Override
public void reserve(NicProfile nic, Network network, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
s_logger.debug("reserve NicProfile on network id: " + network.getId() + " " + network.getName());
s_logger.debug("deviceId: " + nic.getDeviceId());
NicVO nicVO = _nicDao.findById(nic.getId());
assert nicVO != null;
VirtualNetworkModel vnModel = _manager.getDatabase().lookupVirtualNetwork(network.getUuid(), _manager.getCanonicalName(network), network.getTrafficType());
/* Network must have been implemented */
assert vnModel != null;
VirtualMachineModel vmModel = _manager.getDatabase().lookupVirtualMachine(vm.getUuid());
if (vmModel == null) {
VMInstanceVO vmVo = (VMInstanceVO) vm.getVirtualMachine();
vmModel = new VirtualMachineModel(vmVo, vm.getUuid());
vmModel.setProperties(_manager.getModelController(), vmVo);
}
VMInterfaceModel vmiModel = vmModel.getVMInterface(nicVO.getUuid());
if (vmiModel == null) {
vmiModel = new VMInterfaceModel(nicVO.getUuid());
vmiModel.addToVirtualMachine(vmModel);
vmiModel.addToVirtualNetwork(vnModel);
}
try {
vmiModel.build(_manager.getModelController(), (VMInstanceVO) vm.getVirtualMachine(), nicVO);
vmiModel.setActive();
} catch (IOException ex) {
s_logger.error("virtual-machine-interface set", ex);
return;
}
InstanceIpModel ipModel = vmiModel.getInstanceIp();
if (ipModel == null) {
ipModel = new InstanceIpModel(vm.getInstanceName(), nic.getDeviceId());
ipModel.addToVMInterface(vmiModel);
} else {
s_logger.debug("Reuse existing instance-ip object on " + ipModel.getName());
}
if (nic.getIPv4Address() != null) {
s_logger.debug("Nic using existing IP address " + nic.getIPv4Address());
ipModel.setAddress(nic.getIPv4Address());
}
try {
vmModel.update(_manager.getModelController());
} catch (Exception ex) {
s_logger.warn("virtual-machine update", ex);
return;
}
_manager.getDatabase().getVirtualMachines().add(vmModel);
VirtualMachineInterface vmi = vmiModel.getVMInterface();
// allocate mac address
if (nic.getMacAddress() == null) {
MacAddressesType macs = vmi.getMacAddresses();
if (macs == null) {
s_logger.debug("no mac address is allocated for Nic " + nicVO.getUuid());
} else {
s_logger.info("VMI " + _manager.getVifNameByVmUuid(vm.getUuid(), nicVO.getDeviceId()) + " got mac address: " + macs.getMacAddress().get(0));
nic.setMacAddress(macs.getMacAddress().get(0));
}
}
if (nic.getIPv4Address() == null) {
s_logger.debug("Allocated IP address " + ipModel.getAddress());
nic.setIPv4Address(ipModel.getAddress());
if (network.getCidr() != null) {
nic.setIPv4Netmask(NetUtils.cidr2Netmask(network.getCidr()));
}
nic.setIPv4Gateway(network.getGateway());
nic.setFormat(AddressFormat.Ip4);
}
}
use of org.apache.cloudstack.network.contrail.model.VMInterfaceModel in project cloudstack by apache.
the class ContrailGuru method deallocate.
/**
* Release permanent resources of a Nic (VMI and addresses).
*/
@Override
public void deallocate(Network network, NicProfile nic, VirtualMachineProfile vm) {
s_logger.debug("deallocate NicProfile " + nic.getId() + " on " + network.getName());
NicVO nicVO = _nicDao.findById(nic.getId());
assert nicVO != null;
VirtualMachineModel vmModel = _manager.getDatabase().lookupVirtualMachine(vm.getUuid());
if (vmModel == null) {
return;
}
VMInterfaceModel vmiModel = vmModel.getVMInterface(nicVO.getUuid());
if (vmiModel == null) {
return;
}
try {
vmiModel.destroy(_manager.getModelController());
} catch (IOException ex) {
return;
}
vmModel.removeSuccessor(vmiModel);
if (!vmModel.hasDescendents()) {
_manager.getDatabase().getVirtualMachines().remove(vmModel);
try {
vmModel.delete(_manager.getModelController());
} catch (IOException ex) {
s_logger.warn("virtual-machine delete", ex);
return;
}
}
}
use of org.apache.cloudstack.network.contrail.model.VMInterfaceModel in project cloudstack by apache.
the class ContrailElementImpl method prepare.
@Override
public boolean prepare(Network network, NicProfile nicProfile, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
s_logger.debug("NetworkElement prepare: " + network.getName() + ", traffic type: " + network.getTrafficType());
if (network.getTrafficType() == TrafficType.Guest) {
s_logger.debug("ignore network " + network.getName());
return true;
}
s_logger.debug("network: " + network.getId());
VirtualNetworkModel vnModel = _manager.getDatabase().lookupVirtualNetwork(network.getUuid(), _manager.getCanonicalName(network), network.getTrafficType());
if (vnModel == null) {
// this may be the first time we see this network.
return false;
}
VirtualMachineModel vmModel = _manager.getDatabase().lookupVirtualMachine(vm.getUuid());
if (vmModel == null) {
VMInstanceVO vmVo = (VMInstanceVO) vm.getVirtualMachine();
vmModel = new VirtualMachineModel(vmVo, vm.getUuid());
vmModel.setProperties(_manager.getModelController(), vmVo);
}
NicVO nic = _nicDao.findById(nicProfile.getId());
assert nic != null;
VMInterfaceModel vmiModel = vmModel.getVMInterface(nic.getUuid());
if (vmiModel == null) {
vmiModel = new VMInterfaceModel(nic.getUuid());
vmiModel.addToVirtualMachine(vmModel);
vmiModel.addToVirtualNetwork(vnModel);
}
try {
vmiModel.build(_manager.getModelController(), (VMInstanceVO) vm.getVirtualMachine(), nic);
} catch (IOException ex) {
s_logger.warn("vm interface set", ex);
return false;
}
InstanceIpModel ipModel = vmiModel.getInstanceIp();
if (ipModel == null) {
ipModel = new InstanceIpModel(vm.getInstanceName(), nic.getDeviceId());
ipModel.addToVMInterface(vmiModel);
}
ipModel.setAddress(nicProfile.getIPv4Address());
try {
vmModel.update(_manager.getModelController());
} catch (Exception ex) {
s_logger.warn("virtual-machine-update", ex);
return false;
}
_manager.getDatabase().getVirtualMachines().add(vmModel);
return true;
}
use of org.apache.cloudstack.network.contrail.model.VMInterfaceModel in project cloudstack by apache.
the class ContrailElementImpl method release.
@Override
public boolean release(Network network, NicProfile nicProfile, VirtualMachineProfile vm, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException {
if (network.getTrafficType() == TrafficType.Guest) {
return true;
} else if (!_manager.isManagedPhysicalNetwork(network)) {
s_logger.debug("release ignore network " + network.getId());
return true;
}
NicVO nic = _nicDao.findById(nicProfile.getId());
assert nic != null;
VirtualMachineModel vmModel = _manager.getDatabase().lookupVirtualMachine(vm.getUuid());
if (vmModel == null) {
s_logger.debug("vm " + vm.getInstanceName() + " not in local database");
return true;
}
VMInterfaceModel vmiModel = vmModel.getVMInterface(nic.getUuid());
if (vmiModel != null) {
try {
vmiModel.destroy(_manager.getModelController());
} catch (IOException ex) {
s_logger.warn("virtual-machine-interface delete", ex);
}
vmModel.removeSuccessor(vmiModel);
}
if (!vmModel.hasDescendents()) {
_manager.getDatabase().getVirtualMachines().remove(vmModel);
try {
vmModel.delete(_manager.getModelController());
} catch (IOException e) {
return false;
}
}
return true;
}
use of org.apache.cloudstack.network.contrail.model.VMInterfaceModel in project cloudstack by apache.
the class ServerDBSyncImpl method buildNicResources.
private void buildNicResources(VirtualMachineModel vmModel, VMInstanceVO dbVm, StringBuffer syncLogMsg) throws IOException {
List<NicVO> nics = _nicDao.listByVmId(dbVm.getId());
for (NicVO nic : nics) {
VMInterfaceModel vmiModel = vmModel.getVMInterface(nic.getUuid());
if (vmiModel == null) {
vmiModel = new VMInterfaceModel(nic.getUuid());
NetworkVO network = _networksDao.findById(nic.getNetworkId());
VirtualNetworkModel vnModel = _manager.getDatabase().lookupVirtualNetwork(network.getUuid(), _manager.getCanonicalName(network), network.getTrafficType());
if (vnModel == null) {
s_logger.warn("Unable to locate virtual-network for network id " + network.getId());
continue;
}
vmiModel.addToVirtualMachine(vmModel);
vmiModel.addToVirtualNetwork(vnModel);
}
vmiModel.build(_manager.getModelController(), dbVm, nic);
}
}
Aggregations