use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class VlanDaoImpl method listVlansForPodByType.
@Override
public List<VlanVO> listVlansForPodByType(long podId, VlanType vlanType) {
// FIXME: use a join statement to improve the performance (should be minor since we expect only one or two)
List<PodVlanMapVO> vlanMaps = _podVlanMapDao.listPodVlanMapsByPod(podId);
List<VlanVO> result = new ArrayList<VlanVO>();
for (PodVlanMapVO pvmvo : vlanMaps) {
VlanVO vlan = findById(pvmvo.getVlanDbId());
if (vlan.getVlanType() == vlanType) {
result.add(vlan);
}
}
return result;
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class CiscoVnmcElementTest method applyStaticNatsTest.
@Test
public void applyStaticNatsTest() throws ResourceUnavailableException {
URI uri = URI.create("vlan://123");
Network network = mock(Network.class);
when(network.getId()).thenReturn(1L);
when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vlan);
when(network.getDataCenterId()).thenReturn(1L);
when(network.getBroadcastUri()).thenReturn(uri);
when(network.getCidr()).thenReturn("1.1.1.0/24");
when(network.getState()).thenReturn(Network.State.Implemented);
Ip ip = mock(Ip.class);
when(ip.addr()).thenReturn("1.2.3.4");
IpAddress ipAddress = mock(IpAddress.class);
when(ipAddress.getAddress()).thenReturn(ip);
when(ipAddress.getVlanId()).thenReturn(1L);
when(_networkModel.getIp(anyLong())).thenReturn(ipAddress);
when(_networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.StaticNat, Provider.CiscoVnmc)).thenReturn(true);
List<CiscoVnmcControllerVO> devices = new ArrayList<CiscoVnmcControllerVO>();
devices.add(mock(CiscoVnmcControllerVO.class));
when(_ciscoVnmcDao.listByPhysicalNetwork(network.getPhysicalNetworkId())).thenReturn(devices);
when(_networkAsa1000vMapDao.findByNetworkId(network.getId())).thenReturn(mock(NetworkAsa1000vMapVO.class));
HostVO hostVO = mock(HostVO.class);
when(hostVO.getId()).thenReturn(1L);
when(_hostDao.findById(anyLong())).thenReturn(hostVO);
VlanVO vlanVO = mock(VlanVO.class);
when(vlanVO.getVlanTag()).thenReturn(null);
when(_vlanDao.findById(anyLong())).thenReturn(vlanVO);
StaticNat rule = mock(StaticNat.class);
when(rule.getSourceIpAddressId()).thenReturn(1L);
when(rule.getDestIpAddress()).thenReturn("1.2.3.4");
when(rule.isForRevoke()).thenReturn(false);
List<StaticNat> rules = new ArrayList<StaticNat>();
rules.add(rule);
Answer answer = mock(Answer.class);
when(answer.getResult()).thenReturn(true);
when(_agentMgr.easySend(anyLong(), any(SetStaticNatRulesCommand.class))).thenReturn(answer);
assertTrue(_element.applyStaticNats(network, rules));
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class ResourceLimitManagerImpl method calculatePublicIpForAccount.
private long calculatePublicIpForAccount(long accountId) {
Long dedicatedCount = 0L;
Long allocatedCount = 0L;
List<VlanVO> dedicatedVlans = _vlanDao.listDedicatedVlans(accountId);
for (VlanVO dedicatedVlan : dedicatedVlans) {
List<IPAddressVO> ips = _ipAddressDao.listByVlanId(dedicatedVlan.getId());
dedicatedCount += new Long(ips.size());
}
allocatedCount = _ipAddressDao.countAllocatedIPsForAccount(accountId);
if (dedicatedCount > allocatedCount) {
return dedicatedCount;
} else {
return allocatedCount;
}
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class NiciraNvpElement method sharedNetworkSupportNumericalVlanId.
private boolean sharedNetworkSupportNumericalVlanId(Network network, String lSwitchUuid, String ownerName, HostVO niciraNvpHost) {
List<VlanVO> networkVlans = vlanDao.listVlansByNetworkId(network.getId());
if (networkVlans.size() == 1) {
for (VlanVO vlanVO : networkVlans) {
long vlanId = Long.parseLong(vlanVO.getVlanTag());
String l2GatewayServiceUuid = niciraNvpHost.getDetail("l2gatewayserviceuuid");
if (l2GatewayServiceUuid == null) {
throw new CloudRuntimeException("No L2 Gateway Service Uuid found on " + niciraNvpHost.getName());
}
ConfigureSharedNetworkVlanIdCommand cmd = new ConfigureSharedNetworkVlanIdCommand(lSwitchUuid, l2GatewayServiceUuid, vlanId, ownerName, network.getId());
ConfigureSharedNetworkVlanIdAnswer answer = (ConfigureSharedNetworkVlanIdAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);
if (answer.getResult() == false) {
s_logger.error("Failed to configure Shared network " + network.getDisplayText());
return false;
}
}
}
return true;
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class ExternalFirewallDeviceManagerImpl method manageGuestNetworkWithExternalFirewall.
@Override
public boolean manageGuestNetworkWithExternalFirewall(boolean add, Network network) throws ResourceUnavailableException, InsufficientCapacityException {
if (network.getTrafficType() != TrafficType.Guest) {
s_logger.trace("External firewall can only be used for add/remove guest networks.");
return false;
}
long zoneId = network.getDataCenterId();
DataCenterVO zone = _dcDao.findById(zoneId);
HostVO externalFirewall = null;
if (add) {
GlobalLock deviceMapLock = GlobalLock.getInternLock("NetworkFirewallDeviceMap");
try {
if (deviceMapLock.lock(120)) {
try {
ExternalFirewallDeviceVO device = findSuitableFirewallForNetwork(network);
long externalFirewallId = device.getId();
NetworkExternalFirewallVO networkFW = new NetworkExternalFirewallVO(network.getId(), externalFirewallId);
_networkExternalFirewallDao.persist(networkFW);
externalFirewall = _hostDao.findById(device.getHostId());
} finally {
deviceMapLock.unlock();
}
}
} finally {
deviceMapLock.releaseRef();
}
} else {
ExternalFirewallDeviceVO fwDeviceVO = getExternalFirewallForNetwork(network);
if (fwDeviceVO == null) {
s_logger.warn("Network shutdown requested on external firewall element, which did not implement the network." + " Either network implement failed half way through or already network shutdown is completed.");
return true;
}
externalFirewall = _hostDao.findById(fwDeviceVO.getHostId());
}
Account account = _accountDao.findByIdIncludingRemoved(network.getAccountId());
NetworkOffering offering = _networkOfferingDao.findById(network.getNetworkOfferingId());
boolean sharedSourceNat = offering.isSharedSourceNat();
IPAddressVO sourceNatIp = null;
if (!sharedSourceNat) {
// Get the source NAT IP address for this network
List<? extends IpAddress> sourceNatIps = _networkModel.listPublicIpsAssignedToAccount(network.getAccountId(), zoneId, true);
for (IpAddress ipAddress : sourceNatIps) {
if (ipAddress.getAssociatedWithNetworkId().longValue() == network.getId()) {
sourceNatIp = _ipAddressDao.findById(ipAddress.getId());
break;
}
}
if (sourceNatIp == null) {
String errorMsg = "External firewall was unable to find the source NAT IP address for network " + network.getName();
s_logger.error(errorMsg);
return true;
}
}
// Send a command to the external firewall to implement or shutdown the guest network
long guestVlanTag = Long.parseLong(BroadcastDomainType.getValue(network.getBroadcastUri()));
String guestVlanGateway = network.getGateway();
String guestVlanCidr = network.getCidr();
String sourceNatIpAddress = null;
String publicVlanTag = null;
if (sourceNatIp != null) {
sourceNatIpAddress = sourceNatIp.getAddress().addr();
VlanVO publicVlan = _vlanDao.findById(sourceNatIp.getVlanId());
publicVlanTag = publicVlan.getVlanTag();
}
// Get network rate
Integer networkRate = _networkModel.getNetworkRate(network.getId(), null);
IpAddressTO ip = new IpAddressTO(account.getAccountId(), sourceNatIpAddress, add, false, !sharedSourceNat, publicVlanTag, null, null, null, networkRate, false);
IpAddressTO[] ips = new IpAddressTO[1];
ips[0] = ip;
IpAssocCommand cmd = new IpAssocCommand(ips);
cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_GATEWAY, guestVlanGateway);
cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR, guestVlanCidr);
cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG, String.valueOf(guestVlanTag));
Answer answer = _agentMgr.easySend(externalFirewall.getId(), cmd);
List<String> reservedIpAddressesForGuestNetwork = _nicDao.listIpAddressInNetwork(network.getId());
if (answer == null || !answer.getResult()) {
String action = add ? "implement" : "shutdown";
String answerDetails = (answer != null) ? answer.getDetails() : "answer was null";
String msg = "External firewall was unable to " + action + " the guest network on the external firewall in zone " + zone.getName() + " due to " + answerDetails;
s_logger.error(msg);
if (!add && (!reservedIpAddressesForGuestNetwork.contains(network.getGateway()))) {
// If we failed the implementation as well, then just return, no complain
s_logger.error("Skip the shutdown of guest network on SRX because it seems we didn't implement it as well");
return true;
}
throw new ResourceUnavailableException(msg, DataCenter.class, zoneId);
}
if (add && (!reservedIpAddressesForGuestNetwork.contains(network.getGateway()))) {
// Insert a new NIC for this guest network to reserve the gateway address
_networkMgr.savePlaceholderNic(network, network.getGateway(), null, null);
}
// Delete any mappings used for inline external load balancers in this network
List<NicVO> nicsInNetwork = _nicDao.listByNetworkId(network.getId());
for (NicVO nic : nicsInNetwork) {
InlineLoadBalancerNicMapVO mapping = _inlineLoadBalancerNicMapDao.findByNicId(nic.getId());
if (mapping != null) {
_nicDao.expunge(mapping.getNicId());
_inlineLoadBalancerNicMapDao.expunge(mapping.getId());
}
}
// on network shutdown, delete placeHolder nics used for the firewall device
if (!add) {
List<NicVO> nics = _nicDao.listByNetworkId(network.getId());
for (NicVO nic : nics) {
if (nic.getVmType() == null && nic.getReservationStrategy().equals(ReservationStrategy.PlaceHolder) && nic.getIPv4Address().equals(network.getGateway())) {
s_logger.debug("Removing placeholder nic " + nic + " for the network " + network);
_nicDao.remove(nic.getId());
}
}
freeFirewallForNetwork(network);
}
String action = add ? "implemented" : "shut down";
s_logger.debug("External firewall has " + action + " the guest network for account " + account.getAccountName() + "(id = " + account.getAccountId() + ") with VLAN tag " + guestVlanTag);
return true;
}
Aggregations