Search in sources :

Example 1 with LoggingStateChangeHandler

use of org.opennms.netmgt.bsm.test.LoggingStateChangeHandler in project opennms by OpenNMS.

the class DefaultBusinessServiceStateMachineIT method canMaintainStateForBambooHierarchy.

@Test
public void canMaintainStateForBambooHierarchy() {
    // Setup the the test hierarchy
    BambooTestHierarchy testSpecification = new BambooTestHierarchy();
    testSpecification.getServices().forEach(entity -> businessServiceDao.save(entity));
    // Setup the State Machine
    final DefaultBusinessServiceStateMachine stateMachine = new DefaultBusinessServiceStateMachine();
    stateMachine.setBusinessServices(testSpecification.getServices().stream().map(s -> wrap(s)).collect(Collectors.toList()));
    LoggingStateChangeHandler handler = new LoggingStateChangeHandler();
    stateMachine.addHandler(handler, null);
    // Verify the initial state
    assertEquals(Status.NORMAL, stateMachine.getOperationalStatus(wrap(testSpecification.getMasterService())));
    assertEquals(Status.NORMAL, stateMachine.getOperationalStatus(wrap(testSpecification.getAgentsService())));
    assertEquals(Status.NORMAL, stateMachine.getOperationalStatus(wrap(testSpecification.getBambooService())));
    // Send alarms to the state machine
    // Business Service "Master"
    stateMachine.handleNewOrUpdatedAlarm(createAlarmWrapper("uei.opennms.org/dummy", OnmsSeverity.INDETERMINATE, HTTP_8085_BAMBOO_REDUCTION_KEY));
    // no state change
    assertEquals(0, handler.getStateChanges().size());
    stateMachine.handleNewOrUpdatedAlarm(createAlarmWrapper("uei.opennms.org/dummy", OnmsSeverity.WARNING, DISK_USAGE_THRESHOLD_BAMBO_REDUCTION_KEY));
    // "Master" and "Bamboo" changed
    assertEquals(2, handler.getStateChanges().size());
    // Business Service "Agents"
    stateMachine.handleNewOrUpdatedAlarm(createAlarmWrapper("uei.opennms.org/dummy", OnmsSeverity.MINOR, BAMBOO_AGENT_DUKE_REDUCTION_KEY));
    // no state change (threshold not met)
    assertEquals(2, handler.getStateChanges().size());
    stateMachine.handleNewOrUpdatedAlarm(createAlarmWrapper("uei.opennms.org/dummy", OnmsSeverity.NORMAL, BAMBOO_AGENT_NCSTATE_REDUCTION_KEY));
    // no state change (threshold not met)
    assertEquals(2, handler.getStateChanges().size());
    stateMachine.handleNewOrUpdatedAlarm(createAlarmWrapper("uei.opennms.org/dummy", OnmsSeverity.MAJOR, BAMBOO_AGENT_CAROLINA_REDUCTION_KEY));
    // state change (threshold met) for "Agents" and "Bamboo"
    assertEquals(4, handler.getStateChanges().size());
    // Verify the updated state
    // Business Service "Master"
    assertEquals(Status.WARNING, stateMachine.getOperationalStatus(wrap(testSpecification.getMasterService())));
    // Business Service "Agents"
    assertEquals(Status.MAJOR, stateMachine.getOperationalStatus(wrap(testSpecification.getAgentsService())));
    // Business Service "Bamboo" (root)
    assertEquals(Status.MAJOR, stateMachine.getOperationalStatus(wrap(testSpecification.getBambooService())));
}
Also used : BambooTestHierarchy(org.opennms.netmgt.bsm.test.hierarchies.BambooTestHierarchy) LoggingStateChangeHandler(org.opennms.netmgt.bsm.test.LoggingStateChangeHandler) Test(org.junit.Test)

Example 2 with LoggingStateChangeHandler

use of org.opennms.netmgt.bsm.test.LoggingStateChangeHandler 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)

Example 3 with LoggingStateChangeHandler

use of org.opennms.netmgt.bsm.test.LoggingStateChangeHandler in project opennms by OpenNMS.

the class DefaultBusinessServiceStateMachineTest method canReloadTheStateMachineWhilePreservingState.

