Search in sources :

Example 21 with BusinessServiceEntity

use of org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity 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 22 with BusinessServiceEntity

use of org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity in project opennms by OpenNMS.

the class BusinessServiceDaoIT method verifyDeleteOnCascade.

/**
 * If we do not explicitly delete the map or reduce function it should be deleted if not referenced anymore.
 */
@Test
@Transactional
public void verifyDeleteOnCascade() {
    BusinessServiceEntity child2 = new BusinessServiceEntityBuilder().name("Child 2").reduceFunction(new HighestSeverityEntity()).addReductionKey("some-key", new IdentityEntity()).toEntity();
    BusinessServiceEntity child1 = new BusinessServiceEntityBuilder().name("Child 1").reduceFunction(new HighestSeverityEntity()).addChildren(child2, new IdentityEntity()).toEntity();
    BusinessServiceEntity parent = new BusinessServiceEntityBuilder().name("Parent Web Servers").addAttribute("dc", "RDU").addReductionKey("TestReductionKeyA", new IdentityEntity()).addReductionKey("TestReductionKeyB", new IdentityEntity()).addIpService(getMonitoredServiceFromNode1(), new IdentityEntity()).reduceFunction(m_highestSeverity).addChildren(child1, new IdentityEntity()).toEntity();
    m_businessServiceDao.save(child2);
    m_businessServiceDao.save(child1);
    m_businessServiceDao.save(parent);
    m_businessServiceDao.flush();
    assertEquals(3, m_businessServiceDao.countAll());
    assertEquals(3, m_reductionFunctionDao.countAll());
    assertEquals(6, m_edgeDao.countAll());
    // Deletion of child does not delete the edges referencing to that child
    // remove all parent -> child associations manually
    BusinessServiceChildEdgeEntity parentToChild1Edge = parent.getChildEdges().iterator().next();
    parent.removeEdge(parentToChild1Edge);
    m_edgeDao.delete(parentToChild1Edge);
    // edges do not need to be deleted manually, deletes will be cascaded
    m_businessServiceDao.delete(child1);
    m_businessServiceDao.flush();
    assertEquals(2, m_businessServiceDao.countAll());
    assertEquals(2, m_reductionFunctionDao.countAll());
    assertEquals(4, m_edgeDao.countAll());
    // Deletion of parent should delete all references
    m_businessServiceDao.delete(parent);
    assertEquals(1, m_businessServiceDao.countAll());
    assertEquals(1, m_reductionFunctionDao.countAll());
    assertEquals(1, m_edgeDao.countAll());
    // Deletion of Child 2 should also work
    m_businessServiceDao.delete(child2);
    assertEquals(0, m_businessServiceDao.countAll());
    assertEquals(0, m_reductionFunctionDao.countAll());
    assertEquals(0, m_edgeDao.countAll());
}
Also used : BusinessServiceChildEdgeEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceChildEdgeEntity) BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity) HighestSeverityEntity(org.opennms.netmgt.bsm.persistence.api.functions.reduce.HighestSeverityEntity) 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 23 with BusinessServiceEntity

use of org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity in project opennms by OpenNMS.

the class BusinessServiceManagerImpl method deleteBusinessService.

