📄 snmprequesttree.java
字号:
/* * @(#)file SnmpRequestTree.java * @(#)author Sun Microsystems, Inc. * @(#)version 1.27 * @(#)date 08/09/12 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.sun.jmx.snmp.agent;import java.util.Vector;import java.util.ArrayList;import java.util.Hashtable;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import java.util.NoSuchElementException;import java.util.Arrays;import com.sun.jmx.snmp.SnmpVarBind;import com.sun.jmx.snmp.SnmpStatusException;import com.sun.jmx.snmp.SnmpDefinitions;import com.sun.jmx.snmp.SnmpOid;import com.sun.jmx.snmp.SnmpPdu;import com.sun.jmx.snmp.SnmpEngine;import com.sun.jmx.trace.Trace;// XXX: things to do: use SnmpOid rather than `instance' for future// evolutions.// XXX: Maybe use hashlists rather than vectors for entries?// => in that case, the key should be SnmpOid.toString()///** * This class is used to register varbinds from a SNMP varbind list with * the SnmpMibNode responsible for handling the requests concerning that * varbind. * This class holds a hashtable of Handler nodes, whith the involved * SnmpMibNode as a key. * When the involved SnmpMibNode is a group, the sublist of varbind is * directly stored in the Handler node. * When the involved SnmpMibNode is a table, the sublist is stored in a * sorted array indexed by the OID of the entry involved. */final class SnmpRequestTree { // Constructor: // @param req The SnmpMibRequest that will be segmented in this // tree. It holds the original varbind vector passed // by the SnmpSubRequestHandler to this MIB. This // varbind vector is used to retrieve the "real" // position of a varbind in the vector. There is no other easy // way to do this - since as a result of the segmentation the // original positions will be lost. // @param creationflag indicates whether the operation involved // allows for entry creation (ie: it is a SET request). // @param pdutype indicates the type of the request PDU as defined // in SnmpDefinitions // SnmpRequestTree(SnmpMibRequest req, boolean creationflag, int pdutype) { this.request = req; this.version = req.getVersion(); this.creationflag = creationflag; this.hashtable = new Hashtable(); setPduType(pdutype); } public static int mapSetException(int errorStatus, int version) throws SnmpStatusException { final int errorCode = errorStatus; if (version == SnmpDefinitions.snmpVersionOne) return errorCode; int mappedErrorCode = errorCode; // Now take care of V2 errorCodes that can be stored // in the varbind itself: if (errorCode == SnmpStatusException.noSuchObject) // noSuchObject => notWritable mappedErrorCode = SnmpStatusException.snmpRspNotWritable; else if (errorCode == SnmpStatusException.noSuchInstance) // noSuchInstance => notWritable mappedErrorCode = SnmpStatusException.snmpRspNotWritable; return mappedErrorCode; } public static int mapGetException(int errorStatus, int version) throws SnmpStatusException { final int errorCode = errorStatus; if (version == SnmpDefinitions.snmpVersionOne) return errorCode; int mappedErrorCode = errorCode; // Now take care of V2 errorCodes that can be stored // in the varbind itself: if (errorCode == SnmpStatusException.noSuchObject) // noSuchObject => noSuchObject mappedErrorCode = errorCode; else if (errorCode == SnmpStatusException.noSuchInstance) // noSuchInstance => noSuchInstance mappedErrorCode = errorCode; // Now we're going to try to transform every other // global code in either noSuchInstance or noSuchObject, // so that the get can return a partial result. // // Only noSuchInstance or noSuchObject can be stored // in the varbind itself. // // According to RFC 1905: noAccess is emitted when the // the access is denied because it is not in the MIB view... // else if (errorCode == SnmpStatusException.noAccess) // noAccess => noSuchInstance mappedErrorCode = SnmpStatusException.noSuchInstance; // According to RFC 1905: (my interpretation because it is not // really clear) The specified variable name exists - but the // variable does not exists and cannot be created under the // present circumstances (probably because the request specifies // another variable/value which is incompatible, or because the // value of some other variable in the MIB prevents the creation) // // Note that this error should never be raised in a GET context // but who knows? // else if (errorCode == SnmpStatusException.snmpRspInconsistentName) // inconsistentName => noSuchInstance mappedErrorCode = SnmpStatusException.noSuchInstance; // All the errors comprised between snmpRspWrongType and // snmpRspInconsistentValue concern values: so we're going // to assume the OID was correct, and reply with noSuchInstance. // // Note that this error should never be raised in a GET context // but who knows? // else if ((errorCode >= SnmpStatusException.snmpRspWrongType) && (errorCode <= SnmpStatusException.snmpRspInconsistentValue)) mappedErrorCode = SnmpStatusException.noSuchInstance; // We're going to assume the OID was correct, and reply // with noSuchInstance. // else if (errorCode == SnmpStatusException.readOnly) mappedErrorCode = SnmpStatusException.noSuchInstance; // For all other errors but genErr, we're going to reply with // noSuchObject // else if (errorCode != SnmpStatusException.snmpRspAuthorizationError && errorCode != SnmpStatusException.snmpRspGenErr) mappedErrorCode = SnmpStatusException.noSuchObject; // Only genErr will abort the GET and be returned as global // error. // return mappedErrorCode; } //------------------------------------------------------------------- // This class is a package implementation of the enumeration of // SnmSubRequest associated with an Handler node. //------------------------------------------------------------------- static final class Enum implements Enumeration { Enum(SnmpRequestTree hlist,Handler h) { handler = h; this.hlist = hlist; size = h.getSubReqCount(); } private final Handler handler; private final SnmpRequestTree hlist; private int entry = 0; private int iter = 0; private int size = 0; public boolean hasMoreElements() { return iter < size; } public Object nextElement() throws NoSuchElementException { if (iter == 0) { if (handler.sublist != null) { iter++; return hlist.getSubRequest(handler); } } iter ++; if (iter > size) throw new NoSuchElementException(); Object result = hlist.getSubRequest(handler,entry); entry++; return result; } } //------------------------------------------------------------------- // This class is a package implementation of the SnmpMibSubRequest // interface. It can only be instantiated by SnmpRequestTree. //------------------------------------------------------------------- static final class SnmpMibSubRequestImpl implements SnmpMibSubRequest { SnmpMibSubRequestImpl(SnmpMibRequest global, Vector sublist, SnmpOid entryoid, boolean isnew, boolean getnextflag, SnmpVarBind rs) { this.global = global; varbinds = sublist; this.version = global.getVersion(); this.entryoid = entryoid; this.isnew = isnew; this.getnextflag = getnextflag; this.statusvb = rs; } final private Vector varbinds; final private SnmpMibRequest global; final private int version; final private boolean isnew; final private SnmpOid entryoid; final private boolean getnextflag; final private SnmpVarBind statusvb; // ------------------------------------------------------------- // Implements the method defined in SnmpMibRequest interface. // See SnmpMibRequest for the java doc. // ------------------------------------------------------------- public Enumeration getElements() { return varbinds.elements(); } // ------------------------------------------------------------- // Implements the method defined in SnmpMibRequest interface. // See SnmpMibRequest for the java doc. // ------------------------------------------------------------- public Vector getSubList() { return varbinds; } // ------------------------------------------------------------- // Implements the method defined in SnmpMibRequest interface. // See SnmpMibRequest for the java doc. // ------------------------------------------------------------- public final int getSize() { if (varbinds == null) return 0; return varbinds.size(); } // ------------------------------------------------------------- // Implements the method defined in SnmpMibRequest interface. // See SnmpMibRequest for the java doc. // ------------------------------------------------------------- public void addVarBind(SnmpVarBind varbind) { // XXX not sure we must also add the varbind in the global // request? or whether we should raise an exception: // in principle, this method should not be called! varbinds.addElement(varbind); global.addVarBind(varbind); } // ------------------------------------------------------------- // Implements the method defined in SnmpMibSubRequest interface. // See SnmpMibSubRequest for the java doc. // ------------------------------------------------------------- public boolean isNewEntry() { return isnew; } // ------------------------------------------------------------- // Implements the method defined in SnmpMibSubRequest interface. // See SnmpMibSubRequest for the java doc. // ------------------------------------------------------------- public SnmpOid getEntryOid() { return entryoid; } // ------------------------------------------------------------- // Implements the method defined in SnmpMibRequest interface. // See SnmpMibRequest for the java doc. // ------------------------------------------------------------- public int getVarIndex(SnmpVarBind varbind) { if (varbind == null) return 0; return global.getVarIndex(varbind); } // ------------------------------------------------------------- // Implements the method defined in SnmpMibRequest interface. // See SnmpMibRequest for the java doc. // ------------------------------------------------------------- public Object getUserData() { return global.getUserData(); } // ------------------------------------------------------------- // Implements the method defined in SnmpMibSubRequest interface. // See SnmpMibSubRequest for the java doc. // ------------------------------------------------------------- public void registerGetException(SnmpVarBind var, SnmpStatusException exception) throws SnmpStatusException { // The index in the exception must correspond to // the SNMP index ... // if (version == SnmpDefinitions.snmpVersionOne) throw new SnmpStatusException(exception, getVarIndex(var)+1); if (var == null) throw exception; // If we're doing a getnext ==> endOfMibView if (getnextflag) { var.value = SnmpVarBind.endOfMibView; return; } final int errorCode = mapGetException(exception.getStatus(), version); // Now take care of V2 errorCodes that can be stored // in the varbind itself: if (errorCode == SnmpStatusException.noSuchObject) // noSuchObject => noSuchObject var.value= SnmpVarBind.noSuchObject; else if (errorCode == SnmpStatusException.noSuchInstance) // noSuchInstance => noSuchInstance var.value= SnmpVarBind.noSuchInstance; else throw new SnmpStatusException(errorCode, getVarIndex(var)+1); } // ------------------------------------------------------------- // Implements the method defined in SnmpMibSubRequest interface. // See SnmpMibSubRequest for the java doc. // ------------------------------------------------------------- public void registerSetException(SnmpVarBind var, SnmpStatusException exception) throws SnmpStatusException { // The index in the exception must correspond to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -