@Test public void testSendTraps_v1() throws Exception { AmbariSNMPDispatcher.SnmpVersion snmpVersion = AmbariSNMPDispatcher.SnmpVersion.SNMPv1; Snmp snmp = mock(Snmp.class); AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); Map<String, String> properties = new HashMap<String, String>(); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); notification.DispatchProperties = properties; Recipient rec1 = new Recipient(); rec1.Identifier = "192.168.0.2"; notification.Recipients = Arrays.asList(rec1); doReturn(trap).when(dispatcher).prepareTrap(notification, snmpVersion); dispatcher.sendTraps(notification, snmpVersion); ArgumentCaptor<Target> argument = ArgumentCaptor.forClass(Target.class); verify(snmp, times(1)).send(eq(trap), argument.capture()); assertEquals("192.168.0.2/162", argument.getValue().getAddress().toString()); assertEquals(SnmpConstants.version1, argument.getValue().getVersion()); }
protected PDU prepareTrap(Notification notification, SnmpVersion snmpVersion) throws InvalidSnmpConfigurationException { AlertNotification alertNotification; PDU pdu = DefaultPDUFactory.createPDU(snmpVersion.getTargetVersion()); if (Notification.Type.ALERT.equals(notification.getType())) { try { alertNotification = (AlertNotification) notification; } catch (ClassCastException e) { LOG.error("Notification wasn't casted to AlertNotification. Returning empty Protocol data unit", e); return pdu; } } else { LOG.error("Notification for AmbariSNMPDispatcher should be of type AlertNotification, but it wasn't. Returning empty Protocol data unit"); return pdu; } pdu.setType(snmpVersion.getTrapType()); pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AMBARI_ALERT_TRAP_OID))); AlertNoticeDispatchService.AlertInfo alertInfo = alertNotification.getAlertInfo(); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_ID_OID, new BigDecimal(alertInfo.getAlertDefinitionId()).intValueExact()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_NAME_OID, alertInfo.getAlertDefinition().getDefinitionName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_HASH_OID, alertInfo.getAlertDefinitionHash()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_NAME_OID, alertInfo.getAlertName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_TEXT_OID, alertInfo.getAlertText()); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_STATE_OID, alertInfo.getAlertState().getIntValue()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_HOST_NAME_OID, alertInfo.getHostName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_SERVICE_NAME_OID, alertInfo.getServiceName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_COMPONENT_NAME_OID, alertInfo.getComponentName()); return pdu; }
AmbariSNMPDispatcher extends SNMPDispatcher { protected PDU prepareTrap(Notification notification, SnmpVersion snmpVersion) throws InvalidSnmpConfigurationException { AlertNotification alertNotification; PDU pdu = DefaultPDUFactory.createPDU(snmpVersion.getTargetVersion()); if (Notification.Type.ALERT.equals(notification.getType())) { try { alertNotification = (AlertNotification) notification; } catch (ClassCastException e) { LOG.error("Notification wasn't casted to AlertNotification. Returning empty Protocol data unit", e); return pdu; } } else { LOG.error("Notification for AmbariSNMPDispatcher should be of type AlertNotification, but it wasn't. Returning empty Protocol data unit"); return pdu; } pdu.setType(snmpVersion.getTrapType()); pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AMBARI_ALERT_TRAP_OID))); AlertNoticeDispatchService.AlertInfo alertInfo = alertNotification.getAlertInfo(); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_ID_OID, new BigDecimal(alertInfo.getAlertDefinitionId()).intValueExact()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_NAME_OID, alertInfo.getAlertDefinition().getDefinitionName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_HASH_OID, alertInfo.getAlertDefinitionHash()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_NAME_OID, alertInfo.getAlertName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_TEXT_OID, alertInfo.getAlertText()); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_STATE_OID, alertInfo.getAlertState().getIntValue()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_HOST_NAME_OID, alertInfo.getHostName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_SERVICE_NAME_OID, alertInfo.getServiceName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_COMPONENT_NAME_OID, alertInfo.getComponentName()); return pdu; } }
AmbariSNMPDispatcher extends SNMPDispatcher { protected PDU prepareTrap(Notification notification, SnmpVersion snmpVersion) throws InvalidSnmpConfigurationException { AlertNotification alertNotification; PDU pdu = DefaultPDUFactory.createPDU(snmpVersion.getTargetVersion()); if (Notification.Type.ALERT.equals(notification.getType())) { try { alertNotification = (AlertNotification) notification; } catch (ClassCastException e) { LOG.error("Notification wasn't casted to AlertNotification. Returning empty Protocol data unit", e); return pdu; } } else { LOG.error("Notification for AmbariSNMPDispatcher should be of type AlertNotification, but it wasn't. Returning empty Protocol data unit"); return pdu; } pdu.setType(snmpVersion.getTrapType()); pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AMBARI_ALERT_TRAP_OID))); AlertNoticeDispatchService.AlertInfo alertInfo = alertNotification.getAlertInfo(); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_ID_OID, new BigDecimal(alertInfo.getAlertDefinitionId()).intValueExact()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_NAME_OID, alertInfo.getAlertDefinition().getDefinitionName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_HASH_OID, alertInfo.getAlertDefinitionHash()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_NAME_OID, alertInfo.getAlertName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_TEXT_OID, alertInfo.getAlertText()); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_STATE_OID, alertInfo.getAlertState().getIntValue()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_HOST_NAME_OID, alertInfo.getHostName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_SERVICE_NAME_OID, alertInfo.getServiceName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_COMPONENT_NAME_OID, alertInfo.getComponentName()); return pdu; } protected  AmbariSNMPDispatcher(Snmp snmp);  AmbariSNMPDispatcher(Integer port); }
AmbariSNMPDispatcher extends SNMPDispatcher { protected PDU prepareTrap(Notification notification, SnmpVersion snmpVersion) throws InvalidSnmpConfigurationException { AlertNotification alertNotification; PDU pdu = DefaultPDUFactory.createPDU(snmpVersion.getTargetVersion()); if (Notification.Type.ALERT.equals(notification.getType())) { try { alertNotification = (AlertNotification) notification; } catch (ClassCastException e) { LOG.error("Notification wasn't casted to AlertNotification. Returning empty Protocol data unit", e); return pdu; } } else { LOG.error("Notification for AmbariSNMPDispatcher should be of type AlertNotification, but it wasn't. Returning empty Protocol data unit"); return pdu; } pdu.setType(snmpVersion.getTrapType()); pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AMBARI_ALERT_TRAP_OID))); AlertNoticeDispatchService.AlertInfo alertInfo = alertNotification.getAlertInfo(); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_ID_OID, new BigDecimal(alertInfo.getAlertDefinitionId()).intValueExact()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_NAME_OID, alertInfo.getAlertDefinition().getDefinitionName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_HASH_OID, alertInfo.getAlertDefinitionHash()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_NAME_OID, alertInfo.getAlertName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_TEXT_OID, alertInfo.getAlertText()); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_STATE_OID, alertInfo.getAlertState().getIntValue()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_HOST_NAME_OID, alertInfo.getHostName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_SERVICE_NAME_OID, alertInfo.getServiceName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_COMPONENT_NAME_OID, alertInfo.getComponentName()); return pdu; } protected  AmbariSNMPDispatcher(Snmp snmp);  AmbariSNMPDispatcher(Integer port); @Override String getType(); }
AmbariSNMPDispatcher extends SNMPDispatcher { protected PDU prepareTrap(Notification notification, SnmpVersion snmpVersion) throws InvalidSnmpConfigurationException { AlertNotification alertNotification; PDU pdu = DefaultPDUFactory.createPDU(snmpVersion.getTargetVersion()); if (Notification.Type.ALERT.equals(notification.getType())) { try { alertNotification = (AlertNotification) notification; } catch (ClassCastException e) { LOG.error("Notification wasn't casted to AlertNotification. Returning empty Protocol data unit", e); return pdu; } } else { LOG.error("Notification for AmbariSNMPDispatcher should be of type AlertNotification, but it wasn't. Returning empty Protocol data unit"); return pdu; } pdu.setType(snmpVersion.getTrapType()); pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AMBARI_ALERT_TRAP_OID))); AlertNoticeDispatchService.AlertInfo alertInfo = alertNotification.getAlertInfo(); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_ID_OID, new BigDecimal(alertInfo.getAlertDefinitionId()).intValueExact()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_NAME_OID, alertInfo.getAlertDefinition().getDefinitionName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_HASH_OID, alertInfo.getAlertDefinitionHash()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_NAME_OID, alertInfo.getAlertName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_TEXT_OID, alertInfo.getAlertText()); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_STATE_OID, alertInfo.getAlertState().getIntValue()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_HOST_NAME_OID, alertInfo.getHostName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_SERVICE_NAME_OID, alertInfo.getServiceName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_COMPONENT_NAME_OID, alertInfo.getComponentName()); return pdu; } protected  AmbariSNMPDispatcher(Snmp snmp);  AmbariSNMPDispatcher(Integer port); @Override String getType(); static final String BASE_AMBARI_OID; static final String APACHE_AMBARI_TRAPS_OID; static final String AMBARI_ALERT_TRAP_OID; static final String AMBARI_ALERTS_OID; static final String AMBARI_ALERT_TABLE_OID; static final String AMBARI_ALERT_ENTRY_OID; static final String AMBARI_ALERT_DEFINITION_ID_OID; static final String AMBARI_ALERT_DEFINITION_NAME_OID; static final String AMBARI_ALERT_DEFINITION_HASH_OID; static final String AMBARI_ALERT_NAME_OID; static final String AMBARI_ALERT_TEXT_OID; static final String AMBARI_ALERT_STATE_OID; static final String AMBARI_ALERT_HOST_NAME_OID; static final String AMBARI_ALERT_SERVICE_NAME_OID; static final String AMBARI_ALERT_COMPONENT_NAME_OID; }