use of com.cloud.exception.ConcurrentOperationException in project cloudstack by apache.
the class UploadSslCertCmd method execute.
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
try {
SslCertResponse response = _certService.uploadSslCert(this);
setResponseObject(response);
response.setResponseName(getCommandName());
} catch (Exception e) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
}
}
use of com.cloud.exception.ConcurrentOperationException 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 com.cloud.exception.ConcurrentOperationException in project cloudstack by apache.
the class NuageVspGuestNetworkGuru method trash.
@Override
public boolean trash(Network network, NetworkOffering offering) {
long networkId = network.getId();
network = _networkDao.acquireInLockTable(networkId, 1200);
if (network == null) {
throw new ConcurrentOperationException("Unable to acquire lock on network " + networkId);
}
try {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Handling trash() call back to delete the network " + network.getName() + " with uuid " + network.getUuid() + " from VSP");
}
VspNetwork vspNetwork = _nuageVspEntityBuilder.buildVspNetwork(network);
HostVO nuageVspHost = _nuageVspManager.getNuageVspHost(network.getPhysicalNetworkId());
TrashNetworkVspCommand cmd = new TrashNetworkVspCommand(vspNetwork);
Answer answer = _agentMgr.easySend(nuageVspHost.getId(), cmd);
if (answer == null || !answer.getResult()) {
s_logger.error("TrashNetworkNuageVspCommand for network " + network.getUuid() + " failed");
if ((null != answer) && (null != answer.getDetails())) {
s_logger.error(answer.getDetails());
}
return false;
}
} finally {
_networkDao.releaseFromLockTable(network.getId());
}
return super.trash(network, offering);
}
use of com.cloud.exception.ConcurrentOperationException in project cloudstack by apache.
the class IpAddressManagerImpl method associateIPToGuestNetwork.
@DB
@Override
public IPAddressVO associateIPToGuestNetwork(long ipId, long networkId, boolean releaseOnFailure) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, ConcurrentOperationException {
Account caller = CallContext.current().getCallingAccount();
Account owner = null;
IPAddressVO ipToAssoc = _ipAddressDao.findById(ipId);
if (ipToAssoc != null) {
Network network = _networksDao.findById(networkId);
if (network == null) {
throw new InvalidParameterValueException("Invalid network id is given");
}
DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
if (zone.getNetworkType() == NetworkType.Advanced) {
if (network.getGuestType() == Network.GuestType.Shared) {
if (isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), AccessType.UseEntry, false, network);
} else {
throw new InvalidParameterValueException("IP can be associated with guest network of 'shared' type only if " + "network services Source Nat, Static Nat, Port Forwarding, Load balancing, firewall are enabled in the network");
}
}
} else {
_accountMgr.checkAccess(caller, null, true, ipToAssoc);
}
owner = _accountMgr.getAccount(ipToAssoc.getAllocatedToAccountId());
} else {
s_logger.debug("Unable to find ip address by id: " + ipId);
return null;
}
if (ipToAssoc.getAssociatedWithNetworkId() != null) {
s_logger.debug("IP " + ipToAssoc + " is already assocaited with network id" + networkId);
return ipToAssoc;
}
Network network = _networksDao.findById(networkId);
if (network != null) {
_accountMgr.checkAccess(owner, AccessType.UseEntry, false, network);
} else {
s_logger.debug("Unable to find ip address by id: " + ipId);
return null;
}
DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
// allow associating IP addresses to guest network only
if (network.getTrafficType() != TrafficType.Guest) {
throw new InvalidParameterValueException("Ip address can be associated to the network with trafficType " + TrafficType.Guest);
}
// - and it belongs to the system
if (network.getAccountId() != owner.getId()) {
if (zone.getNetworkType() != NetworkType.Basic && !(zone.getNetworkType() == NetworkType.Advanced && network.getGuestType() == Network.GuestType.Shared)) {
throw new InvalidParameterValueException("The owner of the network is not the same as owner of the IP");
}
}
if (zone.getNetworkType() == NetworkType.Advanced) {
// In Advance zone allow to do IP assoc only for Isolated networks with source nat service enabled
if (network.getGuestType() == GuestType.Isolated && !(_networkModel.areServicesSupportedInNetwork(network.getId(), Service.SourceNat))) {
throw new InvalidParameterValueException("In zone of type " + NetworkType.Advanced + " ip address can be associated only to the network of guest type " + GuestType.Isolated + " with the " + Service.SourceNat.getName() + " enabled");
}
// In Advance zone allow to do IP assoc only for shared networks with source nat/static nat/lb/pf services enabled
if (network.getGuestType() == GuestType.Shared && !isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
throw new InvalidParameterValueException("In zone of type " + NetworkType.Advanced + " ip address can be associated with network of guest type " + GuestType.Shared + "only if at " + "least one of the services " + Service.SourceNat.getName() + "/" + Service.StaticNat.getName() + "/" + Service.Lb.getName() + "/" + Service.PortForwarding.getName() + " is enabled");
}
}
NetworkOffering offering = _networkOfferingDao.findById(network.getNetworkOfferingId());
boolean sharedSourceNat = offering.getSharedSourceNat();
boolean isSourceNat = false;
if (!sharedSourceNat) {
if (getExistingSourceNatInNetwork(owner.getId(), networkId) == null) {
if (network.getGuestType() == GuestType.Isolated && network.getVpcId() == null && !ipToAssoc.isPortable()) {
isSourceNat = true;
}
}
}
s_logger.debug("Associating ip " + ipToAssoc + " to network " + network);
IPAddressVO ip = _ipAddressDao.findById(ipId);
//update ip address with networkId
ip.setAssociatedWithNetworkId(networkId);
ip.setSourceNat(isSourceNat);
_ipAddressDao.update(ipId, ip);
boolean success = false;
try {
success = applyIpAssociations(network, false);
if (success) {
s_logger.debug("Successfully associated ip address " + ip.getAddress().addr() + " to network " + network);
} else {
s_logger.warn("Failed to associate ip address " + ip.getAddress().addr() + " to network " + network);
}
return ip;
} finally {
if (!success && releaseOnFailure) {
if (ip != null) {
try {
s_logger.warn("Failed to associate ip address, so releasing ip from the database " + ip);
_ipAddressDao.markAsUnavailable(ip.getId());
if (!applyIpAssociations(network, true)) {
// if fail to apply ip assciations again, unassign ip address without updating resource
// count and generating usage event as there is no need to keep it in the db
_ipAddressDao.unassignIpAddress(ip.getId());
}
} catch (Exception e) {
s_logger.warn("Unable to disassociate ip address for recovery", e);
}
}
}
}
}
use of com.cloud.exception.ConcurrentOperationException in project cloudstack by apache.
the class IpAddressManagerImpl method assignDedicateIpAddress.
@DB
@Override
public PublicIp assignDedicateIpAddress(Account owner, final Long guestNtwkId, final Long vpcId, final long dcId, final boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException {
final long ownerId = owner.getId();
PublicIp ip = null;
try {
ip = Transaction.execute(new TransactionCallbackWithException<PublicIp, InsufficientAddressCapacityException>() {
@Override
public PublicIp doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException {
Account owner = _accountDao.acquireInLockTable(ownerId);
if (owner == null) {
// this ownerId comes from owner or type Account. See the class "AccountVO" and the annotations in that class
// to get the table name and field name that is queried to fill this ownerid.
ConcurrentOperationException ex = new ConcurrentOperationException("Unable to lock account");
throw ex;
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("lock account " + ownerId + " is acquired");
}
boolean displayIp = true;
if (guestNtwkId != null) {
Network ntwk = _networksDao.findById(guestNtwkId);
displayIp = ntwk.getDisplayNetwork();
} else if (vpcId != null) {
VpcVO vpc = _vpcDao.findById(vpcId);
displayIp = vpc.isDisplay();
}
PublicIp ip = fetchNewPublicIp(dcId, null, null, owner, VlanType.VirtualNetwork, guestNtwkId, isSourceNat, false, null, false, vpcId, displayIp);
IPAddressVO publicIp = ip.ip();
markPublicIpAsAllocated(publicIp);
_ipAddressDao.update(publicIp.getId(), publicIp);
return ip;
}
});
return ip;
} finally {
if (owner != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Releasing lock account " + ownerId);
}
_accountDao.releaseFromLockTable(ownerId);
}
if (ip == null) {
s_logger.error("Unable to get source nat ip address for account " + ownerId);
}
}
}
Aggregations