Search in sources :

Example 31 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class TrapIT method canReceiveTraps.

@Test
public void canReceiveTraps() throws Exception {
    Date startOfTest = new Date();
    final InetSocketAddress trapAddr = minionSystem.getServiceAddress(ContainerAlias.MINION, 1162, "udp");
    // Connect to the postgresql container
    InetSocketAddress pgsql = minionSystem.getServiceAddress(ContainerAlias.POSTGRES, 5432);
    HibernateDaoFactory daoFactory = new HibernateDaoFactory(pgsql);
    EventDao eventDao = daoFactory.getDao(EventDaoHibernate.class);
    // Parsing the message correctly relies on the customized syslogd-configuration.xml that is part of the OpenNMS image
    Criteria criteria = new CriteriaBuilder(OnmsEvent.class).eq("eventUei", "uei.opennms.org/generic/traps/SNMP_Warm_Start").ge("eventTime", startOfTest).toCriteria();
    // Send traps to the Minion listener until one makes it through
    await().atMost(5, MINUTES).pollInterval(30, SECONDS).pollDelay(0, SECONDS).until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            sendTrap(trapAddr);
            try {
                await().atMost(30, SECONDS).pollInterval(5, SECONDS).until(DaoUtils.countMatchingCallable(eventDao, criteria), greaterThanOrEqualTo(1));
            } catch (final Exception e) {
                return false;
            }
            return true;
        }
    });
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) HibernateDaoFactory(org.opennms.smoketest.utils.HibernateDaoFactory) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) EventDao(org.opennms.netmgt.dao.api.EventDao) InetSocketAddress(java.net.InetSocketAddress) Criteria(org.opennms.core.criteria.Criteria) Date(java.util.Date) Test(org.junit.Test)

Example 32 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class SyslogKafkaElasticsearch5OutageIT method testMinionSyslogsOverKafkaToEsRest.

