Search in sources :

Example 1 with FilterDao

use of org.opennms.netmgt.filter.api.FilterDao in project opennms by OpenNMS.

the class LatencyThresholdingSetIT method setUp.

@Before
public void setUp() throws Exception {
    BeanUtils.setStaticApplicationContext(m_context);
    // Resets Counters Cache Data
    CollectionResourceWrapper.s_cache.clear();
    MockLogAppender.setupLogging();
    m_fileAnticipator = new FileAnticipator();
    m_resourceStorageDao = new FilesystemResourceStorageDao();
    m_resourceStorageDao.setRrdDirectory(m_fileAnticipator.getTempDir());
    // Use a mock FilterDao that always returns 127.0.0.1 in the active IP list
    FilterDao filterDao = EasyMock.createMock(FilterDao.class);
    EasyMock.expect(filterDao.getActiveIPAddressList((String) EasyMock.anyObject())).andReturn(Collections.singletonList(addr("127.0.0.1"))).anyTimes();
    filterDao.flushActiveIpAddressListCache();
    EasyMock.expectLastCall().anyTimes();
    FilterDaoFactory.setInstance(filterDao);
    EasyMock.replay(filterDao);
    DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
    StringBuffer sb = new StringBuffer("<?xml version=\"1.0\"?>");
    sb.append("<outages>");
    sb.append("<outage name=\"junit outage\" type=\"specific\">");
    sb.append("<time begins=\"");
    sb.append(formatter.format(new Date(System.currentTimeMillis() - 3600000)));
    sb.append("\" ends=\"");
    sb.append(formatter.format(new Date(System.currentTimeMillis() + 3600000)));
    sb.append("\"/>");
    sb.append("<interface address=\"match-any\"/>");
    sb.append("</outage>");
    sb.append("</outages>");
    File file = new File("target/poll-outages.xml");
    FileWriter writer = new FileWriter(file);
    writer.write(sb.toString());
    writer.close();
    PollOutagesConfigFactory.setInstance(new PollOutagesConfigFactory(new FileSystemResource(file)));
    PollOutagesConfigFactory.getInstance().afterPropertiesSet();
    initFactories("/threshd-configuration.xml", "/test-thresholds.xml");
    m_anticipatedEvents = new ArrayList<Event>();
}
Also used : FileWriter(java.io.FileWriter) PollOutagesConfigFactory(org.opennms.netmgt.config.PollOutagesConfigFactory) FilesystemResourceStorageDao(org.opennms.netmgt.dao.support.FilesystemResourceStorageDao) FileSystemResource(org.springframework.core.io.FileSystemResource) Date(java.util.Date) FilterDao(org.opennms.netmgt.filter.api.FilterDao) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Event(org.opennms.netmgt.xml.event.Event) FileAnticipator(org.opennms.test.FileAnticipator) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Before(org.junit.Before)

Example 2 with FilterDao

use of org.opennms.netmgt.filter.api.FilterDao in project opennms by OpenNMS.

the class CollectdTest method setUp.

