use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class Snmp4JStrategyIT method testSendWithGetPduSingleValue.
@Test
public void testSendWithGetPduSingleValue() throws Exception {
SnmpObjId[] oids = new SnmpObjId[] { SnmpObjId.get(".1.3.5.1.1.3.0") };
Snmp4JAgentConfig agentConfig = new Snmp4JAgentConfig(getAgentConfig());
SnmpValue[] retvalues = null;
PDU pdu = m_strategy.buildPdu(agentConfig, PDU.GET, oids, null);
if (pdu != null) {
retvalues = m_strategy.send(agentConfig, pdu, true);
}
SnmpValue[] values = retvalues;
assertNotNull("values should not be null", values);
assertEquals("values list size", 1, values.length);
assertSnmpValueEquals("values[0]", SnmpValue.SNMP_INT32, 42, values[0]);
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class Snmp4JStrategyIT method testSendWithGetNextPduSingleValue.
@Test
public void testSendWithGetNextPduSingleValue() throws Exception {
SnmpObjId[] oids = new SnmpObjId[] { SnmpObjId.get(".1.3.5.1.1.3.0") };
Snmp4JAgentConfig agentConfig = new Snmp4JAgentConfig(getAgentConfig());
SnmpValue[] retvalues = null;
PDU pdu = m_strategy.buildPdu(agentConfig, PDU.GETNEXT, oids, null);
if (pdu != null) {
retvalues = m_strategy.send(agentConfig, pdu, true);
}
SnmpValue[] values = retvalues;
assertNotNull("values should not be null", values);
assertEquals("values list size", 1, values.length);
// Expect the *next* value, so for .1.3.5.1.1.4.0
assertSnmpValueEquals("values[0]", SnmpValue.SNMP_GAUGE32, 42, values[0]);
}
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 */
}
}
}
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 = "valWith\u0000Null\u0000";
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 List<Map<String, Object>> parameters = jdbcTemplate.queryForList("SELECT name, value FROM event_parameters WHERE eventID = " + event.getDbid() + " ORDER BY name");
assertEquals(3, parameters.size());
assertEquals("test", parameters.get(0).get("name"));
assertEquals("B9cECgEXBgArAAA%61", parameters.get(0).get("value"));
assertEquals("test2", parameters.get(1).get("name"));
assertEquals("valWith%0Null%0", parameters.get(1).get("value"));
assertEquals("test3", parameters.get(2).get("name"));
assertEquals(snmpVal.toString(), parameters.get(2).get("value"));
}
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;
final StringBuilder macAddress = new StringBuilder();
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;
}
Aggregations