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()));
}
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();
}
};
}
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;
}
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}"));
}
}
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();
}
}
Aggregations