@Before
public void setUp() throws Exception {
    EventIpcManager m_eventIpcManager;
    NodeDao m_nodeDao;
    MockLogAppender.setupLogging();
    Resource threshdResource = new ClassPathResource("/etc/thresholds.xml");
    File homeDir = threshdResource.getFile().getParentFile().getParentFile();
    System.setProperty("opennms.home", homeDir.getAbsolutePath());
    // Test setup
    m_eventIpcManager = m_easyMockUtils.createMock(EventIpcManager.class);
    EventIpcManagerFactory.setIpcManager(m_eventIpcManager);
    m_nodeDao = m_easyMockUtils.createMock(NodeDao.class);
    m_ipIfDao = m_easyMockUtils.createMock(IpInterfaceDao.class);
    m_scheduler = new MockScheduler();
    m_eventIpcManager.addEventListener(isA(EventListener.class));
    expectLastCall().anyTimes();
    m_eventIpcManager.addEventListener(isA(EventListener.class), isACollection(String.class));
    expectLastCall().anyTimes();
    m_eventIpcManager.addEventListener(isA(EventListener.class), isA(String.class));
    expectLastCall().anyTimes();
    m_eventIpcManager.removeEventListener(isA(EventListener.class));
    expectLastCall().anyTimes();
    // Mock the FilterDao without using EasyMockUtils so that it can be verified separately
    m_filterDao = EasyMock.createMock(FilterDao.class);
    List<InetAddress> allIps = new ArrayList<InetAddress>();
    allIps.add(addr("192.168.1.1"));
    allIps.add(addr("192.168.1.2"));
    allIps.add(addr("192.168.1.3"));
    allIps.add(addr("192.168.1.4"));
    allIps.add(addr("192.168.1.5"));
    expect(m_filterDao.getActiveIPAddressList("IPADDR IPLIKE *.*.*.*")).andReturn(allIps).anyTimes();
    expect(m_filterDao.getActiveIPAddressList("IPADDR IPLIKE 1.1.1.1")).andReturn(new ArrayList<InetAddress>(0)).anyTimes();
    EasyMock.replay(m_filterDao);
    FilterDaoFactory.setInstance(m_filterDao);
    // This call will also ensure that the poll-outages.xml file can parse IPv4
    // and IPv6 addresses.
    Resource resource = new ClassPathResource("etc/poll-outages.xml");
    PollOutagesConfigFactory factory = new PollOutagesConfigFactory(resource);
    factory.afterPropertiesSet();
    PollOutagesConfigFactory.setInstance(factory);
    final MockTransactionTemplate transTemplate = new MockTransactionTemplate();
    transTemplate.afterPropertiesSet();
    m_collectd = new Collectd();
    m_collectd.setEventIpcManager(m_eventIpcManager);
    m_collectd.setNodeDao(m_nodeDao);
    m_collectd.setIpInterfaceDao(m_ipIfDao);
    m_collectd.setFilterDao(m_filterDao);
    m_collectd.setScheduler(m_scheduler);
    m_collectd.setTransactionTemplate(transTemplate);
    m_collectd.setPersisterFactory(new MockPersisterFactory());
    m_collectd.setServiceCollectorRegistry(new DefaultServiceCollectorRegistry());
    m_collectd.setLocationAwareCollectorClient(CollectorTestUtils.createLocationAwareCollectorClient());
    ThresholdingConfigFactory.setInstance(new ThresholdingConfigFactory(ConfigurationTestUtils.getInputStreamForConfigFile("thresholds.xml")));
}
Also used : MockScheduler(org.opennms.netmgt.poller.mock.MockScheduler) DefaultServiceCollectorRegistry(org.opennms.netmgt.collection.support.DefaultServiceCollectorRegistry) PollOutagesConfigFactory(org.opennms.netmgt.config.PollOutagesConfigFactory) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) ArrayList(java.util.ArrayList) MockTransactionTemplate(org.opennms.netmgt.dao.mock.MockTransactionTemplate) ClassPathResource(org.springframework.core.io.ClassPathResource) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) NodeDao(org.opennms.netmgt.dao.api.NodeDao) FilterDao(org.opennms.netmgt.filter.api.FilterDao) EventIpcManager(org.opennms.netmgt.events.api.EventIpcManager) EventListener(org.opennms.netmgt.events.api.EventListener) MockPersisterFactory(org.opennms.netmgt.mock.MockPersisterFactory) File(java.io.File) InetAddress(java.net.InetAddress) ThresholdingConfigFactory(org.opennms.netmgt.config.ThresholdingConfigFactory) Before(org.junit.Before)

Example 3 with FilterDao

use of org.opennms.netmgt.filter.api.FilterDao in project opennms by OpenNMS.

the class RrdSummaryControllerTest method canGenerateEmptySummary.

/**
     * Verifies that an empty summary can be generated so an existing
     * node without any resources.
     */
