View Javadoc

1   package net.sf.snmpadaptor4j.core;
2   
3   import java.util.Iterator;
4   import java.util.Map.Entry;
5   import java.util.Map;
6   import java.util.Set;
7   import java.util.SortedMap;
8   import java.util.TreeMap;
9   import java.util.TreeSet;
10  import net.sf.snmpadaptor4j.SnmpAppContext;
11  import net.sf.snmpadaptor4j.api.SnmpMib;
12  import net.sf.snmpadaptor4j.api.AttributeAccessor;
13  import net.sf.snmpadaptor4j.mbean.SystemInfo;
14  import net.sf.snmpadaptor4j.object.SnmpDataType;
15  import net.sf.snmpadaptor4j.object.SnmpOid;
16  
17  /**
18   * Object representing the <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB) for system attributes.
19   * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
20   */
21  public class SystemSnmpMib
22  		implements SnmpMib {
23  
24  	/**
25  	 * External node of the SNMP <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB).
26  	 */
27  	protected final class ExternalNode
28  			implements AttributeAccessor {
29  
30  		/**
31  		 * Index of attribute.
32  		 */
33  		private final int index;
34  
35  		/**
36  		 * <b>O</b>bject <b>ID</b>entifier (OID) of MIB node.
37  		 */
38  		private final SnmpOid oid;
39  
40  		/**
41  		 * Constructor.
42  		 * @param index Index of attribute.
43  		 * @param oid <b>O</b>bject <b>ID</b>entifier (OID) of MIB node.
44  		 */
45  		protected ExternalNode (final int index, final SnmpOid oid) {
46  			super();
47  			this.index = index;
48  			this.oid = oid;
49  		}
50  
51  		/*
52  		 * {@inheritDoc}
53  		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getOid()
54  		 */
55  		public SnmpOid getOid () {
56  			return this.oid;
57  		}
58  
59  		/*
60  		 * {@inheritDoc}
61  		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getSnmpDataType()
62  		 */
63  		public SnmpDataType getSnmpDataType () {
64  			return SystemSnmpMib.this.getSnmpDataType(this.index);
65  		}
66  
67  		/*
68  		 * {@inheritDoc}
69  		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getJmxDataType()
70  		 */
71  		public Class<?> getJmxDataType () {
72  			return SystemSnmpMib.this.getJmxDataType(this.index);
73  		}
74  
75  		/*
76  		 * {@inheritDoc}
77  		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getValue()
78  		 */
79  		public Object getValue () {
80  			return SystemSnmpMib.this.getValue(this.index);
81  		}
82  
83  		/*
84  		 * {@inheritDoc}
85  		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#setValue(java.lang.Object)
86  		 */
87  		public void setValue (final Object value) {
88  			// NOP
89  		}
90  
91  		/*
92  		 * {@inheritDoc}
93  		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isReadable()
94  		 */
95  		public boolean isReadable () {
96  			return true;
97  		}
98  
99  		/*
100 		 * {@inheritDoc}
101 		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isWritable()
102 		 */
103 		public boolean isWritable () {
104 			return false;
105 		}
106 
107 		/*
108 		 * {@inheritDoc}
109 		 * @see java.lang.Object#toString()
110 		 */
111 		@Override
112 		public String toString () {
113 			return getOid() + ": (" + getSnmpDataType() + ") " + getValue();
114 		}
115 
116 	}
117 
118 	/**
119 	 * External node of the SNMP <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB) for <code>system.sysObjectID</code> attribute.
120 	 */
121 	protected final class SysObjectIDExternalNode
122 			implements AttributeAccessor {
123 
124 		/**
125 		 * <b>O</b>bject <b>ID</b>entifier (OID) of MIB node.
126 		 */
127 		private final SnmpOid oid;
128 
129 		/**
130 		 * Value of MIB node.
131 		 */
132 		private final SnmpOid value;
133 
134 		/**
135 		 * Constructor.
136 		 * @param oid <b>O</b>bject <b>ID</b>entifier (OID) of MIB node.
137 		 * @param value Value of MIB node.
138 		 */
139 		protected SysObjectIDExternalNode (final SnmpOid oid, final SnmpOid value) {
140 			super();
141 			this.oid = oid;
142 			this.value = value;
143 		}
144 
145 		/*
146 		 * {@inheritDoc}
147 		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getOid()
148 		 */
149 		public SnmpOid getOid () {
150 			return this.oid;
151 		}
152 
153 		/*
154 		 * {@inheritDoc}
155 		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getSnmpDataType()
156 		 */
157 		public SnmpDataType getSnmpDataType () {
158 			return SnmpDataType.objectIdentifier;
159 		}
160 
161 		/*
162 		 * {@inheritDoc}
163 		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getJmxDataType()
164 		 */
165 		public Class<?> getJmxDataType () {
166 			return SnmpOid.class;
167 		}
168 
169 		/*
170 		 * {@inheritDoc}
171 		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getValue()
172 		 */
173 		public Object getValue () {
174 			return this.value;
175 		}
176 
177 		/*
178 		 * {@inheritDoc}
179 		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#setValue(java.lang.Object)
180 		 */
181 		public void setValue (final Object value) {
182 			// NOP
183 		}
184 
185 		/*
186 		 * {@inheritDoc}
187 		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isReadable()
188 		 */
189 		public boolean isReadable () {
190 			return true;
191 		}
192 
193 		/*
194 		 * {@inheritDoc}
195 		 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isWritable()
196 		 */
197 		public boolean isWritable () {
198 			return false;
199 		}
200 
201 		/*
202 		 * {@inheritDoc}
203 		 * @see java.lang.Object#toString()
204 		 */
205 		@Override
206 		public String toString () {
207 			return getOid() + ": (" + getSnmpDataType() + ") " + getValue();
208 		}
209 
210 	}
211 
212 	/**
213 	 * Context of main application.
214 	 */
215 	private final SnmpAppContext mainAppContext;
216 
217 	/**
218 	 * Application context map.
219 	 */
220 	private final Map<ClassLoader, SnmpAppContext> appContextMap;
221 
222 	/**
223 	 * Informations on the system.
224 	 */
225 	private final SystemInfo systemInfo;
226 
227 	/**
228 	 * <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB).
229 	 */
230 	private final SortedMap<SnmpOid, AttributeAccessor> mib = new TreeMap<SnmpOid, AttributeAccessor>();
231 
232 	/**
233 	 * Constructor (only used by tests).
234 	 * @param systemInfo Informations on the system.
235 	 */
236 	protected SystemSnmpMib (final SystemInfo systemInfo) {
237 		super();
238 		this.mainAppContext = null;
239 		this.appContextMap = null;
240 		this.systemInfo = systemInfo;
241 	}
242 
243 	/**
244 	 * Constructor.
245 	 * @param mainAppContext Context of main application (must not be <code>NULL</code>).
246 	 * @param appContextMap Application context map.
247 	 * @param systemInfo Informations on the system.
248 	 */
249 	public SystemSnmpMib (final SnmpAppContext mainAppContext, final Map<ClassLoader, SnmpAppContext> appContextMap, final SystemInfo systemInfo) {
250 		super();
251 		this.mainAppContext = mainAppContext;
252 		this.appContextMap = appContextMap;
253 		this.systemInfo = systemInfo;
254 		initSysObjectIDSet();
255 	}
256 
257 	/**
258 	 * Returns the <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB).
259 	 * @return <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB).
260 	 */
261 	protected final SortedMap<SnmpOid, AttributeAccessor> getMib () {
262 		return this.mib;
263 	}
264 
265 	/**
266 	 * Initializes the set of <code>system.sysObjectID</code> values with their OID.
267 	 */
268 	public void initSysObjectIDSet () {
269 		SnmpOid oid;
270 		final Set<SnmpOid> sysObjectIDSet = new TreeSet<SnmpOid>();
271 		if (this.mainAppContext.getDefaultRootOid() != null) {
272 			sysObjectIDSet.add(SnmpOid.newInstance(this.mainAppContext.getDefaultRootOid()));
273 		}
274 		for (final String rootOid : this.mainAppContext.getRootOidMap().values()) {
275 			oid = SnmpOid.newInstance(rootOid);
276 			if (!sysObjectIDSet.contains(oid)) {
277 				sysObjectIDSet.add(oid);
278 			}
279 		}
280 		for (final SnmpAppContext appContext : this.appContextMap.values()) {
281 			if (appContext.getDefaultRootOid() != null) {
282 				oid = SnmpOid.newInstance(appContext.getDefaultRootOid());
283 				if (!sysObjectIDSet.contains(oid)) {
284 					sysObjectIDSet.add(oid);
285 				}
286 			}
287 			for (final String rootOid : appContext.getRootOidMap().values()) {
288 				oid = SnmpOid.newInstance(rootOid);
289 				if (!sysObjectIDSet.contains(oid)) {
290 					sysObjectIDSet.add(oid);
291 				}
292 			}
293 		}
294 		synchronized (this.mib) {
295 			this.mib.clear();
296 
297 			// system.sysName.0
298 			oid = SnmpOid.SYSNAME_OID;
299 			putExternalNode(0, oid);
300 
301 			// system.sysDescr.0
302 			oid = SnmpOid.SYSDESCR_OID;
303 			putExternalNode(1, oid);
304 
305 			// system.sysLocation.0
306 			oid = SnmpOid.SYSLOCATION_OID;
307 			putExternalNode(2, oid);
308 
309 			// system.sysContact.0
310 			oid = SnmpOid.SYSCONTACT_OID;
311 			putExternalNode(3, oid);
312 
313 			// system.sysUpTime.0
314 			oid = SnmpOid.SYSUPTIME_OID;
315 			putExternalNode(4, oid);
316 
317 			// system.sysObjectID.0
318 			int index = 1;
319 			for (final SnmpOid value : sysObjectIDSet) {
320 				if (sysObjectIDSet.size() == 1) {
321 					oid = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 2, 0 });
322 					putExternalNode(oid, value);
323 				}
324 				else {
325 					oid = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 2, index });
326 					putExternalNode(oid, value);
327 				}
328 				index++;
329 			}
330 
331 		}
332 	}
333 
334 	/**
335 	 * Puts an external node of an attribute in the MIB.
336 	 * @param index Index of attribute.
337 	 * @param oid <b>O</b>bject <b>ID</b>entifier (OID) of MIB node.
338 	 */
339 	protected final void putExternalNode (final int index, final SnmpOid oid) {
340 		this.mib.put(oid, new ExternalNode(index, oid));
341 	}
342 
343 	/**
344 	 * Puts an external node of an attribute in the MIB.
345 	 * @param oid <b>O</b>bject <b>ID</b>entifier (OID) of MIB node.
346 	 * @param value Value of MIB node.
347 	 */
348 	protected final void putExternalNode (final SnmpOid oid, final SnmpOid value) {
349 		this.mib.put(oid, new SysObjectIDExternalNode(oid, value));
350 	}
351 
352 	/*
353 	 * {@inheritDoc}
354 	 * @see net.sf.snmpadaptor4j.api.SnmpMib#find(net.sf.snmpadaptor4j.object.SnmpOid)
355 	 */
356 	public final AttributeAccessor find (final SnmpOid oid) {
357 		AttributeAccessor node;
358 		synchronized (this.mib) {
359 			node = this.mib.get(oid);
360 		}
361 		return node;
362 	}
363 
364 	/*
365 	 * {@inheritDoc}
366 	 * @see net.sf.snmpadaptor4j.api.SnmpMib#next(net.sf.snmpadaptor4j.object.SnmpOid)
367 	 */
368 	public final AttributeAccessor next (final SnmpOid oid) {
369 		AttributeAccessor node;
370 		final Iterator<Entry<SnmpOid, AttributeAccessor>> entryIterator = nextSet(oid).entrySet().iterator();
371 		if (entryIterator.hasNext()) {
372 			node = entryIterator.next().getValue();
373 		}
374 		else {
375 			node = null;
376 		}
377 		return node;
378 	}
379 
380 	/*
381 	 * {@inheritDoc}
382 	 * @see net.sf.snmpadaptor4j.api.SnmpMib#nextSet(net.sf.snmpadaptor4j.object.SnmpOid)
383 	 */
384 	public final SortedMap<SnmpOid, AttributeAccessor> nextSet (final SnmpOid oid) {
385 		SortedMap<SnmpOid, AttributeAccessor> nodeMap;
386 		synchronized (this.mib) {
387 			nodeMap = this.mib.tailMap(SnmpOid.newInstance(oid.getOid(), 0));
388 		}
389 		return nodeMap;
390 	}
391 
392 	/**
393 	 * Returns the SNMP data type of attribute.
394 	 * @param index Index of attribute.
395 	 * @return SNMP data type of attribute.
396 	 */
397 	protected final SnmpDataType getSnmpDataType (final int index) {
398 		SnmpDataType type;
399 		switch (index) {
400 			case 0:		// system.sysName.0
401 				type = SnmpDataType.octetString;
402 				break;
403 			case 1:		// system.sysDescr.0
404 				type = SnmpDataType.octetString;
405 				break;
406 			case 2:		// system.sysLocation.0
407 				type = SnmpDataType.octetString;
408 				break;
409 			case 3:		// system.sysContact.0
410 				type = SnmpDataType.octetString;
411 				break;
412 			case 4:		// system.sysUpTime.0
413 				type = SnmpDataType.timeTicks;
414 				break;
415 			default:
416 				type = null;
417 				break;
418 		}
419 		return type;
420 	}
421 
422 	/**
423 	 * Returns the data type of JMX attribute.
424 	 * @param index Index of attribute.
425 	 * @return Data type of JMX attribute.
426 	 */
427 	protected final Class<?> getJmxDataType (final int index) {
428 		Class<?> type;
429 		switch (index) {
430 			case 0:		// system.sysName.0
431 				type = String.class;
432 				break;
433 			case 1:		// system.sysDescr.0
434 				type = String.class;
435 				break;
436 			case 2:		// system.sysLocation.0
437 				type = String.class;
438 				break;
439 			case 3:		// system.sysContact.0
440 				type = String.class;
441 				break;
442 			case 4:		// system.sysUpTime.0
443 				type = Long.class;
444 				break;
445 			default:
446 				type = null;
447 				break;
448 		}
449 		return type;
450 	}
451 
452 	/**
453 	 * Returns the value of attribute.
454 	 * @param index Index of attribute.
455 	 * @return Value of attribute.
456 	 */
457 	public final Object getValue (final int index) {
458 		Object value;
459 		switch (index) {
460 			case 0:		// system.sysName.0
461 				value = this.systemInfo.getSysName();
462 				break;
463 			case 1:		// system.sysDescr.0
464 				value = this.systemInfo.getSysDescr();
465 				break;
466 			case 2:		// system.sysLocation.0
467 				value = this.systemInfo.getSysLocation();
468 				break;
469 			case 3:		// system.sysContact.0
470 				value = this.systemInfo.getSysContact();
471 				break;
472 			case 4:		// system.sysUpTime.0
473 				value = new Long(this.systemInfo.getSysUpTime());
474 				break;
475 			default:
476 				value = null;
477 				break;
478 		}
479 		return value;
480 	}
481 
482 	/*
483 	 * {@inheritDoc}
484 	 * @see java.lang.Object#toString()
485 	 */
486 	@Override
487 	public final String toString () {
488 		String values;
489 		synchronized (this.mib) {
490 			values = this.mib.values().toString();
491 		}
492 		return "SystemSnmpMib" + values;
493 	}
494 
495 }