package org.snmp4j.agent;

import com.cvte.vman.impl.FwUpgradeManager;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.snmp4j.MessageDispatcher;
import org.snmp4j.Session;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.cfg.EngineBootsProvider;
import org.snmp4j.agent.io.MOInput;
import org.snmp4j.agent.io.MOInputFactory;
import org.snmp4j.agent.io.MOPersistenceProvider;
import org.snmp4j.agent.io.MOServerPersistence;
import org.snmp4j.agent.mo.DefaultMOFactory;
import org.snmp4j.agent.mo.MOFactory;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.NotificationLogMib;
import org.snmp4j.agent.mo.snmp.NotificationOriginatorImpl;
import org.snmp4j.agent.mo.snmp.ProxyForwarderImpl;
import org.snmp4j.agent.mo.snmp.SNMPv2MIB;
import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
import org.snmp4j.agent.mo.snmp.SnmpFrameworkMIB;
import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
import org.snmp4j.agent.mo.snmp.SnmpProxyMIB;
import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
import org.snmp4j.agent.mo.snmp.SnmpTlsTmMib;
import org.snmp4j.agent.mo.snmp.SysUpTime;
import org.snmp4j.agent.mo.snmp.UsmMIB;
import org.snmp4j.agent.mo.snmp.VacmMIB;
import org.snmp4j.agent.mo.snmp4j.Snmp4jConfigMib;
import org.snmp4j.agent.mo.snmp4j.Snmp4jLogMib;
import org.snmp4j.agent.mo.snmp4j.Snmp4jProxyMib;
import org.snmp4j.agent.mo.util.MOTableSizeLimit;
import org.snmp4j.agent.security.VACM;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.TSM;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UnsignedInteger32;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.TLSTM;
import org.snmp4j.util.WorkerPool;
import org.snmp4j.version.VersionInfo;

/* JADX INFO: loaded from: classes.dex */
public class AgentConfigManager implements Runnable {
    public static final int STATE_CONFIGURED = 20;
    public static final int STATE_CREATED = 0;
    public static final int STATE_INITIALIZED = 10;
    public static final int STATE_RESTORED = 30;
    public static final int STATE_RUNNING = 40;
    public static final int STATE_SAVED = 50;
    public static final int STATE_SHUTDOWN = -1;
    public static final int STATE_SUSPENDED = 35;
    public static final int STATE_UNSAVED_CHANGES = 45;
    private static final LogAdapter logger = LogFactory.getLogger(AgentConfigManager.class);
    protected CommandProcessor agent;
    protected SnmpCommunityMIB communityMIB;
    protected MOInputFactory configuration;
    protected OctetString defaultContext;
    protected MessageDispatcher dispatcher;
    protected EngineBootsProvider engineBootsProvider;
    protected OctetString engineID;
    protected SnmpFrameworkMIB frameworkMIB;
    protected MOFactory moFactory;
    protected UnsignedInteger32 notificaitonLogGlobalAge;
    protected UnsignedInteger32 notificaitonLogGlobalLimit;
    protected UnsignedInteger32 notificationLogDefaultLimit;
    protected NotificationLogMib notificationLogMIB;
    protected SnmpNotificationMIB notificationMIB;
    protected NotificationOriginator notificationOriginator;
    protected int persistenceImportMode;
    protected MOPersistenceProvider persistenceProvider;
    protected ProxyForwarder proxyForwarder;
    protected SnmpProxyMIB proxyMIB;
    protected AgentState runState;
    protected MOServer[] servers;
    protected Session session;
    protected Snmp4jConfigMib snmp4jConfigMIB;
    protected Snmp4jLogMib snmp4jLogMIB;
    protected Snmp4jProxyMib snmp4jProxyMIB;
    protected SNMPv2MIB snmpv2MIB;
    protected OctetString sysDescr;
    protected OID sysOID;
    protected Integer32 sysServices;
    protected MOTableSizeLimit<MOTableRow> tableSizeLimit;
    protected SnmpTargetMIB targetMIB;
    protected SnmpTlsTmMib tlsTmMib;
    protected USM usm;
    protected UsmMIB usmMIB;
    protected VACM vacm;
    protected VacmMIB vacmMIB;
    protected WorkerPool workerPool;

