Search in sources :

Example 31 with MockNode

use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.

the class AlarmdIT method testPersistManyAlarmsAtOnce.

@Test
public void testPersistManyAlarmsAtOnce() throws InterruptedException {
    int numberOfAlarmsToReduce = 10;
    // there should be no alarms in the alarms table
    assertEmptyAlarmTable();
    final String reductionKey = "countThese";
    final MockNode node = m_mockNetwork.getNode(1);
    final long millis = System.currentTimeMillis() + 2500;
    final CountDownLatch signal = new CountDownLatch(numberOfAlarmsToReduce);
    for (int i = 1; i <= numberOfAlarmsToReduce; i++) {
        MockUtil.println("Creating Runnable: " + i + " of " + numberOfAlarmsToReduce + " events to reduce.");
        class EventRunner implements Runnable {

            @Override
            public void run() {
                try {
                    while (System.currentTimeMillis() < millis) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            MockUtil.println(e.getMessage());
                        }
                    }
                    sendNodeDownEvent(reductionKey, node);
                } catch (Throwable t) {
                    t.printStackTrace();
                } finally {
                    signal.countDown();
                }
            }
        }
        Runnable r = new EventRunner();
        Thread p = new Thread(r);
        p.start();
    }
    signal.await();
    // this should be the first occurrence of this alarm
    // there should be 1 alarm now
    int rowCount = m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue();
    Integer counterColumn = m_jdbcTemplate.queryForObject("select counter from alarms where reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
    MockUtil.println("rowcCount is: " + rowCount + ", expected 1.");
    MockUtil.println("counterColumn is: " + counterColumn + ", expected " + numberOfAlarmsToReduce);
    assertEquals(1, rowCount);
    if (numberOfAlarmsToReduce != counterColumn) {
        final List<Integer> reducedEvents = new ArrayList<>();
        m_jdbcTemplate.query("select eventid from events where alarmID is not null", new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                reducedEvents.add(rs.getInt(1));
            }
        });
        Collections.sort(reducedEvents);
        final List<Integer> nonReducedEvents = new ArrayList<>();
        m_jdbcTemplate.query("select eventid from events where alarmID is null", new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                nonReducedEvents.add(rs.getInt(1));
            }
        });
        Collections.sort(nonReducedEvents);
        fail("number of alarms to reduce (" + numberOfAlarmsToReduce + ") were not reduced into a single alarm (instead the counter column reads " + counterColumn + "); " + "events that were reduced: " + StringUtils.collectionToCommaDelimitedString(reducedEvents) + "; events that were not reduced: " + StringUtils.collectionToCommaDelimitedString(nonReducedEvents));
    }
    Integer alarmId = m_jdbcTemplate.queryForObject("select alarmId from alarms where reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
    rowCount = m_jdbcTemplate.queryForObject("select count(*) from events where alarmid = ?", new Object[] { alarmId }, Integer.class).intValue();
    MockUtil.println(String.valueOf(rowCount) + " of events with alarmid: " + alarmId);
    // assertEquals(numberOfAlarmsToReduce, rowCount);
    rowCount = m_jdbcTemplate.queryForObject("select count(*) from events where alarmid is null", Integer.class).intValue();
    MockUtil.println(String.valueOf(rowCount) + " of events with null alarmid");
    assertEquals(10, rowCount);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) MockNode(org.opennms.netmgt.mock.MockNode) CountDownLatch(java.util.concurrent.CountDownLatch) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) Test(org.junit.Test)

Example 32 with MockNode

use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.

the class PollablesIT method addDownServiceToDownNode.

