Search in sources :

Example 1 with AlarmDao

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());
}
Also used : OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) ArrayList(java.util.ArrayList) MockTransactionTemplate(org.opennms.netmgt.dao.mock.MockTransactionTemplate) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) Test(org.junit.Test)

Example 2 with AlarmDao

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);
}
Also used : OnmsEvent(org.opennms.netmgt.model.OnmsEvent) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) OnmsEventParameter(org.opennms.netmgt.model.OnmsEventParameter) OnmsDistPoller(org.opennms.netmgt.model.OnmsDistPoller) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) Date(java.util.Date) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with AlarmDao

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));
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) Test(org.junit.Test)

Example 4 with AlarmDao

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;
}
Also used : TransactionOperations(org.springframework.transaction.support.TransactionOperations) HashMap(java.util.HashMap) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IfTttConfig(org.opennms.features.ifttt.config.IfTttConfig) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DefaultVariableNameExpansion(org.opennms.features.ifttt.helper.DefaultVariableNameExpansion) VariableNameExpansion(org.opennms.features.ifttt.helper.VariableNameExpansion)

Example 5 with AlarmDao

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;
}
Also used : OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) ArrayList(java.util.ArrayList) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) BeanFactoryReference(org.springframework.beans.factory.access.BeanFactoryReference) Event(org.opennms.netmgt.xml.event.Event) HashMap(java.util.HashMap) Map(java.util.Map) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

AlarmDao (org.opennms.netmgt.dao.api.AlarmDao)6 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)5 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)2 OnmsDistPoller (org.opennms.netmgt.model.OnmsDistPoller)2 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)2 OnmsEventParameter (org.opennms.netmgt.model.OnmsEventParameter)2 Transactional (org.springframework.transaction.annotation.Transactional)2 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)2 File (java.io.File)1 List (java.util.List)1 Map (java.util.Map)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 IfTttConfig (org.opennms.features.ifttt.config.IfTttConfig)1 DefaultVariableNameExpansion (org.opennms.features.ifttt.helper.DefaultVariableNameExpansion)1 VariableNameExpansion (org.opennms.features.ifttt.helper.VariableNameExpansion)1 MockTransactionTemplate (org.opennms.netmgt.dao.mock.MockTransactionTemplate)1