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