use of org.snmp4j.PDUv1 in project opennms by OpenNMS.
the class Snmp4JUtils method convertPduToBytes.
/**
* @param address
* @param port
* @param community
* @param pdu
*
* @return Byte array representing the {@link PDU} in either SNMPv1 or SNMPv2
* format, depending on the type of the {@link PDU} object.
*/
public static byte[] convertPduToBytes(InetAddress address, int port, String community, PDU pdu) throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<byte[]> bytes = new AtomicReference<>();
// IP address is optional when using the DummyTransport because
// all requests are sent to the {@link DummyTransportResponder}
final DummyTransport<IpAddress> transport = new DummyTransport<IpAddress>(null);
final AbstractTransportMapping<IpAddress> responder = transport.getResponder(null);
// Add a DummyTransportResponder listener that will receive the raw bytes of the PDU
responder.addTransportListener(new TransportListener() {
@Override
public void processMessage(TransportMapping transport, Address address, ByteBuffer byteBuffer, TransportStateReference state) {
byteBuffer.rewind();
final byte[] byteArray = new byte[byteBuffer.remaining()];
byteBuffer.get(byteArray);
bytes.set(byteArray);
byteBuffer.rewind();
latch.countDown();
}
});
// Create our own MessageDispatcher since we don't need to do all
// of the crypto operations necessary to initialize SNMPv3 which is slow
MessageDispatcher dispatcher = new MessageDispatcherImpl();
dispatcher.addMessageProcessingModel(new MPv1());
dispatcher.addMessageProcessingModel(new MPv2c());
Snmp snmp = new Snmp(dispatcher, responder);
snmp.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
if (pdu instanceof PDUv1) {
target.setVersion(SnmpConstants.version1);
} else {
target.setVersion(SnmpConstants.version2c);
}
target.setAddress(Snmp4JAgentConfig.convertAddress(address, port));
snmp.send(pdu, target, transport);
latch.await();
return bytes.get();
}
use of org.snmp4j.PDUv1 in project opennms by OpenNMS.
the class Snmp4JStrategy method buildAgentConfig.
protected SnmpAgentConfig buildAgentConfig(String address, int port, String community, PDU pdu) throws UnknownHostException {
SnmpAgentConfig config = new SnmpAgentConfig();
config.setAddress(InetAddress.getByName(address));
config.setPort(port);
config.setVersion(pdu instanceof PDUv1 ? SnmpAgentConfig.VERSION1 : SnmpAgentConfig.VERSION2C);
return config;
}
use of org.snmp4j.PDUv1 in project opennms by OpenNMS.
the class MockProxy method processGetNext.
/**
* @param request
* @return
*/
private PDU processGetNext(PDU request) {
PDU response = request;
response.setErrorIndex(0);
response.setErrorStatus(0);
response.setType(PDU.RESPONSE);
Vector<? extends VariableBinding> varBinds = response.getVariableBindings();
for (int i = 0; i < varBinds.size(); i++) {
VariableBinding varBind = varBinds.get(i);
VariableBinding nextVarBind = m_agent.getNext(varBind.getOid());
if (nextVarBind == null) {
if (response instanceof PDUv1) {
if (response.getErrorIndex() == 0) {
response.setErrorIndex(i + 1);
response.setErrorStatus(PDU.noSuchName);
}
} else {
varBind.setVariable(Null.endOfMibView);
}
} else {
response.set(i, nextVarBind);
}
}
return response;
}
use of org.snmp4j.PDUv1 in project opennms by OpenNMS.
the class MockProxy method processGet.
/**
* @param request
* @return
*/
private PDU processGet(PDU request) {
PDU response = request;
response.setErrorIndex(0);
response.setErrorStatus(0);
response.setType(PDU.RESPONSE);
Vector<? extends VariableBinding> varBinds = response.getVariableBindings();
for (int i = 0; i < varBinds.size(); i++) {
VariableBinding varBind = varBinds.get(i);
VariableBinding nextVarBind = m_agent.get(varBind.getOid());
if (nextVarBind == null) {
if (response instanceof PDUv1) {
if (response.getErrorIndex() == 0) {
response.setErrorIndex(i + 1);
response.setErrorStatus(PDU.noSuchName);
}
} else {
varBind.setVariable(Null.endOfMibView);
}
} else {
response.set(i, nextVarBind);
}
}
return response;
}
use of org.snmp4j.PDUv1 in project camel by apache.
the class SnmpConverters method toString.
/**
* Converts the given snmp pdu to a String body.
*
* @param pdu the snmp pdu
* @return the text content
*/
@Converter
public static String toString(PDU pdu) {
// the output buffer
StringBuilder sb = new StringBuilder();
// prepare the header
if (pdu.getType() == PDU.V1TRAP) {
sb.append("<" + SNMP_TAG + " messageType=\"v1\">");
} else {
sb.append(SNMP_TAG_OPEN);
}
// Extract SNMPv1 specific variables
if (pdu.getType() == PDU.V1TRAP) {
PDUv1 v1pdu = (PDUv1) pdu;
entryAppend(sb, "enterprise", v1pdu.getEnterprise().toString());
entryAppend(sb, "agent-addr", v1pdu.getAgentAddress().toString());
entryAppend(sb, "generic-trap", Integer.toString(v1pdu.getGenericTrap()));
entryAppend(sb, "specific-trap", Integer.toString(v1pdu.getSpecificTrap()));
entryAppend(sb, "time-stamp", Long.toString(v1pdu.getTimestamp()));
}
// now loop all variables of the response
for (Object o : pdu.getVariableBindings()) {
VariableBinding b = (VariableBinding) o;
sb.append(ENTRY_TAG_OPEN);
sb.append(OID_TAG_OPEN);
sb.append(b.getOid().toString());
sb.append(OID_TAG_CLOSE);
sb.append(VALUE_TAG_OPEN);
sb.append(StringHelper.xmlEncode(b.getVariable().toString()));
sb.append(VALUE_TAG_CLOSE);
sb.append(ENTRY_TAG_CLOSE);
}
// prepare the footer
sb.append(SNMP_TAG_CLOSE);
return sb.toString();
}
Aggregations