use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.
the class SnmpTrapNorthbounderTest method testNorthbounder.
/**
* Tests the trap northbounder.
*
* @throws Exception the exception
*/
// FIXME Verify the content of the trap sent.
@Test
public void testNorthbounder() throws Exception {
// Setup the configuration DAO
FileSystemResource resource = new FileSystemResource(new File("src/test/resources/etc/snmptrap-northbounder-config.xml"));
SnmpTrapNorthbounderConfigDao configDao = new SnmpTrapNorthbounderConfigDao();
configDao.setConfigResource(resource);
configDao.afterPropertiesSet();
// Setup the trap northbounder (overriding the settings of the first sink to use the test trap receiver)
SnmpTrapSink sink = configDao.getConfig().getSnmpTrapSink("localTest1");
sink.setIpAddress(TRAP_DESTINATION.getHostAddress());
sink.setPort(TRAP_PORT);
SnmpTrapNorthbounder nbi = new SnmpTrapNorthbounder(configDao, sink.getName());
nbi.afterPropertiesSet();
// Setup test node
OnmsNode node = new OnmsNode();
node.setForeignSource("Server-MacOS");
node.setForeignId("1");
node.setId(1);
node.setLabel("my-test-server");
OnmsSnmpInterface snmpInterface = new OnmsSnmpInterface(node, 1);
snmpInterface.setId(1);
snmpInterface.setIfAlias("Connection to OpenNMS Wifi");
snmpInterface.setIfDescr("en1");
snmpInterface.setIfName("en1/0");
snmpInterface.setPhysAddr("00:00:00:00:00:01");
InetAddress address = InetAddress.getByName("10.0.1.1");
OnmsIpInterface onmsIf = new OnmsIpInterface(address, node);
onmsIf.setSnmpInterface(snmpInterface);
onmsIf.setId(1);
onmsIf.setIfIndex(1);
onmsIf.setIpHostName("my-test-server");
onmsIf.setIsSnmpPrimary(PrimaryType.PRIMARY);
node.getIpInterfaces().add(onmsIf);
// Setup test alarm
OnmsAlarm onmsAlarm = new OnmsAlarm();
onmsAlarm.setId(100);
onmsAlarm.setNode(node);
onmsAlarm.setIpAddr(address);
onmsAlarm.setUei("uei.opennms.org/trap/myTrap1");
onmsAlarm.setLastEvent(new OnmsEvent() {
{
this.setEventParameters(Lists.newArrayList(new OnmsEventParameter(this, "alarmId", "10", "Int32"), new OnmsEventParameter(this, "alarmMessage", "this is a test", "string")));
}
});
NorthboundAlarm alarm = new NorthboundAlarm(onmsAlarm);
Assert.assertEquals(2, alarm.getEventParametersCollection().size());
// Verify the nortbound alarm and send it to the test receiver
Assert.assertTrue(nbi.accepts(alarm));
nbi.forwardAlarms(Collections.singletonList(alarm));
// Introduce a delay to make sure the trap was sent and received.
Thread.sleep(5000);
Assert.assertEquals(1, getTrapsReceivedCount());
}
use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.
the class SyslogConfigDaoTest method testModifyConfiguration.
/**
* Test modify configuration.
*
* @throws Exception the exception
*/
@Test
public void testModifyConfiguration() throws Exception {
File configFile = new File("target/syslog-northbounder-test.xml");
FileWriter writer = new FileWriter(configFile);
writer.write(xmlWithFilters);
writer.close();
Resource resource = new FileSystemResource(configFile);
SyslogNorthbounderConfigDao dao = new SyslogNorthbounderConfigDao();
dao.setConfigResource(resource);
dao.afterPropertiesSet();
assertNotNull(dao.getConfig());
SyslogDestination dst = dao.getConfig().getSyslogDestination("test-host");
assertNotNull(dst);
dst.setHost("192.168.0.1");
dao.save();
dao.reload();
assertEquals("192.168.0.1", dao.getConfig().getSyslogDestination("test-host").getHost());
configFile.delete();
}
use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.
the class SyslogNorthBounderWithFiltersTest method testForwardAlarms.
/* (non-Javadoc)
* @see org.opennms.netmgt.alarmd.northbounder.syslog.SyslogNorthBounderTest#testForwardAlarms()
*/
@Test
@Override
public void testForwardAlarms() throws Exception {
// Initialize the configuration
File configFile = new File("target/syslog-northbounder-config.xml");
FileUtils.copyFile(new File("src/test/resources/syslog-northbounder-config1.xml"), configFile);
// Initialize the configuration DAO
SyslogNorthbounderConfigDao dao = new SyslogNorthbounderConfigDao();
dao.setConfigResource(new FileSystemResource(configFile));
dao.afterPropertiesSet();
// Initialize the Syslog northbound interfaces
List<SyslogNorthbounder> nbis = new LinkedList<>();
for (SyslogDestination syslogDestination : dao.getConfig().getDestinations()) {
SyslogNorthbounder nbi = new SyslogNorthbounder(dao, syslogDestination.getName());
nbi.afterPropertiesSet();
nbis.add(nbi);
}
// Add a sample node to the database
OnmsNode node = new OnmsNode();
node.setForeignSource("TestGroup");
node.setForeignId("1");
node.setId(m_nodeDao.getNextNodeId());
node.setLabel("agalue");
OnmsSnmpInterface snmpInterface = new OnmsSnmpInterface(node, 1);
snmpInterface.setId(1);
snmpInterface.setIfAlias("Connection to OpenNMS Wifi");
snmpInterface.setIfDescr("en1");
snmpInterface.setIfName("en1/0");
snmpInterface.setPhysAddr("00:00:00:00:00:01");
Set<OnmsIpInterface> ipInterfaces = new LinkedHashSet<>();
InetAddress address = InetAddress.getByName("10.0.1.1");
OnmsIpInterface onmsIf = new OnmsIpInterface(address, node);
onmsIf.setSnmpInterface(snmpInterface);
onmsIf.setId(1);
onmsIf.setIfIndex(1);
onmsIf.setIpHostName("agalue");
onmsIf.setIsSnmpPrimary(PrimaryType.PRIMARY);
ipInterfaces.add(onmsIf);
node.setIpInterfaces(ipInterfaces);
m_nodeDao.save(node);
m_nodeDao.flush();
// Create a sample Alarm
OnmsAlarm onmsAlarm = new OnmsAlarm();
onmsAlarm.setId(10);
onmsAlarm.setUei("uei.opennms.org/nodes/interfaceDown");
onmsAlarm.setNode(node);
onmsAlarm.setSeverityId(6);
onmsAlarm.setIpAddr(address);
onmsAlarm.setCounter(1);
onmsAlarm.setLogMsg("Interface Down");
onmsAlarm.setLastEvent(new OnmsEvent() {
{
this.setEventParameters(Lists.newArrayList(new OnmsEventParameter(this, "owner", "agalue", "String")));
}
});
NorthboundAlarm nbAlarm = new NorthboundAlarm(onmsAlarm);
List<NorthboundAlarm> alarms = new LinkedList<>();
alarms.add(nbAlarm);
// Verify filters and send alarms to the northbound interfaces
for (SyslogNorthbounder nbi : nbis) {
Assert.assertTrue(nbi.accepts(nbAlarm));
nbi.forwardAlarms(alarms);
}
// Induce a delay (based on the parent code)
Thread.sleep(100);
// Extract the log messages and verify the content
BufferedReader reader = new BufferedReader(new StringReader(m_logStream.readStream()));
List<String> messages = getMessagesFromBuffer(reader);
Assert.assertTrue("Log messages sent: 2, Log messages received: " + messages.size(), 2 == messages.size());
Assert.assertTrue(messages.get(0).contains("ALARM 10 FROM NODE agalue@TestGroup"));
Assert.assertTrue(messages.get(1).contains("ALARM 10 FROM INTERFACE 10.0.1.1"));
reader.close();
// Remove the temporary configuration file
configFile.delete();
}
use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.
the class Main method main.
/**
* <p>main</p>
*
* @param args an array of {@link java.lang.String} objects.
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext appContext = null;
try {
appContext = new ClassPathXmlApplicationContext("/META-INF/modelImport-appContext.xml");
Provisioner importer = (Provisioner) appContext.getBean("modelImporter");
Resource resource = new FileSystemResource(args[0]);
importer.importModelFromResource(resource, Boolean.TRUE.toString());
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (appContext != null)
appContext.close();
}
}
use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.
the class PropertiesGraphDaoIT method testPrefabConfigDirectoryMultiReports.
/**
* Test that properties files in an included directory with
* multiple graphs defined in them are loaded correctly
*/
@Test
public void testPrefabConfigDirectoryMultiReports() throws IOException {
File rootFile = m_fileAnticipator.tempFile("snmp-graph.properties");
File graphDirectory = m_fileAnticipator.tempDir("snmp-graph.properties.d");
File multiFile1 = m_fileAnticipator.tempFile(graphDirectory, "mib2.bits1.properties");
File multiFile2 = m_fileAnticipator.tempFile(graphDirectory, "mib2.bits2.properties");
m_outputStream = new FileOutputStream(rootFile);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_baseIncludePrefab);
m_writer.close();
m_outputStream.close();
graphDirectory.mkdir();
m_outputStream = new FileOutputStream(multiFile1);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_includedMultiGraph1);
m_writer.close();
m_outputStream.close();
m_outputStream = new FileOutputStream(multiFile2);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_includedMultiGraph2);
m_writer.close();
m_outputStream.close();
HashMap<String, Resource> prefabConfigs = new HashMap<String, Resource>();
prefabConfigs.put("performance", new FileSystemResource(rootFile));
PropertiesGraphDao dao = createPropertiesGraphDao(prefabConfigs, s_emptyMap);
// Check the graphs, basically ensuring that a handful of unique but easily checkable
// bits are uniquely what they should be.
// We check all 4 graphs
PrefabGraph mib2Bits = dao.getPrefabGraph("mib2.bits");
assertNotNull(mib2Bits);
assertEquals("mib2.bits", mib2Bits.getName());
assertEquals("Bits In/Out", mib2Bits.getTitle());
String[] columns1 = { "ifInOctets", "ifOutOctets" };
Assert.assertArrayEquals(columns1, mib2Bits.getColumns());
PrefabGraph mib2HCBits = dao.getPrefabGraph("mib2.HCbits");
assertNotNull(mib2HCBits);
assertEquals("mib2.HCbits", mib2HCBits.getName());
assertEquals("Bits In/Out", mib2HCBits.getTitle());
String[] columns2 = { "ifHCInOctets", "ifHCOutOctets" };
Assert.assertArrayEquals(columns2, mib2HCBits.getColumns());
PrefabGraph mib2Discards = dao.getPrefabGraph("mib2.discards");
assertNotNull(mib2Discards);
assertEquals("mib2.discards", mib2Discards.getName());
assertEquals("Discards In/Out", mib2Discards.getTitle());
String[] columns3 = { "ifInDiscards", "ifOutDiscards" };
Assert.assertArrayEquals(columns3, mib2Discards.getColumns());
PrefabGraph mib2Errors = dao.getPrefabGraph("mib2.errors");
assertNotNull(mib2Errors);
assertEquals("mib2.errors", mib2Errors.getName());
assertEquals("Errors In/Out", mib2Errors.getTitle());
String[] columns4 = { "ifInErrors", "ifOutErrors" };
Assert.assertArrayEquals(columns4, mib2Errors.getColumns());
}
Aggregations