📄 snmpagent.java
字号:
/*
* Copyright (C) 2007 ETH Zurich
*
* This file is part of Fosstrak (www.fosstrak.org).
*
* Fosstrak is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* Fosstrak is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Fosstrak; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package org.fosstrak.reader.rprm.core.mgmt.agent.snmp;
import java.io.File;
import java.io.IOException;
import org.fosstrak.reader.rprm.core.ReaderDevice;
import org.fosstrak.reader.rprm.core.ReaderProtocolException;
import org.fosstrak.reader.rprm.core.mgmt.OperationalStatus;
import org.fosstrak.reader.rprm.core.mgmt.agent.MgmtAgent;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.mib.EpcglobalReaderMib;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.mib.IfMib;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.mib.IpMib;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.EpcgNotifChanSrcTableRowStatusListener;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.EpcgNotifTrigTableRowStatusListener;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.EpcgRdPntSrcTableRowStatusListener;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.EpcgReadTrigTableRowStatusListener;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.EpcgReaderServerTableRowStatusListener;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.SnmpTargetAddrRowStatusListener;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.TableCreator;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.SnmpTable.TableTypeEnum;
import org.fosstrak.reader.rprm.core.mgmt.alarm.AlarmChannel;
import org.fosstrak.reader.rprm.core.mgmt.util.SnmpUtil;
import org.fosstrak.reader.rprm.core.msg.Address;
import org.apache.log4j.Logger;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.BaseAgent;
import org.snmp4j.agent.CommandProcessor;
import org.snmp4j.agent.DuplicateRegistrationException;
import org.snmp4j.agent.mo.MOAccessImpl;
import org.snmp4j.agent.mo.MOScalar;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.RowStatus;
import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
import org.snmp4j.agent.mo.snmp.StorageType;
import org.snmp4j.agent.mo.snmp.TransportDomains;
import org.snmp4j.agent.mo.snmp.VacmMIB;
import org.snmp4j.log.Log4jLogFactory;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.MessageProcessingModel;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModel;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Counter32;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.UnsignedInteger32;
import org.snmp4j.smi.Variable;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.ThreadPool;
/**
* The SNMP agent's core used in the reader management implementation.
*/
public class SnmpAgent extends BaseAgent implements MgmtAgent {
// initialize Log4J logging for the SNMP4J libraries
static {
LogFactory.setLogFactory(new Log4jLogFactory());
}
/**
* The engine ID.
*/
public static final String ENGINE_ID = "80:00:13:70:01:c0:a8:01:21";
/**
* The logger.
*/
private static Logger log = Logger.getLogger(SnmpAgent.class);
/**
* The SNMP agent's address.
*/
private String address;
/**********************************************
* The MIBs *
**********************************************/
/**
* The EpcglobalReaderMib.
*/
private EpcglobalReaderMib epcglobalReaderMib;
/**
* The IfMib.
*/
private IfMib ifMib;
/**
* The IpMib.
*/
private IpMib ipMib;
// /**
// * The SnmpTargetMib.
// */
// private SnmpTargetMib snmpTargetMib;
// /**
// * The SNMPv2Mib.
// */
// private SNMPv2Mib snmpv2Mib;
/**
* The reader device.
*/
private ReaderDevice readerDevice;
/**
* The table creator.
*/
private TableCreator tableCreator;
/**
* <code>true</code> if this agent has been initialized already,
* <code>false</code> otherwise.
*/
private boolean initialized;
/**
* The singleton instance of the <code>SnmpAgent</code>.
*/
private static SnmpAgent instance = null;
/**
* Creates and returns the singleton instance of the <code>SnmpAgent</code>.
* If there already exists an instance it returns that instance.
*
* @param bootCounterFile
* The boot counter file
* @param configFile
* The config file
* @param address
* The SNMP agent's address
* @return The singleton instance of the <code>SnmpAgent</code>
*/
public static SnmpAgent create(File bootCounterFile, File configFile,
String address) {
if (SnmpAgent.instance == null) {
try {
SnmpAgent.instance = new SnmpAgent(bootCounterFile, configFile,
address);
} catch (IOException ioe) {
log.error("Unable to create SnmpAgent.");
}
} else {
log
.warn("SnmpAgent already exists: only one SnmpAgent object can be created.");
}
return SnmpAgent.instance;
}
/**
* Returns the singleton instance of the <code>SnmpAgent</code>.
*
* @return The singleton instance of the <code>SnmpAgent</code>
*/
public static SnmpAgent getInstance() {
if (SnmpAgent.instance == null) log.warn("Call create() method first -> null returned");
return SnmpAgent.instance;
}
/**
* The private constructor.
*
* @param bootCounterFile
* The boot counter file
* @param configFile
* The config file
* @param address
* The SNMP agent's address
* @throws IOException
*/
private SnmpAgent(File bootCounterFile, File configFile, String address)
throws IOException {
super(bootCounterFile, configFile, new CommandProcessor(
// new OctetString(MPv3.createLocalEngineID())));
OctetString.fromHexString(SnmpAgent.ENGINE_ID, ':')));
this.address = address;
initialized = false;
}
/**
* Initialization.
*
* @throws IOException
*/
@Override
public void init() throws IOException {
agent.setThreadPool(ThreadPool.create("RequestPool", 4));
addShutdownHook();
try {
readerDevice = ReaderDevice.getInstance();
} catch (ReaderProtocolException rpe) {
rpe.printStackTrace();
}
tableCreator = new TableCreator(server,readerDevice);
super.init();
initialized = true;
}
/**
* Registers managed objects at the agent's server.
*/
protected void registerManagedObjects() {
try {
// createEpcgReaderDeviceInformation();
createEpcGlobalTables();
createEpcGlobalScalars();
RowStatus epcgReaderServerRowStatus = (RowStatus)epcglobalReaderMib.getEpcgReaderServerEntry().getColumn(EpcglobalReaderMib.idxEpcgReaderServerRowStatus);
epcgReaderServerRowStatus.addRowStatusListener(new EpcgReaderServerTableRowStatusListener(readerDevice));
RowStatus epcgNotifTrigRowStatus = (RowStatus)epcglobalReaderMib.getEpcgNotifTrigEntry().getColumn(EpcglobalReaderMib.idxEpcgNotifTrigRowStatus);
epcgNotifTrigRowStatus.addRowStatusListener(new EpcgNotifTrigTableRowStatusListener());
RowStatus epcgReadTrigRowStatus = (RowStatus)epcglobalReaderMib.getEpcgReadTrigEntry().getColumn(EpcglobalReaderMib.idxEpcgReadTrigRowStatus);
epcgReadTrigRowStatus.addRowStatusListener(new EpcgReadTrigTableRowStatusListener());
RowStatus epcgRdPntSrcRowStatus = (RowStatus)epcglobalReaderMib.getEpcgRdPntSrcEntry().getColumn(EpcglobalReaderMib.idxEpcgRdPntSrcRowStatus);
epcgRdPntSrcRowStatus.addRowStatusListener(new EpcgRdPntSrcTableRowStatusListener());
RowStatus epcgNotifChanSrcRowStatus = (RowStatus)epcglobalReaderMib.getEpcgNotifChanSrcEntry().getColumn(EpcglobalReaderMib.idxEpcgNotifChanSrcRowStatus);
epcgNotifChanSrcRowStatus.addRowStatusListener(new EpcgNotifChanSrcTableRowStatusListener());
RowStatus snmpTargetAddrRowStatus = (RowStatus)snmpTargetMIB.getSnmpTargetAddrEntry().getColumn(7);
snmpTargetAddrRowStatus.addRowStatusListener(new SnmpTargetAddrRowStatusListener(readerDevice));
epcglobalReaderMib.registerMOs(server, null);
ifMib.registerMOs(server, null);
ipMib.registerMOs(server, null);
// snmpTargetMib.registerMOs(server, null);
// snmpv2Mib.registerMOs(server, null);
} catch (DuplicateRegistrationException ex) {
ex.printStackTrace();
}
}
/**
* Adds initial notification targets and filters.
*
* @param targetMIB
* The <code>SnmpTargetMIB</code> holding the target
* configuration
* @param notificationMIB
* The <code>SnmpNotificationMIB</code> holding the
* notification (filter) configuration
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -