Search in sources :

Example 6 with Variable

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

the class SnmpBinding method dispatchPdu.

private void dispatchPdu(Address address, PDU pdu) {
    if (pdu != null & address != null) {
        logger.debug("Received PDU from '{}' '{}'", address, pdu);
        for (SnmpBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                // Check the IP address
                if (!provider.getAddress(itemName).equals(address)) {
                    continue;
                }
                // Check the OID
                OID oid = provider.getOID(itemName);
                Variable variable = pdu.getVariable(oid);
                if (variable != null) {
                    Class<? extends Item> itemType = provider.getItemType(itemName);
                    // Do any transformations
                    String value = variable.toString();
                    try {
                        value = provider.doTransformation(itemName, value);
                    } catch (TransformationException e) {
                        logger.error("Transformation error with item {}: {}", itemName, e);
                    }
                    // Change to a state
                    State state = null;
                    if (itemType.isAssignableFrom(StringItem.class)) {
                        state = StringType.valueOf(value);
                    } else if (itemType.isAssignableFrom(NumberItem.class)) {
                        state = DecimalType.valueOf(value);
                    } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                        state = OnOffType.valueOf(value);
                    }
                    if (state != null) {
                        eventPublisher.postUpdate(itemName, state);
                    } else {
                        logger.debug("'{}' couldn't be parsed to a State. Valid State-Types are String and Number", variable.toString());
                    }
                } else {
                    logger.trace("PDU doesn't contain a variable with OID '{}'", oid.toString());
                }
            }
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) TransformationException(org.openhab.core.transform.TransformationException) Variable(org.snmp4j.smi.Variable) SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) State(org.openhab.core.types.State) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID)

Example 7 with Variable

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

the class MapSubAgent method get.

@Override
public VariableBinding get(OID requested) {
    if (!m_values.containsKey(requested)) {
        return null;
    }
    OID response = new OID(requested);
    Variable value = m_values.get(response);
    return new VariableBinding(response, value);
}
Also used : Variable(org.snmp4j.smi.Variable) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 8 with Variable

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

the class MapSubAgent method getNext.

@Override
public VariableBinding getNext(OID requested) {
    OID successor = nextOID(requested);
    SortedMap<OID, Variable> tailMap = m_values.tailMap(successor);
    if (tailMap.isEmpty()) {
        return null;
    }
    OID next = tailMap.firstKey();
    Variable value = tailMap.get(next);
    return new VariableBinding(next, value);
}
Also used : Variable(org.snmp4j.smi.Variable) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 9 with Variable

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

the class PropertiesBackedManagedObject method commit.

/** {@inheritDoc} */
@Override
public void commit(final SubRequest request) {
    final VariableBinding vb = request.getVariableBinding();
    final Variable v = vb.getVariable();
    m_vars.put(vb.getOid(), v);
    final RequestStatus status = request.getStatus();
    status.setPhaseComplete(true);
}
Also used : DynamicVariable(org.opennms.mock.snmp.responder.DynamicVariable) Variable(org.snmp4j.smi.Variable) VariableBinding(org.snmp4j.smi.VariableBinding) RequestStatus(org.snmp4j.agent.request.RequestStatus)

Example 10 with Variable

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

the class PropertiesBackedManagedObject method getVariableFromValueString.

/**
	 * <p>getVariableFromValueString</p>
	 *
	 * @param oidStr a {@link java.lang.String} object.
	 * @param valStr a {@link java.lang.String} object.
	 * @return a {@link org.snmp4j.smi.Variable} object.
	 * @throws SnmpErrorStatusException
	 */
