KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPv2TrapPDU


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
34
35
36 /**
37 * The SNMPv2TrapPDU class represents an SNMPv2 Trap PDU from RFC 1448, as indicated below. This
38 * forms the payload of an SNMPv2 Trap message.
39
40 -- protocol data units
41
42           3. Definitions
43
44                SNMPv2-PDU DEFINITIONS ::= BEGIN
45
46                IMPORTS
47                    ObjectName, ObjectSyntax, Integer32
48                        FROM SNMPv2-SMI;
49
50                -- protocol data units
51
52                PDUs ::=
53                    CHOICE {
54                        get-request
55                            GetRequest-PDU,
56
57                        get-next-request
58                            GetNextRequest-PDU,
59
60                        get-bulk-request
61                            GetBulkRequest-PDU,
62
63                        response
64                            Response-PDU,
65
66                        set-request
67                            SetRequest-PDU,
68
69                        inform-request
70                            InformRequest-PDU,
71
72                        snmpV2-trap
73                            SNMPv2-Trap-PDU
74                    }
75
76           
77                -- PDUs
78
79                GetRequest-PDU ::=
80                    [0]
81                        IMPLICIT PDU
82
83                GetNextRequest-PDU ::=
84                    [1]
85                        IMPLICIT PDU
86
87                Response-PDU ::=
88                    [2]
89                        IMPLICIT PDU
90
91                SetRequest-PDU ::=
92                    [3]
93                        IMPLICIT PDU
94
95                -- [4] is obsolete
96
97                GetBulkRequest-PDU ::=
98                    [5]
99                        IMPLICIT BulkPDU
100
101                InformRequest-PDU ::=
102                    [6]
103                        IMPLICIT PDU
104
105                SNMPv2-Trap-PDU ::=
106                    [7]
107                        IMPLICIT PDU
108
109           
110                max-bindings
111                    INTEGER ::= 2147483647
112
113                PDU ::=
114                    SEQUENCE {
115                        request-id
116                            Integer32,
117
118                        error-status -- sometimes ignored
119                            INTEGER {
120                                noError(0),
121                                tooBig(1),
122                                noSuchName(2), -- for proxy compatibility
123                                badValue(3), -- for proxy compatibility
124                                readOnly(4), -- for proxy compatibility
125                                genErr(5),
126                                noAccess(6),
127                                wrongType(7),
128                                wrongLength(8),
129                                wrongEncoding(9),
130                                wrongValue(10),
131                                noCreation(11),
132                                inconsistentValue(12),
133                                resourceUnavailable(13),
134                                commitFailed(14),
135                                undoFailed(15),
136                                authorizationError(16),
137                                notWritable(17),
138                                inconsistentName(18)
139                            },
140
141                        error-index -- sometimes ignored
142                            INTEGER (0..max-bindings),
143
144                        variable-bindings -- values are sometimes ignored
145                            VarBindList
146                    }
147
148 */