    public class AgentState {
        private int state = 0;
        private List<ErrorDescriptor> errorsOccured = new LinkedList();

        public AgentState() {
        }

        public void addError(ErrorDescriptor errorDescriptor) {
            this.errorsOccured.add(errorDescriptor);
        }

        public void advanceState(int i2) {
            if (this.state < i2) {
                this.state = i2;
                AgentConfigManager.logger.info("Agent state advanced to " + i2);
            }
        }

        public List<ErrorDescriptor> getErrors() {
            return new ArrayList(this.errorsOccured);
        }

        public int getState() {
            return this.state;
        }

        public void setState(int i2) {
            this.state = i2;
            AgentConfigManager.logger.info("Agent state set to " + i2);
        }
    }

    static class ErrorDescriptor {
        private String description;
        private Exception exception;
        private int sourceState;
        private int targetState;

        ErrorDescriptor(String str, int i2, int i3, Exception exc) {
            this.description = str;
            this.sourceState = i2;
            this.targetState = i3;
            this.exception = exc;
        }

        public String getDescription() {
            return this.description;
        }

        public Exception getException() {
            return this.exception;
        }

        public int getSourceState() {
            return this.sourceState;
        }

        public int getTargetState() {
            return this.targetState;
        }
    }

    public AgentConfigManager(OctetString octetString, MessageDispatcher messageDispatcher, VACM vacm, MOServer[] mOServerArr, WorkerPool workerPool, MOInputFactory mOInputFactory, MOPersistenceProvider mOPersistenceProvider, EngineBootsProvider engineBootsProvider) {
        this.persistenceImportMode = 2;
        this.notificationLogDefaultLimit = new UnsignedInteger32(100);
        this.notificaitonLogGlobalLimit = new UnsignedInteger32(FwUpgradeManager.TYPE_FW_CVTE_CAMERA);
        this.notificaitonLogGlobalAge = new UnsignedInteger32(0);
        this.moFactory = DefaultMOFactory.getInstance();
        this.sysDescr = new OctetString("SNMP4J-Agent " + VersionInfo.getVersion() + " [" + VersionInfo.getVersion() + "] - " + System.getProperty("os.name", "") + " - " + System.getProperty("os.arch") + " - " + System.getProperty("os.version"));
        this.sysOID = new OID("1.3.6.1.4.1.4976.10");
        this.sysServices = new Integer32(72);
        this.runState = new AgentState();
        this.engineID = octetString;
        this.dispatcher = messageDispatcher;
        this.vacm = vacm;
        this.servers = mOServerArr;
        this.workerPool = workerPool;
        this.configuration = mOInputFactory;
        this.engineBootsProvider = engineBootsProvider;
        this.persistenceProvider = mOPersistenceProvider;
    }

    protected static void stopTransportMappings(Collection<? extends TransportMapping> collection) throws IOException {
        Iterator it = new ArrayList(collection).iterator();
        while (it.hasNext()) {
            ((TransportMapping) it.next()).close();
        }
    }

    private VACM vacm() {
        VACM vacm = this.vacm;
        return vacm != null ? vacm : this.vacmMIB;
    }

    public void configure() {
        MOInputFactory mOInputFactory = this.configuration;
        if (mOInputFactory != null) {
            MOInput mOInputCreateMOInput = mOInputFactory.createMOInput();
            if (mOInputCreateMOInput == null) {
                logger.debug("No configuration returned by configuration factory " + this.configuration);
                return;
            }
            try {
                try {
                    new MOServerPersistence(this.servers).loadData(mOInputCreateMOInput);
                } catch (IOException e2) {
                    String str = "Failed to load agent configuration: " + e2.getMessage();
                    logger.error(str, e2);
                    this.runState.addError(new ErrorDescriptor(str, this.runState.getState(), 20, e2));
                    throw new RuntimeException(str, e2);
                }
            } finally {
                try {
                    mOInputCreateMOInput.close();
                } catch (IOException e3) {
                    logger.warn("Failed to close config input stream: " + e3.getMessage());
                }
            }
        }
        this.runState.advanceState(20);
    }