private Variable getVariableFromValueString(String oidStr, String valStr) throws SnmpErrorStatusException {
    Variable newVar;
    if (valStr.startsWith("Wrong Type")) {
        String newVal = valStr.replaceFirst("Wrong Type \\(should be .*\\): ", "");
        s_log.error("Bad Mib walk has value: '" + valStr + "' using '" + newVal + "'");
        valStr = newVal;
    }
    if ("\"\"".equals(valStr)) {
        newVar = new Null();
    } else {
        String moTypeStr = valStr.substring(0, valStr.indexOf(':'));
        String moValStr = valStr.substring(valStr.indexOf(':') + 2);
        try {
            if (moTypeStr.equals("STRING")) {
                if (moValStr.startsWith("\"") && moValStr.endsWith("\"")) {
                    moValStr = moValStr.substring(1, moValStr.length() - 1);
                }
                newVar = new OctetString(moValStr);
            } else if (moTypeStr.equals("Hex-STRING")) {
                newVar = OctetString.fromHexString(moValStr.trim().replace(' ', ':'));
            } else if (moTypeStr.equals("INTEGER")) {
                newVar = new Integer32(Integer.parseInt(moValStr));
            } else if (moTypeStr.equals("Gauge32")) {
                newVar = new Gauge32(Long.parseLong(moValStr));
            } else if (moTypeStr.equals("Counter32")) {
                // a 32 bit counter can be > 2 ^ 31, which is > INTEGER_MAX
                newVar = new Counter32(Long.parseLong(moValStr));
            } else if (moTypeStr.equals("Counter64")) {
                newVar = new Counter64(Long.parseLong(moValStr));
            } else if (moTypeStr.equals("Timeticks")) {
                Integer ticksInt = Integer.parseInt(moValStr.substring(moValStr.indexOf('(') + 1, moValStr.indexOf(')')));
                newVar = new TimeTicks(ticksInt);
            } else if (moTypeStr.equals("OID")) {
                newVar = new OID(moValStr);
            } else if (moTypeStr.equals("IpAddress")) {
                newVar = new IpAddress(moValStr.trim());
            } else if (moTypeStr.equals("Network Address")) {
                newVar = OctetString.fromHexString(moValStr.trim());
            } else if (moTypeStr.equals("Responder")) {
                newVar = handleDynamicVariable(oidStr, moValStr);
            } else {
                //newVar = new OctetString(moValStr);
                throw new IllegalArgumentException("Unrecognized SNMP Type " + moTypeStr);
            }
        } catch (SnmpErrorStatusException e) {
            throw e;
        } catch (Throwable t) {
            throw new UndeclaredThrowableException(t, "Could not convert value '" + moValStr + "' of type '" + moTypeStr + "' to SNMP object for OID " + oidStr);
        }
    }
    return newVar;
}
Also used : OctetString(org.snmp4j.smi.OctetString) Null(org.snmp4j.smi.Null) DynamicVariable(org.opennms.mock.snmp.responder.DynamicVariable) Variable(org.snmp4j.smi.Variable) TimeTicks(org.snmp4j.smi.TimeTicks) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) Counter32(org.snmp4j.smi.Counter32) Integer32(org.snmp4j.smi.Integer32) Counter64(org.snmp4j.smi.Counter64) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Gauge32(org.snmp4j.smi.Gauge32) SnmpErrorStatusException(org.opennms.mock.snmp.responder.SnmpErrorStatusException) IpAddress(org.snmp4j.smi.IpAddress)

Aggregations

Variable (org.snmp4j.smi.Variable)10 OID (org.snmp4j.smi.OID)7 VariableBinding (org.snmp4j.smi.VariableBinding)7 OctetString (org.snmp4j.smi.OctetString)4 DynamicVariable (org.opennms.mock.snmp.responder.DynamicVariable)3 Integer32 (org.snmp4j.smi.Integer32)3 SnmpBindingProvider (org.openhab.binding.snmp.SnmpBindingProvider)2 SnmpErrorStatusException (org.opennms.mock.snmp.responder.SnmpErrorStatusException)2 PDU (org.snmp4j.PDU)2 TimeTicks (org.snmp4j.smi.TimeTicks)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 Producer (org.apache.camel.Producer)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 Test (org.junit.Test)1 NumberItem (org.openhab.core.library.items.NumberItem)1 TransformationException (org.openhab.core.transform.TransformationException)1 State (org.openhab.core.types.State)1 CommunityTarget (org.snmp4j.CommunityTarget)1