Search in sources :

Example 6 with OctetString

use of org.snmp4j.smi.OctetString 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();
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) InetAddress(java.net.InetAddress) TransportMapping(org.snmp4j.TransportMapping) AbstractTransportMapping(org.snmp4j.transport.AbstractTransportMapping) MPv2c(org.snmp4j.mp.MPv2c) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) TransportListener(org.snmp4j.transport.TransportListener) MessageDispatcher(org.snmp4j.MessageDispatcher) TransportStateReference(org.snmp4j.TransportStateReference) Snmp(org.snmp4j.Snmp) IpAddress(org.snmp4j.smi.IpAddress) MPv1(org.snmp4j.mp.MPv1) DummyTransport(org.snmp4j.transport.DummyTransport) PDUv1(org.snmp4j.PDUv1) CommunityTarget(org.snmp4j.CommunityTarget)

Example 7 with OctetString

use of org.snmp4j.smi.OctetString in project opennms by OpenNMS.

the class Snmp4JAgentConfig method createSnmpSession.

public Snmp createSnmpSession() throws IOException {
    final TransportMapping<?> transport = new DefaultUdpTransportMapping();
    final MessageDispatcher disp = new MessageDispatcherImpl();
    final Snmp session;
    // models we need for the specific agent
    if (!isSnmpV3()) {
        disp.addMessageProcessingModel(new MPv1());
        disp.addMessageProcessingModel(new MPv2c());
        session = new Snmp(disp, transport);
    } else {
        // Make a new USM
        final USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        // Add the specified user to the USM
        usm.addUser(getSecurityName(), new UsmUser(getSecurityName(), getAuthProtocol(), getAuthPassPhrase(), getPrivProtocol(), getPrivPassPhrase()));
        disp.addMessageProcessingModel(new MPv3(usm));
        session = new Snmp(disp, transport);
    }
    return session;
}
Also used : OctetString(org.snmp4j.smi.OctetString) MessageDispatcher(org.snmp4j.MessageDispatcher) MPv3(org.snmp4j.mp.MPv3) MPv2c(org.snmp4j.mp.MPv2c) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) MPv1(org.snmp4j.mp.MPv1) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Example 8 with OctetString

use of org.snmp4j.smi.OctetString in project opennms by OpenNMS.

the class Snmp4JAgentConfig method createPdu.

/**
     * Creates an SNMP4J PDU based on the SNMP4J version constants.
     * A v3 request requires a ScopedPDU.
     * 
     * @param type
     * @return
     */
public PDU createPdu(int type) {
    PDU pdu = null;
    if (isSnmpV3()) {
        pdu = new ScopedPDU();
        ScopedPDU scopedPDU = (ScopedPDU) pdu;
        OctetString contextName = getContextName();
        if (contextName != null)
            scopedPDU.setContextName(contextName);
        OctetString contextEngineID = getContextEngineID();
        if (contextEngineID != null)
            scopedPDU.setContextEngineID(contextEngineID);
    } else {
        pdu = new PDU();
    }
    pdu.setType(type);
    return pdu;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) ScopedPDU(org.snmp4j.ScopedPDU)

Example 9 with OctetString

use of org.snmp4j.smi.OctetString in project opennms by OpenNMS.

the class LLDPMibIT method setUp.

@Before
public void setUp() throws Exception {
    // Create a global USM that all client calls will use
    SNMP4JSettings.setEnterpriseID(5813);
    m_usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(m_usm);
    try {
        m_agent = MockSnmpAgent.createAgentAndRun(classPathResource("penrose-lldp-mib.properties"), str(InetAddress.getLocalHost()) + "/0");
    } catch (Throwable e) {
        m_agent = MockSnmpAgent.createAgentAndRun(classPathResource("penrose-lldp-mib.properties"), str(InetAddressUtils.ONE_TWENTY_SEVEN) + "/0");
    }
    m_requestedVarbinds = new ArrayList<AnticipatedRequest>();
}
Also used : OctetString(org.snmp4j.smi.OctetString) USM(org.snmp4j.security.USM) Before(org.junit.Before)

Example 10 with OctetString

use of org.snmp4j.smi.OctetString in project opennms by OpenNMS.

the class MockSnmpAgentIT method setUp.

@Before
public void setUp() throws Exception {
    // Create a global USM that all client calls will use
    SNMP4JSettings.setEnterpriseID(5813);
    m_usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(m_usm);
    m_agent = MockSnmpAgent.createAgentAndRun(classPathResource("loadSnmpDataTest.properties"), "127.0.0.1/0");
    Thread.sleep(200);
    System.err.println("Started MockSnmpAgent on port " + m_agent.getPort());
    m_requestedVarbinds = new ArrayList<AnticipatedRequest>();
}
Also used : OctetString(org.snmp4j.smi.OctetString) USM(org.snmp4j.security.USM) Before(org.junit.Before)

Aggregations

OctetString (org.snmp4j.smi.OctetString)40 PDU (org.snmp4j.PDU)17 OID (org.snmp4j.smi.OID)17 Snmp (org.snmp4j.Snmp)14 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)13 CommunityTarget (org.snmp4j.CommunityTarget)11 ScopedPDU (org.snmp4j.ScopedPDU)11 USM (org.snmp4j.security.USM)11 UdpAddress (org.snmp4j.smi.UdpAddress)11 VariableBinding (org.snmp4j.smi.VariableBinding)11 Test (org.junit.Test)10 UsmUser (org.snmp4j.security.UsmUser)8 Integer32 (org.snmp4j.smi.Integer32)8 ResponseEvent (org.snmp4j.event.ResponseEvent)7 IOException (java.io.IOException)5 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Address (org.snmp4j.smi.Address)5 TimeTicks (org.snmp4j.smi.TimeTicks)5 InetAddress (java.net.InetAddress)4 PDUv1 (org.snmp4j.PDUv1)4