    public boolean continueProcessing() {
        if (this.runState.getState() != 35) {
            return false;
        }
        this.dispatcher.removeCommandResponder(this.agent);
        this.dispatcher.addCommandResponder(this.agent);
        this.runState.setState(40);
        return true;
    }

    protected CommandProcessor createCommandProcessor(OctetString octetString) {
        return new CommandProcessor(octetString);
    }

    protected NotificationOriginator createNotificationOriginator() {
        return new NotificationOriginatorImpl(this.session, vacm(), this.snmpv2MIB.getSysUpTime(), this.targetMIB, this.notificationMIB);
    }

    protected ProxyForwarder createProxyForwarder(CommandProcessor commandProcessor) {
        this.proxyMIB = new SnmpProxyMIB();
        ProxyForwarderImpl proxyForwarderImpl = new ProxyForwarderImpl(this.session, this.proxyMIB, this.targetMIB);
        commandProcessor.addProxyForwarder(proxyForwarderImpl, null, 0);
        proxyForwarderImpl.addCounterListener(this.snmpv2MIB);
        return this.proxyForwarder;
    }

    protected Session createSnmpSession(MessageDispatcher messageDispatcher) {
        return new Snmp(messageDispatcher);
    }

    protected TSM createTSM() {
        return new TSM(this.engineID, false);
    }

    protected USM createUSM() {
        return new USM(getSupportedSecurityProtocols(), this.engineID, this.engineBootsProvider.updateEngineBoots());
    }

    protected void fireLaunchNotifications() {
        NotificationOriginator notificationOriginator = this.notificationOriginator;
        if (notificationOriginator != null) {
            notificationOriginator.notify(new OctetString(), SnmpConstants.coldStart, new VariableBinding[0]);
        }
    }

    public NotificationOriginator getAgentNotificationOriginator() {
        return this.agent.getNotificationOriginator();
    }

    protected OctetString getContext(MOGroup mOGroup, OctetString octetString) {
        return octetString;
    }

    public OctetString getDefaultContext() {
        return this.defaultContext;
    }

    public NotificationLogMib getNotificationLogMIB() {
        return this.notificationLogMIB;
    }

    public NotificationOriginator getNotificationOriginator() {
        return this.notificationOriginator;
    }

    public int getPersistenceImportMode() {
        return this.persistenceImportMode;
    }

    public SNMPv2MIB getSNMPv2MIB() {
        return this.snmpv2MIB;
    }

    public Snmp4jConfigMib getSnmp4jConfigMIB() {
        return this.snmp4jConfigMIB;
    }

    public Snmp4jLogMib getSnmp4jLogMIB() {
        return this.snmp4jLogMIB;
    }

    public Snmp4jProxyMib getSnmp4jProxyMIB() {
        return this.snmp4jProxyMIB;
    }

    public SnmpCommunityMIB getSnmpCommunityMIB() {
        return this.communityMIB;
    }

    public SnmpNotificationMIB getSnmpNotificationMIB() {
        return this.notificationMIB;
    }

    public SnmpTargetMIB getSnmpTargetMIB() {
        return this.targetMIB;
    }

    public int getState() {
        return this.runState.getState();
    }

    protected SecurityProtocols getSupportedSecurityProtocols() {
        SecurityProtocols.getInstance().addDefaultProtocols();
        return SecurityProtocols.getInstance();
    }

    public OctetString getSysDescr() {
        return this.sysDescr;
    }