@Test
public void canReloadTheStateMachineWhilePreservingState() {
    // Create a simple hierarchy
    MockBusinessServiceHierarchy h = MockBusinessServiceHierarchy.builder().withBusinessService(1).withReductionKey(1, "a1").commit().build();
    BusinessService b1 = h.getBusinessServiceById(1);
    // Setup the state machine
    BusinessServiceStateMachine stateMachine = new DefaultBusinessServiceStateMachine();
    LoggingStateChangeHandler stateChangeHandler = new LoggingStateChangeHandler();
    stateMachine.addHandler(stateChangeHandler, Maps.newHashMap());
    stateMachine.setBusinessServices(h.getBusinessServices());
    // Verify the initial state
    assertEquals(Status.NORMAL, stateMachine.getOperationalStatus(b1));
    assertEquals(0, stateChangeHandler.getStateChanges().size());
    // Send an alarm and verify the updated state
    stateMachine.handleNewOrUpdatedAlarm(new MockAlarmWrapper("a1", Status.CRITICAL));
    assertEquals(Status.CRITICAL, stateMachine.getOperationalStatus(b1));
    assertEquals(1, stateChangeHandler.getStateChanges().size());
    // Now reload the state machine without making any changes to the hierarchies
    stateMachine.setBusinessServices(h.getBusinessServices());
    // The original status should remain
    assertEquals(Status.CRITICAL, stateMachine.getOperationalStatus(b1));
    // No additional state changes events should have been generated
    assertEquals(1, stateChangeHandler.getStateChanges().size());
}
Also used : BusinessService(org.opennms.netmgt.bsm.service.model.BusinessService) BusinessServiceStateMachine(org.opennms.netmgt.bsm.service.BusinessServiceStateMachine) MockBusinessServiceHierarchy(org.opennms.netmgt.bsm.mock.MockBusinessServiceHierarchy) LoggingStateChangeHandler(org.opennms.netmgt.bsm.test.LoggingStateChangeHandler) MockAlarmWrapper(org.opennms.netmgt.bsm.mock.MockAlarmWrapper) Test(org.junit.Test)

Example 4 with LoggingStateChangeHandler

use of org.opennms.netmgt.bsm.test.LoggingStateChangeHandler in project opennms by OpenNMS.

the class DefaultBusinessServiceStateMachineIT method canMaintainStateForSimpleHierarchy.

@Test
public void canMaintainStateForSimpleHierarchy() {
    // Setup the the test hierarchy
    SimpleTestHierarchy testHierarchy = new SimpleTestHierarchy(populator);
    testHierarchy.getServices().forEach(entity -> businessServiceDao.save(entity));
    BusinessServiceImpl bsChild1 = wrap(testHierarchy.getChild1());
    BusinessServiceImpl bsChild2 = wrap(testHierarchy.getChild2());
    BusinessServiceImpl bsParent = wrap(testHierarchy.getRoot());
    IpServiceEdge svc1 = wrap(testHierarchy.getServiceChild1());
    IpServiceEdge svc2 = wrap(testHierarchy.getServiceChild2());
    // Manually add a reduction key to a business service to verify that this also works
    bsChild1.addReductionKeyEdge("explicitReductionKey", new Identity(), Edge.DEFAULT_WEIGHT, null);
    businessServiceDao.flush();
    // Setup the state machine
    List<BusinessService> bss = Lists.newArrayList(bsChild1, bsChild2, bsParent);
    LoggingStateChangeHandler handler = new LoggingStateChangeHandler();
    DefaultBusinessServiceStateMachine stateMachine = new DefaultBusinessServiceStateMachine();
    stateMachine.setBusinessServices(bss);
    stateMachine.addHandler(handler, null);
    // Verify the initial state
    assertEquals(0, handler.getStateChanges().size());
    for (BusinessService eachBs : bss) {
        assertEquals(DefaultBusinessServiceStateMachine.MIN_SEVERITY, stateMachine.getOperationalStatus(eachBs));
    }
    // Pass alarm to the state machine
    stateMachine.handleNewOrUpdatedAlarm(createAlarmWrapper(testHierarchy.getServiceChild1().getIpService(), OnmsSeverity.MINOR));
    // Verify the updated state
    assertEquals(2, handler.getStateChanges().size());
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(svc1));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(bsChild1));
    assertEquals(DefaultBusinessServiceStateMachine.MIN_SEVERITY, stateMachine.getOperationalStatus(svc2));
    assertEquals(DefaultBusinessServiceStateMachine.MIN_SEVERITY, stateMachine.getOperationalStatus(bsChild2));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(bsParent));
    // Verify that hierarchy works
    stateMachine.handleNewOrUpdatedAlarm(createAlarmWrapper(testHierarchy.getServiceChild2().getIpService(), OnmsSeverity.MAJOR));
    assertEquals(4, handler.getStateChanges().size());
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(svc1));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(bsChild1));
    assertEquals(Status.MAJOR, stateMachine.getOperationalStatus(svc2));
    assertEquals(Status.MAJOR, stateMachine.getOperationalStatus(bsChild2));
    assertEquals(Status.MAJOR, stateMachine.getOperationalStatus(bsParent));
    // Verify that explicit reductionKeys work as well
    AlarmWrapper alarmWrapper = createAlarmWrapper(EventConstants.NODE_LOST_SERVICE_EVENT_UEI, OnmsSeverity.CRITICAL, "explicitReductionKey");
    stateMachine.handleNewOrUpdatedAlarm(alarmWrapper);
    assertEquals(6, handler.getStateChanges().size());
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(svc1));
    assertEquals(Status.MAJOR, stateMachine.getOperationalStatus(svc2));
    assertEquals(Status.CRITICAL, stateMachine.getOperationalStatus(bsChild1));
    assertEquals(Status.MAJOR, stateMachine.getOperationalStatus(bsChild2));
    assertEquals(Status.CRITICAL, stateMachine.getOperationalStatus(bsParent));
}
Also used : BusinessService(org.opennms.netmgt.bsm.service.model.BusinessService) SimpleTestHierarchy(org.opennms.netmgt.bsm.test.hierarchies.SimpleTestHierarchy) AlarmWrapper(org.opennms.netmgt.bsm.service.model.AlarmWrapper) BsmTestUtils.createAlarmWrapper(org.opennms.netmgt.bsm.test.BsmTestUtils.createAlarmWrapper) LoggingStateChangeHandler(org.opennms.netmgt.bsm.test.LoggingStateChangeHandler) IpServiceEdge(org.opennms.netmgt.bsm.service.model.edge.IpServiceEdge) Identity(org.opennms.netmgt.bsm.service.model.functions.map.Identity) Test(org.junit.Test)