private void addDownServiceToDownNode(int nodeId, String nodeLabel, String nodeLocation, String ipAddr, String existingSvcName, String newSvcName) {
    MockNode mNode = m_mockNetwork.getNode(nodeId);
    PollableService pExistingSvc = m_network.getService(nodeId, getInetAddress(ipAddr), existingSvcName);
    PollableInterface pIface = pExistingSvc.getInterface();
    PollableNode pNode = pExistingSvc.getNode();
    // before we start make sure the node is down
    anticipateDown(mNode);
    mNode.bringDown();
    pExistingSvc.doPoll();
    m_network.processStatusChange(new Date());
    verifyAnticipated();
    // ok the node is down.. here is the cause
    PollEvent nodeCause = pNode.getCause();
    // add a new mock service
    MockService mSvc = m_mockNetwork.addService(nodeId, ipAddr, newSvcName);
    m_db.writeService(mSvc);
    // that is down
    mSvc.bringDown();
    // expect nothing since we already have node down event outstanding
    // simulate a nodeGainedService event
    PollableService pSvc = addServiceToNetwork(nodeId, nodeLabel, nodeLocation, ipAddr, newSvcName);
    assertNotNull(pSvc);
    // before the first poll everthing should have the node down cause
    assertElementHasCause(pSvc, nodeCause);
    assertElementHasCause(pExistingSvc, nodeCause);
    assertElementHasCause(pIface, nodeCause);
    assertElementHasCause(pNode, nodeCause);
    // and should be mored down
    assertDown(pSvc);
    assertDown(pExistingSvc);
    assertDown(pIface);
    assertDown(pNode);
    // now to the first poll
    pSvc.doPoll();
    // everything should still be down
    assertDown(pSvc);
    assertDown(pExistingSvc);
    assertDown(pIface);
    assertDown(pNode);
    m_network.processStatusChange(new Date());
    // and should have the same node down cause
    assertElementHasCause(pSvc, nodeCause);
    assertElementHasCause(pExistingSvc, nodeCause);
    assertElementHasCause(pIface, nodeCause);
    assertElementHasCause(pNode, nodeCause);
    // verify we've received no events
    verifyAnticipated();
}
Also used : MockService(org.opennms.netmgt.mock.MockService) MockNode(org.opennms.netmgt.mock.MockNode) Date(java.util.Date)

Example 33 with MockNode

use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.

the class NotifdIT method testMockNotificationBasic.

@Test
public void testMockNotificationBasic() throws Exception {
    MockNode node = m_network.getNode(1);
    Date downDate = new Date();
    long finishedDowns = anticipateNotificationsForGroup("node 1 down.", "All services are down on node 1.", "InitialGroup", downDate, 0);
    // bring node down now
    m_eventMgr.sendEventToListeners(node.createDownEvent(downDate));
    verifyAnticipated(finishedDowns, 3000);
    m_anticipator.reset();
    Date upDate = new Date();
    anticipateNotificationsForGroup("RESOLVED: node 1 down.", "RESOLVED: All services are down on node 1.", "InitialGroup", upDate, 0);
    long finishedUps = anticipateNotificationsForGroup("node 1 up.", "The node which was previously down is now up.", "UpGroup", upDate, 0);
    // bring node back up now
    m_eventMgr.sendEventToListeners(node.createUpEvent(upDate));
    verifyAnticipated(finishedUps, 3000);
}
Also used : MockNode(org.opennms.netmgt.mock.MockNode) Date(java.util.Date) Test(org.junit.Test)

Example 34 with MockNode

use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.

the class PollerIT method testNoSpuriousNodeDownsOnNodeCategoryMembershipChanged.

/**
 * Test for NMS-7761
 */