    public OID getSysOID() {
        return this.sysOID;
    }

    public Integer32 getSysServices() {
        return this.sysServices;
    }

    public SysUpTime getSysUpTime() {
        return this.snmpv2MIB.getSysUpTime();
    }

    public USM getUsm() {
        return this.usm;
    }

    public UsmMIB getUsmMIB() {
        return this.usmMIB;
    }

    public VACM getVACM() {
        return this.vacm;
    }

    public VacmMIB getVacmMIB() {
        return this.vacmMIB;
    }

    protected void initMandatoryMIBs() {
        SnmpTargetMIB snmpTargetMIB = new SnmpTargetMIB(this.dispatcher);
        this.targetMIB = snmpTargetMIB;
        snmpTargetMIB.addDefaultTDomains();
        this.snmpv2MIB = new SNMPv2MIB(getSysDescr(), getSysOID(), getSysServices());
        this.notificationMIB = new SnmpNotificationMIB();
        this.vacmMIB = new VacmMIB(this.servers);
        UsmMIB usmMIB = new UsmMIB(this.usm, getSupportedSecurityProtocols());
        this.usmMIB = usmMIB;
        this.usm.addUsmUserListener(usmMIB);
        this.communityMIB = new SnmpCommunityMIB(this.targetMIB);
        this.tlsTmMib = new SnmpTlsTmMib(this.moFactory);
    }

    protected void initMessageDispatcherWithMPs(MessageDispatcher messageDispatcher) {
        messageDispatcher.addMessageProcessingModel(new MPv1());
        messageDispatcher.addMessageProcessingModel(new MPv2c());
        messageDispatcher.addMessageProcessingModel(new MPv3(this.agent.getContextEngineID().getValue()));
    }

    protected void initNotificationLogMIB(VACM vacm, SnmpNotificationMIB snmpNotificationMIB) {
        NotificationLogMib notificationLogMib = new NotificationLogMib(this.moFactory, vacm, snmpNotificationMIB);
        this.notificationLogMIB = notificationLogMib;
        this.notificationLogMIB.getNlmConfigLogEntry().addRow((NotificationLogMib.NlmConfigLogEntryRow) notificationLogMib.getNlmConfigLogEntry().createRow(new OID(new int[]{0}), new Variable[]{new OctetString(), this.notificationLogDefaultLimit, new Integer32(1), new Integer32(), new Integer32(4), new Integer32(1)}));
        this.notificationLogMIB.getNlmConfigGlobalAgeOut().setValue(this.notificaitonLogGlobalAge);
        this.notificationLogMIB.getNlmConfigGlobalEntryLimit().setValue(this.notificaitonLogGlobalLimit);
        NotificationOriginator notificationOriginator = this.notificationOriginator;
        if (notificationOriginator instanceof NotificationOriginatorImpl) {
            ((NotificationOriginatorImpl) notificationOriginator).removeNotificationLogListener(this.notificationLogMIB);
            ((NotificationOriginatorImpl) this.notificationOriginator).addNotificationLogListener(this.notificationLogMIB);
        }
    }

    protected void initOptionalMIBs() {
        initSnmp4jLogMIB();
        initSnmp4jConfigMIB(null);
        if (vacm() != null && this.notificationMIB != null) {
            initNotificationLogMIB(vacm(), this.notificationMIB);
        }
        initSnmp4jProxyMIB(getDefaultContext());
    }

    protected void initSecurityModels(EngineBootsProvider engineBootsProvider) {
        USM usmCreateUSM = createUSM();
        this.usm = usmCreateUSM;
        if (usmCreateUSM != null) {
            SecurityModels.getInstance().addSecurityModel(this.usm);
        }
        TSM tsmCreateTSM = createTSM();
        if (tsmCreateTSM != null) {
            SecurityModels.getInstance().addSecurityModel(tsmCreateTSM);
        }
        this.frameworkMIB = new SnmpFrameworkMIB(this.usm, this.dispatcher.getTransportMappings());
    }