@Override
public void deleteBusinessService(BusinessService businessService) {
    BusinessServiceEntity entity = getBusinessServiceEntity(businessService);
    // remove all parent -> child associations
    for (BusinessServiceEntity parent : getDao().findParents(entity)) {
        List<BusinessServiceChildEdgeEntity> collect = parent.getChildEdges().stream().filter(e -> entity.equals(e.getChild())).collect(Collectors.toList());
        collect.forEach(x -> {
            parent.removeEdge(x);
            // we need to delete this edge manually as they cannot be deleted automatically
            edgeDao.delete(x);
        });
    }
    // edges of the entity are deleted automatically by hibernate
    getDao().delete(entity);
}
Also used : ReductionKeyEdgeImpl(org.opennms.netmgt.bsm.service.internal.edge.ReductionKeyEdgeImpl) IpService(org.opennms.netmgt.bsm.service.model.IpService) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) ReductionFunctionDao(org.opennms.netmgt.bsm.persistence.api.functions.reduce.ReductionFunctionDao) ChildEdgeImpl(org.opennms.netmgt.bsm.service.internal.edge.ChildEdgeImpl) BusinessServiceDao(org.opennms.netmgt.bsm.persistence.api.BusinessServiceDao) BusinessServiceManager(org.opennms.netmgt.bsm.service.BusinessServiceManager) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractReductionFunctionEntity(org.opennms.netmgt.bsm.persistence.api.functions.reduce.AbstractReductionFunctionEntity) EventConstants(org.opennms.netmgt.events.api.EventConstants) MapFunction(org.opennms.netmgt.bsm.service.model.functions.map.MapFunction) BusinessServiceEdgeEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEdgeEntity) BusinessService(org.opennms.netmgt.bsm.service.model.BusinessService) MonitoredServiceDao(org.opennms.netmgt.dao.api.MonitoredServiceDao) IpServiceEdgeImpl(org.opennms.netmgt.bsm.service.internal.edge.IpServiceEdgeImpl) BusinessServiceSearchCriteria(org.opennms.netmgt.bsm.service.BusinessServiceSearchCriteria) AbstractEdge(org.opennms.netmgt.bsm.service.internal.edge.AbstractEdge) ReductionFunction(org.opennms.netmgt.bsm.service.model.functions.reduce.ReductionFunction) Status(org.opennms.netmgt.bsm.service.model.Status) BusinessServiceChildEdgeEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceChildEdgeEntity) EdgeEntityVisitor(org.opennms.netmgt.bsm.persistence.api.EdgeEntityVisitor) NoSuchElementException(java.util.NoSuchElementException) ChildEdge(org.opennms.netmgt.bsm.service.model.edge.ChildEdge) ReductionKeyEdge(org.opennms.netmgt.bsm.service.model.edge.ReductionKeyEdge) OnmsNode(org.opennms.netmgt.model.OnmsNode) NodeDao(org.opennms.netmgt.dao.api.NodeDao) AbstractMapFunctionEntity(org.opennms.netmgt.bsm.persistence.api.functions.map.AbstractMapFunctionEntity) BusinessServiceEdgeDao(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEdgeDao) Set(java.util.Set) BusinessServiceGraphImpl(org.opennms.netmgt.bsm.service.model.graph.internal.BusinessServiceGraphImpl) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) SingleReductionKeyEdgeEntity(org.opennms.netmgt.bsm.persistence.api.SingleReductionKeyEdgeEntity) Collectors(java.util.stream.Collectors) Criteria(org.opennms.core.criteria.Criteria) BusinessServiceStateMachine(org.opennms.netmgt.bsm.service.BusinessServiceStateMachine) Objects(java.util.Objects) BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity) BusinessServiceGraph(org.opennms.netmgt.bsm.service.model.graph.BusinessServiceGraph) List(java.util.List) Node(org.opennms.netmgt.bsm.service.model.Node) Edge(org.opennms.netmgt.bsm.service.model.edge.Edge) MapFunctionDao(org.opennms.netmgt.bsm.persistence.api.functions.map.MapFunctionDao) EventForwarder(org.opennms.netmgt.events.api.EventForwarder) IPServiceEdgeEntity(org.opennms.netmgt.bsm.persistence.api.IPServiceEdgeEntity) IpServiceEdge(org.opennms.netmgt.bsm.service.model.edge.IpServiceEdge) BusinessServiceChildEdgeEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceChildEdgeEntity) BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity)

Example 24 with BusinessServiceEntity

use of org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity in project opennms by OpenNMS.

the class DefaultBusinessServiceStateMachineIT method ensureReductionKeyLookupIsFastEnough.

/**
 * Builds 200 business services.
 * Each business Services has one parent and two children.
 * Each children has 12 reduction key edges.
 * In sum there are:
 *  - 600 business services
 *  - 4800 reduction key edges
 *  - 400 child edges
 *  - 5200 edges
 *
 *  See NMS-8978 for more details.
 */
