KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > MOTableIndex


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - MOTableIndex.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (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
22 package org.snmp4j.agent.mo;
23
24 import org.snmp4j.smi.*;
25
26 /**
27  * The <code>MOTableIndex</code> class represents a index definition of a
28  * conceptual table. An index always has to implement also the
29  * {@link MOTableIndexValidator} interface for validation of index values
30  * for newly created rows.
31  *
32  * @author Frank Fock
33  * @version 1.0
34  */

35 public class MOTableIndex implements MOTableIndexValidator {
36
37   public static final int MAX_INDEX_OID_LENGTH = 127;
38
39   private MOTableSubIndex[] subindexes;
40   private boolean impliedLength = false;
41   private MOTableIndexValidator validator;
42
43
44   /**
45    * Creates a index definition from an array of sub-index definitions.
46    * @param subIndexes
47    * an array of sub-index definitions with at least one element.
48    */

49   public MOTableIndex(MOTableSubIndex[] subIndexes) {
50     if ((subIndexes == null) || (subIndexes.length < 1)) {
51       throw new IllegalArgumentException JavaDoc(
52           "Index definition must have at least one sub-index");
53     }
54     this.subindexes = subIndexes;
55   }
56
57   /**
58    * Creates a index definition from an array of sub-index definitions where
59    * the last sub-index may have an implied length.
60    * @param subIndexes
61    * an array of sub-index definitions with at least one element.
62    * @param impliedLength
63    * if <code>true</code> the last sub-index has an implied length if at has
64    * a variable length at all.
65    */

66   public MOTableIndex(MOTableSubIndex[] subIndexes, boolean impliedLength) {
67     this(subIndexes);
68     this.impliedLength = impliedLength;
69   }
70
71   /**
72    * Creates a index definition from an array of sub-index definitions where
73    * the last sub-index may have an implied length.
74    * @param subIndexes
75    * an array of sub-index definitions with at least one element.
76    * @param impliedLength
77    * if <code>true</code> the last sub-index has an implied length if at has
78    * a variable length at all.
79    * @param validator
80    * an index validator that is called whenever a new index value needs to
81    * validated.
82    */

83   public MOTableIndex(MOTableSubIndex[] subIndexes, boolean impliedLength,
84                       MOTableIndexValidator validator) {
85     this(subIndexes, impliedLength);
86     this.validator = validator;
87   }
88
89   /**
90    * Gets the sub-index definition at the specified index.
91    * @param index
92    * a valid sub-index index (zero-based).
93    * @return
94    * the <code>MOTableSubIndex</code>.
95    */

96   public MOTableSubIndex getIndex(int index) {
97     return this.subindexes[index];
98   }
99
100   public boolean isImpliedLength() {
101     return impliedLength;
102   }
103
104   /**
105    * Gets the index validator (if present).
106    * @return
107    * the <code>MOTableIndexValidator</code> associated with this index or
108    * <code>null</code>.
109    */

110   public MOTableIndexValidator getValidator() {
111     return validator;
112   }
113
114   /**
115    * Sets the index validator associated with this index definition.
116    * @param validator
117    * a <code>MOTableIndexValidator</code> instance.
118    */

119   public void setValidator(MOTableIndexValidator validator) {
120     this.validator = validator;
121   }
122
123   /**
124    * Gets the number of sub-index definitions in this index definition.
125    * @return
126    * the sub-index count.
127    */

128   public int size() {
129     return subindexes.length;
130   }
131
132   private static boolean checkIndexBytes(OID index, long start, long end) {
133     if ((start < 0) || (start > MOTableIndex.MAX_INDEX_OID_LENGTH) ||
134         (end < 0) || (end > MOTableIndex.MAX_INDEX_OID_LENGTH)) {
135       return false;
136     }
137     for (int i=(int)start; ((i<index.size()) && (i<end)); i++) {
138       if (index.getUnsigned(i) > 255) {
139         return false;
140       }
141     }
142     return true;
143   }
144
145   private static boolean isStringSyntax(int smiSyntax) {
146     switch (smiSyntax) {
147       case SMIConstants.SYNTAX_OCTET_STRING:
148       case SMIConstants.SYNTAX_IPADDRESS:
149       case SMIConstants.SYNTAX_OPAQUE: {
150         return true;
151       }
152     }
153     return false;
154   }
155
156   /**
157    * Checks whether an index OID is a valid index for this index definition
158    * or not.
159    * @param index
160    * an OID (possibly zero length).
161    * @return
162    * <code>true</code> if the index is valid or <code>false</code> otherwise.
163    */

164   public boolean isValidIndex(OID index) {
165     if (index.size() > MOTableIndex.MAX_INDEX_OID_LENGTH) {
166       return false;
167     }
168     int l = 0;
169     int i;
170     for (i=0; ((i<size()) && (l < index.size())); i++) {
171       MOTableSubIndex subIndex = getIndex(i);
172       if ((i+1 == size()) && (isImpliedLength())) {
173         int type = subIndex.getSmiSyntax();
174         switch (type) {
175           case SMIConstants.SYNTAX_OCTET_STRING:
176           case SMIConstants.SYNTAX_IPADDRESS:
177             if (!checkIndexBytes(index, l, index.size())) {
178               return false;
179             }
180             break;
181         }
182         return true;
183       }
184       else if ((subIndex.getMinLength() != subIndex.getMaxLength())) {
185         if (index.size() < index.get(l)+1) {
186           return false;
187         }
188         if ((index.get(l) < subIndex.getMinLength()) ||
189             (index.get(l) > subIndex.getMaxLength())) {
190           return false;
191         }
192         if (isStringSyntax(subIndex.getSmiSyntax())) {
193           if (!checkIndexBytes(index, l, l+index.getUnsigned(l)+1)) {
194             return false;
195           }
196         }
197         l += index.getUnsigned(l)+1;
198       }
199       else {
200         if (isStringSyntax(subIndex.getSmiSyntax())) {
201           if (!checkIndexBytes(index, l, l+subIndex.getMaxLength())) {
202             return false;
203           }
204         }
205         // min == max
206
l += subIndex.getMaxLength();
207       }
208     }
209     return (((index.size() == l) && (i >= size())) &&
210             ((validator == null) || (validator.isValidIndex(index))));
211   }
212
213   private static Variable getIndexVariable(MOTableSubIndex subIndexDef,
214                                            OID subIndex,
215                                            boolean impliedLength) {
216     switch (subIndexDef.getSmiSyntax()) {
217       case SMIConstants.SYNTAX_OCTET_STRING: {
218         if ((impliedLength) ||
219             (subIndexDef.getMinLength() == subIndexDef.getMaxLength())) {
220           OctetString s = new OctetString(subIndex.toByteArray());
221           return s;
222         }
223         OID suffix = new OID(subIndex.getValue(), 1, subIndex.size() - 1);
224         return new OctetString(suffix.toByteArray());
225       }
226       case SMIConstants.SYNTAX_OBJECT_IDENTIFIER: {
227         if ((impliedLength) ||
228             (subIndexDef.getMinLength() == subIndexDef.getMaxLength())) {
229           return subIndex;
230         }
231         OID suffix = new OID(subIndex.getValue(), 1, subIndex.size() - 1);
232         return suffix;
233       }
234       case SMIConstants.SYNTAX_UNSIGNED_INTEGER32: {
235         return new Gauge32(subIndex.get(subIndex.size()-1));
236       }
237       case SMIConstants.SYNTAX_TIMETICKS: {
238         return new TimeTicks(subIndex.get(subIndex.size()-1));
239       }
240       case SMIConstants.SYNTAX_INTEGER: {
241         return new Integer32(subIndex.get(subIndex.size()-1));
242       }
243       case SMIConstants.SYNTAX_IPADDRESS: {
244         return new IpAddress(subIndex.toString());
245       }
246 /*
247       case SMIConstants.SYN_NETADDRESS: {
248         String id = subIndex.toString();
249         return new IpAddress(id.substring(id.indexOf(".") + 1));
250       }
251 */

252     }
253     return null;
254   }
255
256   /**
257    * Split a table index into an array of object IDs each representing the
258    * value of its corresponding index object. For example if a table's index
259    * would be defined as INDEX { ifIndex, ipAddress } and the index given
260    * would be "1.127.0.0.1" the resulting array would be { "1", "127.0.0.1" }
261    *
262    * @param index
263    * an OID denoting a table's index value.
264    * @return
265    * an array of OID instances with the same size as returned by
266    * {@link #size}. If the given index is not a valid object ID
267    * <code>null</code> is returned.
268    */

269   public OID[] getIndexOIDs(OID index) {
270     OID[] r = new OID[size()];
271     int[] ind = index.getValue();
272     int pos = 0;
273     for (int i = 0; i < subindexes.length; i++) {
274       if ((i+1 == size()) && (isImpliedLength())) {
275         r[i] = new OID(ind, pos, index.size()-pos);
276         break;
277       }
278       else if ((subindexes[i].getMinLength() != subindexes[i].getMaxLength())) {
279         r[i] = new OID(ind, pos, index.get(pos) + 1);
280       }
281       else {
282         r[i] = new OID(index.getValue(), pos, subindexes[i].getMaxLength());
283       }
284       pos += r[i].size();
285     }
286     return r;
287   }
288
289   /**
290    * Gets the index values contained in an index OID.
291    * @param index
292    * the index OID.
293    * @return
294    * an array of values representing the index.
295    * @see #getIndexOID
296    */

297   public Variable[] getIndexValues(OID index) {
298     OID[] oids = getIndexOIDs(index);
299     Variable[] values = new Variable[oids.length];
300     for (int i=0; i<oids.length; i++) {
301       boolean implied = ((isImpliedLength()) && (i+1 == size()));
302       values[i] = getIndexVariable(subindexes[i], oids[i], implied);
303     }
304     return values;
305   }
306
307   /**
308    * Gets the index OID from an array of index values.
309    * @param indexValues
310    * an array of Variable instances that has to match the number and type
311    * of sub-indexes in this index.
312    * @return
313    * the corresponding index OID.
314    * @see #getIndexValues
315    */

316   public OID getIndexOID(Variable[] indexValues) {
317     if (indexValues.length != size()) {
318       throw new IllegalArgumentException JavaDoc("Index value length != size()");
319     }
320     OID index = new OID();
321     for (int i=0; i<indexValues.length; i++) {
322       if (indexValues[i].getSyntax() == this.subindexes[i].getSmiSyntax()) {
323         index.append(indexValues[i].toSubIndex((i + 1 == indexValues.length) &&
324                                                impliedLength));
325       }
326       else {
327         throw new IllegalArgumentException JavaDoc("Syntax of index value #"+i+
328                                            " = "+
329                                            indexValues[i].getSyntaxString()+
330                                            " does not match index definition "+
331                                            AbstractVariable.getSyntaxString(
332                                                this.subindexes[i].getSmiSyntax()));
333       }
334     }
335     return index;
336   }
337 }
338
Popular Tags