KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > PDUv1


1 /*_############################################################################
2   _##
3   _## SNMP4J - PDUv1.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j;
22
23 import java.io.*;
24 import java.util.*;
25 import org.snmp4j.asn1.*;
26 import org.snmp4j.asn1.BER.*;
27 import org.snmp4j.smi.*;
28 import org.snmp4j.smi.OID;
29 // for JavaDoc
30
import org.snmp4j.mp.SnmpConstants;
31
32 /**
33  * The <code>PDUv1</class> represents SNMPv1 PDUs. The behavior of this class
34  * is identical to its superclass {@link PDU} for the PDU type {@link PDU#GET},
35  * {@link PDU#GETNEXT}, and {@link PDU#SET}. The other SNMPv2 PDU types
36  * implemented by <code>PDU</code> are not supported. In contrast to its super
37  * class, <code>PDUv1</code> implements the {@link PDU#V1TRAP} type.
38  *
39  * <p>To support this type, access methods are provided to get and set the
40  * enterprise <code>OID</code>, generic, specific, and timestamp of a SNMPv1
41  * trap PDU.
42  *
43  * <p>The constants defined for generic SNMPv1 traps are included in this class.
44  * The descriptions are taken from the SNMPv2-MIB (RFC 3418). The corresponding
45  * OIDs are defined in {@link SnmpConstants}.
46  *
47  * @author Frank Fock
48  * @version 1.7.3
49  */

