Brooklyn

brooklyn.location.access
[Java] Class PortForwardManager

java.lang.Object
  brooklyn.location.access.PortForwardManager

@Beta
public class PortForwardManager

Records port mappings against public IP addresses with given identifiers

To use, create a new instance shared among all communicating parties. One Location side (e.g. a software process in a VM) can request (acquirePublicPort(String, Location, int)) an unused port on a firewall / public IP address. He may then go on actually to talk to that firewall/IP to provision the forwarding rule.

Subseequently the other side can use this class lookup(Location, int) if it knows the location and private port it wishes to talk to.

This class does not know anything about what the firewall/IP actually is; it just handles a unique identifier for it. It is recommended, however, to recordPublicIpHostname(String, String) an accessible hostname with the identifier (this is required in order to use lookup(Location, int)).

This implementation is not very efficient, and currently has a cap of about 50000 rules. (TODO improve the efficiency and scale)


Field Summary
protected java.util.Map mappings

protected java.util.Map publicIpIdToHostname

 
Method Summary
int acquirePublicPort(java.lang.String publicIpId)

reserves a unique public port on the given publicIpId (often followed by associate(String, int, Location, int) to enable lookup(Location, int))

int acquirePublicPort(java.lang.String publicIpId, Location l, int privatePort)

reserves a unique public port for the purpose of forwarding to the given target, associated with a given location for subsequent lookup purpose; if already allocated, returns the previously allocated

PortMapping acquirePublicPortExplicit(java.lang.String publicIpId, int port)

returns old mapping if it existed, null if it is new

void associate(java.lang.String publicIpId, int publicPort, Location l, int privatePort)

records a location and private port against a publicIp and public port, to support lookup(Location, int) was used; but strongly recommended if acquirePublicPort(String, int) was used e.g. if the location is not known ahead of time)

PortMapping forgetPortMapping(java.lang.String publicIpId, int publicPort)

clears the given port mapping, returning the mapping if there was one

boolean forgetPortMapping(PortMapping m)

boolean forgetPublicIpHostname(java.lang.String publicIpId)

clears a previous call to recordPublicIpHostname(String, String)

java.util.Collection getLocationPublicIpIds(Location l)

returns the subset of port mappings associated with a given location

PortMapping getPortMappingWithPrivateSide(Location l, int privatePort)

java.util.Collection getPortMappingWithPublicIpId(java.lang.String publicIpId)

returns the subset of port mappings associated with a given public IP ID

PortMapping getPortMappingWithPublicSide(java.lang.String publicIpId, int publicPort)

returns the port mapping for a given publicIpId and public port

HostAndPort getPublicHostAndPort(PortMapping m)

returns the public host and port for use accessing the given mapping

java.lang.String getPublicIpHostname(java.lang.String publicIpId)

returns a recorded public hostname or address

HostAndPort lookup(Location l, int privatePort)

returns the public ip hostname and public port for use contacting the given endpoint; null if: no publicPort is associated with this location and private port no publicIpId is associated with this location and private port no publicIpHostname is recorded against the associated publicIpId

protected java.lang.String makeKey(java.lang.String publicIpId, int publicPort)

void recordPublicIpHostname(java.lang.String publicIpId, java.lang.String hostnameOrPublicIpAddress)

records a public hostname or address to be associated with the given publicIpId for lookup purposes

java.lang.String toString()

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Field Detail

mappings

protected final java.util.Map mappings


publicIpIdToHostname

protected final java.util.Map publicIpIdToHostname


 
Method Detail

acquirePublicPort

public int acquirePublicPort(java.lang.String publicIpId)
reserves a unique public port on the given publicIpId (often followed by associate(String, int, Location, int) to enable lookup(Location, int))


acquirePublicPort

public int acquirePublicPort(java.lang.String publicIpId, Location l, int privatePort)
reserves a unique public port for the purpose of forwarding to the given target, associated with a given location for subsequent lookup purpose; if already allocated, returns the previously allocated


acquirePublicPortExplicit

public PortMapping acquirePublicPortExplicit(java.lang.String publicIpId, int port)
returns old mapping if it existed, null if it is new


associate

public void associate(java.lang.String publicIpId, int publicPort, Location l, int privatePort)
records a location and private port against a publicIp and public port, to support lookup(Location, int) was used; but strongly recommended if acquirePublicPort(String, int) was used e.g. if the location is not known ahead of time)


forgetPortMapping

public PortMapping forgetPortMapping(java.lang.String publicIpId, int publicPort)
clears the given port mapping, returning the mapping if there was one


forgetPortMapping

public boolean forgetPortMapping(PortMapping m)


forgetPublicIpHostname

public boolean forgetPublicIpHostname(java.lang.String publicIpId)
clears a previous call to recordPublicIpHostname(String, String)


getLocationPublicIpIds

public java.util.Collection getLocationPublicIpIds(Location l)
returns the subset of port mappings associated with a given location


getPortMappingWithPrivateSide

public PortMapping getPortMappingWithPrivateSide(Location l, int privatePort)


getPortMappingWithPublicIpId

public java.util.Collection getPortMappingWithPublicIpId(java.lang.String publicIpId)
returns the subset of port mappings associated with a given public IP ID


getPortMappingWithPublicSide

public PortMapping getPortMappingWithPublicSide(java.lang.String publicIpId, int publicPort)
returns the port mapping for a given publicIpId and public port


getPublicHostAndPort

public HostAndPort getPublicHostAndPort(PortMapping m)
returns the public host and port for use accessing the given mapping


getPublicIpHostname

public java.lang.String getPublicIpHostname(java.lang.String publicIpId)
returns a recorded public hostname or address


lookup

public HostAndPort lookup(Location l, int privatePort)
returns the public ip hostname and public port for use contacting the given endpoint; null if: * no publicPort is associated with this location and private port * no publicIpId is associated with this location and private port * no publicIpHostname is recorded against the associated publicIpId


makeKey

protected java.lang.String makeKey(java.lang.String publicIpId, int publicPort)


recordPublicIpHostname

public void recordPublicIpHostname(java.lang.String publicIpId, java.lang.String hostnameOrPublicIpAddress)
records a public hostname or address to be associated with the given publicIpId for lookup purposes


toString

@Override
public java.lang.String toString()


 

Brooklyn Multi-Cloud Application Management Platform
brooklyncentral.github.com. Apache License. © 2012.