Search in sources :

Example 76 with CriteriaBuilder

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

the class CriteriaBuilderUtils method buildFrom.

public static CriteriaBuilder buildFrom(Class<?> clazz, QueryParameters queryParameters) {
    Objects.requireNonNull(clazz);
    Objects.requireNonNull(queryParameters);
    CriteriaBuilder criteriaBuilder = new CriteriaBuilder(clazz);
    applyQueryParameters(criteriaBuilder, queryParameters);
    return criteriaBuilder;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder)

Example 77 with CriteriaBuilder

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

the class CriteriaBuilderSearchVisitor method visit.

@Override
public void visit(SearchCondition<T> sc) {
    PrimitiveStatement statement = sc.getStatement();
    if (statement != null) {
        if (statement.getProperty() != null) {
            String name = getRealPropertyName(statement.getProperty());
            // TODO: Figure out how to use validators at some point
            //validatePropertyValue(name, originalValue);
            // Introspect the property type
            ClassValue clsValue = getPrimitiveFieldClass(statement, name, statement.getValue().getClass(), statement.getValueType(), statement.getValue());
            // If the property value is a String
            boolean isWildcard = false;
            if (String.class.equals(clsValue.getCls())) {
                // And if it's a FIQL wildcard
                if (SearchUtils.containsWildcard((String) clsValue.getValue())) {
                    // Then mark it as a wildcard and replace the * wildcards with % wildcards
                    isWildcard = true;
                    clsValue.setValue(SearchUtils.toSqlWildcardString((String) clsValue.getValue(), isWildcardStringMatch()));
                }
            }
            switch(sc.getConditionType()) {
                case EQUALS:
                    if (isWildcard) {
                        m_criteriaBuilder.like(name, clsValue.getValue());
                    } else {
                        if (clsValue.getValue() == null || NULL_VALUE.equals(clsValue.getValue()) || NULL_DATE_VALUE.equals(clsValue.getValue())) {
                            m_criteriaBuilder.isNull(name);
                        } else {
                            m_criteriaBuilder.eq(name, clsValue.getValue());
                        }
                    }
                    break;
                case NOT_EQUALS:
                    if (isWildcard) {
                        m_criteriaBuilder.not().like(name, clsValue.getValue());
                    } else {
                        if (clsValue.getValue() == null || NULL_VALUE.equals(clsValue.getValue()) || NULL_DATE_VALUE.equals(clsValue.getValue())) {
                            m_criteriaBuilder.isNotNull(name);
                        } else {
                            // Match any rows that do not match the value or are null
                            m_criteriaBuilder.or(Restrictions.ne(name, clsValue.getValue()), Restrictions.isNull(name));
                        }
                    }
                    break;
                case LESS_THAN:
                    // TODO: Check for null?
                    m_criteriaBuilder.lt(name, clsValue.getValue());
                    break;
                case GREATER_THAN:
                    // TODO: Check for null?
                    m_criteriaBuilder.gt(name, clsValue.getValue());
                    break;
                case LESS_OR_EQUALS:
                    // TODO: Check for null?
                    m_criteriaBuilder.le(name, clsValue.getValue());
                    break;
                case GREATER_OR_EQUALS:
                    // TODO: Check for null?
                    m_criteriaBuilder.ge(name, clsValue.getValue());
                    break;
                case OR:
                case AND:
                case CUSTOM:
                default:
            }
        }
    } else {
        List<Restriction> subRestrictions = new ArrayList<Restriction>();
        for (SearchCondition<T> condition : sc.getSearchConditions()) {
            // Create a new CriteriaBuilder
            CriteriaBuilder builder = null;
            try {
                // Try to use the same class as the outside CriteriaBuilder
                builder = m_criteriaBuilder.getClass().getConstructor(Class.class).newInstance(m_class);
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
                LOG.warn("Could not create " + m_criteriaBuilder.getClass().getSimpleName() + "; falling back to CriteriaBuilder: " + e.getClass().getSimpleName() + ": " + e.getMessage());
                builder = new CriteriaBuilder(m_class);
            }
            // Create a new visitor for the SearchCondition
            CriteriaBuilderSearchVisitor<T> newVisitor = new CriteriaBuilderSearchVisitor<T>(builder, m_class);
            // Visit the children
            condition.accept(newVisitor);
            // Fetch the rendered restrictions
            Collection<Restriction> restrictions = newVisitor.getQuery().toCriteria().getRestrictions();
            // If there are restrictions...
            if (restrictions != null && restrictions.size() > 0) {
                final Restriction subRestriction;
                // If there are multiple restrictions...
                if (restrictions.size() > 1) {
                    // Wrap them in an AND restriction
                    subRestriction = Restrictions.all(restrictions);
                } else {
                    subRestriction = restrictions.iterator().next();
                }
                LOG.info(subRestriction.toString());
                subRestrictions.add(subRestriction);
            }
        }
        switch(sc.getConditionType()) {
            case OR:
                LOG.info("OR criteria");
                // .or() with current Criteria
                m_criteriaBuilder.or(subRestrictions.toArray(new Restriction[0]));
                break;
            case AND:
                LOG.info("AND criteria");
                // .and() with current Criteria
                m_criteriaBuilder.and(subRestrictions.toArray(new Restriction[0]));
                break;
            default:
        }
    }
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Restriction(org.opennms.core.criteria.restrictions.Restriction) PrimitiveStatement(org.apache.cxf.jaxrs.ext.search.PrimitiveStatement)

Example 78 with CriteriaBuilder

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

the class ScanReportRestService method getCriteriaBuilder.

@Override
public CriteriaBuilder getCriteriaBuilder() {
    final CriteriaBuilder builder = new CriteriaBuilder(ScanReport.class);
    // Order by date (descending) by default
    builder.orderBy("timestamp").desc();
    return builder;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder)

Example 79 with CriteriaBuilder

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

the class MonitoringLocationRestService method getCriteriaBuilder.

protected CriteriaBuilder getCriteriaBuilder() {
    final CriteriaBuilder builder = new CriteriaBuilder(OnmsMonitoringLocation.class);
    // Order by location name by default
    builder.orderBy("locationName").asc();
    return builder;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder)

Example 80 with CriteriaBuilder

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

the class SyslogKafkaElasticsearchBufferingIT method testMinionSyslogsOverKafkaToEsRest.

@Test
public void testMinionSyslogsOverKafkaToEsRest() throws Exception {
    Date startOfTest = new Date();
    int numMessages = 1000;
    int packetsPerSecond = 50;
    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));
    // Shut down OpenNMS. Syslog messages will accumulate in the Kafka
    // message queue while it is down.
    stopContainer(ContainerAlias.OPENNMS);
    LOG.info("Warming up syslog routes by sending 100 packets");
    // Warm up the routes
    sendMessage(ContainerAlias.MINION, sender, 100);
    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 = 50;
    // Make sure that this is an even multiple of chunk
    final int logEvery = 100;
    int count = 0;
    long start = System.currentTimeMillis();
    // 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();
        }
    }
    // Start OpenNMS. It should begin to consume syslog messages and forward
    // them to Elasticsearch without dropping messages.
    startContainer(ContainerAlias.OPENNMS);
    // 100 warm-up messages plus ${numMessages} messages
    pollForElasticsearchEventsUsingJest(this::getEs5Address, 100 + numMessages);
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) InetSocketAddress(java.net.InetSocketAddress) Date(java.util.Date) RateLimiter(com.google.common.util.concurrent.RateLimiter) Test(org.junit.Test)

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