NiciraNvpElement extends AdapterBase implements ConnectivityProvider, SourceNatServiceProvider, PortForwardingServiceProvider, StaticNatServiceProvider,
NiciraNvpElementService, ResourceStateAdapter, IpDeployer { @Override public boolean applyIps(Network network, List<? extends PublicIpAddress> ipAddress, Set<Service> services) throws ResourceUnavailableException { if (services.contains(Service.SourceNat)) { final List<NiciraNvpDeviceVO> devices = 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 = hostDao.findById(niciraNvpDevice.getHostId()); hostDao.loadDetails(niciraNvpHost); final NiciraNvpRouterMappingVO routermapping = 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<String>(); 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)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<Service, Map<Capability, String>> getCapabilities(); @Override Provider getProvider(); @Override boolean configure(String name, Map<String, Object> params); @Override boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context); @Override boolean prepare(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context); @Override boolean release(Network network, NicProfile nic, VirtualMachineProfile vm, ReservationContext context); @Override boolean shutdown(Network network, ReservationContext context, boolean cleanup); @Override boolean destroy(Network network, ReservationContext context); @Override boolean isReady(PhysicalNetworkServiceProvider provider); @Override boolean shutdownProviderInstances(PhysicalNetworkServiceProvider provider, ReservationContext context); @Override boolean canEnableIndividualServices(); @Override boolean verifyServicesCombination(Set<Service> services); @Override List<Class<?>> getCommands(); @Override @DB NiciraNvpDeviceVO addNiciraNvpDevice(AddNiciraNvpDeviceCmd cmd); @Override NiciraNvpDeviceResponse createNiciraNvpDeviceResponse(NiciraNvpDeviceVO niciraNvpDeviceVO); @Override boolean deleteNiciraNvpDevice(DeleteNiciraNvpDeviceCmd cmd); @Override List<NiciraNvpDeviceVO> listNiciraNvpDevices(ListNiciraNvpDevicesCmd cmd); @Override List<? extends Network> listNiciraNvpDeviceNetworks(ListNiciraNvpDeviceNetworksCmd cmd); @Override HostVO createHostVOForConnectedAgent(HostVO host, StartupCommand[] cmd); @Override HostVO createHostVOForDirectConnectAgent(HostVO host, StartupCommand[] startup, ServerResource resource, Map<String, String> details, List<String> hostTags); @Override DeleteHostAnswer deleteHost(HostVO host, boolean isForced, boolean isForceDeleteStorage); @Override IpDeployer getIpDeployer(Network network); @Override boolean applyIps(Network network, List<? extends PublicIpAddress> ipAddress, Set<Service> services); @Override boolean applyStaticNats(Network network, List<? extends StaticNat> rules); @Override boolean applyPFRules(Network network, List<PortForwardingRule> rules);  }
@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; } })); }