KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > agent > SnmpMib


1 /*
2  * @(#)file SnmpMib.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.29
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12
13 package com.sun.jmx.snmp.agent;
14
15
16
17 // java imports
18
//
19
import java.io.Serializable JavaDoc;
20 import java.util.Vector JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.lang.IllegalAccessException JavaDoc;
23
24 // jmx imports
25
//
26
import javax.management.ObjectName JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.InstanceAlreadyExistsException JavaDoc;
30 import javax.management.MBeanRegistrationException JavaDoc;
31 import javax.management.NotCompliantMBeanException JavaDoc;
32 import com.sun.jmx.snmp.SnmpOid;
33 import com.sun.jmx.snmp.SnmpVarBind;
34 import com.sun.jmx.snmp.SnmpDefinitions;
35 import com.sun.jmx.snmp.SnmpStatusException;
36 import com.sun.jmx.snmp.SnmpEngine;
37 import com.sun.jmx.snmp.SnmpUnknownModelException;
38 // SNMP Runtime imports
39
import com.sun.jmx.trace.Trace;
40
41 import com.sun.jmx.snmp.internal.SnmpAccessControlModel;
42 import com.sun.jmx.snmp.internal.SnmpEngineImpl;
43
44
45 /**
46  * This list is used in order to construct the OID during the getnext.
47  * The constructed oid is checked by the checker AcmChecker.
48  */

49 final class LongList {
50
51     public static int DEFAULT_CAPACITY = 10;
52     
53     public static int DEFAULT_INCREMENT = 10;
54     
55
56     private final int DELTA;
57     private int size;
58
59     /**
60      * The list content. Any access to this variable must be protected
61      * by a synchronized block on the LongList object.
62      * Only read-only action should be performed on this object.
63      **/

64     public long[] list;
65     
66     LongList() {
67         this(DEFAULT_CAPACITY,DEFAULT_INCREMENT);
68     }
69     
70     LongList(int initialCapacity) {
71         this(initialCapacity,DEFAULT_INCREMENT);
72     }
73     
74     LongList(int initialCapacity, int delta) {
75         size = 0;
76         DELTA = delta;
77         list = allocate(initialCapacity);
78     }
79     
80     /**
81      * Same behaviour than size() in {@link java.util.List}.
82      **/

83     public final int size() { return size;}
84     
85     /**
86      * Same behaviour than add(long o) in {@link java.util.List}.
87      * Any access to this method should be protected in a synchronized
88      * block on the LongList object.
89      **/

90     public final boolean add(final long o) {
91         if (size >= list.length)
92             resize();
93         list[size++]=o;
94         return true;
95     }
96
97     /**
98      * Same behaviour than add(int index, long o) in
99      * {@link java.util.List}.
100      * Any access to this method should be protected in a synchronized
101      * block on the LongList object.
102      **/

103     public final void add(final int index, final long o) {
104         if (index > size) throw new IndexOutOfBoundsException JavaDoc();
105         if (index >= list.length) resize();
106         if (index == size) {
107             list[size++]=o;
108             return;
109         }
110     
111         java.lang.System.arraycopy(list,index,list,index+1,size-index);
112         list[index]=o;
113         size++;
114     }
115    
116     /**
117      * Adds <var>count</var> elements to the list.
118      * @param at index at which the elements must be inserted. The
119      * first element will be inserted at this index.
120      * @param src An array containing the elements we want to insert.
121      * @param from Index of the first element from <var>src</var> that
122      * must be inserted.
123      * @param count number of elements to insert.
124      * Any access to this method should be protected in a synchronized
125      * block on the LongList object.
126      **/

127     public final void add(final int at,final long[] src, final int from,
128               final int count) {
129     if (count <= 0) return;
130     if (at > size) throw new IndexOutOfBoundsException JavaDoc();
131     ensure(size+count);
132     if (at < size) {
133         java.lang.System.arraycopy(list,at,list,at+count,size-at);
134     }
135     java.lang.System.arraycopy(src,from,list,at,count);
136     size+=count;
137     }
138
139     /**
140      * Any access to this method should be protected in a synchronized
141      * block on the LongList object.
142      **/

143     public final long remove(final int from, final int count) {
144         if (count < 1 || from < 0) return -1;
145         if (from+count > size) return -1;
146
147         final long o = list[from];
148     final int oldsize = size;
149     size = size - count;
150
151         if (from == size) return o;
152     
153         java.lang.System.arraycopy(list,from+count,list,from,
154                                    size-from);
155         return o;
156     }
157
158     /**
159      * Same behaviour than remove(int index) in {@link java.util.List}.
160      * Any access to this method should be protected in a synchronized
161      * block on the LongList object.
162      **/

163     public final long remove(final int index) {
164         if (index >= size) return -1;
165         final long o = list[index];
166         list[index]=0;
167         if (index == --size) return o;
168     
169         java.lang.System.arraycopy(list,index+1,list,index,
170                                    size-index);
171         return o;
172     }
173     
174     /**
175      * Same behaviour than the toArray(long[] a) method in
176      * {@link java.util.List}.
177      * Any access to this method should be protected in a synchronized
178      * block on the LongList object.
179      **/

180     public final long[] toArray(long[] a) {
181         java.lang.System.arraycopy(list,0,a,0,size);
182         return a;
183     }
184
185     /**
186      * Same behaviour than the toArray() method in
187      * {@link java.util.List}.
188      * Any access to this method should be protected in a synchronized
189      * block on the LongList object.
190      **/

191     public final long[] toArray() {
192         return toArray(new long[size]);
193     }
194     
195     /**
196      * Resize the list. Increase its capacity by DELTA elements.
197      * Any call to this method must be protected by a synchronized
198      * block on this LongList.
199      **/

200     private final void resize() {
201         final long[] newlist = allocate(list.length + DELTA);
202         java.lang.System.arraycopy(list,0,newlist,0,size);
203         list = newlist;
204     }
205     
206     /**
207      * Resize the list. Insure that the new length will be at
208      * least equal to <var>length</var>.
209      * @param length new minimal length requested.
210      * Any call to this method must be protected by a synchronized
211      * block on this LongList.
212      **/

213     private final void ensure(int length) {
214     if (list.length < length) {
215         final int min = list.length+DELTA;
216         length=(length<min)?min:length;
217         final long[] newlist = allocate(length);
218         java.lang.System.arraycopy(list,0,newlist,0,size);
219         list = newlist;
220     }
221     }
222     
223     /**
224      * Allocate a new array of object of specified length.
225      **/

226     private final long[] allocate(final int length) {
227         return new long[length];
228     }
229     
230 }
231
232 /**
233  * Oid Checker makes use of ACM to check each OID during the getnext process.
234  */

