/** * Validates the Flow target given by attempting to retrieve data * * @param target The target to validate * @return VALIDATED if the target was successfully validated, an error message otherwise */ public static String validateFlowTarget(VMTTarget target) { if (target != null) { String logPrefix = target.getNameOrAddress() + " : "; if (!FeaturesManager.vmtMANAGER.isNetworkModeEnabled()) { logger.warn(logPrefix + "Validation failed"); return UNLICENSED; } if ("NETFLOW".equals(target.getVersion())) { ch.ethz.ssh2.Connection conn = null; try { if (logger.isDebugEnabled()) logger.debug(logPrefix + "Validating " + target.getNameOrAddress()); VMTCredentials credentials = target.getCredentials(); if (credentials instanceof UsernamePassword) { UsernamePassword user = (UsernamePassword) credentials; String password = VMTEncryptionUtil.decrypt( credentials, MediationPackage.eINSTANCE.getUsernamePassword_Password()); conn = SshUtil.establishConnection( target.getNameOrAddress(), user.getUsername(), password); if (conn != null && conn.isAuthenticationComplete()) { Session ioSess = conn.openSession(); ioSess.execCommand(nFlowDumpCommand); return VALIDATED; } else { logger.warn(logPrefix + "Validation failed"); String reason = "Cannot establish SSH connection with the flow collector using the credential"; return reason; } } } catch (Exception e) { logger.warn(logPrefix + "Validation failed"); return e.getClass().getSimpleName(); } finally { SshUtil.closeConnection(conn, target); } } else if ("SFLOW".equals(target.getVersion())) { String targetAddress = target.getNameOrAddress(); String url = formUrl(targetAddress, SFLOW_REQUEST_IN_JSON); URL nodesURL; URLConnection nodesConnection = null; InputStreamReader in = null; try { nodesURL = new java.net.URL(url); nodesConnection = nodesURL.openConnection(); nodesConnection.connect(); in = new InputStreamReader((InputStream) nodesConnection.getContent()); if (in.ready()) return VALIDATED; else return "Cannot validate the SFLOW target"; } catch (Exception e) { return e.getMessage(); } finally { // Close the connection if (in != null) { try { in.close(); } catch (IOException e) { handleException( e, logger, "The following exception occured while attempting to close the connection"); } } } } } else { logger.warn("Null target : Validation failed"); return "TARGET IS NULL"; } return VALIDATED; // TODO: probably not the wanted behaviour }