package org.snmp4j.agent;

import java.io.File;
import java.io.IOException;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.Session;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.cfg.EngineBootsCounterFile;
import org.snmp4j.agent.io.DefaultMOPersistenceProvider;
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.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.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.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

/* JADX INFO: loaded from: classes.dex */
public abstract class BaseAgent implements Runnable {
    public static final int STATE_CREATED = 0;
    public static final int STATE_INIT_FINISHED = 20;
    public static final int STATE_INIT_STARTED = 10;
    public static final int STATE_RUNNING = 40;
    public static final int STATE_STOPPED = 30;
    private static final LogAdapter logger = LogFactory.getLogger(BaseAgent.class);
    protected CommandProcessor agent;
    protected int agentState;
    protected EngineBootsCounterFile bootCounterFile;
    protected String configFileURI;
    protected OctetString defaultContext;
    protected DefaultMOPersistenceProvider defaultPersistenceProvider;
    protected ProxyForwarder defaultProxyForwarder;
    protected MessageDispatcherImpl dispatcher;
    protected MPv3 mpv3;
    protected NotificationOriginator notificationOriginator;
    protected DefaultMOServer server;
    protected Snmp session;
    protected Snmp4jConfigMib snmp4jConfigMIB;
    protected Snmp4jLogMib snmp4jLogMIB;
    protected SnmpCommunityMIB snmpCommunityMIB;
    protected SnmpFrameworkMIB snmpFrameworkMIB;
    protected SnmpNotificationMIB snmpNotificationMIB;
    protected SnmpProxyMIB snmpProxyMIB;
    protected SnmpTargetMIB snmpTargetMIB;
    protected SNMPv2MIB snmpv2MIB;
    protected OctetString sysDescr;
    protected OID sysOID;
    protected Integer32 sysServices;
    protected TransportMapping<? extends Address>[] transportMappings;
    protected USM usm;
    protected UsmMIB usmMIB;
    protected VacmMIB vacmMIB;

    protected BaseAgent(String str) {
        this.sysDescr = new OctetString("SNMP4J-Agent - " + System.getProperty("os.name", "") + " - " + System.getProperty("os.arch") + " - " + System.getProperty("os.version"));
        this.sysOID = new OID("1.3.6.1.4.1.4976");
        this.sysServices = new Integer32(10);
        this.agentState = 0;
        this.configFileURI = str;
        DefaultMOServer defaultMOServer = new DefaultMOServer();
        this.server = defaultMOServer;
        this.defaultPersistenceProvider = new DefaultMOPersistenceProvider(new MOServer[]{defaultMOServer}, str);
    }

    protected abstract void addCommunities(SnmpCommunityMIB snmpCommunityMIB);

    protected abstract void addNotificationTargets(SnmpTargetMIB snmpTargetMIB, SnmpNotificationMIB snmpNotificationMIB);

    protected void addShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() { // from class: org.snmp4j.agent.BaseAgent.1
            @Override // java.lang.Thread, java.lang.Runnable
            public void run() throws Throwable {
                BaseAgent.this.saveConfig();
            }
        });
    }

    protected abstract void addUsmUser(USM usm);

    protected abstract void addViews(VacmMIB vacmMIB);

    protected void finishInit() {
        if (this.agentState < 10) {
            logger.fatal("Agent initialization finish is called before initialization, current state is " + this.agentState);
        }
        this.agent.setNotificationOriginator(this.notificationOriginator);
        this.agent.addMOServer(this.server);
        this.agent.setCoexistenceProvider(this.snmpCommunityMIB);
        this.agent.setVacm(this.vacmMIB);
        this.dispatcher.addCommandResponder(this.agent);
        this.agentState = 20;
    }

    public CommandProcessor getAgent() {
        return this.agent;
    }

    public int getAgentState() {
        return this.agentState;
    }

    public File getBootCounterFile() {
        return this.bootCounterFile.getBootCounterFile();
    }

    public File getConfigFile() {
        return new File(this.configFileURI);
    }

    protected OctetString getContext(MOGroup mOGroup) {
        return getDefaultContext();
    }

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

    public ProxyForwarder getDefaultProxyForwarder() {
        return this.defaultProxyForwarder;
    }

    protected int getEngineBoots() {
        return this.bootCounterFile.getEngineBoots();
    }

    public MPv3 getMPv3() {
        return this.mpv3;
    }

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

    public DefaultMOServer getServer() {
        return this.server;
    }

    public Snmp getSession() {
        return this.session;
    }

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

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

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

    public SnmpFrameworkMIB getSnmpFrameworkMIB() {
        return this.snmpFrameworkMIB;
    }

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

    public SnmpProxyMIB getSnmpProxyMIB() {
        return this.snmpProxyMIB;
    }

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

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

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

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

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

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

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

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

    public void init() throws IOException {
        this.agentState = 10;
        initTransportMappings();
        initMessageDispatcher();
        this.server.addContext(new OctetString());
        SNMPv2MIB sNMPv2MIB = new SNMPv2MIB(this.sysDescr, this.sysOID, this.sysServices);
        this.snmpv2MIB = sNMPv2MIB;
        this.dispatcher.addCounterListener(sNMPv2MIB);
        this.agent.addCounterListener(this.snmpv2MIB);
        this.snmpFrameworkMIB = new SnmpFrameworkMIB((USM) this.mpv3.getSecurityModel(3), this.dispatcher.getTransportMappings());
        UsmMIB usmMIB = new UsmMIB(this.usm, SecurityProtocols.getInstance());
        this.usmMIB = usmMIB;
        this.usm.addUsmUserListener(usmMIB);
        this.vacmMIB = new VacmMIB(new MOServer[]{this.server});
        this.snmpTargetMIB = new SnmpTargetMIB(this.dispatcher);
        this.snmpNotificationMIB = new SnmpNotificationMIB();
        this.snmpCommunityMIB = new SnmpCommunityMIB(this.snmpTargetMIB);
        initConfigMIB();
        this.snmpProxyMIB = new SnmpProxyMIB();
        this.notificationOriginator = new NotificationOriginatorImpl(this.session, this.vacmMIB, this.snmpv2MIB.getSysUpTime(), this.snmpTargetMIB, this.snmpNotificationMIB, this.snmpCommunityMIB);
        this.snmpv2MIB.setNotificationOriginator(this.agent);
        setupDefaultProxyForwarder();
        addUsmUser(this.usm);
        addCommunities(this.snmpCommunityMIB);
        addViews(this.vacmMIB);
        addNotificationTargets(this.snmpTargetMIB, this.snmpNotificationMIB);
        registerSnmpMIBs();
    }

    protected void initConfigMIB() {
        this.snmp4jLogMIB = new Snmp4jLogMib();
        if (this.configFileURI == null || this.defaultPersistenceProvider == null) {
            return;
        }
        Snmp4jConfigMib snmp4jConfigMib = new Snmp4jConfigMib(this.snmpv2MIB.getSysUpTime());
        this.snmp4jConfigMIB = snmp4jConfigMib;
        snmp4jConfigMib.setSnmpCommunityMIB(this.snmpCommunityMIB);
        this.snmp4jConfigMIB.setPrimaryProvider(this.defaultPersistenceProvider);
    }

    protected void initMessageDispatcher() {
        this.dispatcher = new MessageDispatcherImpl();
        this.mpv3 = new MPv3(this.agent.getContextEngineID().getValue());
        this.usm = new USM(SecurityProtocols.getInstance(), this.agent.getContextEngineID(), updateEngineBoots());
        SecurityModels.getInstance().addSecurityModel(this.usm);
        SecurityProtocols.getInstance().addDefaultProtocols();
        this.dispatcher.addMessageProcessingModel(new MPv1());
        this.dispatcher.addMessageProcessingModel(new MPv2c());
        this.dispatcher.addMessageProcessingModel(this.mpv3);
        initSnmpSession();
    }

    protected void initSnmpSession() {
        this.session = new Snmp(this.dispatcher);
        for (TransportMapping<? extends Address> transportMapping : this.transportMappings) {
            try {
                this.session.addTransportMapping(transportMapping);
            } catch (Exception e2) {
                logger.warn("Failed to initialize transport mapping '" + transportMapping + "' with: " + e2.getMessage());
            }
        }
        updateSession(this.session);
    }

    protected void initTransportMappings() throws IOException {
        this.transportMappings = new DefaultUdpTransportMapping[]{new DefaultUdpTransportMapping(new UdpAddress("0.0.0.0/161"))};
    }

    public void loadConfig(int i2) throws Throwable {
        try {
            this.defaultPersistenceProvider.restore(null, i2);
        } catch (IOException e2) {
            logger.error(e2);
        }
    }

    protected abstract void registerManagedObjects();

    protected void registerSnmpMIBs() {
        try {
            this.snmpTargetMIB.registerMOs(this.server, getContext(this.snmpTargetMIB));
            this.snmpNotificationMIB.registerMOs(this.server, getContext(this.snmpNotificationMIB));
            this.vacmMIB.registerMOs(this.server, getContext(this.vacmMIB));
            this.usmMIB.registerMOs(this.server, getContext(this.usmMIB));
            this.snmpv2MIB.registerMOs(this.server, getContext(this.snmpv2MIB));
            this.snmpFrameworkMIB.registerMOs(this.server, getContext(this.snmpFrameworkMIB));
            this.snmpCommunityMIB.registerMOs(this.server, getContext(this.snmpCommunityMIB));
            this.snmp4jLogMIB.registerMOs(this.server, getContext(this.snmp4jLogMIB));
            if (this.snmp4jConfigMIB != null) {
                this.snmp4jConfigMIB.registerMOs(this.server, getContext(this.snmp4jConfigMIB));
            }
            this.snmpProxyMIB.registerMOs(this.server, getContext(this.snmpProxyMIB));
            registerManagedObjects();
        } catch (DuplicateRegistrationException e2) {
            e2.printStackTrace();
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        if (this.agentState < 20) {
            logger.fatal("Agent is run uninitialized, current state is " + this.agentState);
        }
        if (this.agentState == 30) {
            initSnmpSession();
        }
        try {
            this.session.listen();
            this.agentState = 40;
        } catch (IOException e2) {
            logger.error(e2);
        }
    }

    public void saveConfig() throws Throwable {
        try {
            this.defaultPersistenceProvider.store(this.configFileURI);
        } catch (IOException e2) {
            logger.error(e2);
        }
    }

    protected void sendColdStartNotification() {
        this.notificationOriginator.notify(new OctetString(), SnmpConstants.coldStart, new VariableBinding[0]);
    }

    public void setAgent(CommandProcessor commandProcessor) {
        this.agent = commandProcessor;
    }

    public void setBootCounterFile(File file) {
        this.bootCounterFile = new EngineBootsCounterFile(file);
    }

    public void setConfigFile(File file) {
        this.configFileURI = file.getPath();
    }

    public void setDefaultContext(OctetString octetString) {
        this.defaultContext = octetString;
    }

    public void setDefaultProxyForwarder(ProxyForwarder proxyForwarder) {
        this.defaultProxyForwarder = proxyForwarder;
    }

    public void setSysDescr(OctetString octetString) {
        this.sysDescr.setValue(octetString.getValue());
    }

    public void setSysOID(OID oid) {
        this.sysOID.setValue(oid.getValue());
    }

    public void setSysServices(Integer32 integer32) {
        this.sysServices.setValue(integer32.getValue());
    }

    protected void setupDefaultProxyForwarder() {
        ProxyForwarderImpl proxyForwarderImpl = new ProxyForwarderImpl(this.session, this.snmpProxyMIB, this.snmpTargetMIB);
        this.defaultProxyForwarder = proxyForwarderImpl;
        this.agent.addProxyForwarder(proxyForwarderImpl, null, 0);
        ((ProxyForwarderImpl) this.defaultProxyForwarder).addCounterListener(this.snmpv2MIB);
    }

    public void stop() {
        if (this.agentState != 40) {
            logger.error("Agent is stopped although it is not running, current state is " + this.agentState);
        }
        try {
            this.session.close();
        } catch (IOException e2) {
            logger.warn("Closing agent session threw IOException: " + e2.getMessage());
        }
        this.session = null;
        this.agentState = 30;
    }

    protected abstract void unregisterManagedObjects();

    protected void unregisterSnmpMIBs() {
        SnmpTargetMIB snmpTargetMIB = this.snmpTargetMIB;
        snmpTargetMIB.unregisterMOs(this.server, getContext(snmpTargetMIB));
        SnmpNotificationMIB snmpNotificationMIB = this.snmpNotificationMIB;
        snmpNotificationMIB.unregisterMOs(this.server, getContext(snmpNotificationMIB));
        VacmMIB vacmMIB = this.vacmMIB;
        vacmMIB.unregisterMOs(this.server, getContext(vacmMIB));
        UsmMIB usmMIB = this.usmMIB;
        usmMIB.unregisterMOs(this.server, getContext(usmMIB));
        SNMPv2MIB sNMPv2MIB = this.snmpv2MIB;
        sNMPv2MIB.unregisterMOs(this.server, getContext(sNMPv2MIB));
        SnmpFrameworkMIB snmpFrameworkMIB = this.snmpFrameworkMIB;
        snmpFrameworkMIB.unregisterMOs(this.server, getContext(snmpFrameworkMIB));
        SnmpCommunityMIB snmpCommunityMIB = this.snmpCommunityMIB;
        snmpCommunityMIB.unregisterMOs(this.server, getContext(snmpCommunityMIB));
        Snmp4jLogMib snmp4jLogMib = this.snmp4jLogMIB;
        snmp4jLogMib.unregisterMOs(this.server, getContext(snmp4jLogMib));
        Snmp4jConfigMib snmp4jConfigMib = this.snmp4jConfigMIB;
        if (snmp4jConfigMib != null) {
            snmp4jConfigMib.unregisterMOs(this.server, getContext(snmp4jConfigMib));
        }
        SnmpProxyMIB snmpProxyMIB = this.snmpProxyMIB;
        snmpProxyMIB.unregisterMOs(this.server, getContext(snmpProxyMIB));
        unregisterManagedObjects();
    }

    protected int updateEngineBoots() {
        return this.bootCounterFile.updateEngineBoots();
    }

    protected void updateSession(Session session) {
        NotificationOriginator notificationOriginator = this.notificationOriginator;
        if (notificationOriginator instanceof NotificationOriginatorImpl) {
            ((NotificationOriginatorImpl) notificationOriginator).setSession(session);
        }
        ProxyForwarder proxyForwarder = this.defaultProxyForwarder;
        if (proxyForwarder instanceof ProxyForwarderImpl) {
            ((ProxyForwarderImpl) proxyForwarder).setSession(session);
        }
    }

    protected BaseAgent(File file, File file2, CommandProcessor commandProcessor) {
        this(file2 == null ? null : file2.getPath());
        this.bootCounterFile = new EngineBootsCounterFile(file);
        this.agent = commandProcessor;
    }
}