Example 5 with LoggingStateChangeHandler

use of org.opennms.netmgt.bsm.test.LoggingStateChangeHandler in project opennms by OpenNMS.

the class DefaultBusinessServiceStateMachineIT method canShareIpService.

/**
 * Verifies that two Business Services can share the same Ip Service and both Business Services will change their
 * status when the IP Service changes its status.
 */
@Test
public void canShareIpService() {
    BusinessServicesShareIpServiceHierarchy testHierarchy = new BusinessServicesShareIpServiceHierarchy(populator);
    testHierarchy.getServices().forEach(entity -> businessServiceDao.save(entity));
    businessServiceDao.flush();
    BusinessServiceImpl bsChild1 = wrap(testHierarchy.getChild1());
    BusinessServiceImpl bsChild2 = wrap(testHierarchy.getChild2());
    BusinessServiceImpl bsParent = wrap(testHierarchy.getRoot());
    IpServiceEdge svc1 = wrap(testHierarchy.getServiceChild1());
    IpServiceEdge svc2 = wrap(testHierarchy.getServiceChild2());
    // Setup the state machine
    List<BusinessService> bss = Lists.newArrayList(bsChild1, bsChild2, bsParent);
    LoggingStateChangeHandler handler = new LoggingStateChangeHandler();
    DefaultBusinessServiceStateMachine stateMachine = new DefaultBusinessServiceStateMachine();
    stateMachine.setBusinessServices(bss);
    stateMachine.addHandler(handler, null);
    // Verify the initial state
    assertEquals(0, handler.getStateChanges().size());
    for (BusinessService eachBs : bss) {
        assertEquals(DefaultBusinessServiceStateMachine.MIN_SEVERITY, stateMachine.getOperationalStatus(eachBs));
    }
    // Pass alarm to the state machine
    stateMachine.handleNewOrUpdatedAlarm(createAlarmWrapper(testHierarchy.getServiceChild1().getIpService(), OnmsSeverity.MINOR));
    // Verify the updated state
    assertEquals(3, handler.getStateChanges().size());
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(svc1));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(bsChild1));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(svc2));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(bsChild2));
    assertEquals(Status.MINOR, stateMachine.getOperationalStatus(bsParent));
}
Also used : BusinessServicesShareIpServiceHierarchy(org.opennms.netmgt.bsm.test.hierarchies.BusinessServicesShareIpServiceHierarchy) BusinessService(org.opennms.netmgt.bsm.service.model.BusinessService) LoggingStateChangeHandler(org.opennms.netmgt.bsm.test.LoggingStateChangeHandler) IpServiceEdge(org.opennms.netmgt.bsm.service.model.edge.IpServiceEdge) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 LoggingStateChangeHandler (org.opennms.netmgt.bsm.test.LoggingStateChangeHandler)5 BusinessService (org.opennms.netmgt.bsm.service.model.BusinessService)4 MockAlarmWrapper (org.opennms.netmgt.bsm.mock.MockAlarmWrapper)2 MockBusinessServiceHierarchy (org.opennms.netmgt.bsm.mock.MockBusinessServiceHierarchy)2 IpServiceEdge (org.opennms.netmgt.bsm.service.model.edge.IpServiceEdge)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AlarmProvider (org.opennms.netmgt.bsm.service.AlarmProvider)1 BusinessServiceStateMachine (org.opennms.netmgt.bsm.service.BusinessServiceStateMachine)1 AlarmWrapper (org.opennms.netmgt.bsm.service.model.AlarmWrapper)1 Edge (org.opennms.netmgt.bsm.service.model.edge.Edge)1 Identity (org.opennms.netmgt.bsm.service.model.functions.map.Identity)1 BsmTestUtils.createAlarmWrapper (org.opennms.netmgt.bsm.test.BsmTestUtils.createAlarmWrapper)1 BambooTestHierarchy (org.opennms.netmgt.bsm.test.hierarchies.BambooTestHierarchy)1 BusinessServicesShareIpServiceHierarchy (org.opennms.netmgt.bsm.test.hierarchies.BusinessServicesShareIpServiceHierarchy)1 SimpleTestHierarchy (org.opennms.netmgt.bsm.test.hierarchies.SimpleTestHierarchy)1