use of com.cloud.dc.DataCenterVnetVO in project cosmic by MissionCriticalCloud.
the class DedicateGuestVlanRangesTest method runDedicateGuestVlanRangePostiveTest.
void runDedicateGuestVlanRangePostiveTest() throws Exception {
final TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangePostiveTest");
final Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan");
dedicateVlanField.setAccessible(true);
dedicateVlanField.set(dedicateGuestVlanRangesCmd, "2-5");
final PhysicalNetworkVO physicalNetwork = new PhysicalNetworkVO(1L, 1L, "2-5", "200", 1L, null, "testphysicalnetwork");
physicalNetwork.addIsolationMethod("VLAN");
final AccountGuestVlanMapVO accountGuestVlanMapVO = new AccountGuestVlanMapVO(1L, 1L);
when(networkService._physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetwork);
when(networkService._datacneterVnet.listAllocatedVnetsInRange(anyLong(), anyLong(), anyInt(), anyInt())).thenReturn(null);
when(networkService._accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(anyLong())).thenReturn(null);
when(networkService._accountGuestVlanMapDao.persist(any(AccountGuestVlanMapVO.class))).thenReturn(accountGuestVlanMapVO);
when(networkService._datacneterVnet.update(anyLong(), any(DataCenterVnetVO.class))).thenReturn(true);
final List<DataCenterVnetVO> dataCenterVnetList = new ArrayList<>();
final DataCenterVnetVO dataCenterVnetVO = new DataCenterVnetVO("2-5", 1L, 1L);
dataCenterVnetList.add(dataCenterVnetVO);
when(networkService._datacneterVnet.findVnet(anyLong(), anyString())).thenReturn(dataCenterVnetList);
try {
final GuestVlan result = networkService.dedicateGuestVlanRange(dedicateGuestVlanRangesCmd);
Assert.assertNotNull(result);
} catch (final Exception e) {
s_logger.info("exception in testing runDedicateGuestVlanRangePostiveTest message: " + e.toString());
} finally {
txn.close("runDedicateGuestRangePostiveTest");
}
}
use of com.cloud.dc.DataCenterVnetVO in project cosmic by MissionCriticalCloud.
the class DedicateGuestVlanRangesTest method runDedicateGuestVlanRangeAllocatedVlans.
void runDedicateGuestVlanRangeAllocatedVlans() throws Exception {
final TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangeAllocatedVlans");
final Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan");
dedicateVlanField.setAccessible(true);
dedicateVlanField.set(dedicateGuestVlanRangesCmd, "2-5");
final PhysicalNetworkVO physicalNetwork = new PhysicalNetworkVO(1L, 1L, "2-5", "200", 1L, null, "testphysicalnetwork");
physicalNetwork.addIsolationMethod("VLAN");
when(networkService._physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetwork);
final List<DataCenterVnetVO> dataCenterList = new ArrayList<>();
final DataCenterVnetVO dataCenter = new DataCenterVnetVO("2-5", 1L, 1L);
dataCenter.setAccountId(1L);
dataCenterList.add(dataCenter);
when(networkService._datacneterVnet.listAllocatedVnetsInRange(anyLong(), anyLong(), anyInt(), anyInt())).thenReturn(dataCenterList);
try {
networkService.dedicateGuestVlanRange(dedicateGuestVlanRangesCmd);
} catch (final Exception e) {
Assert.assertTrue(e.getMessage().contains("is allocated to a different account"));
} finally {
txn.close("runDedicateGuestVlanRangeAllocatedVlans");
}
}
use of com.cloud.dc.DataCenterVnetVO in project cloudstack by apache.
the class NetworkServiceImpl method validateVlanRange.
private List<Pair<Integer, Integer>> validateVlanRange(PhysicalNetworkVO network, String[] listOfRanges) {
Integer StartVnet;
Integer EndVnet;
List<Pair<Integer, Integer>> vlanTokens = new ArrayList<Pair<Integer, Integer>>();
for (String vlanRange : listOfRanges) {
String[] VnetRange = vlanRange.split("-");
// Init with [min,max] of VLAN. Actually 0x000 and 0xFFF are reserved by IEEE, shoudn't be used.
long minVnet = MIN_VLAN_ID;
long maxVnet = MAX_VLAN_ID;
// for GRE phynets allow up to 32bits
// TODO: Not happy about this test.
// What about guru-like objects for physical networs?
s_logger.debug("ISOLATION METHODS:" + network.getIsolationMethods());
// Java does not have unsigned types...
if (network.getIsolationMethods().contains("GRE")) {
minVnet = MIN_GRE_KEY;
maxVnet = MAX_GRE_KEY;
} else if (network.getIsolationMethods().contains("VXLAN")) {
minVnet = MIN_VXLAN_VNI;
maxVnet = MAX_VXLAN_VNI;
// since adding a range adds each VNI to the database, need only check min/max
for (String vnet : VnetRange) {
s_logger.debug("Looking to see if VNI " + vnet + " already exists on another network in zone " + network.getDataCenterId());
List<DataCenterVnetVO> vnis = _datacneterVnet.findVnet(network.getDataCenterId(), vnet);
if (vnis != null && !vnis.isEmpty()) {
for (DataCenterVnetVO vni : vnis) {
if (vni.getPhysicalNetworkId() != network.getId()) {
s_logger.debug("VNI " + vnet + " already exists on another network in zone, please specify a unique range");
throw new InvalidParameterValueException("VNI " + vnet + " already exists on another network in zone, please specify a unique range");
}
}
}
}
}
String rangeMessage = " between " + minVnet + " and " + maxVnet;
if (VnetRange.length == 1 && VnetRange[0].equals("")) {
return vlanTokens;
}
if (VnetRange.length < 2) {
throw new InvalidParameterValueException("Please provide valid vnet range. vnet range should be a coma seperated list of vlan ranges. example 500-500,600-601" + rangeMessage);
}
if (VnetRange[0] == null || VnetRange[1] == null) {
throw new InvalidParameterValueException("Please provide valid vnet range" + rangeMessage);
}
try {
StartVnet = Integer.parseInt(VnetRange[0]);
EndVnet = Integer.parseInt(VnetRange[1]);
} catch (NumberFormatException e) {
s_logger.warn("Unable to parse vnet range:", e);
throw new InvalidParameterValueException("Please provide valid vnet range. The vnet range should be a coma seperated list example 2001-2012,3000-3005." + rangeMessage);
}
if (StartVnet < minVnet || EndVnet > maxVnet) {
throw new InvalidParameterValueException("Vnet range has to be" + rangeMessage);
}
if (StartVnet > EndVnet) {
throw new InvalidParameterValueException("Vnet range has to be" + rangeMessage + " and start range should be lesser than or equal to stop range");
}
vlanTokens.add(new Pair<Integer, Integer>(StartVnet, EndVnet));
}
return vlanTokens;
}
use of com.cloud.dc.DataCenterVnetVO in project cloudstack by apache.
the class NetworkServiceImpl method dedicateGuestVlanRange.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_GUEST_VLAN_RANGE_DEDICATE, eventDescription = "dedicating guest vlan range", async = false)
public GuestVlan dedicateGuestVlanRange(DedicateGuestVlanRangeCmd cmd) {
String vlan = cmd.getVlan();
String accountName = cmd.getAccountName();
Long domainId = cmd.getDomainId();
Long physicalNetworkId = cmd.getPhysicalNetworkId();
Long projectId = cmd.getProjectId();
int startVlan, endVlan;
String updatedVlanRange = null;
long guestVlanMapId = 0;
long guestVlanMapAccountId = 0;
long vlanOwnerId = 0;
// Verify account is valid
Account vlanOwner = null;
if (projectId != null) {
if (accountName != null) {
throw new InvalidParameterValueException("accountName and projectId are mutually exclusive");
}
Project project = _projectMgr.getProject(projectId);
if (project == null) {
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
}
vlanOwner = _accountMgr.getAccount(project.getProjectAccountId());
}
if ((accountName != null) && (domainId != null)) {
vlanOwner = _accountDao.findActiveAccount(accountName, domainId);
}
if (vlanOwner == null) {
throw new InvalidParameterValueException("Unable to find account by name " + accountName);
}
vlanOwnerId = vlanOwner.getAccountId();
// Verify physical network isolation type is VLAN
PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
if (physicalNetwork == null) {
throw new InvalidParameterValueException("Unable to find physical network by id " + physicalNetworkId);
} else if (!physicalNetwork.getIsolationMethods().isEmpty() && !physicalNetwork.getIsolationMethods().contains("VLAN")) {
throw new InvalidParameterValueException("Cannot dedicate guest vlan range. " + "Physical isolation type of network " + physicalNetworkId + " is not VLAN");
}
// Get the start and end vlan
String[] vlanRange = vlan.split("-");
if (vlanRange.length != 2) {
throw new InvalidParameterValueException("Invalid format for parameter value vlan " + vlan + " .Vlan should be specified as 'startvlan-endvlan'");
}
try {
startVlan = Integer.parseInt(vlanRange[0]);
endVlan = Integer.parseInt(vlanRange[1]);
} catch (NumberFormatException e) {
s_logger.warn("Unable to parse guest vlan range:", e);
throw new InvalidParameterValueException("Please provide valid guest vlan range");
}
// Verify guest vlan range exists in the system
List<Pair<Integer, Integer>> existingRanges = physicalNetwork.getVnet();
Boolean exists = false;
if (!existingRanges.isEmpty()) {
for (int i = 0; i < existingRanges.size(); i++) {
int existingStartVlan = existingRanges.get(i).first();
int existingEndVlan = existingRanges.get(i).second();
if (startVlan <= endVlan && startVlan >= existingStartVlan && endVlan <= existingEndVlan) {
exists = true;
break;
}
}
if (!exists) {
throw new InvalidParameterValueException("Unable to find guest vlan by range " + vlan);
}
}
// Verify guest vlans in the range don't belong to a network of a different account
for (int i = startVlan; i <= endVlan; i++) {
List<DataCenterVnetVO> allocatedVlans = _datacneterVnet.listAllocatedVnetsInRange(physicalNetwork.getDataCenterId(), physicalNetwork.getId(), startVlan, endVlan);
if (allocatedVlans != null && !allocatedVlans.isEmpty()) {
for (DataCenterVnetVO allocatedVlan : allocatedVlans) {
if (allocatedVlan.getAccountId() != vlanOwner.getAccountId()) {
throw new InvalidParameterValueException("Guest vlan from this range " + allocatedVlan.getVnet() + " is allocated to a different account." + " Can only dedicate a range which has no allocated vlans or has vlans allocated to the same account ");
}
}
}
}
List<AccountGuestVlanMapVO> guestVlanMaps = _accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(physicalNetworkId);
// Verify if vlan range is already dedicated
for (AccountGuestVlanMapVO guestVlanMap : guestVlanMaps) {
List<Integer> vlanTokens = getVlanFromRange(guestVlanMap.getGuestVlanRange());
int dedicatedStartVlan = vlanTokens.get(0).intValue();
int dedicatedEndVlan = vlanTokens.get(1).intValue();
if ((startVlan < dedicatedStartVlan & endVlan >= dedicatedStartVlan) || (startVlan >= dedicatedStartVlan & startVlan <= dedicatedEndVlan)) {
throw new InvalidParameterValueException("Vlan range is already dedicated. Cannot" + " dedicate guest vlan range " + vlan);
}
}
// Sort the existing dedicated vlan ranges
Collections.sort(guestVlanMaps, new Comparator<AccountGuestVlanMapVO>() {
@Override
public int compare(AccountGuestVlanMapVO obj1, AccountGuestVlanMapVO obj2) {
List<Integer> vlanTokens1 = getVlanFromRange(obj1.getGuestVlanRange());
List<Integer> vlanTokens2 = getVlanFromRange(obj2.getGuestVlanRange());
return vlanTokens1.get(0).compareTo(vlanTokens2.get(0));
}
});
// Verify if vlan range extends an already dedicated range
for (int i = 0; i < guestVlanMaps.size(); i++) {
guestVlanMapId = guestVlanMaps.get(i).getId();
guestVlanMapAccountId = guestVlanMaps.get(i).getAccountId();
List<Integer> vlanTokens1 = getVlanFromRange(guestVlanMaps.get(i).getGuestVlanRange());
// Range extends a dedicated vlan range to the left
if (endVlan == (vlanTokens1.get(0).intValue() - 1)) {
if (guestVlanMapAccountId == vlanOwnerId) {
updatedVlanRange = startVlan + "-" + vlanTokens1.get(1).intValue();
}
break;
}
// Range extends a dedicated vlan range to the right
if (startVlan == (vlanTokens1.get(1).intValue() + 1) & guestVlanMapAccountId == vlanOwnerId) {
if (i != (guestVlanMaps.size() - 1)) {
List<Integer> vlanTokens2 = getVlanFromRange(guestVlanMaps.get(i + 1).getGuestVlanRange());
// Range extends 2 vlan ranges, both to the right and left
if (endVlan == (vlanTokens2.get(0).intValue() - 1) && guestVlanMaps.get(i + 1).getAccountId() == vlanOwnerId) {
_datacneterVnet.releaseDedicatedGuestVlans(guestVlanMaps.get(i + 1).getId());
_accountGuestVlanMapDao.remove(guestVlanMaps.get(i + 1).getId());
updatedVlanRange = vlanTokens1.get(0).intValue() + "-" + vlanTokens2.get(1).intValue();
break;
}
}
updatedVlanRange = vlanTokens1.get(0).intValue() + "-" + endVlan;
break;
}
}
// Dedicate vlan range
AccountGuestVlanMapVO accountGuestVlanMapVO;
if (updatedVlanRange != null) {
accountGuestVlanMapVO = _accountGuestVlanMapDao.findById(guestVlanMapId);
accountGuestVlanMapVO.setGuestVlanRange(updatedVlanRange);
_accountGuestVlanMapDao.update(guestVlanMapId, accountGuestVlanMapVO);
} else {
accountGuestVlanMapVO = new AccountGuestVlanMapVO(vlanOwner.getAccountId(), physicalNetworkId);
accountGuestVlanMapVO.setGuestVlanRange(startVlan + "-" + endVlan);
_accountGuestVlanMapDao.persist(accountGuestVlanMapVO);
}
// For every guest vlan set the corresponding account guest vlan map id
List<Integer> finaVlanTokens = getVlanFromRange(accountGuestVlanMapVO.getGuestVlanRange());
for (int i = finaVlanTokens.get(0).intValue(); i <= finaVlanTokens.get(1).intValue(); i++) {
List<DataCenterVnetVO> dataCenterVnet = _datacneterVnet.findVnet(physicalNetwork.getDataCenterId(), physicalNetworkId, Integer.toString(i));
dataCenterVnet.get(0).setAccountGuestVlanMapId(accountGuestVlanMapVO.getId());
_datacneterVnet.update(dataCenterVnet.get(0).getId(), dataCenterVnet.get(0));
}
return accountGuestVlanMapVO;
}
use of com.cloud.dc.DataCenterVnetVO in project cloudstack by apache.
the class DataCenterDaoImpl method allocateVnet.
@Override
public String allocateVnet(long dataCenterId, long physicalNetworkId, long accountId, String reservationId, boolean canUseSystemGuestVlans) {
ArrayList<Long> dedicatedVlanDbIds = new ArrayList<Long>();
boolean useDedicatedGuestVlans = false;
List<AccountGuestVlanMapVO> maps = _accountGuestVlanMapDao.listAccountGuestVlanMapsByAccount(accountId);
for (AccountGuestVlanMapVO map : maps) {
dedicatedVlanDbIds.add(map.getId());
}
if (dedicatedVlanDbIds != null && !dedicatedVlanDbIds.isEmpty()) {
useDedicatedGuestVlans = true;
DataCenterVnetVO vo = _vnetAllocDao.take(physicalNetworkId, accountId, reservationId, dedicatedVlanDbIds);
if (vo != null)
return vo.getVnet();
}
if (!useDedicatedGuestVlans || (useDedicatedGuestVlans && canUseSystemGuestVlans)) {
DataCenterVnetVO vo = _vnetAllocDao.take(physicalNetworkId, accountId, reservationId, null);
if (vo != null) {
return vo.getVnet();
}
}
return null;
}
Aggregations