Search in sources :

Example 41 with CriteriaBuilder

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

the class BusinessServiceDaoIT method verifyDistinctObjectLoading.

@Test
@Transactional
public void verifyDistinctObjectLoading() {
    BusinessServiceEntity entity = new BusinessServiceEntityBuilder().name("Parent Web Servers").addReductionKey("TestReductionKeyA", new IdentityEntity()).addReductionKey("TestReductionKeyB", new IdentityEntity()).addReductionKey("TestReductionKeyC", new IdentityEntity()).reduceFunction(m_highestSeverity).toEntity();
    m_businessServiceDao.save(entity);
    m_businessServiceDao.flush();
    assertEquals(1, m_businessServiceDao.countAll());
    assertEquals(3, m_edgeDao.countAll());
    Criteria criteria = new CriteriaBuilder(BusinessServiceEntity.class).toCriteria();
    // verify that root entity is merged
    assertEquals(1, m_businessServiceDao.findMatching(criteria).size());
    // verify that countMatching also works
    assertEquals(1, m_businessServiceDao.countMatching(criteria));
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity) Criteria(org.opennms.core.criteria.Criteria) BusinessServiceEntityBuilder(org.opennms.netmgt.bsm.test.BusinessServiceEntityBuilder) IdentityEntity(org.opennms.netmgt.bsm.persistence.api.functions.map.IdentityEntity) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 42 with CriteriaBuilder

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

the class EventdIT method testPersistEvent.

@Test(timeout = 30000)
public void testPersistEvent() throws Exception {
    CriteriaBuilder cb = new CriteriaBuilder(OnmsEvent.class);
    cb.eq("eventUei", EventConstants.NODE_DOWN_EVENT_UEI);
    assertEquals(0, m_eventDao.countMatching(cb.toCriteria()));
    OnmsNode node = m_databasePopulator.getNode1();
    assertNotNull(node);
    sendNodeDownEvent(null, node);
    while (m_eventDao.countMatching(cb.toCriteria()) < 1) {
        Thread.sleep(SLEEP_TIME);
    }
    final List<OnmsEvent> matching = m_eventDao.findMatching(cb.toCriteria());
    System.err.println("matching = " + matching);
    assertEquals(1, m_eventDao.countMatching(cb.toCriteria()));
    node = m_databasePopulator.getNode2();
    assertNotNull(node);
    Event generatedEvent = sendNodeDownEvent(null, node);
    while (m_eventDao.countMatching(cb.toCriteria()) < 2) {
        Thread.sleep(SLEEP_TIME);
    }
    assertEquals(2, m_eventDao.countMatching(cb.toCriteria()));
    assertNull(generatedEvent.getInterfaceAddress());
    cb.isNull("ipAddr");
    assertEquals("failed, found: " + m_eventDao.findMatching(cb.toCriteria()), 2, m_eventDao.countMatching(cb.toCriteria()));
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.netmgt.xml.event.Event) Test(org.junit.Test)

Example 43 with CriteriaBuilder

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

the class EventdIT method testPersistEventWithService.

/**
 * Test that eventd's service ID lookup works properly.
 */
@Test(timeout = 30000)
public void testPersistEventWithService() throws Exception {
    CriteriaBuilder cb = new CriteriaBuilder(OnmsEvent.class);
    cb.eq("eventUei", EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI);
    assertEquals(0, m_eventDao.countMatching(cb.toCriteria()));
    assertNotNull(m_serviceTypeDao.findByName("ICMP"));
    OnmsNode node = m_databasePopulator.getNode1();
    assertNotNull(node);
    OnmsIpInterface intf = node.getIpInterfaceByIpAddress("192.168.1.1");
    assertNotNull(intf);
    System.err.println("services = " + intf.getMonitoredServices());
    OnmsMonitoredService svc = intf.getMonitoredServiceByServiceType("ICMP");
    assertNotNull(svc);
    assertEquals("192.168.1.1", str(svc.getIpAddress()));
    final Integer serviceId = svc.getServiceId();
    sendServiceDownEvent(null, svc);
    while (m_eventDao.countMatching(cb.toCriteria()) != 1) {
        Thread.sleep(SLEEP_TIME);
    }
    assertEquals(1, m_eventDao.countMatching(cb.toCriteria()));
    assertEquals("service ID for event", serviceId, m_eventDao.findMatching(cb.toCriteria()).get(0).getServiceType().getId());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) Test(org.junit.Test)

Example 44 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder 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 45 with CriteriaBuilder

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

the class IpLikeHopCriteria method getVertices.

@Override
public Set<VertexRef> getVertices() {
    CriteriaBuilder bldr = new CriteriaBuilder(OnmsIpInterface.class);
    bldr.iplike("ipAddr", m_ipQuery);
    List<OnmsIpInterface> ips = ipInterfaceProvider.findMatching(bldr.toCriteria());
    Set<VertexRef> vertices = new TreeSet<VertexRef>(new RefComparator());
    for (OnmsIpInterface ip : ips) {
        OnmsNode node = ip.getNode();
        vertices.add(new DefaultVertexRef("nodes", String.valueOf(node.getId()), node.getLabel()));
    }
    return vertices;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) TreeSet(java.util.TreeSet) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) RefComparator(org.opennms.features.topology.api.topo.RefComparator)

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