
package uk.co.wingpath.modsnmp;

import java.net.*;
import org.snmp4j.*;
import org.snmp4j.smi.*;
import uk.co.wingpath.snmp.*;
import uk.co.wingpath.util.Waiter;

/**
* This class implements the {@link SnmpModel} interface using constant-valued
* OIDs configured by the user.
*/
public class ConstantOidModel
    implements SnmpModel
{
    private final ConstantOids oidMap;

    /**
    * Constructs a {@code ConstantOidModel}.
    * @param oidMap the mapping from OIDs to constant values.
    */
    public ConstantOidModel (ConstantOids oidMap)
    {
        this.oidMap = oidMap;
    }

    public String getName ()
    {
        return "Constant";
    }

    public boolean manages (OID oid, String userName)
    {
        return oidMap.getEntry (oid) != null;
    }

    public Variable getValue (OID oid, String userName,
        long requestTime, Waiter waiter)
    {
        ConstantEntry entry = oidMap.getEntry (oid);
        if (entry == null)
            return Null.noSuchObject;
        return entry.getValue ();
    }

    public void checkWritable (OID oid, String userName, Variable value)
        throws SnmpException
    {
        throw new SnmpException (PDU.notWritable);
    }

    public boolean setValue (OID oid, String userName, Variable value,
            long requestTime, Waiter waiter)
        throws SnmpException
    {
        throw new SnmpException (PDU.notWritable);
    }

    public OID getNextOID (OID oid, String userName)
    {
        for (OID nextOid : oidMap.getOids ())
        {
            if (nextOid.compareTo (oid) > 0)
                return nextOid;
        }

        return null;
    }
}

