Search in sources :

Example 1 with AlarmProvider

use of org.opennms.netmgt.bsm.service.AlarmProvider 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(1);
        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) ReductionKeyHelper(org.opennms.netmgt.bsm.persistence.api.ReductionKeyHelper) 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) 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 2 with AlarmProvider

use of org.opennms.netmgt.bsm.service.AlarmProvider in project opennms by OpenNMS.

the class DefaultBusinessServiceStateMachineTest method canLookupNewAlarmsWhenReloading.

@Test
public void canLookupNewAlarmsWhenReloading() {
    // Create a simple hierarchy
    MockBusinessServiceHierarchy h = MockBusinessServiceHierarchy.builder().withBusinessService(1).withReductionKey(1, "a1").commit().build();
    BusinessService b1 = h.getBusinessServiceById(1);
    Edge a1 = h.getEdgeByReductionKey("a1");
    // Setup the state machine
    DefaultBusinessServiceStateMachine stateMachine = new DefaultBusinessServiceStateMachine();
    LoggingStateChangeHandler stateChangeHandler = new LoggingStateChangeHandler();
    stateMachine.addHandler(stateChangeHandler, Maps.newHashMap());
    stateMachine.setBusinessServices(h.getBusinessServices());
    stateMachine.setAlarmProvider(new AlarmProvider() {

        @Override
        public Map<String, AlarmWrapper> lookup(Set<String> reductionKeys) {
            if (reductionKeys.contains("a2")) {
                return ImmutableMap.<String, AlarmWrapper>builder().put("a2", new MockAlarmWrapper("a2", Status.CRITICAL)).build();
            }
            return new HashMap<>();
        }
    });
    // Verify the initial state
    assertEquals(Status.NORMAL, stateMachine.getOperationalStatus(b1));
    assertEquals(Status.NORMAL, stateMachine.getOperationalStatus(a1));
    assertEquals(0, stateChangeHandler.getStateChanges().size());
    // Send an alarm and verify the updated state
    stateMachine.handleNewOrUpdatedAlarm(new MockAlarmWrapper("a1", Status.MINOR));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(b1));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(a1));
    assertEquals(1, stateChangeHandler.getStateChanges().size());
    // Update the hierarchy and reload the state machine
    h = MockBusinessServiceHierarchy.builder().withBusinessService(1).withReductionKey(1, "a1").withReductionKey(2, "a2").commit().build();
    stateMachine.setBusinessServices(h.getBusinessServices());
    Edge a2 = h.getEdgeByReductionKey("a2");
    // The state should be upgraded
    assertEquals(Status.CRITICAL, stateMachine.getOperationalStatus(b1));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(a1));
    assertEquals(Status.CRITICAL, stateMachine.getOperationalStatus(a2));
    // One additional state change event should have been generated
    assertEquals(2, stateChangeHandler.getStateChanges().size());
}
Also used : BusinessService(org.opennms.netmgt.bsm.service.model.BusinessService) AlarmProvider(org.opennms.netmgt.bsm.service.AlarmProvider) MockBusinessServiceHierarchy(org.opennms.netmgt.bsm.mock.MockBusinessServiceHierarchy) LoggingStateChangeHandler(org.opennms.netmgt.bsm.test.LoggingStateChangeHandler) Edge(org.opennms.netmgt.bsm.service.model.edge.Edge) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) MockAlarmWrapper(org.opennms.netmgt.bsm.mock.MockAlarmWrapper) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 AlarmProvider (org.opennms.netmgt.bsm.service.AlarmProvider)2 BusinessService (org.opennms.netmgt.bsm.service.model.BusinessService)2 Edge (org.opennms.netmgt.bsm.service.model.edge.Edge)2 LoggingStateChangeHandler (org.opennms.netmgt.bsm.test.LoggingStateChangeHandler)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 After (org.junit.After)1 Assert (org.junit.Assert)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Before (org.junit.Before)1 RunWith (org.junit.runner.RunWith)1 CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)1 BeanUtils (org.opennms.core.spring.BeanUtils)1 OpenNMSJUnit4ClassRunner (org.opennms.core.test.OpenNMSJUnit4ClassRunner)1