@Test
public void ensureReductionKeyLookupIsFastEnough() {
    for (int i = 0; i < 200; i++) {
        BusinessServiceEntity parentEntity = new BusinessServiceEntityBuilder().name("Parent " + i).reduceFunction(new HighestSeverityEntity()).toEntity();
        for (int c = 0; c < 2; c++) {
            BusinessServiceEntityBuilder childBuilder = new BusinessServiceEntityBuilder().name("Child " + i + " " + c).reduceFunction(new HighestSeverityEntity());
            for (int a = 0; a < 12; a++) {
                childBuilder.addReductionKey("custom." + i + "." + c + "." + a, new IdentityEntity());
            }
            BusinessServiceEntity childEntity = childBuilder.toEntity();
            parentEntity.addChildServiceEdge(childEntity, new IdentityEntity());
            businessServiceDao.save(childEntity);
        }
        businessServiceDao.save(parentEntity);
    }
    final Set<String> uniqueReductionKeys = businessServiceDao.findMatching(new CriteriaBuilder(BusinessServiceEntity.class).like("name", "Child%").toCriteria()).stream().flatMap(service -> service.getReductionKeyEdges().stream()).flatMap(edge -> edge.getReductionKeys().stream()).collect(Collectors.toSet());
    for (String eachKey : uniqueReductionKeys) {
        final OnmsAlarm alarm = new OnmsAlarm();
        alarm.setUei("custom");
        alarm.setAlarmType(OnmsAlarm.PROBLEM_TYPE);
        alarm.setDescription("dummy");
        alarm.setLogMsg("dummy");
        alarm.setSeverity(OnmsSeverity.WARNING);
        alarm.setReductionKey(eachKey);
        alarm.setDistPoller(distPollerDao.whoami());
        alarm.setCounter(1);
        populator.getAlarmDao().save(alarm);
    }
    populator.getAlarmDao().flush();
    businessServiceDao.flush();
    // Simulate lookup of reduction keys
    final long start = System.currentTimeMillis();
    final DefaultBusinessServiceStateMachine stateMachine = new DefaultBusinessServiceStateMachine();
    stateMachine.setAlarmProvider(alarmProvider);
    stateMachine.setBusinessServices(businessServiceDao.findAll().stream().map(e -> new BusinessServiceImpl(businessServiceManager, e)).collect(Collectors.toList()));
    long diff = System.currentTimeMillis() - start;
    LoggerFactory.getLogger(getClass()).info("Took {} ms to initialize state machine", diff);
    Assert.assertTrue("Reduction Key lookup took much longer than expected. Expected was 1000 ms, but took " + diff + " ms", 1000 >= diff);
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) BusinessServicesShareIpServiceHierarchy(org.opennms.netmgt.bsm.test.hierarchies.BusinessServicesShareIpServiceHierarchy) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AlarmWrapper(org.opennms.netmgt.bsm.service.model.AlarmWrapper) BusinessService(org.opennms.netmgt.bsm.service.model.BusinessService) LoggingStateChangeHandler(org.opennms.netmgt.bsm.test.LoggingStateChangeHandler) IpServiceEdgeImpl(org.opennms.netmgt.bsm.service.internal.edge.IpServiceEdgeImpl) HighestSeverityEntity(org.opennms.netmgt.bsm.persistence.api.functions.reduce.HighestSeverityEntity) After(org.junit.After) IdentityEntity(org.opennms.netmgt.bsm.persistence.api.functions.map.IdentityEntity) Set(java.util.Set) Collectors(java.util.stream.Collectors) BeanUtils(org.opennms.core.spring.BeanUtils) BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity) List(java.util.List) OpenNMSJUnit4ClassRunner(org.opennms.core.test.OpenNMSJUnit4ClassRunner) HTTP_8085_BAMBOO_REDUCTION_KEY(org.opennms.netmgt.bsm.test.hierarchies.BambooTestHierarchy.HTTP_8085_BAMBOO_REDUCTION_KEY) Edge(org.opennms.netmgt.bsm.service.model.edge.Edge) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) IPServiceEdgeEntity(org.opennms.netmgt.bsm.persistence.api.IPServiceEdgeEntity) Identity(org.opennms.netmgt.bsm.service.model.functions.map.Identity) BambooTestHierarchy(org.opennms.netmgt.bsm.test.hierarchies.BambooTestHierarchy) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) IpServiceEdge(org.opennms.netmgt.bsm.service.model.edge.IpServiceEdge) RunWith(org.junit.runner.RunWith) BusinessServiceDao(org.opennms.netmgt.bsm.persistence.api.BusinessServiceDao) BusinessServiceManager(org.opennms.netmgt.bsm.service.BusinessServiceManager) DISK_USAGE_THRESHOLD_BAMBO_REDUCTION_KEY(org.opennms.netmgt.bsm.test.hierarchies.BambooTestHierarchy.DISK_USAGE_THRESHOLD_BAMBO_REDUCTION_KEY) EventConstants(org.opennms.netmgt.events.api.EventConstants) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) BsmDatabasePopulator(org.opennms.netmgt.bsm.test.BsmDatabasePopulator) Lists(com.google.common.collect.Lists) DistPollerDao(org.opennms.netmgt.dao.api.DistPollerDao) Status(org.opennms.netmgt.bsm.service.model.Status) Qualifier(org.springframework.beans.factory.annotation.Qualifier) AlarmProvider(org.opennms.netmgt.bsm.service.AlarmProvider) SimpleTestHierarchy(org.opennms.netmgt.bsm.test.hierarchies.SimpleTestHierarchy) BAMBOO_AGENT_CAROLINA_REDUCTION_KEY(org.opennms.netmgt.bsm.test.hierarchies.BambooTestHierarchy.BAMBOO_AGENT_CAROLINA_REDUCTION_KEY) Before(org.junit.Before) BsmTestUtils.createAlarmWrapper(org.opennms.netmgt.bsm.test.BsmTestUtils.createAlarmWrapper) JUnitConfigurationEnvironment(org.opennms.test.JUnitConfigurationEnvironment) Test(org.junit.Test) BAMBOO_AGENT_DUKE_REDUCTION_KEY(org.opennms.netmgt.bsm.test.hierarchies.BambooTestHierarchy.BAMBOO_AGENT_DUKE_REDUCTION_KEY) BusinessServiceEntityBuilder(org.opennms.netmgt.bsm.test.BusinessServiceEntityBuilder) ReductionKeyHelper(org.opennms.netmgt.dao.util.ReductionKeyHelper) ContextConfiguration(org.springframework.test.context.ContextConfiguration) BAMBOO_AGENT_NCSTATE_REDUCTION_KEY(org.opennms.netmgt.bsm.test.hierarchies.BambooTestHierarchy.BAMBOO_AGENT_NCSTATE_REDUCTION_KEY) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) OnmsSeverity(org.opennms.netmgt.model.OnmsSeverity) Transactional(org.springframework.transaction.annotation.Transactional) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) HighestSeverityEntity(org.opennms.netmgt.bsm.persistence.api.functions.reduce.HighestSeverityEntity) BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity) BusinessServiceEntityBuilder(org.opennms.netmgt.bsm.test.BusinessServiceEntityBuilder) IdentityEntity(org.opennms.netmgt.bsm.persistence.api.functions.map.IdentityEntity) Test(org.junit.Test)

