Coverage Report - net.sf.snmpadaptor4j.core.trap.SnmpTrapBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
SnmpTrapBuilder
100 %
42/42
100 %
26/26
4
 
 1  
 package net.sf.snmpadaptor4j.core.trap;
 2  
 
 3  
 import java.io.Serializable;
 4  
 import java.util.HashMap;
 5  
 import java.util.Map;
 6  
 import javax.management.Notification;
 7  
 import net.sf.snmpadaptor4j.core.mapping.DataMapTrapMapping;
 8  
 import net.sf.snmpadaptor4j.core.mapping.GenericSnmpTrapMapping;
 9  
 import net.sf.snmpadaptor4j.core.mapping.MapDataMapTrapMapping;
 10  
 import net.sf.snmpadaptor4j.core.mapping.SimpleDataMapTrapMapping;
 11  
 import net.sf.snmpadaptor4j.core.mapping.SnmpTrapMapping;
 12  
 import net.sf.snmpadaptor4j.core.mapping.SpecificSnmpTrapMapping;
 13  
 import net.sf.snmpadaptor4j.core.mapping.UserDataEntryDataMapTrapMapping;
 14  
 import net.sf.snmpadaptor4j.mbean.SystemInfo;
 15  
 import net.sf.snmpadaptor4j.object.SnmpDataType;
 16  
 import net.sf.snmpadaptor4j.object.SnmpOid;
 17  
 import net.sf.snmpadaptor4j.object.SnmpTrap;
 18  
 import net.sf.snmpadaptor4j.object.SnmpTrapData;
 19  
 
 20  
 /**
 21  
  * Builder of SNMP traps from JMX notifications for an MBean.
 22  
  * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
 23  
  */
 24  
 public class SnmpTrapBuilder
 25  
                 implements Serializable {
 26  
 
 27  
         /**
 28  
          * Serial number.
 29  
          */
 30  
         private static final long serialVersionUID = -4410423782282390100L;
 31  
 
 32  
         /**
 33  
          * Map of mappings to build SNMP traps for each notification type.
 34  
          */
 35  
         private final Map<String, SnmpTrapMapping> mappingMap;
 36  
 
 37  
         /**
 38  
          * Informations on the system.
 39  
          */
 40  
         private final SystemInfo systemInfo;
 41  
 
 42  
         /**
 43  
          * Constructor.
 44  
          * @param mappingMap Map of mappings to build SNMP traps for each notification type.
 45  
          * @param systemInfo Informations on the system.
 46  
          */
 47  
         public SnmpTrapBuilder (final Map<String, SnmpTrapMapping> mappingMap, final SystemInfo systemInfo) {
 48  17
                 super();
 49  17
                 this.mappingMap = mappingMap;
 50  17
                 this.systemInfo = systemInfo;
 51  17
         }
 52  
 
 53  
         /**
 54  
          * Returns the map of mappings to build SNMP traps for each notification type.
 55  
          * @return Map of mappings to build SNMP traps for each notification type.
 56  
          */
 57  
         public Map<String, SnmpTrapMapping> getMappingMap () {
 58  1
                 return this.mappingMap;
 59  
         }
 60  
 
 61  
         /**
 62  
          * Creates and returns a new SNMP trap by a JMX notification.
 63  
          * @param notification JMX notification generated by a MBean.
 64  
          * @return New SNMP trap or <code>NULL</code> if the notification is not mapped.
 65  
          */
 66  
         public SnmpTrap newTrap (final Notification notification) {
 67  3
                 SnmpTrap trap = null;
 68  3
                 final SnmpTrapMapping mapping = this.mappingMap.get(notification.getType());
 69  3
                 if (mapping instanceof GenericSnmpTrapMapping) {
 70  1
                         final Map<SnmpOid, SnmpTrapData> dataMap = newDataMap(mapping.getDataMap(), notification);
 71  1
                         trap = SnmpTrap.newInstance(notification.getTimeStamp() / 10, mapping.getSource(), ((GenericSnmpTrapMapping) mapping).getType(), dataMap);
 72  1
                 }
 73  2
                 else if (mapping instanceof SpecificSnmpTrapMapping) {
 74  1
                         final Map<SnmpOid, SnmpTrapData> dataMap = newDataMap(mapping.getDataMap(), notification);
 75  1
                         trap = SnmpTrap.newInstance(notification.getTimeStamp() / 10, mapping.getSource(), ((SpecificSnmpTrapMapping) mapping).getType(), dataMap);
 76  
                 }
 77  3
                 return trap;
 78  
         }
 79  
 
 80  
         /**
 81  
          * Creates and returns a new <code>dataMap</code> of {@link SnmpTrap} by a JMX notification.
 82  
          * @param mapping Mapping to build the <code>dataMap</code> field of {@link SnmpTrap} from a JMX notification.
 83  
          * @param notification JMX notification generated by a MBean.
 84  
          * @return New <code>dataMap</code> of {@link SnmpTrap}.
 85  
          */
 86  
         protected final Map<SnmpOid, SnmpTrapData> newDataMap (final DataMapTrapMapping mapping, final Notification notification) {
 87  11
                 final Map<SnmpOid, SnmpTrapData> dataMap = new HashMap<SnmpOid, SnmpTrapData>();
 88  11
                 if (mapping.getSequenceNumberOid() != null) {
 89  9
                         dataMap.put(mapping.getSequenceNumberOid(), new SnmpTrapData(SnmpDataType.unsigned32, new Long(notification.getSequenceNumber())));
 90  
                 }
 91  11
                 if (mapping.getMessageOid() != null) {
 92  9
                         dataMap.put(mapping.getMessageOid(), new SnmpTrapData(SnmpDataType.octetString, notification.getMessage()));
 93  
                 }
 94  11
                 if (mapping.isHasSystemInfo() && (this.systemInfo != null)) {
 95  1
                         dataMap.put(SnmpOid.SYSNAME_OID, new SnmpTrapData(SnmpDataType.octetString, this.systemInfo.getSysName()));
 96  1
                         dataMap.put(SnmpOid.SYSDESCR_OID, new SnmpTrapData(SnmpDataType.octetString, this.systemInfo.getSysDescr()));
 97  1
                         dataMap.put(SnmpOid.SYSLOCATION_OID, new SnmpTrapData(SnmpDataType.octetString, this.systemInfo.getSysLocation()));
 98  1
                         dataMap.put(SnmpOid.SYSCONTACT_OID, new SnmpTrapData(SnmpDataType.octetString, this.systemInfo.getSysContact()));
 99  
                 }
 100  11
                 if (mapping instanceof SimpleDataMapTrapMapping) {
 101  3
                         final SimpleDataMapTrapMapping simpleMapping = (SimpleDataMapTrapMapping) mapping;
 102  3
                         if (notification.getUserData() instanceof Map) {
 103  1
                                 throw new RuntimeException("UserData field of notification \"" + notification.getType() + "\" contains map - see mapping file of MBean");
 104  
                         }
 105  2
                         dataMap.put(simpleMapping.getUserDataOid(), new SnmpTrapData(simpleMapping.getUserDataType(), notification.getUserData()));
 106  2
                 }
 107  8
                 else if (mapping instanceof MapDataMapTrapMapping) {
 108  5
                         if (notification.getUserData() != null) {
 109  3
                                 final MapDataMapTrapMapping mapMapping = (MapDataMapTrapMapping) mapping;
 110  3
                                 if (!(notification.getUserData() instanceof Map)) {
 111  1
                                         throw new RuntimeException("UserData field of notification \"" + notification.getType() + "\" doesn't contain map - see mapping file of MBean");
 112  
                                 }
 113  2
                                 final Map<?, ?> userDataMap = (Map<?, ?>) notification.getUserData();
 114  2
                                 for (final UserDataEntryDataMapTrapMapping userDataEntry : mapMapping.getUserDataEntryList()) {
 115  4
                                         if (userDataMap.containsKey(userDataEntry.getKey())) {
 116  3
                                                 dataMap.put(userDataEntry.getOid(), new SnmpTrapData(userDataEntry.getType(), userDataMap.get(userDataEntry.getKey())));
 117  
                                         }
 118  
                                 }
 119  
                         }
 120  
                 }
 121  9
                 return dataMap;
 122  
         }
 123  
 
 124  
         /*
 125  
          * {@inheritDoc}
 126  
          * @see java.lang.Object#toString()
 127  
          */
 128  
         @Override
 129  
         public final String toString () {
 130  1
                 return "SnmpTrapBuilder:" + this.mappingMap;
 131  
         }
 132  
 
 133  
 }