    public void initSnmp4jConfigMIB(MOPersistenceProvider[] mOPersistenceProviderArr) {
        Snmp4jConfigMib snmp4jConfigMib = new Snmp4jConfigMib(this.snmpv2MIB.getSysUpTime());
        this.snmp4jConfigMIB = snmp4jConfigMib;
        snmp4jConfigMib.setSnmpCommunityMIB(this.communityMIB);
        MOPersistenceProvider mOPersistenceProvider = this.persistenceProvider;
        if (mOPersistenceProvider != null) {
            this.snmp4jConfigMIB.setPrimaryProvider(mOPersistenceProvider);
        }
        if (mOPersistenceProviderArr != null) {
            for (MOPersistenceProvider mOPersistenceProvider2 : mOPersistenceProviderArr) {
                if (mOPersistenceProvider2 != this.persistenceProvider) {
                    this.snmp4jConfigMIB.addPersistenceProvider(mOPersistenceProvider2);
                }
            }
        }
    }

    public void initSnmp4jLogMIB() {
        this.snmp4jLogMIB = new Snmp4jLogMib();
    }

    public void initSnmp4jProxyMIB(OctetString octetString) {
        this.snmp4jProxyMIB = new Snmp4jProxyMib(this.moFactory, this.session, this.agent.getServer(octetString), this.targetMIB);
    }

    public void initialize() {
        this.session = createSnmpSession(this.dispatcher);
        if (this.engineID == null) {
            this.engineID = new OctetString(MPv3.createLocalEngineID());
        }
        CommandProcessor commandProcessorCreateCommandProcessor = createCommandProcessor(this.engineID);
        this.agent = commandProcessorCreateCommandProcessor;
        commandProcessorCreateCommandProcessor.setWorkerPool(this.workerPool);
        initSecurityModels(this.engineBootsProvider);
        initMessageDispatcherWithMPs(this.dispatcher);
        initMandatoryMIBs();
        linkCounterListener();
        this.agent.setVacm(vacm());
        for (MOServer mOServer : this.servers) {
            this.agent.addMOServer(mOServer);
        }
        this.agent.setCoexistenceProvider(this.communityMIB);
        if (this.notificationOriginator == null) {
            this.notificationOriginator = createNotificationOriginator();
        }
        this.agent.setNotificationOriginator(this.notificationOriginator);
        this.snmpv2MIB.setNotificationOriginator(this.agent);
        initOptionalMIBs();
        try {
            registerMIBs(getDefaultContext());
        } catch (DuplicateRegistrationException e2) {
            logger.error("Duplicate MO registration: " + e2.getMessage(), e2);
        }
        this.runState.advanceState(10);
    }

    protected void launch() {
        if (this.tableSizeLimit != null) {
            for (MOServer mOServer : this.servers) {
                DefaultMOServer.unregisterTableRowListener(mOServer, this.tableSizeLimit);
                DefaultMOServer.registerTableRowListener(mOServer, this.tableSizeLimit);
            }
        }
        this.dispatcher.removeCommandResponder(this.agent);
        this.dispatcher.addCommandResponder(this.agent);
        registerTransportMappings();
        try {
            launchTransportMappings();
        } catch (IOException e2) {
            String str = "Could not put all transport mappings in listen mode: " + e2.getMessage();
            logger.error(str, e2);
            AgentState agentState = this.runState;
            agentState.addError(new ErrorDescriptor(str, agentState.getState(), 40, e2));
        }
        this.runState.advanceState(40);
        fireLaunchNotifications();
    }

    protected void launchTransportMappings() throws IOException {
        launchTransportMappings(this.dispatcher.getTransportMappings());
    }