Example 25 with BusinessServiceEntity

use of org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity in project opennms by OpenNMS.

the class BusinessServiceDaoIT method verifyUniqueNameConstraint.

@Test()
@Transactional
public void verifyUniqueNameConstraint() {
    BusinessServiceEntity entity1 = new BusinessServiceEntityBuilder().name("Some Custom Name").reduceFunction(m_highestSeverity).toEntity();
    m_businessServiceDao.save(entity1);
    m_businessServiceDao.flush();
    BusinessServiceEntity entity2 = new BusinessServiceEntityBuilder().name("Some Custom Name").reduceFunction(m_highestSeverity).toEntity();
    m_businessServiceDao.save(entity2);
    // Should throw a ConstraintViolationException (name not unique)
    try {
        m_businessServiceDao.flush();
        fail("ConstraintViolationException must be thrown");
    } catch (final DataIntegrityViolationException e) {
    }
}
Also used : BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity) BusinessServiceEntityBuilder(org.opennms.netmgt.bsm.test.BusinessServiceEntityBuilder) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BusinessServiceEntity (org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity)45 Test (org.junit.Test)29 BusinessServiceEntityBuilder (org.opennms.netmgt.bsm.test.BusinessServiceEntityBuilder)18 IdentityEntity (org.opennms.netmgt.bsm.persistence.api.functions.map.IdentityEntity)14 HighestSeverityEntity (org.opennms.netmgt.bsm.persistence.api.functions.reduce.HighestSeverityEntity)14 Transactional (org.springframework.transaction.annotation.Transactional)10 IPServiceEdgeEntity (org.opennms.netmgt.bsm.persistence.api.IPServiceEdgeEntity)9 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)9 BusinessServiceEdgeEntity (org.opennms.netmgt.bsm.persistence.api.BusinessServiceEdgeEntity)8 Criteria (org.opennms.core.criteria.Criteria)7 BusinessServiceChildEdgeEntity (org.opennms.netmgt.bsm.persistence.api.BusinessServiceChildEdgeEntity)7 BusinessService (org.opennms.netmgt.bsm.service.model.BusinessService)7 NoSuchElementException (java.util.NoSuchElementException)6 Set (java.util.Set)6 IpServiceEdgeImpl (org.opennms.netmgt.bsm.service.internal.edge.IpServiceEdgeImpl)6 OnmsNode (org.opennms.netmgt.model.OnmsNode)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 BusinessServiceDao (org.opennms.netmgt.bsm.persistence.api.BusinessServiceDao)5 SingleReductionKeyEdgeEntity (org.opennms.netmgt.bsm.persistence.api.SingleReductionKeyEdgeEntity)5