View Javadoc

1   package net.sf.snmpadaptor4j.object;
2   
3   import java.io.Serializable;
4   import java.util.Arrays;
5   import java.util.StringTokenizer;
6   
7   /**
8    * Object representing an object identifier in a SNMP <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB).
9    * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
10   */
11  public final class SnmpOid
12  		implements Comparable<SnmpOid>, Serializable {
13  
14  	/**
15  	 * Serial number.
16  	 */
17  	private static final long serialVersionUID = -1099844003876586949L;
18  
19  	/**
20  	 * OID: system.sysName.0
21  	 */
22  	public static SnmpOid SYSNAME_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 });
23  
24  	/**
25  	 * OID: system.sysDescr.0
26  	 */
27  	public static SnmpOid SYSDESCR_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 1, 0 });
28  
29  	/**
30  	 * OID: system.sysLocation.0
31  	 */
32  	public static SnmpOid SYSLOCATION_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 6, 0 });
33  
34  	/**
35  	 * OID: system.sysContact.0
36  	 */
37  	public static SnmpOid SYSCONTACT_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 4, 0 });
38  
39  	/**
40  	 * OID: system.sysUpTime.0
41  	 */
42  	public static SnmpOid SYSUPTIME_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 3, 0 });
43  
44  	/**
45  	 * Object identifier as numeric sequence.
46  	 */
47  	private final int[] oid;
48  
49  	/**
50  	 * Creates and returns a new instance of {@link SnmpOid} by its string value.
51  	 * @param oid Object identifier as string.
52  	 * @param nodeNum Node number to insert at the last position of object identifier.
53  	 * @param index Index to insert at the last position of object identifier.
54  	 * @return New instance of {@link SnmpOid}.
55  	 */
56  	public static SnmpOid newInstance (final String oid, final int nodeNum, final int index) {
57  		final StringTokenizer tokens = new StringTokenizer(oid.trim(), ".");
58  		int[] array = new int[tokens.countTokens() + 2];
59  		int i = 0;
60  		while (tokens.hasMoreTokens()) {
61  			array[i] = Integer.parseInt(tokens.nextToken());
62  			i++;
63  		}
64  		array[i] = nodeNum;
65  		array[i + 1] = index;
66  		return new SnmpOid(array);
67  	}
68  
69  	/**
70  	 * Creates and returns a new instance of {@link SnmpOid} by an another {@link SnmpOid}.
71  	 * @param oid Object identifier as {@link SnmpOid}.
72  	 * @param nodeNum Node number to insert at the last position of object identifier.
73  	 * @param index Index to insert at the last position of object identifier.
74  	 * @return New instance of {@link SnmpOid}.
75  	 */
76  	public static SnmpOid newInstance (final SnmpOid oid, final int nodeNum, final int index) {
77  		final int[] array = new int[oid.oid.length + 2];
78  		for (int i = 0; i < oid.oid.length; i++) {
79  			array[i] = oid.oid[i];
80  		}
81  		array[oid.oid.length] = nodeNum;
82  		array[oid.oid.length + 1] = index;
83  		return new SnmpOid(array);
84  	}
85  
86  	/**
87  	 * Creates and returns a new instance of {@link SnmpOid} by its string value.
88  	 * @param oid Object identifier as string.
89  	 * @return New instance of {@link SnmpOid}.
90  	 */
91  	public static SnmpOid newInstance (final String oid) {
92  		final StringTokenizer tokens = new StringTokenizer(oid.trim(), ".");
93  		int[] array = new int[tokens.countTokens()];
94  		int i = 0;
95  		while (tokens.hasMoreTokens()) {
96  			array[i] = Integer.parseInt(tokens.nextToken());
97  			i++;
98  		}
99  		return new SnmpOid(array);
100 	}
101 
102 	/**
103 	 * Creates and returns a new instance of {@link SnmpOid} by its numeric sequence.
104 	 * @param oid Object identifier as numeric sequence.
105 	 * @param index Index to insert at the last position of object identifier.
106 	 * @return New instance of {@link SnmpOid}.
107 	 */
108 	public static SnmpOid newInstance (final int[] oid, final int index) {
109 		int[] newOid = new int[oid.length + 1];
110 		for (int i = 0; i < oid.length; i++) {
111 			newOid[i] = oid[i];
112 		}
113 		newOid[oid.length] = index;
114 		return new SnmpOid(newOid);
115 	}
116 
117 	/**
118 	 * Creates and returns a new instance of {@link SnmpOid} by its numeric sequence.
119 	 * @param oid Object identifier as numeric sequence.
120 	 * @return New instance of {@link SnmpOid}.
121 	 */
122 	public static SnmpOid newInstance (final int[] oid) {
123 		return new SnmpOid(oid);
124 	}
125 
126 	/**
127 	 * Hidden constructor.
128 	 * @param oid Object identifier as numeric sequence.
129 	 */
130 	private SnmpOid (final int[] oid) {
131 		super();
132 		this.oid = oid;
133 	}
134 
135 	/**
136 	 * Returns the object identifier as numeric sequence.
137 	 * @return Object identifier.
138 	 */
139 	public int[] getOid () {
140 		return this.oid;
141 	}
142 
143 	/**
144 	 * Returns the index part of the object identifier.
145 	 * @return Index part of the object identifier.
146 	 */
147 	public int getIndex () {
148 		return this.oid[this.oid.length - 1];
149 	}
150 
151 	/*
152 	 * {@inheritDoc}
153 	 * @see java.lang.Object#hashCode()
154 	 */
155 	@Override
156 	public int hashCode () {
157 		return Arrays.hashCode(this.oid);
158 	}
159 
160 	/*
161 	 * {@inheritDoc}
162 	 * @see java.lang.Object#equals(java.lang.Object)
163 	 */
164 	@Override
165 	public boolean equals (final Object obj) {
166 		boolean result = false;
167 		if (obj == this) {
168 			result = true;
169 		}
170 		else if ((obj != null) && (getClass().equals(obj.getClass()))) {
171 			result = Arrays.equals(this.oid, ((SnmpOid) obj).oid);
172 		}
173 		return result;
174 	}
175 
176 	/**
177 	 * Returns <code>TRUE</code> if the OID without its index is equals to another OID.
178 	 * @param other Another OID.
179 	 * @return <code>TRUE</code> if the OID without its index is equals to another OID.
180 	 */
181 	public boolean equalsWithoutIndex (final SnmpOid other) {
182 		boolean result = false;
183 		if ((other != null) && (other.oid.length == (this.oid.length - 1))) {
184 			result = true;
185 			int i = 0;
186 			while ((i < other.oid.length) && result) {
187 				result = (other.oid[i] == this.oid[i]);
188 				i++;
189 			}
190 		}
191 		return result;
192 	}
193 
194 	/**
195 	 * Returns <code>TRUE</code> if the OID is the root of another OID passed as parameter.
196 	 * @param other Another OID.
197 	 * @return <code>TRUE</code> if the OID is the root of another OID passed as parameter.
198 	 */
199 	public boolean isRootOf (final SnmpOid other) {
200 		boolean result = false;
201 		if ((other != null) && (other.oid.length >= this.oid.length)) {
202 			result = true;
203 			int i = 0;
204 			while ((i < this.oid.length) && result) {
205 				result = (other.oid[i] == this.oid[i]);
206 				i++;
207 			}
208 		}
209 		return result;
210 	}
211 
212 	/*
213 	 * {@inheritDoc}
214 	 * @see java.lang.Comparable#compareTo(java.lang.Object)
215 	 */
216 	public int compareTo (final SnmpOid obj) {
217 		int result = 0;
218 		int i = 0;
219 		boolean compare = true;
220 		while ((i < this.oid.length) && compare) {
221 			if (i < obj.oid.length) {
222 				if (this.oid[i] != obj.oid[i]) {
223 					result = this.oid[i] - obj.oid[i];
224 					compare = false;
225 				}
226 			}
227 			else {
228 				result = 1;
229 				compare = false;
230 			}
231 			i++;
232 		}
233 		if (compare && (i < obj.oid.length)) {
234 			result = -1;
235 		}
236 		return result;
237 	}
238 
239 	/*
240 	 * {@inheritDoc}
241 	 * @see java.lang.Object#toString()
242 	 */
243 	@Override
244 	public String toString () {
245 		String result = "";
246 		for (final int num : this.oid) {
247 			result = result + "." + num;
248 		}
249 		return result;
250 	}
251 
252 }