Search in sources :

Example 81 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class EventIpcManagerDefaultImplTest method testRecursiveEvents.

/**
 * This test creates two event listeners that both create events as they
 * handle events. This test can be used to detect deadlocks between the
 * listeners.
 *
 * @throws InterruptedException
 */
public void testRecursiveEvents() throws InterruptedException {
    final int numberOfEvents = 20;
    CountDownLatch fooCounter = new CountDownLatch(numberOfEvents);
    CountDownLatch barCounter = new CountDownLatch(numberOfEvents);
    CountDownLatch kiwiCounter = new CountDownLatch(numberOfEvents);
    CountDownLatch ulfCounter = new CountDownLatch(numberOfEvents);
    final EventIpcManagerDefaultImpl manager = new EventIpcManagerDefaultImpl(m_registry);
    manager.setHandlerPoolSize(1);
    // manager.setHandlerQueueLength(5);
    DefaultEventHandlerImpl handler = new DefaultEventHandlerImpl(m_registry);
    manager.setEventHandler(handler);
    manager.afterPropertiesSet();
    EventListener slowFooBarListener = new EventListener() {

        @Override
        public String getName() {
            return "slowFooBarListener";
        }

        @Override
        public void onEvent(Event event) {
            if ("uei.opennms.org/foo".equals(event.getUei())) {
                EventBuilder bldr = new EventBuilder("uei.opennms.org/bar", "testRecursiveEvents");
                Event e = bldr.getEvent();
                manager.broadcastNow(e, false);
                fooCounter.countDown();
            } else {
                try {
                    Thread.sleep(SLOW_EVENT_OPERATION_DELAY);
                } catch (InterruptedException e) {
                }
                barCounter.countDown();
            }
        }
    };
    EventListener slowKiwiUlfListener = new EventListener() {

        @Override
        public String getName() {
            return "slowKiwiUlfListener";
        }

        @Override
        public void onEvent(Event event) {
            if ("uei.opennms.org/foo".equals(event.getUei())) {
                EventBuilder bldr = new EventBuilder("uei.opennms.org/ulf", "testRecursiveEvents");
                Event e = bldr.getEvent();
                manager.broadcastNow(e, false);
                kiwiCounter.countDown();
            } else {
                try {
                    Thread.sleep(SLOW_EVENT_OPERATION_DELAY);
                } catch (InterruptedException e) {
                }
                ulfCounter.countDown();
            }
        }
    };
    manager.addEventListener(slowFooBarListener);
    manager.addEventListener(slowKiwiUlfListener);
    // 
    for (int i = 0; i < numberOfEvents; i++) {
        EventBuilder bldr = new EventBuilder("uei.opennms.org/foo", "testRecursiveEvents");
        Event e = bldr.getEvent();
        manager.broadcastNow(e, false);
    }
    assertTrue("foo counter not satisfied: " + fooCounter.getCount(), fooCounter.await(100, TimeUnit.SECONDS));
    assertTrue("bar counter not satisfied: " + barCounter.getCount(), barCounter.await(100, TimeUnit.SECONDS));
    assertTrue("kiwi counter not satisfied: " + kiwiCounter.getCount(), kiwiCounter.await(100, TimeUnit.SECONDS));
    assertTrue("ulf counter not satisfied: " + ulfCounter.getCount(), ulfCounter.await(100, TimeUnit.SECONDS));
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) ThreadAwareEventListener(org.opennms.netmgt.events.api.ThreadAwareEventListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 82 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class EventdIT method sendServiceDownEvent.

/**
 * @param reductionKey
 */
private void sendServiceDownEvent(String reductionKey, OnmsMonitoredService svc) {
    final EventBuilder e = MockEventUtil.createEventBuilder("Test", EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI, svc.getNodeId(), str(svc.getIpAddress()), svc.getServiceName(), "Not responding");
    if (reductionKey != null) {
        AlarmData data = new AlarmData();
        data.setAlarmType(1);
        data.setReductionKey(reductionKey);
        e.setAlarmData(data);
    } else {
        e.setAlarmData(null);
    }
    e.setLogDest("logndisplay");
    e.setLogMessage("testing");
    m_eventdIpcMgr.sendNow(e.getEvent());
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) AlarmData(org.opennms.netmgt.xml.event.AlarmData)

Example 83 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class HibernateEventWriterIT method testEventDistPoller.

/**
 * Tests writing events with various distPoller values.
 *
 * @throws SQLException
 */
@Test
public void testEventDistPoller() throws Exception {
    String systemId = UUID.randomUUID().toString();
    EventBuilder bldr = new EventBuilder("testUei", "testSource");
    bldr.setDistPoller(systemId);
    bldr.setLogMessage("test");
    Event event = bldr.getEvent();
    assertEquals(new Integer(0), event.getDbid());
    m_eventWriter.process(bldr.getLog());
    assertTrue(event.getDbid() > 0);
    String minionId = jdbcTemplate.queryForObject("SELECT systemId FROM events LIMIT 1", String.class);
    assertEquals(DistPollerDao.DEFAULT_DIST_POLLER_ID, minionId);
    jdbcTemplate.execute("DELETE FROM events");
    jdbcTemplate.execute(String.format("INSERT INTO monitoringsystems (id, location, type) VALUES ('%s', 'Hello World', '%s')", systemId, OnmsMonitoringSystem.TYPE_MINION));
    event = bldr.getEvent();
    m_eventWriter.process(bldr.getLog());
    assertTrue(event.getDbid() > 0);
    minionId = jdbcTemplate.queryForObject("SELECT systemId FROM events LIMIT 1", String.class);
    assertEquals(systemId, minionId);
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Event(org.opennms.netmgt.xml.event.Event) Test(org.junit.Test)

Example 84 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class HibernateEventWriterIT method testWriteEventWithNull.

/**
 * Tests writing nulls to postgres db and the db encoding.
 * @throws SQLException
 */
@Test
public void testWriteEventWithNull() throws Exception {
    EventBuilder bldr = new EventBuilder("testUei", "testSource");
    bldr.setLogDest(HibernateEventWriter.LOG_MSG_DEST_LOG_AND_DISPLAY);
    bldr.addParam("test", "testVal");
    final String testVal2 = "valWith\u0000Null\u0000";
    bldr.addParam("test2", testVal2);
    byte[] bytes = new byte[] { 0x07, (byte) 0xD7, 0x04, 0x0A, 0x01, 0x17, 0x06, 0x00, 0x2B, 0x00, 0x00 };
    SnmpValue snmpVal = SnmpUtils.getValueFactory().getOctetString(bytes);
    assertFalse(snmpVal.isDisplayable());
    bldr.addParam("test3", snmpVal.toString());
    String b64 = EventConstants.toString(EventConstants.XML_ENCODING_BASE64, snmpVal);
    bldr.addParam("test", b64);
    Event event = bldr.getEvent();
    assertEquals(new Integer(0), event.getDbid());
    m_eventWriter.process(bldr.getLog());
    assertTrue(event.getDbid() > 0);
    final List<Map<String, Object>> parameters = jdbcTemplate.queryForList("SELECT name, value FROM event_parameters WHERE eventID = " + event.getDbid() + " ORDER BY name");
    assertEquals(3, parameters.size());
    assertEquals("test", parameters.get(0).get("name"));
    assertEquals("B9cECgEXBgArAAA%61", parameters.get(0).get("value"));
    assertEquals("test2", parameters.get(1).get("name"));
    assertEquals("valWith%0Null%0", parameters.get(1).get("value"));
    assertEquals("test3", parameters.get(2).get("name"));
    assertEquals(snmpVal.toString(), parameters.get(2).get("value"));
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Event(org.opennms.netmgt.xml.event.Event) Map(java.util.Map) Test(org.junit.Test)

Example 85 with EventBuilder

use of org.opennms.netmgt.model.events.EventBuilder in project opennms by OpenNMS.

the class HibernateEventWriterIT method testWriteEventWithParameters.

@Test
public void testWriteEventWithParameters() throws Exception {
    final EventBuilder builder = new EventBuilder("testUei", "testSource");
    builder.setLogDest(HibernateEventWriter.LOG_MSG_DEST_LOG_AND_DISPLAY);
    builder.addParam("param1", "value1");
    builder.addParam("param2", 1337);
    builder.addParam("param3", true);
    builder.addParam("param4", 23.42);
    // Test for duplicated values - last should win
    builder.addParam("param4", 42.23);
    m_eventWriter.process(builder.getLog());
    final List<Map<String, Object>> parameters = jdbcTemplate.queryForList("SELECT name, value FROM event_parameters WHERE eventID = " + builder.getEvent().getDbid() + " ORDER BY name");
    assertEquals(4, parameters.size());
    assertEquals("param1", parameters.get(0).get("name"));
    assertEquals("value1", parameters.get(0).get("value"));
    assertEquals("param2", parameters.get(1).get("name"));
    assertEquals("1337", parameters.get(1).get("value"));
    assertEquals("param3", parameters.get(2).get("name"));
    assertEquals("true", parameters.get(2).get("value"));
    assertEquals("param4", parameters.get(3).get("name"));
    assertEquals("42.23", parameters.get(3).get("value"));
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Map(java.util.Map) Test(org.junit.Test)

Aggregations

EventBuilder (org.opennms.netmgt.model.events.EventBuilder)305 Test (org.junit.Test)88 Event (org.opennms.netmgt.xml.event.Event)84 Date (java.util.Date)22 OnmsNode (org.opennms.netmgt.model.OnmsNode)17 InetAddress (java.net.InetAddress)16 EventProxyException (org.opennms.netmgt.events.api.EventProxyException)15 AlarmData (org.opennms.netmgt.xml.event.AlarmData)14 EventAnticipator (org.opennms.netmgt.dao.mock.EventAnticipator)12 ServletException (javax.servlet.ServletException)9 EventHandler (org.opennms.netmgt.events.api.annotations.EventHandler)9 Parm (org.opennms.netmgt.xml.event.Parm)9 Event (org.opennms.netmgt.xml.eventconf.Event)9 File (java.io.File)8 ArrayList (java.util.ArrayList)8 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)7 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)7 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)7 SyntaxToEvent (org.opennms.netmgt.model.events.snmp.SyntaxToEvent)7 Transactional (org.springframework.transaction.annotation.Transactional)6