Search in sources :

Example 1 with Criteria

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

the class ApplicationSearchProvider method query.

@Override
public List<SearchResult> query(SearchQuery searchQuery, GraphContainer container) {
    LOG.info("ApplicationServiceSearchProvider->query: called with search query: '{}'", searchQuery);
    List<SearchResult> results = Lists.newArrayList();
    String queryString = searchQuery.getQueryString();
    CriteriaBuilder bldr = new CriteriaBuilder(OnmsApplication.class);
    if (queryString != null && queryString.length() > 0) {
        bldr.ilike("name", String.format("%%%s%%", queryString));
    }
    bldr.orderBy("name", true);
    bldr.limit(10);
    Criteria dbQueryCriteria = bldr.toCriteria();
    for (OnmsApplication application : applicationDao.findMatching(dbQueryCriteria)) {
        final ApplicationVertex applicationVertex = new ApplicationVertex(application);
        SearchResult searchResult = new SearchResult(applicationVertex, true, false);
        results.add(searchResult);
    }
    LOG.info("ApplicationServiceSearchProvider->query: found {} results: {}", results.size(), results);
    return results;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) SearchResult(org.opennms.features.topology.api.topo.SearchResult) Criteria(org.opennms.core.criteria.Criteria) OnmsApplication(org.opennms.netmgt.model.OnmsApplication)

Example 2 with Criteria

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

the class EnLinkdElementFactory method convertFromModel.

@Transactional
@SuppressWarnings("deprecation")
private LldpLinkNode convertFromModel(int nodeid, LldpLink link) {
    LldpLinkNode linknode = new LldpLinkNode();
    linknode.setLldpPortString(getPortString(link.getLldpPortId(), link.getLldpPortIdSubType()));
    linknode.setLldpPortDescr(link.getLldpPortDescr());
    linknode.setLldpPortUrl(getSnmpInterfaceUrl(Integer.valueOf(nodeid), link.getLldpPortIfindex()));
    linknode.setLldpRemSysName(link.getLldpRemSysname());
    linknode.setLldpRemPortString(getPortString(link.getLldpRemPortId(), link.getLldpRemPortIdSubType()));
    linknode.setLldpRemPortDescr(link.getLldpRemPortDescr());
    linknode.setLldpCreateTime(Util.formatDateToUIString(link.getLldpLinkCreateTime()));
    linknode.setLldpLastPollTime(Util.formatDateToUIString(link.getLldpLinkLastPollTime()));
    OnmsNode remNode = null;
    List<LldpElement> lldpremelements = m_lldpElementDao.findByChassisId(link.getLldpRemChassisId(), link.getLldpRemChassisIdSubType());
    if (lldpremelements.size() == 1) {
        remNode = lldpremelements.get(0).getNode();
    } else if (lldpremelements.size() > 1) {
        linknode.setLldpRemChassisIdString(getChassisIdString("Found " + lldpremelements.size() + " nodes for", link.getLldpRemChassisId(), link.getLldpRemChassisIdSubType()));
        return linknode;
    } else {
        final Criteria criteria = new Criteria(OnmsNode.class).addRestriction(new EqRestriction("sysName", link.getLldpRemSysname()));
        List<OnmsNode> nodes = m_nodeDao.findMatching(criteria);
        if (nodes.size() == 1)
            remNode = nodes.get(0);
    }
    if (remNode != null) {
        linknode.setLldpRemChassisIdString(getChassisIdString(remNode.getLabel(), link.getLldpRemChassisId(), link.getLldpRemChassisIdSubType()));
        linknode.setLldpRemChassisIdUrl(getNodeUrl(remNode.getId()));
        if (link.getLldpRemPortIdSubType() == LldpPortIdSubType.LLDP_PORTID_SUBTYPE_LOCAL) {
            try {
                Integer remIfIndex = Integer.getInteger(link.getLldpRemPortId());
                linknode.setLldpRemPortUrl(getSnmpInterfaceUrl(Integer.valueOf(remNode.getId()), remIfIndex));
            } catch (Exception e) {
            }
        }
    } else {
        linknode.setLldpRemChassisIdString(getChassisIdString(link.getLldpRemChassisId(), link.getLldpRemChassisIdSubType()));
    }
    return linknode;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) List(java.util.List) ArrayList(java.util.ArrayList) LldpElement(org.opennms.netmgt.model.LldpElement) Criteria(org.opennms.core.criteria.Criteria) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Criteria

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