@Test
public void testNoSpuriousNodeDownsOnNodeCategoryMembershipChanged() {
    m_pollerConfig.setNodeOutageProcessingEnabled(true);
    MockNode node = m_network.getNode(1);
    // Start the poller
    startDaemons();
    resetAnticipated();
    anticipateDown(node);
    // Bring down the node (duh)
    node.bringDown();
    // Make sure the correct events are received
    verifyAnticipated(10000);
    // Send a uei.opennms.org/nodes/nodeCategoryMembershipChanged
    EventBuilder eventBuilder = MockEventUtil.createEventBuilder("Test", EventConstants.NODE_CATEGORY_MEMBERSHIP_CHANGED_EVENT_UEI);
    eventBuilder.setNodeid(node.getNodeId());
    Event nodeCatMemChangedEvent = eventBuilder.getEvent();
    m_eventMgr.sendEventToListeners(nodeCatMemChangedEvent);
    // We shouldn't receive any other events
    verifyAnticipated(2000, true);
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Event(org.opennms.netmgt.xml.event.Event) MockNode(org.opennms.netmgt.mock.MockNode) Test(org.junit.Test)

Example 35 with MockNode

use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.

the class PollerIT method sendNodeGainedServices.

private void sendNodeGainedServices(int nodeid, String nodeLabel, String ipAddr, String... svcNames) {
    assertNotNull(svcNames);
    assertTrue(svcNames.length > 0);
    MockNode node = m_network.addNode(nodeid, nodeLabel);
    m_db.writeNode(node);
    MockInterface iface = m_network.addInterface(nodeid, ipAddr);
    m_db.writeInterface(iface);
    List<MockService> services = new ArrayList<>();
    for (String svcName : svcNames) {
        MockService svc = m_network.addService(nodeid, ipAddr, svcName);
        m_db.writeService(svc);
        m_pollerConfig.addService(svc);
        services.add(svc);
    }
    MockVisitor gainSvcSender = new MockVisitorAdapter() {

        @Override
        public void visitService(MockService svc) {
            Event event = MockEventUtil.createNodeGainedServiceEvent("Test", svc);
            m_eventMgr.sendEventToListeners(event);
        }
    };
    node.visit(gainSvcSender);
    MockService svc1 = services.get(0);
    PollAnticipator anticipator = new PollAnticipator();
    svc1.addAnticipator(anticipator);
    anticipator.anticipateAllServices(svc1);
    final StringBuilder didNotOccur = new StringBuilder();
    for (MockService service : anticipator.waitForAnticipated(10000)) {
        didNotOccur.append(service.toString());
    }
    final StringBuilder unanticipatedStuff = new StringBuilder();
    for (MockService service : anticipator.unanticipatedPolls()) {
        unanticipatedStuff.append(service.toString());
    }
    assertEquals(unanticipatedStuff.toString(), "", didNotOccur.toString());
    anticipateDown(svc1);
    svc1.bringDown();
    verifyAnticipated(10000);
}
Also used : PollAnticipator(org.opennms.netmgt.mock.PollAnticipator) MockVisitorAdapter(org.opennms.netmgt.mock.MockVisitorAdapter) MockInterface(org.opennms.netmgt.mock.MockInterface) MockVisitor(org.opennms.netmgt.mock.MockVisitor) ArrayList(java.util.ArrayList) MockService(org.opennms.netmgt.mock.MockService) Event(org.opennms.netmgt.xml.event.Event) MockNode(org.opennms.netmgt.mock.MockNode)

Aggregations

MockNode (org.opennms.netmgt.mock.MockNode)53 Test (org.junit.Test)43 Event (org.opennms.netmgt.xml.event.Event)21 MockService (org.opennms.netmgt.mock.MockService)16 MockInterface (org.opennms.netmgt.mock.MockInterface)7 Date (java.util.Date)5 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)5 ResultSet (java.sql.ResultSet)4 PollAnticipator (org.opennms.netmgt.mock.PollAnticipator)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 MockNetwork (org.opennms.netmgt.mock.MockNetwork)3 MockVisitor (org.opennms.netmgt.mock.MockVisitor)3 MockVisitorAdapter (org.opennms.netmgt.mock.MockVisitorAdapter)3 Ignore (org.junit.Ignore)2 Querier (org.opennms.core.utils.Querier)2 RowCallbackHandler (org.springframework.jdbc.core.RowCallbackHandler)2 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Before (org.junit.Before)1