Search in sources :

Example 51 with Criteria

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

the class BusinessServiceDaoIT method verifyFindMatching.

@Test
@Transactional
public void verifyFindMatching() {
    /*
         * Test that offset and limit work, when only ordered by id
         */
    // create test data
    Long bsId1 = m_businessServiceDao.save(new BusinessServiceEntityBuilder().name("BS 1.1").reduceFunction(new HighestSeverityEntity()).addReductionKey("bs1.key1", new IgnoreEntity(), 1).addReductionKey("bs1.key2", new IgnoreEntity(), 2).addReductionKey("bs1.key3", new IgnoreEntity(), 3).toEntity());
    Long bsId2 = m_businessServiceDao.save(new BusinessServiceEntityBuilder().name("BS 2.0").reduceFunction(new HighestSeverityEntity()).addReductionKey("bs2.key1", new IgnoreEntity(), 1).addReductionKey("bs2.key2", new IgnoreEntity(), 2).addReductionKey("bs2.key3", new IgnoreEntity(), 3).toEntity());
    m_businessServiceDao.flush();
    // create criteria to limit result
    org.opennms.core.criteria.Criteria criteria = new CriteriaBuilder(BusinessServiceEntity.class).offset(0).limit(2).orderBy("id").toCriteria();
    // verify that entities are distinct
    List<BusinessServiceEntity> filteredBusinessServices = m_businessServiceDao.findMatching(criteria);
    Assert.assertEquals(2, filteredBusinessServices.size());
    Assert.assertEquals(m_businessServiceDao.findAll().stream().sorted((bs1, bs2) -> bs1.getId().compareTo(bs2.getId())).collect(Collectors.toList()), filteredBusinessServices);
    // create another bs for a more complex test
    Long bsId3 = m_businessServiceDao.save(new BusinessServiceEntityBuilder().name("BS 3.1").reduceFunction(new HighestSeverityEntity()).addReductionKey("bs3.key3.1", new IgnoreEntity(), 4).addReductionKey("bs3.key3.2", new IgnoreEntity(), 5).addReductionKey("bs3.key3.3", new IgnoreEntity(), 6).toEntity());
    m_businessServiceDao.flush();
    // restrict to edge.weight > 3 and order by id descending
    criteria.setAliases(Lists.newArrayList(new Alias("edges", "edge", Alias.JoinType.INNER_JOIN, Restrictions.ge("edge.weight", 3))));
    criteria.setOrders(Lists.newArrayList(new Order("id", false)));
    // Verify
    filteredBusinessServices = m_businessServiceDao.findMatching(criteria);
    Assert.assertEquals(2, filteredBusinessServices.size());
    Assert.assertEquals(Lists.newArrayList(m_businessServiceDao.get(bsId3), m_businessServiceDao.get(bsId2)), filteredBusinessServices);
    /*
         * Verify that one can also order by name
         */
    Criteria nameCriteria = new CriteriaBuilder(BusinessServiceEntity.class).ilike("name", "BS %.1").orderBy("name").limit(2).toCriteria();
    filteredBusinessServices = m_businessServiceDao.findMatching(nameCriteria);
    Assert.assertEquals(2, filteredBusinessServices.size());
    Assert.assertEquals(Lists.newArrayList(m_businessServiceDao.get(bsId1), m_businessServiceDao.get(bsId3)), filteredBusinessServices);
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Order(org.opennms.core.criteria.Order) Criteria(org.opennms.core.criteria.Criteria) HighestSeverityEntity(org.opennms.netmgt.bsm.persistence.api.functions.reduce.HighestSeverityEntity) Criteria(org.opennms.core.criteria.Criteria) IgnoreEntity(org.opennms.netmgt.bsm.persistence.api.functions.map.IgnoreEntity) Alias(org.opennms.core.criteria.Alias) BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity) BusinessServiceEntityBuilder(org.opennms.netmgt.bsm.test.BusinessServiceEntityBuilder) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 52 with Criteria

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

the class BusinessServiceManagerImpl method transform.

/**
 * The criteria is build on BusinessService classes.
 * However we want to use the dao to filter. Therefore we have to perform a mapping from BusinessService to BusinessServiceEntity.
 *
 * @param input
 * @return
 */
private Criteria transform(Criteria input) {
    Criteria criteria = input.clone();
    criteria.setClass(BusinessServiceEntity.class);
    return criteria;
}
Also used : BusinessServiceSearchCriteria(org.opennms.netmgt.bsm.service.BusinessServiceSearchCriteria) Criteria(org.opennms.core.criteria.Criteria)

Example 53 with Criteria

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

the class DefaultGeolocationResolver method resolve.