149
150
151 public class SNMPv2TrapPDU extends SNMPPDU
152 {
153     
154     
155     
156     /**
157     * Create a new Trap PDU with given trapOID and sysUptime,
158     * and containing the supplied SNMP sequence as data.
159     */

160     
161     public SNMPv2TrapPDU(SNMPTimeTicks sysUptime, SNMPObjectIdentifier snmpTrapOID, SNMPSequence varList)
162         throws SNMPBadValueException
163     {
164         super(SNMPBERCodec.SNMPv2TRAP, 0, 0, 0, varList);
165         
166         // create a variable pair for sysUptime, and insert into varBindList
167
SNMPObjectIdentifier sysUptimeOID = new SNMPObjectIdentifier("1.3.6.1.2.1.1.3.0");
168         SNMPVariablePair sysUptimePair = new SNMPVariablePair(sysUptimeOID, sysUptime);
169         varList.insertSNMPObjectAt(sysUptimePair, 0);
170         
171         // create a variable pair for snmpTrapOID, and insert into varBindList
172
SNMPObjectIdentifier snmpTrapOIDOID = new SNMPObjectIdentifier("1.3.6.1.6.3.1.1.4.1.0");
173         SNMPVariablePair snmpOIDPair = new SNMPVariablePair(snmpTrapOIDOID, snmpTrapOID);
174         varList.insertSNMPObjectAt(snmpOIDPair, 1);
175         
176     }
177     
178     
179     
180     /**
181     * Create a new Trap PDU with given trapOID and sysUptime,
182     * and containing an empty SNMP sequence (VarBindList) as additional data.
183     */

184     
185     public SNMPv2TrapPDU(SNMPObjectIdentifier snmpTrapOID, SNMPTimeTicks sysUptime)
186         throws SNMPBadValueException
187     {
188         this(sysUptime, snmpTrapOID, new SNMPSequence());
189     }
190     
191     
192     
193     
194     /**
195     * Create a new PDU of the specified type from the supplied BER encoding.
196     * @throws SNMPBadValueException Indicates invalid SNMP PDU encoding supplied in enc.
197     */

198     
199     protected SNMPv2TrapPDU(byte[] enc)
200         throws SNMPBadValueException
201     {
202         super(enc, SNMPBERCodec.SNMPv2TRAP);
203         
204         // validate the message: make sure the first two components of the varBindList
205
// are the appropriate variable pairs
206
SNMPSequence varBindList = this.getVarBindList();
207         
208         
209         if (varBindList.size() < 2)
210         {
211             throw new SNMPBadValueException("Bad v2 Trap PDU: missing snmpTrapOID or sysUptime");
212         }
213         
214         // validate that the first variable binding is the sysUptime
215
SNMPSequence variablePair = (SNMPSequence)varBindList.getSNMPObjectAt(0);
216         SNMPObjectIdentifier oid = (SNMPObjectIdentifier)variablePair.getSNMPObjectAt(0);
217         SNMPObject value = variablePair.getSNMPObjectAt(1);
218         SNMPObjectIdentifier sysUptimeOID = new SNMPObjectIdentifier("1.3.6.1.2.1.1.3.0");
219         if (!(value instanceof SNMPTimeTicks) || !oid.equals(sysUptimeOID))
220         {
221             throw new SNMPBadValueException("Bad v2 Trap PDU: bad sysUptime in variable binding list");
222         }
223         
224         // validate that the second variable binding is the snmpTrapOID
225
variablePair = (SNMPSequence)varBindList.getSNMPObjectAt(1);
226         oid = (SNMPObjectIdentifier)variablePair.getSNMPObjectAt(0);
227         value = variablePair.getSNMPObjectAt(1);
228         SNMPObjectIdentifier snmpTrapOIDOID = new SNMPObjectIdentifier("1.3.6.1.6.3.1.1.4.1.0");
229         if (!(value instanceof SNMPObjectIdentifier) || !oid.equals(snmpTrapOIDOID))
230         {
231             throw new SNMPBadValueException("Bad v2 Trap PDU: bad snmpTrapOID in variable binding list");
232         }
233         
234     }
235     
236     
237     
238     
239     /**
240     * A utility method that extracts the snmpTrapOID from the variable bind list (it's the second of the
241     * variable pairs).
242     */

243     
244     public SNMPObjectIdentifier getSNMPTrapOID()
245     {
246         SNMPSequence contents = this.getVarBindList();
247         SNMPSequence variablePair = (SNMPSequence)contents.getSNMPObjectAt(1);
248         return (SNMPObjectIdentifier)variablePair.getSNMPObjectAt(1);
249     }
250     
251     
252     
253     /**
254     * A utility method that extracts the sysUptime from the variable bind list (it's the first of the
255     * variable pairs).
256     */

257     
258     public SNMPTimeTicks getSysUptime()
259     {
260         SNMPSequence contents = this.getVarBindList();
261         SNMPSequence variablePair = (SNMPSequence)contents.getSNMPObjectAt(0);
262         return (SNMPTimeTicks)variablePair.getSNMPObjectAt(1);
263     }
264     
265     
266 }
Popular Tags