public ArrayList<FloatingIP> listFloatingIp() throws RegionException, NeutronException { Optional<? extends FloatingIPApi> floatingIPApiExtension = neutronApi.getFloatingIPApi(getRegionOne()); if (floatingIPApiExtension.isPresent()) { FloatingIPApi floatingIPApi = floatingIPApiExtension.get(); return Lists.newArrayList(floatingIPApi.list().concat()); } else { throw new NeutronException("FloatingIp is not present"); } }
public FloatingIP createFloatingIP() throws RegionException, NeutronException { Optional<? extends FloatingIPApi> floatingIPApiExtension = neutronApi.getFloatingIPApi(getRegionOne()); if (floatingIPApiExtension.isPresent()) { FloatingIPApi floatingIPApi = floatingIPApiExtension.get(); return floatingIPApi.create( CreateFloatingIP.createBuilder( getNetworkIdByName(OpenstackConfiguration.EXTERNAL_NETWORK)) .build()); } else { throw new NeutronException("FloatingIp is not present"); } }
/** * Get all unassigned Floating IPs. To get all unassigned Floating IPs from a floating network use * {@link #getUnassignedFloatingIPsByNetworkUuid(String)} * * @return list of all the unassigned {@link FloatingIP} from all floating networks allocated to * this tenant */ private ArrayList<FloatingIP> getUnassignedFloatingIPs() { ArrayList<FloatingIP> availableFloatingIPs = Lists.newArrayList( Iterables.filter( floatingIPApi.list().concat().toList(), new Predicate<FloatingIP>() { @Override public boolean apply(FloatingIP arg0) { return arg0.getPortId() == null; } })); return availableFloatingIPs; }
/** * Create a new Floating IP from the given floating network and assign it to the given port * * @param port the port to which a Floating IP to be assigned * @param floatingNetworkUuid the network uuid of the floating network from which the Floating IP * should be created * @return the newly created/assigned {@link FloatingIP} */ private FloatingIP createAndAssignFloatingIP(Port port, String floatingNetworkUuid) { assertNotNull(port, "Cannot create floating IP. Invalid port. Port cannot be null"); assertNotNullAndNotEmpty( floatingNetworkUuid, "Cannot create floating IP. Invalid floating network uuid. " + "Floating network uuid cannot be null"); if (log.isDebugEnabled()) { String msg = String.format( "Trying to create a floating IP from network %s to assign to the port %s", floatingNetworkUuid, port.getId()); log.debug(msg); } FloatingIP.CreateFloatingIP createFip; try { createFip = FloatingIP.createBuilder(floatingNetworkUuid).portId(port.getId()).build(); } catch (Exception e) { String msg = String.format( "Error while getting floating IP builder for the external network %s and port %s", floatingNetworkUuid, port.toString()); log.error(msg, e); throw new CloudControllerException(msg, e); } FloatingIP floatingIP = null; try { floatingIP = floatingIPApi.create(createFip); } catch (Exception e) { String msg = String.format( "Error while creating floating IP for the port %s, from floating network %s", port.toString(), floatingNetworkUuid); log.error(msg, e); throw new CloudControllerException(msg, e); } String msg = String.format("Unable to create a floating IP from network %s", floatingNetworkUuid); assertNotNull(floatingIP, msg); return floatingIP; }
/** * Assign the given Floating IP to the given port. * * @param floatingIP the Floating IP to be assigned * @param portTobeAssigned the port to which the given Floating IP to be assigned * @return the updated {@link FloatingIP} */ private FloatingIP updateFloatingIP(FloatingIP floatingIP, Port portTobeAssigned) { assertNotNull(floatingIP, "Cannot update floating IP. Given floating IP is null"); String portNotNullMsg = String.format( "Cannot update floating IP %s. Given port is null", floatingIP.getFloatingIpAddress()); assertNotNull(portTobeAssigned, portNotNullMsg); FloatingIP updatedFloatingIP = null; if (log.isDebugEnabled()) { String msg = String.format( "Trying to assign existing floating IP %s to the port %s", floatingIP.getFloatingIpAddress(), portTobeAssigned.getId()); log.debug(msg); } try { updatedFloatingIP = floatingIPApi.update( floatingIP.getId(), FloatingIP.UpdateFloatingIP.updateBuilder() .portId(portTobeAssigned.getId()) .fixedIpAddress(portTobeAssigned.getFixedIps().iterator().next().getIpAddress()) .build()); } catch (Exception e) { String msg = String.format( "Error while trying to assign existing floating IP %s to the port %s", floatingIP.toString(), portTobeAssigned.toString()); log.error(msg, e); throw new CloudControllerException(msg, e); } String updatedFloatingIPNullMessage = String.format( "Unable to assign existing floating IP %s " + "to the port %s", floatingIP.toString(), portTobeAssigned.toString()); assertNotNull(updatedFloatingIP, updatedFloatingIPNullMessage); if (log.isDebugEnabled()) { String msg = String.format("Successfully updated the floating IP %s", floatingIP.toString()); log.debug(msg); } return updatedFloatingIP; }
/** * Get all unassigned Floating IPs from the given floating network. To get all unassigned Floating * IPs from all floating networks use {@link #getUnassignedFloatingIPs()} * * @param networkUuid the network uuid of the floating network * @return list of all unassigned {@link FloatingIP} from the given floating network */ private ArrayList<FloatingIP> getUnassignedFloatingIPsByNetworkUuid(final String networkUuid) { if (networkUuid == null || networkUuid.isEmpty()) { return null; } ArrayList<FloatingIP> availableFloatingIPs = Lists.newArrayList( Iterables.filter( floatingIPApi.list().concat().toList(), new Predicate<FloatingIP>() { @Override public boolean apply(FloatingIP arg0) { return arg0.getPortId() == null && arg0.getFloatingNetworkId() != null && arg0.getFloatingNetworkId().equals(networkUuid); } })); return availableFloatingIPs; }
/** * Get the {@link FloatingIP} by its Floating IP Address * * @param floatingIPAddress the Floating IP Address (a.k.a public IP address) * @return the {@link FloatingIP} if found, null otherwise */ private FloatingIP getFloatingIPByIPAddress(final String floatingIPAddress) { if (!isValidIP(floatingIPAddress)) { return null; } Iterable<FloatingIP> floatingIP = Iterables.filter( floatingIPApi.list().concat().toList(), new Predicate<FloatingIP>() { @Override public boolean apply(FloatingIP input) { return input.getFloatingIpAddress() != null && input.getFloatingIpAddress().equals(floatingIPAddress); } }); if (floatingIP.iterator().hasNext()) { return floatingIP.iterator().next(); } return null; }
@Override public void releaseAddress(String ip) { String iPNotValidMsg = String.format("Unable to release the IP. The given IP is not valid", ip); assertValidIP(ip, iPNotValidMsg); if (null == neutronApi || null == portApi || null == floatingIPApi) { buildNeutronApi(); } if (log.isDebugEnabled()) { String msg = String.format("Trying delete the floating IP %s", ip); log.debug(msg); } FloatingIP floatingIP = getFloatingIPByIPAddress(ip); if (null == floatingIP) { if (log.isDebugEnabled()) { String msg = String.format( "Floating IP %s is not found. " + "It might be already deleted, if instance is already terminated", ip); log.debug(msg); } return; } boolean deleted = floatingIPApi.delete(floatingIP.getId()); if (deleted) { if (log.isDebugEnabled()) { String msg = String.format("Successfully deleted the floating IP %s", ip); log.debug(msg); } } else { String msg = String.format("Couldn't release the floating IP %s", ip); log.error(msg); throw new CloudControllerException(msg); } }