Search in sources :

Example 6 with BusinessServiceChildEdgeEntity

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

use of org.opennms.netmgt.bsm.persistence.api.BusinessServiceChildEdgeEntity 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)

Aggregations

BusinessServiceChildEdgeEntity (org.opennms.netmgt.bsm.persistence.api.BusinessServiceChildEdgeEntity)7 BusinessServiceEntity (org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity)5 IPServiceEdgeEntity (org.opennms.netmgt.bsm.persistence.api.IPServiceEdgeEntity)4 SingleReductionKeyEdgeEntity (org.opennms.netmgt.bsm.persistence.api.SingleReductionKeyEdgeEntity)4 ChildEdgeImpl (org.opennms.netmgt.bsm.service.internal.edge.ChildEdgeImpl)4 BusinessServiceEdgeEntity (org.opennms.netmgt.bsm.persistence.api.BusinessServiceEdgeEntity)3 EdgeEntityVisitor (org.opennms.netmgt.bsm.persistence.api.EdgeEntityVisitor)3 IpServiceEdgeImpl (org.opennms.netmgt.bsm.service.internal.edge.IpServiceEdgeImpl)3 ReductionKeyEdgeImpl (org.opennms.netmgt.bsm.service.internal.edge.ReductionKeyEdgeImpl)3 List (java.util.List)2 NoSuchElementException (java.util.NoSuchElementException)2 Objects (java.util.Objects)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 Criteria (org.opennms.core.criteria.Criteria)2 BusinessServiceDao (org.opennms.netmgt.bsm.persistence.api.BusinessServiceDao)2 BusinessServiceEdgeDao (org.opennms.netmgt.bsm.persistence.api.BusinessServiceEdgeDao)2 AbstractMapFunctionEntity (org.opennms.netmgt.bsm.persistence.api.functions.map.AbstractMapFunctionEntity)2 IdentityEntity (org.opennms.netmgt.bsm.persistence.api.functions.map.IdentityEntity)2