@Test
public void testMinionSyslogsOverKafkaToEsRest() throws Exception {
    Date startOfTest = new Date();
    int numMessages = 10000;
    int packetsPerSecond = 250;
    InetSocketAddress minionSshAddr = testEnvironment.getServiceAddress(ContainerAlias.MINION, 8201);
    InetSocketAddress opennmsSshAddr = testEnvironment.getServiceAddress(ContainerAlias.OPENNMS, 8101);
    InetSocketAddress kafkaAddress = testEnvironment.getServiceAddress(ContainerAlias.KAFKA, 9092);
    InetSocketAddress zookeeperAddress = testEnvironment.getServiceAddress(ContainerAlias.KAFKA, 2181);
    // Install the Kafka syslog and trap handlers on the Minion system
    installFeaturesOnMinion(minionSshAddr, kafkaAddress);
    // Install the Kafka and Elasticsearch features on the OpenNMS system
    installFeaturesOnOpenNMS(opennmsSshAddr, kafkaAddress, zookeeperAddress);
    final String sender = testEnvironment.getContainerInfo(ContainerAlias.SNMPD).networkSettings().ipAddress();
    // Wait for the minion to show up
    await().atMost(90, SECONDS).pollInterval(5, SECONDS).until(DaoUtils.countMatchingCallable(getDaoFactory().getDao(MinionDaoHibernate.class), new CriteriaBuilder(OnmsMinion.class).gt("lastUpdated", startOfTest).eq("location", "MINION").toCriteria()), is(1));
    LOG.info("Warming up syslog routes by sending 100 packets");
    // Warm up the routes
    sendMessage(ContainerAlias.MINION, sender, 100);
    for (int i = 0; i < 10; i++) {
        LOG.info("Slept for " + i + " seconds");
        Thread.sleep(1000);
    }
    LOG.info("Resetting statistics");
    resetRouteStatistics(opennmsSshAddr, minionSshAddr);
    for (int i = 0; i < 20; i++) {
        LOG.info("Slept for " + i + " seconds");
        Thread.sleep(1000);
    }
    // Make sure that this evenly divides into the numMessages
    final int chunk = 250;
    // Make sure that this is an even multiple of chunk
    final int logEvery = 1000;
    int count = 0;
    long start = System.currentTimeMillis();
    AtomicInteger restartCounter = new AtomicInteger();
    // Start a timer that occasionally restarts Elasticsearch
    Timer restarter = new Timer("Elasticsearch-Restarter", true);
    restarter.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            final DockerClient docker = ((AbstractTestEnvironment) testEnvironment).getDockerClient();
            final String id = testEnvironment.getContainerInfo(ContainerAlias.ELASTICSEARCH_5).id();
            try {
                LOG.info("Restarting container: {}", id);
                docker.restartContainer(id);
                restartCounter.incrementAndGet();
                LOG.info("Container restarted: {}", id);
            } catch (DockerException | InterruptedException e) {
                LOG.warn("Unexpected exception while restarting container {}", id, e);
            }
        }
    }, 0L, TimeUnit.SECONDS.toMillis(29));
    // Send ${numMessages} syslog messages
    RateLimiter limiter = RateLimiter.create(packetsPerSecond);
    for (int i = 0; i < (numMessages / chunk); i++) {
        limiter.acquire(chunk);
        sendMessage(ContainerAlias.MINION, sender, chunk);
        count += chunk;
        if (count % logEvery == 0) {
            long mid = System.currentTimeMillis();
            LOG.info(String.format("Sent %d packets in %d milliseconds", logEvery, mid - start));
            start = System.currentTimeMillis();
        }
    }
    // Stop restarting Elasticsearch
    restarter.cancel();
    // 100 warm-up messages plus ${numMessages} messages
    pollForElasticsearchEventsUsingJest(this::getEs5Address, 100 + numMessages);
    assertTrue("Elasticsearch was never restarted", restartCounter.get() > 0);
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) DockerClient(com.spotify.docker.client.DockerClient) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) InetSocketAddress(java.net.InetSocketAddress) Date(java.util.Date) RateLimiter(com.google.common.util.concurrent.RateLimiter) Timer(java.util.Timer) TimerTask(java.util.TimerTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 33 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class HibernateCriteriaConverterIT method testDistinctQuery.

@Test
@JUnitTemporaryDatabase
public void testDistinctQuery() {
    List<OnmsNode> nodes = null;
    final CriteriaBuilder cb = new CriteriaBuilder(OnmsNode.class);
    cb.isNotNull("id").distinct();
    cb.eq("label", "node1").join("ipInterfaces", "ipInterface", JoinType.LEFT_JOIN).eq("ipInterface.ipAddress", "192.168.1.1");
    nodes = m_nodeDao.findMatching(cb.toCriteria());
    assertEquals(1, nodes.size());
    assertEquals(Integer.valueOf(1), nodes.get(0).getId());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsNode(org.opennms.netmgt.model.OnmsNode) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 34 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class HibernateCriteriaConverterIT method testNodeIlikeQuery.

@Test
@JUnitTemporaryDatabase
public void testNodeIlikeQuery() {
    final CriteriaBuilder cb = new CriteriaBuilder(OnmsNode.class);
    cb.isNotNull("id").eq("label", "node1").alias("ipInterfaces", "iface").ilike("iface.ipAddress", "1%");
    final List<OnmsNode> nodes = m_nodeDao.findMatching(cb.toCriteria());
    assertEquals(3, nodes.size());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsNode(org.opennms.netmgt.model.OnmsNode) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 35 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class AlarmHopCriteria method findAlarms.

private List<OnmsAlarm> findAlarms() {
    CriteriaBuilder bldr = new CriteriaBuilder(OnmsAlarm.class);
    String query = getSearchResult().getQuery();
    if (isSeverityQuery()) {
        bldr.eq("severity", OnmsSeverity.get(query));
    } else {
        bldr.eq("id", getSearchResult().getAlarmId());
    }
    return alarmProvider.findMatchingAlarms(bldr.toCriteria());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder)

Aggregations

CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)155 Test (org.junit.Test)60 Criteria (org.opennms.core.criteria.Criteria)31 OnmsNode (org.opennms.netmgt.model.OnmsNode)25 Transactional (org.springframework.transaction.annotation.Transactional)23 Date (java.util.Date)20 GET (javax.ws.rs.GET)13 ArrayList (java.util.ArrayList)12 Produces (javax.ws.rs.Produces)12 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)11 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)11 InetSocketAddress (java.net.InetSocketAddress)9 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)7 HibernateDaoFactory (org.opennms.smoketest.utils.HibernateDaoFactory)7 OnmsMinion (org.opennms.netmgt.model.minion.OnmsMinion)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)4 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)4 EventDao (org.opennms.netmgt.dao.api.EventDao)4 OnmsSeverity (org.opennms.netmgt.model.OnmsSeverity)4 OnmsSnmpInterface (org.opennms.netmgt.model.OnmsSnmpInterface)4