@Override
public Map<Integer, Coordinates> resolve(Collection<Integer> nodeIds) {
    if (nodeIds == null || nodeIds.isEmpty()) {
        // nothing to do
        return new HashMap<>();
    }
    // Lookup all nodes and gather the address string
    final Criteria criteria = new CriteriaBuilder(OnmsNode.class).in("id", nodeIds).toCriteria();
    final Map<Integer, String> nodeIdAddressMap = nodeDao.findMatching(criteria).stream().filter(n -> getGeoLocation(n) != null).filter(n -> getGeoLocation(n).getLatitude() == null && getGeoLocation(n).getLongitude() == null).filter(n -> !Strings.isNullOrEmpty(getGeoLocation(n).asAddressString())).collect(Collectors.toMap(OnmsNode::getId, n -> n.getAssetRecord().getGeolocation().asAddressString()));
    return resolve(nodeIdAddressMap);
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) GeocoderService(org.opennms.features.geocoder.GeocoderService) NodeDao(org.opennms.netmgt.dao.api.NodeDao) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Criteria(org.opennms.core.criteria.Criteria) GeocoderException(org.opennms.features.geocoder.GeocoderException) Coordinates(org.opennms.features.geolocation.api.Coordinates) Objects(java.util.Objects) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Strings(com.google.common.base.Strings) Map(java.util.Map) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) GeolocationResolver(org.opennms.features.geolocation.api.GeolocationResolver) OnmsNode(org.opennms.netmgt.model.OnmsNode) HashMap(java.util.HashMap) Criteria(org.opennms.core.criteria.Criteria)

Example 54 with Criteria

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

the class DefaultSurveillanceViewService method getNotificationsWithCriterias.

/**
 * Returns a list of notifications for a given list of nodes.
 *
 * @param rowCategories  the row catgories
 * @param colCategories  the column categories
 * @param customSeverity the custom severity mapping for notifications
 * @param severity       the severity for these nodes
 * @param criterias      the restrictions to use
 * @return the list of notifications
 */
private List<OnmsNotification> getNotificationsWithCriterias(final Set<OnmsCategory> rowCategories, final Set<OnmsCategory> colCategories, final Map<OnmsNotification, String> customSeverity, final String severity, final Restriction... criterias) {
    CriteriaBuilder criteriaBuilder = new CriteriaBuilder(OnmsNotification.class);
    criteriaBuilder.alias("node", "node");
    final List<String> parameters = new ArrayList<>(rowCategories.stream().map(OnmsCategory::getName).collect(Collectors.toList()));
    parameters.addAll(colCategories.stream().map(OnmsCategory::getName).collect(Collectors.toList()));
    final Type[] types = new Type[parameters.size()];
    Arrays.fill(types, Type.STRING);
    // Restrict on OnmsNotification.nodeId
    criteriaBuilder.sql(createQuery("{alias}.nodeId", rowCategories, colCategories), parameters.toArray(new String[parameters.size()]), types);
    criteriaBuilder.ne("node.type", "D");
    criteriaBuilder.orderBy("pageTime", false);
    Criteria myCriteria = criteriaBuilder.toCriteria();
    for (Restriction criteria : criterias) {
        myCriteria.addRestriction(criteria);
    }
    List<OnmsNotification> notifications = m_notificationDao.findMatching(myCriteria);
    for (OnmsNotification onmsNotification : notifications) {
        customSeverity.put(onmsNotification, severity);
    }
    return notifications;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Restriction(org.opennms.core.criteria.restrictions.Restriction) OnmsResourceType(org.opennms.netmgt.model.OnmsResourceType) Type(org.opennms.core.criteria.restrictions.SqlRestriction.Type) OnmsCategory(org.opennms.netmgt.model.OnmsCategory) ArrayList(java.util.ArrayList) Criteria(org.opennms.core.criteria.Criteria) OnmsNotification(org.opennms.netmgt.model.OnmsNotification)

Example 55 with Criteria

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

the class HypericAckProcessor method fetchUnclearedHypericAlarms.

/**
 * <p>fetchUnclearedHypericAlarms</p>
 *
 * @return a {@link java.util.List} object.
 */
public List<OnmsAlarm> fetchUnclearedHypericAlarms() {
    // Query for existing, unacknowledged alarms in OpenNMS that were generated based on Hyperic alerts
    Criteria criteria = new Criteria(OnmsAlarm.class);
    // criteria.add(Restrictions.isNull("alarmAckUser"));
    // Restrict to Hyperic alerts
    criteria.addRestriction(new EqRestriction("uei", "uei.opennms.org/external/hyperic/alert"));
    // Only consider alarms that are above severity NORMAL
    // {@see org.opennms.netmgt.model.OnmsSeverity}
    criteria.addRestriction(new GtRestriction("severity", OnmsSeverity.NORMAL));
    // Query list of outstanding alerts with remote platform identifiers
    return m_alarmDao.findMatching(criteria);
}
Also used : EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) Criteria(org.opennms.core.criteria.Criteria) GtRestriction(org.opennms.core.criteria.restrictions.GtRestriction)

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