    protected void linkCounterListener() {
        this.agent.removeCounterListener(this.snmpv2MIB);
        this.agent.addCounterListener(this.snmpv2MIB);
        this.usm.getCounterSupport().removeCounterListener(this.snmpv2MIB);
        this.usm.getCounterSupport().addCounterListener(this.snmpv2MIB);
        for (TransportMapping transportMapping : this.dispatcher.getTransportMappings()) {
            if (transportMapping instanceof TLSTM) {
                TLSTM tlstm = (TLSTM) transportMapping;
                tlstm.getCounterSupport().removeCounterListener(this.tlsTmMib.getCounterListener());
                tlstm.getCounterSupport().addCounterListener(this.tlsTmMib.getCounterListener());
            }
        }
    }

    protected void registerMIBs(OctetString octetString) throws DuplicateRegistrationException {
        MOServer server = this.agent.getServer(octetString);
        SnmpTargetMIB snmpTargetMIB = this.targetMIB;
        snmpTargetMIB.registerMOs(server, getContext(snmpTargetMIB, octetString));
        SnmpNotificationMIB snmpNotificationMIB = this.notificationMIB;
        snmpNotificationMIB.registerMOs(server, getContext(snmpNotificationMIB, octetString));
        VacmMIB vacmMIB = this.vacmMIB;
        vacmMIB.registerMOs(server, getContext(vacmMIB, octetString));
        UsmMIB usmMIB = this.usmMIB;
        usmMIB.registerMOs(server, getContext(usmMIB, octetString));
        SNMPv2MIB sNMPv2MIB = this.snmpv2MIB;
        sNMPv2MIB.registerMOs(server, getContext(sNMPv2MIB, octetString));
        SnmpFrameworkMIB snmpFrameworkMIB = this.frameworkMIB;
        snmpFrameworkMIB.registerMOs(server, getContext(snmpFrameworkMIB, octetString));
        SnmpCommunityMIB snmpCommunityMIB = this.communityMIB;
        snmpCommunityMIB.registerMOs(server, getContext(snmpCommunityMIB, octetString));
        Snmp4jLogMib snmp4jLogMib = this.snmp4jLogMIB;
        if (snmp4jLogMib != null) {
            snmp4jLogMib.registerMOs(server, getContext(snmp4jLogMib, octetString));
        }
        Snmp4jConfigMib snmp4jConfigMib = this.snmp4jConfigMIB;
        if (snmp4jConfigMib != null) {
            snmp4jConfigMib.registerMOs(server, getContext(snmp4jConfigMib, octetString));
        }
        Snmp4jProxyMib snmp4jProxyMib = this.snmp4jProxyMIB;
        if (snmp4jProxyMib != null) {
            snmp4jProxyMib.registerMOs(server, getContext(snmp4jProxyMib, octetString));
        }
        SnmpProxyMIB snmpProxyMIB = this.proxyMIB;
        if (snmpProxyMIB != null) {
            snmpProxyMIB.registerMOs(server, getContext(snmpProxyMIB, octetString));
        }
        NotificationLogMib notificationLogMib = this.notificationLogMIB;
        if (notificationLogMib != null) {
            notificationLogMib.registerMOs(server, getContext(notificationLogMib, octetString));
        }
        SnmpTlsTmMib snmpTlsTmMib = this.tlsTmMib;
        if (snmpTlsTmMib != null) {
            snmpTlsTmMib.registerMOs(server, getContext(snmpTlsTmMib, octetString));
        }
    }

