📄 snmp4jsession.java.svn-base
字号:
package org.opengoss.snmphibernate.impl.snmp4j;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.opengoss.snmphibernate.api.AbstractSnmpSession;
import org.opengoss.snmphibernate.api.ISnmpTarget;
import org.opengoss.snmphibernate.api.SmiType;
import org.opengoss.snmphibernate.api.SnmpAnnotationException;
import org.opengoss.snmphibernate.api.SnmpException;
import org.opengoss.snmphibernate.api.annotation.MibIndex;
import org.opengoss.snmphibernate.api.annotation.MibObjectType;
import org.opengoss.snmphibernate.api.annotation.RowStatus;
import org.opengoss.snmphibernate.api.annotation.MibObjectType.Access;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.UnsignedInteger32;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class Snmp4JSession extends AbstractSnmpSession {
private Snmp snmp4J;
private final Snmp4JTarget target;
public Snmp4JSession(ISnmpTarget target) throws IOException {
this.target = (Snmp4JTarget) target;
TransportMapping tm = new DefaultUdpTransportMapping();
this.snmp4J = new Snmp(tm);
this.snmp4J.listen();
}
private Target getReadTarget() {
Target targetImpl = target.getReadTarget();
initTimeoutRetries(targetImpl);
return targetImpl;
}
private void initTimeoutRetries(Target targetImpl) {
targetImpl.setTimeout(getTimeout());
targetImpl.setRetries(getRetries());
}
private Target getWriteTarget() {
Target targetImpl = target.getWriteTarget();
initTimeoutRetries(targetImpl);
return targetImpl;
}
public <T> T get(Class<T> scalarClass) throws IOException, SnmpException,
SnmpAnnotationException {
PDU reqPDU = newGetPDU(scalarClass);
return get(scalarClass, reqPDU);
}
public <T> T get(Class<T> scalarClass, String[] fields) throws IOException,
SnmpException, SnmpAnnotationException {
try {
PDU reqPDU = newGetPDU(scalarClass, fields);
return get(scalarClass, reqPDU);
} catch (SecurityException e) {
throw new SnmpAnnotationException(e);
} catch (NoSuchFieldException e) {
throw new SnmpAnnotationException(e);
}
}
public <T> List<T> getTable(Class<T> entryClass) throws IOException, SnmpException,
SnmpAnnotationException {
try {
List<T> list = new ArrayList<T>();
// init pdu
PDU reqPDU = newGetNextFirstEntryPDU(entryClass);
checkReqError(reqPDU);
OID firstReqOid = reqPDU.get(0).getOid();
while (true) {
ResponseEvent event = snmp4J.getNext(reqPDU, getReadTarget());
checkEventError(event);
PDU respPDU = event.getResponse();
checkResError(respPDU);
OID firstRespOid = respPDU.get(0).getOid();
if (isTableEnd(firstReqOid, firstRespOid)) {
break;
}
int[] indexOids = extractIndexOids(firstRespOid, firstReqOid);
T entry = entryClass.newInstance();
fillIndices(entry, indexOids);
fillProperties(entry, respPDU);
list.add(entry);
reqPDU = newGetNextEntryPDU(entry);
}
return list;
} catch (IllegalArgumentException e) {
throw new SnmpAnnotationException(e);
} catch (InstantiationException e) {
throw new SnmpAnnotationException(e);
} catch (IllegalAccessException e) {
throw new SnmpAnnotationException(e);
}
}
public <T> T getByIndex(Class<T> entryClass, Serializable indices)
throws IOException, SnmpException, SnmpAnnotationException {
try {
T entry = buildEntryWithIndices(entryClass, indices);
PDU reqPDU = newGetEntryPDU(entry);
return getEntryByIndex(entry, reqPDU);
} catch (InstantiationException e) {
throw new SnmpAnnotationException(e);
} catch (IllegalAccessException e) {
throw new SnmpAnnotationException(e);
}
}
public <T> T getByIndex(Class<T> entryClass, Serializable indices,
String[] fields) throws IOException, SnmpException,
SnmpAnnotationException {
try {
T entry = buildEntryWithIndices(entryClass, indices);
PDU reqPDU = newGetEntryPDU(entry, fields);
return getEntryByIndex(entry, reqPDU);
} catch (IllegalArgumentException e) {
throw new SnmpAnnotationException(e);
} catch (SecurityException e) {
throw new SnmpAnnotationException(e);
} catch (IllegalAccessException e) {
throw new SnmpAnnotationException(e);
} catch (NoSuchFieldException e) {
throw new SnmpAnnotationException(e);
} catch (InstantiationException e) {
throw new SnmpAnnotationException(e);
}
}
public void set(Object entry) throws IOException, SnmpException,
SnmpAnnotationException {
try {
PDU reqPDU = newSetPDU(entry);
checkReqError(reqPDU);
ResponseEvent event = snmp4J.set(reqPDU, getWriteTarget());
checkEventError(event);
PDU resPDU = event.getResponse();
checkResError(resPDU);
} catch (InstantiationException e) {
throw new SnmpAnnotationException(e);
} catch (IllegalAccessException e) {
throw new SnmpAnnotationException(e);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
throw new SnmpAnnotationException(e);
} catch (NoSuchMethodException e) {
throw new SnmpAnnotationException(e);
} catch (InvocationTargetException e) {
throw new SnmpAnnotationException(e);
}
}
public void create(Object entry) throws IOException, SnmpException,
SnmpAnnotationException {
try {
PDU reqPDU = newCreatePDU(entry);
checkReqError(reqPDU);
ResponseEvent event = snmp4J.set(reqPDU, getWriteTarget());
checkEventError(event);
PDU resPDU = event.getResponse();
checkResError(resPDU);
} catch (IllegalArgumentException e) {
throw new SnmpAnnotationException(e);
} catch (IllegalAccessException e) {
throw new SnmpAnnotationException(e);
} catch (SecurityException e) {
throw new SnmpAnnotationException(e);
} catch (NoSuchMethodException e) {
throw new SnmpAnnotationException(e);
} catch (InstantiationException e) {
throw new SnmpAnnotationException(e);
} catch (InvocationTargetException e) {
throw new SnmpAnnotationException(e);
}
}
public void delete(Object entry) throws IOException, SnmpException,
SnmpAnnotationException {
try {
PDU reqPDU = newDeletPDU(entry);
checkReqError(reqPDU);
ResponseEvent event = snmp4J.set(reqPDU, getWriteTarget());
checkEventError(event);
PDU resPDU = event.getResponse();
checkResError(resPDU);
} catch (IllegalArgumentException e) {
throw new SnmpAnnotationException(e);
} catch (IllegalAccessException e) {
throw new SnmpAnnotationException(e);
}
}
public void close() throws IOException {
snmp4J.close();
}
private Field[] getWritePropFields(Class clazz) {
List<Field> list = new ArrayList<Field>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(MibObjectType.class)
&& !field.isAnnotationPresent(MibIndex.class)) {
MibObjectType mot = field.getAnnotation(MibObjectType.class);
if (mot.access() == Access.WRITE
|| mot.access() == Access.CREATE) {
list.add(field);
}
}
}
return (Field[]) list.toArray(new Field[list.size()]);
}
private OID buildIndexOid(Object entry) throws IllegalArgumentException,
IllegalAccessException {
Class clazz = entry.getClass();
Field[] indexFields = getIndexFields(clazz);
OID oid = null;
for (Field indexField : indexFields) {
indexField.setAccessible(true);
Object value = indexField.get(entry);
MibIndex miAnnotation = indexField.getAnnotation(MibIndex.class);
int length = miAnnotation.length();
if (length == MibIndex.VARSTR_WITH_LENGTH) {
byte[] bytes = ((String) value).getBytes();
int[] integers = new int[bytes.length + 1];
integers[0] = bytes.length;
int i = 1;
for (byte b : bytes) {
integers[i++] = b;
}
oid = appendRawOids(oid, integers);
} else if (length == MibIndex.VARSTR_WITHOUT_LENGTH) {
byte[] bytes = ((String) value).getBytes();
int[] integers = new int[bytes.length];
int i = 0;
for (byte b : bytes) {
integers[i++] = b;
}
oid = appendRawOids(oid, integers);
} else if (length == 1) {
MibObjectType mot = indexField
.getAnnotation(MibObjectType.class);
Class smiTypeClass = getSmiTypeProvider().getSmiType(
mot.smiType());
if (smiTypeClass.equals(Integer32.class)) {
int v = ((Integer) value).intValue();
oid = appendRawOids(oid, new int[] { v });
} else if (UnsignedInteger32.class
.isAssignableFrom(smiTypeClass)) {
int v = (int) ((Long) value).longValue();
oid = appendRawOids(oid, new int[] { v });
} else {
throw new RuntimeException("Index length should not be 1."
+ indexField);
}
} else if (length >= 1) {
MibObjectType mot = indexField
.getAnnotation(MibObjectType.class);
SmiType smiType = mot.smiType();
if (smiType == SmiType.OID) {
if (oid == null)
oid = new OID();
oid.append((String) value);
} else if (smiType == SmiType.DISPLAY_STRING) {
byte[] bytes = ((String) value).getBytes();
int[] integers = new int[length];
for (int i = 0; i < integers.length; i++) {
integers[i] = (int) bytes[i];
}
oid = appendRawOids(oid, integers);
} else if (smiType == SmiType.OCTET_STRING) {
byte[] bytes = ((byte[]) value);
int[] integers = new int[length];
for (int i = 0; i < integers.length; i++) {
integers[i] = (int) bytes[i];
}
oid = appendRawOids(oid, integers);
} else if (smiType == SmiType.IPADDRESS) {
String[] strBytes = ((String) value).split("\\.");
if (strBytes.length != 4)
throw new RuntimeException(
"Assert faild. IpAddres length must be 1.");
int[] integers = new int[strBytes.length];
for (int i = 0; i < integers.length; i++) {
integers[i] = Integer.parseInt(strBytes[i]);
}
oid = appendRawOids(oid, integers);
} else {
throw new RuntimeException("Unknow smiType: " + smiType);
}
} else {
throw new RuntimeException(
"Assert Failed! Unknow index length.");
}
}
return oid;
}
private OID appendRawOids(OID oid, int[] integers) {
if (oid == null)
return new OID(integers);
oid.append(new OID(integers));
return oid;
}
private void fillIndices(Object entry, int[] indexOids)
throws IllegalArgumentException, IllegalAccessException {
Class clazz = entry.getClass();
Field[] indexFields = getIndexFields(clazz);
for (int i = 0, j = 0; i < indexFields.length && j < indexOids.length; i++) {
Field indexField = indexFields[i];
indexField.setAccessible(true);
MibIndex miAnnotation = indexField.getAnnotation(MibIndex.class);
int length = miAnnotation.length();
if (length == MibIndex.VARSTR_WITH_LENGTH) {
byte[] bytes = new byte[indexOids[j++]];
bytes = copyBytes(indexOids, j, bytes);
indexField.set(entry, new String(bytes));
j += bytes.length;
} else if (length == MibIndex.VARSTR_WITHOUT_LENGTH) {
byte[] bytes = new byte[indexOids.length - j];
bytes = copyBytes(indexOids, j, bytes);
indexField.set(entry, new String(bytes));
j += bytes.length;
} else if (length == 1) {
MibObjectType mot = indexField
.getAnnotation(MibObjectType.class);
Class smiTypeClass = getSmiTypeProvider().getSmiType(
mot.smiType());
if (smiTypeClass.equals(Integer32.class)) {
indexField.set(entry, indexOids[j++]);
} else if (UnsignedInteger32.class
.isAssignableFrom(smiTypeClass)) {
indexField.set(entry, (long) indexOids[j++]);
} else {
throw new RuntimeException("Index length should not be 1."
+ indexField);
}
} else if (length >= 1) {
MibObjectType mot = indexField
.getAnnotation(MibObjectType.class);
SmiType smiType = mot.smiType();
if (smiType == SmiType.OID) {
int[] oidValue = new int[length];
System.arraycopy(indexOids, j, oidValue, 0, length);
indexField.set(entry, intAry2Str(oidValue));
} else if (smiType == SmiType.DISPLAY_STRING) {
byte[] bytes = new byte[length];
bytes = copyBytes(indexOids, j, bytes);
indexField.set(entry, new String(bytes));
} else if (smiType == SmiType.OCTET_STRING) {
byte[] bytes = new byte[length];
bytes = copyBytes(indexOids, j, bytes);
indexField.set(entry, bytes);
} else if (smiType == SmiType.IPADDRESS) {
if (length != 4)
throw new RuntimeException(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -