blob: bf8ffbaf461a0517c2053ffa91b3994ead0beee5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.jmx.snmp.agent;
27
28// java imports
29//
30import java.io.Serializable;
31import java.util.Hashtable;
32import java.util.Enumeration;
33import java.util.Vector;
34
35// jmx imports
36//
37import com.sun.jmx.snmp.SnmpOid;
38import com.sun.jmx.snmp.SnmpValue;
39import com.sun.jmx.snmp.SnmpVarBind;
40import com.sun.jmx.snmp.SnmpStatusException;
41
42// SNMP Runtime imports
43//
44import com.sun.jmx.snmp.agent.SnmpMibOid;
45import com.sun.jmx.snmp.agent.SnmpMibNode;
46
47/**
48 * Represents a node in an SNMP MIB which corresponds to a group.
49 * This class allows subnodes to be registered below a group, providing
50 * support for nested groups. The subnodes are registered at run time
51 * when registering the nested groups in the global MIB OID tree.
52 * <P>
53 * This class is used by the class generated by <CODE>mibgen</CODE>.
54 * You should not need to use this class directly.
55 *
56 * <p><b>This API is a Sun Microsystems internal API and is subject
57 * to change without notice.</b></p>
58 */
59
60public abstract class SnmpMibGroup extends SnmpMibOid
61 implements Serializable {
62
63 // We will register the OID arcs leading to subgroups in this hashtable.
64 // So for each arc in varList, if the arc is also in subgroups, it leads
65 // to a subgroup, if it is not in subgroup, it leads either to a table
66 // or to a variable.
67 protected Hashtable<Long, Long> subgroups = null;
68
69 /**
70 * Tells whether the given arc identifies a table in this group.
71 *
72 * @param arc An OID arc.
73 *
74 * @return <CODE>true</CODE> if `arc' leads to a table.
75 */
76 public abstract boolean isTable(long arc);
77
78 /**
79 * Tells whether the given arc identifies a variable (scalar object) in
80 * this group.
81 *
82 * @param arc An OID arc.
83 *
84 * @return <CODE>true</CODE> if `arc' leads to a variable.
85 */
86 public abstract boolean isVariable(long arc);
87
88 /**
89 * Tells whether the given arc identifies a readable scalar object in
90 * this group.
91 *
92 * @param arc An OID arc.
93 *
94 * @return <CODE>true</CODE> if `arc' leads to a readable variable.
95 */
96 public abstract boolean isReadable(long arc);
97
98
99 /**
100 * Gets the table identified by the given `arc'.
101 *
102 * @param arc An OID arc.
103 *
104 * @return The <CODE>SnmpMibTable</CODE> identified by `arc', or
105 * <CODE>null</CODE> if `arc' does not identify any table.
106 */
107 public abstract SnmpMibTable getTable(long arc);
108
109 /**
110 * Checks whether the given OID arc identifies a variable (scalar
111 * object).
112 *
113 * @exception If the given `arc' does not identify any variable in this
114 * group, throws an SnmpStatusException.
115 */
116 public void validateVarId(long arc, Object userData)
117 throws SnmpStatusException {
118 if (isVariable(arc) == false)
119 throw noSuchObjectException;
120 }
121
122
123 // -------------------------------------------------------------------
124 // We use a hashtable (subgroup) in order to determine whether an
125 // OID arc leads to a subgroup. This implementation can be changed if
126 // needed...
127 // For instance, the subclass could provide a generated isNestedArc()
128 // method in which the subgroup OID arcs would be hardcoded.
129 // However, the generic approach was prefered because at this time
130 // groups and subgroups are dynamically registered in the MIB.
131 //
132 /**
133 * Tell whether the given OID arc identifies a sub-tree
134 * leading to a nested SNMP sub-group. This method is used internally.
135 * You shouldn't need to call it directly.
136 *
137 * @param arc An OID arc.
138 *
139 * @return <CODE>true</CODE> if the given OID arc identifies a subtree
140 * leading to a nested SNMP sub-group.
141 *
142 */
143 public boolean isNestedArc(long arc) {
144 if (subgroups == null) return false;
145 Object obj = subgroups.get(new Long(arc));
146 // if the arc is registered in the hashtable,
147 // it leads to a subgroup.
148 return (obj != null);
149 }
150
151 /**
152 * Generic handling of the <CODE>get</CODE> operation.
153 * <p>The actual implementation of this method will be generated
154 * by mibgen. Usually, this implementation only delegates the
155 * job to some other provided runtime class, which knows how to
156 * access the MBean. The current toolkit thus provides two
157 * implementations:
158 * <ul><li>The standard implementation will directly access the
159 * MBean through a java reference,</li>
160 * <li>The generic implementation will access the MBean through
161 * the MBean server.</li>
162 * </ul>
163 * <p>Both implementations rely upon specific - and distinct, set of
164 * mibgen generated methods.
165 * <p> You can override this method if you need to implement some
166 * specific policies for minimizing the accesses made to some remote
167 * underlying resources.
168 * <p>
169 *
170 * @param req The sub-request that must be handled by this node.
171 *
172 * @param depth The depth reached in the OID tree.
173 *
174 * @exception SnmpStatusException An error occurred while accessing
175 * the MIB node.
176 */
177 abstract public void get(SnmpMibSubRequest req, int depth)
178 throws SnmpStatusException;
179
180 /**
181 * Generic handling of the <CODE>set</CODE> operation.
182 * <p>The actual implementation of this method will be generated
183 * by mibgen. Usually, this implementation only delegates the
184 * job to some other provided runtime class, which knows how to
185 * access the MBean. The current toolkit thus provides two
186 * implementations:
187 * <ul><li>The standard implementation will directly access the
188 * MBean through a java reference,</li>
189 * <li>The generic implementation will access the MBean through
190 * the MBean server.</li>
191 * </ul>
192 * <p>Both implementations rely upon specific - and distinct, set of
193 * mibgen generated methods.
194 * <p> You can override this method if you need to implement some
195 * specific policies for minimizing the accesses made to some remote
196 * underlying resources.
197 * <p>
198 *
199 * @param req The sub-request that must be handled by this node.
200 *
201 * @param depth The depth reached in the OID tree.
202 *
203 * @exception SnmpStatusException An error occurred while accessing
204 * the MIB node.
205 */
206 abstract public void set(SnmpMibSubRequest req, int depth)
207 throws SnmpStatusException;
208
209 /**
210 * Generic handling of the <CODE>check</CODE> operation.
211 *
212 * <p>The actual implementation of this method will be generated
213 * by mibgen. Usually, this implementation only delegates the
214 * job to some other provided runtime class, which knows how to
215 * access the MBean. The current toolkit thus provides two
216 * implementations:
217 * <ul><li>The standard implementation will directly access the
218 * MBean through a java reference,</li>
219 * <li>The generic implementation will access the MBean through
220 * the MBean server.</li>
221 * </ul>
222 * <p>Both implementations rely upon specific - and distinct, set of
223 * mibgen generated methods.
224 * <p> You can override this method if you need to implement some
225 * specific policies for minimizing the accesses made to some remote
226 * underlying resources, or if you need to implement some consistency
227 * checks between the different values provided in the varbind list.
228 * <p>
229 *
230 * @param req The sub-request that must be handled by this node.
231 *
232 * @param depth The depth reached in the OID tree.
233 *
234 * @exception SnmpStatusException An error occurred while accessing
235 * the MIB node.
236 */
237 abstract public void check(SnmpMibSubRequest req, int depth)
238 throws SnmpStatusException;
239
240 // --------------------------------------------------------------------
241 // If we reach this node, we are below the root OID, so we just
242 // return.
243 // --------------------------------------------------------------------
244 public void getRootOid(Vector result) {
245 return;
246 }
247
248 // -------------------------------------------------------------------
249 // PACKAGE METHODS
250 // -------------------------------------------------------------------
251
252 // -------------------------------------------------------------------
253 // This method can also be overriden in a subclass to provide a
254 // different implementation of the isNestedArc() method.
255 // => if isNestedArc() is hardcoded, then registerSubArc() becomes
256 // useless and can become empty.
257 /**
258 * Register an OID arc that identifies a sub-tree
259 * leading to a nested SNMP sub-group. This method is used internally.
260 * You shouldn't ever call it directly.
261 *
262 * @param arc An OID arc.
263 *
264 */
265 void registerNestedArc(long arc) {
266 Long obj = new Long(arc);
267 if (subgroups == null) subgroups = new Hashtable<Long, Long>();
268 // registers the arc in the hashtable.
269 subgroups.put(obj,obj);
270 }
271
272 // -------------------------------------------------------------------
273 // The SnmpMibOid algorithm relies on the fact that for every arc
274 // registered in varList, there is a corresponding node at the same
275 // position in children.
276 // So the trick is to register a null node in children for each variable
277 // in varList, so that the real subgroup nodes can be inserted at the
278 // correct location.
279 // registerObject() should be called for each scalar object and each
280 // table arc by the generated subclass.
281 /**
282 * Register an OID arc that identifies a scalar object or a table.
283 * This method is used internally. You shouldn't ever call it directly.
284 *
285 * @param arc An OID arc.
286 *
287 */
288 protected void registerObject(long arc)
289 throws IllegalAccessException {
290
291 // this will register the variable in both varList and children
292 // The node registered in children will be null, so that the parent
293 // algorithm will behave as if no node were registered. This is a
294 // trick that makes the parent algorithm behave as if only subgroups
295 // were registered in varList and children.
296 long[] oid = new long[1];
297 oid[0] = arc;
298 super.registerNode(oid,0,null);
299 }
300
301 // -------------------------------------------------------------------
302 // registerNode() will be called at runtime when nested groups are
303 // registered in the MIB. So we do know that this method will only
304 // be called to register nested-groups.
305 // We trap registerNode() in order to call registerSubArc()
306 /**
307 * Register a child node of this node in the OID tree.
308 * This method is used internally. You shouldn't ever call it directly.
309 *
310 * @param oid The oid of the node being registered.
311 * @param cursor The position reached in the oid.
312 * @param node The node being registered.
313 *
314 */
315 void registerNode(long[] oid, int cursor ,SnmpMibNode node)
316 throws IllegalAccessException {
317 super.registerNode(oid,cursor,node);
318 if (cursor < 0) return;
319 if (cursor >= oid.length) return;
320 // if we get here, then it means we are registering a subgroup.
321 // We will thus register the sub arc in the subgroups hashtable.
322 registerNestedArc(oid[cursor]);
323 }
324
325 // -------------------------------------------------------------------
326 // see comments in SnmpMibNode
327 // -------------------------------------------------------------------
328 void findHandlingNode(SnmpVarBind varbind,
329 long[] oid, int depth,
330 SnmpRequestTree handlers)
331 throws SnmpStatusException {
332
333 int length = oid.length;
334 SnmpMibNode node = null;
335
336 if (handlers == null)
337 throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
338
339 final Object data = handlers.getUserData();
340
341 if (depth >= length) {
342 // Nothing is left... the oid is not valid
343 throw new SnmpStatusException(SnmpStatusException.noAccess);
344 }
345
346 long arc = oid[depth];
347
348 if (isNestedArc(arc)) {
349 // This arc leads to a subgroup: delegates the search to the
350 // method defined in SnmpMibOid
351 super.findHandlingNode(varbind,oid,depth,handlers);
352 return;
353 } else if (isTable(arc)) {
354 // This arc leads to a table: forward the search to the table.
355
356 // Gets the table
357 SnmpMibTable table = getTable(arc);
358
359 // Forward the search to the table
360 table.findHandlingNode(varbind,oid,depth+1,handlers);
361
362 } else {
363 // If it's not a variable, throws an exception
364 validateVarId(arc, data);
365
366 // The trailing .0 is missing in the OID
367 if (depth+2 > length)
368 throw noSuchInstanceException;
369
370 // There are too many arcs left in the OID (there should remain
371 // a single trailing .0)
372 if (depth+2 < length)
373 throw noSuchInstanceException;
374
375 // The last trailing arc is not .0
376 if (oid[depth+1] != 0L)
377 throw noSuchInstanceException;
378
379 // It's one of our variable, register this node.
380 handlers.add(this,depth,varbind);
381 }
382 }
383
384 // -------------------------------------------------------------------
385 // See comments in SnmpMibNode.
386 // -------------------------------------------------------------------
387 long[] findNextHandlingNode(SnmpVarBind varbind,
388 long[] oid, int pos, int depth,
389 SnmpRequestTree handlers, AcmChecker checker)
390 throws SnmpStatusException {
391
392 int length = oid.length;
393 SnmpMibNode node = null;
394
395 if (handlers == null)
396 // This should be considered as a genErr, but we do not want to
397 // abort the whole request, so we're going to throw
398 // a noSuchObject...
399 //
400 throw noSuchObjectException;
401
402 final Object data = handlers.getUserData();
403 final int pduVersion = handlers.getRequestPduVersion();
404
405
406 // The generic case where the end of the OID has been reached is
407 // handled in the superclass
408 // XXX Revisit: this works but it is somewhat convoluted. Just setting
409 // arc to -1 would work too.
410 if (pos >= length)
411 return super.findNextHandlingNode(varbind,oid,pos,depth,
412 handlers, checker);
413
414 // Ok, we've got the arc.
415 long arc = oid[pos];
416
417 long[] result = null;
418
419 // We have a recursive logic. Should we have a loop instead?
420 try {
421
422 if (isTable(arc)) {
423 // If the arc identifies a table, then we need to forward
424 // the search to the table.
425
426 // Gets the table identified by `arc'
427 SnmpMibTable table = getTable(arc);
428
429 // Forward to the table
430 checker.add(depth, arc);
431 try {
432 result = table.findNextHandlingNode(varbind,oid,pos+1,
433 depth+1,handlers,
434 checker);
435 }catch(SnmpStatusException ex) {
436 throw noSuchObjectException;
437 } finally {
438 checker.remove(depth);
439 }
440 // Build up the leaf OID
441 result[depth] = arc;
442 return result;
443 } else if (isReadable(arc)) {
444 // If the arc identifies a readable variable, then two cases:
445
446 if (pos == (length - 1)) {
447 // The end of the OID is reached, so we return the leaf
448 // corresponding to the variable identified by `arc'
449
450 // Build up the OID
451 // result = new SnmpOid(0);
452 // result.insert((int)arc);
453 result = new long[depth+2];
454 result[depth+1] = 0L;
455 result[depth] = arc;
456
457 checker.add(depth, result, depth, 2);
458 try {
459 checker.checkCurrentOid();
460 } catch(SnmpStatusException e) {
461 throw noSuchObjectException;
462 } finally {
463 checker.remove(depth,2);
464 }
465
466 // Registers this node
467 handlers.add(this,depth,varbind);
468 return result;
469 }
470
471 // The end of the OID is not yet reached, so we must return
472 // the next leaf following the variable identified by `arc'.
473 // We cannot return the variable because whatever follows in
474 // the OID will be greater or equals to 0, and 0 identifies
475 // the variable itself - so we have indeed to return the
476 // next object.
477 // So we do nothing, because this case is handled at the
478 // end of the if ... else if ... else ... block.
479
480 } else if (isNestedArc(arc)) {
481 // Now if the arc leads to a subgroup, we delegate the
482 // search to the child, just as done in SnmpMibNode.
483 //
484
485 // get the child ( = nested arc node).
486 //
487 final SnmpMibNode child = getChild(arc);
488
489 if (child != null) {
490 checker.add(depth, arc);
491 try {
492 result = child.findNextHandlingNode(varbind,oid,pos+1,
493 depth+1,handlers,
494 checker);
495 result[depth] = arc;
496 return result;
497 } finally {
498 checker.remove(depth);
499 }
500 }
501 }
502
503 // The oid is not valid, we will throw an exception in order
504 // to try with the next valid identifier...
505 //
506 throw noSuchObjectException;
507
508 } catch (SnmpStatusException e) {
509 // We didn't find anything at the given arc, so we're going
510 // to try with the next valid arc
511 //
512 long[] newOid = new long[1];
513 newOid[0] = getNextVarId(arc,data,pduVersion);
514 return findNextHandlingNode(varbind,newOid,0,depth,
515 handlers,checker);
516 }
517 }
518
519}