use of org.opennms.netmgt.dao.api.AlarmDao in project opennms by OpenNMS.
the class OnmsDaoContainerTest method testGetItemIdsWithOnlyOneItem.
@Test
public void testGetItemIdsWithOnlyOneItem() {
MockTransactionTemplate transactionTemplate = new MockTransactionTemplate();
transactionTemplate.afterPropertiesSet();
List<OnmsAlarm> alarmList = new ArrayList<>();
OnmsAlarm alarm = new OnmsAlarm();
alarm.setId(102);
alarmList.add(alarm);
final AlarmDao alarmDaoMock = EasyMock.createNiceMock(AlarmDao.class);
EasyMock.expect(alarmDaoMock.countMatching((Criteria) EasyMock.anyObject())).andReturn(1);
EasyMock.expect(alarmDaoMock.findMatching((Criteria) EasyMock.anyObject())).andReturn(alarmList);
EasyMock.replay(alarmDaoMock);
AlarmDaoContainer container = new AlarmDaoContainer(alarmDaoMock, transactionTemplate);
List<Integer> items = container.getItemIds(0, 1);
Assert.assertNotNull(items);
Assert.assertEquals(1, items.size());
}
use of org.opennms.netmgt.dao.api.AlarmDao in project opennms by OpenNMS.
the class AlarmRepositoryFilterIT method testParmsLikeFilter.
@Test
@Transactional
@JUnitTemporaryDatabase
public void testParmsLikeFilter() {
OnmsDistPoller poller = m_dbPopulator.getDistPollerDao().whoami();
assertNotNull(poller);
final OnmsEvent event1 = new OnmsEvent();
event1.setDistPoller(poller);
event1.setEventCreateTime(new Date(1436881548292L));
event1.setEventTime(new Date(1436881548292L));
event1.setEventCreateTime(new Date(1437061537126L));
event1.setEventDescr("This is the description of a test event.");
event1.setEventDisplay("Y");
event1.setEventHost("127.0.0.1");
event1.setEventLog("Y");
event1.setEventLogMsg("Test Event Log Message");
event1.setEventSeverity(1);
event1.setEventSource("test");
event1.setEventTime(new Date(1437061537105L));
event1.setEventUei("uei.opennms.org/test");
event1.setIpAddr(InetAddressUtils.getInetAddress("192.168.1.1"));
event1.setNode(m_dbPopulator.getNode1());
event1.setServiceType(m_dbPopulator.getServiceTypeDao().findByName("ICMP"));
event1.setEventParameters(Lists.newArrayList(new OnmsEventParameter(event1, "url", "http://localhost:8980/opennms/rtc/post/Network+Interfaces", "string"), new OnmsEventParameter(event1, "user", "rtc", "string"), new OnmsEventParameter(event1, "passwd", "rtc", "string"), new OnmsEventParameter(event1, "catlabel", "Network Interfaces", "string")));
m_dbPopulator.getEventDao().saveOrUpdate(event1);
m_dbPopulator.getEventDao().flush();
final OnmsEvent event2 = new OnmsEvent();
event2.setDistPoller(poller);
event2.setEventCreateTime(new Date(1436881548292L));
event2.setEventTime(new Date(1436881548292L));
event2.setEventCreateTime(new Date(1437061537126L));
event2.setEventDescr("This is the description of a test event.");
event2.setEventDisplay("Y");
event2.setEventHost("127.0.0.1");
event2.setEventLog("Y");
event2.setEventLogMsg("Test Event Log Message");
event2.setEventSeverity(1);
event2.setEventSource("test");
event2.setEventTime(new Date(1437061537105L));
event2.setEventUei("uei.opennms.org/test");
event2.setIpAddr(InetAddressUtils.getInetAddress("192.168.1.1"));
event2.setNode(m_dbPopulator.getNode1());
event2.setServiceType(m_dbPopulator.getServiceTypeDao().findByName("ICMP"));
event2.setEventParameters(Lists.newArrayList(new OnmsEventParameter(event2, "componentType", "serviceElement", "string"), new OnmsEventParameter(event2, "url", "http://localhost:8980/opennms/rtc/post/Network+Interfaces", "string"), new OnmsEventParameter(event2, "user", "rtcbomb", "string"), new OnmsEventParameter(event2, "passwd", "rtc", "string"), new OnmsEventParameter(event2, "catlabel", "Network Interfaces", "string")));
m_dbPopulator.getEventDao().saveOrUpdate(event2);
m_dbPopulator.getEventDao().flush();
OnmsAlarm alarm = new OnmsAlarm();
alarm.setUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp");
alarm.setLastEvent(event1);
alarm.setSeverityId(3);
alarm.setDistPoller(poller);
alarm.setCounter(100);
alarm.setLastEvent(event1);
AlarmDao alarmDao = m_dbPopulator.getAlarmDao();
alarmDao.save(alarm);
alarmDao.flush();
OnmsAlarm alarm2 = new OnmsAlarm();
alarm2.setUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp");
alarm2.setLastEvent(event2);
alarm2.setSeverityId(3);
alarm2.setDistPoller(poller);
alarm2.setCounter(100);
alarm2.setLastEvent(event2);
alarmDao.save(alarm2);
alarmDao.flush();
EventParmLikeFilter eventParmFilter = new EventParmLikeFilter("user=rtc");
assertEquals("user=\"rtc\"", eventParmFilter.getTextDescription());
AlarmCriteria criteria = new AlarmCriteria(eventParmFilter);
OnmsAlarm[] alarms = m_daoAlarmRepo.getMatchingAlarms(AlarmUtil.getOnmsCriteria(criteria));
assertEquals(1, alarms.length);
}
use of org.opennms.netmgt.dao.api.AlarmDao in project opennms by OpenNMS.
the class BsmdIT method verifyNoDaoLookupsWhenNoRulesAreDefined.
@Test
public void verifyNoDaoLookupsWhenNoRulesAreDefined() throws Exception {
// Mock the DAO
AlarmDao alarmDao = mock(AlarmDao.class);
m_bsmd.setAlarmDao(alarmDao);
// Start up without any business services
m_bsmd.start();
assertThat("Alarm polling should be disabled where there are no services.", m_bsmd.isAlarmPolling(), equalTo(false));
// Create the alarm
OnmsAlarm alarm = createAlarm();
m_alarmDao.save(alarm);
// Send alarm created event
EventBuilder ebldr = new EventBuilder(EventConstants.ALARM_CREATED_UEI, "test");
ebldr.addParam(EventConstants.PARM_ALARM_ID, alarm.getId());
m_bsmd.handleAlarmLifecycleEvents(ebldr.getEvent());
// The AlarmDAO should not have any lookups
verify(alarmDao, never()).get(anyObject());
// Now let's add a rule
createBusinessService("svc");
// Reload the daemon
reloadBsmd();
assertThat("Alarm polling should be enabled when there are 1+ services.", m_bsmd.isAlarmPolling(), equalTo(true));
// Send alarm updated event
ebldr = new EventBuilder(EventConstants.ALARM_UPDATED_WITH_REDUCED_EVENT_UEI, "test");
ebldr.addParam(EventConstants.PARM_ALARM_ID, alarm.getId());
m_bsmd.handleAlarmLifecycleEvents(ebldr.getEvent());
// The AlarmDAO should have a single lookup
verify(alarmDao, times(1)).get(anyObject());
// Clear all of the BSs and reload
deleteAllBusinessServices();
reloadBsmd();
assertThat("Alarm polling should be disabled where there are no services.", m_bsmd.isAlarmPolling(), equalTo(false));
}
use of org.opennms.netmgt.dao.api.AlarmDao in project opennms by OpenNMS.
the class IfTttDaemonTest method runIfTttDaemonTest.
public Map<String, List<ResultEntry>> runIfTttDaemonTest(int timeout, int entryCount) throws Exception {
final AlarmDao alarmDao = mock(AlarmDao.class);
when(alarmDao.findMatching((Criteria) Matchers.anyObject())).thenReturn(alarmMap.values().stream().collect(Collectors.toList()));
final TransactionOperations transactionOperations = mock(TransactionOperations.class);
when(transactionOperations.execute(Matchers.anyObject())).thenAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
TransactionCallbackWithoutResult transactionCallbackWithoutResult = invocationOnMock.getArgumentAt(0, TransactionCallbackWithoutResult.class);
transactionCallbackWithoutResult.doInTransaction(null);
return null;
}
});
// final List<ResultEntry> receivedEntries = new ArrayList<>();
final Map<String, List<ResultEntry>> receivedEntries = new HashMap<>();
final IfTttDaemon ifTttDaemon = new IfTttDaemon(alarmDao, transactionOperations, new File("src/test/resources/etc/ifttt-config.xml")) {
@Override
protected void fireIfTttTriggerSet(IfTttConfig ifTttConfig, String categoryFilter, String name, VariableNameExpansion variableNameExpansion) {
if (!receivedEntries.containsKey(categoryFilter)) {
receivedEntries.put(categoryFilter, new ArrayList<>());
}
receivedEntries.get(categoryFilter).add(new ResultEntry(name, variableNameExpansion));
}
};
ifTttDaemon.start();
await().atMost(timeout, SECONDS).until(() -> allEntrySizesMatch(receivedEntries, entryCount - 2));
LOG.debug("#1: {}", receivedEntries);
addAlarm(12, 4, OnmsSeverity.MAJOR);
when(alarmDao.findMatching((Criteria) Matchers.anyObject())).thenReturn(alarmMap.values().stream().collect(Collectors.toList()));
await().atMost(timeout, SECONDS).until(() -> allEntrySizesMatch(receivedEntries, entryCount - 1));
LOG.debug("#2: {}", receivedEntries);
ifTttDaemon.stop();
await().atMost(timeout, SECONDS).until(() -> allEntrySizesMatch(receivedEntries, entryCount));
LOG.debug("#3: {}", receivedEntries);
return receivedEntries;
}
use of org.opennms.netmgt.dao.api.AlarmDao in project opennms by OpenNMS.
the class AlarmEventSynchronization method getEvents.
@Override
public List<Event> getEvents() {
BeanFactoryReference bf = BeanUtils.getBeanFactory("daoContext");
final AlarmDao alarmDao = BeanUtils.getBean(bf, "alarmDao", AlarmDao.class);
final List<Event> xmlevents = new ArrayList<>();
TransactionTemplate transTemplate = BeanUtils.getBean(bf, "transactionTemplate", TransactionTemplate.class);
try {
transTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
Map<String, OnmsAlarm> forwardAlarms = new HashMap<String, OnmsAlarm>();
for (OnmsAlarm alarm : alarmDao.findAll()) {
// Got Clear alarm
if (alarm.getAlarmType() == 2) {
if (forwardAlarms.containsKey(alarm.getClearKey())) {
OnmsAlarm raise = forwardAlarms.get(alarm.getClearKey());
if (raise.getLastEventTime().before(alarm.getLastEventTime())) {
forwardAlarms.remove(alarm.getClearKey());
}
} else {
forwardAlarms.put(alarm.getClearKey(), alarm);
}
} else if (alarm.getAlarmType() == 1) {
if (forwardAlarms.containsKey(alarm.getReductionKey())) {
OnmsAlarm clear = forwardAlarms.get(alarm.getReductionKey());
if (clear.getLastEventTime().before(alarm.getLastEventTime())) {
forwardAlarms.put(alarm.getReductionKey(), alarm);
}
} else {
forwardAlarms.put(alarm.getReductionKey(), alarm);
}
} else {
forwardAlarms.put(alarm.getReductionKey(), alarm);
}
}
for (OnmsAlarm alarm : forwardAlarms.values()) {
if (alarm.getAlarmType() != 2) {
Event xmlEvent = getXMLEvent(alarm);
if (xmlEvent != null)
xmlevents.add(xmlEvent);
}
}
}
});
} catch (final RuntimeException e) {
}
return xmlevents;
}
Aggregations