Search in sources :

Example 11 with MockNetwork

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

the class PollerConfigWithPSMIT method setUp.

@Before
public void setUp() throws Exception {
    MockLogAppender.setupLogging();
    Resource dbConfig = new ClassPathResource("/org/opennms/netmgt/config/test-database-schema.xml");
    InputStream s = dbConfig.getInputStream();
    DatabaseSchemaConfigFactory dscf = new DatabaseSchemaConfigFactory(s);
    s.close();
    DatabaseSchemaConfigFactory.setInstance(dscf);
    MockNetwork network = new MockNetwork();
    network.setCriticalService("ICMP");
    network.addNode(1, "Router");
    network.addInterface("192.168.1.1");
    network.addService("ICMP");
    network.addService("SMTP");
    network.addInterface("192.168.1.2");
    network.addService("ICMP");
    network.addService("SMTP");
    network.addNode(2, "Server");
    network.addInterface("192.168.1.3");
    network.addService("ICMP");
    network.addService("HTTP");
    network.addNode(3, "Firewall");
    network.addInterface("192.168.1.4");
    network.addService("SMTP");
    network.addService("HTTP");
    network.addInterface("192.168.1.5");
    network.addService("SMTP");
    network.addService("HTTP");
    network.addInterface("192.169.1.5");
    network.addService("SMTP");
    network.addService("HTTP");
    network.addNode(4, "TestNode121");
    network.addInterface("123.12.123.121");
    network.addService("HTTP");
    network.addNode(5, "TestNode122");
    network.addInterface("123.12.123.122");
    network.addService("HTTP");
    MockDatabase db = new MockDatabase();
    db.populate(network);
    DataSourceFactory.setInstance(db);
}
Also used : MockNetwork(org.opennms.netmgt.mock.MockNetwork) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockDatabase(org.opennms.core.test.db.MockDatabase) ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before)

Example 12 with MockNetwork

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

the class PollerIT method setUp.

//
// SetUp and TearDown
//
@Before
public void setUp() throws Exception {
    // System.setProperty("mock.logLevel", "DEBUG");
    // System.setProperty("mock.debug", "true");
    MockUtil.println("------------ Begin Test  --------------------------");
    MockLogAppender.setupLogging();
    m_network = new MockNetwork();
    m_network.setCriticalService("ICMP");
    m_network.addNode(1, "Router");
    m_network.addInterface("192.168.1.1");
    m_network.addService("ICMP");
    m_network.addService("SMTP");
    m_network.addService("SNMP");
    m_network.addInterface("192.168.1.2");
    m_network.addService("ICMP");
    m_network.addService("SMTP");
    m_network.addNode(2, "Server");
    m_network.addInterface("192.168.1.3");
    m_network.addService("ICMP");
    m_network.addService("HTTP");
    m_network.addService("SMTP");
    m_network.addService("SNMP");
    m_network.addNode(3, "Firewall");
    m_network.addInterface("192.168.1.4");
    m_network.addService("SMTP");
    m_network.addService("HTTP");
    m_network.addInterface("192.168.1.5");
    m_network.addService("SMTP");
    m_network.addService("HTTP");
    m_network.addNode(4, "DownNode");
    m_network.addInterface("192.168.1.6");
    m_network.addService("SNMP");
    m_network.addNode(5, "Loner");
    m_network.addInterface("192.168.1.7");
    m_network.addService("ICMP");
    m_network.addService("SNMP");
    MockService unmonitoredService = m_network.addService("NotMonitored");
    m_db.populate(m_network);
    DataSourceFactory.setInstance(m_db);
    m_pollerConfig = new MockPollerConfig(m_network);
    m_pollerConfig.setNextOutageIdSql(m_db.getNextOutageIdStatement());
    m_pollerConfig.setNodeOutageProcessingEnabled(true);
    m_pollerConfig.setCriticalService("ICMP");
    m_pollerConfig.addPackage("TestPackage");
    m_pollerConfig.addDowntime(1000L, 0L, -1L, false);
    m_pollerConfig.setDefaultPollInterval(1000L);
    m_pollerConfig.populatePackage(m_network, unmonitoredService);
    m_pollerConfig.addPackage("TestPkg2");
    m_pollerConfig.addDowntime(1000L, 0L, -1L, false);
    m_pollerConfig.setDefaultPollInterval(2000L);
    m_pollerConfig.addService(m_network.getService(2, "192.168.1.3", "HTTP"));
    m_outageAnticipator = new OutageAnticipator(m_db);
    m_eventMgr = new MockEventIpcManager();
    m_eventMgr.setEventWriter(m_db);
    m_eventMgr.setEventAnticipator(m_eventMgr.getEventAnticipator());
    m_eventMgr.addEventListener(m_outageAnticipator);
    m_eventMgr.setSynchronous(false);
    m_eventMgr.setNumSchedulerThreads(2);
    m_locationAwarePingClient = mock(LocationAwarePingClient.class);
    DefaultPollContext pollContext = new DefaultPollContext();
    pollContext.setEventManager(m_eventMgr);
    pollContext.setLocalHostName("localhost");
    pollContext.setName("Test.DefaultPollContext");
    pollContext.setPollerConfig(m_pollerConfig);
    pollContext.setQueryManager(m_queryManager);
    pollContext.setLocationAwarePingClient(m_locationAwarePingClient);
    PollableNetwork network = new PollableNetwork(pollContext);
    m_poller = new Poller();
    m_poller.setMonitoredServiceDao(m_monitoredServiceDao);
    m_poller.setOutageDao(m_outageDao);
    m_poller.setTransactionTemplate(m_transactionTemplate);
    m_poller.setEventIpcManager(m_eventMgr);
    m_poller.setNetwork(network);
    m_poller.setQueryManager(m_queryManager);
    m_poller.setPollerConfig(m_pollerConfig);
    m_poller.setPollOutagesConfig(m_pollerConfig);
    m_poller.setLocationAwarePollerClient(m_locationAwarePollerClient);
}
Also used : LocationAwarePingClient(org.opennms.netmgt.icmp.proxy.LocationAwarePingClient) MockNetwork(org.opennms.netmgt.mock.MockNetwork) MockEventIpcManager(org.opennms.netmgt.dao.mock.MockEventIpcManager) MockService(org.opennms.netmgt.mock.MockService) OutageAnticipator(org.opennms.netmgt.mock.OutageAnticipator) MockPollerConfig(org.opennms.netmgt.mock.MockPollerConfig) PollableNetwork(org.opennms.netmgt.poller.pollables.PollableNetwork) Before(org.junit.Before)