@Test
public void canGenerateEmptySummary() {
    // Return a single node when called using the given filter
    String rule = "ipaddr iplike 172.20.1.1";
    FilterDao filterDao = mock(FilterDao.class);
    when(filterDao.getNodeMap(rule)).thenReturn(ImmutableSortedMap.of(1, "node1"));
    OnmsNode node = mock(OnmsNode.class);
    NodeDao nodeDao = mock(NodeDao.class);
    when(nodeDao.load(1)).thenReturn(node);
    OnmsResource resource = mock(OnmsResource.class);
    ResourceDao resourceDao = mock(ResourceDao.class);
    when(resourceDao.getResourceForNode(node)).thenReturn(resource);
    // Use our mocks
    m_rrdSummaryService.setFilterDao(filterDao);
    m_rrdSummaryService.setNodeDao(nodeDao);
    m_rrdSummaryService.setResourceDao(resourceDao);
    // Building the summary spec.
    SummarySpecification summarySpec = new SummarySpecification();
    summarySpec.setFilterRule("ipaddr iplike 172.20.1.1");
    summarySpec.setStartTime(1472746964);
    summarySpec.setEndTime(1473265364);
    summarySpec.setAttributeSieve(".*");
    // Invoke the controller
    HttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mv = m_controller.processFormSubmission(response, summarySpec);
    Summary summary = (Summary) mv.getModel().get("summary");
    // Verify the response
    assertEquals(0, summary.getResources().size());
}
Also used : NodeDao(org.opennms.netmgt.dao.api.NodeDao) FilterDao(org.opennms.netmgt.filter.api.FilterDao) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsResource(org.opennms.netmgt.model.OnmsResource) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Summary(org.opennms.netmgt.config.attrsummary.Summary) ResourceDao(org.opennms.netmgt.dao.api.ResourceDao) SummarySpecification(org.opennms.web.svclayer.model.SummarySpecification) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with FilterDao

use of org.opennms.netmgt.filter.api.FilterDao in project opennms by OpenNMS.

the class PollableServiceConfigTest method testPollableServiceConfig.

@Test
public void testPollableServiceConfig() throws Exception {
    final FilterDao fd = mock(FilterDao.class);
    FilterDaoFactory.setInstance(fd);
    InputStream is = new FileInputStream(new File("src/test/resources/etc/psm-poller-configuration.xml"));
    PollerConfigFactory factory = new PollerConfigFactory(0, is, "localhost", false);
    PollerConfigFactory.setInstance(factory);
    IOUtils.closeQuietly(is);
    PersisterFactory persisterFactory = new MockPersisterFactory();
    ResourceStorageDao resourceStorageDao = new FilesystemResourceStorageDao();
    final PollContext context = mock(PollContext.class);
    final PollableNetwork network = new PollableNetwork(context);
    final PollableNode node = network.createNodeIfNecessary(1, "foo", null);
    final PollableInterface iface = new PollableInterface(node, InetAddressUtils.addr("127.0.0.1"));
    final PollableService svc = new PollableService(iface, "MQ_API_DirectRte_v2");
    final PollOutagesConfig pollOutagesConfig = mock(PollOutagesConfig.class);
    final Package pkg = factory.getPackage("MapQuest");
    final Timer timer = mock(Timer.class);
    final PollableServiceConfig psc = new PollableServiceConfig(svc, factory, pollOutagesConfig, pkg, timer, persisterFactory, resourceStorageDao, m_locationAwarePollerClient);
    PollStatus pollStatus = psc.poll();
    assertThat(pollStatus.getReason(), not(containsString("Unexpected exception")));
}
Also used : FilesystemResourceStorageDao(org.opennms.netmgt.dao.support.FilesystemResourceStorageDao) ResourceStorageDao(org.opennms.netmgt.dao.api.ResourceStorageDao) PollStatus(org.opennms.netmgt.poller.PollStatus) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MockPersisterFactory(org.opennms.netmgt.mock.MockPersisterFactory) PersisterFactory(org.opennms.netmgt.collection.api.PersisterFactory) FilesystemResourceStorageDao(org.opennms.netmgt.dao.support.FilesystemResourceStorageDao) FileInputStream(java.io.FileInputStream) FilterDao(org.opennms.netmgt.filter.api.FilterDao) PollerConfigFactory(org.opennms.netmgt.config.PollerConfigFactory) Timer(org.opennms.netmgt.scheduler.Timer) MockPersisterFactory(org.opennms.netmgt.mock.MockPersisterFactory) Package(org.opennms.netmgt.config.poller.Package) File(java.io.File) PollOutagesConfig(org.opennms.netmgt.config.PollOutagesConfig) Test(org.junit.Test)