235 class AcmChecker {
236
237
238     SnmpAccessControlModel model = null;
239     String JavaDoc principal = null;
240     int securityLevel = -1;
241     int version = -1;
242     int pduType = -1;
243     int securityModel = -1;
244     byte[] contextName = null;
245     SnmpEngineImpl engine = null;
246     LongList l = null;
247     AcmChecker(SnmpMibRequest req) {
248     engine = (SnmpEngineImpl) req.getEngine();
249     //We are in V3 architecture, ACM is in the picture.
250
if(engine != null) {
251         if(engine.isCheckOidActivated()) {
252         try {
253             if (isDebugOn())
254             debug("AcmChecker",
255                   " SNMP V3 Access Control to be done.");
256             model = (SnmpAccessControlModel)
257             engine.getAccessControlSubSystem().
258             getModel(SnmpDefinitions.snmpVersionThree);
259             principal = req.getPrincipal();
260             securityLevel = req.getSecurityLevel();
261             pduType = req.getPdu().type;
262             version = req.getRequestPduVersion();
263             securityModel = req.getSecurityModel();
264             contextName = req.getAccessContextName();
265             l = new LongList();
266             if (isDebugOn())
267             debug("AcmChecker",
268                   "Will check oid for : principal : " + principal +
269                   ";securityLevel : " +
270                   securityLevel +";pduType : " + pduType +
271                   ";version : "
272                   + version + ";securityModel : " +
273                   securityModel +";contextName : " +
274                   (contextName == null ? null :
275                    new String JavaDoc(contextName)));
276         }catch(SnmpUnknownModelException e) {
277             if (isDebugOn())
278             debug("AcmChecker",
279                   " Unknown Model, no ACM check.");
280         }
281         }
282     }
283     }
284     
285     void add(int index, long arc) {
286     if(model != null)
287         l.add(index, arc);
288     }
289     
290     void remove(int index) {
291     if(model != null)
292         l.remove(index);
293     }
294     
295     void add(final int at,final long[] src, final int from,
296          final int count) {
297     if(model != null)
298         l.add(at,src,from,count);
299     }
300
301     void remove(final int from, final int count) {
302     if(model != null)
303         l.remove(from,count);
304     }
305
306     void checkCurrentOid() throws SnmpStatusException {
307     if(model != null) {
308         SnmpOid oid = new SnmpOid(l.toArray());
309         if (isDebugOn())
310         debug("check",
311               " Checking access for : " + oid);
312         model.checkAccess(version,
313                   principal,
314                   securityLevel,
315                   pduType,
316                   securityModel,
317                   contextName,
318                   oid);
319     }
320     }
321     
322     // Returns true if debug is on
323
private final static boolean isDebugOn() {
324         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
325     }
326     
327     // Prints a debug message
328
private final static void debug(String JavaDoc func, String JavaDoc info) {
329         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP,
330            "AcmChecker", func, info);
331     }
332 }
333
334 /**
335  * Abstract class for representing an SNMP MIB.
336  * <P>
337  * When compiling a SNMP MIB, among all the classes generated by
338  * <CODE>mibgen</CODE>, there is one which extends <CODE>SnmpMib</CODE>
339  * for representing a whole MIB.
340  * <BR>The class is used by the SNMP protocol adaptor as the entry point in
341  * the MIB.
342  *
343  * <p>This generated class can be subclassed in your code in order to
344  * plug in your own specific behaviour.
345  * </p>
346  *
347  * <p><b>This API is a Sun Microsystems internal API and is subject
348  * to change without notice.</b></p>
349  * @version 4.29 02/25/04
350  * @author Sun Microsystems, Inc
351  */

