Search in sources :

Example 21 with SnmpValue

use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.

the class SnmpValueTest method testSnmpObjId.

@Test
public void testSnmpObjId() {
    for (final SnmpValueFactory factory : m_factories) {
        final String oid = ".1.3.6.1.4.1.2925.4.5.2.1.1";
        final SnmpObjId id = SnmpObjId.get(oid);
        final SnmpValue value = factory.getObjectId(id);
        final String className = factory.getClass().getName();
        assertTrue(className + ": getInetAddress isDisplayable should be true", value.isDisplayable());
        assertEquals(className + ": getObjectId to SnmpObjId should return " + oid, id, value.toSnmpObjId());
        assertEquals(className + ": getObjectId to String should return " + oid, oid, value.toString());
        assertEquals(className + ": getObjectId to DisplayString should return " + oid, oid, value.toDisplayString());
        try {
            value.toInt();
            fail(className + ": getObjectId to int should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
        try {
            value.toLong();
            fail(className + ": getObjectId to long should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
        try {
            value.toBigInteger();
            fail(className + ": getObjectId to BigInteger should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
        try {
            value.toHexString();
            fail(className + ": getObjectId to HexString should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
        try {
            value.toInetAddress();
            fail(className + ": getObjectId to InetAddress should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException e) {
        /* expected */
        }
    }
}
Also used : SnmpValueFactory(org.opennms.netmgt.snmp.SnmpValueFactory) JoeSnmpValueFactory(org.opennms.netmgt.snmp.joesnmp.JoeSnmpValueFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) Test(org.junit.Test)

Example 22 with SnmpValue

use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.

the class EventConstants method toString.

//
// For Trapd
//
/**
     * Converts the value of the instance to a string representation in the
     * correct encoding system.
     *
     * @param encoding a {@link java.lang.String} object.
     * @param value a {@link java.lang.Object} object.
     * @return a {@link java.lang.String} object.
     */
public static String toString(String encoding, Object value) {
    if (encoding == null || value == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    String result = null;
    if (XML_ENCODING_TEXT.equals(encoding)) {
        if (value instanceof String)
            result = (String) value;
        else if (value instanceof Number)
            result = value.toString();
        else if (value instanceof SnmpValue)
            result = ((SnmpValue) value).toString();
    } else if (XML_ENCODING_BASE64.equals(encoding)) {
        if (value instanceof String)
            result = new String(Base64.encodeBase64(((String) value).getBytes()));
        else if (value instanceof Number) {
            byte[] ibuf = null;
            if (value instanceof BigInteger)
                ibuf = ((BigInteger) value).toByteArray();
            else
                ibuf = BigInteger.valueOf(((Number) value).longValue()).toByteArray();
            result = new String(Base64.encodeBase64(ibuf));
        } else if (value instanceof SnmpValue) {
            SnmpValue snmpValue = (SnmpValue) value;
            result = new String(Base64.encodeBase64(snmpValue.getBytes()));
        }
    } else if (XML_ENCODING_MAC_ADDRESS.equals(encoding)) {
        if (value instanceof SnmpValue) {
            SnmpValue snmpValue = (SnmpValue) value;
            StringBuffer macAddress = new StringBuffer();
            byte[] bytes = snmpValue.getBytes();
            for (int i = 0; i < bytes.length; i++) {
                if (i > 0)
                    macAddress.append(":");
                macAddress.append(String.format("%02X", bytes[i]));
            }
            result = macAddress.toString();
        }
    }
    if (result == null)
        throw new IllegalArgumentException("unable to encode " + value + " of type " + value.getClass());
    return result;
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) BigInteger(java.math.BigInteger)

Example 23 with SnmpValue

use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.

the class HibernateEventWriterIT method testWriteEventWithNull.

/**
     * Tests writing nulls to postgres db and the db encoding.
     * @throws SQLException
     */
@Test
public void testWriteEventWithNull() throws Exception {
    EventBuilder bldr = new EventBuilder("testUei", "testSource");
    bldr.setLogDest(HibernateEventWriter.LOG_MSG_DEST_LOG_AND_DISPLAY);
    bldr.addParam("test", "testVal");
    final String testVal2 = "valWithNull";
    bldr.addParam("test2", testVal2);
    byte[] bytes = new byte[] { 0x07, (byte) 0xD7, 0x04, 0x0A, 0x01, 0x17, 0x06, 0x00, 0x2B, 0x00, 0x00 };
    SnmpValue snmpVal = SnmpUtils.getValueFactory().getOctetString(bytes);
    assertFalse(snmpVal.isDisplayable());
    bldr.addParam("test3", snmpVal.toString());
    String b64 = EventConstants.toString(EventConstants.XML_ENCODING_BASE64, snmpVal);
    bldr.addParam("test", b64);
    Event event = bldr.getEvent();
    assertEquals(new Integer(0), event.getDbid());
    m_eventWriter.process(bldr.getLog());
    assertTrue(event.getDbid() > 0);
    final String parms = jdbcTemplate.queryForObject("SELECT eventParms FROM events LIMIT 1", String.class);
    assertEquals("test=testVal(string,text);test2=valWith%0Null%0(string,text);test3=" + snmpVal.toString() + "(string,text);test=B9cECgEXBgArAAA%61(string,text)", parms);
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Event(org.opennms.netmgt.xml.event.Event) Test(org.junit.Test)

Example 24 with SnmpValue

use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.

the class SnmpMonitorStrategyTest method testErrorConditions.

@Test
public void testErrorConditions() {
    SnmpValue result = int32Value(1);
    ThrowableAnticipator ta = new ThrowableAnticipator();
    ta.anticipate(new IllegalArgumentException("operator X is unknown"));
    try {
        monitor.meetsCriteria(result, "X", "123");
    } catch (Throwable t) {
        ta.throwableReceived(t);
    }
    ta.verifyAnticipated();
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) ThrowableAnticipator(org.opennms.test.ThrowableAnticipator) Test(org.junit.Test)

Example 25 with SnmpValue

use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.

the class SnmpMonitorStrategyTest method testMeetsCriteriaWithObjectID.

@Test
public void testMeetsCriteriaWithObjectID() {
    SnmpValue result = oid(".1.2.3.4.5.6.7.8.9");
    testSyntaxEquals(result, ".1.2.3.4.5.6.7.8.9", "..1.2.3.4.5.6.7.8.9");
    testSyntaxMatches(result, "\\.7\\.", "\\.11\\.");
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Test(org.junit.Test)

Aggregations

SnmpValue (org.opennms.netmgt.snmp.SnmpValue)112 Test (org.junit.Test)57 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)47 SnmpValueFactory (org.opennms.netmgt.snmp.SnmpValueFactory)24 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)21 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)20 InetAddress (java.net.InetAddress)19 Map (java.util.Map)14 PollStatus (org.opennms.netmgt.poller.PollStatus)14 JoeSnmpValueFactory (org.opennms.netmgt.snmp.joesnmp.JoeSnmpValueFactory)12 ArrayList (java.util.ArrayList)11 ParameterMap (org.opennms.core.utils.ParameterMap)9 Parm (org.opennms.netmgt.xml.event.Parm)9 PDU (org.snmp4j.PDU)9 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)8 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)8 LinkedHashMap (java.util.LinkedHashMap)7 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)5 HashMap (java.util.HashMap)4 NumericAttributeType (org.opennms.netmgt.collectd.NumericAttributeType)4