Example 13 with MockNetwork

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

the class ThresholdingVisitorIT method testReloadThreshdConfig.

/*
     * Use case A:
     * 
     * I have 5 nodes. The current threshd-config matches 2 of them. The new threshd-config will match the other 2, by
     * adding a new threshold package. For example: n1 y n2 belongs to category CAT1, n2, n3 y n4 belongs to category CAT2.
     * The initial configuration is related with CAT1 and the new package is related with CAT2. In both cases, n5 should
     * never match any threshold package.
     * 
     * Use case B:
     * 
     * I have a package with SNMP thresholds. Then update the package by adding HTTP thresholds. The test node should
     * support both services.
     * 
     * IMPORTANT:
     *     The reload should be do it first, then notify all visitors (I think this is the current behavior)
     *     The reload should not be executed inside the visitor because every collector thread has their own visitor.
     */
@Test
public void testReloadThreshdConfig() throws Exception {
    String baseIpAddress = "10.0.0.";
    // Initialize Mock Network
    MockNetwork network = new MockNetwork();
    network.setCriticalService("ICMP");
    for (int i = 1; i <= 5; i++) {
        String ipAddress = baseIpAddress + i;
        network.addNode(i, "testNode-" + ipAddress);
        network.addInterface(ipAddress);
        network.setIfAlias("eth0");
        network.addService("ICMP");
        network.addService("SNMP");
        if (i == 5) {
            // Adding HTTP on node 5
            network.addService("HTTP");
        }
    }
    network.addPathOutage(1, InetAddressUtils.addr("192.168.1.1"), "ICMP");
    MockDatabase db = new MockDatabase();
    db.populate(network);
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 10, "CAT1");
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 11, "CAT2");
    for (int i = 1; i <= 5; i++) {
        db.update("update snmpinterface set snmpifname=?, snmpifdescr=? where id=?", "eth0", "eth0", i);
        db.update("update node set nodesysoid=? where nodeid=?", ".1.3.6.1.4.1.9.1.222", i);
    }
    for (int i = 1; i <= 2; i++) {
        db.update("insert into category_node values (?, ?)", 10, i);
    }
    for (int i = 3; i <= 5; i++) {
        db.update("insert into category_node values (?, ?)", 11, i);
    }
    DataSourceFactory.setInstance(db);
    // Initialize Filter DAO
    System.setProperty("opennms.home", "src/test/resources");
    DatabaseSchemaConfigFactory.init();
    JdbcFilterDao jdbcFilterDao = new JdbcFilterDao();
    jdbcFilterDao.setDataSource(db);
    jdbcFilterDao.setDatabaseSchemaConfigFactory(DatabaseSchemaConfigFactory.getInstance());
    jdbcFilterDao.afterPropertiesSet();
    FilterDaoFactory.setInstance(jdbcFilterDao);
    // Initialize Factories
    initFactories("/threshd-configuration-reload-use-case-a.xml", "/test-thresholds-reload-use-cases.xml");
    // Initialize Thresholding Visitors
    System.err.println("-----------------------------------------------------------------------------------");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("thresholding-enabled", "true");
    ServiceParameters svcParams = new ServiceParameters(params);
    List<ThresholdingVisitor> visitors = new ArrayList<ThresholdingVisitor>();
    for (int i = 1; i <= 5; i++) {
        String ipAddress = baseIpAddress + i;
        ThresholdingVisitor visitor = ThresholdingVisitor.create(i, ipAddress, "SNMP", getRepository(), svcParams, m_resourceStorageDao);
        assertNotNull(visitor);
        visitors.add(visitor);
        if (i == 5) {
            ThresholdingVisitor httpVisitor = ThresholdingVisitor.create(i, ipAddress, "HTTP", getRepository(), svcParams, m_resourceStorageDao);
            assertNotNull(httpVisitor);
            visitors.add(httpVisitor);
        }
    }
    System.err.println("-----------------------------------------------------------------------------------");
    // Check Visitors
    for (int i = 0; i < 2; i++) {
        // Nodes n1 and n2 has thresholds defined on one threshold group.
        assertTrue(visitors.get(i).hasThresholds());
        assertEquals(1, visitors.get(i).getThresholdGroups().size());
    }
    for (int i = 2; i < 6; i++) {
        // Nodes n3, n4 and n5 should not have thresholds defined.
        assertFalse(visitors.get(i).hasThresholds());
        assertEquals(0, visitors.get(i).getThresholdGroups().size());
    }
    // Re-Initialize Factories
    initFactories("/threshd-configuration-reload-use-case-b.xml", "/test-thresholds-reload-use-cases.xml");
    // Reload state on each visitor
    System.err.println("-----------------------------------------------------------------------------------");
    for (ThresholdingVisitor visitor : visitors) {
        visitor.reload();
    }
    System.err.println("-----------------------------------------------------------------------------------");
    // Check Visitors
    for (int i = 0; i < 6; i++) {
        assertTrue(visitors.get(i).hasThresholds());
        assertEquals(1, visitors.get(i).getThresholdGroups().size());
        if (i == 5) {
            assertEquals("web-services", visitors.get(i).getThresholdGroups().get(0).getName());
        }
    }
}
Also used : MockNetwork(org.opennms.netmgt.mock.MockNetwork) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MockDatabase(org.opennms.core.test.db.MockDatabase) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) JdbcFilterDao(org.opennms.netmgt.filter.JdbcFilterDao) Test(org.junit.Test)

