Search in sources :

Example 1 with EventListener

use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.

the class MockNetworkTest method testEventListeners.

public void testEventListeners() {
    Event sentEvent = MockEventUtil.createEvent("Test", EventConstants.NODE_GAINED_SERVICE_EVENT_UEI, 1, "192.168.1.1", "NEW", null);
    Event sentEvent2 = MockEventUtil.createEvent("Test", EventConstants.NODE_REGAINED_SERVICE_EVENT_UEI, 1, "192.168.1.1", "NEW", null);
    class MockListener implements EventListener {

        private Event receivedEvent;

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

        public Event getReceivedEvent() {
            return receivedEvent;
        }

        @Override
        public void onEvent(Event event) {
            System.err.println("onEvent: " + event.getUei());
            receivedEvent = event;
        }

        public void reset() {
            receivedEvent = null;
        }
    }
    ;
    MockListener listener = new MockListener();
    m_eventMgr.addEventListener(listener, EventConstants.NODE_GAINED_SERVICE_EVENT_UEI);
    m_eventMgr.sendEventToListeners(sentEvent);
    assertTrue(EventUtils.eventsMatch(sentEvent, listener.getReceivedEvent()));
    listener.reset();
    m_eventMgr.sendEventToListeners(sentEvent2);
    assertFalse(EventUtils.eventsMatch(sentEvent2, listener.getReceivedEvent()));
}
Also used : Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener)

Example 2 with EventListener

use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.

the class PolicyIT method anticipateEvents.

private BackgroundTask anticipateEvents(String... ueis) {
    final CountDownLatch eventRecieved = new CountDownLatch(1);
    m_eventSubscriber.addEventListener(new EventListener() {

        @Override
        public void onEvent(Event e) {
            eventRecieved.countDown();
        }

        @Override
        public String getName() {
            return "Test Initial Setup";
        }
    }, Arrays.asList(ueis));
    return new BackgroundTask() {

        @Override
        public void await() throws InterruptedException {
            eventRecieved.await();
        }
    };
}
Also used : Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with EventListener

use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.

the class ProvisioningITCase method anticipateEvents.

protected CountDownLatch anticipateEvents(final int numberToMatch, final String... ueis) {
    final CountDownLatch eventReceived = new CountDownLatch(numberToMatch);
    m_eventSubscriber.addEventListener(new EventListener() {

        @Override
        public void onEvent(final Event e) {
            eventReceived.countDown();
        }

        @Override
        public String getName() {
            return "Provisioning Test Case";
        }
    }, Arrays.asList(ueis));
    return eventReceived;
}
Also used : Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with EventListener

use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.

the class EifAdapterBlueprintTest method testCanParseEifWithSemicolonsInSlotsAndGenerateEvents.

@Test
public void testCanParseEifWithSemicolonsInSlotsAndGenerateEvents() throws Exception {
    // Register an event listener
    final List<Event> receivedEvents = Lists.newArrayList();
    eventIpcManager.addEventListener(new EventListener() {

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

        @Override
        public void onEvent(Event e) {
            receivedEvents.add(e);
        }
    });
    FileInputStream eifPacketCapture = new FileInputStream(new File("src/test/resources/eif_packets_semicolon_test.dat"));
    Socket clientSocket = new Socket(InetAddrUtils.getLocalHostAddress(), 1828);
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    outToServer.write(IOUtils.toByteArray(eifPacketCapture));
    outToServer.flush();
    clientSocket.close();
    await().atMost(15, SECONDS).until(() -> receivedEvents.size() == 6);
    for (Event event : receivedEvents) {
        assertTrue("UEI must match regex.", event.getUei().matches("^uei.opennms.org/vendor/IBM/EIF/EIF_TEST_EVENT_TYPE_\\w$"));
        assertTrue("situation_name must match regex.", event.getParm("situation_name").getValue().getContent().matches("^Situation \\d{2}"));
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) File(java.io.File) FileInputStream(java.io.FileInputStream) Socket(java.net.Socket) Test(org.junit.Test) CamelBlueprintTest(org.opennms.core.test.camel.CamelBlueprintTest)

Example 5 with EventListener

use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.

the class EventIpcManagerDefaultImpl method broadcastNow.

@Override
public void broadcastNow(Event event, boolean synchronous) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Event ID {} to be broadcasted: {}", event.getDbid(), event.getUei());
    }
    if (LOG.isDebugEnabled() && m_listeners.isEmpty()) {
        LOG.debug("No listeners interested in all events");
    }
    List<CompletableFuture<Void>> listenerFutures = new ArrayList<>();
    // Send to listeners interested in receiving all events
    for (EventListener listener : m_listeners) {
        listenerFutures.add(queueEventToListener(event, listener));
    }
    if (event.getUei() == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Event ID {} does not have a UEI, so skipping UEI matching", event.getDbid());
        }
        return;
    }
    /*
         * Send to listeners who are interested in this event UEI.
         * Loop to attempt partial wild card "directory" matches.
         */
    Set<EventListener> sentToListeners = new HashSet<EventListener>();
    for (String uei = event.getUei(); uei.length() > 0; ) {
        if (m_ueiListeners.containsKey(uei)) {
            for (EventListener listener : m_ueiListeners.get(uei)) {
                if (!sentToListeners.contains(listener)) {
                    listenerFutures.add(queueEventToListener(event, listener));
                    sentToListeners.add(listener);
                }
            }
        }
        // Try wild cards: Find / before last character
        int i = uei.lastIndexOf("/", uei.length() - 2);
        if (i > 0) {
            // Split at "/", including the /
            uei = uei.substring(0, i + 1);
        } else {
            // No more wild cards to match
            break;
        }
    }
    if (sentToListeners.isEmpty()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No listener interested in event ID {}: {}", event.getDbid(), event.getUei());
        }
    }
    // If synchronous...
    if (synchronous) {
        // Wait for all of the listeners to complete before returning
        CompletableFuture.allOf(listenerFutures.toArray(new CompletableFuture[0])).join();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) EventListener(org.opennms.netmgt.events.api.EventListener) ThreadAwareEventListener(org.opennms.netmgt.events.api.ThreadAwareEventListener) HashSet(java.util.HashSet)

Aggregations

EventListener (org.opennms.netmgt.events.api.EventListener)10 Event (org.opennms.netmgt.xml.event.Event)9 ThreadAwareEventListener (org.opennms.netmgt.events.api.ThreadAwareEventListener)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 CamelBlueprintTest (org.opennms.core.test.camel.CamelBlueprintTest)3 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)3 DataOutputStream (java.io.DataOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Socket (java.net.Socket)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CompletableFuture (java.util.concurrent.CompletableFuture)1