use of org.opennms.protocols.sftp.Sftp3gppUrlConnection in project opennms by OpenNMS.
the class Sftp3gppVTDXmlCollectionHandler method collect.
@Override
public CollectionSet collect(CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters) throws CollectionException {
String status = "finished";
// Create a new collection set.
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
// TODO We could be careful when handling exceptions because parsing exceptions will be treated different from connection or retrieval exceptions
DateTime startTime = new DateTime();
Sftp3gppUrlConnection connection = null;
try {
// FIXME: Does not support storeByFS
ResourcePath resourcePath = ResourcePath.get(Integer.toString(agent.getNodeId()));
for (XmlSource source : collection.getXmlSources()) {
if (!source.getUrl().startsWith(Sftp3gppUrlHandler.PROTOCOL)) {
throw new CollectionException("The 3GPP SFTP Collection Handler can only use the protocol " + Sftp3gppUrlHandler.PROTOCOL);
}
final String urlStr = source.getUrl();
final Request request = source.getRequest();
URL url = UrlFactory.getUrl(source.getUrl(), source.getRequest());
String lastFile = Sftp3gppUtils.getLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath());
connection = (Sftp3gppUrlConnection) url.openConnection();
if (lastFile == null) {
lastFile = connection.get3gppFileName();
LOG.debug("collect(single): retrieving file from {}{}{} from {}", url.getPath(), File.separatorChar, lastFile, agent.getHostAddress());
VTDNav doc = getVTDXmlDocument(urlStr, request);
fillCollectionSet(agent, builder, source, doc);
Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), lastFile);
Sftp3gppUtils.deleteFile(connection, lastFile);
} else {
connection.connect();
List<String> files = connection.getFileList();
long lastTs = connection.getTimeStampFromFile(lastFile);
boolean collected = false;
for (String fileName : files) {
if (connection.getTimeStampFromFile(fileName) > lastTs) {
LOG.debug("collect(multiple): retrieving file {} from {}", fileName, agent.getHostAddress());
InputStream is = connection.getFile(fileName);
try {
VTDNav doc = getVTDXmlDocument(is, request);
fillCollectionSet(agent, builder, source, doc);
} finally {
IOUtils.closeQuietly(is);
}
Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), fileName);
Sftp3gppUtils.deleteFile(connection, fileName);
collected = true;
}
}
if (!collected) {
LOG.warn("collect: could not find any file after {} on {}", lastFile, agent);
}
}
}
return builder.build();
} catch (Exception e) {
status = "failed";
throw new CollectionException(e.getMessage(), e);
} finally {
DateTime endTime = new DateTime();
LOG.debug("collect: {} collection {}: duration: {} ms", status, collection.getName(), endTime.getMillis() - startTime.getMillis());
UrlFactory.disconnect(connection);
}
}
use of org.opennms.protocols.sftp.Sftp3gppUrlConnection in project opennms by OpenNMS.
the class Sftp3gppXmlCollectionHandler method collect.
/* (non-Javadoc)
* @see org.opennms.protocols.xml.collector.XmlCollectionHandler#collect(org.opennms.netmgt.collectd.CollectionAgent, org.opennms.protocols.xml.config.XmlDataCollection, java.util.Map)
*/
@Override
public CollectionSet collect(CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters) throws CollectionException {
String status = "finished";
// Create a new collection set.
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
// TODO We could be careful when handling exceptions because parsing exceptions will be treated different from connection or retrieval exceptions
DateTime startTime = new DateTime();
Sftp3gppUrlConnection connection = null;
try {
ResourcePath resourcePath = ResourcePath.get(ResourceTypeUtils.SNMP_DIRECTORY, Integer.toString(agent.getNodeId()));
for (XmlSource source : collection.getXmlSources()) {
if (!source.getUrl().startsWith(Sftp3gppUrlHandler.PROTOCOL)) {
throw new CollectionException("The 3GPP SFTP Collection Handler can only use the protocol " + Sftp3gppUrlHandler.PROTOCOL);
}
final String urlStr = source.getUrl();
final Request request = source.getRequest();
URL url = UrlFactory.getUrl(urlStr, request);
String lastFile = Sftp3gppUtils.getLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath());
connection = (Sftp3gppUrlConnection) url.openConnection();
if (lastFile == null) {
lastFile = connection.get3gppFileName();
LOG.debug("collect(single): retrieving file from {}{}{} from {}", url.getPath(), File.separatorChar, lastFile, agent.getHostAddress());
Document doc = getXmlDocument(urlStr, request);
fillCollectionSet(agent, builder, source, doc);
Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), lastFile);
Sftp3gppUtils.deleteFile(connection, lastFile);
} else {
connection.connect();
List<String> files = connection.getFileList();
long lastTs = connection.getTimeStampFromFile(lastFile);
boolean collected = false;
for (String fileName : files) {
if (connection.getTimeStampFromFile(fileName) > lastTs) {
LOG.debug("collect(multiple): retrieving file {} from {}", fileName, agent.getHostAddress());
InputStream is = connection.getFile(fileName);
try {
Document doc = getXmlDocument(is, request);
IOUtils.closeQuietly(is);
fillCollectionSet(agent, builder, source, doc);
} finally {
IOUtils.closeQuietly(is);
}
Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), fileName);
Sftp3gppUtils.deleteFile(connection, fileName);
collected = true;
}
}
if (!collected) {
LOG.warn("collect: could not find any file after {} on {}", lastFile, agent);
}
}
}
return builder.build();
} catch (Exception e) {
status = "failed";
throw new CollectionException(e.getMessage(), e);
} finally {
DateTime endTime = new DateTime();
LOG.debug("collect: {} collection {}: duration: {} ms", status, collection.getName(), endTime.getMillis() - startTime.getMillis());
UrlFactory.disconnect(connection);
}
}
use of org.opennms.protocols.sftp.Sftp3gppUrlConnection in project opennms by OpenNMS.
the class UrlFactoryTest method testUrlFactory.
/**
* Test time parser.
*
* @throws Exception the exception
*/
@Test
public void testUrlFactory() throws Exception {
URL url = UrlFactory.getUrl("https://www.google.com", null);
Assert.assertEquals(HttpsUrlHandler.HTTPS, url.getProtocol());
Assert.assertTrue(url.openConnection() instanceof HttpUrlConnection);
url = UrlFactory.getUrl("HTTP://www.opennms.org", null);
Assert.assertEquals(HttpUrlHandler.HTTP, url.getProtocol());
Assert.assertTrue(url.openConnection() instanceof HttpUrlConnection);
url = UrlFactory.getUrl("sftp://www.opennms.org", null);
Assert.assertEquals(SftpUrlHandler.PROTOCOL, url.getProtocol());
Assert.assertTrue(url.openConnection() instanceof SftpUrlConnection);
url = UrlFactory.getUrl("sftp.3GPP://junier-router.local/opt/3gpp/data", null);
Assert.assertEquals(Sftp3gppUrlHandler.PROTOCOL, url.getProtocol());
Assert.assertTrue(url.openConnection() instanceof Sftp3gppUrlConnection);
}
Aggregations