Example 14 with MockNetwork

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

the class ProvisionerIT method testSendEventsOnImport.

@Test(timeout = 300000)
public void testSendEventsOnImport() throws Exception {
    final int nextNodeId = m_nodeDao.getNextNodeId();
    final String nodeLabel = "node1";
    final MockNetwork network = new MockNetwork();
    final MockNode node = network.addNode(nextNodeId, nodeLabel);
    network.addInterface("192.0.2.204");
    network.addService("ICMP");
    network.addService("HTTP");
    network.addInterface("192.0.2.201");
    network.addService("ICMP");
    network.addService("SNMP");
    anticipateCreationEvents(node);
    m_eventAnticipator.anticipateEvent(getNodeCategoryEvent(nextNodeId, nodeLabel));
    for (final Event e : m_eventAnticipator.getAnticipatedEvents()) {
        System.err.println("anticipated: " + e);
    }
    importFromResource("classpath:/tec_dump.xml", Boolean.TRUE.toString());
    for (final Event e : m_eventAnticipator.getAnticipatedEventsReceived()) {
        System.err.println("received anticipated: " + e);
    }
    for (final Event e : m_eventAnticipator.getUnanticipatedEvents()) {
        System.err.println("received unanticipated: " + e);
    }
    m_eventAnticipator.verifyAnticipated();
}
Also used : MockNetwork(org.opennms.netmgt.mock.MockNetwork) Event(org.opennms.netmgt.xml.event.Event) MockNode(org.opennms.netmgt.mock.MockNode) Test(org.junit.Test)

