diff --git a/api/src/com/cloud/api/commands/UpdatePortForwardingRuleCmd.java b/api/src/com/cloud/api/commands/UpdatePortForwardingRuleCmd.java index dffbd2b1891..610f34d4e62 100644 --- a/api/src/com/cloud/api/commands/UpdatePortForwardingRuleCmd.java +++ b/api/src/com/cloud/api/commands/UpdatePortForwardingRuleCmd.java @@ -79,7 +79,7 @@ public class UpdatePortForwardingRuleCmd extends BaseAsyncCmd { public long getEntityOwnerId() { IpAddress addr = _entityMgr.findById(IpAddress.class, getPublicIp()); if (addr != null) { - return addr.getAccountId(); + return addr.getAllocatedToAccountId(); } // bad address given, parent this command to SYSTEM so ERROR events are tracked diff --git a/api/src/com/cloud/network/IpAddress.java b/api/src/com/cloud/network/IpAddress.java index c352a273af0..8cb87a00862 100644 --- a/api/src/com/cloud/network/IpAddress.java +++ b/api/src/com/cloud/network/IpAddress.java @@ -19,30 +19,48 @@ package com.cloud.network; import java.util.Date; -public interface IpAddress { +import com.cloud.acl.ControlledEntity; + +/** + * IpAddress represents the public ip address to be allocated in the CloudStack. + * + * When it is not allocated, it should have + * - State = Free + * - Allocated = null + * - AccountId = null + * - DomainId = null + * + * When it is allocated, it should have + * - State = Allocated + * - AccountId = account owner. + * - DomainId = domain of the account owner. + * - Allocated = time it was allocated. + */ +public interface IpAddress extends ControlledEntity { + enum State { + Allocating, // The IP Address is being propagated to other network elements and is not ready for use yet. + Allocated, // The IP address is in used. + Releasing, // The IP address is being released for other network elements and is not ready for allocation. + Free // The IP address is ready to be allocated. + } + long getDataCenterId(); String getAddress(); - Long getAccountId(); - Long getDomainId(); - Date getAllocated(); + + Long getAllocatedToAccountId(); + + Long getAllocatedInDomainId(); + + Date getAllocatedTime(); + boolean isSourceNat(); - void setAccountId(Long accountId); - - void setDomainId(Long domainId); - - void setSourceNat(boolean sourceNat); - - boolean getSourceNat(); - - void setAllocated(Date allocated); - - long getVlanDbId(); - - void setVlanDbId(long vlanDbId); + long getVlanId(); boolean isOneToOneNat(); - - void setOneToOneNat(boolean oneToOneNat); + + State getState(); + + boolean readyToUse(); } diff --git a/server/src/com/cloud/api/ApiDBUtils.java b/server/src/com/cloud/api/ApiDBUtils.java index f6baeab15cc..dca5ba28918 100755 --- a/server/src/com/cloud/api/ApiDBUtils.java +++ b/server/src/com/cloud/api/ApiDBUtils.java @@ -411,7 +411,7 @@ public class ApiDBUtils { List vms = _userVmDao.listVmsUsingGuestIpAddress(addr.getDataCenterId(), guestIp); if (vms != null) { for (UserVmVO vm : vms) { - if (vm.getAccountId() == addr.getAccountId()) { + if (vm.getAccountId() == addr.getAllocatedToAccountId()) { return vm; } } diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index e394b12b382..92c2d007c17 100644 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -767,20 +767,20 @@ public class ApiResponseHelper implements ResponseGenerator { @Override public IPAddressResponse createIPAddressResponse(IpAddress ipAddress) { - VlanVO vlan = ApiDBUtils.findVlanById(ipAddress.getVlanDbId()); + VlanVO vlan = ApiDBUtils.findVlanById(ipAddress.getVlanId()); boolean forVirtualNetworks = vlan.getVlanType().equals(VlanType.VirtualNetwork); IPAddressResponse ipResponse = new IPAddressResponse(); ipResponse.setIpAddress(ipAddress.getAddress()); - if (ipAddress.getAllocated() != null) { - ipResponse.setAllocated(ipAddress.getAllocated()); + if (ipAddress.getAllocatedTime() != null) { + ipResponse.setAllocated(ipAddress.getAllocatedTime()); } ipResponse.setZoneId(ipAddress.getDataCenterId()); ipResponse.setZoneName(ApiDBUtils.findZoneById(ipAddress.getDataCenterId()).getName()); ipResponse.setSourceNat(ipAddress.isSourceNat()); // get account information - Account accountTemp = ApiDBUtils.findAccountById(ipAddress.getAccountId()); + Account accountTemp = ApiDBUtils.findAccountById(ipAddress.getAllocatedToAccountId()); if (accountTemp != null) { ipResponse.setAccountName(accountTemp.getAccountName()); ipResponse.setDomainId(accountTemp.getDomainId()); @@ -793,8 +793,8 @@ public class ApiResponseHelper implements ResponseGenerator { // show this info to admin only Account account = UserContext.current().getAccount(); if ((account == null) || account.getType() == Account.ACCOUNT_TYPE_ADMIN) { - ipResponse.setVlanId(ipAddress.getVlanDbId()); - ipResponse.setVlanName(ApiDBUtils.findVlanById(ipAddress.getVlanDbId()).getVlanId()); + ipResponse.setVlanId(ipAddress.getVlanId()); + ipResponse.setVlanName(ApiDBUtils.findVlanById(ipAddress.getVlanId()).getVlanId()); } ipResponse.setObjectName("ipaddress"); return ipResponse; diff --git a/server/src/com/cloud/async/executor/DisassociateIpAddressExecutor.java b/server/src/com/cloud/async/executor/DisassociateIpAddressExecutor.java index 4021189b35e..fadc692ce81 100644 --- a/server/src/com/cloud/async/executor/DisassociateIpAddressExecutor.java +++ b/server/src/com/cloud/async/executor/DisassociateIpAddressExecutor.java @@ -98,7 +98,7 @@ public class DisassociateIpAddressExecutor extends BaseAsyncJobExecutor { if (ip.isSourceNat()) { router = routerDao.findByPublicIpAddress(param.getIpAddress()); } else { - router = routerDao.findBy(ip.getAccountId(), ip.getDataCenterId()); + router = routerDao.findBy(ip.getAllocatedToAccountId(), ip.getDataCenterId()); } return router; diff --git a/server/src/com/cloud/network/IPAddressVO.java b/server/src/com/cloud/network/IPAddressVO.java index 08a34ab8ae7..0ea5fa11d59 100644 --- a/server/src/com/cloud/network/IPAddressVO.java +++ b/server/src/com/cloud/network/IPAddressVO.java @@ -29,18 +29,16 @@ import javax.persistence.TemporalType; /** * A bean representing a public IP Address - * - * @author Will Chan * */ @Entity @Table(name=("user_ip_address")) public class IPAddressVO implements IpAddress { @Column(name="account_id") - private Long accountId = null; + private Long allocatedToAccountId = null; @Column(name="domain_id") - private Long domainId = null; + private Long allocatedInDomainId = null; @Id @Column(name="public_ip_address") @@ -54,22 +52,34 @@ public class IPAddressVO implements IpAddress { @Column(name="allocated") @Temporal(value=TemporalType.TIMESTAMP) - private Date allocated; + private Date allocatedTime; @Column(name="vlan_db_id") - private long vlanDbId; + private long vlanId; @Column(name="one_to_one_nat") private boolean oneToOneNat; + + @Column(name="state") + private State state; protected IPAddressVO() { + } + + @Override + public boolean readyToUse() { + return state == State.Allocated; } public IPAddressVO(String address, long dataCenterId, long vlanDbId, boolean sourceNat) { this.address = address; this.dataCenterId = dataCenterId; - this.vlanDbId = vlanDbId; - this.sourceNat = sourceNat; + this.vlanId = vlanDbId; + this.sourceNat = sourceNat; + this.allocatedInDomainId = null; + this.allocatedToAccountId = null; + this.allocatedTime = null; + this.state = State.Free; } @Override @@ -80,57 +90,51 @@ public class IPAddressVO implements IpAddress { @Override public String getAddress() { return address; - } + } + @Override - public Long getAccountId() { - return accountId; - } + public Long getAllocatedToAccountId() { + return allocatedToAccountId; + } + @Override - public Long getDomainId() { - return domainId; - } + public Long getAllocatedInDomainId() { + return allocatedInDomainId; + } + @Override - public Date getAllocated() { - return allocated; - } - @Override - public boolean isSourceNat() { - return sourceNat; + public Date getAllocatedTime() { + return allocatedTime; } - @Override - public void setAccountId(Long accountId) { - this.accountId = accountId; + public void setAllocatedToAccountId(Long accountId) { + this.allocatedToAccountId = accountId; } - @Override - public void setDomainId(Long domainId) { - this.domainId = domainId; + public void setAllocatedInDomainId(Long domainId) { + this.allocatedInDomainId = domainId; } - @Override public void setSourceNat(boolean sourceNat) { this.sourceNat = sourceNat; } @Override - public boolean getSourceNat() { - return this.sourceNat; + public boolean isSourceNat() { + return sourceNat; } - @Override - public void setAllocated(Date allocated) { - this.allocated = allocated; + public void setAllocatedTime(Date allocated) { + this.allocatedTime = allocated; } @Override - public long getVlanDbId() { - return this.vlanDbId; + public long getVlanId() { + return this.vlanId; } - @Override - public void setVlanDbId(long vlanDbId) { - this.vlanDbId = vlanDbId; + public void setVlanId(long vlanDbId) { + this.vlanId = vlanDbId; } @Override @@ -138,10 +142,28 @@ public class IPAddressVO implements IpAddress { return oneToOneNat; } - @Override public void setOneToOneNat(boolean oneToOneNat) { this.oneToOneNat = oneToOneNat; } + + @Override + public long getDomainId() { + return allocatedInDomainId == null ? -1 : allocatedInDomainId; + } + + @Override + public long getAccountId() { + return allocatedToAccountId == null ? -1 : allocatedToAccountId; + } + + @Override + public State getState() { + return state; + } + + public void setState(State state) { + this.state = state; + } @Override public String toString() { diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index ac7bbddb668..de7c70deeb6 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -469,7 +469,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag for (final String ipAddress: ipAddrList) { IPAddressVO ip = _ipAddressDao.findById(ipAddress); - VlanVO vlan = _vlanDao.findById(ip.getVlanDbId()); + VlanVO vlan = _vlanDao.findById(ip.getVlanId()); ArrayList ipList = vlanIpMap.get(vlan.getId()); if (ipList == null) { ipList = new ArrayList(); @@ -487,7 +487,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } }); for (final IPAddressVO ip: ipList) { - sourceNat = ip.getSourceNat(); + sourceNat = ip.isSourceNat(); VlanVO vlan = vlanAndIp.getKey(); String vlanId = vlan.getVlanId(); String vlanGateway = vlan.getVlanGateway(); @@ -713,7 +713,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag public boolean associateIP(final DomainRouterVO router, final String ipAddress, final boolean add, long vmId) { Commands cmds = new Commands(OnError.Continue); IPAddressVO ip = _ipAddressDao.findById(ipAddress); - VlanVO vlan = _vlanDao.findById(ip.getVlanDbId()); + VlanVO vlan = _vlanDao.findById(ip.getVlanId()); boolean sourceNat = ip.isSourceNat(); boolean firstIP = (!sourceNat && (_ipAddressDao.countIPs(vlan.getDataCenterId(), router.getAccountId(), vlan.getVlanId(), vlan.getVlanGateway(), vlan.getVlanNetmask()) == 1)); String vlanId = vlan.getVlanId(); @@ -762,13 +762,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return null; } - if (ip.getAllocated() == null) { + if (ip.getAllocatedTime() == null) { s_logger.debug("Ip Address is already rleeased: " + ipAddress); return null; } - ip.setAccountId(null); - ip.setDomainId(null); + ip.setAllocatedToAccountId(null); + ip.setAllocatedInDomainId(null); _ipAddressDao.update(ip.getAddress(), ip); txn.commit(); return ip; @@ -819,7 +819,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag final EventVO event = new EventVO(); event.setUserId(userId); - event.setAccountId(ip.getAccountId()); + event.setAccountId(ip.getAllocatedToAccountId()); event.setType(EventTypes.EVENT_NET_IP_RELEASE); event.setParameters("address=" + ipAddress + "\nsourceNat="+ip.isSourceNat()); event.setDescription("released a public ip: " + ipAddress); @@ -980,7 +980,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override public List listPublicIpAddressesInVirtualNetwork(long accountId, long dcId, Boolean sourceNat) { SearchBuilder ipAddressSB = _ipAddressDao.createSearchBuilder(); - ipAddressSB.and("accountId", ipAddressSB.entity().getAccountId(), SearchCriteria.Op.EQ); + ipAddressSB.and("accountId", ipAddressSB.entity().getAllocatedToAccountId(), SearchCriteria.Op.EQ); ipAddressSB.and("dataCenterId", ipAddressSB.entity().getDataCenterId(), SearchCriteria.Op.EQ); if (sourceNat != null) { ipAddressSB.and("sourceNat", ipAddressSB.entity().isSourceNat(), SearchCriteria.Op.EQ); @@ -988,7 +988,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag SearchBuilder virtualNetworkVlanSB = _vlanDao.createSearchBuilder(); virtualNetworkVlanSB.and("vlanType", virtualNetworkVlanSB.entity().getVlanType(), SearchCriteria.Op.EQ); - ipAddressSB.join("virtualNetworkVlanSB", virtualNetworkVlanSB, ipAddressSB.entity().getVlanDbId(), virtualNetworkVlanSB.entity().getId(), JoinBuilder.JoinType.INNER); + ipAddressSB.join("virtualNetworkVlanSB", virtualNetworkVlanSB, ipAddressSB.entity().getVlanId(), virtualNetworkVlanSB.entity().getId(), JoinBuilder.JoinType.INNER); SearchCriteria ipAddressSC = ipAddressSB.create(); ipAddressSC.setParameters("accountId", accountId); @@ -1325,8 +1325,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag private Account findAccountByIpAddress(String ipAddress) { IPAddressVO address = _ipAddressDao.findById(ipAddress); - if ((address != null) && (address.getAccountId() != null)) { - return _accountDao.findById(address.getAccountId()); + if ((address != null) && (address.getAllocatedToAccountId() != null)) { + return _accountDao.findById(address.getAllocatedToAccountId()); } return null; } @@ -1368,7 +1368,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return false; } - if (ipVO.getAllocated() == null) { + if (ipVO.getAllocatedTime() == null) { return true; } @@ -1377,18 +1377,18 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return false; } - if ((ipVO.getAccountId() == null) || (ipVO.getAccountId().longValue() != accountId)) { + if ((ipVO.getAllocatedToAccountId() == null) || (ipVO.getAllocatedToAccountId().longValue() != accountId)) { // FIXME: is the user visible in the admin account's domain???? if (!BaseCmd.isAdmin(Account.getType())) { if (s_logger.isDebugEnabled()) { s_logger.debug("permission denied disassociating IP address " + ipAddress + "; acct: " + accountId + "; ip (acct / dc / dom / alloc): " - + ipVO.getAccountId() + " / " + ipVO.getDataCenterId() + " / " + ipVO.getDomainId() + " / " + ipVO.getAllocated()); + + ipVO.getAllocatedToAccountId() + " / " + ipVO.getDataCenterId() + " / " + ipVO.getAllocatedInDomainId() + " / " + ipVO.getAllocatedTime()); } throw new PermissionDeniedException("User/account does not own supplied address"); } } - if (ipVO.getAllocated() == null) { + if (ipVO.getAllocatedTime() == null) { return true; } @@ -1396,13 +1396,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new IllegalArgumentException("ip address is used for source nat purposes and can not be disassociated."); } - VlanVO vlan = _vlanDao.findById(ipVO.getVlanDbId()); + VlanVO vlan = _vlanDao.findById(ipVO.getVlanId()); if (!vlan.getVlanType().equals(VlanType.VirtualNetwork)) { throw new IllegalArgumentException("only ip addresses that belong to a virtual network may be disassociated."); } //Check for account wide pool. It will have an entry for account_vlan_map. - if (_accountVlanMapDao.findAccountVlanMap(accountId,ipVO.getVlanDbId()) != null){ + if (_accountVlanMapDao.findAccountVlanMap(accountId,ipVO.getVlanId()) != null){ throw new PermissionDeniedException(ipAddress + " belongs to Account wide IP pool and cannot be disassociated"); } diff --git a/server/src/com/cloud/network/dao/IPAddressDaoImpl.java b/server/src/com/cloud/network/dao/IPAddressDaoImpl.java index 01ae7a8a408..b77b58e0bc2 100644 --- a/server/src/com/cloud/network/dao/IPAddressDaoImpl.java +++ b/server/src/com/cloud/network/dao/IPAddressDaoImpl.java @@ -16,8 +16,8 @@ * */ -package com.cloud.network.dao; - +package com.cloud.network.dao; + import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; @@ -29,218 +29,211 @@ import javax.ejb.Local; import org.apache.log4j.Logger; import com.cloud.network.IPAddressVO; +import com.cloud.network.IpAddress.State; import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; - -@Local(value={IPAddressDao.class}) -public class IPAddressDaoImpl extends GenericDaoBase implements IPAddressDao { +import com.cloud.utils.exception.CloudRuntimeException; + +@Local(value = { IPAddressDao.class }) +@DB +public class IPAddressDaoImpl extends GenericDaoBase implements IPAddressDao { private static final Logger s_logger = Logger.getLogger(IPAddressDaoImpl.class); - - protected SearchBuilder DcIpSearch; - protected SearchBuilder VlanDbIdSearchUnallocated; - protected SearchBuilder AccountSearch; - - // make it public for JUnit test - public IPAddressDaoImpl() { - DcIpSearch = createSearchBuilder(); - DcIpSearch.and("dataCenterId", DcIpSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); - DcIpSearch.and("ipAddress", DcIpSearch.entity().getAddress(), SearchCriteria.Op.EQ); - DcIpSearch.done(); - - VlanDbIdSearchUnallocated = createSearchBuilder(); - VlanDbIdSearchUnallocated.and("allocated", VlanDbIdSearchUnallocated.entity().getAllocated(), SearchCriteria.Op.NULL); - VlanDbIdSearchUnallocated.and("vlanDbId", VlanDbIdSearchUnallocated.entity().getVlanDbId(), SearchCriteria.Op.EQ); - //VlanDbIdSearchUnallocated.addRetrieve("ipAddress", VlanDbIdSearchUnallocated.entity().getAddress()); - VlanDbIdSearchUnallocated.done(); - - AccountSearch = createSearchBuilder(); - AccountSearch.and("accountId", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountSearch.done(); - } - + + protected final SearchBuilder AllFieldsSearch; + protected final SearchBuilder VlanDbIdSearchUnallocated; + protected final GenericSearchBuilder AllIpCount; + protected final GenericSearchBuilder AllocatedIpCount; + + // make it public for JUnit test + public IPAddressDaoImpl() { + AllFieldsSearch = createSearchBuilder(); + AllFieldsSearch.and("dataCenterId", AllFieldsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + AllFieldsSearch.and("ipAddress", AllFieldsSearch.entity().getAddress(), SearchCriteria.Op.EQ); + AllFieldsSearch.and("vlan", AllFieldsSearch.entity().getVlanId(), Op.EQ); + AllFieldsSearch.and("accountId", AllFieldsSearch.entity().getAllocatedToAccountId(), Op.EQ); + AllFieldsSearch.done(); + + VlanDbIdSearchUnallocated = createSearchBuilder(); + VlanDbIdSearchUnallocated.and("allocated", VlanDbIdSearchUnallocated.entity().getAllocatedTime(), SearchCriteria.Op.NULL); + VlanDbIdSearchUnallocated.and("vlanDbId", VlanDbIdSearchUnallocated.entity().getVlanId(), SearchCriteria.Op.EQ); + // VlanDbIdSearchUnallocated.addRetrieve("ipAddress", + // VlanDbIdSearchUnallocated.entity().getAddress()); + VlanDbIdSearchUnallocated.done(); + + AllIpCount = createSearchBuilder(Integer.class); + AllIpCount.and("dc", AllIpCount.entity().getDataCenterId(), Op.EQ); + AllIpCount.and("vlan", AllIpCount.entity().getVlanId(), Op.EQ); + AllIpCount.done(); + + AllocatedIpCount = createSearchBuilder(Integer.class); + AllocatedIpCount.and("dc", AllocatedIpCount.entity().getDataCenterId(), Op.EQ); + AllocatedIpCount.and("vlan", AllocatedIpCount.entity().getVlanId(), Op.EQ); + AllocatedIpCount.and("allocated", AllocatedIpCount.entity().getAllocatedTime(), Op.NNULL); + AllocatedIpCount.done(); + } + @Override - public boolean mark(long dcId, String ip) { - SearchCriteria sc = DcIpSearch.create(); - sc.setParameters("dataCenterId", dcId); - sc.setParameters("ipAddress", ip); - - IPAddressVO vo = createForUpdate(); - vo.setAllocated(new Date()); - - return update(vo, sc) >= 1; - } + public boolean mark(long dcId, String ip) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("dataCenterId", dcId); + sc.setParameters("ipAddress", ip); + + IPAddressVO vo = createForUpdate(); + vo.setAllocatedTime(new Date()); + + return update(vo, sc) >= 1; + } @Override @DB public List assignAcccountSpecificIps(long accountId, long domainId, Long vlanDbId, boolean sourceNat) { - - SearchBuilder VlanDbIdSearch = createSearchBuilder(); - VlanDbIdSearch.and("vlanDbId", VlanDbIdSearch.entity().getVlanDbId(), SearchCriteria.Op.EQ); - VlanDbIdSearch.and("sourceNat", VlanDbIdSearch.entity().getSourceNat(), SearchCriteria.Op.EQ); - VlanDbIdSearch.done(); - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - SearchCriteria sc = VlanDbIdSearch.create(); - sc.setParameters("vlanDbId", vlanDbId); - sc.setParameters("sourceNat", sourceNat); - - List ipList = this.lockRows(sc, null, true); - List ipStringList = new ArrayList(); - - for(IPAddressVO ip:ipList){ - - ip.setAccountId(accountId); - ip.setAllocated(new Date()); - ip.setDomainId(domainId); - ip.setSourceNat(sourceNat); - - if (!update(ip.getAddress(), ip)) { - s_logger.debug("Unable to retrieve ip address " + ip.getAddress()); - return null; - } - ipStringList.add(ip.getAddress()); - } - txn.commit(); - return ipStringList; - } catch (Exception e) { - s_logger.warn("Unable to assign IP", e); - } - return null; - - } - @Override - public void setIpAsSourceNat(String ipAddr){ - IPAddressVO ip = createForUpdate(ipAddr); - ip.setSourceNat(true); - s_logger.debug("Setting " + ipAddr + " as source Nat "); - update(ipAddr, ip); - } - - @Override - public String assignIpAddress(long accountId, long domainId, long vlanDbId, boolean sourceNat) { - - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - SearchCriteria sc = VlanDbIdSearchUnallocated.create(); - sc.setParameters("vlanDbId", vlanDbId); - - IPAddressVO ip = this.lockOneRandomRow(sc, true); - if(ip != null) { - ip.setAccountId(accountId); - ip.setAllocated(new Date()); - ip.setDomainId(domainId); - ip.setSourceNat(sourceNat); - - if (!update(ip.getAddress(), ip)) { - s_logger.debug("Unable to retrieve any ip addresses"); - return null; - } - - txn.commit(); - return ip.getAddress(); - } else { - txn.rollback(); - //we do not log this as an error now, as there can be multiple vlans across which we iterate - s_logger.warn("Unable to find an available IP address with related vlan, vlanDbId: " + vlanDbId); - } - } catch (Exception e) { - s_logger.warn("Unable to assign IP", e); - } - return null; - } - - @Override - public void unassignIpAddress(String ipAddress) { - IPAddressVO address = createForUpdate(); - address.setAccountId(null); - address.setDomainId(null); - address.setAllocated(null); - address.setSourceNat(false); - address.setOneToOneNat(false); - update(ipAddress, address); - } - - @Override - public void unassignIpAsSourceNat(String ipAddress) { - IPAddressVO address = createForUpdate(); - address.setSourceNat(false); - update(ipAddress, address); - } - - @Override - public List listByAccount(long accountId) { - SearchCriteria sc = AccountSearch.create(); - sc.setParameters("accountId", accountId); - return listIncludingRemovedBy(sc); - } - - @Override - public List listByDcIdIpAddress(long dcId, String ipAddress) { - SearchCriteria sc = DcIpSearch.create(); - sc.setParameters("dataCenterId", dcId); - sc.setParameters("ipAddress", ipAddress); - return listIncludingRemovedBy(sc); - } - - @Override @DB - public int countIPs(long dcId, long vlanDbId, boolean onlyCountAllocated) { - Transaction txn = Transaction.currentTxn(); - PreparedStatement pstmt = null; - int ipCount = 0; - try { - String sql = "SELECT count(*) from `cloud`.`user_ip_address` where data_center_id = " + dcId; - - if (vlanDbId != -1) { - sql += " AND vlan_db_id = " + vlanDbId; - } - - if (onlyCountAllocated) { - sql += " AND allocated IS NOT NULL"; - } - - pstmt = txn.prepareAutoCloseStatement(sql); - ResultSet rs = pstmt.executeQuery(); - - if (rs.next()) { - ipCount = rs.getInt(1); - } - - } catch (Exception e) { - s_logger.warn("Exception counting IP addresses", e); + SearchBuilder VlanDbIdSearch = createSearchBuilder(); + VlanDbIdSearch.and("vlanDbId", VlanDbIdSearch.entity().getVlanId(), SearchCriteria.Op.EQ); + VlanDbIdSearch.and("sourceNat", VlanDbIdSearch.entity().isSourceNat(), SearchCriteria.Op.EQ); + VlanDbIdSearch.done(); + Transaction txn = Transaction.currentTxn(); + try { + txn.start(); + SearchCriteria sc = VlanDbIdSearch.create(); + sc.setParameters("vlanDbId", vlanDbId); + sc.setParameters("sourceNat", sourceNat); + + List ipList = this.lockRows(sc, null, true); + List ipStringList = new ArrayList(); + + for (IPAddressVO ip : ipList) { + + ip.setAllocatedToAccountId(accountId); + ip.setAllocatedTime(new Date()); + ip.setAllocatedInDomainId(domainId); + ip.setSourceNat(sourceNat); + ip.setState(State.Allocated); + + if (!update(ip.getAddress(), ip)) { + s_logger.debug("Unable to retrieve ip address " + ip.getAddress()); + return null; + } + ipStringList.add(ip.getAddress()); + } + txn.commit(); + return ipStringList; + } catch (Exception e) { + s_logger.warn("Unable to assign IP", e); } - - return ipCount; - } - - @Override @DB - public int countIPs(long dcId, Long accountId, String vlanId, String vlanGateway, String vlanNetmask) { - Transaction txn = Transaction.currentTxn(); - int ipCount = 0; - try { - String sql = "SELECT count(*) FROM user_ip_address u INNER JOIN vlan v on (u.vlan_db_id = v.id AND v.data_center_id = ? AND v.vlan_id = ? AND v.vlan_gateway = ? AND v.vlan_netmask = ? AND u.account_id = ?)"; - - - PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql); - pstmt.setLong(1, dcId); - pstmt.setString(2, vlanId); - pstmt.setString(3, vlanGateway); - pstmt.setString(4, vlanNetmask); - pstmt.setLong(5, accountId); - ResultSet rs = pstmt.executeQuery(); - - if (rs.next()) { - ipCount = rs.getInt(1); - } - } catch (Exception e) { - s_logger.warn("Exception counting IP addresses", e); - } - - return ipCount; - } -} + return null; + + } + + @Override + public void setIpAsSourceNat(String ipAddr) { + + IPAddressVO ip = createForUpdate(ipAddr); + ip.setSourceNat(true); + s_logger.debug("Setting " + ipAddr + " as source Nat "); + update(ipAddr, ip); + } + + @Override + @DB + public String assignIpAddress(long accountId, long domainId, long vlanDbId, boolean sourceNat) { + Transaction txn = Transaction.currentTxn(); + txn.start(); + + SearchCriteria sc = VlanDbIdSearchUnallocated.create(); + sc.setParameters("vlanDbId", vlanDbId); + + IPAddressVO ip = this.lockOneRandomRow(sc, true); + if (ip == null) { + s_logger.info("Unable to get an ip address in " + vlanDbId); + return null; + } + + ip.setAllocatedToAccountId(accountId); + ip.setAllocatedTime(new Date()); + ip.setAllocatedInDomainId(domainId); + ip.setSourceNat(sourceNat); + ip.setState(State.Allocated); + + if (!update(ip.getAddress(), ip)) { + throw new CloudRuntimeException("How can I lock the row but can't update it: " + ip.getAddress()); + } + + txn.commit(); + return ip.getAddress(); + } + + @Override + public void unassignIpAddress(String ipAddress) { + IPAddressVO address = createForUpdate(); + address.setAllocatedToAccountId(null); + address.setAllocatedInDomainId(null); + address.setAllocatedTime(null); + address.setSourceNat(false); + address.setOneToOneNat(false); + address.setState(State.Free); + update(ipAddress, address); + } + + @Override + public void unassignIpAsSourceNat(String ipAddress) { + IPAddressVO address = createForUpdate(); + address.setSourceNat(false); + update(ipAddress, address); + } + + @Override + public List listByAccount(long accountId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("accountId", accountId); + return listIncludingRemovedBy(sc); + } + + @Override + public List listByDcIdIpAddress(long dcId, String ipAddress) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("dataCenterId", dcId); + sc.setParameters("ipAddress", ipAddress); + return listIncludingRemovedBy(sc); + } + + @Override + public int countIPs(long dcId, long vlanId, boolean onlyCountAllocated) { + SearchCriteria sc = onlyCountAllocated ? AllocatedIpCount.create() : AllIpCount.create(); + sc.setParameters("dc", dcId); + sc.setParameters("vlan", vlanId); + + return customSearch(sc, null).get(0); + } + + @Override + @DB + public int countIPs(long dcId, Long accountId, String vlanId, String vlanGateway, String vlanNetmask) { + Transaction txn = Transaction.currentTxn(); + int ipCount = 0; + try { + String sql = "SELECT count(*) FROM user_ip_address u INNER JOIN vlan v on (u.vlan_db_id = v.id AND v.data_center_id = ? AND v.vlan_id = ? AND v.vlan_gateway = ? AND v.vlan_netmask = ? AND u.account_id = ?)"; + + PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql); + pstmt.setLong(1, dcId); + pstmt.setString(2, vlanId); + pstmt.setString(3, vlanGateway); + pstmt.setString(4, vlanNetmask); + pstmt.setLong(5, accountId); + ResultSet rs = pstmt.executeQuery(); + + if (rs.next()) { + ipCount = rs.getInt(1); + } + } catch (Exception e) { + s_logger.warn("Exception counting IP addresses", e); + } + + return ipCount; + } +} diff --git a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java index 1b116f49044..3292eb55070 100644 --- a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java +++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java @@ -207,7 +207,7 @@ public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesManager, // make sure ip address exists IPAddressVO ipAddr = _ipAddressDao.findById(srcIp.addr()); - if (ipAddr == null || ipAddr.getAllocated() == null || ipAddr.getAccountId() == null) { + if (ipAddr == null || ipAddr.getAllocatedTime() == null || ipAddr.getAllocatedToAccountId() == null) { throw new InvalidParameterValueException("Unable to create load balancer rule, invalid IP address " + srcIp); } @@ -264,7 +264,7 @@ public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesManager, EventVO event = new EventVO(); event.setUserId(userId); - event.setAccountId(ipAddr.getAccountId()); + event.setAccountId(ipAddr.getAllocatedToAccountId()); event.setType(EventTypes.EVENT_LOAD_BALANCER_CREATE); if (!success) { diff --git a/server/src/com/cloud/network/router/DomainRouterManagerImpl.java b/server/src/com/cloud/network/router/DomainRouterManagerImpl.java index 312e92d5739..af7458d8074 100644 --- a/server/src/com/cloud/network/router/DomainRouterManagerImpl.java +++ b/server/src/com/cloud/network/router/DomainRouterManagerImpl.java @@ -488,7 +488,7 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute // Find the VLAN ID, VLAN gateway, and VLAN netmask for publicIpAddress IPAddressVO ipVO = _ipAddressDao.findById(publicIpAddress); - VlanVO vlan = _vlanDao.findById(ipVO.getVlanDbId()); + VlanVO vlan = _vlanDao.findById(ipVO.getVlanId()); String vlanId = vlan.getVlanId(); String vlanGateway = vlan.getVlanGateway(); String vlanNetmask = vlan.getVlanNetmask(); diff --git a/server/src/com/cloud/network/rules/RulesManagerImpl.java b/server/src/com/cloud/network/rules/RulesManagerImpl.java index d241abac093..a0be9c2e827 100644 --- a/server/src/com/cloud/network/rules/RulesManagerImpl.java +++ b/server/src/com/cloud/network/rules/RulesManagerImpl.java @@ -110,7 +110,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { @Override public void checkIpAndUserVm(IpAddress ipAddress, UserVm userVm, Account caller) throws InvalidParameterValueException, PermissionDeniedException { - if (ipAddress == null || ipAddress.getAllocated() == null || ipAddress.getAccountId() == null) { + if (ipAddress == null || ipAddress.getAllocatedTime() == null || ipAddress.getAllocatedToAccountId() == null) { throw new InvalidParameterValueException("Unable to create ip forwarding rule on address " + ipAddress + ", invalid IP address specified."); } @@ -125,7 +125,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { _accountMgr.checkAccess(caller, userVm); // validate that IP address and userVM belong to the same account - if (ipAddress.getAccountId().longValue() != userVm.getAccountId()) { + if (ipAddress.getAllocatedToAccountId().longValue() != userVm.getAccountId()) { throw new InvalidParameterValueException("Unable to create ip forwarding rule, IP address " + ipAddress + " owner is not the same as owner of virtual machine " + userVm.toString()); } @@ -342,7 +342,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { Account caller = UserContext.current().getAccount(); IPAddressVO ipAddressVO = _ipAddressDao.findById(ipAddress.addr()); - if (ipAddressVO == null || ipAddressVO.getAllocated() == null) { + if (ipAddressVO == null || ipAddressVO.getAllocatedTime() == null) { throw new InvalidParameterValueException("Unable to find IP address " + ipAddress); } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 888e835697d..869307d2433 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -2354,8 +2354,8 @@ public class ManagementServerImpl implements ManagementServer { @Override public Account findAccountByIpAddress(String ipAddress) { IPAddressVO address = _publicIpAddressDao.findById(ipAddress); - if ((address != null) && (address.getAccountId() != null)) { - return _accountDao.findById(address.getAccountId()); + if ((address != null) && (address.getAllocatedToAccountId() != null)) { + return _accountDao.findById(address.getAllocatedToAccountId()); } return null; } @@ -3266,26 +3266,26 @@ public class ManagementServerImpl implements ManagementServer { Object forVirtualNetwork = cmd.isForVirtualNetwork(); SearchBuilder sb = _publicIpAddressDao.createSearchBuilder(); - sb.and("accountIdEQ", sb.entity().getAccountId(), SearchCriteria.Op.EQ); + sb.and("accountIdEQ", sb.entity().getAllocatedToAccountId(), SearchCriteria.Op.EQ); sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ); sb.and("address", sb.entity().getAddress(), SearchCriteria.Op.LIKE); - sb.and("vlanDbId", sb.entity().getVlanDbId(), SearchCriteria.Op.EQ); + sb.and("vlanDbId", sb.entity().getVlanId(), SearchCriteria.Op.EQ); if ((accountId == null) && (domainId != null)) { // if accountId isn't specified, we can do a domain match for the admin case SearchBuilder domainSearch = _domainDao.createSearchBuilder(); domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE); - sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER); + sb.join("domainSearch", domainSearch, sb.entity().getAllocatedInDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER); } if (forVirtualNetwork != null) { SearchBuilder vlanSearch = _vlanDao.createSearchBuilder(); vlanSearch.and("vlanType", vlanSearch.entity().getVlanType(), SearchCriteria.Op.EQ); - sb.join("vlanSearch", vlanSearch, sb.entity().getVlanDbId(), vlanSearch.entity().getId(), JoinBuilder.JoinType.INNER); + sb.join("vlanSearch", vlanSearch, sb.entity().getVlanId(), vlanSearch.entity().getId(), JoinBuilder.JoinType.INNER); } if ((isAllocated != null) && (isAllocated == true)) { - sb.and("allocated", sb.entity().getAllocated(), SearchCriteria.Op.NNULL); + sb.and("allocated", sb.entity().getAllocatedTime(), SearchCriteria.Op.NNULL); } SearchCriteria sc = sb.create(); @@ -5495,7 +5495,7 @@ public class ManagementServerImpl implements ManagementServer { if (ipAddressVO == null) { throw new InvalidParameterValueException("Unable to list remote access vpns, IP address " + ipAddress + " not found."); } else { - Long ipAddrAcctId = ipAddressVO.getAccountId(); + Long ipAddrAcctId = ipAddressVO.getAllocatedToAccountId(); if (ipAddrAcctId == null) { throw new InvalidParameterValueException("Unable to list remote access vpns, IP address " + ipAddress + " is not associated with an account."); } diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java index 323cf003a63..1bc40aa3508 100755 --- a/server/src/com/cloud/user/AccountManagerImpl.java +++ b/server/src/com/cloud/user/AccountManagerImpl.java @@ -872,7 +872,7 @@ public class AccountManagerImpl implements AccountManager, AccountService { } for (IPAddressVO ip : ips) { - List podVlanMaps = _podVlanMapDao.listPodVlanMapsByVlan(ip.getVlanDbId()); + List podVlanMaps = _podVlanMapDao.listPodVlanMapsByVlan(ip.getVlanId()); if (podVlanMaps != null && podVlanMaps.size() != 0) { Long podId = podVlanMaps.get(0).getPodId(); if (podId != null) { diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index f126ab1046d..3dd1dbaaa1e 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -1554,7 +1554,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, VirtualM if (offering.getGuestIpType() != NetworkOffering.GuestIpType.Virtual) { IPAddressVO guestIP = (userVm.getGuestIpAddress() == null) ? null : _ipAddressDao.findById(userVm.getGuestIpAddress()); - if (guestIP != null && guestIP.getAllocated() != null) { + if (guestIP != null && guestIP.getAllocatedTime() != null) { _ipAddressDao.unassignIpAddress(userVm.getGuestIpAddress()); s_logger.debug("Released guest IP address=" + userVm.getGuestIpAddress() + " vmName=" + userVm.getHostName() + " dcId=" + userVm.getDataCenterId());