use of com.cloud.network.PublicIpAddress in project cloudstack by apache.
the class NicPlugInOutRules method getNicsToChangeOnRouter.
private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNicsToChangeOnRouter(final NetworkTopologyVisitor visitor) {
// 1) check which nics need to be plugged/unplugged and plug/unplug them
final Map<String, PublicIpAddress> nicsToPlug = new HashMap<String, PublicIpAddress>();
final Map<String, PublicIpAddress> nicsToUnplug = new HashMap<String, PublicIpAddress>();
VpcManager vpcMgr = visitor.getVirtualNetworkApplianceFactory().getVpcMgr();
NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
// find out nics to unplug
for (PublicIpAddress ip : _ipAddresses) {
long publicNtwkId = ip.getNetworkId();
// rules, release it on the backend
if (!vpcMgr.isIpAllocatedToVpc(ip)) {
ip.setState(IpAddress.State.Releasing);
}
if (ip.getState() == IpAddress.State.Releasing) {
Nic nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), ip.getAddress().addr());
if (nic != null) {
nicsToUnplug.put(ip.getVlanTag(), ip);
s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
}
}
}
// find out nics to plug
for (PublicIpAddress ip : _ipAddresses) {
URI broadcastUri = BroadcastDomainType.Vlan.toUri(ip.getVlanTag());
long publicNtwkId = ip.getNetworkId();
// rules, release it on the backend
if (!vpcMgr.isIpAllocatedToVpc(ip)) {
ip.setState(IpAddress.State.Releasing);
}
if (ip.getState() == IpAddress.State.Allocated || ip.getState() == IpAddress.State.Allocating) {
// nic has to be plugged only when there are no nics for this
// vlan tag exist on VR
Nic nic = nicDao.findByNetworkIdInstanceIdAndBroadcastUri(publicNtwkId, _router.getId(), broadcastUri.toString());
if (nic == null && nicsToPlug.get(ip.getVlanTag()) == null) {
nicsToPlug.put(ip.getVlanTag(), ip);
s_logger.debug("Need to plug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
} else {
final PublicIpAddress nicToUnplug = nicsToUnplug.get(ip.getVlanTag());
if (nicToUnplug != null) {
NicVO nicVO = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), nicToUnplug.getAddress().addr());
nicVO.setIPv4Address(ip.getAddress().addr());
nicDao.update(nicVO.getId(), nicVO);
s_logger.debug("Updated the nic " + nicVO + " with the new ip address " + ip.getAddress().addr());
nicsToUnplug.remove(ip.getVlanTag());
}
}
}
}
Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> nicsToChange = new Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>>(nicsToPlug, nicsToUnplug);
return nicsToChange;
}
use of com.cloud.network.PublicIpAddress in project cloudstack by apache.
the class VpcVirtualNetworkApplianceManagerImpl method getNicsToChangeOnRouter.
protected Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNicsToChangeOnRouter(final List<? extends PublicIpAddress> publicIps, final VirtualRouter router) {
// 1) check which nics need to be plugged/unplugged and plug/unplug them
final Map<String, PublicIpAddress> nicsToPlug = new HashMap<String, PublicIpAddress>();
final Map<String, PublicIpAddress> nicsToUnplug = new HashMap<String, PublicIpAddress>();
// find out nics to unplug
for (final PublicIpAddress ip : publicIps) {
final long publicNtwkId = ip.getNetworkId();
// rules, release it on the backend
if (!_vpcMgr.isIpAllocatedToVpc(ip)) {
ip.setState(IpAddress.State.Releasing);
}
if (ip.getState() == IpAddress.State.Releasing) {
final Nic nic = _nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, router.getId(), ip.getAddress().addr());
if (nic != null) {
nicsToUnplug.put(ip.getVlanTag(), ip);
s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
}
}
}
// find out nics to plug
for (final PublicIpAddress ip : publicIps) {
final URI broadcastUri = BroadcastDomainType.Vlan.toUri(ip.getVlanTag());
final long publicNtwkId = ip.getNetworkId();
// rules, release it on the backend
if (!_vpcMgr.isIpAllocatedToVpc(ip)) {
ip.setState(IpAddress.State.Releasing);
}
if (ip.getState() == IpAddress.State.Allocated || ip.getState() == IpAddress.State.Allocating) {
// nic has to be plugged only when there are no nics for this
// vlan tag exist on VR
final Nic nic = _nicDao.findByNetworkIdInstanceIdAndBroadcastUri(publicNtwkId, router.getId(), broadcastUri.toString());
if (nic == null && nicsToPlug.get(ip.getVlanTag()) == null) {
nicsToPlug.put(ip.getVlanTag(), ip);
s_logger.debug("Need to plug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
} else {
final PublicIpAddress nicToUnplug = nicsToUnplug.get(ip.getVlanTag());
if (nicToUnplug != null) {
final NicVO nicVO = _nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, router.getId(), nicToUnplug.getAddress().addr());
nicVO.setIPv4Address(ip.getAddress().addr());
_nicDao.update(nicVO.getId(), nicVO);
s_logger.debug("Updated the nic " + nicVO + " with the new ip address " + ip.getAddress().addr());
nicsToUnplug.remove(ip.getVlanTag());
}
}
}
}
final Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> nicsToChange = new Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>>(nicsToPlug, nicsToUnplug);
return nicsToChange;
}
use of com.cloud.network.PublicIpAddress in project cloudstack by apache.
the class NiciraNvpElementTest method applyIpTest.
@Test
public void applyIpTest() throws ResourceUnavailableException {
final Network network = mock(Network.class);
when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Lswitch);
when(network.getId()).thenReturn(NETWORK_ID);
when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
final NetworkOffering offering = mock(NetworkOffering.class);
when(offering.getId()).thenReturn(NETWORK_ID);
when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
when(offering.getGuestType()).thenReturn(GuestType.Isolated);
final List<PublicIpAddress> ipAddresses = new ArrayList<PublicIpAddress>();
final PublicIpAddress pipReleased = mock(PublicIpAddress.class);
final PublicIpAddress pipAllocated = mock(PublicIpAddress.class);
final Ip ipReleased = new Ip("42.10.10.10");
final Ip ipAllocated = new Ip("10.10.10.10");
when(pipAllocated.getState()).thenReturn(IpAddress.State.Allocated);
when(pipAllocated.getAddress()).thenReturn(ipAllocated);
when(pipAllocated.getNetmask()).thenReturn("255.255.255.0");
when(pipReleased.getState()).thenReturn(IpAddress.State.Releasing);
when(pipReleased.getAddress()).thenReturn(ipReleased);
when(pipReleased.getNetmask()).thenReturn("255.255.255.0");
ipAddresses.add(pipAllocated);
ipAddresses.add(pipReleased);
final Set<Service> services = new HashSet<Service>();
services.add(Service.SourceNat);
services.add(Service.StaticNat);
services.add(Service.PortForwarding);
final List<NiciraNvpDeviceVO> deviceList = new ArrayList<NiciraNvpDeviceVO>();
final NiciraNvpDeviceVO nndVO = mock(NiciraNvpDeviceVO.class);
final NiciraNvpRouterMappingVO nnrmVO = mock(NiciraNvpRouterMappingVO.class);
when(niciraNvpRouterMappingDao.findByNetworkId(NETWORK_ID)).thenReturn(nnrmVO);
when(nnrmVO.getLogicalRouterUuid()).thenReturn("abcde");
when(nndVO.getHostId()).thenReturn(NETWORK_ID);
final HostVO hvo = mock(HostVO.class);
when(hvo.getId()).thenReturn(NETWORK_ID);
when(hvo.getDetail("l3gatewayserviceuuid")).thenReturn("abcde");
when(hostDao.findById(NETWORK_ID)).thenReturn(hvo);
deviceList.add(nndVO);
when(niciraNvpDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(deviceList);
final ConfigurePublicIpsOnLogicalRouterAnswer answer = mock(ConfigurePublicIpsOnLogicalRouterAnswer.class);
when(answer.getResult()).thenReturn(true);
when(agentManager.easySend(eq(NETWORK_ID), any(ConfigurePublicIpsOnLogicalRouterCommand.class))).thenReturn(answer);
assertTrue(element.applyIps(network, ipAddresses, services));
verify(agentManager, atLeast(1)).easySend(eq(NETWORK_ID), argThat(new ArgumentMatcher<ConfigurePublicIpsOnLogicalRouterCommand>() {
@Override
public boolean matches(final Object argument) {
final ConfigurePublicIpsOnLogicalRouterCommand command = (ConfigurePublicIpsOnLogicalRouterCommand) argument;
if (command.getPublicCidrs().size() == 1)
return true;
return false;
}
}));
}
use of com.cloud.network.PublicIpAddress in project cloudstack by apache.
the class MidoNetElement method associatePublicIP.
public boolean associatePublicIP(Network network, final List<? extends PublicIpAddress> ipAddress) throws ResourceUnavailableException {
s_logger.debug("associatePublicIP called with network: " + network.toString());
/*
* Get Mido Router for this network and set source rules
* These should only be allocated inside the for loop, because
* this function could be called as a part of network cleanup. In
* that case, we do not want to recreate the guest network or
* any ports.
*/
boolean resources = false;
Router tenantRouter = null;
Router providerRouter = null;
RouterPort[] ports = null;
RouterPort tenantUplink = null;
RouterPort providerDownlink = null;
RuleChain preNat = null;
RuleChain post = null;
String accountIdStr = null;
String routerName = null;
// Set Source NAT rules on router
for (PublicIpAddress ip : ipAddress) {
// ip is the external one we sourcenat to
if (ip.isSourceNat()) {
if (resources == false) {
tenantRouter = getOrCreateGuestNetworkRouter(network);
providerRouter = api.getRouter(_providerRouterId);
ports = getOrCreateProviderRouterPorts(tenantRouter, providerRouter);
tenantUplink = ports[0];
providerDownlink = ports[1];
accountIdStr = getAccountUuid(network);
boolean isVpc = getIsVpc(network);
long id = getRouterId(network, isVpc);
routerName = getRouterName(isVpc, id);
preNat = getChain(accountIdStr, routerName, RuleChainCode.TR_PRENAT);
post = api.getChain(tenantRouter.getOutboundFilterId());
resources = true;
}
applySourceNat(// Routers
tenantRouter, // Routers
providerRouter, // Ports
tenantUplink, // Ports
providerDownlink, // Chains
preNat, // Chains
post, // The IP
ip);
}
}
return true;
}
Aggregations