Example 15 with MockNetwork

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

the class AvailabilityServiceIT method canCalculateAvailability.

@Test
public void canCalculateAvailability() throws Exception {
    final MockNetwork mockNetwork = new MockNetwork();
    // This test depends on the specifics in the standard network definition
    mockNetwork.createStandardNetwork();
    m_mockDatabase.populate(mockNetwork);
    final RTCCategory rtcCat = EasyMock.createNiceMock(RTCCategory.class);
    EasyMock.expect(rtcCat.getLabel()).andReturn("NOC").anyTimes();
    EasyMock.expect(rtcCat.getNodes()).andReturn(Lists.newArrayList(1, 2)).anyTimes();
    EasyMock.replay(rtcCat);
    // Verify the availability when no outages are present
    EuiLevel euiLevel = m_availabilityService.getEuiLevel(rtcCat);
    assertEquals(1, euiLevel.getCategory().size());
    Category category = euiLevel.getCategory().get(0);
    assertEquals(100.0, category.getCatvalue(), 0.001);
    assertEquals(2, category.getNode().size());
    // Assumes the nodes are sorted
    assertEquals(4, category.getNode().get(0).getNodesvccount());
    assertEquals(2, category.getNode().get(1).getNodesvccount());
    // Create an outage that is both open and closed within the window
    final Date now = new Date();
    final Date oneHourAgo = new Date(now.getTime() - (60 * 60 * 1000));
    final Date thirtyMinutesAgo = new Date(now.getTime() - (30 * 60 * 1000));
    final OnmsMonitoredService icmpService = toMonitoredService(mockNetwork.getService(1, "192.168.1.1", "ICMP"));
    OnmsOutage outage = new OnmsOutage();
    outage.setMonitoredService(icmpService);
    outage.setIfLostService(oneHourAgo);
    outage.setIfRegainedService(thirtyMinutesAgo);
    m_outageDao.save(outage);
    m_outageDao.flush();
    // Verify the availability when outages are present
    euiLevel = m_availabilityService.getEuiLevel(rtcCat);
    assertEquals(1, euiLevel.getCategory().size());
    category = euiLevel.getCategory().get(0);
    // This number should only need to be adjusted if the duration of the outage
    // or the number of services in the category changes
    assertEquals(RTCUtils.getOutagePercentage(1800000, 86400000, 6), category.getCatvalue(), 0.0001);
    assertEquals(2, category.getNode().size());
}
Also used : OnmsOutage(org.opennms.netmgt.model.OnmsOutage) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) Category(org.opennms.netmgt.xml.rtc.Category) MockNetwork(org.opennms.netmgt.mock.MockNetwork) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) Date(java.util.Date) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) Test(org.junit.Test)

Aggregations

MockNetwork (org.opennms.netmgt.mock.MockNetwork)25 Before (org.junit.Before)11 MockDatabase (org.opennms.core.test.db.MockDatabase)8 Test (org.junit.Test)6 MockPollerConfig (org.opennms.netmgt.mock.MockPollerConfig)6 MockEventIpcManager (org.opennms.netmgt.dao.mock.MockEventIpcManager)4 OutageAnticipator (org.opennms.netmgt.mock.OutageAnticipator)4 PollableNetwork (org.opennms.netmgt.poller.pollables.PollableNetwork)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)3 LocationAwarePingClient (org.opennms.netmgt.icmp.proxy.LocationAwarePingClient)3 MockNode (org.opennms.netmgt.mock.MockNode)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 FileInputStream (java.io.FileInputStream)2 MibObject (org.opennms.netmgt.config.datacollection.MibObject)2 Package (org.opennms.netmgt.config.poller.Package)2 JdbcFilterDao (org.opennms.netmgt.filter.JdbcFilterDao)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1