Search in sources :

Example 1 with SnmpErrorStatusException

use of org.opennms.mock.snmp.responder.SnmpErrorStatusException 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 2 with SnmpErrorStatusException

use of org.opennms.mock.snmp.responder.SnmpErrorStatusException 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

DynamicVariable (org.opennms.mock.snmp.responder.DynamicVariable)2 SnmpErrorStatusException (org.opennms.mock.snmp.responder.SnmpErrorStatusException)2 Variable (org.snmp4j.smi.Variable)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 Counter32 (org.snmp4j.smi.Counter32)1 Counter64 (org.snmp4j.smi.Counter64)1 Gauge32 (org.snmp4j.smi.Gauge32)1 Integer32 (org.snmp4j.smi.Integer32)1 IpAddress (org.snmp4j.smi.IpAddress)1 Null (org.snmp4j.smi.Null)1 OID (org.snmp4j.smi.OID)1 OctetString (org.snmp4j.smi.OctetString)1 TimeTicks (org.snmp4j.smi.TimeTicks)1 VariableBinding (org.snmp4j.smi.VariableBinding)1