Search in sources :

Example 86 with FileSystemResource

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());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) FileSystemResource(org.springframework.core.io.FileSystemResource) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsEventParameter(org.opennms.netmgt.model.OnmsEventParameter) File(java.io.File) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 87 with FileSystemResource

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();
}
Also used : FileWriter(java.io.FileWriter) FileSystemResource(org.springframework.core.io.FileSystemResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Test(org.junit.Test)

Example 88 with FileSystemResource

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) FileSystemResource(org.springframework.core.io.FileSystemResource) LinkedList(java.util.LinkedList) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) OnmsEventParameter(org.opennms.netmgt.model.OnmsEventParameter) File(java.io.File) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 89 with FileSystemResource

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();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource)

Example 90 with FileSystemResource

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());
}
Also used : PrefabGraph(org.opennms.netmgt.model.PrefabGraph) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) FileSystemResource(org.springframework.core.io.FileSystemResource) OnmsResource(org.opennms.netmgt.model.OnmsResource) Resource(org.springframework.core.io.Resource) OutputStreamWriter(java.io.OutputStreamWriter) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Test(org.junit.Test)

Aggregations

FileSystemResource (org.springframework.core.io.FileSystemResource)148 File (java.io.File)77 Test (org.junit.Test)44 Resource (org.springframework.core.io.Resource)42 Before (org.junit.Before)27 ClassPathResource (org.springframework.core.io.ClassPathResource)16 IOException (java.io.IOException)12 FileWriter (java.io.FileWriter)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Properties (java.util.Properties)8 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)8 FileOutputStream (java.io.FileOutputStream)7 URL (java.net.URL)7 InputStreamResource (org.springframework.core.io.InputStreamResource)7 UrlResource (org.springframework.core.io.UrlResource)7 Date (java.util.Date)5 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)5 FileReader (java.io.FileReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4