Coverage Report - net.sf.snmpadaptor4j.api.opennms.OpennmsSnmpTrapSender
 
Classes in this File Line Coverage Branch Coverage Complexity
OpennmsSnmpTrapSender
100 %
136/136
100 %
70/70
4,5
OpennmsSnmpTrapSender$NullHandler
100 %
4/4
N/A
4,5
 
 1  
 package net.sf.snmpadaptor4j.api.opennms;
 2  
 
 3  
 import java.net.InetAddress;
 4  
 import java.util.Map.Entry;
 5  
 import org.opennms.protocols.snmp.SnmpHandler;
 6  
 import org.opennms.protocols.snmp.SnmpIPAddress;
 7  
 import org.opennms.protocols.snmp.SnmpObjectId;
 8  
 import org.opennms.protocols.snmp.SnmpParameters;
 9  
 import org.opennms.protocols.snmp.SnmpPduPacket;
 10  
 import org.opennms.protocols.snmp.SnmpPduRequest;
 11  
 import org.opennms.protocols.snmp.SnmpPduTrap;
 12  
 import org.opennms.protocols.snmp.SnmpPeer;
 13  
 import org.opennms.protocols.snmp.SnmpSMI;
 14  
 import org.opennms.protocols.snmp.SnmpSession;
 15  
 import org.opennms.protocols.snmp.SnmpSyntax;
 16  
 import org.opennms.protocols.snmp.SnmpVarBind;
 17  
 import net.sf.snmpadaptor4j.api.SnmpTrapSender;
 18  
 import net.sf.snmpadaptor4j.object.SnmpDataType;
 19  
 import net.sf.snmpadaptor4j.object.SnmpOid;
 20  
 import net.sf.snmpadaptor4j.object.SnmpTrap;
 21  
 import net.sf.snmpadaptor4j.object.SnmpTrapData;
 22  
 import net.sf.snmpadaptor4j.object.GenericSnmpTrap;
 23  
 import net.sf.snmpadaptor4j.object.SpecificSnmpTrap;
 24  
 import net.sf.snmpadaptor4j.object.GenericSnmpTrapType;
 25  
 
 26  
 /**
 27  
  * SNMP trap sender implementation for <b>joesnmp</b> API.
 28  
  * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
 29  
  */
 30  
 public final class OpennmsSnmpTrapSender
 31  
                 extends OpennmsSupport
 32  
                 implements SnmpTrapSender {
 33  
 
 34  
         /**
 35  
          * OID CONSTANT: Time stamp.
 36  
          */
 37  1
         private static final SnmpObjectId TIME_STAMP_OID = new SnmpObjectId("1.3.6.1.2.1.1.3.0");
 38  
 
 39  
         /**
 40  
          * OID CONSTANT: Type.
 41  
          */
 42  1
         private static final SnmpObjectId TYPE_OID = new SnmpObjectId("1.3.6.1.6.3.1.1.4.1.0");
 43  
 
 44  
         /**
 45  
          * OID CONSTANT: Agent address.
 46  
          */
 47  1
         private static final SnmpObjectId AGENT_ADDRESS_OID = new SnmpObjectId("1.3.6.1.6.3.18.1.3.0");
 48  
 
 49  
         /**
 50  
          * OID CONSTANT: Community name.
 51  
          */
 52  1
         private static final SnmpObjectId COMMUNITY_OID = new SnmpObjectId("1.3.6.1.6.3.18.1.4.0");
 53  
 
 54  
         /**
 55  
          * OID CONSTANT: Enterprise.
 56  
          */
 57  1
         private static final SnmpObjectId ENTERPRISE_OID = new SnmpObjectId("1.3.6.1.6.3.1.1.4.3.0");
 58  
 
 59  
         /**
 60  
          * TYPE VALUE CONSTANT: coldStart.
 61  
          */
 62  1
         private static final SnmpOid GENERIC_COLD_START_TYPE_VALUE = SnmpOid.newInstance("1.3.6.1.6.3.1.1.5.1");
 63  
 
 64  
         /**
 65  
          * TYPE VALUE CONSTANT: warmStart.
 66  
          */
 67  1
         private static final SnmpOid GENERIC_WARM_START_TYPE_VALUE = SnmpOid.newInstance("1.3.6.1.6.3.1.1.5.2");
 68  
 
 69  
         /**
 70  
          * TYPE VALUE CONSTANT: linkDown.
 71  
          */
 72  1
         private static final SnmpOid GENERIC_LINK_DOWN_TYPE_VALUE = SnmpOid.newInstance("1.3.6.1.6.3.1.1.5.3");
 73  
 
 74  
         /**
 75  
          * TYPE VALUE CONSTANT: linkUp.
 76  
          */
 77  1
         private static final SnmpOid GENERIC_LINK_UP_TYPE_VALUE = SnmpOid.newInstance("1.3.6.1.6.3.1.1.5.4");
 78  
 
 79  
         /**
 80  
          * TYPE VALUE CONSTANT: authenticationFailure.
 81  
          */
 82  1
         private static final SnmpOid GENERIC_AUTHENTICATION_FAILURE_TYPE_VALUE = SnmpOid.newInstance("1.3.6.1.6.3.1.1.5.5");
 83  
 
 84  
         /**
 85  
          * TYPE VALUE CONSTANT: egpNeighborLoss.
 86  
          */
 87  1
         private static final SnmpOid GENERIC_EGP_NEIGHBOR_LOSS_TYPE_VALUE = SnmpOid.newInstance("1.3.6.1.6.3.1.1.5.6");
 88  
 
 89  
         /**
 90  
          * Null SNMP handler.
 91  
          */
 92  23
         protected final class NullHandler
 93  
                         implements SnmpHandler {
 94  
 
 95  
                 /*
 96  
                  * {@inheritDoc}
 97  
                  * @see org.opennms.protocols.snmp.SnmpHandler#snmpReceivedPdu(org.opennms.protocols.snmp.SnmpSession, int, org.opennms.protocols.snmp.SnmpPduPacket)
 98  
                  */
 99  
                 public void snmpReceivedPdu (final SnmpSession session, final int command, final SnmpPduPacket pdu) {
 100  
                         // NOP
 101  1
                 }
 102  
 
 103  
                 /*
 104  
                  * {@inheritDoc}
 105  
                  * @see org.opennms.protocols.snmp.SnmpHandler#snmpInternalError(org.opennms.protocols.snmp.SnmpSession, int, org.opennms.protocols.snmp.SnmpSyntax)
 106  
                  */
 107  
                 public void snmpInternalError (final SnmpSession session, final int err, final SnmpSyntax pdu) {
 108  
                         // NOP
 109  1
                 }
 110  
 
 111  
                 /*
 112  
                  * {@inheritDoc}
 113  
                  * @see org.opennms.protocols.snmp.SnmpHandler#snmpTimeoutError(org.opennms.protocols.snmp.SnmpSession, org.opennms.protocols.snmp.SnmpSyntax)
 114  
                  */
 115  
                 public void snmpTimeoutError (final SnmpSession session, final SnmpSyntax pdu) {
 116  
                         // NOP
 117  1
                 }
 118  
 
 119  
         }
 120  
 
 121  
         /**
 122  
          * IP address of SNMP agent.
 123  
          */
 124  
         private final SnmpIPAddress agentAddress;
 125  
 
 126  
         /**
 127  
          * IP address of SNMP manager responsible of traps handling.
 128  
          */
 129  
         private final String managerAddress;
 130  
 
 131  
         /**
 132  
          * UDP port of SNMP manager responsible of traps handling.
 133  
          */
 134  
         private final int managerPort;
 135  
 
 136  
         /**
 137  
          * Protocol version of SNMP manager.
 138  
          */
 139  
         private final int managerVersion;
 140  
 
 141  
         /**
 142  
          * Community of SNMP manager.
 143  
          */
 144  
         private final String managerCommunity;
 145  
 
 146  
         /**
 147  
          * SNMP session.
 148  
          */
 149  
         private SnmpSession snmpSession;
 150  
 
 151  
         /**
 152  
          * Hidden constructor.
 153  
          * @param agentAddress IP address of SNMP agent (must be a local address of the host).
 154  
          * @param managerAddress IP address of SNMP manager responsible of traps handling.
 155  
          * @param managerPort UDP port of SNMP manager responsible of traps handling.
 156  
          * @param managerVersion Protocol version of SNMP manager.
 157  
          * @param managerCommunity Community of SNMP manager.
 158  
          * @see OpennmsSnmpApiFactory#newSnmpTrapSender(InetAddress, String, int, int, String)
 159  
          */
 160  
         OpennmsSnmpTrapSender (final InetAddress agentAddress, final String managerAddress, final int managerPort, final int managerVersion,
 161  
                         final String managerCommunity) {
 162  37
                 super();
 163  37
                 this.agentAddress = new SnmpIPAddress(agentAddress);
 164  37
                 this.managerAddress = managerAddress;
 165  37
                 this.managerPort = managerPort;
 166  37
                 this.managerVersion = managerVersion;
 167  37
                 this.managerCommunity = managerCommunity;
 168  37
         }
 169  
 
 170  
         /*
 171  
          * {@inheritDoc}
 172  
          * @see net.sf.snmpadaptor4j.api.SnmpTrapSender#getName()
 173  
          */
 174  
         public String getName () {
 175  2
                 return this.managerAddress + ":" + this.managerPort + "/" + this.managerCommunity;
 176  
         }
 177  
 
 178  
         /*
 179  
          * {@inheritDoc}
 180  
          * @see net.sf.snmpadaptor4j.api.SnmpTrapSender#open()
 181  
          */
 182  
         public void open () throws Exception {
 183  22
                 synchronized (this) {
 184  22
                         if (this.snmpSession == null) {
 185  20
                                 if (this.logger.isTraceEnabled()) {
 186  3
                                         this.logger.trace("Connecting to " + this.managerAddress + ":" + this.managerPort + "...");
 187  
                                 }
 188  20
                                 final SnmpPeer peer = new SnmpPeer(InetAddress.getByName(this.managerAddress));
 189  20
                                 peer.setPort(this.managerPort);
 190  20
                                 final SnmpParameters params = peer.getParameters();
 191  20
                                 params.setReadCommunity(this.managerCommunity);
 192  20
                                 switch (this.managerVersion) {
 193  
                                         case 1:
 194  11
                                                 params.setVersion(SnmpSMI.SNMPV1);
 195  11
                                                 break;
 196  
                                         default:
 197  9
                                                 params.setVersion(SnmpSMI.SNMPV2);
 198  
                                                 break;
 199  
                                 }
 200  20
                                 this.snmpSession = new SnmpSession(peer);
 201  20
                                 this.snmpSession.setDefaultHandler(new NullHandler());
 202  20
                                 Thread.sleep(100);
 203  20
                                 this.logger.trace("Connected");
 204  20
                         }
 205  2
                         else if (this.logger.isTraceEnabled()) {
 206  1
                                 this.logger.trace(this.managerAddress + ":" + this.managerPort + " already connected");
 207  
                         }
 208  22
                 }
 209  22
         }
 210  
 
 211  
         /*
 212  
          * {@inheritDoc}
 213  
          * @see net.sf.snmpadaptor4j.api.SnmpTrapSender#close()
 214  
          */
 215  
         public void close () {
 216  22
                 synchronized (this) {
 217  22
                         if (this.snmpSession != null) {
 218  20
                                 if (this.logger.isTraceEnabled()) {
 219  3
                                         this.logger.trace("Disconnecting from " + this.managerAddress + ":" + this.managerPort + "...");
 220  
                                 }
 221  20
                                 this.snmpSession.close();
 222  20
                                 this.snmpSession = null;
 223  20
                                 this.logger.trace("Disconnected");
 224  
                         }
 225  2
                         else if (this.logger.isTraceEnabled()) {
 226  1
                                 this.logger.trace(this.managerAddress + ":" + this.managerPort + " already disconnected");
 227  
                         }
 228  22
                 }
 229  22
         }
 230  
 
 231  
         /*
 232  
          * {@inheritDoc}
 233  
          * @see net.sf.snmpadaptor4j.api.SnmpTrapSender#isConnected()
 234  
          */
 235  
         public boolean isConnected () {
 236  15
                 return (this.snmpSession != null);
 237  
         }
 238  
 
 239  
         /*
 240  
          * {@inheritDoc}
 241  
          * @see net.sf.snmpadaptor4j.api.SnmpTrapSender#send(net.sf.snmpadaptor4j.object.SnmpTrap)
 242  
          */
 243  
         public void send (final SnmpTrap trap) throws Exception {
 244  22
                 switch (this.managerVersion) {
 245  
                         case 1:
 246  11
                                 sendV1(trap);
 247  7
                                 break;
 248  
                         default:
 249  11
                                 sendV2(trap);
 250  
                                 break;
 251  
                 }
 252  14
         }
 253  
 
 254  
         /**
 255  
          * Sends a SNMP trap V1 to the manager.
 256  
          * @param trap Object representing a specific or generic SNMP trap.
 257  
          * @throws Exception Exception if an error has occurred.
 258  
          */
 259  
         private void sendV1 (final SnmpTrap trap) throws Exception {
 260  
 
 261  
                 // Packet building
 262  11
                 final SnmpPduTrap trapPdu = new SnmpPduTrap();
 263  11
                 trapPdu.setTimeStamp(trap.getTimeStamp());
 264  11
                 if (trap.getSource() == null) {
 265  1
                         throw new Exception("The source of trap is missing");
 266  
                 }
 267  10
                 trapPdu.setEnterprise(new SnmpObjectId(trap.getSource().getOid()));
 268  10
                 trapPdu.setAgentAddress(this.agentAddress);
 269  10
                 if (trap instanceof SpecificSnmpTrap) {
 270  2
                         trapPdu.setGeneric(SnmpPduTrap.GenericEnterpriseSpecific);
 271  2
                         trapPdu.setSpecific(((SpecificSnmpTrap) trap).getType());
 272  
                 }
 273  8
                 else if (trap instanceof GenericSnmpTrap) {
 274  7
                         final GenericSnmpTrap genericTrap = (GenericSnmpTrap) trap;
 275  7
                         if (genericTrap.getType() == GenericSnmpTrapType.coldStart) {
 276  1
                                 trapPdu.setGeneric(SnmpPduTrap.GenericColdStart);
 277  
                         }
 278  6
                         else if (genericTrap.getType() == GenericSnmpTrapType.warmStart) {
 279  1
                                 trapPdu.setGeneric(SnmpPduTrap.GenericWarmStart);
 280  
                         }
 281  5
                         else if (genericTrap.getType() == GenericSnmpTrapType.linkDown) {
 282  1
                                 trapPdu.setGeneric(SnmpPduTrap.GenericLinkDown);
 283  
                         }
 284  4
                         else if (genericTrap.getType() == GenericSnmpTrapType.linkUp) {
 285  1
                                 trapPdu.setGeneric(SnmpPduTrap.GenericLinkUp);
 286  
                         }
 287  3
                         else if (genericTrap.getType() == GenericSnmpTrapType.authenticationFailure) {
 288  1
                                 trapPdu.setGeneric(SnmpPduTrap.GenericAuthenticationFailure);
 289  
                         }
 290  2
                         else if (genericTrap.getType() == GenericSnmpTrapType.egpNeighborLoss) {
 291  1
                                 trapPdu.setGeneric(SnmpPduTrap.GenericEgpNeighborLoss);
 292  
                         }
 293  
                         else {
 294  1
                                 throw new Exception("Generic trap type unknown");
 295  
                         }
 296  6
                         trapPdu.setSpecific(0);
 297  6
                 }
 298  
                 else {
 299  1
                         throw new Exception("Trap type unknown");
 300  
                 }
 301  8
                 for (Entry<SnmpOid, SnmpTrapData> entry : trap.getDataMap().entrySet()) {
 302  14
                         trapPdu.addVarBind(new SnmpVarBind(new SnmpObjectId(entry.getKey().getOid()), newSnmpValue(entry.getValue().getType(), entry.getValue().getValue())));
 303  
                 }
 304  
 
 305  
                 // Sending
 306  8
                 synchronized (this) {
 307  8
                         if (this.logger.isTraceEnabled()) {
 308  1
                                 this.logger.trace(trap + " sending to " + this.managerAddress + ":" + this.managerPort + "...");
 309  
                         }
 310  8
                         if (this.snmpSession == null) {
 311  1
                                 throw new Exception("The connection to the SNMP manager is closed");
 312  
                         }
 313  7
                         this.snmpSession.send(trapPdu);
 314  7
                         if (this.logger.isTraceEnabled()) {
 315  1
                                 this.logger.trace(trap + " sent");
 316  
                         }
 317  7
                 }
 318  
 
 319  7
         }
 320  
 
 321  
         /**
 322  
          * Sends a SNMP trap V2 to the manager.
 323  
          * @param trap Object representing a SNMP trap V2.
 324  
          * @throws Exception Exception if an error has occurred.
 325  
          */
 326  
         private void sendV2 (final SnmpTrap trap) throws Exception {
 327  
 
 328  
                 // Packet building
 329  11
                 final SnmpPduRequest trapPdu = new SnmpPduRequest(SnmpPduPacket.V2TRAP);
 330  11
                 trapPdu.addVarBind(new SnmpVarBind(OpennmsSnmpTrapSender.TIME_STAMP_OID, newSnmpValue(SnmpDataType.timeTicks, new Long(trap.getTimeStamp()))));
 331  11
                 if (trap.getSource() == null) {
 332  1
                         throw new Exception("The source of trap is missing");
 333  
                 }
 334  
                 SnmpOid typeValue;
 335  10
                 if (trap instanceof SpecificSnmpTrap) {
 336  2
                         typeValue = SnmpOid.newInstance(trap.getSource(), 0, ((SpecificSnmpTrap) trap).getType() + 1);
 337  
                 }
 338  8
                 else if (trap instanceof GenericSnmpTrap) {
 339  7
                         final GenericSnmpTrap genericTrap = (GenericSnmpTrap) trap;
 340  7
                         if (genericTrap.getType() == GenericSnmpTrapType.coldStart) {
 341  1
                                 typeValue = OpennmsSnmpTrapSender.GENERIC_COLD_START_TYPE_VALUE;
 342  
                         }
 343  6
                         else if (genericTrap.getType() == GenericSnmpTrapType.warmStart) {
 344  1
                                 typeValue = OpennmsSnmpTrapSender.GENERIC_WARM_START_TYPE_VALUE;
 345  
                         }
 346  5
                         else if (genericTrap.getType() == GenericSnmpTrapType.linkDown) {
 347  1
                                 typeValue = OpennmsSnmpTrapSender.GENERIC_LINK_DOWN_TYPE_VALUE;
 348  
                         }
 349  4
                         else if (genericTrap.getType() == GenericSnmpTrapType.linkUp) {
 350  1
                                 typeValue = OpennmsSnmpTrapSender.GENERIC_LINK_UP_TYPE_VALUE;
 351  
                         }
 352  3
                         else if (genericTrap.getType() == GenericSnmpTrapType.authenticationFailure) {
 353  1
                                 typeValue = OpennmsSnmpTrapSender.GENERIC_AUTHENTICATION_FAILURE_TYPE_VALUE;
 354  
                         }
 355  2
                         else if (genericTrap.getType() == GenericSnmpTrapType.egpNeighborLoss) {
 356  1
                                 typeValue = OpennmsSnmpTrapSender.GENERIC_EGP_NEIGHBOR_LOSS_TYPE_VALUE;
 357  
                         }
 358  
                         else {
 359  1
                                 throw new Exception("Generic trap type unknown");
 360  
                         }
 361  6
                 }
 362  
                 else {
 363  1
                         throw new Exception("Trap type unknown");
 364  
                 }
 365  8
                 trapPdu.addVarBind(new SnmpVarBind(OpennmsSnmpTrapSender.TYPE_OID, newSnmpValue(SnmpDataType.objectIdentifier, typeValue)));
 366  8
                 trapPdu.addVarBind(new SnmpVarBind(OpennmsSnmpTrapSender.AGENT_ADDRESS_OID, newSnmpValue(SnmpDataType.ipAddress, this.agentAddress.convertToIpAddress())));
 367  8
                 trapPdu.addVarBind(new SnmpVarBind(OpennmsSnmpTrapSender.COMMUNITY_OID, newSnmpValue(SnmpDataType.octetString, this.managerCommunity)));
 368  8
                 trapPdu.addVarBind(new SnmpVarBind(OpennmsSnmpTrapSender.ENTERPRISE_OID, newSnmpValue(SnmpDataType.objectIdentifier, trap.getSource())));
 369  8
                 for (Entry<SnmpOid, SnmpTrapData> entry : trap.getDataMap().entrySet()) {
 370  14
                         trapPdu.addVarBind(new SnmpVarBind(new SnmpObjectId(entry.getKey().getOid()), newSnmpValue(entry.getValue().getType(), entry.getValue().getValue())));
 371  
                 }
 372  
 
 373  
                 // Sending
 374  8
                 synchronized (this) {
 375  8
                         if (this.logger.isTraceEnabled()) {
 376  1
                                 this.logger.trace(trap + " sending to " + this.managerAddress + ":" + this.managerPort + "...");
 377  
                         }
 378  8
                         if (this.snmpSession == null) {
 379  1
                                 throw new Exception("The connection to the SNMP manager is closed");
 380  
                         }
 381  7
                         this.snmpSession.send(trapPdu);
 382  7
                         if (this.logger.isTraceEnabled()) {
 383  1
                                 this.logger.trace(trap + " sent");
 384  
                         }
 385  7
                 }
 386  
 
 387  7
         }
 388  
 
 389  
         /*
 390  
          * {@inheritDoc}
 391  
          * @see java.lang.Object#toString()
 392  
          */
 393  
         @Override
 394  
         public String toString () {
 395  1
                 return "SnmpTrapSender:opennms[" + getName() + "]";
 396  
         }
 397  
 
 398  
 }