Search in sources :

Example 1 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project openhab1-addons by openhab.

the class SnmpBinding method internalReceiveCommand.

/**
     * @{inheritDoc
     */
@Override
public void internalReceiveCommand(String itemName, Command command) {
    logger.debug("SNMP receive command {} from {}", itemName, command);
    SnmpBindingProvider providerCmd = null;
    for (SnmpBindingProvider provider : this.providers) {
        OID oid = provider.getOID(itemName, command);
        if (oid != null) {
            providerCmd = provider;
            break;
        }
    }
    if (providerCmd == null) {
        logger.warn("No match for binding provider [itemName={}, command={}]", itemName, command);
        return;
    }
    logger.debug("SNMP command for {} to {}", itemName, providerCmd.toString());
    // Set up the target
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(providerCmd.getCommunity(itemName, command));
    target.setAddress(providerCmd.getAddress(itemName, command));
    target.setRetries(retries);
    target.setTimeout(timeout);
    target.setVersion(providerCmd.getSnmpVersion(itemName, command));
    Variable var = providerCmd.getValue(itemName, command);
    OID oid = providerCmd.getOID(itemName, command);
    VariableBinding varBind = new VariableBinding(oid, var);
    // Create the PDU
    PDU pdu = new PDU();
    pdu.add(varBind);
    pdu.setType(PDU.SET);
    pdu.setRequestID(new Integer32(1));
    logger.debug("SNMP: Send CMD PDU {} {}", providerCmd.getAddress(itemName, command), pdu);
    if (snmp == null) {
        logger.error("SNMP: snmp not initialised - aborting request");
    } else {
        sendPDU(target, pdu);
    }
}
Also used : PDU(org.snmp4j.PDU) Integer32(org.snmp4j.smi.Integer32) Variable(org.snmp4j.smi.Variable) SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) OID(org.snmp4j.smi.OID) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 2 with VariableBinding

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

the class PropertiesBackedManagedObject method getVariable.

private void getVariable(final SubRequest request, final OID oid) {
    try {
        final Variable value = findValueForOID(oid);
        final VariableBinding vb = request.getVariableBinding();
        vb.setOid(oid);
        vb.setVariable(value == null ? Null.noSuchObject : value);
        request.completed();
    } catch (SnmpErrorStatusException e) {
        request.setErrorStatus(e.getErrorStatus());
        request.completed();
    }
}
Also used : DynamicVariable(org.opennms.mock.snmp.responder.DynamicVariable) Variable(org.snmp4j.smi.Variable) SnmpErrorStatusException(org.opennms.mock.snmp.responder.SnmpErrorStatusException) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 3 with VariableBinding

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

the class BrocadeMibIT method requestAndVerifyResponse.

private void requestAndVerifyResponse(int pduType, int version) throws Exception {
    PDU pdu = createPDU(version);
    for (AnticipatedRequest a : m_requestedVarbinds) {
        pdu.add(a.getRequestVarbind());
    }
    pdu.setType(pduType);
    PDU response = sendRequest(pdu, version);
    assertNotNull("request timed out", response);
    System.err.println("Response is: " + response);
    assertTrue("unexpected report pdu: " + ((VariableBinding) response.getVariableBindings().get(0)).getOid(), response.getType() != PDU.REPORT);
    assertEquals("Unexpected number of varbinds returned.", m_requestedVarbinds.size(), response.getVariableBindings().size());
    for (int i = 0; i < m_requestedVarbinds.size(); i++) {
        AnticipatedRequest a = m_requestedVarbinds.get(i);
        VariableBinding vb = response.get(i);
        a.verify(vb);
    }
    reset();
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 4 with VariableBinding

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

the class MockSnmpAgentIT method testSleeperResponder.

@Test
public void testSleeperResponder() throws Exception {
    final String myOid = "1.3.5.1.1.11.0";
    // Verify that the sleeper responds correctly
    Sleeper.getInstance().setVariable(new Integer32(1));
    request(myOid).andExpect(myOid, SMIConstants.SYNTAX_INTEGER, new Integer32(1));
    doGet();
    // Set the timeout
    Sleeper.getInstance().setSleepTime(DEFAULT_TIMEOUT + 1000);
    // Make another request
    PDU pdu = createPDU(m_version);
    OID oid = new OID(myOid);
    pdu.add(new VariableBinding(oid));
    pdu.setType(PDU.GET);
    PDU response = sendRequest(pdu, m_version);
    // Verify that the request does in fact timeout
    assertNull("request timed out", response);
    // Clear the timeout
    Sleeper.getInstance().setSleepTime(0);
    // Update the variable
    Sleeper.getInstance().setVariable(new OctetString("Bingo!"));
    request(myOid).andExpect(myOid, SMIConstants.SYNTAX_OCTET_STRING, new OctetString("Bingo!"));
    doGet();
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) Integer32(org.snmp4j.smi.Integer32) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding) Test(org.junit.Test)

Example 5 with VariableBinding

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

the class TrapNotificationSerializationTest method testsnmp4JV3Serialization.

@Test
public void testsnmp4JV3Serialization() throws UnknownHostException {
    // create instance of snmp4JV3Trap
    PDU snmp4JV3TrapPdu = new ScopedPDU();
    snmp4JV3TrapPdu.setType(PDU.TRAP);
    snmp4JV3TrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("mockhost")));
    snmp4JV3TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("mockhost")));
    snmp4JV3TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.0"), new OctetString("mockhost")));
    TrapInformation snmp4JV3Trap = new Snmp4JTrapNotifier.Snmp4JV2TrapInformation(inetAddress, new String("public"), snmp4JV3TrapPdu);
    assertTrue(writeTrapNotificationObject(snmp4JV3Trap));
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) ScopedPDU(org.snmp4j.ScopedPDU) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) VariableBinding(org.snmp4j.smi.VariableBinding) TrapInformation(org.opennms.netmgt.snmp.TrapInformation) Test(org.junit.Test)

Aggregations

VariableBinding (org.snmp4j.smi.VariableBinding)52 OID (org.snmp4j.smi.OID)37 OctetString (org.snmp4j.smi.OctetString)31 PDU (org.snmp4j.PDU)30 ScopedPDU (org.snmp4j.ScopedPDU)17 Test (org.junit.Test)11 PDUv1 (org.snmp4j.PDUv1)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)8 ResponseEvent (org.snmp4j.event.ResponseEvent)8 Variable (org.snmp4j.smi.Variable)8 ArrayList (java.util.ArrayList)7 IpAddress (org.snmp4j.smi.IpAddress)7 TimeTicks (org.snmp4j.smi.TimeTicks)6 DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)6 TableEvent (org.snmp4j.util.TableEvent)6 TableUtils (org.snmp4j.util.TableUtils)6 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Map (java.util.Map)4