50 public class PDUv1 extends PDU {
51
52   private static final long serialVersionUID = -6478805117911347898L;
53
54   /**
55    * A coldStart(0) trap signifies that the SNMP entity,
56    * supporting a notification originator application, is
57    * reinitializing itself and that its configuration may
58    * have been altered.
59    */

60   public static final int COLDSTART = 0;
61
62   /**
63    * A warmStart(1) trap signifies that the SNMP entity,
64    * supporting a notification originator application,
65    * is reinitializing itself such that its configuration
66    * is unaltered.
67    */

68   public static final int WARMSTART = 1;
69
70   /**
71    * A linkDown(2) trap signifies that the SNMP entity, acting in
72    * an agent role, has detected that the ifOperStatus object for
73    * one of its communication links is about to enter the down
74    * state from some other state (but not from the notPresent
75    * state). This other state is indicated by the included value
76    * of ifOperStatus.
77    */

78   public static final int LINKDOWN = 2;
79
80   /**
81    * A linkUp(3) trap signifies that the SNMP entity, acting in an
82    * agent role, has detected that the ifOperStatus object for
83    * one of its communication links left the down state and
84    * transitioned into some other state (but not into the
85    * notPresent state). This other state is indicated by the
86    * included value of ifOperStatus.
87    */

88   public static final int LINKUP = 3;
89
90   /**
91    * An authenticationFailure(4) trap signifies that the SNMP
92    * entity has received a protocol message that is not
93    * properly authenticated. While all implementations
94    * of SNMP entities MAY be capable of generating this
95    * trap, the snmpEnableAuthenTraps object indicates
96    * whether this trap will be generated.
97    */

98   public static final int AUTHENTICATIONFAILURE = 4;
99
100   /**
101    * If the generic trap identifier is <code>ENTERPRISE_SPECIFIC</code>(6), then
102    * the enterprise specific trap ID is given by the specificTrap member field.
103    */

104   public static final int ENTERPRISE_SPECIFIC = 6;
105
106   private static final String JavaDoc OPERATION_NOT_SUPPORTED =
107       "Operation not supported for SNMPv1 PDUs";
108
109   private OID enterprise = new OID();
110   private IpAddress agentAddress = new IpAddress("0.0.0.0");
111   private Integer32 genericTrap = new Integer32(0);
112   private Integer32 specificTrap = new Integer32(0);
113   private TimeTicks timestamp = new TimeTicks(0);
114
115
116   public PDUv1() {
117     setType(V1TRAP);
118   }
119
120   /**
121    * Decodes a <code>Variable</code> from an <code>InputStream</code>.
122    *
123    * @param inputStream an <code>InputStream</code> containing a BER encoded
124    * byte stream.
125    * @throws IOException
126    */

127   public void decodeBER(BERInputStream inputStream) throws IOException {
128     MutableByte pduType = new MutableByte();
129     int length = BER.decodeHeader(inputStream, pduType);
130     int pduStartPos = (int)inputStream.getPosition();
131
132     switch (pduType.getValue()) {
133       case PDU.SET:
134       case PDU.GET:
135       case PDU.GETNEXT:
136       case PDU.V1TRAP:
137       case PDU.RESPONSE:
138         break;
139       default:
140         throw new IOException("Unsupported PDU type: "+pduType.getValue());
141     }
142     this.setType(pduType.getValue());
143     if (getType() == PDU.V1TRAP) {
144       enterprise.decodeBER(inputStream);
145       agentAddress.decodeBER(inputStream);
146       genericTrap.decodeBER(inputStream);
147       specificTrap.decodeBER(inputStream);
148       timestamp.decodeBER(inputStream);
149     }
150     else {
151       requestID.decodeBER(inputStream);
152       errorStatus.decodeBER(inputStream);
153       errorIndex.decodeBER(inputStream);
154
155     }
156     // reusing pduType here to save memory ;-)
157
pduType = new BER.MutableByte();
158     int vbLength = BER.decodeHeader(inputStream, pduType);
159     if (pduType.getValue() != BER.SEQUENCE) {
160       throw new IOException("Encountered invalid tag, SEQUENCE expected: "+
161                             pduType.getValue());
162     }
163     // rest read count
164
int startPos = (int)inputStream.getPosition();
165     variableBindings = new Vector();
166     while (inputStream.getPosition() - startPos < vbLength) {
167       VariableBinding vb = new VariableBinding();
168       vb.decodeBER(inputStream);
169       if (vb.getSyntax() == SMIConstants.SYNTAX_COUNTER64) {
170         throw new MessageException("Counter64 encountered in SNMPv1 PDU "+
171                                    "(RFC 2576 §4.1.2.1)");
172       }
173       variableBindings.add(vb);
174     }
175     if (BER.isCheckSequenceLength()) {
176       BER.checkSequenceLength(vbLength,
177                               (int) inputStream.getPosition() - startPos, this);
178       BER.checkSequenceLength(length,
179                               (int) inputStream.getPosition() - pduStartPos, this);
180     }
181   }
182
183   /**
184    * Encodes a <code>Variable</code> to an <code>OutputStream</code>.
185    *
186    * @param outputStream an <code>OutputStream</code>.
187    * @throws IOException if an error occurs while writing to the stream.
188    */

189   public void encodeBER(OutputStream outputStream) throws IOException {
190     BER.encodeHeader(outputStream, type, getBERPayloadLength());
191
192     if (type == PDU.V1TRAP) {
193       enterprise.encodeBER(outputStream);
194       agentAddress.encodeBER(outputStream);
195       genericTrap.encodeBER(outputStream);
196       specificTrap.encodeBER(outputStream);
197       timestamp.encodeBER(outputStream);
198     }
199     else {
200       requestID.encodeBER(outputStream);
201       errorStatus.encodeBER(outputStream);
202       errorIndex.encodeBER(outputStream);
203     }
204     int vbLength = 0;
205     for (int i=0; i<variableBindings.size(); i++) {
206       vbLength += ((VariableBinding)variableBindings.get(i)).getBERLength();
207     }
208     BER.encodeHeader(outputStream, BER.SEQUENCE, vbLength);
209     for (int i=0; i<variableBindings.size(); i++) {
210       VariableBinding vb = (VariableBinding)variableBindings.get(i);
211       if (vb.getVariable() instanceof Counter64) {
212         throw new IOException("Cannot encode Counter64 into a SNMPv1 PDU");
213       }
214       vb.encodeBER(outputStream);
215     }
216   }
217
218   protected int getBERPayloadLengthPDU() {
219     if (getType() != PDU.V1TRAP) {
220       return super.getBERPayloadLengthPDU();
221     }
222     else {
223       int length = 0;
224       // length for all vbs
225
for (int i = 0; i < variableBindings.size(); i++) {
226         length += ((VariableBinding)variableBindings.get(i)).getBERLength();
227       }
228       length += BER.getBERLengthOfLength(length) + 1;
229       length += agentAddress.getBERLength();
230       length += enterprise.getBERLength();
231       length += genericTrap.getBERLength();
232       length += specificTrap.getBERLength();
233       length += timestamp.getBERLength();
234       return length;
235     }
236   }
237
238   /**
239    * This method is not supported for SNMPv1 PDUs and will throw a
240    * {@link java.lang.UnsupportedOperationException}
241    * @return
242    * nothing
243    * @throws UnsupportedOperationException
244    */

245   public int getMaxRepetitions() {
246     throw new UnsupportedOperationException JavaDoc(OPERATION_NOT_SUPPORTED);
247   }
248
249   /**
250    * This method is not supported for SNMPv1 PDUs and will throw a
251    * {@link java.lang.UnsupportedOperationException}
252    * @param maxRepetitions int
253    * @throws UnsupportedOperationException
254    */

255   public void setMaxRepetitions(int maxRepetitions) {
256     throw new UnsupportedOperationException JavaDoc(OPERATION_NOT_SUPPORTED);
257   }
258
259   /**
260    * This method is not supported for SNMPv1 PDUs and will throw a
261    * {@link java.lang.UnsupportedOperationException}
262    *
263    * @param maxSizeScopedPDU int
264    * @throws UnsupportedOperationException
265    */

266   public void setMaxSizeScopedPDU(int maxSizeScopedPDU) {
267     throw new UnsupportedOperationException JavaDoc(OPERATION_NOT_SUPPORTED);
268   }
269
270   /**
271    * This method is not supported for SNMPv1 PDUs and will throw a
272    * {@link java.lang.UnsupportedOperationException}
273    *
274    * @param nonRepeaters int
275    * @throws UnsupportedOperationException
276    */

277   public void setNonRepeaters(int nonRepeaters) {
278     throw new UnsupportedOperationException JavaDoc(OPERATION_NOT_SUPPORTED);
279   }
280
281   private void checkV1TRAP() {
282     if (getType() != PDU.V1TRAP) {
283       throw new UnsupportedOperationException JavaDoc(
284           "Operation is only supported for SNMPv1 trap PDUs (V1TRAP)");
285     }
286   }
287
288   /**
289    * Gets the "enterprise" OID of the SNMPv1 trap. The enterprise OID could be
290    * any OID although the name could lead to the assumption that the
291    * enterprise OID has to be an OID under the
292    * iso(1).org(3).dod(6).internet(1).private(4).enterprises(1) node, but that's
293    * not true.
294    *
295    * @return
296    * an OID instance.
297    * @throws UnsupportedOperationException if the type of this PDU is not
298    * {@link PDU#V1TRAP}.
299    */

300   public OID getEnterprise() {
301     checkV1TRAP();
302     return enterprise;
303   }
304
305   /**
306    * Sets the "enterprise" OID of the SNMPv1 trap. The enterprise OID could be
307    * any OID although the name could lead to the assumption that the
308    * enterprise OID has to be an OID under the
309    * iso(1).org(3).dod(6).internet(1).private(4).enterprises(1) node, but that's
310    * not true.
311    *
312    * @param enterprise
313    * an OID instance.
314    * @throws UnsupportedOperationException if the type of this PDU is not
315    * {@link PDU#V1TRAP}.
316    */

317   public void setEnterprise(org.snmp4j.smi.OID enterprise) {
318     checkV1TRAP();
319     checkNull(enterprise);
320     this.enterprise = (OID) enterprise.clone();
321   }
322
323   /**
324    * Gets the IP address of the originator system of this SNMPv1 trap.
325    * If this value is 0.0.0.0 (the recommended default), then the address
326    * of the peer SNMP entity should be extracted from the {@link Target}
327    * object associated with this PDU.
328    *
329    * @return
330    * an IpAddress instance.
331    * @throws UnsupportedOperationException if the type of this PDU is not
332    * {@link PDU#V1TRAP}.
333    */

334   public org.snmp4j.smi.IpAddress getAgentAddress() {
335     checkV1TRAP();
336     return agentAddress;
337   }
338
339   /**
340    * Sets the IP address of the originator system of this SNMPv1 trap.
341    * The default value is 0.0.0.0, which should be only overriden in special
342    * cases, for example when forwarding SNMPv1 traps through a SNMP proxy.
343    * @param agentAddress
344    * a <code>IpAddress</code> instance.
345    * @throws UnsupportedOperationException if the type of this PDU is not
346    * {@link PDU#V1TRAP}.
347    */

348   public void setAgentAddress(org.snmp4j.smi.IpAddress agentAddress) {
349     checkV1TRAP();
350     checkNull(agentAddress);
351     this.agentAddress = agentAddress;
352   }
353
354   /**
355    * Gets the generic trap ID. If this value is ENTERPRISE_SPECIFIC(6), then
356    * {@link #getSpecificTrap()} will return the trap ID of the enterprise
357    * specific trap.
358    * @return
359    * an Integer32 instance with a value between 0 and 6.
360    * @throws UnsupportedOperationException if the type of this PDU is not
361    * {@link PDU#V1TRAP}.
362    */

363   public int getGenericTrap() {
364     checkV1TRAP();
365     return genericTrap.getValue();
366   }
367
368   /**
369    * Sets the generic trap ID. If this value is ENTERPRISE_SPECIFIC(6), then
370    * {@link #setSpecificTrap} must be used to set the trap ID of the enterprise
371    * specific trap.
372    * @param genericTrap
373    * an integer value >= 0 and <= 6.
374    * @throws UnsupportedOperationException if the type of this PDU is not
375    * {@link PDU#V1TRAP}.
376    */

377   public void setGenericTrap(int genericTrap) {
378     checkV1TRAP();
379     this.genericTrap.setValue(genericTrap);
380   }
381
382   /**
383    * Gets the specific trap ID. If this value is set,
384    * {@link #getGenericTrap()} must return ENTERPRISE_SPECIFIC(6).
385    * @return
386    * an integer value > 0.
387    * @throws UnsupportedOperationException if the type of this PDU is not
388    * {@link PDU#V1TRAP}.
389    */

390   public int getSpecificTrap() {
391     checkV1TRAP();
392     return specificTrap.getValue();
393   }
394   /**
395    * Sets the specific trap ID. If this value is set,
396    * {@link #setGenericTrap(int genericTrap)} must be called with value
397    * {@link #ENTERPRISE_SPECIFIC}.
398    *
399    * @param specificTrap
400    * an integer value > 0.
401    * @throws UnsupportedOperationException if the type of this PDU is not
402    * {@link PDU#V1TRAP}.
403    */

404   public void setSpecificTrap(int specificTrap) {
405     checkV1TRAP();
406     this.specificTrap.setValue(specificTrap);
407   }
408
409   /**
410    * Gets the <code>TimeTicks</code> value of the trap sender's notion of
411    * its sysUpTime value when this trap has been generated.
412    *
413    * @return
414    * a long value.
415    * @throws UnsupportedOperationException if the type of this PDU is not
416    * {@link PDU#V1TRAP}.
417    */

418   public long getTimestamp() {
419     checkV1TRAP();
420     return timestamp.getValue();
421   }
422
423   /**
424    * Sets the <code>TimeTicks</code> value of the trap sender's notion of
425    * its sysUpTime value when this trap has been generated.
426    *
427    * @param timeStamp
428    * a long value.
429    */

430   public void setTimestamp(long timeStamp) {
431     checkV1TRAP();
432     this.timestamp.setValue(timeStamp);
433   }
434
435   /**
436    * Checks for null parameters.
437    * @param parameter
438    * an Object instance.
439    * @throws NullPointerException if <code>parameter</code> is null.
440    */

441   protected void checkNull(Object JavaDoc parameter) {
442     if (parameter == null) {
443       throw new NullPointerException JavaDoc("Members of PDUv1 must not be null");
444     }
445   }
446
447   public String JavaDoc toString() {
448     if (type == PDU.V1TRAP) {
449       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
450       buf.append(getTypeString(type));
451       buf.append("[reqestID=");
452       buf.append(requestID);
453       buf.append(",timestamp=");
454       buf.append(timestamp);
455       buf.append(",enterprise=");
456       buf.append(enterprise);
457       buf.append(",genericTrap=");
458       buf.append(genericTrap);
459       buf.append(",specificTrap=");
460       buf.append(specificTrap);
461       buf.append(", VBS[");
462       for (int i = 0; i < variableBindings.size(); i++) {
463         buf.append(variableBindings.get(i));
464         if (i + 1 < variableBindings.size()) {
465           buf.append("; ");
466         }
467       }
468       buf.append("]]");
469       return buf.toString();
470     }
471     return super.toString();
472   }
473
474 }
475
Popular Tags