/
TrapHandlerTestCase.java
645 lines (543 loc) · 23.1 KB
/
TrapHandlerTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2005-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.netmgt.trapd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.opennms.core.utils.InetAddressUtils.addr;
import java.net.InetAddress;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.spring.BeanUtils;
import org.opennms.core.test.MockLogAppender;
import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
import org.opennms.core.utils.Base64;
import org.opennms.core.utils.InetAddressUtils;
import org.opennms.netmgt.dao.mock.EventAnticipator;
import org.opennms.netmgt.dao.mock.MockEventIpcManager;
import org.opennms.netmgt.events.api.EventConstants;
import org.opennms.netmgt.model.events.EventBuilder;
import org.opennms.netmgt.snmp.SnmpInstId;
import org.opennms.netmgt.snmp.SnmpObjId;
import org.opennms.netmgt.snmp.SnmpTrapBuilder;
import org.opennms.netmgt.snmp.SnmpUtils;
import org.opennms.netmgt.snmp.SnmpV1TrapBuilder;
import org.opennms.netmgt.snmp.SnmpValue;
import org.opennms.netmgt.snmp.SnmpValueFactory;
import org.opennms.netmgt.xml.event.Event;
import org.opennms.netmgt.xml.event.Parm;
import org.opennms.test.JUnitConfigurationEnvironment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
@RunWith(OpenNMSJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
"classpath:/META-INF/opennms/applicationContext-minimal-conf.xml",
"classpath:/META-INF/opennms/applicationContext-soa.xml",
"classpath:/META-INF/opennms/mockEventIpcManager.xml",
"classpath:/META-INF/opennms/applicationContext-daemon.xml",
"classpath:/META-INF/opennms/applicationContext-trapDaemon.xml",
"classpath:/org/opennms/netmgt/trapd/applicationContext-trapDaemonTest.xml"}
)
@JUnitConfigurationEnvironment
public class TrapHandlerTestCase implements InitializingBean {
@Autowired
private Trapd m_trapd = null;
@Autowired
private MockEventIpcManager m_eventMgr;
@Autowired
private MockTrapdIpMgr m_trapdIpMgr;
@Autowired
private TrapQueueProcessorFactory m_processorFactory;
private EventAnticipator m_anticipator;
private InetAddress m_localhost = null;
@Resource(name="snmpTrapPort")
private Integer m_snmpTrapPort;
private boolean m_doStop = false;
private static final String m_ip = "127.0.0.1";
private static final long m_nodeId = 1;
@BeforeClass
public static void setUpLogging() {
MockLogAppender.setupLogging();
}
@Override
public void afterPropertiesSet() throws Exception {
BeanUtils.assertAutowiring(this);
}
@Before
public void setUp() throws Exception {
m_anticipator = new EventAnticipator();
m_eventMgr.setEventAnticipator(m_anticipator);
m_localhost = InetAddressUtils.addr(m_ip);
m_trapdIpMgr.clearKnownIpsMap();
m_trapdIpMgr.setNodeId(m_ip, m_nodeId);
m_trapd.start();
m_doStop = true;
}
public Collection<Event> finishUp() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
m_eventMgr.finishProcessingEvents();
m_anticipator.verifyAnticipated(1000, 0, 0, 0, 0);
return m_anticipator.getAnticipatedEventsRecieved();
}
@After
public void tearDown() throws Exception {
if (m_trapd != null && m_doStop) {
m_trapd.stop();
m_trapd = null;
}
}
@Test
@DirtiesContext
public void testV1TrapNoNewSuspect() throws Exception {
m_trapdIpMgr.clearKnownIpsMap();
anticipateAndSend(false, false, "uei.opennms.org/default/trap", "v1",
null, 6, 1);
}
@Test
@DirtiesContext
public void testV2TrapNoNewSuspect() throws Exception {
m_trapdIpMgr.clearKnownIpsMap();
anticipateAndSend(false, false, "uei.opennms.org/default/trap",
"v2c", null, 6, 1);
}
@Test
@DirtiesContext
public void testV1TrapNewSuspect() throws Exception {
m_trapdIpMgr.clearKnownIpsMap();
anticipateAndSend(true, false, "uei.opennms.org/default/trap",
"v1", null, 6, 1);
}
@Test
@DirtiesContext
public void testV2TrapNewSuspect() throws Exception {
m_trapdIpMgr.clearKnownIpsMap();
anticipateAndSend(true, false, "uei.opennms.org/default/trap",
"v2c", null, 6, 1);
}
@Test
@DirtiesContext
public void testV1EnterpriseIdAndGenericMatch() throws Exception {
anticipateAndSend(false, true,
"uei.opennms.org/IETF/BGP/traps/bgpEstablished",
"v1", ".1.3.6.1.2.1.15.7", 6, 1);
}
@Test
@DirtiesContext
public void testV2EnterpriseIdAndGenericAndSpecificMatch()
throws Exception {
anticipateAndSend(false, true,
"uei.opennms.org/IETF/BGP/traps/bgpEstablished",
"v2c", ".1.3.6.1.2.1.15.7", 6, 1);
}
@Test
@DirtiesContext
public void testV1EnterpriseIdAndGenericAndSpecificAndMatchWithVarbinds()
throws Exception {
SnmpValueFactory valueFactory = SnmpUtils.getValueFactory();
LinkedHashMap<String, SnmpValue> varbinds = new LinkedHashMap <String, SnmpValue>();
varbinds.put(".1.3.6.1.4.1.11.2.14.11.1.7.2.1.4.2404", valueFactory.getInt32(3));
varbinds.put(".1.3.6.1.4.1.11.2.14.11.1.7.2.1.5.2404", valueFactory.getInt32(2));
varbinds.put(".1.3.6.1.4.1.11.2.14.11.1.7.2.1.6.2404", valueFactory.getInt32(5));
varbinds.put(".1.3.6.1.4.1.11.2.14.11.1.7.3.0.2404", valueFactory.getOctetString("http://a.b.c.d/cgi/fDetail?index=2404".getBytes()));
anticipateAndSend(false, true,
"uei.opennms.org/vendor/HP/traps/hpicfFaultFinderTrap",
"v1", ".1.3.6.1.4.1.11.2.14.12.1", 6, 5, varbinds);
}
@Test
@DirtiesContext
public void testV1TrapOIDWildCardMatch()
throws Exception {
SnmpValueFactory valueFactory = SnmpUtils.getValueFactory();
LinkedHashMap<String, SnmpValue> varbinds = new LinkedHashMap <String, SnmpValue>();
varbinds.put(".1.3.6.1.4.1.32473.42.42.42", valueFactory.getInt32(42));
anticipateAndSend(false, true,
"uei.opennms.org/IANA/Example/traps/exampleEnterpriseTrap",
"v1", ".1.3.6.1.4.1.32473.42", 6, 5, varbinds);
}
@Test
@DirtiesContext
public void testV2EnterpriseIdAndGenericAndSpecificAndMatchWithVarbinds()
throws Exception {
SnmpValueFactory valueFactory = SnmpUtils.getValueFactory();
LinkedHashMap<String, SnmpValue> varbinds = new LinkedHashMap <String, SnmpValue>();
varbinds.put(".1.3.6.1.4.1.11.2.14.11.1.7.2.1.4.2404", valueFactory.getInt32(3));
varbinds.put(".1.3.6.1.4.1.11.2.14.11.1.7.2.1.5.2404", valueFactory.getInt32(2));
varbinds.put(".1.3.6.1.4.1.11.2.14.11.1.7.2.1.6.2404", valueFactory.getInt32(5));
varbinds.put(".1.3.6.1.4.1.11.2.14.11.1.7.3.0.2404", valueFactory.getOctetString("http://a.b.c.d/cgi/fDetail?index=2404".getBytes()));
anticipateAndSend(false, true,
"uei.opennms.org/vendor/HP/traps/hpicfFaultFinderTrap",
"v2c", ".1.3.6.1.4.1.11.2.14.12.1", 6, 5, varbinds);
}
// These exist to provide testing for the new Textual Convention feature
// See EventConfDataTest for the other part of this testing
@Test
@DirtiesContext
public void testV1EnterpriseIdAndGenericAndSpecificAndMatchWithVarbindsAndTC()
throws Exception {
SnmpValueFactory valueFactory = SnmpUtils.getValueFactory();
LinkedHashMap<String, SnmpValue> varbinds = new LinkedHashMap <String, SnmpValue>();
varbinds.put(".1.3.6.1.4.1.14179.2.6.2.20.0", valueFactory.getOctetString(new byte[]{(byte)0x00,(byte)0x14,(byte)0xf1,(byte)0xad,(byte)0xa7,(byte)0x50}));
Collection<Event> events = anticipateAndSend(false, true,
"uei.opennms.org/vendor/cisco/bsnAPNoiseProfileUpdatedToPass",
"v1", ".1.3.6.1.4.1.14179.2.6.3", 6, 38, varbinds);
boolean foundMacAddress = false;
// Assert that the MAC address varbind has been formatted into a colon-separated octet string
for (Event event : events) {
for (Parm parm : event.getParmCollection()) {
if (".1.3.6.1.4.1.14179.2.6.2.20.0".equals(parm.getParmName())) {
assertEquals("MAC address does not match", "00:14:F1:AD:A7:50", parm.getValue().getContent());
foundMacAddress = true;
}
}
}
assertTrue("Did not find expected MAC address parm", foundMacAddress);
}
// FIXME: these exist to provide testing for the new Textual Convention feature
@Test
@DirtiesContext
public void testV2EnterpriseIdAndGenericAndSpecificAndMatchWithVarbindsAndTC()
throws Exception {
SnmpValueFactory valueFactory = SnmpUtils.getValueFactory();
byte[] macAddr = new byte[]{(byte)0x00,(byte)0x14,(byte)0xf1,(byte)0xad,(byte)0xa7,(byte)0x50};
String encoded = new String(Base64.encodeBase64(macAddr));
byte[] decodeBytes = Base64.decodeBase64(encoded.toCharArray());
assertByteArrayEquals(macAddr, decodeBytes);
LinkedHashMap<String, SnmpValue> varbinds = new LinkedHashMap <String, SnmpValue>();
varbinds.put(".1.3.6.1.4.1.14179.2.6.2.20.0", valueFactory.getOctetString(macAddr));
Collection<Event> events = anticipateAndSend(false, true,
"uei.opennms.org/vendor/cisco/bsnAPNoiseProfileUpdatedToPass",
"v2c", ".1.3.6.1.4.1.14179.2.6.3", 6, 38, varbinds);
boolean foundMacAddress = false;
// Assert that the MAC address varbind has been formatted into a colon-separated octet string
for (Event event : events) {
for (Parm parm : event.getParmCollection()) {
if (".1.3.6.1.4.1.14179.2.6.2.20.0".equals(parm.getParmName())) {
assertEquals("MAC address does not match", "00:14:F1:AD:A7:50", parm.getValue().getContent());
foundMacAddress = true;
}
}
}
assertTrue("Did not find expected MAC address parm", foundMacAddress);
}
private void assertByteArrayEquals(byte[] macAddr, byte[] bytes) {
assertEquals("expect length: "+macAddr.length, macAddr.length, bytes.length);
for (int i = 0; i < macAddr.length; i++) {
assertEquals("Expected byte "+i+" to match", macAddr[i], bytes[i]);
}
}
@Test
@DirtiesContext
public void testV2EnterpriseIdAndGenericAndSpecificMatchWithZero()
throws Exception {
anticipateAndSend(false, true,
"uei.opennms.org/IETF/BGP/traps/bgpEstablished",
"v2c", ".1.3.6.1.2.1.15.7.0", 6, 1);
}
@Test
@DirtiesContext
public void testV2EnterpriseIdAndGenericAndSpecificMissWithExtraZeros()
throws Exception {
anticipateAndSend(false, true, "uei.opennms.org/default/trap", "v2c",
".1.3.6.1.2.1.15.7.0.0", 6, 1);
}
@Test
@DirtiesContext
public void testV1EnterpriseIdAndGenericAndSpecificMissWithWrongGeneric()
throws Exception {
anticipateAndSend(false, true, "uei.opennms.org/default/trap", "v1",
".1.3.6.1.2.1.15.7", 5, 1);
}
@Test
@DirtiesContext
public void testV1EnterpriseIdAndGenericAndSpecificMissWithWrongSpecific()
throws Exception {
anticipateAndSend(false, true, "uei.opennms.org/default/trap", "v1",
".1.3.6.1.2.1.15.7", 6, 50);
}
@Test
@DirtiesContext
public void testV1GenericMatch() throws Exception {
anticipateAndSend(false, true,
"uei.opennms.org/generic/traps/SNMP_Cold_Start",
"v1", null, 0, 0);
}
@Test
@DirtiesContext
public void testV2GenericMatch() throws Exception {
anticipateAndSend(false, true,
"uei.opennms.org/generic/traps/SNMP_Cold_Start",
"v2c", ".1.3.6.1.6.3.1.1.5.1", 0, 0);
}
@Test
@DirtiesContext
public void testV1TrapDroppedEvent() throws Exception {
anticipateAndSend(false, true, null, "v1", ".1.3.6.1.2.1.15.7", 6, 2);
}
@Test
@DirtiesContext
public void testV2TrapDroppedEvent() throws Exception {
anticipateAndSend(false, true, null, "v2c", ".1.3.6.1.2.1.15.7", 6, 2);
}
@Test
@DirtiesContext
public void testV1TrapDefaultEvent() throws Exception {
anticipateAndSend(false, true, "uei.opennms.org/default/trap",
"v1", null, 6, 1);
}
@Test
@DirtiesContext
public void testV2TrapDefaultEvent() throws Exception {
anticipateAndSend(false, true, "uei.opennms.org/default/trap",
"v2c", null, 6, 1);
}
@Test
@DirtiesContext
public void testNodeGainedModifiesIpMgr() throws Exception {
long nodeId = 2;
m_processorFactory.setNewSuspect(true);
anticipateEvent("uei.opennms.org/default/trap", m_ip, nodeId);
Event event =
anticipateEvent(EventConstants.NODE_GAINED_INTERFACE_EVENT_UEI,
m_ip, nodeId);
m_eventMgr.sendNow(event);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// do nothing
}
sendTrap("v1", null, 6, 1);
finishUp();
}
@Test
@DirtiesContext
public void testInterfaceReparentedModifiesIpMgr() throws Exception {
long nodeId = 2;
m_processorFactory.setNewSuspect(true);
anticipateEvent("uei.opennms.org/default/trap", m_ip, nodeId);
Event event =
anticipateEvent(EventConstants.INTERFACE_REPARENTED_EVENT_UEI,
m_ip, nodeId);
m_eventMgr.sendNow(event);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// do nothing
}
sendTrap("v1", null, 6, 1);
finishUp();
}
@Test
@DirtiesContext
public void testInterfaceDeletedModifiesIpMgr() throws Exception {
long nodeId = 0;
m_processorFactory.setNewSuspect(true);
anticipateEvent("uei.opennms.org/default/trap", m_ip, nodeId);
Event event =
anticipateEvent(EventConstants.INTERFACE_DELETED_EVENT_UEI,
m_ip, nodeId);
m_eventMgr.sendNow(event);
anticipateEvent(EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI, m_ip, nodeId);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// do nothing
}
sendTrap("v1", null, 6, 1);
finishUp();
}
public Event anticipateEvent(String uei) {
return anticipateEvent(uei, m_ip, m_nodeId);
}
public Event anticipateEvent(String uei, String ip, long nodeId) {
EventBuilder bldr = new EventBuilder(uei, "TrapHandlerTestCase");
bldr.setNodeid(nodeId);
bldr.setInterface(addr(ip));
m_anticipator.anticipateEvent(bldr.getEvent());
return bldr.getEvent();
}
public Collection<Event> anticipateAndSend(boolean newSuspectOnTrap, boolean nodeKnown,
String event,
String version, String enterprise,
int generic, int specific) throws Exception {
return anticipateAndSend(newSuspectOnTrap, nodeKnown, event, version, enterprise, generic, specific, null);
}
/**
* @param newSuspectOnTrap Will a new suspect event be triggered by the trap?
* @param nodeKnown Is the node in the database?
* @param event Event that is anticipated to result when the trap is processed
* @param snmpTrapVersion SNMP version of trap, valid values: <code>v1</code>, <code>v2c</code>
* @param enterprise Enterprise ID of the trap
* @param varbinds Varbinds attached to the trap
*/
public Collection<Event> anticipateAndSend(boolean newSuspectOnTrap, boolean nodeKnown,
String event,
String snmpTrapVersion, String enterprise,
int generic, int specific, LinkedHashMap<String, SnmpValue> varbinds) throws Exception {
m_processorFactory.setNewSuspect(newSuspectOnTrap);
if (newSuspectOnTrap) {
// Note: the nodeId will be zero because the node is not known
anticipateEvent(EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI, m_ip, 0);
}
if (event != null) {
if (nodeKnown) {
anticipateEvent(event);
} else {
/*
* If the node is unknown, the nodeId on the trap event
* will be zero.
*/
anticipateEvent(event, m_ip, 0);
}
}
if (varbinds == null) {
sendTrap(snmpTrapVersion, enterprise, generic, specific);
} else {
sendTrap(snmpTrapVersion, enterprise, generic, specific, varbinds);
}
return finishUp();
}
public void sendTrap(String version, String enterprise, int generic,
int specific) throws Exception {
if (enterprise == null) {
enterprise = ".0.0";
}
if (version.equals("v1")) {
sendV1Trap(enterprise, generic, specific);
} else if (version.equals("v2c")) {
sendV2Trap(enterprise, specific);
} else {
throw new Exception("unsupported SNMP version for test: "
+ version);
}
}
private void sendTrap(String version, String enterprise, int generic,
int specific, LinkedHashMap<String, SnmpValue> varbinds) throws Exception {
if (enterprise == null) {
enterprise = ".0.0";
}
if (version.equals("v1")) {
sendV1Trap(enterprise, generic, specific, varbinds);
} else if (version.equals("v2c")) {
sendV2Trap(enterprise, specific, varbinds);
} else {
throw new Exception("unsupported SNMP version for test: "
+ version);
}
}
public void sendV1Trap(String enterprise, int generic, int specific)
throws Exception {
SnmpV1TrapBuilder pdu = SnmpUtils.getV1TrapBuilder();
pdu.setEnterprise(SnmpObjId.get(enterprise));
pdu.setGeneric(generic);
pdu.setSpecific(specific);
pdu.setTimeStamp(0);
pdu.setAgentAddress(m_localhost);
pdu.send(getHostAddress(), m_snmpTrapPort, "public");
}
private String getHostAddress() {
return InetAddressUtils.str(m_localhost);
}
public void sendV1Trap(String enterprise, int generic, int specific, LinkedHashMap<String, SnmpValue> varbinds)
throws Exception {
SnmpV1TrapBuilder pdu = SnmpUtils.getV1TrapBuilder();
pdu.setEnterprise(SnmpObjId.get(enterprise));
pdu.setGeneric(generic);
pdu.setSpecific(specific);
pdu.setTimeStamp(0);
pdu.setAgentAddress(m_localhost);
Iterator<Map.Entry<String,SnmpValue>> it = varbinds.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String,SnmpValue> pairs = it.next();
pdu.addVarBind(SnmpObjId.get(pairs.getKey()), pairs.getValue());
}
pdu.send(getHostAddress(), m_snmpTrapPort, "public");
}
public void sendV2Trap(String enterprise, int specific) throws Exception {
SnmpObjId enterpriseId = SnmpObjId.get(enterprise);
boolean isGeneric = false;
SnmpObjId trapOID;
if (SnmpObjId.get(".1.3.6.1.6.3.1.1.5").isPrefixOf(enterpriseId)) {
isGeneric = true;
trapOID = enterpriseId;
} else {
trapOID = SnmpObjId.get(enterpriseId, new SnmpInstId(specific));
// XXX or should it be this
// trap OID = enterprise + ".0." + specific;
}
SnmpTrapBuilder pdu = SnmpUtils.getV2TrapBuilder();
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.2.1.1.3.0"),
SnmpUtils.getValueFactory().getTimeTicks(0));
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.1.0"),
SnmpUtils.getValueFactory().getObjectId(trapOID));
if (isGeneric) {
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.3.0"),
SnmpUtils.getValueFactory().getObjectId(enterpriseId));
}
pdu.send(getHostAddress(), m_snmpTrapPort, "public");
}
public void sendV2Trap(String enterprise, int specific, LinkedHashMap<String, SnmpValue> varbinds) throws Exception {
SnmpObjId enterpriseId = SnmpObjId.get(enterprise);
boolean isGeneric = false;
SnmpObjId trapOID;
if (SnmpObjId.get(".1.3.6.1.6.3.1.1.5").isPrefixOf(enterpriseId)) {
isGeneric = true;
trapOID = enterpriseId;
} else {
trapOID = SnmpObjId.get(enterpriseId, new SnmpInstId(specific));
// XXX or should it be this
// trap OID = enterprise + ".0." + specific;
}
SnmpTrapBuilder pdu = SnmpUtils.getV2TrapBuilder();
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.2.1.1.3.0"),
SnmpUtils.getValueFactory().getTimeTicks(0));
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.1.0"),
SnmpUtils.getValueFactory().getObjectId(trapOID));
if (isGeneric) {
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.3.0"),
SnmpUtils.getValueFactory().getObjectId(enterpriseId));
}
for (Map.Entry<String, SnmpValue> entry : varbinds.entrySet()) {
pdu.addVarBind(SnmpObjId.get(entry.getKey()), entry.getValue());
}
pdu.send(getHostAddress(), m_snmpTrapPort, "public");
}
}