352 public abstract class SnmpMib extends SnmpMibAgent implements Serializable JavaDoc {
353   
354     /**
355      * Default constructor.
356      * Initializes the OID tree.
357      */

358     public SnmpMib() {
359         root= new SnmpMibOid();
360     }
361
362
363     // --------------------------------------------------------------------
364
// POLYMORHIC METHODS
365
// --------------------------------------------------------------------
366

367     /**
368      * <p>
369      * This callback should return the OID associated to the group
370      * identified by the given <code>groupName</code>.
371      * </p>
372      *
373      * <p>
374      * This method is provided as a hook to plug-in some custom
375      * specific behavior. Although doing so is discouraged you might
376      * want to subclass this method in order to store & provide more metadata
377      * information (mapping OID <-> symbolic name) within the agent,
378      * or to "change" the root of the MIB OID by prefixing the
379      * defaultOid by an application dependant OID string, for instance.
380      * </p>
381      *
382      * <p>
383      * The default implementation of this method is to return the given
384      * <code>defaultOid</code>
385      * </p>
386      *
387      * @param groupName The java-ized name of the SNMP group.
388      * @param defaultOid The OID defined in the MIB for that group
389      * (in dot notation).
390      *
391      * @return The OID of the group identified by <code>groupName</code>,
392      * in dot-notation.
393      */

394     protected String JavaDoc getGroupOid(String JavaDoc groupName, String JavaDoc defaultOid) {
395     return defaultOid;
396     }
397
398     /**
399      * <p>
400      * This callback should return the ObjectName associated to the
401      * group identified by the given <code>groupName</code>.
402      * </p>
403      *
404      * <p>
405      * This method is provided as a hook to plug-in some custom
406      * specific behavior. You might want to override this method
407      * in order to provide a different object naming scheme than
408      * that proposed by default by <code>mibgen</code>.
409      * </p>
410      *
411      * <p>
412      * This method is only meaningful if the MIB is registered
413      * in the MBeanServer, otherwise, it will not be called.
414      * </p>
415      *
416      * <p>
417      * The default implementation of this method is to return an ObjectName
418      * built from the given <code>defaultName</code>.
419      * </p>
420      *
421      * @param name The java-ized name of the SNMP group.
422      * @param oid The OID returned by getGroupOid() - in dot notation.
423      * @param defaultName The name by default generated by <code>
424      * mibgen</code>
425      *
426      * @return The ObjectName of the group identified by <code>name</code>
427      */

428     protected ObjectName JavaDoc getGroupObjectName(String JavaDoc name, String JavaDoc oid,
429                         String JavaDoc defaultName)
430     throws MalformedObjectNameException JavaDoc {
431     return new ObjectName JavaDoc(defaultName);
432     }
433
434     /**
435      * <p>
436      * Register an SNMP group and its metadata node in the MIB.
437      * </p>
438      *
439      * <p>
440      * This method is provided as a hook to plug-in some custom
441      * specific behavior. You might want to override this method
442      * if you want to set special links between the MBean, its metadata
443      * node, its OID or ObjectName etc..
444      * </p>
445      *
446      * <p>
447      * If the MIB is not registered in the MBeanServer, the <code>
448      * server</code> and <code>groupObjName</code> parameters will be
449      * <code>null</code>.<br>
450      * If the given group MBean is not <code>null</code>, and if the
451      * <code>server</code> and <code>groupObjName</code> parameters are
452      * not null, then this method will also automatically register the
453      * group MBean with the given MBeanServer <code>server</code>.
454      * </p>
455      *
456      * @param groupName The java-ized name of the SNMP group.
457      * @param groupOid The OID as returned by getGroupOid() - in dot
458      * notation.
459      * @param groupObjName The ObjectName as returned by getGroupObjectName().
460      * This parameter may be <code>null</code> if the
461      * MIB is not registered in the MBeanServer.
462      * @param node The metadata node, as returned by the metadata
463      * factory method for this group.
464      * @param group The MBean for this group, as returned by the
465      * MBean factory method for this group.
466      * @param server The MBeanServer in which the groups are to be
467      * registered. This parameter will be <code>null</code>
468      * if the MIB is not registered, otherwise it is a
469      * reference to the MBeanServer in which the MIB is
470      * registered.
471      *
472      */

473     protected void registerGroupNode(String JavaDoc groupName, String JavaDoc groupOid,
474                      ObjectName JavaDoc groupObjName, SnmpMibNode node,
475                      Object JavaDoc group, MBeanServer JavaDoc server)
476     throws NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc,
477     InstanceAlreadyExistsException JavaDoc, IllegalAccessException JavaDoc {
478     root.registerNode(groupOid,node);
479     if (server != null && groupObjName != null && group != null)
480         server.registerMBean(group,groupObjName);
481     }
482  
483     /**
484      * <p>
485      * Register an SNMP Table metadata node in the MIB.
486      * </p>
487      *
488      * <p>
489      * <b><i>
490      * This method is used internally and you should never need to
491      * call it directly.</i></b><br> It is used to establish the link
492      * between an SNMP table metadata node and its bean-like counterpart.
493      * <br>
494      * The group metadata nodes will create and register their
495      * underlying table metadata nodes in the MIB using this
496      * method. <br>
497      * The metadata nodes will be later retrieved from the MIB by the
498      * bean-like table objects using the getRegisterTableMeta() method.
499      * </p>
500      *
501      * @param name The java-ized name of the SNMP table.
502      * @param table The SNMP table metadata node - usually this
503      * corresponds to a <code>mibgen</code> generated
504      * object.
505      */

506     public abstract void registerTableMeta(String JavaDoc name, SnmpMibTable table);
507
508     /**
509      * Returns a registered SNMP Table metadata node.
510      *
511      * <p><b><i>
512      * This method is used internally and you should never need to
513      * call it directly.
514      * </i></b></p>
515      *
516      */

517     public abstract SnmpMibTable getRegisteredTableMeta(String JavaDoc name);
518
519     // --------------------------------------------------------------------
520
// PUBLIC METHODS
521
// --------------------------------------------------------------------
522

523     /**
524      * Processes a <CODE>get</CODE> operation.
525      *
526      **/

527     // Implements the method defined in SnmpMibAgent. See SnmpMibAgent
528
// for java-doc
529
//
530
public void get(SnmpMibRequest req) throws SnmpStatusException {
531
532     // Builds the request tree: creation is not allowed, operation
533
// is not atomic.
534

535     final int reqType = SnmpDefinitions.pduGetRequestPdu;
536     SnmpRequestTree handlers = getHandlers(req,false,false,reqType);
537
538     SnmpRequestTree.Handler h = null;
539     SnmpMibNode meta = null;
540
541     if (isDebugOn())
542         debug("get","Processing handlers for GET... ");
543
544     // For each sub-request stored in the request-tree, invoke the
545
// get() method.
546
for (Enumeration JavaDoc eh=handlers.getHandlers();eh.hasMoreElements();) {
547         h = (SnmpRequestTree.Handler) eh.nextElement();
548
549         // Gets the Meta node. It can be either a Group Meta or a
550
// Table Meta.
551
//
552
meta = handlers.getMetaNode(h);
553
554         // Gets the depth of the Meta node in the OID tree
555
final int depth = handlers.getOidDepth(h);
556
557         for (Enumeration JavaDoc rqs=handlers.getSubRequests(h);
558          rqs.hasMoreElements();) {
559
560         // Invoke the get() operation.
561
meta.get((SnmpMibSubRequest)rqs.nextElement(),depth);
562         }
563     }
564     }
565
566     /**
567      * Processes a <CODE>set</CODE> operation.
568      *
569      */

570     // Implements the method defined in SnmpMibAgent. See SnmpMibAgent
571
// for java-doc
572
//
573
public void set(SnmpMibRequest req) throws SnmpStatusException {
574
575     SnmpRequestTree handlers = null;
576
577     // Optimization: we're going to get the whole SnmpRequestTree
578
// built in the "check" method, so that we don't have to rebuild
579
// it here.
580
//
581
if (req instanceof SnmpMibRequestImpl)
582         handlers = ((SnmpMibRequestImpl)req).getRequestTree();
583
584     // Optimization didn't work: we have to rebuild the tree.
585
//
586
// Builds the request tree: creation is not allowed, operation
587
// is atomic.
588
//
589
final int reqType = SnmpDefinitions.pduSetRequestPdu;
590     if (handlers == null) handlers = getHandlers(req,false,true,reqType);
591     handlers.switchCreationFlag(false);
592     handlers.setPduType(reqType);
593
594     SnmpRequestTree.Handler h = null;
595     SnmpMibNode meta = null;
596
597     if (isDebugOn())
598         debug("set","Processing handlers for SET... ");
599
600     // For each sub-request stored in the request-tree, invoke the
601
// get() method.
602
for (Enumeration JavaDoc eh=handlers.getHandlers();eh.hasMoreElements();) {
603         h = (SnmpRequestTree.Handler) eh.nextElement();
604
605         // Gets the Meta node. It can be either a Group Meta or a
606
// Table Meta.
607
//
608
meta = handlers.getMetaNode(h);
609
610         // Gets the depth of the Meta node in the OID tree
611
final int depth = handlers.getOidDepth(h);
612
613         for (Enumeration JavaDoc rqs=handlers.getSubRequests(h);
614          rqs.hasMoreElements();) {
615
616         // Invoke the set() operation
617
meta.set((SnmpMibSubRequest)rqs.nextElement(),depth);
618         }
619     }
620     }
621
622     /**
623      * Checks if a <CODE>set</CODE> operation can be performed.
624      * If the operation cannot be performed, the method will raise a
625      * <CODE>SnmpStatusException</CODE>.
626      *
627      */

628     // Implements the method defined in SnmpMibAgent. See SnmpMibAgent
629
// for java-doc
630
//
631
public void check(SnmpMibRequest req) throws SnmpStatusException {
632
633     final int reqType = SnmpDefinitions.pduWalkRequest;
634     // Builds the request tree: creation is allowed, operation
635
// is atomic.
636
SnmpRequestTree handlers = getHandlers(req,true,true,reqType);
637
638     SnmpRequestTree.Handler h = null;
639     SnmpMibNode meta = null;
640
641     if (isDebugOn())
642         debug("check","Processing handlers for CHECK... ");
643
644     // For each sub-request stored in the request-tree, invoke the
645
// check() method.
646
for (Enumeration JavaDoc eh=handlers.getHandlers();eh.hasMoreElements();) {
647         h = (SnmpRequestTree.Handler) eh.nextElement();
648
649         // Gets the Meta node. It can be either a Group Meta or a
650
// Table Meta.
651
//
652
meta = handlers.getMetaNode(h);
653
654         // Gets the depth of the Meta node in the OID tree
655
final int depth = handlers.getOidDepth(h);
656
657         for (Enumeration JavaDoc rqs=handlers.getSubRequests(h);
658          rqs.hasMoreElements();) {
659
660         // Invoke the check() operation
661
meta.check((SnmpMibSubRequest)rqs.nextElement(),depth);
662         }
663     }
664
665     // Optimization: we're going to pass the whole SnmpRequestTree
666
// to the "set" method, so that we don't have to rebuild it there.
667
//
668
if (req instanceof SnmpMibRequestImpl) {
669         ((SnmpMibRequestImpl)req).setRequestTree(handlers);
670     }
671
672     }
673
674     /**
675      * Processes a <CODE>getNext</CODE> operation.
676      *
677      */

678     // Implements the method defined in SnmpMibAgent. See SnmpMibAgent
679
// for java-doc
680
//
681
public void getNext(SnmpMibRequest req) throws SnmpStatusException {
682     // Build the request tree for the operation
683
// The subrequest stored in the request tree are valid GET requests
684
SnmpRequestTree handlers = getGetNextHandlers(req);
685
686     SnmpRequestTree.Handler h = null;
687     SnmpMibNode meta = null;
688
689     if (isDebugOn())
690         debug("getNext","Processing handlers for GET-NEXT... ");
691
692     // Now invoke get() for each subrequest of the request tree.
693
for (Enumeration JavaDoc eh=handlers.getHandlers();eh.hasMoreElements();) {
694         h = (SnmpRequestTree.Handler) eh.nextElement();
695         
696         // Gets the Meta node. It can be either a Group Meta or a
697
// Table Meta.
698
//
699
meta = handlers.getMetaNode(h);
700
701         // Gets the depth of the Meta node in the OID tree
702
int depth = handlers.getOidDepth(h);
703
704         for (Enumeration JavaDoc rqs=handlers.getSubRequests(h);
705          rqs.hasMoreElements();) {
706
707         // Invoke the get() operation
708
meta.get((SnmpMibSubRequest)rqs.nextElement(),depth);
709         }
710     }
711     }
712
713     
714     /**
715      * Processes a <CODE>getBulk</CODE> operation.
716      * The method implements the <CODE>getBulk</CODE> operation by calling
717      * appropriately the <CODE>getNext</CODE> method.
718      *
719      */

720     // Implements the method defined in SnmpMibAgent. See SnmpMibAgent
721
// for java-doc
722
//
723
public void getBulk(SnmpMibRequest req, int nonRepeat, int maxRepeat)
724     throws SnmpStatusException {
725      
726         getBulkWithGetNext(req, nonRepeat, maxRepeat);
727     }
728     
729     /**
730      * Gets the root object identifier of the MIB.
731      * <P>In order to be accurate, the method should be called once the
732      * MIB is fully initialized (that is, after a call to <CODE>init</CODE>
733      * or <CODE>preRegister</CODE>).
734      *
735      * @return The root object identifier.
736      */

737     public long[] getRootOid() {
738         
739         if( rootOid == null) {
740             Vector JavaDoc list= new Vector JavaDoc(10);
741
742             // Ask the tree to do the job !
743
//
744
root.getRootOid(list);
745
746             // Now format the result
747
//
748
rootOid= new long[list.size()];
749             int i=0;
750             for(Enumeration JavaDoc e= list.elements(); e.hasMoreElements(); ) {
751                 Integer JavaDoc val= (Integer JavaDoc) e.nextElement();
752                 rootOid[i++]= val.longValue();
753             }
754         }
755         return rootOid;
756
757     }
758   
759     // --------------------------------------------------------------------
760
// PRIVATE METHODS
761
//---------------------------------------------------------------------
762

763     /**
764      * This method builds the temporary request-tree that will be used to
765      * perform the SNMP request associated with the given vector of varbinds
766      * `list'.
767      *
768      * @param req The SnmpMibRequest object holding the varbind list
769      * concerning this MIB.
770      * @param createflag Indicates whether the operation allow for creation
771      * of new instances (ie: it is a SET).
772      * @param atomic Indicates whether the operation is atomic or not.
773      * @param type Request type (from SnmpDefinitions).
774      *
775      * @return The request-tree where the original varbind list has been
776      * dispatched to the appropriate nodes.
777      */

778     private SnmpRequestTree getHandlers(SnmpMibRequest req,
779                     boolean createflag, boolean atomic,
780                     int type)
781     throws SnmpStatusException {
782
783     // Build an empty request tree
784
SnmpRequestTree handlers =
785         new SnmpRequestTree(req,createflag,type);
786
787         int index=0;
788         SnmpVarBind var = null;
789         final int ver= req.getVersion();
790
791     // For each varbind in the list finds its handling node.
792
for (Enumeration JavaDoc e= req.getElements(); e.hasMoreElements(); index++) {
793
794             var= (SnmpVarBind) e.nextElement();
795
796             try {
797         // Find the handling node for this varbind.
798
root.findHandlingNode(var,var.oid.longValue(false),
799                       0,handlers);
800             } catch(SnmpStatusException x) {
801
802         if (isDebugOn())
803             debug("getHandlers","Couldn't find a handling node for "
804               + var.oid.toString());
805
806         // If the operation is atomic (Check/Set) or the version
807
// is V1 we must generate an exception.
808
//
809
if (ver == SnmpDefinitions.snmpVersionOne) {
810
811             if (isDebugOn())
812             debug("getHandlers","\tV1: Throwing exception");
813
814             // The index in the exception must correspond to the
815
// SNMP index ...
816
//
817
final SnmpStatusException sse =
818             new SnmpStatusException(x, index + 1);
819             sse.initCause(x);
820             throw sse;
821         } else if ((type == SnmpDefinitions.pduWalkRequest) ||
822                (type == SnmpDefinitions.pduSetRequestPdu)) {
823             final int status =
824             SnmpRequestTree.mapSetException(x.getStatus(),ver);
825
826             if (isDebugOn())
827             debug("getHandlers","\tSET: Throwing exception");
828
829             final SnmpStatusException sse =
830             new SnmpStatusException(status, index + 1);
831             sse.initCause(x);
832             throw sse;
833         } else if (atomic) {
834
835             // Should never come here...
836
if (isDebugOn())
837             debug("getHandlers","\tATOMIC: Throwing exception");
838
839             final SnmpStatusException sse =
840             new SnmpStatusException(x, index + 1);
841             sse.initCause(x);
842             throw sse;
843         }
844
845         final int status =
846             SnmpRequestTree.mapGetException(x.getStatus(),ver);
847             
848         if (status == SnmpStatusException.noSuchInstance) {
849
850             if (isDebugOn())
851             debug("getHandlers",
852                   "\tGET: Registering noSuchInstance");
853
854             var.value= SnmpVarBind.noSuchInstance;
855
856         } else if (status == SnmpStatusException.noSuchObject) {
857             
858             if (isDebugOn())
859             debug("getHandlers",
860                   "\tGET: Registering noSuchObject");
861
862             var.value= SnmpVarBind.noSuchObject;
863
864         } else {
865
866             if (isDebugOn())
867             debug("getHandlers",
868                   "\tGET: Registering global error: "
869                   + status);
870
871             final SnmpStatusException sse =
872             new SnmpStatusException(status, index + 1);
873             sse.initCause(x);
874             throw sse;
875         }
876         }
877         }
878     return handlers;
879     }
880
881     /**
882      * This method builds the temporary request-tree that will be used to
883      * perform the SNMP GET-NEXT request associated with the given vector
884      * of varbinds `list'.
885      *
886      * @param req The SnmpMibRequest object holding the varbind list
887      * concerning this MIB.
888      *
889      * @return The request-tree where the original varbind list has been
890      * dispatched to the appropriate nodes, and where the original
891      * OIDs have been replaced with the correct "next" OID.
892      */

893     private SnmpRequestTree getGetNextHandlers(SnmpMibRequest req)
894     throws SnmpStatusException {
895
896     // Creates an empty request tree, no entry creation is allowed (false)
897
SnmpRequestTree handlers = new
898         SnmpRequestTree(req,false,SnmpDefinitions.pduGetNextRequestPdu);
899
900     // Sets the getNext flag: if version=V2, status exception are
901
// transformed in endOfMibView
902
handlers.setGetNextFlag();
903
904     if (isDebugOn())
905         debug("getGetNextHandlers","Received MIB request : " + req);
906     AcmChecker checker = new AcmChecker(req);
907     int index=0;
908         SnmpVarBind var = null;
909         final int ver= req.getVersion();
910     SnmpOid original = null;
911     // For each varbind, finds the handling node.
912
// This function has the side effect of transforming a GET-NEXT
913
// request into a valid GET request, replacing the OIDs in the
914
// original GET-NEXT request with the OID of the first leaf that
915
// follows.
916
for (Enumeration JavaDoc e= req.getElements(); e.hasMoreElements(); index++) {
917
918             var = (SnmpVarBind) e.nextElement();
919         SnmpOid result = null;
920             try {
921         // Find the node handling the OID that follows the varbind
922
// OID. `result' contains this next leaf OID.
923
//ACM loop.
924
if (isDebugOn())
925             debug("getGetNextHandlers"," Next Oid of :" + var.oid);
926         result = new SnmpOid(root.findNextHandlingNode
927                      (var,var.oid.longValue(false),0,
928                       0,handlers, checker));
929         
930         if (isDebugOn())
931             debug("getGetNextHandlers"," is :" + result);
932         // We replace the varbind original OID with the OID of the
933
// leaf object we have to return.
934
var.oid = result;
935             } catch(SnmpStatusException x) {
936
937         // if (isDebugOn())
938
// debug("getGetNextHandlers",
939
// "Couldn't find a handling node for "
940
// + var.oid.toString());
941

942                 if (ver == SnmpDefinitions.snmpVersionOne) {
943             if (isDebugOn())
944             debug("getGetNextHandlers","\tThrowing exception" +
945                   x.toString());
946             // The index in the exception must correspond to the
947
// SNMP index ...
948
//
949
throw new SnmpStatusException(x, index + 1);
950         }
951         if (isDebugOn())
952             debug("getGetNextHandlers","Exception : " + x.getStatus());
953         
954                 var.setSnmpValue(SnmpVarBind.endOfMibView);
955             }
956         }
957     return handlers;
958     }
959     
960     // Returns true if debug is on
961
private final static boolean isDebugOn() {
962         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
963     }
964
965     // Prints a debug message
966
private final static void debug(String JavaDoc func, String JavaDoc info) {
967         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP,
968            "SnmpMib", func, info);
969     }
970
971     // --------------------------------------------------------------------
972
// PROTECTED VARIABLES
973
// --------------------------------------------------------------------
974

975     /**
976      * The top element in the Mib tree.
977      * @serial
978      */

979     protected SnmpMibOid root;
980
981   
982     // --------------------------------------------------------------------
983
// PRIVATE VARIABLES
984
// --------------------------------------------------------------------
985

986     /**
987      * The root object identifier of the MIB.
988      */

989     private transient long[] rootOid= null;
990 }
991
Popular Tags