diff --git a/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java b/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java index 29b0b4af249..183d0007c98 100644 --- a/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java +++ b/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java @@ -105,7 +105,7 @@ public class AssociateIPAddrCmd extends BaseAsyncCreateCmd { assert (networks.size() <= 1) : "Too many virtual networks. This logic should be obsolete"; return networks.get(0).getId(); } else { - Network defaultGuestNetwork = _networkService.getSystemNetworkByZoneAndTrafficType(zone.getId(), TrafficType.Guest); + Network defaultGuestNetwork = _networkService.getExclusiveGuestNetwork(zoneId); if (defaultGuestNetwork == null) { throw new InvalidParameterValueException("Unable to find a default Guest network for account " + getAccountName() + " in domain id=" + getDomainId()); } else { diff --git a/api/src/com/cloud/network/NetworkService.java b/api/src/com/cloud/network/NetworkService.java index c93800a27b3..0cfc45bc5d7 100644 --- a/api/src/com/cloud/network/NetworkService.java +++ b/api/src/com/cloud/network/NetworkService.java @@ -34,7 +34,6 @@ import com.cloud.network.Network.Capability; import com.cloud.network.Network.Provider; import com.cloud.network.Network.Service; import com.cloud.network.Networks.TrafficType; -import com.cloud.offering.NetworkOffering; import com.cloud.user.Account; public interface NetworkService { @@ -127,4 +126,6 @@ public interface NetworkService { List listTrafficTypes(Long physicalNetworkId); PhysicalNetwork getDefaultPhysicalNetworkByZoneAndTrafficType(long zoneId, TrafficType trafficType); + + Network getExclusiveGuestNetwork(long zoneId); } diff --git a/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java b/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java index acfc02bed96..c46dc7bbc27 100755 --- a/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java +++ b/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java @@ -305,7 +305,7 @@ public class BareMetalVmManagerImpl extends UserVmManagerImpl implements BareMet s_logger.warn("Bare Metal only supports basical network mode now, switch to baisc network automatically"); } - Network defaultNetwork = _networkMgr.getSystemNetworkByZoneAndTrafficType(dc.getId(), TrafficType.Guest); + Network defaultNetwork = _networkMgr.getExclusiveGuestNetwork(dc.getId()); if (defaultNetwork == null) { throw new InvalidParameterValueException("Unable to find a default network to start a vm"); } diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index f1eece47450..ac5aa961905 100755 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -1941,7 +1941,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura } else { if (network == null) { if (zone.getNetworkType() == DataCenter.NetworkType.Basic) { - networkId = _networkMgr.getSystemNetworkByZoneAndTrafficType(zoneId, TrafficType.Guest).getId(); + networkId = _networkMgr.getExclusiveGuestNetwork(zoneId).getId(); } else { network = _networkMgr.getNetworkWithSecurityGroupEnabled(zoneId); if (network == null) { diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index c1c47aac795..566ad546e5c 100644 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -546,10 +546,10 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx Account systemAcct = _accountMgr.getSystemAccount(); DataCenterDeployment plan = new DataCenterDeployment(dataCenterId); - List defaultOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork); + NetworkOfferingVO defaultOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork).get(0); if (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()) { - defaultOffering.add(_networkMgr.getExclusiveGuestNetworkOffering()); + defaultOffering = _networkMgr.getExclusiveGuestNetworkOffering(); } List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork, NetworkOfferingVO.SystemManagementNetwork); @@ -557,7 +557,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx NicProfile defaultNic = new NicProfile(); defaultNic.setDefaultNic(true); defaultNic.setDeviceId(2); - networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, defaultOffering.get(0), plan, null, null, false, false).get(0), defaultNic)); + networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, defaultOffering, plan, null, null, false, false).get(0), defaultNic)); for (NetworkOfferingVO offering : offerings) { networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false, false).get(0), null)); } diff --git a/server/src/com/cloud/network/NetworkManager.java b/server/src/com/cloud/network/NetworkManager.java index ebc41011841..f4d8cdfadfe 100644 --- a/server/src/com/cloud/network/NetworkManager.java +++ b/server/src/com/cloud/network/NetworkManager.java @@ -40,7 +40,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.offering.NetworkOffering; import com.cloud.offerings.NetworkOfferingVO; import com.cloud.user.Account; import com.cloud.utils.Pair; @@ -229,7 +228,7 @@ public interface NetworkManager extends NetworkService { boolean isProviderAvailable(long physicalNetowrkId, String providerName); - boolean isServiceEnabled(Long physicalNetworkId, long networkOfferingId, Service service); + boolean isServiceEnabled(long physicalNetworkId, long networkOfferingId, Service service); List getNetworkTags(HypervisorType hType, Network network); diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index f9b283a81a7..e415f6b715d 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -1377,11 +1377,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag // associate a source NAT IP (if one isn't already associated with the network) boolean sharedSourceNat = false; - Map sourceNatCapabilities = getServiceCapabilities(network.getNetworkOfferingId(), Service.SourceNat); - if (sourceNatCapabilities != null) { - String supportedSourceNatTypes = sourceNatCapabilities.get(Capability.SupportedSourceNatTypes).toLowerCase(); - if (supportedSourceNatTypes.contains("zone")) { - sharedSourceNat = true; + if (isServiceSupportedByNetworkOffering(network.getNetworkOfferingId(), Service.SourceNat)) { + Map sourceNatCapabilities = getServiceCapabilities(network.getNetworkOfferingId(), Service.SourceNat); + if (sourceNatCapabilities != null) { + String supportedSourceNatTypes = sourceNatCapabilities.get(Capability.SupportedSourceNatTypes).toLowerCase(); + if (supportedSourceNatTypes.contains("zone")) { + sharedSourceNat = true; + } } } @@ -1491,7 +1493,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag element.prepare(network, profile, vmProfile, dest, context); } - profile.setSecurityGroupEnabled(isServiceEnabled(network.getPhysicalNetworkId(), network.getNetworkOfferingId(), Service.SecurityGroup)); + profile.setSecurityGroupEnabled(isSecurityGroupSupportedInNetwork(network)); guru.updateNicProfile(profile, network); vmProfile.addNic(profile); } @@ -4407,9 +4409,14 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override public boolean isSecurityGroupSupportedInNetwork(Network network) { - boolean supported = isServiceEnabled(network.getPhysicalNetworkId(), network.getNetworkOfferingId(), Service.SecurityGroup); + Long physicalNetworkId = network.getPhysicalNetworkId(); - return supported; + //physical network id can be null in Guest Network in Basic zone, so locate the physical network + if (physicalNetworkId == null) { + physicalNetworkId = findPhysicalNetworkId(network.getDataCenterId(), null); + } + + return isServiceEnabled(physicalNetworkId, network.getNetworkOfferingId(), Service.SecurityGroup); } @Override @@ -4603,7 +4610,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } @Override - public boolean isServiceEnabled(Long physicalNetworkId, long networkOfferingId, Service service) { + public boolean isServiceEnabled(long physicalNetworkId, long networkOfferingId, Service service) { //check if the service is supported by the network offering if (!isServiceSupportedByNetworkOffering(networkOfferingId, service)) { s_logger.debug("Service " + service.getName() + " is not supported by the network offering id=" + networkOfferingId); @@ -4611,13 +4618,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } //get providers for the service and check if all of them are supported - if (physicalNetworkId != null) { - List providers = _ntwkOfferingSrvcDao.getProvidersForService(networkOfferingId, service); - for (String provider : providers) { - if (!isProviderAvailable(physicalNetworkId, provider)) { - s_logger.debug("Provider " + provider + " is not enabled in physical network id=" + physicalNetworkId); - return false; - } + List providers = _ntwkOfferingSrvcDao.getProvidersForService(networkOfferingId, service); + for (String provider : providers) { + if (!isProviderAvailable(physicalNetworkId, provider)) { + s_logger.debug("Provider " + provider + " is not enabled in physical network id=" + physicalNetworkId); + return false; } } @@ -4654,4 +4659,20 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return new ArrayList(); } + + @Override + public NetworkVO getExclusiveGuestNetwork(long zoneId) { + List networks = _networksDao.listBy(Account.ACCOUNT_ID_SYSTEM, zoneId, GuestType.Shared, TrafficType.Guest); + if (networks == null || networks.isEmpty()) { + throw new InvalidParameterValueException("Unable to find network with trafficType " + TrafficType.Guest + " and guestType " + GuestType.Shared + " in zone " + zoneId); + } + + if (networks.size() > 1) { + throw new InvalidParameterValueException("Found more than 1 network with trafficType " + TrafficType.Guest + " and guestType " + GuestType.Shared + " in zone " + zoneId); + + } + + return networks.get(0); + } + } diff --git a/server/src/com/cloud/network/dao/NetworkDao.java b/server/src/com/cloud/network/dao/NetworkDao.java index ab40b96074f..895a941644c 100644 --- a/server/src/com/cloud/network/dao/NetworkDao.java +++ b/server/src/com/cloud/network/dao/NetworkDao.java @@ -75,4 +75,6 @@ public interface NetworkDao extends GenericDao { List listSecurityGroupEnabledNetworks(); List listByPhysicalNetworkTrafficType(long physicalNetworkId, TrafficType trafficType); + + List listBy(long accountId, long dataCenterId, Network.GuestType type, TrafficType trafficType); } diff --git a/server/src/com/cloud/network/dao/NetworkDaoImpl.java b/server/src/com/cloud/network/dao/NetworkDaoImpl.java index 204d664139e..249de89e98c 100644 --- a/server/src/com/cloud/network/dao/NetworkDaoImpl.java +++ b/server/src/com/cloud/network/dao/NetworkDaoImpl.java @@ -370,5 +370,14 @@ public class NetworkDaoImpl extends GenericDaoBase implements N return listBy(sc); } - + @Override + public List listBy(long accountId, long dataCenterId, Network.GuestType type, TrafficType trafficType) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("datacenter", dataCenterId); + sc.setParameters("account", accountId); + sc.setParameters("guestType", type); + sc.setParameters("trafficType", trafficType); + + return listBy(sc, null); + } } diff --git a/server/src/com/cloud/network/guru/DirectNetworkGuru.java b/server/src/com/cloud/network/guru/DirectNetworkGuru.java index 5c8d07d0e38..11eb445383b 100644 --- a/server/src/com/cloud/network/guru/DirectNetworkGuru.java +++ b/server/src/com/cloud/network/guru/DirectNetworkGuru.java @@ -34,6 +34,7 @@ import com.cloud.exception.InsufficientVirtualNetworkCapcityException; import com.cloud.exception.InvalidParameterValueException; import com.cloud.network.IPAddressVO; import com.cloud.network.Network; +import com.cloud.network.Network.GuestType; import com.cloud.network.Network.Service; import com.cloud.network.Network.State; import com.cloud.network.NetworkManager; @@ -99,7 +100,7 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru { } State state = State.Allocated; - if (offering.isSystemOnly()) { + if (dc.getNetworkType() == NetworkType.Basic) { state = State.Setup; } diff --git a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java index 94e8cb0ee44..fa9677b3f53 100644 --- a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java +++ b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java @@ -492,10 +492,10 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V DataCenterDeployment plan = new DataCenterDeployment(dataCenterId); DataCenter dc = _dcDao.findById(plan.getDataCenterId()); - List defaultOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork); + NetworkOfferingVO defaultOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork).get(0); if (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()) { - defaultOffering.add(_networkMgr.getExclusiveGuestNetworkOffering()); + defaultOffering = _networkMgr.getExclusiveGuestNetworkOffering(); } List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork, NetworkOfferingVO.SystemManagementNetwork); @@ -504,7 +504,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V defaultNic.setDefaultNic(true); defaultNic.setDeviceId(2); try { - networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, defaultOffering.get(0), plan, null, null, false, false).get(0), defaultNic)); + networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, defaultOffering, plan, null, null, false, false).get(0), defaultNic)); for (NetworkOfferingVO offering : offerings) { networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false, false).get(0), null)); } diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index f776bed794a..fc4086998ec 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -1996,7 +1996,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager _accountMgr.checkAccess(caller, null, owner); // Get default guest network in Basic zone - Network defaultNetwork = _networkMgr.getSystemNetworkByZoneAndTrafficType(zone.getId(), TrafficType.Guest); + Network defaultNetwork = _networkMgr.getExclusiveGuestNetwork(zone.getId()); if (defaultNetwork == null) { throw new InvalidParameterValueException("Unable to find a default network to start a vm"); @@ -2097,7 +2097,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager throw new InvalidParameterValueException("Unable to find network by id " + networkIdList.get(0).longValue()); } - boolean isSecurityGroupEnabled = _networkMgr.isServiceEnabled(network.getPhysicalNetworkId(), network.getNetworkOfferingId(), Service.SecurityGroup); + boolean isSecurityGroupEnabled = _networkMgr.isSecurityGroupSupportedInNetwork(network); if (isSecurityGroupEnabled && networkIdList.size() > 1) { throw new InvalidParameterValueException("Can't create a vm with multiple networks one of which is Security Group enabled"); } @@ -2421,7 +2421,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager networks.add(new Pair(network, profile)); - if (_networkMgr.isServiceEnabled(network.getPhysicalNetworkId(), network.getNetworkOfferingId(), Service.SecurityGroup)) { + if (_networkMgr.isSecurityGroupSupportedInNetwork(network)) { securityGroupEnabled = true; } }