    public void registerShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() { // from class: org.snmp4j.agent.AgentConfigManager.1
            @Override // java.lang.Thread, java.lang.Runnable
            public void run() {
                AgentConfigManager.this.shutdown();
            }
        });
    }

    protected void registerTransportMappings() {
        for (TransportMapping transportMapping : new ArrayList(this.dispatcher.getTransportMappings())) {
            transportMapping.removeTransportListener(this.dispatcher);
            transportMapping.addTransportListener(this.dispatcher);
            if (transportMapping instanceof TLSTM) {
                ((TLSTM) transportMapping).setSecurityCallback(this.tlsTmMib);
            }
        }
    }

    public boolean restoreState() {
        if (this.persistenceProvider == null) {
            return false;
        }
        try {
            if (logger.isInfoEnabled()) {
                logger.info("Restoring persistent data (mode=" + this.persistenceImportMode + ") from " + this.persistenceProvider.getDefaultURI());
            }
            this.persistenceProvider.restore(this.persistenceProvider.getDefaultURI(), this.persistenceImportMode);
            this.runState.advanceState(30);
            return true;
        } catch (FileNotFoundException e2) {
            logger.warn("Saved agent state not found: " + e2.getMessage());
            return false;
        } catch (IOException e3) {
            String str = "Failed to load agent state: " + e3.getMessage();
            logger.error(str, e3);
            AgentState agentState = this.runState;
            agentState.addError(new ErrorDescriptor(str, agentState.getState(), 30, e3));
            return false;
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        if (this.runState.getState() < 10) {
            initialize();
        }
        if (this.runState.getState() < 20) {
            configure();
        }
        if (this.runState.getState() < 30) {
            restoreState();
        }
        if (this.runState.getState() < 40) {
            launch();
        }
    }

    public void saveState() {
        MOPersistenceProvider mOPersistenceProvider = this.persistenceProvider;
        if (mOPersistenceProvider != null) {
            try {
                mOPersistenceProvider.store(mOPersistenceProvider.getDefaultURI());
                this.runState.advanceState(50);
            } catch (IOException e2) {
                String str = "Failed to save agent state: " + e2.getMessage();
                logger.error(str, e2);
                AgentState agentState = this.runState;
                agentState.addError(new ErrorDescriptor(str, agentState.getState(), 50, e2));
            }
        }
    }

    public void setNotificationOriginator(NotificationOriginator notificationOriginator) {
        this.notificationOriginator = notificationOriginator;
        CommandProcessor commandProcessor = this.agent;
        if (commandProcessor != null) {
            commandProcessor.setNotificationOriginator(notificationOriginator);
        }
    }

    public void setPersistenceImportMode(int i2) {
        this.persistenceImportMode = i2;
    }

    public void setTableSizeLimit(int i2) {
        MOServer[] mOServerArr;
        if (this.tableSizeLimit != null && (mOServerArr = this.servers) != null) {
            for (MOServer mOServer : mOServerArr) {
                DefaultMOServer.unregisterTableRowListener(mOServer, this.tableSizeLimit);
            }
        }
        this.tableSizeLimit = new MOTableSizeLimit<>(i2);
        if (getState() == 40) {
            for (MOServer mOServer2 : this.servers) {
                DefaultMOServer.registerTableRowListener(mOServer2, this.tableSizeLimit);
            }
        }
    }

    public void setTableSizeLimits(Properties properties) {
        MOServer[] mOServerArr;
        if (this.tableSizeLimit != null && (mOServerArr = this.servers) != null) {
            for (MOServer mOServer : mOServerArr) {
                DefaultMOServer.unregisterTableRowListener(mOServer, this.tableSizeLimit);
            }
        }
        this.tableSizeLimit = new MOTableSizeLimit<>(properties);
        if (getState() == 40) {
            for (MOServer mOServer2 : this.servers) {
                DefaultMOServer.registerTableRowListener(mOServer2, this.tableSizeLimit);
            }
        }
    }

    public void setupProxyForwarder() {
        this.proxyForwarder = createProxyForwarder(this.agent);
    }

    public void shutdown() {
        logger.info("Shutdown agent: suspending request processing");
        suspendProcessing();
        try {
            if (this.dispatcher != null) {
                logger.info("Shutdown agent: closing transport mappings");
                stopTransportMappings(this.dispatcher.getTransportMappings());
            }
            if (this.session != null) {
                logger.info("Shutdown agent: closing session");
                this.session.close();
                this.session = null;
            }
        } catch (IOException e2) {
            logger.warn("Failed to close SNMP session: " + e2.getMessage());
        }
        logger.info("Shutdown agent: saving state");
        saveState();
        if (this.tableSizeLimit != null) {
            for (MOServer mOServer : this.servers) {
                DefaultMOServer.unregisterTableRowListener(mOServer, this.tableSizeLimit);
            }
        }
        logger.info("Shutdown agent: unregistering MIB objects");
        unregisterMIBs(null);
        this.runState.setState(-1);
        logger.info("Shutdown agent: finished");
    }

    public void suspendProcessing() {
        this.dispatcher.removeCommandResponder(this.agent);
        this.runState.setState(35);
    }

    protected void unregisterMIBs(OctetString octetString) {
        MOServer server = this.agent.getServer(octetString);
        SnmpTargetMIB snmpTargetMIB = this.targetMIB;
        snmpTargetMIB.unregisterMOs(server, getContext(snmpTargetMIB, octetString));
        SnmpNotificationMIB snmpNotificationMIB = this.notificationMIB;
        snmpNotificationMIB.unregisterMOs(server, getContext(snmpNotificationMIB, octetString));
        VacmMIB vacmMIB = this.vacmMIB;
        vacmMIB.unregisterMOs(server, getContext(vacmMIB, octetString));
        UsmMIB usmMIB = this.usmMIB;
        usmMIB.unregisterMOs(server, getContext(usmMIB, octetString));
        SNMPv2MIB sNMPv2MIB = this.snmpv2MIB;
        sNMPv2MIB.unregisterMOs(server, getContext(sNMPv2MIB, octetString));
        SnmpFrameworkMIB snmpFrameworkMIB = this.frameworkMIB;
        snmpFrameworkMIB.unregisterMOs(server, getContext(snmpFrameworkMIB, octetString));
        SnmpCommunityMIB snmpCommunityMIB = this.communityMIB;
        snmpCommunityMIB.unregisterMOs(server, getContext(snmpCommunityMIB, octetString));
        Snmp4jLogMib snmp4jLogMib = this.snmp4jLogMIB;
        if (snmp4jLogMib != null) {
            snmp4jLogMib.unregisterMOs(server, getContext(snmp4jLogMib, octetString));
        }
        Snmp4jConfigMib snmp4jConfigMib = this.snmp4jConfigMIB;
        if (snmp4jConfigMib != null) {
            snmp4jConfigMib.unregisterMOs(server, getContext(this.targetMIB, octetString));
        }
        SnmpProxyMIB snmpProxyMIB = this.proxyMIB;
        if (snmpProxyMIB != null) {
            snmpProxyMIB.unregisterMOs(server, getContext(snmpProxyMIB, octetString));
        }
        NotificationLogMib notificationLogMib = this.notificationLogMIB;
        if (notificationLogMib != null) {
            notificationLogMib.unregisterMOs(server, getContext(notificationLogMib, octetString));
        }
        SnmpTlsTmMib snmpTlsTmMib = this.tlsTmMib;
        if (snmpTlsTmMib != null) {
            snmpTlsTmMib.unregisterMOs(server, getContext(snmpTlsTmMib, octetString));
        }
    }

    protected static void launchTransportMappings(Collection<? extends TransportMapping> collection) throws IOException {
        for (TransportMapping transportMapping : new ArrayList(collection)) {
            if (!transportMapping.isListening()) {
                transportMapping.listen();
            }
        }
    }

    public AgentConfigManager(OctetString octetString, MessageDispatcher messageDispatcher, VACM vacm, MOServer[] mOServerArr, WorkerPool workerPool, MOInputFactory mOInputFactory, MOPersistenceProvider mOPersistenceProvider, EngineBootsProvider engineBootsProvider, MOFactory mOFactory) {
        this(octetString, messageDispatcher, vacm, mOServerArr, workerPool, mOInputFactory, mOPersistenceProvider, engineBootsProvider);
        this.moFactory = mOFactory == null ? this.moFactory : mOFactory;
    }
}
