View Javadoc
1   // NAME
2   //      $RCSfile: InterfaceGetNextPduStub.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.4 $
7   // CREATED
8   //      $Date: 2006/11/29 16:12:50 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 2005 - 2006 by Westhawk Ltd
16   * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
17   *
18   * Permission to use, copy, modify, and distribute this software
19   * for any purpose and without fee is hereby granted, provided
20   * that the above copyright notices appear in all copies and that
21   * both the copyright notice and this permission notice appear in
22   * supporting documentation.
23   * This software is provided "as is" without express or implied
24   * warranty.
25   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
26   */
27   
28  package uk.co.westhawk.snmp.pdu;
29  
30  /*-
31   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
32   * SNMP Java Client
33   * ჻჻჻჻჻჻
34   * Copyright 2023 MetricsHub, Westhawk
35   * ჻჻჻჻჻჻
36   * This program is free software: you can redistribute it and/or modify
37   * it under the terms of the GNU Lesser General Public License as
38   * published by the Free Software Foundation, either version 3 of the
39   * License, or (at your option) any later version.
40   *
41   * This program is distributed in the hope that it will be useful,
42   * but WITHOUT ANY WARRANTY; without even the implied warranty of
43   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44   * GNU General Lesser Public License for more details.
45   *
46   * You should have received a copy of the GNU General Lesser Public
47   * License along with this program.  If not, see
48   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
49   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
50   */
51  
52  import uk.co.westhawk.snmp.stack.*;
53  import uk.co.westhawk.snmp.pdu.*;
54  import java.util.*;
55  
56  
57  /**
58   * The class InterfaceGetNextPduStub.
59   *
60   * This file is auto generated by the StubBrowser utility, using Mibble.
61   * See the uk/co/westhawk/stub/ directory.
62   *
63   * Make sure that you replace the package name and classname placeholders. 
64   * Also, move this file to the correct package directory.
65   * If these things are not done, this class will not compile correctly!!
66   *
67   * @version $Revision: 3.4 $ $Date: 2006/11/29 16:12:50 $
68   * @since 4_14
69   */
70  public class InterfaceGetNextPduStub extends GetNextPdu {
71      private static final String version_id = "@(#)$Id: InterfaceGetNextPduStub.java,v 3.4 2006/11/29 16:12:50 birgit Exp $ Copyright Westhawk Ltd";
72  
73      public final static String sysUpTime_OID = "1.3.6.1.2.1.1.3";
74      public final static String ifNumber_OID = "1.3.6.1.2.1.2.1";
75      public final static String ifIndex_OID = "1.3.6.1.2.1.2.2.1.1";
76      public final static String ifDescr_OID = "1.3.6.1.2.1.2.2.1.2";
77      public final static String ifSpeed_OID = "1.3.6.1.2.1.2.2.1.5";
78      public final static String ifOperStatus_OID = "1.3.6.1.2.1.2.2.1.8";
79      public final static String ifInOctets_OID = "1.3.6.1.2.1.2.2.1.10";
80      public final static String ifOutOctets_OID = "1.3.6.1.2.1.2.2.1.16";
81  
82      public final static int NO_SCAL = 2;
83      public final static int NO_COL = 6;
84      public final static int NO_OID = NO_SCAL + NO_COL;
85  
86      public final static String scal_oids[] = {
87              sysUpTime_OID,
88              ifNumber_OID,
89      };
90  
91      public final static String col_oids[] = {
92              ifIndex_OID,
93              ifDescr_OID,
94              ifSpeed_OID,
95              ifOperStatus_OID,
96              ifInOctets_OID,
97              ifOutOctets_OID,
98      };
99  
100     public final static String all_oids[] = {
101             sysUpTime_OID,
102             ifNumber_OID,
103             ifIndex_OID,
104             ifDescr_OID,
105             ifSpeed_OID,
106             ifOperStatus_OID,
107             ifInOctets_OID,
108             ifOutOctets_OID,
109     };
110 
111     protected long _sysUpTime;
112     protected int _ifNumber;
113     protected int _ifIndex;
114     protected String _ifDescr;
115     protected long _ifSpeed;
116     protected HashMap _ifOperStatusMap = new HashMap(3);
117     protected int _ifOperStatus;
118     protected long _ifInOctets;
119     protected long _ifOutOctets;
120 
121     protected boolean _valid = false;
122 
123     /**
124      * Constructor.
125      *
126      * @param con The context of the request
127      */
128     public InterfaceGetNextPduStub(SnmpContextBasisFace con) {
129         super(con);
130         _ifOperStatusMap.put(new Integer(3), "testing");
131         _ifOperStatusMap.put(new Integer(1), "up");
132         _ifOperStatusMap.put(new Integer(2), "down");
133 
134         _valid = false;
135     }
136 
137     /**
138      * Constructor that will send the first request immediately.
139      *
140      * @param con The context of the request
141      * @param o   the Observer that will be notified when the answer is
142      *            received
143      */
144     public InterfaceGetNextPduStub(SnmpContextBasisFace con, Observer o)
145             throws PduException, java.io.IOException {
146         this(con);
147         addOids(null);
148         if (o != null) {
149             addObserver(o);
150         }
151         send();
152     }
153 
154     /**
155      * The method addOids is the basis for the GetNext functionality.
156      *
157      * If old is null, it initialises the varbinds from all_oids.
158      * If old is not null, it copies the column OIDs from the
159      * old InterfaceGetNextPduStub object.
160      * so the request continues where the previous one left.
161      *
162      * Note, the scalars and the columns OIDs are handled differently. The
163      * scalars are always copied from the original scal_oids, only the
164      * column OIDs are copied from the old
165      * InterfaceGetNextPduStub object.
166      */
167     public void addOids(InterfaceGetNextPduStub old) {
168         if (old != null) {
169             for (int i = 0; i < NO_SCAL; i++) {
170                 addOid(scal_oids[i]);
171             }
172             for (int i = NO_SCAL; i < NO_OID; i++) {
173                 varbind var = (varbind) old.respVarbinds.elementAt(i);
174                 addOid(var.getOid());
175             }
176         } else {
177             for (int i = 0; i < NO_OID; i++) {
178                 addOid(all_oids[i]);
179             }
180         }
181     }
182 
183     /**
184      * This method sets the column index. By doing this, the request will
185      * return (only) this index.
186      *
187      * The index parameters only applies to the column OIDs.
188      * The scalars are copied from the original scal_oids.
189      */
190     public void addOids(int index) {
191         for (int i = 0; i < NO_SCAL; i++) {
192             addOid(scal_oids[i]);
193         }
194         for (int i = 0; i < NO_COL; i++) {
195             addOid(col_oids[i] + "." + index);
196         }
197     }
198 
199     /**
200      * The value of the request is set. This will be called by
201      * Pdu.fillin().
202      *
203      * I check if the variables are still in range.
204      * I do this because I'm only interessed in a part of the MIB. If I
205      * would not do this check, I'll get the whole MIB from the starting
206      * point, instead of the variables in the table.
207      *
208      * @param n   the index of the value
209      * @param res the value
210      * @see Pdu#new_value
211      */
212     protected void new_value(int n, varbind res) {
213         if (getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
214             AsnObjectId oid = res.getOid();
215             AsnObject value = res.getValue();
216 
217             if (res.getOid().toString().startsWith(all_oids[n])) {
218                 try {
219                     switch (n) {
220                         case 0:
221                             setSysUpTime(value);
222                             break;
223                         case 1:
224                             setIfNumber(value);
225                             break;
226                         case 2:
227                             setIfIndex(value);
228                             break;
229                         case 3:
230                             setIfDescr(value);
231                             break;
232                         case 4:
233                             setIfSpeed(value);
234                             break;
235                         case 5:
236                             setIfOperStatus(value);
237                             break;
238                         case 6:
239                             setIfInOctets(value);
240                             break;
241                         case 7:
242                             setIfOutOctets(value);
243                             break;
244                         default:
245                             _valid = false;
246                     }
247                 } catch (ClassCastException exc) {
248                     _valid = false;
249                 }
250 
251                 if (n == NO_OID - 1) {
252                     _valid = true;
253                 }
254             } else {
255                 setErrorStatus(AsnObject.SNMP_ERR_NOSUCHNAME);
256                 setErrorIndex(n);
257             }
258         }
259     }
260 
261     /**
262      * sysUpTime
263      * OBJECT-TYPE (
264      * Syntax: [APPLICATION 3] INTEGER (0..4294967295)
265      * Access: read-only
266      * Status: mandatory
267      * Description: The time (in hundredths of a second) since the
268      * network management portion of the system was last
269      * re-initialized.
270      * )
271      */
272     public void setSysUpTime(AsnObject new_value) {
273         AsnUnsInteger obj = (AsnUnsInteger) new_value;
274         _sysUpTime = obj.getValue();
275     }
276 
277     public long getSysUpTime() {
278         return _sysUpTime;
279     }
280 
281     /**
282      * ifNumber
283      * OBJECT-TYPE (
284      * Syntax: [UNIVERSAL 2] INTEGER
285      * Access: read-only
286      * Status: mandatory
287      * Description: The number of network interfaces (regardless of
288      * their current state) present on this system.
289      * )
290      */
291     public void setIfNumber(AsnObject new_value) {
292         AsnInteger obj = (AsnInteger) new_value;
293         _ifNumber = obj.getValue();
294     }
295 
296     public int getIfNumber() {
297         return _ifNumber;
298     }
299 
300     /**
301      * ifIndex
302      * OBJECT-TYPE (
303      * Syntax: [UNIVERSAL 2] INTEGER
304      * Access: read-only
305      * Status: mandatory
306      * Description: A unique value for each interface. Its value
307      * ranges between 1 and the value of ifNumber. The
308      * value for each interface must remain constant at
309      * least from one re-initialization of the entity's
310      * network management system to the next re-
311      * initialization.
312      * )
313      */
314     public void setIfIndex(AsnObject new_value) {
315         AsnInteger obj = (AsnInteger) new_value;
316         _ifIndex = obj.getValue();
317     }
318 
319     public int getIfIndex() {
320         return _ifIndex;
321     }
322 
323     /**
324      * ifDescr
325      * OBJECT-TYPE (
326      * Syntax: [UNIVERSAL 4] OCTET STRING (SIZE (0..255))
327      * Access: read-only
328      * Status: mandatory
329      * Description: A textual string containing information about the
330      * interface. This string should include the name of
331      * the manufacturer, the product name and the version
332      * of the hardware interface.
333      * )
334      */
335     public void setIfDescr(AsnObject new_value) {
336         AsnOctets obj = (AsnOctets) new_value;
337         _ifDescr = obj.toDisplayString();
338     }
339 
340     public String getIfDescr() {
341         return _ifDescr;
342     }
343 
344     /**
345      * ifSpeed
346      * OBJECT-TYPE (
347      * Syntax: [APPLICATION 2] INTEGER (0..4294967295)
348      * Access: read-only
349      * Status: mandatory
350      * Description: An estimate of the interface's current bandwidth
351      * in bits per second. For interfaces which do not
352      * vary in bandwidth or for those where no accurate
353      * estimation can be made, this object should contain
354      * the nominal bandwidth.
355      * )
356      */
357     public void setIfSpeed(AsnObject new_value) {
358         AsnUnsInteger obj = (AsnUnsInteger) new_value;
359         _ifSpeed = obj.getValue();
360     }
361 
362     public long getIfSpeed() {
363         return _ifSpeed;
364     }
365 
366     /**
367      * ifOperStatus
368      * OBJECT-TYPE (
369      * Syntax: [UNIVERSAL 2] INTEGER (1 | 2 | 3)
370      * Access: read-only
371      * Status: mandatory
372      * Description: The current operational state of the interface.
373      * The testing(3) state indicates that no operational
374      * packets can be passed.
375      * )
376      */
377     public void setIfOperStatus(AsnObject new_value) {
378         AsnInteger obj = (AsnInteger) new_value;
379         _ifOperStatus = obj.getValue();
380     }
381 
382     public String getIfOperStatusStr() {
383         return (String) _ifOperStatusMap.get(new Integer(_ifOperStatus));
384     }
385 
386     public int getIfOperStatus() {
387         return _ifOperStatus;
388     }
389 
390     /**
391      * ifInOctets
392      * OBJECT-TYPE (
393      * Syntax: [APPLICATION 1] INTEGER (0..4294967295)
394      * Access: read-only
395      * Status: mandatory
396      * Description: The total number of octets received on the
397      * interface, including framing characters.
398      * )
399      */
400     public void setIfInOctets(AsnObject new_value) {
401         AsnUnsInteger obj = (AsnUnsInteger) new_value;
402         _ifInOctets = obj.getValue();
403     }
404 
405     public long getIfInOctets() {
406         return _ifInOctets;
407     }
408 
409     /**
410      * ifOutOctets
411      * OBJECT-TYPE (
412      * Syntax: [APPLICATION 1] INTEGER (0..4294967295)
413      * Access: read-only
414      * Status: mandatory
415      * Description: The total number of octets transmitted out of the
416      * interface, including framing characters.
417      * )
418      */
419     public void setIfOutOctets(AsnObject new_value) {
420         AsnUnsInteger obj = (AsnUnsInteger) new_value;
421         _ifOutOctets = obj.getValue();
422     }
423 
424     public long getIfOutOctets() {
425         return _ifOutOctets;
426     }
427 
428     /**
429      * Returns if this set of values is valid.
430      */
431     public boolean isValid() {
432         return _valid;
433     }
434 
435     public String toString() {
436         StringBuffer buffer = new StringBuffer(getClass().getName());
437         buffer.append("[");
438         buffer.append("sysUpTime=").append(_sysUpTime);
439         buffer.append(", ifNumber=").append(_ifNumber);
440         buffer.append(", ifIndex=").append(_ifIndex);
441         buffer.append(", ifDescr=").append(_ifDescr);
442         buffer.append(", ifSpeed=").append(_ifSpeed);
443         buffer.append(", ifOperStatus=").append(_ifOperStatusMap.get(new Integer(_ifOperStatus)));
444         buffer.append(", ifInOctets=").append(_ifInOctets);
445         buffer.append(", ifOutOctets=").append(_ifOutOctets);
446         buffer.append(", valid=").append(_valid);
447         buffer.append("]");
448         return buffer.toString();
449     }
450 
451 }