SNMP4J的测试代码

基本功能的测试,Snmp类的封装有些怪异,不看源码会写错。

import java.io.IOException;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SnmpTest {

	private final static Logger LOG = LoggerFactory.getLogger(SnmpTest.class);
	private static Snmp snmp;
	private static CommunityTarget target1;
	private static CommunityTarget target2;

	@BeforeClass
	public static void init() throws IOException {
		// SNMP echo agent at port 50162
		// snmpListen = new Snmp(new DefaultUdpTransportMapping(new
		// UdpAddress(50162), true));
		snmp = new Snmp(new DefaultUdpTransportMapping());
		snmp.listen();
		CommandResponder listener = new CommandResponder() {
			@Override
			public void processPdu(CommandResponderEvent event) {
				LOG.info(event.getTransportMapping().getListenAddress() + " Get request: {}", event.getPDU());
				PDU pdu = new PDU(event.getPDU()); // echo
				pdu.setType(PDU.RESPONSE);
				pdu.setRequestID(event.getPDU().getRequestID());
				CommunityTarget respTarget = new CommunityTarget(event.getPeerAddress(), new OctetString("public"));
				// respTarget.setRetries(2);
				// respTarget.setTimeout(1500);
				// respTarget.setVersion(SnmpConstants.version2c);
				try { // send an empty response PDU
					snmp.send(pdu, respTarget);
				} catch (IOException e) {
					LOG.error(null, e);
				}
			}
		};
		snmp.addNotificationListener(new UdpAddress(50162), listener);
		snmp.addNotificationListener(new UdpAddress(50168), listener);

		// session to send
		target1 = new CommunityTarget(UdpAddress.parse("localhost/50162"), new OctetString("public"));
		// target.setRetries(2);
		// target.setTimeout(1500);
		target1.setVersion(SnmpConstants.version2c); // support getBulk
		target2 = new CommunityTarget(UdpAddress.parse("localhost/50168"), new OctetString("public"));
	}

	@AfterClass
	public static void destroy() throws IOException {
		snmp.close();
	}

	@Test
	public void testSnmpSend() throws InterruptedException, IOException {
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1 })));
		checkResponse(snmp.get(pdu, target1));
		checkResponse(snmp.getNext(pdu, target1));
		checkResponse(snmp.getBulk(pdu, target1));
		checkResponse(snmp.set(pdu, target1));
		checkResponse(snmp.inform(pdu, target1));
		pdu.setType(PDU.TRAP);
		snmp.send(pdu, target1);
	}

	@Test
	public void testSnmpSendAsync() throws Exception {
		// creating PDU
		final PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1 })));
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 2 })));
		pdu.setType(PDU.GETNEXT);
		// sending request
		final ResponseListener listener = new ResponseListener() {
			public void onResponse(ResponseEvent event) {
				// important!!!
				((Snmp) event.getSource()).cancel(event.getRequest(), this);
				checkResponse(event);
				synchronized (pdu) {
					pdu.notifyAll();
				}
			}
		};
		snmp.getNext(pdu, target2, null, listener);
		synchronized (pdu) {
			pdu.wait(5000);
		}
	}

	private void checkResponse(ResponseEvent event) {
		if (event == null) {
			Assert.fail("response timeout");
		} else if (event.getError() != null) {
			LOG.error("Get response error", event.getError());
			Assert.fail("failed: " + event.getError().getLocalizedMessage());
		} else if (event.getResponse() == null) {
			Assert.fail("response timeout");
		} else
			LOG.info("Get response {}: {}", Thread.currentThread(), event.getResponse());
	}

	@Test
	public void testTable() {
		// TableUtils util = new TableUtils(snmp,new DefaultPDUFactory());
		// OID oidIndex;
		// OID oidStatus;
		// VariableBinding[] values;
		// util.createRow(target1, oidStatus, oidIndex, values);
		// util.destroyRow(target1, oidIndex, oidIndex);
		// OID[] columnOIDs;
		// util.getTable(target1, columnOIDs, null, oidIndex);
	}
}