KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPv2BulkRequestPDU


1 /*
2  * SNMP Package
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30
31 package snmp;
32
33 import java.math.BigInteger JavaDoc;
34 import java.util.Vector JavaDoc;
35
36
37
38
39 /**
40 * The SNMPv2BulkRequestPDU class represents an SNMPv2 Bulk Request PDU from RFC 1905, as indicated below. This
41 * forms the payload of an SNMPv2 Bulk Request message.
42
43 -- protocol data units
44
45           3. Definitions
46
47                SNMPv2-PDU DEFINITIONS ::= BEGIN
48
49                IMPORTS
50                    ObjectName, ObjectSyntax, Integer32
51                        FROM SNMPv2-SMI;
52
53                -- protocol data units
54
55                PDUs ::=
56                    CHOICE {
57                        get-request
58                            GetRequest-PDU,
59
60                        get-next-request
61                            GetNextRequest-PDU,
62
63                        get-bulk-request
64                            GetBulkRequest-PDU,
65
66                        response
67                            Response-PDU,
68
69                        set-request
70                            SetRequest-PDU,
71
72                        inform-request
73                            InformRequest-PDU,
74
75                        snmpV2-trap
76                            SNMPv2-Trap-PDU
77                    }
78
79           
80                -- PDUs
81
82                GetRequest-PDU ::=
83                    [0]
84                        IMPLICIT PDU
85
86                GetNextRequest-PDU ::=
87                    [1]
88                        IMPLICIT PDU
89
90                Response-PDU ::=
91                    [2]
92                        IMPLICIT PDU
93
94                SetRequest-PDU ::=
95                    [3]
96                        IMPLICIT PDU
97
98                -- [4] is obsolete
99
100                GetBulkRequest-PDU ::=
101                    [5]
102                        IMPLICIT BulkPDU
103
104                InformRequest-PDU ::=
105                    [6]
106                        IMPLICIT PDU
107
108                SNMPv2-Trap-PDU ::=
109                    [7]
110                        IMPLICIT PDU
111
112           
113                max-bindings
114                    INTEGER ::= 2147483647
115
116                
117                BulkPDU ::= -- MUST be identical in
118                     SEQUENCE { -- structure to PDU
119                         
120                         request-id Integer32,
121
122                         non-repeaters INTEGER (0..max-bindings),
123
124                         max-repetitions INTEGER (0..max-bindings),
125
126                         variable-bindings VarBindList -- values are ignored
127                         
128          }
129
130 */

131
132
133 public class SNMPv2BulkRequestPDU extends SNMPSequence
134 {
135     
136     
137     /**
138     * Create a new PDU of the specified type, with given request ID, non-repeaters, and max-repetitions fields,
139     * and containing the supplied SNMP sequence as data.
140     */

141     
142     public SNMPv2BulkRequestPDU(int requestID, int nonRepeaters, int maxRepetitions, SNMPSequence varList)
143         throws SNMPBadValueException
144     {
145         super();
146         Vector JavaDoc contents = new Vector JavaDoc();
147         tag = SNMPBERCodec.SNMPv2BULKREQUEST;
148         contents.insertElementAt(new SNMPInteger(requestID), 0);
149         contents.insertElementAt(new SNMPInteger(nonRepeaters), 1);
150         contents.insertElementAt(new SNMPInteger(maxRepetitions), 2);
151         contents.insertElementAt(varList, 3);
152         this.setValue(contents);
153     }
154     
155     
156     
157     
158     /**
159     * Create a new PDU of the specified type from the supplied BER encoding.
160     * @throws SNMPBadValueException Indicates invalid SNMP Bulk PDU encoding supplied in enc.
161     */

162     
163     protected SNMPv2BulkRequestPDU(byte[] enc, byte pduType)
164         throws SNMPBadValueException
165     {
166         tag = pduType;
167         extractFromBEREncoding(enc);
168         
169         // validate the message: make sure we have the appropriate pieces
170
Vector JavaDoc contents = (Vector JavaDoc)(this.getValue());
171         
172         if (contents.size() != 4)
173         {
174             throw new SNMPBadValueException("Bad Bulk Request PDU");
175         }
176         
177         if (!(contents.elementAt(0) instanceof SNMPInteger))
178         {
179             throw new SNMPBadValueException("Bad Bulk Request PDU: bad request ID");
180         }
181         
182         if (!(contents.elementAt(1) instanceof SNMPInteger))
183         {
184             throw new SNMPBadValueException("Bad Bulk Request PDU: bad non-repeaters field");
185         }
186         
187         if (!(contents.elementAt(2) instanceof SNMPInteger))
188         {
189             throw new SNMPBadValueException("Bad Bulk Request PDU: bad max-repetitions field");
190         }
191         
192         if (!(contents.elementAt(3) instanceof SNMPSequence))
193         {
194             throw new SNMPBadValueException("Bad Bulk Request PDU: bad variable binding list");
195         }
196         
197         // now validate the variable binding list: should be list of sequences which
198
// are (OID, value) pairs
199
SNMPSequence varBindList = this.getVarBindList();
200         for (int i = 0; i < varBindList.size(); i++)
201         {
202             SNMPObject element = varBindList.getSNMPObjectAt(i);
203             
204             // must be a two-element sequence
205
if (!(element instanceof SNMPSequence))
206             {
207                 throw new SNMPBadValueException("Bad Bulk Request PDU: bad variable binding at index" + i);
208             }
209             
210             // variable binding sequence must have 2 elements, first of which must be an object identifier
211
SNMPSequence varBind = (SNMPSequence)element;
212             if ((varBind.size() != 2) || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier))
213             {
214                 throw new SNMPBadValueException("Bad Bulk Request PDU: bad variable binding at index" + i);
215             }
216         }
217         
218         
219     }
220     
221     
222     
223     
224     /**
225     * A utility method that extracts the variable binding list from the pdu. Useful for retrieving
226     * the set of (object identifier, value) pairs returned in response to a request to an SNMP
227     * device. The variable binding list is just an SNMP sequence containing the identifier, value pairs.
228     * @see snmp.SNMPVarBindList
229     */

230     
231     public SNMPSequence getVarBindList()
232     {
233         Vector JavaDoc contents = (Vector JavaDoc)(this.getValue());
234         return (SNMPSequence)(contents.elementAt(3));
235     }
236     
237     
238     
239     /**
240     * A utility method that extracts the request ID number from this PDU.
241     */

242     
243     public int getRequestID()
244     {
245         Vector JavaDoc contents = (Vector JavaDoc)(this.getValue());
246         return ((BigInteger JavaDoc)((SNMPInteger)(contents.elementAt(0))).getValue()).intValue();
247     }
248     
249     
250     
251     /**
252     * A utility method that extracts the non-repeaters field for this PDU.
253     */

254     
255     public int getNonRepeaters()
256     {
257         Vector JavaDoc contents = (Vector JavaDoc)(this.getValue());
258         return ((BigInteger JavaDoc)((SNMPInteger)(contents.elementAt(1))).getValue()).intValue();
259     }
260     
261     
262     
263     /**
264     * A utility method that returns the max-repetitions field for this PDU.
265     */

266     
267     public int getMaxRepetitions()
268     {
269         Vector JavaDoc contents = (Vector JavaDoc)(this.getValue());
270         return ((BigInteger JavaDoc)((SNMPInteger)(contents.elementAt(2))).getValue()).intValue();
271     }
272     
273     
274     
275     /**
276     * A utility method that returns the PDU type of this PDU.
277     */

278     
279     public byte getPDUType()
280     {
281         return tag;
282     }
283     
284     
285 }
Popular Tags