physicalNetworkId is optional in createNetwork api. Works as follows:

* can be specified for Shared network only
* if not specified for the Shared networks, try to locate it based on the zoneId and tags. If tags is not null, pick up first physicalNetwork from the zone that has matching tags. If tags is null, and there are none/more than 1 physical netwroks in the zone, error out.
This commit is contained in:
alena 2011-11-01 15:49:39 -07:00
parent caefd11d2c
commit 9560e92015
11 changed files with 109 additions and 62 deletions

View File

@ -32,6 +32,8 @@ import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.network.Network;
import com.cloud.network.Network.GuestType;
import com.cloud.offering.NetworkOffering;
import com.cloud.user.UserContext;
@Implementation(description="Creates a network", responseObject=NetworkResponse.class)
@ -89,9 +91,6 @@ public class CreateNetworkCmd extends BaseCmd {
@Parameter(name=ApiConstants.IS_SHARED, type=CommandType.BOOLEAN, description="true is network is shared across accounts in the Zone")
private Boolean isShared;
@Parameter(name=ApiConstants.TAGS, type=CommandType.LIST, collectionType=CommandType.STRING, description="Tag the network")
private List<String> tags;
@Parameter(name=ApiConstants.PHYSICAL_NETWORK_ID, type=CommandType.LONG, description="the Physical Network ID the network belongs to")
private Long physicalNetworkId;
@ -101,10 +100,6 @@ public class CreateNetworkCmd extends BaseCmd {
public Long getNetworkOfferingId() {
return networkOfferingId;
}
public List<String> getTags() {
return tags;
}
public String getGateway() {
return gateway;
@ -158,13 +153,38 @@ public class CreateNetworkCmd extends BaseCmd {
return isShared == null ? false : isShared;
}
public Long getZoneId() {
Long physicalNetworkId = getPhysicalNetworkId();
if (physicalNetworkId == null && zoneId == null) {
throw new InvalidParameterValueException("Zone id is required");
}
return zoneId;
}
public List<String> getTags() {
//FIXME - remove this method
return null;
}
public Long getPhysicalNetworkId() {
if (physicalNetworkId != null) {
return physicalNetworkId;
} else if (zoneId != null) {
return _networkService.translateZoneIdToPhysicalNetworkId(zoneId);
NetworkOffering offering = _configService.getNetworkOffering(networkOfferingId);
if (offering == null) {
throw new InvalidParameterValueException("Unable to find network offering by id " + networkOfferingId);
}
if (offering.getGuestType() == GuestType.Shared) {
if (physicalNetworkId != null) {
return physicalNetworkId;
} else if (zoneId != null) {
return _networkService.findPhysicalNetworkId(zoneId, offering.getTags());
} else {
throw new InvalidParameterValueException("Either zoneId or physicalNetworkId have to be specified for the network of type " + GuestType.Shared);
}
} else if (physicalNetworkId != null) {
throw new InvalidParameterValueException("Physical network id can be specified for networks of guest ip type " + GuestType.Shared + " only.");
} else {
throw new InvalidParameterValueException("Either zoneId or physicalNetworkId have to be specified");
return null;
}
}

View File

@ -18,8 +18,6 @@
package com.cloud.api.commands;
import java.util.List;
import org.apache.log4j.Logger;
import com.cloud.api.ApiConstants;
@ -54,9 +52,6 @@ public class UpdateNetworkCmd extends BaseAsyncCmd {
@Parameter(name=ApiConstants.DISPLAY_TEXT, type=CommandType.STRING, description="the new display text for the network")
private String displayText;
@Parameter(name=ApiConstants.TAGS, type=CommandType.LIST, collectionType=CommandType.STRING, description="tags for the network")
private List<String> tags;
@Parameter(name=ApiConstants.NETWORK_DOMAIN, type=CommandType.STRING, description="network domain")
private String networkDomain;
@ -79,10 +74,6 @@ public class UpdateNetworkCmd extends BaseAsyncCmd {
return displayText;
}
public List<String> getTags() {
return tags;
}
private String getNetworkDomain() {
return networkDomain;
}
@ -112,7 +103,7 @@ public class UpdateNetworkCmd extends BaseAsyncCmd {
@Override
public void execute() throws InsufficientCapacityException, ConcurrentOperationException{
Network result = _networkService.updateNetwork(getId(), getNetworkName(), getDisplayText(), tags, UserContext.current().getCaller(), getNetworkDomain(), getNetworkOfferingId());
Network result = _networkService.updateNetwork(getId(), getNetworkName(), getDisplayText(), UserContext.current().getCaller(), getNetworkDomain(), getNetworkOfferingId());
if (result != null) {
NetworkResponse response = _responseGenerator.createNetworkResponse(result);
response.setResponseName(getCommandName());

View File

@ -276,4 +276,6 @@ public interface Network extends ControlledEntity {
boolean getIsShared();
Long getPhysicalNetworkId();
void setPhysicalNetworkId(Long physicalNetworkId);
}

View File

@ -205,4 +205,9 @@ public class NetworkProfile implements Network {
public Long getPhysicalNetworkId() {
return physicalNetworkId;
}
@Override
public void setPhysicalNetworkId(Long physicalNetworkId) {
this.physicalNetworkId = physicalNetworkId;
}
}

View File

@ -77,7 +77,7 @@ public interface NetworkService {
Long getDedicatedNetworkDomain(long networkId);
Network updateNetwork(long networkId, String name, String displayText, List<String> tags, Account caller, String domainSuffix, Long networkOfferingId);
Network updateNetwork(long networkId, String name, String displayText, Account caller, String domainSuffix, Long networkOfferingId);
Integer getNetworkRate(long networkId, Long vmId);
@ -113,7 +113,7 @@ public interface NetworkService {
PhysicalNetworkServiceProvider getCreatedPhysicalNetworkServiceProvider(Long providerId);
long translateZoneIdToPhysicalNetworkId(long zoneId);
long findPhysicalNetworkId(long zoneId, String tag);
PhysicalNetworkTrafficType addTrafficTypeToPhysicalNetwork(Long physicalNetworkId, String trafficType, String xenLabel, String kvmLabel, String vmwareLabel);

View File

@ -157,7 +157,7 @@ public interface NetworkManager extends NetworkService {
boolean destroyNetwork(long networkId, ReservationContext context);
Network createNetwork(long networkOfferingId, String name, String displayText, Boolean isDefault, String gateway, String cidr, String vlanId, String networkDomain, Account owner, boolean isSecurityGroupEnabled,
Long domainId, List<String> tags, Boolean isShared, PhysicalNetwork physicalNetwork) throws ConcurrentOperationException, InsufficientCapacityException;
Long domainId, List<String> tags, Boolean isShared, PhysicalNetwork physicalNetwork, long zoneId) throws ConcurrentOperationException, InsufficientCapacityException;
/**
* @throws InsufficientCapacityException

View File

@ -1235,6 +1235,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
network.setBroadcastUri(profile.getBroadcastUri());
network.setDns1(profile.getDns1());
network.setDns2(profile.getDns2());
network.setPhysicalNetworkId(profile.getPhysicalNetworkId());
}
protected NicTO toNicTO(NicVO nic, NicProfile profile, NetworkVO config) {
@ -1303,6 +1304,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
network.setBroadcastUri(result.getBroadcastUri());
network.setGateway(result.getGateway());
network.setMode(result.getMode());
network.setPhysicalNetworkId(result.getPhysicalNetworkId());
_networksDao.update(networkId, network);
//implement network elements and re-apply all the network rules
@ -1645,7 +1647,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
List<String> tags = cmd.getTags();
boolean isDomainSpecific = false;
Boolean isShared = cmd.getIsShared();
long physicalNetworkId = cmd.getPhysicalNetworkId();
Long physicalNetworkId = cmd.getPhysicalNetworkId();
Long zoneId = cmd.getZoneId();
if (tags != null && tags.size() > 1) {
throw new InvalidParameterException("Only one tag can be specified for a network at this time");
@ -1698,17 +1701,20 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
}
// Check if physical network exists
PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
if (pNtwk == null) {
throw new InvalidParameterValueException("Unable to find physical network by id " + physicalNetworkId);
PhysicalNetwork pNtwk = null;
if (physicalNetworkId != null) {
pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
if (pNtwk == null) {
throw new InvalidParameterValueException("Unable to find physical network by id " + physicalNetworkId);
}
//check that the physical network is enabled
if (pNtwk.getState() != PhysicalNetwork.State.Enabled) {
throw new InvalidParameterValueException("Physical network id " + physicalNetworkId + " is in incorrect state: " + pNtwk.getState());
}
}
//check that the physical network is enabled
if (pNtwk.getState() != PhysicalNetwork.State.Enabled) {
throw new InvalidParameterValueException("Physical network id " + physicalNetworkId + " is in incorrect state: " + pNtwk.getState());
}
DataCenter zone = _dcDao.findById(pNtwk.getDataCenterId());
DataCenter zone = _dcDao.findById(zoneId);
if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getType())) {
throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zone.getId());
@ -1774,7 +1780,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
domainId = cmd.getDomainId();
}
Network network = createNetwork(networkOfferingId, name, displayText, isDefault, gateway, cidr, vlanId, networkDomain, owner, false, domainId, tags, isShared, pNtwk);
Network network = createNetwork(networkOfferingId, name, displayText, isDefault, gateway, cidr, vlanId, networkDomain, owner, false, domainId, tags, isShared, pNtwk, zoneId);
// Don't pass owner to create vlan when network offering is of type Shared - done to prevent accountVlanMap entry
// creation when vlan is mapped to network
@ -1795,11 +1801,10 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
@Override
@DB
public Network createNetwork(long networkOfferingId, String name, String displayText, Boolean isDefault, String gateway, String cidr, String vlanId, String networkDomain, Account owner,
boolean isSecurityGroupEnabled, Long domainId, List<String> tags, Boolean isShared, PhysicalNetwork physicalNetwork) throws ConcurrentOperationException, InsufficientCapacityException {
boolean isSecurityGroupEnabled, Long domainId, List<String> tags, Boolean isShared, PhysicalNetwork pNtwk, long zoneId) throws ConcurrentOperationException, InsufficientCapacityException {
NetworkOfferingVO networkOffering = _networkOfferingDao.findById(networkOfferingId);
DataCenterVO zone = _dcDao.findById(physicalNetwork.getDataCenterId());
long zoneId = zone.getId();
DataCenterVO zone = _dcDao.findById(zoneId);
// allow isDefault to be set only for Shared network
if (networkOffering.getGuestType() == Network.GuestType.Isolated) {
@ -1893,7 +1898,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
Transaction txn = Transaction.currentTxn();
txn.start();
DataCenterDeployment plan = new DataCenterDeployment(zoneId, null, null, null, null, physicalNetwork.getId());
Long physicalNetworkId = null;
if (pNtwk != null) {
physicalNetworkId = pNtwk.getId();
}
DataCenterDeployment plan = new DataCenterDeployment(zoneId, null, null, null, null, physicalNetworkId);
NetworkVO userNetwork = new NetworkVO();
userNetwork.setNetworkDomain(networkDomain);
@ -2811,7 +2820,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
if (createNetwork) {
List<? extends NetworkOffering> offerings = _configMgr.listNetworkOfferings(TrafficType.Guest, false);
PhysicalNetwork physicalNetwork = translateZoneIdToPhysicalNetwork(zoneId);
network = createNetwork(offerings.get(0).getId(), owner.getAccountName() + "-network", owner.getAccountName() + "-network", null, null, null, null, null, owner, false, null, null, false, physicalNetwork);
network = createNetwork(offerings.get(0).getId(), owner.getAccountName() + "-network", owner.getAccountName() + "-network", null, null, null, null, null, owner, false, null, null, false, physicalNetwork, zoneId);
if (network == null) {
s_logger.warn("Failed to create default Virtual network for the account " + accountId + "in zone " + zoneId);
@ -3143,7 +3152,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
@Override
@ActionEvent(eventType = EventTypes.EVENT_NETWORK_UPDATE, eventDescription = "updating network", async = true)
public Network updateNetwork(long networkId, String name, String displayText, List<String> tags, Account caller, String domainSuffix, Long networkOfferingId) {
public Network updateNetwork(long networkId, String name, String displayText, Account caller, String domainSuffix, Long networkOfferingId) {
boolean restartNetwork = false;
// verify input parameters
@ -3152,9 +3161,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
throw new InvalidParameterValueException("Network id=" + networkId + "doesn't exist in the system");
}
if (tags != null && tags.size() > 1) {
throw new InvalidParameterException("Unable to support more than one tag on network yet");
}
_accountMgr.checkAccess(caller, null, network);
// Don't allow to update system network
@ -3191,10 +3197,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
if (displayText != null) {
network.setDisplayText(displayText);
}
if (tags != null) {
network.setTags(tags);
}
long oldNetworkOfferingId = network.getNetworkOfferingId();
if (networkOfferingId != null) {
@ -3455,6 +3457,12 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
return false;
}
//tags should be the same
if (!oldNetworkOffering.getTags().equalsIgnoreCase(newNetworkOffering.getTags())) {
s_logger.debug("Network offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " have different tags, can't upgrade");
return false;
}
//Traffic types should be the same
if (oldNetworkOffering.getTrafficType() != newNetworkOffering.getTrafficType()) {
s_logger.debug("Network offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " have different traffic types, can't upgrade");
@ -4017,17 +4025,33 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
@Override
public long translateZoneIdToPhysicalNetworkId(long zoneId) {
public long findPhysicalNetworkId(long zoneId, String tag) {
List<PhysicalNetworkVO> pNtwks = _physicalNetworkDao.listByZone(zoneId);
if (pNtwks.isEmpty()) {
throw new InvalidParameterValueException("Unable to find physical network in zone id=" + zoneId);
}
if (pNtwks.size() > 1) {
throw new InvalidParameterValueException("More than one physical networks exist in zone id=" + zoneId);
if (tag == null) {
throw new InvalidParameterValueException("More than one physical networks exist in zone id=" + zoneId + " and no tags are specified in order to make a choice");
}
Long pNtwkId = null;
for (PhysicalNetwork pNtwk : pNtwks) {
if (pNtwk.getTags().contains(tag)) {
s_logger.debug("Found physical network id=" + pNtwk.getId() + " based on requested tags " + tag);
pNtwkId = pNtwk.getId();
break;
}
}
if (pNtwkId == null) {
throw new InvalidParameterValueException("Unable to find physical network which match the tags " + tag);
}
return pNtwkId;
} else {
return pNtwks.get(0).getId();
}
return pNtwks.get(0).getId();
}
@Override

View File

@ -378,7 +378,8 @@ public class NetworkVO implements Network {
return physicalNetworkId;
}
public void setPhysicalNetworkId(long physicalNetworkId) {
@Override
public void setPhysicalNetworkId(Long physicalNetworkId) {
this.physicalNetworkId = physicalNetworkId;
}

View File

@ -139,12 +139,15 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
assert (network.getState() == State.Implementing) : "Why are we implementing " + network;
long dcId = dest.getDataCenter().getId();
//get physical network id
long physicalNetworkId = _networkMgr.findPhysicalNetworkId(dcId, offering.getTags());
NetworkVO implemented = new NetworkVO(network.getTrafficType(), network.getMode(), network.getBroadcastDomainType(), network.getNetworkOfferingId(), State.Allocated,
network.getDataCenterId(), network.getPhysicalNetworkId());
network.getDataCenterId(), physicalNetworkId);
if (network.getBroadcastUri() == null) {
String vnet = _dcDao.allocateVnet(dcId, network.getPhysicalNetworkId(), network.getAccountId(), context.getReservationId());
String vnet = _dcDao.allocateVnet(dcId, physicalNetworkId, network.getAccountId(), context.getReservationId());
if (vnet == null) {
throw new InsufficientVirtualNetworkCapcityException("Unable to allocate vnet as a part of network " + network + " implement ", DataCenter.class, dcId);
}
@ -239,6 +242,7 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
EventUtils.saveEvent(UserContext.current().getCallerUserId(), profile.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_ZONE_VLAN_RELEASE, "Released Zone Vlan: "
+profile.getBroadcastUri().getHost()+" for Network: "+profile.getId(), 0);
profile.setBroadcastUri(null);
profile.setPhysicalNetworkId(null);
}
}

View File

@ -2186,7 +2186,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
if (virtualNetworks.isEmpty()) {
s_logger.debug("Creating default Virtual network for account " + owner + " as a part of deployVM process");
Network newNetwork = _networkMgr.createNetwork(defaultVirtualOffering.get(0).getId(), owner.getAccountName() + "-network", owner.getAccountName() + "-network", null, null,
null, null, null, owner, false, null, null, false, physicalNetwork);
null, null, null, owner, false, null, null, false, physicalNetwork, zone.getId());
defaultNetwork = _networkDao.findById(newNetwork.getId());
} else if (virtualNetworks.size() > 1) {
throw new InvalidParameterValueException("More than 1 default Virtaul networks are found for account " + owner + "; please specify networkIds");
@ -2199,7 +2199,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
if (defaultVirtualOffering.get(0).getAvailability() == Availability.Optional) {
s_logger.debug("Creating default Virtual network for account " + owner + " as a part of deployVM process");
Network newNetwork = _networkMgr.createNetwork(defaultVirtualOffering.get(0).getId(), owner.getAccountName() + "-network", owner.getAccountName() + "-network", null, null,
null, null, null, owner, false, null, null, false, physicalNetwork);
null, null, null, owner, false, null, null, false, physicalNetwork, zone.getId());
defaultNetwork = _networkDao.findById(newNetwork.getId());
} else {
throw new InvalidParameterValueException("Unable to find default networks for account " + owner);
@ -3355,7 +3355,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
List<NetworkVO> virtualNetworks = _networkMgr.listNetworksForAccount(newAccount.getId(), zone.getId(), Network.GuestType.Isolated, true);
if (virtualNetworks.isEmpty()) {
Network newNetwork = _networkMgr.createNetwork(networkOffering, newAccount.getAccountName() + "-network", newAccount.getAccountName() + "-network", null, null,
null, null, null, newAccount, false, null, null, false, physicalNetwork);
null, null, null, newAccount, false, null, null, false, physicalNetwork, zone.getId());
defaultNetwork = _networkDao.findById(newNetwork.getId());
} else if (virtualNetworks.size() > 1) {
throw new InvalidParameterValueException("More than 1 default Virtaul networks are found for account " + newAccount + "; please specify networkIds");

View File

@ -341,7 +341,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
@Override
public Network createNetwork(long networkOfferingId, String name, String displayText, Boolean isDefault, String gateway, String cidr, String vlanId, String networkDomain, Account owner,
boolean isSecurityGroupEnabled, Long domainId, List<String> tags, Boolean isShared, PhysicalNetwork physicalNetwork) throws ConcurrentOperationException, InsufficientCapacityException {
boolean isSecurityGroupEnabled, Long domainId, List<String> tags, Boolean isShared, PhysicalNetwork physicalNetwork, long zoneId) throws ConcurrentOperationException, InsufficientCapacityException {
// TODO Auto-generated method stub
return null;
}
@ -480,7 +480,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
}
@Override
public Network updateNetwork(long networkId, String name, String displayText, List<String> tags, Account caller, String domainSuffix, Long networkOfferingId) {
public Network updateNetwork(long networkId, String name, String displayText, Account caller, String domainSuffix, Long networkOfferingId) {
// TODO Auto-generated method stub
return null;
}