Search in sources :

Example 16 with Criteria

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

the class EventUtilDaoImpl method getHardwareFieldValue.

@Override
public String getHardwareFieldValue(String parm, long nodeId) {
    final Matcher matcher = HW_PARM_PATTERN.matcher(parm);
    if (!matcher.matches()) {
        LOG.warn("Unsupported hardware field parameter '{}'.", parm);
        return null;
    }
    final String hwFieldSelector = matcher.group(1);
    final String hwField = matcher.group(2).toLowerCase();
    // Retrieve the entity with a like query
    if (hwFieldSelector.startsWith("~")) {
        final String likeQuery = hwFieldSelector.substring(1);
        LOG.debug("Retrieving hardware field value {} on {} with like query {}", parm, nodeId, likeQuery);
        Criteria criteria = new Criteria(OnmsHwEntity.class).setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN) })).addRestriction(new EqRestriction("node.id", (int) nodeId)).addRestriction(new LikeRestriction("entPhysicalName", likeQuery)).setOrders(Arrays.asList(new Order[] { Order.desc("id") }));
        List<OnmsHwEntity> hwEntities = hwEntityDao.findMatching(criteria);
        System.err.println(hwEntities);
        if (hwEntities.size() < 1) {
            return null;
        }
        return getStringPropertyByName(hwField, hwEntities.get(0), hwEntityDescriptorsByName);
    }
    // Retrieve the entity by index if the select is an integer
    try {
        int index = Integer.parseInt(hwFieldSelector);
        OnmsHwEntity hwEntity = hwEntityDao.findEntityByIndex((int) nodeId, index);
        if (hwEntity == null) {
            // No entry with this index
            return null;
        }
        return getStringPropertyByName(hwField, hwEntity, hwEntityDescriptorsByName);
    } catch (NumberFormatException e) {
    // pass
    }
    // Retrieve the entity by name
    OnmsHwEntity hwEntity = hwEntityDao.findEntityByName((int) nodeId, hwFieldSelector);
    if (hwEntity == null) {
        // No entry with this name
        return null;
    }
    return getStringPropertyByName(hwField, hwEntity, hwEntityDescriptorsByName);
}
Also used : Order(org.opennms.core.criteria.Order) OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) Matcher(java.util.regex.Matcher) Alias(org.opennms.core.criteria.Alias) LikeRestriction(org.opennms.core.criteria.restrictions.LikeRestriction) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) Criteria(org.opennms.core.criteria.Criteria)

Example 17 with Criteria

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

the class IpLikeSearchProvider method query.

/**
 * This method processes the <SearchQuery> that the user has typed and returns a <SearchResult> list
 * of matching IP addresses as well as the query string itself, which is collapsible, to act
 * as a subnet container.
 */
@Override
public List<SearchResult> query(SearchQuery searchQuery, GraphContainer graphContainer) {
    LOG.info("SearchProvider->query: called with search query: '{}'", searchQuery);
    List<SearchResult> results = new ArrayList<>();
    String queryString = searchQuery.getQueryString();
    if (!isIpLikeQuery(queryString)) {
        LOG.debug("SearchProvider->query: query not IPLIKE compatible.");
        return results;
    }
    CriteriaBuilder bldr = new CriteriaBuilder(OnmsIpInterface.class);
    bldr.iplike("ipAddr", queryString).orderBy("ipAddress", true);
    Criteria dbQueryCriteria = bldr.toCriteria();
    List<OnmsIpInterface> ips;
    // since it seems to do something very similar in its matches method.
    try {
        ips = ipInterfaceProvider.findMatching(dbQueryCriteria);
        if (ips.size() == 0) {
            return results;
        } else {
            if (isIpLikeQuery(queryString)) {
                LOG.debug("SearchProvider->query: adding IPLIKE search spec '{}' to the search results.", queryString);
                SearchResult searchResult = new SearchResult(getSearchProviderNamespace(), queryString, queryString, queryString, SearchResult.COLLAPSIBLE, !SearchResult.COLLAPSED);
                if (!results.contains(searchResult)) {
                    results.add(searchResult);
                }
            }
        }
        Set<String> ipAddrs = new HashSet<>();
        LOG.info("SearchProvider->query: creating IP address set.");
        for (OnmsIpInterface ip : ips) {
            String hostAddress = ip.getIpAddress().getHostAddress();
            LOG.debug("SearchProvider->query: adding '{}' to set of IPs.", hostAddress);
            ipAddrs.add(hostAddress);
        }
        LOG.info("SearchProvider->query: building search result from set of IPs.");
        IPLOOP: for (String ip : ipAddrs) {
            if (findCriterion(ip, graphContainer) != null) {
                continue IPLOOP;
            } else {
                SearchResult searchResult = createSearchResult(ip, queryString);
                if (!results.contains(searchResult)) {
                    results.add(searchResult);
                }
            }
        }
        LOG.info("SearchProvider->query: found: '{}' IP interfaces.", ips.size());
    } catch (Exception e) {
        LOG.error("SearchProvider-query: caught exception during iplike query: {}", e);
    }
    LOG.info("SearchProvider->query: built search result with {} results.", results.size());
    return results;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) ArrayList(java.util.ArrayList) SearchResult(org.opennms.features.topology.api.topo.SearchResult) CollapsibleCriteria(org.opennms.features.topology.api.topo.CollapsibleCriteria) Criteria(org.opennms.core.criteria.Criteria) IpLikeHopCriteria(org.opennms.features.topology.app.internal.support.IpLikeHopCriteria) HashSet(java.util.HashSet)

