mirror of https://github.com/apache/cloudstack.git
VPC: moved vpc related code from NetworkManager to VpcManager
This commit is contained in:
parent
cee948681f
commit
21f2d1b779
|
|
@ -240,7 +240,12 @@ public class AssociateIPAddrCmd extends BaseAsyncCreateCmd {
|
|||
|
||||
IpAddress result = null;
|
||||
|
||||
result = _networkService.associateIP(getEntityId(), getNetworkId(), getVpcId());
|
||||
if (getVpcId() != null) {
|
||||
result = _vpcService.associateIPToVpc(getEntityId(), getVpcId());
|
||||
} else if (getNetworkId() != null) {
|
||||
result = _networkService.associateIPToNetwork(getEntityId(), getNetworkId());
|
||||
}
|
||||
|
||||
|
||||
if (result != null) {
|
||||
IPAddressResponse ipResponse = _responseGenerator.createIPAddressResponse(result);
|
||||
|
|
|
|||
|
|
@ -112,14 +112,26 @@ public class UpdateNetworkCmd extends BaseAsyncCmd {
|
|||
@Override
|
||||
public void execute() throws InsufficientCapacityException, ConcurrentOperationException{
|
||||
User callerUser = _accountService.getActiveUser(UserContext.current().getCallerUserId());
|
||||
Account callerAccount = _accountService.getActiveAccountById(callerUser.getAccountId());
|
||||
Network result = _networkService.updateGuestNetwork(getId(), getNetworkName(), getDisplayText(), callerAccount,
|
||||
callerUser, getNetworkDomain(), getNetworkOfferingId(), getChangeCidr());
|
||||
Account callerAccount = _accountService.getActiveAccountById(callerUser.getAccountId());
|
||||
Network network = _networkService.getNetwork(id);
|
||||
if (network == null) {
|
||||
throw new InvalidParameterValueException("Couldn't find network by id", null);
|
||||
}
|
||||
|
||||
Network result = null;
|
||||
if (network.getVpcId() != null) {
|
||||
result = _vpcService.updateVpcGuestNetwork(getId(), getNetworkName(), getDisplayText(), callerAccount,
|
||||
callerUser, getNetworkDomain(), getNetworkOfferingId(), getChangeCidr());
|
||||
} else {
|
||||
result = _networkService.updateGuestNetwork(getId(), getNetworkName(), getDisplayText(), callerAccount,
|
||||
callerUser, getNetworkDomain(), getNetworkOfferingId(), getChangeCidr());
|
||||
}
|
||||
|
||||
if (result != null) {
|
||||
NetworkResponse response = _responseGenerator.createNetworkResponse(result);
|
||||
response.setResponseName(getCommandName());
|
||||
this.setResponseObject(response);
|
||||
}else {
|
||||
} else {
|
||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update network");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -135,16 +135,15 @@ public interface NetworkService {
|
|||
boolean isVmPartOfNetwork(long vmId, long ntwkId);
|
||||
|
||||
/**
|
||||
* @param entityId
|
||||
* @param networkId
|
||||
* @param vpcId
|
||||
* @param entityId
|
||||
* @return
|
||||
* @throws ConcurrentOperationException
|
||||
* @throws ResourceUnavailableException
|
||||
* @throws ResourceAllocationException
|
||||
* @throws InsufficientAddressCapacityException
|
||||
*/
|
||||
IpAddress associateIP(long ipId, Long networkId, Long vpcId) throws InsufficientAddressCapacityException,
|
||||
IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAddressCapacityException,
|
||||
ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -19,13 +19,17 @@ import java.util.Set;
|
|||
import com.cloud.api.commands.ListPrivateGatewaysCmd;
|
||||
import com.cloud.api.commands.ListStaticRoutesCmd;
|
||||
import com.cloud.exception.ConcurrentOperationException;
|
||||
import com.cloud.exception.InsufficientAddressCapacityException;
|
||||
import com.cloud.exception.InsufficientCapacityException;
|
||||
import com.cloud.exception.NetworkRuleConflictException;
|
||||
import com.cloud.exception.ResourceAllocationException;
|
||||
import com.cloud.exception.ResourceUnavailableException;
|
||||
import com.cloud.network.IpAddress;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.Network.Provider;
|
||||
import com.cloud.network.Network.Service;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.User;
|
||||
|
||||
/**
|
||||
* @author Alena Prokharchyk
|
||||
|
|
@ -221,4 +225,19 @@ public interface VpcService {
|
|||
* @return
|
||||
*/
|
||||
VpcGateway getVpcGateway(long id);
|
||||
|
||||
/**
|
||||
* @param ipId
|
||||
* @param vpcId
|
||||
* @return
|
||||
* @throws ResourceAllocationException
|
||||
* @throws ResourceUnavailableException
|
||||
* @throws InsufficientAddressCapacityException
|
||||
* @throws ConcurrentOperationException
|
||||
*/
|
||||
IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationException, ResourceUnavailableException,
|
||||
InsufficientAddressCapacityException, ConcurrentOperationException;
|
||||
|
||||
public Network updateVpcGuestNetwork(long networkId, String name, String displayText, Account callerAccount,
|
||||
User callerUser, String domainSuffix, Long ntwkOffId, Boolean changeCidr);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@ import com.cloud.network.element.UserDataServiceProvider;
|
|||
import com.cloud.network.guru.NetworkGuru;
|
||||
import com.cloud.network.rules.FirewallRule;
|
||||
import com.cloud.network.rules.StaticNat;
|
||||
import com.cloud.network.vpc.Vpc;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.offerings.NetworkOfferingVO;
|
||||
import com.cloud.user.Account;
|
||||
|
|
@ -311,16 +310,6 @@ public interface NetworkManager extends NetworkService {
|
|||
PublicIp assignSourceNatIpAddressToGuestNetwork(Account owner, Network guestNetwork) throws InsufficientAddressCapacityException, ConcurrentOperationException;
|
||||
|
||||
|
||||
/**
|
||||
* @param owner
|
||||
* @param vpc
|
||||
* @return
|
||||
* @throws ConcurrentOperationException
|
||||
* @throws InsufficientAddressCapacityException
|
||||
*/
|
||||
PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException;
|
||||
|
||||
|
||||
/**
|
||||
* @param accountId
|
||||
* @param zoneId
|
||||
|
|
@ -401,13 +390,6 @@ public interface NetworkManager extends NetworkService {
|
|||
InsufficientAddressCapacityException, ConcurrentOperationException;
|
||||
|
||||
|
||||
/**
|
||||
* @param ipId
|
||||
* @param networkId TODO
|
||||
*/
|
||||
void unassignIPFromVpcNetwork(long ipId, long networkId);
|
||||
|
||||
|
||||
/**
|
||||
* @param vm
|
||||
* @param networkId
|
||||
|
|
@ -471,9 +453,21 @@ public interface NetworkManager extends NetworkService {
|
|||
|
||||
|
||||
/**
|
||||
* @param ip
|
||||
* @return
|
||||
* @param addr
|
||||
*/
|
||||
boolean ipUsedInVpc(IpAddress ip);
|
||||
void markPublicIpAsAllocated(IPAddressVO addr);
|
||||
|
||||
|
||||
/**
|
||||
* @param owner
|
||||
* @param guestNtwkId
|
||||
* @param vpcId
|
||||
* @param dcId
|
||||
* @param isSourceNat
|
||||
* @return
|
||||
* @throws ConcurrentOperationException
|
||||
* @throws InsufficientAddressCapacityException
|
||||
*/
|
||||
PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId, boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -155,7 +155,6 @@ import com.cloud.network.rules.StaticNatRuleImpl;
|
|||
import com.cloud.network.rules.dao.PortForwardingRulesDao;
|
||||
import com.cloud.network.vpc.NetworkACLManager;
|
||||
import com.cloud.network.vpc.PrivateIpVO;
|
||||
import com.cloud.network.vpc.Vpc;
|
||||
import com.cloud.network.vpc.VpcManager;
|
||||
import com.cloud.network.vpc.Dao.PrivateIpDao;
|
||||
import com.cloud.network.vpn.RemoteAccessVpnService;
|
||||
|
|
@ -467,7 +466,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
}
|
||||
|
||||
@DB
|
||||
protected void markPublicIpAsAllocated(IPAddressVO addr) {
|
||||
@Override
|
||||
public void markPublicIpAsAllocated(IPAddressVO addr) {
|
||||
|
||||
assert (addr.getState() == IpAddress.State.Allocating || addr.getState() == IpAddress.State.Free) :
|
||||
"Unable to transition from state " + addr.getState() + " to " + IpAddress.State.Allocated;
|
||||
|
|
@ -499,25 +499,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
txn.commit();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException {
|
||||
long dcId = vpc.getZoneId();
|
||||
|
||||
IPAddressVO sourceNatIp = getExistingSourceNat(owner.getId(), null, vpc.getId());
|
||||
|
||||
PublicIp ipToReturn = null;
|
||||
|
||||
if (sourceNatIp != null) {
|
||||
ipToReturn = new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()),
|
||||
NetUtils.createSequenceBasedMacAddress(sourceNatIp.getMacAddress()));
|
||||
} else {
|
||||
ipToReturn = assignDedicateIpAddress(owner, null, vpc.getId(), dcId, true);
|
||||
}
|
||||
|
||||
return ipToReturn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublicIp assignSourceNatIpAddressToGuestNetwork(Account owner, Network guestNetwork)
|
||||
throws InsufficientAddressCapacityException, ConcurrentOperationException {
|
||||
|
|
@ -525,7 +506,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
"can't participate in source nat. What do you have to say for yourself?";
|
||||
long dcId = guestNetwork.getDataCenterId();
|
||||
|
||||
IPAddressVO sourceNatIp = getExistingSourceNat(owner.getId(), guestNetwork.getId(), guestNetwork.getVpcId());
|
||||
IPAddressVO sourceNatIp = getExistingSourceNatInNetwork(owner.getId(), guestNetwork.getId());
|
||||
|
||||
PublicIp ipToReturn = null;
|
||||
if (sourceNatIp != null) {
|
||||
|
|
@ -545,6 +526,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
|
||||
|
||||
@DB
|
||||
@Override
|
||||
public PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId, boolean isSourceNat)
|
||||
throws ConcurrentOperationException, InsufficientAddressCapacityException {
|
||||
|
||||
|
|
@ -1110,15 +1092,10 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
return ip;
|
||||
}
|
||||
|
||||
|
||||
protected IPAddressVO getExistingSourceNatInNetwork(long ownerId, Long networkId) {
|
||||
|
||||
protected IPAddressVO getExistingSourceNat(long ownerId, Long networkId, Long vpcId) {
|
||||
|
||||
List<IPAddressVO> addrs = null;
|
||||
if (vpcId != null) {
|
||||
addrs = listPublicIpsAssignedToVpc(ownerId, true, vpcId);
|
||||
} else if (networkId != null) {
|
||||
addrs = listPublicIpsAssignedToGuestNtwk(ownerId, networkId, true);
|
||||
}
|
||||
List<IPAddressVO> addrs = listPublicIpsAssignedToGuestNtwk(ownerId, networkId, true);
|
||||
|
||||
IPAddressVO sourceNatIp = null;
|
||||
if (addrs.isEmpty()) {
|
||||
|
|
@ -1133,7 +1110,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
}
|
||||
|
||||
assert (sourceNatIp != null) : "How do we get a bunch of ip addresses but none of them are source nat? " +
|
||||
"account=" + ownerId + "; networkId=" + networkId + "; vpcId=" + vpcId;
|
||||
"account=" + ownerId + "; networkId=" + networkId;
|
||||
}
|
||||
|
||||
return sourceNatIp;
|
||||
|
|
@ -1196,7 +1173,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
boolean sharedSourceNat = offering.getSharedSourceNat();
|
||||
boolean isSourceNat = false;
|
||||
if (!sharedSourceNat) {
|
||||
if (getExistingSourceNat(owner.getId(), networkId, null) == null) {
|
||||
if (getExistingSourceNatInNetwork(owner.getId(), networkId) == null) {
|
||||
if (network.getGuestType() == GuestType.Isolated && network.getVpcId() == null) {
|
||||
isSourceNat = true;
|
||||
}
|
||||
|
|
@ -1239,55 +1216,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@DB
|
||||
protected IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationException, ResourceUnavailableException,
|
||||
InsufficientAddressCapacityException, ConcurrentOperationException {
|
||||
Account caller = UserContext.current().getCaller();
|
||||
Account owner = null;
|
||||
|
||||
IpAddress ipToAssoc = getIp(ipId);
|
||||
if (ipToAssoc != null) {
|
||||
_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;
|
||||
}
|
||||
|
||||
Vpc vpc = _vpcMgr.getVpc(vpcId);
|
||||
if (vpc == null) {
|
||||
throw new InvalidParameterValueException("Invalid VPC id provided", null);
|
||||
}
|
||||
|
||||
// check permissions
|
||||
_accountMgr.checkAccess(caller, null, true, owner, vpc);
|
||||
|
||||
boolean isSourceNat = false;
|
||||
if (getExistingSourceNat(owner.getId(), null, vpcId) == null) {
|
||||
isSourceNat = true;
|
||||
}
|
||||
|
||||
s_logger.debug("Associating ip " + ipToAssoc + " to vpc " + vpc);
|
||||
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
IPAddressVO ip = _ipAddressDao.findById(ipId);
|
||||
//update ip address with networkId
|
||||
ip.setVpcId(vpcId);
|
||||
ip.setSourceNat(isSourceNat);
|
||||
_ipAddressDao.update(ipId, ip);
|
||||
|
||||
//mark ip as allocated
|
||||
markPublicIpAsAllocated(ip);
|
||||
txn.commit();
|
||||
|
||||
s_logger.debug("Successfully assigned ip " + ipToAssoc + " to vpc " + vpc);
|
||||
|
||||
return _ipAddressDao.findById(ipId);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@DB
|
||||
public boolean disassociatePublicIpAddress(long addrId, long userId, Account caller) {
|
||||
|
|
@ -1652,18 +1580,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
return _ipAddressDao.search(sc, null);
|
||||
}
|
||||
|
||||
protected List<IPAddressVO> listPublicIpsAssignedToVpc(long accountId, Boolean sourceNat, long vpcId) {
|
||||
SearchCriteria<IPAddressVO> sc = IpAddressSearch.create();
|
||||
sc.setParameters("accountId", accountId);
|
||||
sc.setParameters("vpcId", vpcId);
|
||||
|
||||
if (sourceNat != null) {
|
||||
sc.addAnd("sourceNat", SearchCriteria.Op.EQ, sourceNat);
|
||||
}
|
||||
sc.setJoinParameters("virtualNetworkVlanSB", "vlanType", VlanType.VirtualNetwork);
|
||||
|
||||
return _ipAddressDao.search(sc, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NetworkVO> setupNetwork(Account owner, NetworkOfferingVO offering, DeploymentPlan plan, String name,
|
||||
|
|
@ -2073,24 +1989,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
&& !sharedSourceNat) {
|
||||
|
||||
List<IPAddressVO> ips = null;
|
||||
Vpc vpc = null;
|
||||
if (network.getVpcId() != null) {
|
||||
vpc = _vpcMgr.getVpc(network.getVpcId());
|
||||
ips = _ipAddressDao.listByAssociatedVpc(vpc.getId(), true);
|
||||
ips = _ipAddressDao.listByAssociatedVpc(network.getVpcId(), true);
|
||||
if (ips.isEmpty()) {
|
||||
throw new CloudRuntimeException("Vpc is not implemented; there is no source nat ip");
|
||||
}
|
||||
} else {
|
||||
ips = _ipAddressDao.listByAssociatedNetwork(network.getId(), true);
|
||||
}
|
||||
|
||||
|
||||
if (ips.isEmpty()) {
|
||||
String target = vpc != null ? vpc.toString() : network.toString();
|
||||
s_logger.debug("Creating a source nat ip for " + target);
|
||||
s_logger.debug("Creating a source nat ip for network " + network);
|
||||
Account owner = _accountMgr.getAccount(network.getAccountId());
|
||||
if (vpc != null) {
|
||||
assignSourceNatIpAddressToVpc(owner, vpc);
|
||||
} else {
|
||||
assignSourceNatIpAddressToGuestNetwork(owner, network);
|
||||
}
|
||||
assignSourceNatIpAddressToGuestNetwork(owner, network);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2621,17 +2532,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
throw ex;
|
||||
}
|
||||
|
||||
//validate vpc
|
||||
if (vpcId != null) {
|
||||
Vpc vpc = _vpcMgr.getActiveVpc(vpcId);
|
||||
if (vpc == null) {
|
||||
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC ", null);
|
||||
ex.addProxyObject("vpc", vpcId, "VPC");
|
||||
throw ex;
|
||||
}
|
||||
_accountMgr.checkAccess(caller, null, false, vpc);
|
||||
}
|
||||
|
||||
// Only domain and account ACL types are supported in Acton.
|
||||
ACLType aclType = null;
|
||||
if (aclTypeStr != null) {
|
||||
|
|
@ -2803,8 +2703,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
if (!_configMgr.isOfferingForVpc(ntwkOff)){
|
||||
throw new InvalidParameterValueException("Network offering can't be used for VPC networks", null);
|
||||
}
|
||||
network = createVpcGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId,
|
||||
networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId);
|
||||
network = _vpcMgr.createVpcGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId,
|
||||
networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId, caller);
|
||||
} else {
|
||||
if (_configMgr.isOfferingForVpc(ntwkOff)){
|
||||
throw new InvalidParameterValueException("Network offering can be used for VPC networks only", null);
|
||||
|
|
@ -2823,31 +2723,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
|
||||
return network;
|
||||
}
|
||||
|
||||
@DB
|
||||
protected Network createVpcGuestNetwork(long ntwkOffId, String name, String displayText, String gateway,
|
||||
String cidr, String vlanId, String networkDomain, Account owner, Long domainId,
|
||||
PhysicalNetwork pNtwk, long zoneId, ACLType aclType, Boolean subdomainAccess, long vpcId)
|
||||
throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException {
|
||||
|
||||
Vpc vpc = _vpcMgr.getActiveVpc(vpcId);
|
||||
if (networkDomain == null) {
|
||||
networkDomain = vpc.getNetworkDomain();
|
||||
}
|
||||
|
||||
if (vpc.getZoneId() != zoneId) {
|
||||
throw new InvalidParameterValueException("New network doesn't belong to vpc zone", null);
|
||||
}
|
||||
|
||||
//1) Validate if network can be created for VPC
|
||||
_vpcMgr.validateGuestNtkwForVpc(_configMgr.getNetworkOffering(ntwkOffId), cidr, networkDomain, owner, vpc, null, gateway);
|
||||
|
||||
//2) Create network
|
||||
Network guestNetwork = createGuestNetwork(ntwkOffId, name, displayText, gateway, cidr, vlanId,
|
||||
networkDomain, owner, domainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId);
|
||||
|
||||
return guestNetwork;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@DB
|
||||
|
|
@ -4710,12 +4586,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
throw new InvalidParameterValueException("Can't update as the new offering is " + errMsg, null);
|
||||
}
|
||||
|
||||
//perform below validation if the network is vpc network
|
||||
if (network.getVpcId() != null) {
|
||||
Vpc vpc = _vpcMgr.getVpc(network.getVpcId());
|
||||
_vpcMgr.validateGuestNtkwForVpc(networkOffering, null, null, null,vpc, networkId, null);
|
||||
}
|
||||
|
||||
if (networkOfferingId != oldNetworkOfferingId) {
|
||||
if (networkOfferingIsConfiguredForExternalNetworking(networkOfferingId) != networkOfferingIsConfiguredForExternalNetworking(oldNetworkOfferingId)
|
||||
&& !changeCidr) {
|
||||
|
|
@ -6071,7 +5941,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
IPAddressVO ip = markIpAsUnavailable(ipToRelease.getId());
|
||||
assert (ip != null) : "Unable to mark the ip address id=" + ipToRelease.getId() + " as unavailable.";
|
||||
} else {
|
||||
unassignIPFromVpcNetwork(ipToRelease.getId(), network.getId());
|
||||
_vpcMgr.unassignIPFromVpcNetwork(ipToRelease.getId(), network.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -7075,7 +6945,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
// allocate ip
|
||||
ip = allocateIP(owner, true, guestNetwork.getDataCenterId());
|
||||
// apply ip associations
|
||||
ip = associateIP(ip.getId(), networkId, null);
|
||||
ip = associateIPToNetwork(ip.getId(), networkId);
|
||||
} catch (ResourceAllocationException ex) {
|
||||
throw new CloudRuntimeException("Failed to allocate system ip due to ", ex);
|
||||
} catch (ConcurrentOperationException ex) {
|
||||
|
|
@ -7235,26 +7105,20 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
|
||||
@Override
|
||||
@ActionEvent(eventType = EventTypes.EVENT_NET_IP_ASSIGN, eventDescription = "associating Ip", async = true)
|
||||
public IpAddress associateIP(long ipId, Long networkId, Long vpcId) throws InsufficientAddressCapacityException,
|
||||
public IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAddressCapacityException,
|
||||
ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
|
||||
if (vpcId != null) {
|
||||
return associateIPToVpc(ipId, vpcId);
|
||||
|
||||
Network network = _networksDao.findById(networkId);
|
||||
if (network == null) {
|
||||
throw new InvalidParameterValueException("Invalid network id is given", null);
|
||||
}
|
||||
|
||||
if (networkId != null) {
|
||||
Network network = _networksDao.findById(networkId);
|
||||
if (network == null) {
|
||||
throw new InvalidParameterValueException("Invalid network id is given", null);
|
||||
}
|
||||
|
||||
if (network.getVpcId() != null) {
|
||||
throw new InvalidParameterValueException("Can't assign ip to the network directly when network belongs" +
|
||||
" to VPC.Specify vpcId to associate ip address to VPC", null);
|
||||
}
|
||||
return associateIPToGuestNetwork(ipId, networkId, true);
|
||||
if (network.getVpcId() != null) {
|
||||
throw new InvalidParameterValueException("Can't assign ip to the network directly when network belongs" +
|
||||
" to VPC.Specify vpcId to associate ip address to VPC", null);
|
||||
}
|
||||
|
||||
return null;
|
||||
return associateIPToGuestNetwork(ipId, networkId, true);
|
||||
|
||||
}
|
||||
|
||||
private PhysicalNetwork getOnePhysicalNetworkByZoneAndTrafficType(long zoneId, TrafficType trafficType) {
|
||||
|
|
@ -7275,45 +7139,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
return networkList.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unassignIPFromVpcNetwork(long ipId, long networkId) {
|
||||
IPAddressVO ip = _ipAddressDao.findById(ipId);
|
||||
if (ipUsedInVpc(ip)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ip == null || ip.getVpcId() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
s_logger.debug("Releasing VPC ip address " + ip + " from vpc network id=" + networkId);
|
||||
|
||||
long vpcId = ip.getVpcId();
|
||||
boolean success = false;
|
||||
try {
|
||||
//unassign ip from the VPC router
|
||||
success = applyIpAssociations(getNetwork(networkId), true);
|
||||
} catch (ResourceUnavailableException ex) {
|
||||
throw new CloudRuntimeException("Failed to apply ip associations for network id=" + networkId +
|
||||
" as a part of unassigning ip " + ipId + " from vpc", ex);
|
||||
}
|
||||
|
||||
if (success) {
|
||||
ip.setAssociatedWithNetworkId(null);
|
||||
_ipAddressDao.update(ipId, ip);
|
||||
s_logger.debug("IP address " + ip + " is no longer associated with the network inside vpc id=" + vpcId);
|
||||
} else {
|
||||
throw new CloudRuntimeException("Failed to apply ip associations for network id=" + networkId +
|
||||
" as a part of unassigning ip " + ipId + " from vpc");
|
||||
}
|
||||
s_logger.debug("Successfully released VPC ip address " + ip + " back to VPC pool ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ipUsedInVpc(IpAddress ip) {
|
||||
return (ip != null && ip.getVpcId() != null &&
|
||||
(ip.isOneToOneNat() || !_firewallDao.listByIp(ip.getId()).isEmpty()));
|
||||
}
|
||||
|
||||
@Override @DB
|
||||
public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId,
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ import com.cloud.network.rules.FirewallRule.State;
|
|||
import com.cloud.network.rules.FirewallRuleVO;
|
||||
import com.cloud.network.rules.PortForwardingRuleVO;
|
||||
import com.cloud.network.rules.dao.PortForwardingRulesDao;
|
||||
import com.cloud.network.vpc.VpcManager;
|
||||
import com.cloud.projects.Project.ListProjectResourcesCriteria;
|
||||
import com.cloud.server.ResourceTag.TaggedResourceType;
|
||||
import com.cloud.tags.ResourceTagVO;
|
||||
|
|
@ -108,6 +109,8 @@ public class FirewallManagerImpl implements FirewallService, FirewallManager, Ma
|
|||
UserVmDao _vmDao;
|
||||
@Inject
|
||||
ResourceTagDao _resourceTagDao;
|
||||
@Inject
|
||||
VpcManager _vpcMgr;
|
||||
|
||||
private boolean _elbEnabled = false;
|
||||
|
||||
|
|
@ -465,7 +468,7 @@ public class FirewallManagerImpl implements FirewallService, FirewallManager, Ma
|
|||
if (rule.getSourceIpAddressId() != null) {
|
||||
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
|
||||
IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
|
||||
_networkMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
|
||||
_vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -102,6 +102,7 @@ import com.cloud.network.rules.LbStickinessMethod.LbStickinessMethodParam;
|
|||
import com.cloud.network.rules.LoadBalancer;
|
||||
import com.cloud.network.rules.RulesManager;
|
||||
import com.cloud.network.rules.StickinessPolicy;
|
||||
import com.cloud.network.vpc.VpcManager;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.projects.Project.ListProjectResourcesCriteria;
|
||||
import com.cloud.server.ResourceTag.TaggedResourceType;
|
||||
|
|
@ -212,6 +213,8 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
|
|||
DataCenterDao _dcDao = null;
|
||||
@Inject
|
||||
UserDao _userDao;
|
||||
@Inject
|
||||
VpcManager _vpcMgr;
|
||||
|
||||
// Will return a string. For LB Stickiness this will be a json, for autoscale this will be "," separated values
|
||||
@Override
|
||||
|
|
@ -956,7 +959,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
|
|||
// release ip address if ipassoc was perfored
|
||||
if (performedIpAssoc) {
|
||||
ipVO = _ipAddressDao.findById(ipVO.getId());
|
||||
_networkMgr.unassignIPFromVpcNetwork(ipVO.getId(), lb.getNetworkId());
|
||||
_vpcMgr.unassignIPFromVpcNetwork(ipVO.getId(), lb.getNetworkId());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1540,7 +1543,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
|
|||
|
||||
// if the rule is the last one for the ip address assigned to VPC, unassign it from the network
|
||||
IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
|
||||
_networkMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
|
||||
_vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -207,7 +207,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
|
|||
}
|
||||
}
|
||||
|
||||
PublicIp sourceNatIp = _networkMgr.assignSourceNatIpAddressToVpc(owner, vpc);
|
||||
PublicIp sourceNatIp = _vpcMgr.assignSourceNatIpAddressToVpc(owner, vpc);
|
||||
|
||||
DomainRouterVO router = deployVpcRouter(owner, dest, plan, params, false, vpcVrProvider, offeringId,
|
||||
vpc.getId(), sourceNatIp);
|
||||
|
|
@ -1209,7 +1209,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
|
|||
PublicIp publicIp = new PublicIp(ip, _vlanDao.findById(ip.getVlanId()),
|
||||
NetUtils.createSequenceBasedMacAddress(ip.getMacAddress()));
|
||||
if ((ip.getState() == IpAddress.State.Allocated || ip.getState() == IpAddress.State.Allocating)
|
||||
&& _networkMgr.ipUsedInVpc(ip)&& !publicVlans.contains(publicIp.getVlanTag())) {
|
||||
&& _vpcMgr.ipUsedInVpc(ip)&& !publicVlans.contains(publicIp.getVlanTag())) {
|
||||
s_logger.debug("Allocating nic for router in vlan " + publicIp.getVlanTag());
|
||||
NicProfile publicNic = new NicProfile();
|
||||
publicNic.setDefaultNic(false);
|
||||
|
|
@ -1278,7 +1278,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
|
|||
long publicNtwkId = ip.getNetworkId();
|
||||
|
||||
//if ip is not associated to any network, and there are no firewall rules, release it on the backend
|
||||
if (!_networkMgr.ipUsedInVpc(ip)) {
|
||||
if (!_vpcMgr.ipUsedInVpc(ip)) {
|
||||
ip.setState(IpAddress.State.Releasing);
|
||||
}
|
||||
|
||||
|
|
@ -1298,7 +1298,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
|
|||
long publicNtwkId = ip.getNetworkId();
|
||||
|
||||
//if ip is not associated to any network, and there are no firewall rules, release it on the backend
|
||||
if (!_networkMgr.ipUsedInVpc(ip)) {
|
||||
if (!_vpcMgr.ipUsedInVpc(ip)) {
|
||||
ip.setState(IpAddress.State.Releasing);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ import com.cloud.network.dao.IPAddressDao;
|
|||
import com.cloud.network.rules.FirewallRule.FirewallRuleType;
|
||||
import com.cloud.network.rules.FirewallRule.Purpose;
|
||||
import com.cloud.network.rules.dao.PortForwardingRulesDao;
|
||||
import com.cloud.network.vpc.VpcManager;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.projects.Project.ListProjectResourcesCriteria;
|
||||
import com.cloud.server.ResourceTag.TaggedResourceType;
|
||||
|
|
@ -114,6 +115,8 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
|
|||
NicDao _nicDao;
|
||||
@Inject
|
||||
ResourceTagDao _resourceTagDao;
|
||||
@Inject
|
||||
VpcManager _vpcMgr;
|
||||
|
||||
@Override
|
||||
public void checkIpAndUserVm(IpAddress ipAddress, UserVm userVm, Account caller) {
|
||||
|
|
@ -294,7 +297,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
|
|||
if (performedIpAssoc) {
|
||||
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
|
||||
IpAddress ip = _ipAddressDao.findById(ipAddress.getId());
|
||||
_networkMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);
|
||||
_vpcMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -491,7 +494,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
|
|||
if (performedIpAssoc) {
|
||||
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
|
||||
IpAddress ip = _ipAddressDao.findById(ipAddress.getId());
|
||||
_networkMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);
|
||||
_vpcMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1243,7 +1246,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
|
|||
ipAddress.setSystem(false);
|
||||
}
|
||||
_ipAddressDao.update(ipAddress.getId(), ipAddress);
|
||||
_networkMgr.unassignIPFromVpcNetwork(ipAddress.getId(), networkId);
|
||||
_vpcMgr.unassignIPFromVpcNetwork(ipAddress.getId(), networkId);
|
||||
|
||||
if (isIpSystem && releaseIpIfElastic && !_networkMgr.handleSystemIpRelease(ipAddress)) {
|
||||
s_logger.warn("Failed to release system ip address " + ipAddress);
|
||||
|
|
@ -1435,6 +1438,6 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
|
|||
|
||||
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
|
||||
IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
|
||||
_networkMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
|
||||
_vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,9 +14,16 @@ package com.cloud.network.vpc;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import com.cloud.acl.ControlledEntity.ACLType;
|
||||
import com.cloud.exception.ConcurrentOperationException;
|
||||
import com.cloud.exception.InsufficientAddressCapacityException;
|
||||
import com.cloud.exception.InsufficientCapacityException;
|
||||
import com.cloud.exception.ResourceAllocationException;
|
||||
import com.cloud.exception.ResourceUnavailableException;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.network.IpAddress;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.PhysicalNetwork;
|
||||
import com.cloud.network.addr.PublicIp;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.vm.DomainRouterVO;
|
||||
|
||||
|
|
@ -27,7 +34,7 @@ import com.cloud.vm.DomainRouterVO;
|
|||
public interface VpcManager extends VpcService{
|
||||
|
||||
/**
|
||||
* @param guestNtwkOff
|
||||
* @param ntwkOffId
|
||||
* @param cidr
|
||||
* @param networkDomain
|
||||
* @param networkOwner
|
||||
|
|
@ -36,7 +43,7 @@ public interface VpcManager extends VpcService{
|
|||
* @param gateway TODO
|
||||
* @return
|
||||
*/
|
||||
void validateGuestNtkwForVpc(NetworkOffering guestNtwkOff, String cidr, String networkDomain, Account networkOwner,
|
||||
void validateNtkwOffForVpc(long ntwkOffId, String cidr, String networkDomain, Account networkOwner,
|
||||
Vpc vpc, Long networkId, String gateway);
|
||||
|
||||
|
||||
|
|
@ -68,4 +75,55 @@ public interface VpcManager extends VpcService{
|
|||
*/
|
||||
VpcGateway getPrivateGatewayForVpc(long vpcId);
|
||||
|
||||
|
||||
/**
|
||||
* @param ip
|
||||
* @return
|
||||
*/
|
||||
boolean ipUsedInVpc(IpAddress ip);
|
||||
|
||||
|
||||
/**
|
||||
* @param ipId
|
||||
* @param networkId
|
||||
*/
|
||||
void unassignIPFromVpcNetwork(long ipId, long networkId);
|
||||
|
||||
|
||||
/**
|
||||
* @param ntwkOffId
|
||||
* @param name
|
||||
* @param displayText
|
||||
* @param gateway
|
||||
* @param cidr
|
||||
* @param vlanId
|
||||
* @param networkDomain
|
||||
* @param owner
|
||||
* @param domainId
|
||||
* @param pNtwk
|
||||
* @param zoneId
|
||||
* @param aclType
|
||||
* @param subdomainAccess
|
||||
* @param vpcId
|
||||
* @param caller
|
||||
* @return
|
||||
* @throws ConcurrentOperationException
|
||||
* @throws InsufficientCapacityException
|
||||
* @throws ResourceAllocationException
|
||||
*/
|
||||
Network createVpcGuestNetwork(long ntwkOffId, String name, String displayText, String gateway, String cidr,
|
||||
String vlanId, String networkDomain, Account owner, Long domainId, PhysicalNetwork pNtwk, long zoneId,
|
||||
ACLType aclType, Boolean subdomainAccess, long vpcId, Account caller)
|
||||
throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException;
|
||||
|
||||
|
||||
/**
|
||||
* @param owner
|
||||
* @param vpc
|
||||
* @return
|
||||
* @throws InsufficientAddressCapacityException
|
||||
* @throws ConcurrentOperationException
|
||||
*/
|
||||
PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,16 +27,21 @@ import javax.naming.ConfigurationException;
|
|||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.acl.ControlledEntity.ACLType;
|
||||
import com.cloud.api.commands.ListPrivateGatewaysCmd;
|
||||
import com.cloud.api.commands.ListStaticRoutesCmd;
|
||||
import com.cloud.configuration.Config;
|
||||
import com.cloud.configuration.ConfigurationManager;
|
||||
import com.cloud.configuration.dao.ConfigurationDao;
|
||||
import com.cloud.dc.DataCenter;
|
||||
import com.cloud.dc.Vlan.VlanType;
|
||||
import com.cloud.dc.VlanVO;
|
||||
import com.cloud.dc.dao.VlanDao;
|
||||
import com.cloud.deploy.DeployDestination;
|
||||
import com.cloud.event.ActionEvent;
|
||||
import com.cloud.event.EventTypes;
|
||||
import com.cloud.exception.ConcurrentOperationException;
|
||||
import com.cloud.exception.InsufficientAddressCapacityException;
|
||||
import com.cloud.exception.InsufficientCapacityException;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
import com.cloud.exception.NetworkRuleConflictException;
|
||||
|
|
@ -45,6 +50,7 @@ import com.cloud.exception.ResourceAllocationException;
|
|||
import com.cloud.exception.ResourceUnavailableException;
|
||||
import com.cloud.exception.UnsupportedServiceException;
|
||||
import com.cloud.network.IPAddressVO;
|
||||
import com.cloud.network.IpAddress;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.Network.GuestType;
|
||||
import com.cloud.network.Network.Provider;
|
||||
|
|
@ -54,6 +60,7 @@ import com.cloud.network.NetworkVO;
|
|||
import com.cloud.network.Networks.BroadcastDomainType;
|
||||
import com.cloud.network.Networks.TrafficType;
|
||||
import com.cloud.network.PhysicalNetwork;
|
||||
import com.cloud.network.addr.PublicIp;
|
||||
import com.cloud.network.dao.FirewallRulesDao;
|
||||
import com.cloud.network.dao.IPAddressDao;
|
||||
import com.cloud.network.dao.NetworkDao;
|
||||
|
|
@ -147,6 +154,8 @@ public class VpcManagerImpl implements VpcManager, Manager{
|
|||
Site2SiteVpnManager _s2sVpnMgr;
|
||||
@Inject
|
||||
FirewallRulesDao _firewallDao;
|
||||
@Inject
|
||||
VlanDao _vlanDao = null;
|
||||
|
||||
private final ScheduledExecutorService _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("VpcChecker"));
|
||||
private VpcProvider vpcElement = null;
|
||||
|
|
@ -154,6 +163,7 @@ public class VpcManagerImpl implements VpcManager, Manager{
|
|||
String _name;
|
||||
int _cleanupInterval;
|
||||
int _maxNetworks;
|
||||
SearchBuilder<IPAddressVO> IpAddressSearch;
|
||||
|
||||
@Override
|
||||
@DB
|
||||
|
|
@ -194,6 +204,18 @@ public class VpcManagerImpl implements VpcManager, Manager{
|
|||
|
||||
String maxNtwks = configs.get(Config.VpcMaxNetworks.key());
|
||||
_maxNetworks = NumbersUtil.parseInt(maxNtwks, 3); // max=3 is default
|
||||
|
||||
|
||||
IpAddressSearch = _ipAddressDao.createSearchBuilder();
|
||||
IpAddressSearch.and("accountId", IpAddressSearch.entity().getAllocatedToAccountId(), Op.EQ);
|
||||
IpAddressSearch.and("dataCenterId", IpAddressSearch.entity().getDataCenterId(), Op.EQ);
|
||||
IpAddressSearch.and("vpcId", IpAddressSearch.entity().getVpcId(), Op.EQ);
|
||||
IpAddressSearch.and("associatedWithNetworkId", IpAddressSearch.entity().getAssociatedWithNetworkId(), Op.EQ);
|
||||
SearchBuilder<VlanVO> virtualNetworkVlanSB = _vlanDao.createSearchBuilder();
|
||||
virtualNetworkVlanSB.and("vlanType", virtualNetworkVlanSB.entity().getVlanType(), Op.EQ);
|
||||
IpAddressSearch.join("virtualNetworkVlanSB", virtualNetworkVlanSB, IpAddressSearch.entity().getVlanId(), virtualNetworkVlanSB.entity().getId(), JoinBuilder.JoinType.INNER);
|
||||
IpAddressSearch.done();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -902,8 +924,10 @@ public class VpcManagerImpl implements VpcManager, Manager{
|
|||
|
||||
@Override
|
||||
@DB
|
||||
public void validateGuestNtkwForVpc(NetworkOffering guestNtwkOff, String cidr, String networkDomain,
|
||||
public void validateNtkwOffForVpc(long ntwkOffId, String cidr, String networkDomain,
|
||||
Account networkOwner, Vpc vpc, Long networkId, String gateway) {
|
||||
|
||||
NetworkOffering guestNtwkOff = _configMgr.getNetworkOffering(ntwkOffId);
|
||||
|
||||
if (networkId == null) {
|
||||
//1) Validate attributes that has to be passed in when create new guest network
|
||||
|
|
@ -1677,5 +1701,202 @@ public class VpcManagerImpl implements VpcManager, Manager{
|
|||
public VpcGateway getPrivateGatewayForVpc(long vpcId) {
|
||||
return _vpcGatewayDao.getPrivateGatewayForVpc(vpcId);
|
||||
}
|
||||
|
||||
@DB
|
||||
@Override
|
||||
@ActionEvent(eventType = EventTypes.EVENT_NET_IP_ASSIGN, eventDescription = "associating Ip", async = true)
|
||||
public IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationException, ResourceUnavailableException,
|
||||
InsufficientAddressCapacityException, ConcurrentOperationException {
|
||||
Account caller = UserContext.current().getCaller();
|
||||
Account owner = null;
|
||||
|
||||
IpAddress ipToAssoc = _ntwkMgr.getIp(ipId);
|
||||
if (ipToAssoc != null) {
|
||||
_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;
|
||||
}
|
||||
|
||||
Vpc vpc = getVpc(vpcId);
|
||||
if (vpc == null) {
|
||||
throw new InvalidParameterValueException("Invalid VPC id provided", null);
|
||||
}
|
||||
|
||||
// check permissions
|
||||
_accountMgr.checkAccess(caller, null, true, owner, vpc);
|
||||
|
||||
boolean isSourceNat = false;
|
||||
if (getExistingSourceNatInVpc(owner.getId(), vpcId) == null) {
|
||||
isSourceNat = true;
|
||||
}
|
||||
|
||||
s_logger.debug("Associating ip " + ipToAssoc + " to vpc " + vpc);
|
||||
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
IPAddressVO ip = _ipAddressDao.findById(ipId);
|
||||
//update ip address with networkId
|
||||
ip.setVpcId(vpcId);
|
||||
ip.setSourceNat(isSourceNat);
|
||||
_ipAddressDao.update(ipId, ip);
|
||||
|
||||
//mark ip as allocated
|
||||
_ntwkMgr.markPublicIpAsAllocated(ip);
|
||||
txn.commit();
|
||||
|
||||
s_logger.debug("Successfully assigned ip " + ipToAssoc + " to vpc " + vpc);
|
||||
|
||||
return _ipAddressDao.findById(ipId);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void unassignIPFromVpcNetwork(long ipId, long networkId) {
|
||||
IPAddressVO ip = _ipAddressDao.findById(ipId);
|
||||
if (ipUsedInVpc(ip)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ip == null || ip.getVpcId() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
s_logger.debug("Releasing VPC ip address " + ip + " from vpc network id=" + networkId);
|
||||
|
||||
long vpcId = ip.getVpcId();
|
||||
boolean success = false;
|
||||
try {
|
||||
//unassign ip from the VPC router
|
||||
success = _ntwkMgr.applyIpAssociations(_ntwkMgr.getNetwork(networkId), true);
|
||||
} catch (ResourceUnavailableException ex) {
|
||||
throw new CloudRuntimeException("Failed to apply ip associations for network id=" + networkId +
|
||||
" as a part of unassigning ip " + ipId + " from vpc", ex);
|
||||
}
|
||||
|
||||
if (success) {
|
||||
ip.setAssociatedWithNetworkId(null);
|
||||
_ipAddressDao.update(ipId, ip);
|
||||
s_logger.debug("IP address " + ip + " is no longer associated with the network inside vpc id=" + vpcId);
|
||||
} else {
|
||||
throw new CloudRuntimeException("Failed to apply ip associations for network id=" + networkId +
|
||||
" as a part of unassigning ip " + ipId + " from vpc");
|
||||
}
|
||||
s_logger.debug("Successfully released VPC ip address " + ip + " back to VPC pool ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ipUsedInVpc(IpAddress ip) {
|
||||
return (ip != null && ip.getVpcId() != null &&
|
||||
(ip.isOneToOneNat() || !_firewallDao.listByIp(ip.getId()).isEmpty()));
|
||||
}
|
||||
|
||||
@DB
|
||||
@Override
|
||||
public Network createVpcGuestNetwork(long ntwkOffId, String name, String displayText, String gateway,
|
||||
String cidr, String vlanId, String networkDomain, Account owner, Long domainId,
|
||||
PhysicalNetwork pNtwk, long zoneId, ACLType aclType, Boolean subdomainAccess, long vpcId, Account caller)
|
||||
throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException {
|
||||
|
||||
Vpc vpc = getActiveVpc(vpcId);
|
||||
|
||||
if (vpc == null) {
|
||||
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC ", null);
|
||||
ex.addProxyObject("vpc", vpcId, "VPC");
|
||||
throw ex;
|
||||
}
|
||||
_accountMgr.checkAccess(caller, null, false, vpc);
|
||||
|
||||
if (networkDomain == null) {
|
||||
networkDomain = vpc.getNetworkDomain();
|
||||
}
|
||||
|
||||
if (vpc.getZoneId() != zoneId) {
|
||||
throw new InvalidParameterValueException("New network doesn't belong to vpc zone", null);
|
||||
}
|
||||
|
||||
//1) Validate if network can be created for VPC
|
||||
validateNtkwOffForVpc(ntwkOffId, cidr, networkDomain, owner, vpc, null, gateway);
|
||||
|
||||
//2) Create network
|
||||
Network guestNetwork = _ntwkMgr.createGuestNetwork(ntwkOffId, name, displayText, gateway, cidr, vlanId,
|
||||
networkDomain, owner, domainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId);
|
||||
|
||||
return guestNetwork;
|
||||
}
|
||||
|
||||
|
||||
protected IPAddressVO getExistingSourceNatInVpc(long ownerId, long vpcId) {
|
||||
|
||||
List<IPAddressVO> addrs = listPublicIpsAssignedToVpc(ownerId, true, vpcId);
|
||||
|
||||
IPAddressVO sourceNatIp = null;
|
||||
if (addrs.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
// Account already has ip addresses
|
||||
for (IPAddressVO addr : addrs) {
|
||||
if (addr.isSourceNat()) {
|
||||
sourceNatIp = addr;
|
||||
return sourceNatIp;
|
||||
}
|
||||
}
|
||||
|
||||
assert (sourceNatIp != null) : "How do we get a bunch of ip addresses but none of them are source nat? " +
|
||||
"account=" + ownerId + "; vpcId=" + vpcId;
|
||||
}
|
||||
|
||||
return sourceNatIp;
|
||||
}
|
||||
|
||||
protected List<IPAddressVO> listPublicIpsAssignedToVpc(long accountId, Boolean sourceNat, long vpcId) {
|
||||
SearchCriteria<IPAddressVO> sc = IpAddressSearch.create();
|
||||
sc.setParameters("accountId", accountId);
|
||||
sc.setParameters("vpcId", vpcId);
|
||||
|
||||
if (sourceNat != null) {
|
||||
sc.addAnd("sourceNat", SearchCriteria.Op.EQ, sourceNat);
|
||||
}
|
||||
sc.setJoinParameters("virtualNetworkVlanSB", "vlanType", VlanType.VirtualNetwork);
|
||||
|
||||
return _ipAddressDao.search(sc, null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException {
|
||||
long dcId = vpc.getZoneId();
|
||||
|
||||
IPAddressVO sourceNatIp = getExistingSourceNatInVpc(owner.getId(), vpc.getId());
|
||||
|
||||
PublicIp ipToReturn = null;
|
||||
|
||||
if (sourceNatIp != null) {
|
||||
ipToReturn = new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()),
|
||||
NetUtils.createSequenceBasedMacAddress(sourceNatIp.getMacAddress()));
|
||||
} else {
|
||||
ipToReturn = _ntwkMgr.assignDedicateIpAddress(owner, null, vpc.getId(), dcId, true);
|
||||
}
|
||||
|
||||
return ipToReturn;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Network updateVpcGuestNetwork(long networkId, String name, String displayText, Account callerAccount,
|
||||
User callerUser, String domainSuffix, Long ntwkOffId, Boolean changeCidr) {
|
||||
NetworkVO network = _ntwkDao.findById(networkId);
|
||||
if (network == null) {
|
||||
throw new InvalidParameterValueException("Couldn't find network by id", null);
|
||||
}
|
||||
//perform below validation if the network is vpc network
|
||||
if (network.getVpcId() != null && ntwkOffId != null) {
|
||||
Vpc vpc = getVpc(network.getVpcId());
|
||||
validateNtkwOffForVpc(ntwkOffId, null, null, null, vpc, networkId, null);
|
||||
}
|
||||
|
||||
return _ntwkMgr.updateGuestNetwork(networkId, name, displayText, callerAccount, callerUser, domainSuffix,
|
||||
ntwkOffId, changeCidr);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -865,7 +865,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
|||
* @see com.cloud.network.NetworkService#associateIP(long, java.lang.Long, java.lang.Long)
|
||||
*/
|
||||
@Override
|
||||
public IpAddress associateIP(long ipId, Long networkId, Long vpcId) throws InsufficientAddressCapacityException, ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
|
||||
public IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAddressCapacityException, ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue