@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<>(); final PublicIpAddress pipReleased = mock(PublicIpAddress.class); final PublicIpAddress pipAllocated = mock(PublicIpAddress.class); final Ip ipReleased = new Ip(NetUtils.ip2Long("42.10.10.10")); final Ip ipAllocated = new Ip(NetUtils.ip2Long("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<>(); services.add(Service.SourceNat); services.add(Service.StaticNat); services.add(Service.PortForwarding); final List<NiciraNvpDeviceVO> deviceList = new ArrayList<>(); 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; } })); }
@Override public boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Network.Service> services) throws ResourceUnavailableException { if (services.contains(Network.Service.SourceNat)) { final List<NiciraNvpDeviceVO> devices = this.niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); if (devices.isEmpty()) { s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); return false; } final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); final HostVO niciraNvpHost = this.hostDao.findById(niciraNvpDevice.getHostId()); this.hostDao.loadDetails(niciraNvpHost); final NiciraNvpRouterMappingVO routermapping = this.niciraNvpRouterMappingDao.findByNetworkId(network.getId()); if (routermapping == null) { s_logger.error("No logical router uuid found for network " + network.getDisplayText()); return false; } final List<String> cidrs = new ArrayList<>(); for (final PublicIpAddress ip : ipAddress) { if (ip.getState() == IpAddress.State.Releasing) { continue; } cidrs.add(ip.getAddress().addr() + "/" + NetUtils.getCidrSize(ip.getNetmask())); } final ConfigurePublicIpsOnLogicalRouterCommand cmd = new ConfigurePublicIpsOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), niciraNvpHost.getDetail("l3gatewayserviceuuid"), cidrs); final ConfigurePublicIpsOnLogicalRouterAnswer answer = (ConfigurePublicIpsOnLogicalRouterAnswer) this.agentMgr.easySend(niciraNvpHost.getId(), cmd); return answer.getResult(); } else { s_logger.debug("No need to provision ip addresses as we are not providing L3 services."); } return true; }
NiciraNvpElement extends AdapterBase implements ConnectivityProvider, SourceNatServiceProvider, PortForwardingServiceProvider, StaticNatServiceProvider,
        NiciraNvpElementService, ResourceStateAdapter, IpDeployer { @Override public boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Network.Service> services) throws ResourceUnavailableException { if (services.contains(Network.Service.SourceNat)) { final List<NiciraNvpDeviceVO> devices = this.niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); if (devices.isEmpty()) { s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); return false; } final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); final HostVO niciraNvpHost = this.hostDao.findById(niciraNvpDevice.getHostId()); this.hostDao.loadDetails(niciraNvpHost); final NiciraNvpRouterMappingVO routermapping = this.niciraNvpRouterMappingDao.findByNetworkId(network.getId()); if (routermapping == null) { s_logger.error("No logical router uuid found for network " + network.getDisplayText()); return false; } final List<String> cidrs = new ArrayList<>(); for (final PublicIpAddress ip : ipAddress) { if (ip.getState() == IpAddress.State.Releasing) { continue; } cidrs.add(ip.getAddress().addr() + "/" + NetUtils.getCidrSize(ip.getNetmask())); } final ConfigurePublicIpsOnLogicalRouterCommand cmd = new ConfigurePublicIpsOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), niciraNvpHost.getDetail("l3gatewayserviceuuid"), cidrs); final ConfigurePublicIpsOnLogicalRouterAnswer answer = (ConfigurePublicIpsOnLogicalRouterAnswer) this.agentMgr.easySend(niciraNvpHost.getId(), cmd); return answer.getResult(); } else { s_logger.debug("No need to provision ip addresses as we are not providing L3 services."); } return true; } }
NiciraNvpElement extends AdapterBase implements ConnectivityProvider, SourceNatServiceProvider, PortForwardingServiceProvider, StaticNatServiceProvider,
        NiciraNvpElementService, ResourceStateAdapter, IpDeployer { @Override public boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Network.Service> services) throws ResourceUnavailableException { if (services.contains(Network.Service.SourceNat)) { final List<NiciraNvpDeviceVO> devices = this.niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); if (devices.isEmpty()) { s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); return false; } final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); final HostVO niciraNvpHost = this.hostDao.findById(niciraNvpDevice.getHostId()); this.hostDao.loadDetails(niciraNvpHost); final NiciraNvpRouterMappingVO routermapping = this.niciraNvpRouterMappingDao.findByNetworkId(network.getId()); if (routermapping == null) { s_logger.error("No logical router uuid found for network " + network.getDisplayText()); return false; } final List<String> cidrs = new ArrayList<>(); for (final PublicIpAddress ip : ipAddress) { if (ip.getState() == IpAddress.State.Releasing) { continue; } cidrs.add(ip.getAddress().addr() + "/" + NetUtils.getCidrSize(ip.getNetmask())); } final ConfigurePublicIpsOnLogicalRouterCommand cmd = new ConfigurePublicIpsOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), niciraNvpHost.getDetail("l3gatewayserviceuuid"), cidrs); final ConfigurePublicIpsOnLogicalRouterAnswer answer = (ConfigurePublicIpsOnLogicalRouterAnswer) this.agentMgr.easySend(niciraNvpHost.getId(), cmd); return answer.getResult(); } else { s_logger.debug("No need to provision ip addresses as we are not providing L3 services."); } return true; }  }
NiciraNvpElement extends AdapterBase implements ConnectivityProvider, SourceNatServiceProvider, PortForwardingServiceProvider, StaticNatServiceProvider,
        NiciraNvpElementService, ResourceStateAdapter, IpDeployer { @Override public boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Network.Service> services) throws ResourceUnavailableException { if (services.contains(Network.Service.SourceNat)) { final List<NiciraNvpDeviceVO> devices = this.niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); if (devices.isEmpty()) { s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); return false; } final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); final HostVO niciraNvpHost = this.hostDao.findById(niciraNvpDevice.getHostId()); this.hostDao.loadDetails(niciraNvpHost); final NiciraNvpRouterMappingVO routermapping = this.niciraNvpRouterMappingDao.findByNetworkId(network.getId()); if (routermapping == null) { s_logger.error("No logical router uuid found for network " + network.getDisplayText()); return false; } final List<String> cidrs = new ArrayList<>(); for (final PublicIpAddress ip : ipAddress) { if (ip.getState() == IpAddress.State.Releasing) { continue; } cidrs.add(ip.getAddress().addr() + "/" + NetUtils.getCidrSize(ip.getNetmask())); } final ConfigurePublicIpsOnLogicalRouterCommand cmd = new ConfigurePublicIpsOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), niciraNvpHost.getDetail("l3gatewayserviceuuid"), cidrs); final ConfigurePublicIpsOnLogicalRouterAnswer answer = (ConfigurePublicIpsOnLogicalRouterAnswer) this.agentMgr.easySend(niciraNvpHost.getId(), cmd); return answer.getResult(); } else { s_logger.debug("No need to provision ip addresses as we are not providing L3 services."); } return true; }  @Override Map<Network.Service, Map<Network.Capability, String>> getCapabilities(); @Override Network.Provider getProvider(); @Override boolean implement(final Network network, final NetworkOffering offering, final DeployDestination dest, final ReservationContext context); @Override boolean prepare(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final DeployDestination dest, final ReservationContext context); @Override boolean release(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final ReservationContext context); @Override boolean shutdown(final Network network, final ReservationContext context, final boolean cleanup); @Override boolean destroy(final Network network, final ReservationContext context); @Override boolean isReady(final PhysicalNetworkServiceProvider provider); @Override boolean shutdownProviderInstances(final PhysicalNetworkServiceProvider provider, final ReservationContext context); @Override boolean canEnableIndividualServices(); @Override boolean verifyServicesCombination(final Set<Network.Service> services); @Override boolean configure(final String name, final Map<String, Object> params); @Override List<Class<?>> getCommands(); @Override @DB NiciraNvpDeviceVO addNiciraNvpDevice(final AddNiciraNvpDeviceCmd cmd); @Override NiciraNvpDeviceResponse createNiciraNvpDeviceResponse(final NiciraNvpDeviceVO niciraNvpDeviceVO); @Override boolean deleteNiciraNvpDevice(final DeleteNiciraNvpDeviceCmd cmd); @Override List<? extends Network> listNiciraNvpDeviceNetworks(final ListNiciraNvpDeviceNetworksCmd cmd); @Override List<NiciraNvpDeviceVO> listNiciraNvpDevices(final ListNiciraNvpDevicesCmd cmd); @Override HostVO createHostVOForConnectedAgent(final HostVO host, final StartupCommand[] cmd); @Override HostVO createHostVOForDirectConnectAgent(final HostVO host, final StartupCommand[] startup, final ServerResource resource, final Map<String, String> details, final
    List<String> hostTags); @Override DeleteHostAnswer deleteHost(final HostVO host, final boolean isForced, final boolean isForceDeleteStorage); @Override IpDeployer getIpDeployer(final Network network); @Override boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Network.Service> services); @Override boolean applyStaticNats(final Network network, final List<? extends StaticNat> rules); @Override boolean applyPFRules(final Network network, final List<PortForwardingRule> rules); }