Example 18 with Criteria

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

the class NodeDaoIT method testCriteriaBuilderOrderBy.

@Test
@Transactional
public void testCriteriaBuilderOrderBy() {
    CriteriaBuilder cb = new CriteriaBuilder(OnmsNode.class);
    cb.alias("ipInterfaces", "ipInterface").distinct();
    // TODO: Make this work but we need to put the fields into
    // an aggregator function since node->ipInterfaces is a 1->M
    // relationship.
    // 
    // cb.orderBy("ipInterfaces.ipAddress").distinct();
    Criteria criteria = cb.toCriteria();
    System.out.println("Criteria: " + criteria.toString());
    List<OnmsNode> nodes = m_nodeDao.findMatching(criteria);
    nodes.stream().forEach(System.out::println);
    assertEquals(6, nodes.size());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsNode(org.opennms.netmgt.model.OnmsNode) Criteria(org.opennms.core.criteria.Criteria) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with Criteria

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

the class NotificationDaoIT method testCriteria.

/**
 * Test to make sure that orderBy across an alias works.
 */
@Test
@Transactional
public void testCriteria() {
    Criteria criteria = new Criteria(OnmsNotification.class);
    criteria.setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN), new Alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN), new Alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN), new Alias("event", "event", JoinType.LEFT_JOIN), new Alias("usersNotified", "usersNotified", JoinType.LEFT_JOIN), new Alias("serviceType", "serviceType", JoinType.LEFT_JOIN) }));
    criteria.setOrders(Arrays.asList(new Order[] { new Order("event.id", false), new Order("event.eventSeverity", false), new Order("node.label", false), new Order("serviceType.name", false) }));
    m_notificationDao.findMatching(criteria);
    CriteriaBuilder builder = new CriteriaBuilder(OnmsNotification.class);
    builder.alias("node", "node", JoinType.LEFT_JOIN);
    builder.alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
    builder.alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
    builder.alias("event", "event", JoinType.LEFT_JOIN);
    builder.alias("usersNotified", "usersNotified", JoinType.LEFT_JOIN);
    builder.alias("serviceType", "serviceType", JoinType.LEFT_JOIN);
    builder.orderBy("event.id", false);
    builder.orderBy("event.eventSeverity", false);
    builder.orderBy("node.label", false);
    builder.orderBy("serviceType.name", false);
    m_notificationDao.findMatching(builder.toCriteria());
}
Also used : Order(org.opennms.core.criteria.Order) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Alias(org.opennms.core.criteria.Alias) Criteria(org.opennms.core.criteria.Criteria) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with Criteria

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

the class DefaultReportStoreService method getAll.

/**
 * <p>getAll</p>
 *
 * @return a {@link java.util.List} object.
 */
@Override
public List<ReportCatalogEntry> getAll() {
    final Criteria onmsCrit = new Criteria(ReportCatalogEntry.class);
    onmsCrit.setOrders(Arrays.asList(new Order[] { Order.desc("date") }));
    return m_reportCatalogDao.findMatching(onmsCrit);
}
Also used : Order(org.opennms.core.criteria.Order) Criteria(org.opennms.core.criteria.Criteria)

Aggregations

Criteria (org.opennms.core.criteria.Criteria)62 CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)31 Test (org.junit.Test)16 EqRestriction (org.opennms.core.criteria.restrictions.EqRestriction)16 Date (java.util.Date)13 Alias (org.opennms.core.criteria.Alias)13 ArrayList (java.util.ArrayList)10 Order (org.opennms.core.criteria.Order)9 Transactional (org.springframework.transaction.annotation.Transactional)9 GET (javax.ws.rs.GET)8 PUT (javax.ws.rs.PUT)7 OnmsNode (org.opennms.netmgt.model.OnmsNode)7 POST (javax.ws.rs.POST)6 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)5 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)5 OnmsOutage (org.opennms.netmgt.model.OnmsOutage)5 Produces (javax.ws.rs.Produces)4 EventDao (org.opennms.netmgt.dao.api.EventDao)4 ScanReport (org.opennms.netmgt.model.ScanReport)4 ScanReportRestService (org.opennms.web.rest.v2.ScanReportRestService)4