diff --git a/api/src/com/cloud/api/commands/CreatePrivateNetworkCmd.java b/api/src/com/cloud/api/commands/CreatePrivateNetworkCmd.java index 276c1fb8e24..1a6ce0927e5 100644 --- a/api/src/com/cloud/api/commands/CreatePrivateNetworkCmd.java +++ b/api/src/com/cloud/api/commands/CreatePrivateNetworkCmd.java @@ -139,7 +139,7 @@ public class CreatePrivateNetworkCmd extends BaseAsyncCreateCmd { Network result = null; try { result = _networkService.createPrivateNetwork(getNetworkName(), getDisplayText(), getPhysicalNetworkId(), getVlan(), - getStartIp(), getEndIp(), getGateway(), getNetmask(), getEntityOwnerId()); + getStartIp(), getEndIp(), getGateway(), getNetmask(), getEntityOwnerId(), null); } catch (InsufficientCapacityException ex){ s_logger.info(ex); s_logger.trace(ex); diff --git a/api/src/com/cloud/api/commands/CreateVPCCmd.java b/api/src/com/cloud/api/commands/CreateVPCCmd.java index 8802f55983f..9b5fd4677b3 100644 --- a/api/src/com/cloud/api/commands/CreateVPCCmd.java +++ b/api/src/com/cloud/api/commands/CreateVPCCmd.java @@ -126,7 +126,7 @@ public class CreateVPCCmd extends BaseAsyncCreateCmd{ public void execute() { Vpc vpc = null; try { - if (_vpcService.startVpc(this.getEntityId())) { + if (_vpcService.startVpc(this.getEntityId(), true)) { vpc = _vpcService.getVpc(getEntityId()); } } catch (ResourceUnavailableException ex) { diff --git a/api/src/com/cloud/network/NetworkService.java b/api/src/com/cloud/network/NetworkService.java index d0754f171cb..731cff9f5ab 100755 --- a/api/src/com/cloud/network/NetworkService.java +++ b/api/src/com/cloud/network/NetworkService.java @@ -157,13 +157,14 @@ public interface NetworkService { * @param gateway * @param netmask * @param networkOwnerId + * @param vpcId TODO * @return * @throws InsufficientCapacityException * @throws ConcurrentOperationException * @throws ResourceAllocationException */ Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, String vlan, - String startIp, String endIP, String gateway, String netmask, long networkOwnerId) + String startIp, String endIP, String gateway, String netmask, long networkOwnerId, Long vpcId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException; /** * @param network diff --git a/api/src/com/cloud/network/vpc/VpcService.java b/api/src/com/cloud/network/vpc/VpcService.java index bf35d1fcf69..c55a0290796 100644 --- a/api/src/com/cloud/network/vpc/VpcService.java +++ b/api/src/com/cloud/network/vpc/VpcService.java @@ -118,12 +118,13 @@ public interface VpcService { /** * @param vpcId + * @param destroyOnFailure TODO * @return * @throws InsufficientCapacityException * @throws ResourceUnavailableException * @throws ConcurrentOperationException */ - boolean startVpc(long vpcId) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException; + boolean startVpc(long vpcId, boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException; /** * @param vpcId diff --git a/api/src/com/cloud/vm/NicProfile.java b/api/src/com/cloud/vm/NicProfile.java index a21748f3db2..e5e84939d03 100644 --- a/api/src/com/cloud/vm/NicProfile.java +++ b/api/src/com/cloud/vm/NicProfile.java @@ -294,4 +294,5 @@ public class NicProfile { public String toString() { return new StringBuilder("NicProfile[").append(id).append("-").append(vmId).append("-").append(reservationId).toString(); } + } diff --git a/server/src/com/cloud/network/NetworkManager.java b/server/src/com/cloud/network/NetworkManager.java index 65d35da25ec..78fb983a38a 100755 --- a/server/src/com/cloud/network/NetworkManager.java +++ b/server/src/com/cloud/network/NetworkManager.java @@ -55,6 +55,7 @@ import com.cloud.vm.ReservationContext; import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachineProfile; +import com.cloud.vm.VirtualMachineProfileImpl; /** * NetworkManager manages the network for the different end users. @@ -460,6 +461,22 @@ public interface NetworkManager extends NetworkService { * @return */ boolean isPrivateGateway(Nic guestNic); + + + /** + * @param network + * @param requested + * @param context + * @param vmProfile + * @return + * @throws InsufficientVirtualNetworkCapcityException + * @throws InsufficientAddressCapacityException + * @throws ConcurrentOperationException + * @throws InsufficientCapacityException + * @throws ResourceUnavailableException + */ + NicProfile allocateAndPrepareNic(Network network, NicProfile requested, ReservationContext context, VirtualMachineProfileImpl vmProfile) throws InsufficientVirtualNetworkCapcityException, + InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException; } diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 7b1ab94e1e0..d53a946f8e3 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -95,6 +95,7 @@ import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.exception.UnsupportedServiceException; +import com.cloud.host.Host; import com.cloud.host.HostVO; import com.cloud.host.Status; import com.cloud.host.dao.HostDao; @@ -208,6 +209,7 @@ import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachine.Type; import com.cloud.vm.VirtualMachineProfile; +import com.cloud.vm.VirtualMachineProfileImpl; import com.cloud.vm.dao.DomainRouterDao; import com.cloud.vm.dao.NicDao; import com.cloud.vm.dao.UserVmDao; @@ -7248,7 +7250,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override @DB public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, - String vlan, String startIp, String endIp, String gateway, String netmask, long networkOwnerId) + String vlan, String startIp, String endIp, String gateway, String netmask, long networkOwnerId, Long vpcId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException { Account owner = _accountMgr.getAccount(networkOwnerId); @@ -7313,7 +7315,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Long nextMac = mac + 1; dc.setMacAddress(nextMac); - privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac); + privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId); _privateIpDao.persist(privateIp); _dcDao.update(dc.getId(), dc); @@ -7357,4 +7359,56 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } return true; } + + @Override + public NicProfile allocateAndPrepareNic(Network network, NicProfile requested, ReservationContext context, + VirtualMachineProfileImpl vmProfile) + throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, + ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException { + + VirtualMachine vm = vmProfile.getVirtualMachine(); + NetworkVO networkVO = _networksDao.findById(network.getId()); + DataCenter dc = _configMgr.getZone(network.getDataCenterId()); + Host host = _hostDao.findById(vm.getHostId()); + DeployDestination dest = new DeployDestination(dc, null, null, host); + + NicProfile nic = null; + String broadcastUri = null; + if (requested != null && requested.getBroadCastUri() != null) { + broadcastUri = requested.getBroadCastUri().toString(); + NicVO nicVO = _nicDao.findByInstanceIdNetworkIdAndBroadcastUri(network.getId(), vm.getId(), broadcastUri); + if (nicVO != null) { + nic = getNicProfile(vm, network.getId()); + } + } else { + NicVO nicVO = _nicDao.findByInstanceIdAndNetworkId(network.getId(), vm.getId()); + if (nicVO != null) { + nic = getNicProfile(vm, network.getId()); + } + } + + //1) allocate nic (if needed) + if (nic == null) { + s_logger.debug("Allocating nic for the " + vm + " in network " + network); + int deviceId = _nicDao.countNics(vm.getId()); + + nic = allocateNic(requested, network, false, + deviceId, vmProfile).first(); + + if (nic == null) { + throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network); + } + + s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network); + + + + s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network); + } + + //2) prepare nic + nic = prepareNic(vmProfile, dest, context, nic.getId(), networkVO); + + return nic; + } } diff --git a/server/src/com/cloud/network/guru/PrivateNetworkGuru.java b/server/src/com/cloud/network/guru/PrivateNetworkGuru.java index ab078743498..0546f23e848 100644 --- a/server/src/com/cloud/network/guru/PrivateNetworkGuru.java +++ b/server/src/com/cloud/network/guru/PrivateNetworkGuru.java @@ -101,7 +101,7 @@ public class PrivateNetworkGuru extends AdapterBase implements NetworkGuru { return null; } - NetworkVO network = new NetworkVO(offering.getTrafficType(), Mode.Dhcp, BroadcastDomainType.Vlan, offering.getId(), + NetworkVO network = new NetworkVO(offering.getTrafficType(), Mode.Static, BroadcastDomainType.Vlan, offering.getId(), State.Allocated, plan.getDataCenterId(), plan.getPhysicalNetworkId()); if (userSpecified != null) { if ((userSpecified.getCidr() == null && userSpecified.getGateway() != null) || @@ -175,10 +175,9 @@ public class PrivateNetworkGuru extends AdapterBase implements NetworkGuru { protected void getIp(NicProfile nic, DataCenter dc, Network network) - throws InsufficientVirtualNetworkCapcityException, - InsufficientAddressCapacityException { + throws InsufficientVirtualNetworkCapcityException,InsufficientAddressCapacityException { if (nic.getIp4Address() == null) { - PrivateIpVO ipVO = _privateIpDao.allocateIpAddress(network.getDataCenterId(), network.getId()); + PrivateIpVO ipVO = _privateIpDao.allocateIpAddress(network.getDataCenterId(), network.getId(), null); String vlanTag = network.getBroadcastUri().getHost(); String netmask = NetUtils.getCidrNetmask(network.getCidr()); PrivateIpAddress ip = new PrivateIpAddress(ipVO, vlanTag, network.getGateway(), netmask, ipVO.getMacAddress()); diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index f73f961590f..a9e26ef4e53 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -1257,8 +1257,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian //3) deploy virtual router(s) int count = routerCount - routers.size(); for (int i = 0; i < count; i++) { + List> networks = createRouterNetworks(owner, isRedundant, plan, guestNetwork, + new Pair(publicNetwork, sourceNatIp)); DomainRouterVO router = deployRouter(owner, dest, plan, params, isRedundant, vrProvider, offeringId, - null, publicNetwork, guestNetwork, new Pair(publicNetwork, sourceNatIp)); + null, networks); //add router to router network map if (!_routerDao.isRouterPartOfGuestNetwork(router.getId(), network.getId())) { DomainRouterVO routerVO = _routerDao.findById(router.getId()); @@ -1276,7 +1278,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian protected DomainRouterVO deployRouter(Account owner, DeployDestination dest, DeploymentPlan plan, Map params, boolean isRedundant, VirtualRouterProvider vrProvider, long svcOffId, - Long vpcId, boolean setupPublicNetwork, Network guestNetwork, Pair publicNetwork) throws ConcurrentOperationException, + Long vpcId, List> networks) throws ConcurrentOperationException, InsufficientAddressCapacityException, InsufficientServerCapacityException, InsufficientCapacityException, StorageUnavailableException, ResourceUnavailableException { @@ -1284,15 +1286,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (s_logger.isDebugEnabled()) { s_logger.debug("Creating the router " + id + " in datacenter " + dest.getDataCenter()); } - - //1) Create router networks - List> networks = createRouterNetworks(owner, isRedundant, plan, guestNetwork, - publicNetwork); - ServiceOfferingVO routerOffering = _serviceOfferingDao.findById(svcOffId); - //2) Router is the network element, we don't know the hypervisor type yet. + // Router is the network element, we don't know the hypervisor type yet. //Try to allocate the domR twice using diff hypervisors, and when failed both times, throw the exception up List supportedHypervisors = new ArrayList(); HypervisorType defaults = _resourceMgr.getDefaultHypervisor(dest.getDataCenter().getId()); @@ -1456,7 +1453,6 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian networks.add(new Pair(publicNetworks.get(0), defaultNic)); } - return networks; } diff --git a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java index 8d3472dd97a..3ea96debb35 100644 --- a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java @@ -62,6 +62,8 @@ import com.cloud.network.Network; import com.cloud.network.Network.Provider; import com.cloud.network.Network.Service; import com.cloud.network.NetworkService; +import com.cloud.network.NetworkVO; +import com.cloud.network.Networks.AddressFormat; import com.cloud.network.Networks.BroadcastDomainType; import com.cloud.network.Networks.IsolationType; import com.cloud.network.Networks.TrafficType; @@ -396,10 +398,10 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian InsufficientAddressCapacityException, InsufficientServerCapacityException, InsufficientCapacityException, StorageUnavailableException, ResourceUnavailableException { - + List> networks = createVpcRouterNetworks(owner, isRedundant, plan, new Pair(true, sourceNatIp), + vpcId); DomainRouterVO router = - super.deployRouter(owner, dest, plan, params, isRedundant, vrProvider, svcOffId, vpcId, false, - null, new Pair(true, sourceNatIp)); + super.deployRouter(owner, dest, plan, params, isRedundant, vrProvider, svcOffId, vpcId, networks); return router; } @@ -807,6 +809,10 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian } } + + + + //4) PREPARE PLUG NIC COMMANDS try { //add VPC router to public networks List sourceNat = new ArrayList(1); @@ -867,7 +873,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian return false; } - //3) RE-APPLY ALL STATIC ROUTE RULES + //5) RE-APPLY ALL STATIC ROUTE RULES List routes = _staticRouteDao.listByVpcId(router.getVpcId()); List staticRouteProfiles = new ArrayList(routes.size()); Map gatewayMap = new HashMap(); @@ -886,9 +892,9 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian createStaticRouteCommands(staticRouteProfiles, router, cmds); } - //4) REISSUE VPN CONNECTION + //6) REISSUE VPN CONNECTION - //5) REPROGRAM GUEST NETWORK + //7) REPROGRAM GUEST NETWORK boolean reprogramGuestNtwks = true; if (profile.getParameter(Param.ReProgramGuestNetworks) != null && (Boolean) profile.getParameter(Param.ReProgramGuestNetworks) == false) { @@ -938,7 +944,9 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian boolean result = true; try { Network network = _networkMgr.getNetwork(gateway.getNetworkId()); - NicProfile guestNic = _itMgr.addVmToNetwork(router, network, null); + NicProfile requested = createPrivateNicProfile(gateway); + + NicProfile guestNic = _itMgr.addVmToNetwork(router, network, requested); //setup source nat if (guestNic != null) { @@ -1174,4 +1182,45 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian cmds.addCommand("IPAssocVpcCommand", cmd); } } + + + protected List> createVpcRouterNetworks(Account owner, boolean isRedundant, + DeploymentPlan plan, Pair publicNetwork, long vpcId) throws ConcurrentOperationException, + InsufficientAddressCapacityException { + + List> networks = new ArrayList>(4); + networks = super.createRouterNetworks(owner, isRedundant, plan, null, publicNetwork); + + //allocate nic for private gateway if needed + VpcGateway privateGateway = _vpcMgr.getPrivateGatewayForVpc(vpcId); + if (privateGateway != null) { + NicProfile privateNic = createPrivateNicProfile(privateGateway); + Network privateNetwork = _networkMgr.getNetwork(privateGateway.getNetworkId()); + networks.add(new Pair((NetworkVO) privateNetwork, privateNic)); + } + + return networks; + } + + @DB + protected NicProfile createPrivateNicProfile(VpcGateway privateGateway) { + Network network = _networkMgr.getNetwork(privateGateway.getNetworkId()); + PrivateIpVO ipVO = _privateIpDao.allocateIpAddress(network.getDataCenterId(), network.getId(), privateGateway.getIp4Address()); + + NicProfile privateNic = new NicProfile(); + String vlanTag = network.getBroadcastUri().getHost(); + String netmask = NetUtils.getCidrNetmask(network.getCidr()); + PrivateIpAddress ip = new PrivateIpAddress(ipVO, vlanTag, network.getGateway(), netmask, ipVO.getMacAddress()); + + privateNic.setIp4Address(ip.getIpAddress()); + privateNic.setGateway(ip.getGateway()); + privateNic.setNetmask(ip.getNetmask()); + privateNic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag())); + privateNic.setBroadcastUri(IsolationType.Vlan.toUri(ip.getVlanTag())); + privateNic.setBroadcastType(BroadcastDomainType.Vlan); + privateNic.setFormat(AddressFormat.Ip4); + privateNic.setReservationId(String.valueOf(ip.getVlanTag())); + privateNic.setMacAddress(ip.getMacAddress()); + return privateNic; + } } diff --git a/server/src/com/cloud/network/vpc/Dao/PrivateIpDao.java b/server/src/com/cloud/network/vpc/Dao/PrivateIpDao.java index 9f8d8114d83..339e566b417 100644 --- a/server/src/com/cloud/network/vpc/Dao/PrivateIpDao.java +++ b/server/src/com/cloud/network/vpc/Dao/PrivateIpDao.java @@ -25,9 +25,10 @@ public interface PrivateIpDao extends GenericDao{ /** * @param dcId * @param networkId + * @param requestedIp TODO * @return */ - PrivateIpVO allocateIpAddress(long dcId, long networkId); + PrivateIpVO allocateIpAddress(long dcId, long networkId, String requestedIp); /** * @param ipAddress @@ -61,5 +62,13 @@ public interface PrivateIpDao extends GenericDao{ int countByNetworkId(long ntwkId); + /** + * @param vpcId + * @param ip4Address + * @return + */ + PrivateIpVO findByIpAndVpcId(long vpcId, String ip4Address); + + } diff --git a/server/src/com/cloud/network/vpc/Dao/PrivateIpDaoImpl.java b/server/src/com/cloud/network/vpc/Dao/PrivateIpDaoImpl.java index 7fc626c5b8b..22f3f62a39d 100644 --- a/server/src/com/cloud/network/vpc/Dao/PrivateIpDaoImpl.java +++ b/server/src/com/cloud/network/vpc/Dao/PrivateIpDaoImpl.java @@ -51,6 +51,7 @@ public class PrivateIpDaoImpl extends GenericDaoBase implemen AllFieldsSearch.and("networkId", AllFieldsSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); AllFieldsSearch.and("ipAddress", AllFieldsSearch.entity().getIpAddress(), SearchCriteria.Op.EQ); AllFieldsSearch.and("taken", AllFieldsSearch.entity().getTakenAt(), SearchCriteria.Op.EQ); + AllFieldsSearch.and("vpcId", AllFieldsSearch.entity().getVpcId(), SearchCriteria.Op.EQ); AllFieldsSearch.done(); CountAllocatedByNetworkId = createSearchBuilder(Integer.class); @@ -66,11 +67,15 @@ public class PrivateIpDaoImpl extends GenericDaoBase implemen } @Override - public PrivateIpVO allocateIpAddress(long dcId, long networkId) { + public PrivateIpVO allocateIpAddress(long dcId, long networkId, String requestedIp) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("networkId", networkId); sc.setParameters("taken", (Date)null); + if (requestedIp != null) { + sc.setParameters("ipAddress", requestedIp); + } + Transaction txn = Transaction.currentTxn(); txn.start(); PrivateIpVO vo = lockOneRandomRow(sc, true); @@ -99,9 +104,7 @@ public class PrivateIpDaoImpl extends GenericDaoBase implemen update(vo, sc); } - /* (non-Javadoc) - * @see com.cloud.network.vpc.Dao.PrivateIpDao#findByIpAndSourceNetworkId(long, java.lang.String) - */ + @Override public PrivateIpVO findByIpAndSourceNetworkId(long networkId, String ip4Address) { SearchCriteria sc = AllFieldsSearch.create(); @@ -110,6 +113,14 @@ public class PrivateIpDaoImpl extends GenericDaoBase implemen return findOneBy(sc); } + @Override + public PrivateIpVO findByIpAndVpcId(long vpcId, String ip4Address) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("ip", ip4Address); + sc.setParameters("vpcId", vpcId); + return findOneBy(sc); + } + @Override public List listByNetworkId(long networkId) { SearchCriteria sc = AllFieldsSearch.create(); diff --git a/server/src/com/cloud/network/vpc/Dao/VpcGatewayDao.java b/server/src/com/cloud/network/vpc/Dao/VpcGatewayDao.java index add2f4228ad..0984cb5c9c4 100644 --- a/server/src/com/cloud/network/vpc/Dao/VpcGatewayDao.java +++ b/server/src/com/cloud/network/vpc/Dao/VpcGatewayDao.java @@ -19,5 +19,5 @@ import com.cloud.utils.db.GenericDao; * @author Alena Prokharchyk */ public interface VpcGatewayDao extends GenericDao{ - VpcGatewayVO getPrivateGateway(long vpcId); + VpcGatewayVO getPrivateGatewayForVpc(long vpcId); } diff --git a/server/src/com/cloud/network/vpc/Dao/VpcGatewayDaoImpl.java b/server/src/com/cloud/network/vpc/Dao/VpcGatewayDaoImpl.java index f19ff569f22..550eb8c84e6 100644 --- a/server/src/com/cloud/network/vpc/Dao/VpcGatewayDaoImpl.java +++ b/server/src/com/cloud/network/vpc/Dao/VpcGatewayDaoImpl.java @@ -39,7 +39,7 @@ public class VpcGatewayDaoImpl extends GenericDaoBase implem @Override - public VpcGatewayVO getPrivateGateway(long vpcId) { + public VpcGatewayVO getPrivateGatewayForVpc(long vpcId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("vpcId", vpcId); sc.setParameters("type", VpcGateway.Type.Private); diff --git a/server/src/com/cloud/network/vpc/PrivateIpVO.java b/server/src/com/cloud/network/vpc/PrivateIpVO.java index b9f31429d66..c34c43fa382 100644 --- a/server/src/com/cloud/network/vpc/PrivateIpVO.java +++ b/server/src/com/cloud/network/vpc/PrivateIpVO.java @@ -49,13 +49,17 @@ public class PrivateIpVO{ @Column(name="network_id", updatable=false, nullable=false) private long networkId; + @Column(name="vpc_id") + private Long vpcId; + public PrivateIpVO() { } - public PrivateIpVO(String ipAddress, long networkId, long macAddress) { + public PrivateIpVO(String ipAddress, long networkId, long macAddress, long vpcId) { this.ipAddress = ipAddress; this.networkId = networkId; this.macAddress = macAddress; + this.vpcId = vpcId; } public void setTakenAt(Date takenDate) { @@ -81,4 +85,8 @@ public class PrivateIpVO{ public long getMacAddress() { return macAddress; } + + public Long getVpcId() { + return vpcId; + } } diff --git a/server/src/com/cloud/network/vpc/VpcManager.java b/server/src/com/cloud/network/vpc/VpcManager.java index ebad3b4548b..64c044bc8d3 100644 --- a/server/src/com/cloud/network/vpc/VpcManager.java +++ b/server/src/com/cloud/network/vpc/VpcManager.java @@ -102,4 +102,10 @@ public interface VpcManager extends VpcService{ * @return */ boolean vpcProviderEnabledInZone(long zoneId); + + /** + * @param vpcId + * @return + */ + VpcGateway getPrivateGatewayForVpc(long vpcId); } diff --git a/server/src/com/cloud/network/vpc/VpcManagerImpl.java b/server/src/com/cloud/network/vpc/VpcManagerImpl.java index 0a6a9b318a6..b7453755611 100644 --- a/server/src/com/cloud/network/vpc/VpcManagerImpl.java +++ b/server/src/com/cloud/network/vpc/VpcManagerImpl.java @@ -801,7 +801,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ } @Override - public boolean startVpc(long vpcId) throws ConcurrentOperationException, ResourceUnavailableException, + public boolean startVpc(long vpcId, boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { UserContext ctx = UserContext.current(); Account caller = ctx.getCaller(); @@ -835,7 +835,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ result = false; } finally { //do cleanup - if (!result) { + if (!result && destroyOnFailure) { s_logger.debug("Destroying vpc " + vpc + " that failed to start"); if (destroyVpc(vpc)) { s_logger.warn("Successfully destroyed vpc " + vpc + " that failed to start"); @@ -1032,7 +1032,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ } //3) Delete private gateway - PrivateGateway gateway = getVpcPrivateGateway(vpcId); + VpcGateway gateway = getPrivateGatewayForVpc(vpcId); if (gateway != null) { s_logger.debug("Deleting private gateway " + gateway + " as a part of vpc " + vpcId + " resources cleanup"); if (!deleteVpcPrivateGateway(gateway.getId())) { @@ -1074,7 +1074,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ } s_logger.debug("Starting VPC " + vpc + " as a part of VPC restart process"); - if (!startVpc(vpcId)) { + if (!startVpc(vpcId, false)) { s_logger.warn("Failed to start vpc as a part of VPC " + vpc + " restart process"); restartRequired = true; return false; @@ -1130,7 +1130,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ } //allow only one private gateway per vpc - VpcGatewayVO gatewayVO = _vpcGatewayDao.getPrivateGateway(vpcId); + VpcGatewayVO gatewayVO = _vpcGatewayDao.getPrivateGatewayForVpc(vpcId); if (gatewayVO != null) { throw new InvalidParameterValueException("Private ip address already exists for vpc " + vpc); } @@ -1150,7 +1150,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ //1) create private network String networkName = "vpc-" + vpc.getName() + "-privateNetwork"; Network privateNtwk = _ntwkMgr.createPrivateNetwork(networkName, networkName, physicalNetworkId, - vlan, ipAddress, null, gateway, netmask, gatewayOwnerId); + vlan, ipAddress, null, gateway, netmask, gatewayOwnerId, vpcId); //2) create gateway entry gatewayVO = new VpcGatewayVO(ipAddress, VpcGateway.Type.Private, vpcId, privateNtwk.getDataCenterId(), @@ -1220,7 +1220,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ Transaction txn = Transaction.currentTxn(); txn.start(); - PrivateIpVO ip = _privateIpDao.findByIpAndSourceNetworkId(gateway.getNetworkId(), gateway.getIp4Address()); + PrivateIpVO ip = _privateIpDao.findByIpAndVpcId(gateway.getVpcId(), gateway.getIp4Address()); if (ip != null) { _privateIpDao.remove(ip.getId()); s_logger.debug("Deleted private ip " + ip); @@ -1584,4 +1584,9 @@ public class VpcManagerImpl implements VpcManager, Manager{ } } } + + @Override + public VpcGateway getPrivateGatewayForVpc(long vpcId) { + return _vpcGatewayDao.getPrivateGatewayForVpc(vpcId); + } } diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index a81b5dd9590..ff059bce65b 100755 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -2449,7 +2449,6 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.debug("Adding vm " + vm + " to network " + network); VMInstanceVO vmVO = _vmDao.findById(vm.getId()); - NetworkVO networkVO = _networkDao.findById(network.getId()); ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(User.UID_SYSTEM), _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM)); @@ -2460,40 +2459,8 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene Host host = _hostDao.findById(vm.getHostId()); DeployDestination dest = new DeployDestination(dc, null, null, host); - NicProfile nic = null; - String broadcastUri = null; - if (requested != null && requested.getBroadCastUri() != null) { - broadcastUri = requested.getBroadCastUri().toString(); - NicVO nicVO = _nicsDao.findByInstanceIdNetworkIdAndBroadcastUri(network.getId(), vm.getId(), broadcastUri); - if (nicVO != null) { - nic = _networkMgr.getNicProfile(vm, network.getId()); - } - } else { - NicVO nicVO = _nicsDao.findByInstanceIdAndNetworkId(network.getId(), vm.getId()); - if (nicVO != null) { - nic = _networkMgr.getNicProfile(vm, network.getId()); - } - } - - if (nic == null) { - s_logger.debug("Allocating nic for the " + vm + " in network " + network); - //1) allocate nic and prepare nic if needed - int deviceId = _nicsDao.countNics(vm.getId()); - - nic = _networkMgr.allocateNic(requested, network, false, - deviceId, vmProfile).first(); - - if (nic == null) { - throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network); - } - - s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network); - - nic = _networkMgr.prepareNic(vmProfile, dest, context, nic.getId(), networkVO); - - s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network); - - } + //1) allocate and prepare nic + NicProfile nic = _networkMgr.allocateAndPrepareNic(network, requested, context, vmProfile); //2) Convert vmProfile to vmTO HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); @@ -2515,6 +2482,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } + @Override public NicTO toNicTO(NicProfile nic, HypervisorType hypervisorType) { HypervisorGuru hvGuru = _hvGuruMgr.getGuru(hypervisorType); diff --git a/server/test/com/cloud/network/MockNetworkManagerImpl.java b/server/test/com/cloud/network/MockNetworkManagerImpl.java index 61d4a817c46..7ce527f9f69 100755 --- a/server/test/com/cloud/network/MockNetworkManagerImpl.java +++ b/server/test/com/cloud/network/MockNetworkManagerImpl.java @@ -880,7 +880,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS * @see com.cloud.network.NetworkService#createPrivateNetwork(java.lang.String, java.lang.String, long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, long) */ @Override - public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, String vlan, String startIp, String endIP, String gateway, String netmask, long networkOwnerId) + public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, String vlan, String startIp, String endIP, String gateway, String netmask, long networkOwnerId, Long vpcId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException { // TODO Auto-generated method stub return null; diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index f24715c4a37..d7e5fed1520 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -2285,8 +2285,10 @@ CREATE TABLE `cloud`.`private_ip_address` ( `network_id` bigint unsigned NOT NULL COMMENT 'id of the network ip belongs to', `reservation_id` char(40) COMMENT 'reservation id', `mac_address` varchar(17) COMMENT 'mac address', + `vpc_id` bigint unsigned COMMENT 'vpc this ip belongs to', `taken` datetime COMMENT 'Date taken', PRIMARY KEY (`id`), + CONSTRAINT `fk_private_ip_address__vpc_id` FOREIGN KEY `fk_private_ip_address__vpc_id`(`vpc_id`) REFERENCES `vpc`(`id`), CONSTRAINT `fk_private_ip_address__network_id` FOREIGN KEY (`network_id`) REFERENCES `networks` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;