View Javadoc
1   // NAME
2   //      $RCSfile: varbind.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.10 $
7   // CREATED
8   //      $Date: 2007/10/17 10:47:47 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 1995, 1996 by West Consulting BV
16   *
17   * Permission to use, copy, modify, and distribute this software
18   * for any purpose and without fee is hereby granted, provided
19   * that the above copyright notices appear in all copies and that
20   * both the copyright notice and this permission notice appear in
21   * supporting documentation.
22   * This software is provided "as is" without express or implied
23   * warranty.
24   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
25   */
26  
27  /*
28   * Copyright (C) 1996 - 2006 by Westhawk Ltd
29   * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
30   *
31   * Permission to use, copy, modify, and distribute this software
32   * for any purpose and without fee is hereby granted, provided
33   * that the above copyright notices appear in all copies and that
34   * both the copyright notice and this permission notice appear in
35   * supporting documentation.
36   * This software is provided "as is" without express or implied
37   * warranty.
38   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
39   */
40  
41  package uk.co.westhawk.snmp.stack;
42  
43  /*-
44   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
45   * SNMP Java Client
46   * ჻჻჻჻჻჻
47   * Copyright 2023 MetricsHub, Westhawk
48   * ჻჻჻჻჻჻
49   * This program is free software: you can redistribute it and/or modify
50   * it under the terms of the GNU Lesser General Public License as
51   * published by the Free Software Foundation, either version 3 of the
52   * License, or (at your option) any later version.
53   *
54   * This program is distributed in the hope that it will be useful,
55   * but WITHOUT ANY WARRANTY; without even the implied warranty of
56   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57   * GNU General Lesser Public License for more details.
58   *
59   * You should have received a copy of the GNU General Lesser Public
60   * License along with this program.  If not, see
61   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
62   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
63   */
64  
65  /**
66   * <p>
67   * This class represents the variable bindings to a PDU.
68   * A variable consists of a name (an AsnObjectId) and a value (an
69   * AsnObject)
70   * </p>
71   *
72   * <p>
73   * The varbind is usually passed to the Observers of the PDU when
74   * notifying them. 
75   * </p>
76   *
77   * @see Pdu#addOid(varbind)
78   * @see Pdu#addOid(String, AsnObject)
79   * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
80   * @version $Revision: 3.10 $ $Date: 2007/10/17 10:47:47 $
81   */
82  public class varbind extends Object {
83      private static final String version_id = "@(#)$Id: varbind.java,v 3.10 2007/10/17 10:47:47 birgita Exp $ Copyright Westhawk Ltd";
84  
85      private AsnObjectId name;
86      private AsnObject value;
87  
88      /**
89       * Constructor.
90       * It will clone the varbind given as parameter.
91       *
92       * @param var The varbind
93       */
94      public varbind(varbind var) {
95          name = var.name;
96          value = var.value;
97      }
98  
99      /**
100      * Constructor.
101      * The name will be set to the Oid, the value will be set to
102      * AsnNull. This is usually used in Get or GetNext requests.
103      *
104      * @param Oid The oid
105      * @see AsnNull
106      */
107     public varbind(String Oid) {
108         this(new AsnObjectId(Oid), new AsnNull());
109     }
110 
111     /**
112      * Constructor.
113      * The name will be set to the Oid, the value will be set to
114      * AsnNull. This is usually used in Get or GetNext requests.
115      *
116      * @param Oid The oid
117      * @see AsnNull
118      */
119     public varbind(AsnObjectId Oid) {
120         this(Oid, new AsnNull());
121     }
122 
123     /**
124      * Constructor.
125      * The name and value will be set.
126      * This is usually used in Set requests.
127      *
128      * @param Oid The oid
129      * @param val The value for the varbind
130      */
131     public varbind(String Oid, AsnObject val) {
132         this(new AsnObjectId(Oid), val);
133     }
134 
135     /**
136      * Constructor.
137      * The name and value will be set.
138      * This is usually used in Set requests.
139      *
140      * @param Oid The oid
141      * @param val The value for the varbind
142      * @since 4_12
143      */
144     public varbind(AsnObjectId Oid, AsnObject val) {
145         name = Oid;
146         value = val;
147     }
148 
149     varbind(AsnSequence vb)
150             throws IllegalArgumentException {
151         Object obj = vb.getObj(0);
152         if (obj instanceof AsnObjectId) {
153             name = (AsnObjectId) obj;
154             value = vb.getObj(1);
155         } else {
156             String msg = "First object should be AsnObjectId, but is ";
157             if (obj != null) {
158                 msg += obj.getClass().getName();
159             } else {
160                 msg += "null";
161             }
162             throw new IllegalArgumentException(msg);
163         }
164     }
165 
166     /**
167      * Returns the oid, this is the name of the varbind.
168      *
169      * @return the name as an AsnObjectId
170      */
171     public AsnObjectId getOid() {
172         return name;
173     }
174 
175     /**
176      * Returns the value of the varbind.
177      *
178      * @return the value as AsnObject
179      */
180     public AsnObject getValue() {
181         return value;
182     }
183 
184     Object setValue(AsnSequence vb)
185             throws IllegalArgumentException {
186         varbind tmp = new varbind(vb);
187         name = tmp.name;
188         value = tmp.value;
189         return value;
190     }
191 
192     /**
193      * Returns the string representation of the varbind.
194      *
195      * @return The string of the varbind
196      */
197     public String toString() {
198         return (name.toString() + ": " + value.toString());
199     }
200 }