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
19
20
21 public class SystemSnmpMib
22 implements SnmpMib {
23
24
25
26
27 protected final class ExternalNode
28 implements AttributeAccessor {
29
30
31
32
33 private final int index;
34
35
36
37
38 private final SnmpOid oid;
39
40
41
42
43
44
45 protected ExternalNode (final int index, final SnmpOid oid) {
46 super();
47 this.index = index;
48 this.oid = oid;
49 }
50
51
52
53
54
55 public SnmpOid getOid () {
56 return this.oid;
57 }
58
59
60
61
62
63 public SnmpDataType getSnmpDataType () {
64 return SystemSnmpMib.this.getSnmpDataType(this.index);
65 }
66
67
68
69
70
71 public Class<?> getJmxDataType () {
72 return SystemSnmpMib.this.getJmxDataType(this.index);
73 }
74
75
76
77
78
79 public Object getValue () {
80 return SystemSnmpMib.this.getValue(this.index);
81 }
82
83
84
85
86
87 public void setValue (final Object value) {
88
89 }
90
91
92
93
94
95 public boolean isReadable () {
96 return true;
97 }
98
99
100
101
102
103 public boolean isWritable () {
104 return false;
105 }
106
107
108
109
110
111 @Override
112 public String toString () {
113 return getOid() + ": (" + getSnmpDataType() + ") " + getValue();
114 }
115
116 }
117
118
119
120
121 protected final class SysObjectIDExternalNode
122 implements AttributeAccessor {
123
124
125
126
127 private final SnmpOid oid;
128
129
130
131
132 private final SnmpOid value;
133
134
135
136
137
138
139 protected SysObjectIDExternalNode (final SnmpOid oid, final SnmpOid value) {
140 super();
141 this.oid = oid;
142 this.value = value;
143 }
144
145
146
147
148
149 public SnmpOid getOid () {
150 return this.oid;
151 }
152
153
154
155
156
157 public SnmpDataType getSnmpDataType () {
158 return SnmpDataType.objectIdentifier;
159 }
160
161
162
163
164
165 public Class<?> getJmxDataType () {
166 return SnmpOid.class;
167 }
168
169
170
171
172
173 public Object getValue () {
174 return this.value;
175 }
176
177
178
179
180
181 public void setValue (final Object value) {
182
183 }
184
185
186
187
188
189 public boolean isReadable () {
190 return true;
191 }
192
193
194
195
196
197 public boolean isWritable () {
198 return false;
199 }
200
201
202
203
204
205 @Override
206 public String toString () {
207 return getOid() + ": (" + getSnmpDataType() + ") " + getValue();
208 }
209
210 }
211
212
213
214
215 private final SnmpAppContext mainAppContext;
216
217
218
219
220 private final Map<ClassLoader, SnmpAppContext> appContextMap;
221
222
223
224
225 private final SystemInfo systemInfo;
226
227
228
229
230 private final SortedMap<SnmpOid, AttributeAccessor> mib = new TreeMap<SnmpOid, AttributeAccessor>();
231
232
233
234
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
245
246
247
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
259
260
261 protected final SortedMap<SnmpOid, AttributeAccessor> getMib () {
262 return this.mib;
263 }
264
265
266
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
298 oid = SnmpOid.SYSNAME_OID;
299 putExternalNode(0, oid);
300
301
302 oid = SnmpOid.SYSDESCR_OID;
303 putExternalNode(1, oid);
304
305
306 oid = SnmpOid.SYSLOCATION_OID;
307 putExternalNode(2, oid);
308
309
310 oid = SnmpOid.SYSCONTACT_OID;
311 putExternalNode(3, oid);
312
313
314 oid = SnmpOid.SYSUPTIME_OID;
315 putExternalNode(4, oid);
316
317
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
336
337
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
345
346
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
354
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
366
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
382
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
394
395
396
397 protected final SnmpDataType getSnmpDataType (final int index) {
398 SnmpDataType type;
399 switch (index) {
400 case 0:
401 type = SnmpDataType.octetString;
402 break;
403 case 1:
404 type = SnmpDataType.octetString;
405 break;
406 case 2:
407 type = SnmpDataType.octetString;
408 break;
409 case 3:
410 type = SnmpDataType.octetString;
411 break;
412 case 4:
413 type = SnmpDataType.timeTicks;
414 break;
415 default:
416 type = null;
417 break;
418 }
419 return type;
420 }
421
422
423
424
425
426
427 protected final Class<?> getJmxDataType (final int index) {
428 Class<?> type;
429 switch (index) {
430 case 0:
431 type = String.class;
432 break;
433 case 1:
434 type = String.class;
435 break;
436 case 2:
437 type = String.class;
438 break;
439 case 3:
440 type = String.class;
441 break;
442 case 4:
443 type = Long.class;
444 break;
445 default:
446 type = null;
447 break;
448 }
449 return type;
450 }
451
452
453
454
455
456
457 public final Object getValue (final int index) {
458 Object value;
459 switch (index) {
460 case 0:
461 value = this.systemInfo.getSysName();
462 break;
463 case 1:
464 value = this.systemInfo.getSysDescr();
465 break;
466 case 2:
467 value = this.systemInfo.getSysLocation();
468 break;
469 case 3:
470 value = this.systemInfo.getSysContact();
471 break;
472 case 4:
473 value = new Long(this.systemInfo.getSysUpTime());
474 break;
475 default:
476 value = null;
477 break;
478 }
479 return value;
480 }
481
482
483
484
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 }