Example 5 with FilterDao

use of org.opennms.netmgt.filter.api.FilterDao in project opennms by OpenNMS.

the class LatencyStoringServiceMonitorAdaptorIT method executeThresholdTest.

private void executeThresholdTest(Double[] rtValues) throws Exception {
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("rrd-repository", "/tmp");
    parameters.put("ds-name", "icmp");
    parameters.put("rrd-base-name", "icmp");
    parameters.put("thresholding-enabled", "true");
    FilterDao filterDao = m_mocks.createMock(FilterDao.class);
    expect(filterDao.getActiveIPAddressList((String) EasyMock.anyObject())).andReturn(Collections.singletonList(addr("127.0.0.1"))).anyTimes();
    filterDao.flushActiveIpAddressListCache();
    EasyMock.expectLastCall().anyTimes();
    FilterDaoFactory.setInstance(filterDao);
    MonitoredService svc = m_mocks.createMock(MonitoredService.class);
    expect(svc.getNodeId()).andReturn(1);
    expect(svc.getIpAddr()).andReturn("127.0.0.1").atLeastOnce();
    expect(svc.getSvcName()).andReturn("ICMP").atLeastOnce();
    expect(svc.getNodeLocation()).andReturn(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID).atLeastOnce();
    ServiceMonitor service = new MockServiceMonitor(rtValues);
    int step = 1;
    List<String> rras = Collections.singletonList("RRA:AVERAGE:0.5:1:2016");
    Package pkg = new Package();
    Rrd rrd = new Rrd();
    rrd.setStep(step);
    rrd.setRras(rras);
    pkg.setRrd(rrd);
    expect(m_pollerConfig.getRRAList(pkg)).andReturn(rras).anyTimes();
    expect(m_pollerConfig.getStep(pkg)).andReturn(step).anyTimes();
    m_mocks.replayAll();
    LatencyStoringServiceMonitorAdaptor adaptor = new LatencyStoringServiceMonitorAdaptor(m_pollerConfig, pkg, m_persisterFactory, m_resourceStorageDao);
    // Make sure that the ThresholdingSet initializes with test settings
    String previousOpennmsHome = System.setProperty("opennms.home", "src/test/resources");
    for (int i = 0; i < rtValues.length; i++) {
        adaptor.handlePollResult(svc, parameters, service.poll(svc, parameters));
        // Emulate the appropriate wait time prior inserting another value into the RRD files.
        Thread.sleep(1000 * step);
    }
    System.setProperty("opennms.home", previousOpennmsHome);
    m_mocks.verifyAll();
}
Also used : HashMap(java.util.HashMap) Rrd(org.opennms.netmgt.config.poller.Rrd) MonitoredService(org.opennms.netmgt.poller.MonitoredService) FilterDao(org.opennms.netmgt.filter.api.FilterDao) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) AbstractServiceMonitor(org.opennms.netmgt.poller.support.AbstractServiceMonitor) Package(org.opennms.netmgt.config.poller.Package)

Aggregations

FilterDao (org.opennms.netmgt.filter.api.FilterDao)5 File (java.io.File)3 Before (org.junit.Before)2 Test (org.junit.Test)2 PollOutagesConfigFactory (org.opennms.netmgt.config.PollOutagesConfigFactory)2 Package (org.opennms.netmgt.config.poller.Package)2 NodeDao (org.opennms.netmgt.dao.api.NodeDao)2 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)2 MockPersisterFactory (org.opennms.netmgt.mock.MockPersisterFactory)2 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 InetAddress (java.net.InetAddress)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 PersisterFactory (org.opennms.netmgt.collection.api.PersisterFactory)1