the class AlarmLifecycleEventsIT method canGenerateAlarmDeletedLifecycleEvents.

@Test
public void canGenerateAlarmDeletedLifecycleEvents() {
    // Expect an alarmCreated event
    m_eventMgr.getEventAnticipator().resetAnticipated();
    m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CREATED_UEI, "alarmd").getEvent());
    m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
    // Send a nodeDown
    sendNodeDownEvent(1);
    // Wait until we've received the alarmCreated event
    await().until(allAnticipatedEventsWereReceived());
    // Expect an alarmCreated and a alarmCleared event
    m_eventMgr.getEventAnticipator().resetAnticipated();
    m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CREATED_UEI, "alarmd").getEvent());
    m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CLEARED_UEI, "alarmd").getEvent());
    m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
    // Send a nodeUp
    sendNodeUpEvent(1);
    // Wait until we've received the alarmCreated and alarmCleared events
    // We need to wait for the cosmicClear automation, which currently runs every 30 seconds
    await().atMost(1, MINUTES).until(allAnticipatedEventsWereReceived());
    // Expect an alarmDeleted event
    m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_DELETED_EVENT_UEI, "alarmd").getEvent());
    m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
    // We need to wait for the cleanUp automation, which currently runs every 60 seconds
    // but it will only trigger then 'lastautomationtime' and 'lasteventtime' < "5 minutes ago"
    // so we cheat a little and update the timestamps ourselves instead of waiting
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            Criteria criteria = new Criteria(OnmsAlarm.class);
            criteria.addRestriction(new EqRestriction("node.id", 1));
            criteria.addRestriction(new EqRestriction("uei", EventConstants.NODE_DOWN_EVENT_UEI));
            for (OnmsAlarm alarm : m_alarmDao.findMatching(criteria)) {
                LocalDateTime tenMinutesAgo = LocalDateTime.now().minusMinutes(10);
                Date then = Date.from(tenMinutesAgo.toInstant(ZoneOffset.UTC));
                alarm.setLastAutomationTime(then);
                alarm.setLastEventTime(then);
                m_alarmDao.save(alarm);
            }
            m_alarmDao.flush();
        }
    });
    // Wait until we've received the alarmDeleted event
    await().atMost(2, MINUTES).until(allAnticipatedEventsWereReceived());
}
Also used : LocalDateTime(java.time.LocalDateTime) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) TransactionStatus(org.springframework.transaction.TransactionStatus) Criteria(org.opennms.core.criteria.Criteria) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Date(java.util.Date) Test(org.junit.Test)

Example 4 with Criteria

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

the class BeanWrapperVisitorTest method testNot.

@Test
public void testNot() {
    final Criteria criteria = new CriteriaBuilder(TestBean.class).not().eq("string", "I am a string.").toCriteria();
    LOG.debug("criteria = {}", criteria);
    criteria.visit(m_visitor);
    assertEquals(0, m_visitor.getMatches().size());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Criteria(org.opennms.core.criteria.Criteria) Test(org.junit.Test)

Example 5 with Criteria

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

the class DefaultPollingContext method getInterfaceList.

protected OnmsIpInterfaceList getInterfaceList() {
    StringBuffer filterRules = new StringBuffer(getPackage().getEffectiveFilter());
    List<InetAddress> ipList = FilterDaoFactory.getInstance().getActiveIPAddressList(filterRules.toString());
    OnmsIpInterfaceList ifaces = new OnmsIpInterfaceList();
    // Only poll the primary interface
    final Criteria criteria = new Criteria(OnmsIpInterface.class);
    criteria.addRestriction(new EqRestriction("isSnmpPrimary", PrimaryType.PRIMARY));
    List<OnmsIpInterface> allValidIfaces = getIpInterfaceDao().findMatching(criteria);
    for (OnmsIpInterface iface : allValidIfaces) {
        if (ipList.contains(iface.getIpAddress())) {
            ifaces.add(iface);
        }
    }
    return ifaces;
}
Also used : OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) Criteria(org.opennms.core.criteria.Criteria) InetAddress(java.net.InetAddress) OnmsIpInterfaceList(org.opennms.netmgt.model.OnmsIpInterfaceList)

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