NiciraNvpElement extends AdapterBase implements ConnectivityProvider, SourceNatServiceProvider, PortForwardingServiceProvider, StaticNatServiceProvider,
        NiciraNvpElementService, ResourceStateAdapter, IpDeployer { @Override public boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Network.Service> services) throws ResourceUnavailableException { if (services.contains(Network.Service.SourceNat)) { final List<NiciraNvpDeviceVO> devices = this.niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); if (devices.isEmpty()) { s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); return false; } final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); final HostVO niciraNvpHost = this.hostDao.findById(niciraNvpDevice.getHostId()); this.hostDao.loadDetails(niciraNvpHost); final NiciraNvpRouterMappingVO routermapping = this.niciraNvpRouterMappingDao.findByNetworkId(network.getId()); if (routermapping == null) { s_logger.error("No logical router uuid found for network " + network.getDisplayText()); return false; } final List<String> cidrs = new ArrayList<>(); for (final PublicIpAddress ip : ipAddress) { if (ip.getState() == IpAddress.State.Releasing) { continue; } cidrs.add(ip.getAddress().addr() + "/" + NetUtils.getCidrSize(ip.getNetmask())); } final ConfigurePublicIpsOnLogicalRouterCommand cmd = new ConfigurePublicIpsOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), niciraNvpHost.getDetail("l3gatewayserviceuuid"), cidrs); final ConfigurePublicIpsOnLogicalRouterAnswer answer = (ConfigurePublicIpsOnLogicalRouterAnswer) this.agentMgr.easySend(niciraNvpHost.getId(), cmd); return answer.getResult(); } else { s_logger.debug("No need to provision ip addresses as we are not providing L3 services."); } return true; }  @Override Map<Network.Service, Map<Network.Capability, String>> getCapabilities(); @Override Network.Provider getProvider(); @Override boolean implement(final Network network, final NetworkOffering offering, final DeployDestination dest, final ReservationContext context); @Override boolean prepare(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final DeployDestination dest, final ReservationContext context); @Override boolean release(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final ReservationContext context); @Override boolean shutdown(final Network network, final ReservationContext context, final boolean cleanup); @Override boolean destroy(final Network network, final ReservationContext context); @Override boolean isReady(final PhysicalNetworkServiceProvider provider); @Override boolean shutdownProviderInstances(final PhysicalNetworkServiceProvider provider, final ReservationContext context); @Override boolean canEnableIndividualServices(); @Override boolean verifyServicesCombination(final Set<Network.Service> services); @Override boolean configure(final String name, final Map<String, Object> params); @Override List<Class<?>> getCommands(); @Override @DB NiciraNvpDeviceVO addNiciraNvpDevice(final AddNiciraNvpDeviceCmd cmd); @Override NiciraNvpDeviceResponse createNiciraNvpDeviceResponse(final NiciraNvpDeviceVO niciraNvpDeviceVO); @Override boolean deleteNiciraNvpDevice(final DeleteNiciraNvpDeviceCmd cmd); @Override List<? extends Network> listNiciraNvpDeviceNetworks(final ListNiciraNvpDeviceNetworksCmd cmd); @Override List<NiciraNvpDeviceVO> listNiciraNvpDevices(final ListNiciraNvpDevicesCmd cmd); @Override HostVO createHostVOForConnectedAgent(final HostVO host, final StartupCommand[] cmd); @Override HostVO createHostVOForDirectConnectAgent(final HostVO host, final StartupCommand[] startup, final ServerResource resource, final Map<String, String> details, final
    List<String> hostTags); @Override DeleteHostAnswer deleteHost(final HostVO host, final boolean isForced, final boolean isForceDeleteStorage); @Override IpDeployer getIpDeployer(final Network network); @Override boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Network.Service> services); @Override boolean applyStaticNats(final Network network, final List<? extends StaticNat> rules); @Override boolean applyPFRules(final Network network, final List<PortForwardingRule> rules);  }