use of com.emc.fapiclient.ws.FunctionalAPIImpl in project coprhd-controller by CoprHD.
the class RecoverPointClientFactory method getClient.
/**
* Static method to manage Recover Point connections via the RecoverPointClient class.
*
* When a valid connection is created the RecoverPointClient object is kept in a ConcurrentHashMap
* and referenced by a unique key combination comprised of the endpoint + username + password.
*
* If a connection key exists, a handle to an existing RecoverPointClient object will
* be returned instead of creating a brand new connection. Otherwise, a new connection will be created
* and stored (if valid).
*
* If the connection info is invalid, an exception will be thrown and the object will not be stored.
*
* The method can potentially be called by multiple threads, therefore synchronization is required
* to maintain thread safety. ConcurrentHashMap is, by default, thread safe which is why
* it is being used to store valid connections.
*
* @param protectionSystem for a unique key, if we connect using a secondary address, keep using that address.
* @param endpoints URI to the RecoverPoint System
* @param username Username to log into the RecoverPoint System
* @param password Password to log into the RecoverPoint System
*
* @return RecoverPointClient with a connection to the RPA.
* @throws RecoverPointException
*/
public static synchronized RecoverPointClient getClient(URI protectionSystem, List<URI> endpoints, String username, String password) throws RecoverPointException {
logger.info("Attempting to get RecoverPointClient connection...");
// Throw an exception if null credentials are passed in.
if (endpoints == null || username == null || password == null) {
throw RecoverPointException.exceptions.invalidCrendentialsPassedIn(username, password);
}
// Unique key to identify RP connections for different Protection Systems
String key = String.valueOf(protectionSystem) + username + password;
// See if there is an existing valid RecoverPointClient using the protection system key
RecoverPointClient existingClient = clientMap.get(key);
if (existingClient != null) {
logger.info("Existing RecoverPointClient connection found. Re-using connection: " + existingClient.getEndpoint().toString());
try {
// Do a ping check. If this fails, try the other IP addresses.
existingClient.ping();
return existingClient;
} catch (Exception e) {
logger.error("Received " + e.toString() + ". Failed to ping Mgmt IP: " + existingClient.getEndpoint().toString() + ", Cause: " + RecoverPointClient.getCause(e));
// remove the protection system's connection from the factory, and the endpoint from the endpoints list
clientMap.remove(key);
endpoints.remove(existingClient.getEndpoint());
}
}
// Now go through the endpoints and try to create a new RP client connection.
Iterator<URI> endpointIter = endpoints.iterator();
while (endpointIter.hasNext()) {
URI endpoint = endpointIter.next();
// Throw an exception if the endpoint can not be resolved to an ASCII string
String mgmtIPAddress = endpoint.toASCIIString();
if (mgmtIPAddress == null) {
throw RecoverPointException.exceptions.noRecoverPointEndpoint();
}
logger.info("Creating new RecoverPointClient connection to: " + mgmtIPAddress);
// If we don't have an existing RecoverPointClient, create a new one and add it to the client map only
// if the connection is valid.
RecoverPointClient newRecoverpointClient = new RecoverPointClient(endpoint, username, password);
FunctionalAPIImpl impl = null;
try {
// Create the connection
impl = new RecoverPointConnection().connect(endpoint, username, password);
logger.info("New RecoverPointClient connection created to: " + mgmtIPAddress);
// Add the new RecoverPointConnection to the RecoverPointClient
newRecoverpointClient.setFunctionalAPI(impl);
// We just connected but to be safe, lets do a quick ping to confirm that
// we can reach the new RecoverPoint client
newRecoverpointClient.ping();
// Update the client map
clientMap.put(key, newRecoverpointClient);
return newRecoverpointClient;
} catch (Exception e) {
logger.error("Received " + e.toString() + ". Failed to create new RP connection: " + endpoint.toString() + ", Cause: " + RecoverPointClient.getCause(e));
// Remove invalid entry
clientMap.remove(key);
if (endpointIter.hasNext()) {
logger.info("Trying a different IP address to contact RP...");
} else {
throw RecoverPointException.exceptions.failedToPingMgmtIP(mgmtIPAddress, RecoverPointClient.getCause(e));
}
}
}
// You'll never get here.
return null;
}
use of com.emc.fapiclient.ws.FunctionalAPIImpl in project coprhd-controller by CoprHD.
the class RecoverPointClient method reconnect.
/**
* Method to refresh the connection of this RPClient to the Recover Point System
* via FAPI. Used just in case the connection has become stale.
*/
public void reconnect() {
logger.info(String.format("Attempt to refresh connection to RecoverPoint at %s", this.getEndpoint()));
try {
// Remove existing FAPI reference
this.setFunctionalAPI(null);
// Create the connection
FunctionalAPIImpl impl = new RecoverPointConnection().connect(this.getEndpoint(), this.getUsername(), this.getPassword());
// Add the new FAPI instance to the RecoverPointClient
this.setFunctionalAPI(impl);
// We just connected but to be safe, lets do a quick ping to confirm that
// we can reach the new RecoverPoint client
this.ping();
logger.info("Connection refreshed.");
} catch (Exception e) {
logger.error("Received " + e.toString() + ". Failed to refresh RP connection: " + this.getEndpoint().toString() + ", Cause: " + RecoverPointClient.getCause(e));
throw RecoverPointException.exceptions.failedToPingMgmtIP(this.getEndpoint().toString(), RecoverPointClient.getCause(e));
}
}
use of com.emc.fapiclient.ws.FunctionalAPIImpl in project coprhd-controller by CoprHD.
the class RecoverPointConnection method connect.
/**
* Connect to RP and return a handle that can be used for FAPI calls
*
* @param endpoint - Address to connect to
* @param username - Username for credentials
* @param password - Password for credentials
*
* @return FunctionalAPIImpl - A handle for FAPI access
*/
public FunctionalAPIImpl connect(URI endpoint, String username, String password) {
try {
ignoreCertifications();
// interceptCertificates();
} catch (Exception e) {
// so what?
}
String destAddress = endpoint.toASCIIString();
try {
URL baseUrl = FunctionalAPIImplService.class.getResource(".");
URL url = new URL(baseUrl, destAddress);
final String finalUser = username;
final String finalPassword = password;
// Modify the System Property for Max Redirects to a smaller number so we don't hammer
// the device over and over unnecessarily with bad credentials (if they're bad) as this
// takes a long time. Default is 20 redirects.
// However we will save the old redirect value and restore it after we're done.
String oldMaxRedirectsValue = null;
try {
oldMaxRedirectsValue = System.getProperty(SYSPROPS_HTTP_MAX_REDIRECTS);
} catch (NullPointerException npe) {
logger.warn("The System property " + SYSPROPS_HTTP_MAX_REDIRECTS + " does not already exist for some reason.");
}
// Set the Property for http.maxRedirects to 2 to prevent unnecessary retries
System.setProperty(SYSPROPS_HTTP_MAX_REDIRECTS, NEW_MAX_REDIRECTS);
// If we're creating a new connection, we want to clear the cache as it may be holding
// onto a previously valid connection.
AuthCacheValue.setAuthCache(new AuthCacheImpl());
Authenticator.setDefault(null);
// Create a PasswordAuthentication so when the request is asked via HTTP for authentication,
// the below will be automatically returned. This is set here, but invoked behind the scenes.
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(finalUser, finalPassword.toCharArray());
}
});
logger.info("Attempting to connect to service " + FAPI_SERVICENAME + " at url " + FAPI_URL + " using auth credentials for: " + finalUser);
// Connect to the service
FunctionalAPIImplService service = new FunctionalAPIImplService(url, new QName(FAPI_URL, FAPI_SERVICENAME));
FunctionalAPIImpl impl = service.getFunctionalAPIImplPort();
BindingProvider bp = (BindingProvider) impl;
Map<String, Object> map = bp.getRequestContext();
logger.info("RecoverPoint service: Dest: " + destAddress + ", user: " + username);
map.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, destAddress);
map.put(BindingProvider.USERNAME_PROPERTY, username);
map.put(BindingProvider.PASSWORD_PROPERTY, password);
// Reset the System Property for http.maxRedirects, but only if an existing value was present
if (oldMaxRedirectsValue != null && !oldMaxRedirectsValue.isEmpty()) {
System.setProperty(SYSPROPS_HTTP_MAX_REDIRECTS, oldMaxRedirectsValue);
}
logger.info("Connected.");
return impl;
} catch (MalformedURLException e) {
logger.error("Failed to create URL for the wsdl Location: " + destAddress);
logger.error(e.getMessage());
return null;
}
}
Aggregations