package org.snmp4j.agent.mo.ext;

import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;
import org.snmp4j.agent.DefaultMOScope;
import org.snmp4j.agent.DuplicateRegistrationException;
import org.snmp4j.agent.MOContextScope;
import org.snmp4j.agent.MOGroup;
import org.snmp4j.agent.MOScope;
import org.snmp4j.agent.MOServer;
import org.snmp4j.agent.ManagedObject;
import org.snmp4j.agent.request.SubRequest;
import org.snmp4j.smi.Null;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;

/* JADX INFO: loaded from: classes.dex */
public class StaticMOGroup implements ManagedObject, MOGroup {
    private OID root;
    private MOScope scope;
    private SortedMap<OID, Variable> vbs = new TreeMap();

    public StaticMOGroup(OID oid, VariableBinding[] variableBindingArr) {
        this.root = oid;
        this.scope = new DefaultMOScope(oid, true, oid.nextPeer(), false);
        for (VariableBinding variableBinding : variableBindingArr) {
            if (variableBinding.getOid() != null && variableBinding.getVariable() != null && variableBinding.getOid().size() >= oid.size() && variableBinding.getOid().leftMostCompare(oid.size(), oid) == 0) {
                this.vbs.put(variableBinding.getOid(), variableBinding.getVariable());
            }
        }
    }

    @Override // org.snmp4j.agent.ManagedObject
    public void cleanup(SubRequest subRequest) {
    }

    @Override // org.snmp4j.agent.ManagedObject
    public void commit(SubRequest subRequest) {
        subRequest.setErrorStatus(14);
    }

    @Override // org.snmp4j.agent.ManagedObject
    public OID find(MOScope mOScope) {
        SortedMap<OID, Variable> sortedMapTailMap = this.vbs.tailMap(mOScope.getLowerBound());
        OID oidFirstKey = sortedMapTailMap.firstKey();
        if (!mOScope.getLowerBound().equals(oidFirstKey) || mOScope.isLowerIncluded()) {
            return oidFirstKey;
        }
        if (sortedMapTailMap.size() <= 1) {
            return null;
        }
        Iterator<OID> it = sortedMapTailMap.keySet().iterator();
        it.next();
        return it.next();
    }

    @Override // org.snmp4j.agent.ManagedObject
    public void get(SubRequest subRequest) {
        Variable variable = this.vbs.get(subRequest.getVariableBinding().getOid());
        if (variable == null) {
            subRequest.getVariableBinding().setVariable(Null.noSuchInstance);
        } else {
            subRequest.getVariableBinding().setVariable(variable);
        }
        subRequest.completed();
    }

    @Override // org.snmp4j.agent.ManagedObject
    public MOScope getScope() {
        return this.scope;
    }

    @Override // org.snmp4j.agent.ManagedObject
    public boolean next(SubRequest subRequest) {
        MOContextScope scope = subRequest.getQuery().getScope();
        SortedMap<OID, Variable> sortedMapTailMap = this.vbs.tailMap(scope.getLowerBound());
        OID oidFirstKey = sortedMapTailMap.firstKey();
        if (scope.getLowerBound().equals(oidFirstKey) && !scope.isLowerIncluded()) {
            if (sortedMapTailMap.size() <= 1) {
                return false;
            }
            Iterator<OID> it = sortedMapTailMap.keySet().iterator();
            it.next();
            oidFirstKey = it.next();
        }
        if (oidFirstKey == null) {
            return false;
        }
        Variable variable = this.vbs.get(oidFirstKey);
        if (variable == null) {
            subRequest.getVariableBinding().setVariable(Null.noSuchInstance);
        }
        subRequest.getVariableBinding().setOid(oidFirstKey);
        subRequest.getVariableBinding().setVariable(variable);
        subRequest.completed();
        return true;
    }

    @Override // org.snmp4j.agent.ManagedObject
    public void prepare(SubRequest subRequest) {
        subRequest.setErrorStatus(17);
    }

    @Override // org.snmp4j.agent.MOGroup
    public void registerMOs(MOServer mOServer, OctetString octetString) throws DuplicateRegistrationException {
        mOServer.register(this, octetString);
    }

    public String toString() {
        return "StaticMOGroup[root=" + this.root + ",vbs=" + this.vbs + "]";
    }

    @Override // org.snmp4j.agent.ManagedObject
    public void undo(SubRequest subRequest) {
        subRequest.setErrorStatus(15);
    }

    @Override // org.snmp4j.agent.MOGroup
    public void unregisterMOs(MOServer mOServer, OctetString octetString) {
        mOServer.unregister(this, octetString);
    }
}
