0N/A/*
2362N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jmx.snmp.agent;
0N/A
0N/A// java imports
0N/A//
0N/Aimport java.io.Serializable;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Vector;
0N/A
0N/A// jmx imports
0N/A//
0N/Aimport com.sun.jmx.snmp.SnmpOid;
0N/Aimport com.sun.jmx.snmp.SnmpValue;
0N/Aimport com.sun.jmx.snmp.SnmpVarBind;
0N/Aimport com.sun.jmx.snmp.SnmpStatusException;
0N/A
0N/A// SNMP Runtime imports
0N/A//
0N/Aimport com.sun.jmx.snmp.agent.SnmpMibOid;
0N/Aimport com.sun.jmx.snmp.agent.SnmpMibNode;
0N/A
0N/A/**
0N/A * Represents a node in an SNMP MIB which corresponds to a group.
0N/A * This class allows subnodes to be registered below a group, providing
0N/A * support for nested groups. The subnodes are registered at run time
0N/A * when registering the nested groups in the global MIB OID tree.
0N/A * <P>
0N/A * This class is used by the class generated by <CODE>mibgen</CODE>.
0N/A * You should not need to use this class directly.
0N/A *
0N/A * <p><b>This API is a Sun Microsystems internal API and is subject
0N/A * to change without notice.</b></p>
0N/A */
0N/A
0N/Apublic abstract class SnmpMibGroup extends SnmpMibOid
0N/A implements Serializable {
0N/A
0N/A // We will register the OID arcs leading to subgroups in this hashtable.
0N/A // So for each arc in varList, if the arc is also in subgroups, it leads
0N/A // to a subgroup, if it is not in subgroup, it leads either to a table
0N/A // or to a variable.
0N/A protected Hashtable<Long, Long> subgroups = null;
0N/A
0N/A /**
0N/A * Tells whether the given arc identifies a table in this group.
0N/A *
0N/A * @param arc An OID arc.
0N/A *
0N/A * @return <CODE>true</CODE> if `arc' leads to a table.
0N/A */
0N/A public abstract boolean isTable(long arc);
0N/A
0N/A /**
0N/A * Tells whether the given arc identifies a variable (scalar object) in
0N/A * this group.
0N/A *
0N/A * @param arc An OID arc.
0N/A *
0N/A * @return <CODE>true</CODE> if `arc' leads to a variable.
0N/A */
0N/A public abstract boolean isVariable(long arc);
0N/A
0N/A /**
0N/A * Tells whether the given arc identifies a readable scalar object in
0N/A * this group.
0N/A *
0N/A * @param arc An OID arc.
0N/A *
0N/A * @return <CODE>true</CODE> if `arc' leads to a readable variable.
0N/A */
0N/A public abstract boolean isReadable(long arc);
0N/A
0N/A
0N/A /**
0N/A * Gets the table identified by the given `arc'.
0N/A *
0N/A * @param arc An OID arc.
0N/A *
0N/A * @return The <CODE>SnmpMibTable</CODE> identified by `arc', or
0N/A * <CODE>null</CODE> if `arc' does not identify any table.
0N/A */
0N/A public abstract SnmpMibTable getTable(long arc);
0N/A
0N/A /**
0N/A * Checks whether the given OID arc identifies a variable (scalar
0N/A * object).
0N/A *
0N/A * @exception If the given `arc' does not identify any variable in this
0N/A * group, throws an SnmpStatusException.
0N/A */
0N/A public void validateVarId(long arc, Object userData)
0N/A throws SnmpStatusException {
0N/A if (isVariable(arc) == false)
0N/A throw noSuchObjectException;
0N/A }
0N/A
0N/A
0N/A // -------------------------------------------------------------------
0N/A // We use a hashtable (subgroup) in order to determine whether an
0N/A // OID arc leads to a subgroup. This implementation can be changed if
0N/A // needed...
0N/A // For instance, the subclass could provide a generated isNestedArc()
0N/A // method in which the subgroup OID arcs would be hardcoded.
0N/A // However, the generic approach was prefered because at this time
0N/A // groups and subgroups are dynamically registered in the MIB.
0N/A //
0N/A /**
0N/A * Tell whether the given OID arc identifies a sub-tree
0N/A * leading to a nested SNMP sub-group. This method is used internally.
0N/A * You shouldn't need to call it directly.
0N/A *
0N/A * @param arc An OID arc.
0N/A *
0N/A * @return <CODE>true</CODE> if the given OID arc identifies a subtree
0N/A * leading to a nested SNMP sub-group.
0N/A *
0N/A */
0N/A public boolean isNestedArc(long arc) {
0N/A if (subgroups == null) return false;
0N/A Object obj = subgroups.get(new Long(arc));
0N/A // if the arc is registered in the hashtable,
0N/A // it leads to a subgroup.
0N/A return (obj != null);
0N/A }
0N/A
0N/A /**
0N/A * Generic handling of the <CODE>get</CODE> operation.
0N/A * <p>The actual implementation of this method will be generated
0N/A * by mibgen. Usually, this implementation only delegates the
0N/A * job to some other provided runtime class, which knows how to
0N/A * access the MBean. The current toolkit thus provides two
0N/A * implementations:
0N/A * <ul><li>The standard implementation will directly access the
0N/A * MBean through a java reference,</li>
0N/A * <li>The generic implementation will access the MBean through
0N/A * the MBean server.</li>
0N/A * </ul>
0N/A * <p>Both implementations rely upon specific - and distinct, set of
0N/A * mibgen generated methods.
0N/A * <p> You can override this method if you need to implement some
0N/A * specific policies for minimizing the accesses made to some remote
0N/A * underlying resources.
0N/A * <p>
0N/A *
0N/A * @param req The sub-request that must be handled by this node.
0N/A *
0N/A * @param depth The depth reached in the OID tree.
0N/A *
0N/A * @exception SnmpStatusException An error occurred while accessing
0N/A * the MIB node.
0N/A */
0N/A abstract public void get(SnmpMibSubRequest req, int depth)
0N/A throws SnmpStatusException;
0N/A
0N/A /**
0N/A * Generic handling of the <CODE>set</CODE> operation.
0N/A * <p>The actual implementation of this method will be generated
0N/A * by mibgen. Usually, this implementation only delegates the
0N/A * job to some other provided runtime class, which knows how to
0N/A * access the MBean. The current toolkit thus provides two
0N/A * implementations:
0N/A * <ul><li>The standard implementation will directly access the
0N/A * MBean through a java reference,</li>
0N/A * <li>The generic implementation will access the MBean through
0N/A * the MBean server.</li>
0N/A * </ul>
0N/A * <p>Both implementations rely upon specific - and distinct, set of
0N/A * mibgen generated methods.
0N/A * <p> You can override this method if you need to implement some
0N/A * specific policies for minimizing the accesses made to some remote
0N/A * underlying resources.
0N/A * <p>
0N/A *
0N/A * @param req The sub-request that must be handled by this node.
0N/A *
0N/A * @param depth The depth reached in the OID tree.
0N/A *
0N/A * @exception SnmpStatusException An error occurred while accessing
0N/A * the MIB node.
0N/A */
0N/A abstract public void set(SnmpMibSubRequest req, int depth)
0N/A throws SnmpStatusException;
0N/A
0N/A /**
0N/A * Generic handling of the <CODE>check</CODE> operation.
0N/A *
0N/A * <p>The actual implementation of this method will be generated
0N/A * by mibgen. Usually, this implementation only delegates the
0N/A * job to some other provided runtime class, which knows how to
0N/A * access the MBean. The current toolkit thus provides two
0N/A * implementations:
0N/A * <ul><li>The standard implementation will directly access the
0N/A * MBean through a java reference,</li>
0N/A * <li>The generic implementation will access the MBean through
0N/A * the MBean server.</li>
0N/A * </ul>
0N/A * <p>Both implementations rely upon specific - and distinct, set of
0N/A * mibgen generated methods.
0N/A * <p> You can override this method if you need to implement some
0N/A * specific policies for minimizing the accesses made to some remote
0N/A * underlying resources, or if you need to implement some consistency
0N/A * checks between the different values provided in the varbind list.
0N/A * <p>
0N/A *
0N/A * @param req The sub-request that must be handled by this node.
0N/A *
0N/A * @param depth The depth reached in the OID tree.
0N/A *
0N/A * @exception SnmpStatusException An error occurred while accessing
0N/A * the MIB node.
0N/A */
0N/A abstract public void check(SnmpMibSubRequest req, int depth)
0N/A throws SnmpStatusException;
0N/A
0N/A // --------------------------------------------------------------------
0N/A // If we reach this node, we are below the root OID, so we just
0N/A // return.
0N/A // --------------------------------------------------------------------
0N/A public void getRootOid(Vector result) {
0N/A return;
0N/A }
0N/A
0N/A // -------------------------------------------------------------------
0N/A // PACKAGE METHODS
0N/A // -------------------------------------------------------------------
0N/A
0N/A // -------------------------------------------------------------------
0N/A // This method can also be overriden in a subclass to provide a
0N/A // different implementation of the isNestedArc() method.
0N/A // => if isNestedArc() is hardcoded, then registerSubArc() becomes
0N/A // useless and can become empty.
0N/A /**
0N/A * Register an OID arc that identifies a sub-tree
0N/A * leading to a nested SNMP sub-group. This method is used internally.
0N/A * You shouldn't ever call it directly.
0N/A *
0N/A * @param arc An OID arc.
0N/A *
0N/A */
0N/A void registerNestedArc(long arc) {
0N/A Long obj = new Long(arc);
0N/A if (subgroups == null) subgroups = new Hashtable<Long, Long>();
0N/A // registers the arc in the hashtable.
0N/A subgroups.put(obj,obj);
0N/A }
0N/A
0N/A // -------------------------------------------------------------------
0N/A // The SnmpMibOid algorithm relies on the fact that for every arc
0N/A // registered in varList, there is a corresponding node at the same
0N/A // position in children.
0N/A // So the trick is to register a null node in children for each variable
0N/A // in varList, so that the real subgroup nodes can be inserted at the
0N/A // correct location.
0N/A // registerObject() should be called for each scalar object and each
0N/A // table arc by the generated subclass.
0N/A /**
0N/A * Register an OID arc that identifies a scalar object or a table.
0N/A * This method is used internally. You shouldn't ever call it directly.
0N/A *
0N/A * @param arc An OID arc.
0N/A *
0N/A */
0N/A protected void registerObject(long arc)
0N/A throws IllegalAccessException {
0N/A
0N/A // this will register the variable in both varList and children
0N/A // The node registered in children will be null, so that the parent
0N/A // algorithm will behave as if no node were registered. This is a
0N/A // trick that makes the parent algorithm behave as if only subgroups
0N/A // were registered in varList and children.
0N/A long[] oid = new long[1];
0N/A oid[0] = arc;
0N/A super.registerNode(oid,0,null);
0N/A }
0N/A
0N/A // -------------------------------------------------------------------
0N/A // registerNode() will be called at runtime when nested groups are
0N/A // registered in the MIB. So we do know that this method will only
0N/A // be called to register nested-groups.
0N/A // We trap registerNode() in order to call registerSubArc()
0N/A /**
0N/A * Register a child node of this node in the OID tree.
0N/A * This method is used internally. You shouldn't ever call it directly.
0N/A *
0N/A * @param oid The oid of the node being registered.
0N/A * @param cursor The position reached in the oid.
0N/A * @param node The node being registered.
0N/A *
0N/A */
0N/A void registerNode(long[] oid, int cursor ,SnmpMibNode node)
0N/A throws IllegalAccessException {
0N/A super.registerNode(oid,cursor,node);
0N/A if (cursor < 0) return;
0N/A if (cursor >= oid.length) return;
0N/A // if we get here, then it means we are registering a subgroup.
0N/A // We will thus register the sub arc in the subgroups hashtable.
0N/A registerNestedArc(oid[cursor]);
0N/A }
0N/A
0N/A // -------------------------------------------------------------------
0N/A // see comments in SnmpMibNode
0N/A // -------------------------------------------------------------------
0N/A void findHandlingNode(SnmpVarBind varbind,
0N/A long[] oid, int depth,
0N/A SnmpRequestTree handlers)
0N/A throws SnmpStatusException {
0N/A
0N/A int length = oid.length;
0N/A SnmpMibNode node = null;
0N/A
0N/A if (handlers == null)
0N/A throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
0N/A
0N/A final Object data = handlers.getUserData();
0N/A
0N/A if (depth >= length) {
0N/A // Nothing is left... the oid is not valid
0N/A throw new SnmpStatusException(SnmpStatusException.noAccess);
0N/A }
0N/A
0N/A long arc = oid[depth];
0N/A
0N/A if (isNestedArc(arc)) {
0N/A // This arc leads to a subgroup: delegates the search to the
0N/A // method defined in SnmpMibOid
0N/A super.findHandlingNode(varbind,oid,depth,handlers);
0N/A return;
0N/A } else if (isTable(arc)) {
0N/A // This arc leads to a table: forward the search to the table.
0N/A
0N/A // Gets the table
0N/A SnmpMibTable table = getTable(arc);
0N/A
0N/A // Forward the search to the table
0N/A table.findHandlingNode(varbind,oid,depth+1,handlers);
0N/A
0N/A } else {
0N/A // If it's not a variable, throws an exception
0N/A validateVarId(arc, data);
0N/A
0N/A // The trailing .0 is missing in the OID
0N/A if (depth+2 > length)
0N/A throw noSuchInstanceException;
0N/A
0N/A // There are too many arcs left in the OID (there should remain
0N/A // a single trailing .0)
0N/A if (depth+2 < length)
0N/A throw noSuchInstanceException;
0N/A
0N/A // The last trailing arc is not .0
0N/A if (oid[depth+1] != 0L)
0N/A throw noSuchInstanceException;
0N/A
0N/A // It's one of our variable, register this node.
0N/A handlers.add(this,depth,varbind);
0N/A }
0N/A }
0N/A
0N/A // -------------------------------------------------------------------
0N/A // See comments in SnmpMibNode.
0N/A // -------------------------------------------------------------------
0N/A long[] findNextHandlingNode(SnmpVarBind varbind,
0N/A long[] oid, int pos, int depth,
0N/A SnmpRequestTree handlers, AcmChecker checker)
0N/A throws SnmpStatusException {
0N/A
0N/A int length = oid.length;
0N/A SnmpMibNode node = null;
0N/A
0N/A if (handlers == null)
0N/A // This should be considered as a genErr, but we do not want to
0N/A // abort the whole request, so we're going to throw
0N/A // a noSuchObject...
0N/A //
0N/A throw noSuchObjectException;
0N/A
0N/A final Object data = handlers.getUserData();
0N/A final int pduVersion = handlers.getRequestPduVersion();
0N/A
0N/A
0N/A // The generic case where the end of the OID has been reached is
0N/A // handled in the superclass
0N/A // XXX Revisit: this works but it is somewhat convoluted. Just setting
0N/A // arc to -1 would work too.
0N/A if (pos >= length)
0N/A return super.findNextHandlingNode(varbind,oid,pos,depth,
0N/A handlers, checker);
0N/A
0N/A // Ok, we've got the arc.
0N/A long arc = oid[pos];
0N/A
0N/A long[] result = null;
0N/A
0N/A // We have a recursive logic. Should we have a loop instead?
0N/A try {
0N/A
0N/A if (isTable(arc)) {
0N/A // If the arc identifies a table, then we need to forward
0N/A // the search to the table.
0N/A
0N/A // Gets the table identified by `arc'
0N/A SnmpMibTable table = getTable(arc);
0N/A
0N/A // Forward to the table
0N/A checker.add(depth, arc);
0N/A try {
0N/A result = table.findNextHandlingNode(varbind,oid,pos+1,
0N/A depth+1,handlers,
0N/A checker);
0N/A }catch(SnmpStatusException ex) {
0N/A throw noSuchObjectException;
0N/A } finally {
0N/A checker.remove(depth);
0N/A }
0N/A // Build up the leaf OID
0N/A result[depth] = arc;
0N/A return result;
0N/A } else if (isReadable(arc)) {
0N/A // If the arc identifies a readable variable, then two cases:
0N/A
0N/A if (pos == (length - 1)) {
0N/A // The end of the OID is reached, so we return the leaf
0N/A // corresponding to the variable identified by `arc'
0N/A
0N/A // Build up the OID
0N/A // result = new SnmpOid(0);
0N/A // result.insert((int)arc);
0N/A result = new long[depth+2];
0N/A result[depth+1] = 0L;
0N/A result[depth] = arc;
0N/A
0N/A checker.add(depth, result, depth, 2);
0N/A try {
0N/A checker.checkCurrentOid();
0N/A } catch(SnmpStatusException e) {
0N/A throw noSuchObjectException;
0N/A } finally {
0N/A checker.remove(depth,2);
0N/A }
0N/A
0N/A // Registers this node
0N/A handlers.add(this,depth,varbind);
0N/A return result;
0N/A }
0N/A
0N/A // The end of the OID is not yet reached, so we must return
0N/A // the next leaf following the variable identified by `arc'.
0N/A // We cannot return the variable because whatever follows in
0N/A // the OID will be greater or equals to 0, and 0 identifies
0N/A // the variable itself - so we have indeed to return the
0N/A // next object.
0N/A // So we do nothing, because this case is handled at the
0N/A // end of the if ... else if ... else ... block.
0N/A
0N/A } else if (isNestedArc(arc)) {
0N/A // Now if the arc leads to a subgroup, we delegate the
0N/A // search to the child, just as done in SnmpMibNode.
0N/A //
0N/A
0N/A // get the child ( = nested arc node).
0N/A //
0N/A final SnmpMibNode child = getChild(arc);
0N/A
0N/A if (child != null) {
0N/A checker.add(depth, arc);
0N/A try {
0N/A result = child.findNextHandlingNode(varbind,oid,pos+1,
0N/A depth+1,handlers,
0N/A checker);
0N/A result[depth] = arc;
0N/A return result;
0N/A } finally {
0N/A checker.remove(depth);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // The oid is not valid, we will throw an exception in order
0N/A // to try with the next valid identifier...
0N/A //
0N/A throw noSuchObjectException;
0N/A
0N/A } catch (SnmpStatusException e) {
0N/A // We didn't find anything at the given arc, so we're going
0N/A // to try with the next valid arc
0N/A //
0N/A long[] newOid = new long[1];
0N/A newOid[0] = getNextVarId(arc,data,pduVersion);
0N/A return findNextHandlingNode(varbind,newOid,0,depth,
0N/A handlers,checker);
0N/A }
0N/A }
0N/A
0N/A}