View Javadoc

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  		super();
49  		this.mappingMap = mappingMap;
50  		this.systemInfo = systemInfo;
51  	}
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  		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  		SnmpTrap trap = null;
68  		final SnmpTrapMapping mapping = this.mappingMap.get(notification.getType());
69  		if (mapping instanceof GenericSnmpTrapMapping) {
70  			final Map<SnmpOid, SnmpTrapData> dataMap = newDataMap(mapping.getDataMap(), notification);
71  			trap = SnmpTrap.newInstance(notification.getTimeStamp() / 10, mapping.getSource(), ((GenericSnmpTrapMapping) mapping).getType(), dataMap);
72  		}
73  		else if (mapping instanceof SpecificSnmpTrapMapping) {
74  			final Map<SnmpOid, SnmpTrapData> dataMap = newDataMap(mapping.getDataMap(), notification);
75  			trap = SnmpTrap.newInstance(notification.getTimeStamp() / 10, mapping.getSource(), ((SpecificSnmpTrapMapping) mapping).getType(), dataMap);
76  		}
77  		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  		final Map<SnmpOid, SnmpTrapData> dataMap = new HashMap<SnmpOid, SnmpTrapData>();
88  		if (mapping.getSequenceNumberOid() != null) {
89  			dataMap.put(mapping.getSequenceNumberOid(), new SnmpTrapData(SnmpDataType.unsigned32, new Long(notification.getSequenceNumber())));
90  		}
91  		if (mapping.getMessageOid() != null) {
92  			dataMap.put(mapping.getMessageOid(), new SnmpTrapData(SnmpDataType.octetString, notification.getMessage()));
93  		}
94  		if (mapping.isHasSystemInfo() && (this.systemInfo != null)) {
95  			dataMap.put(SnmpOid.SYSNAME_OID, new SnmpTrapData(SnmpDataType.octetString, this.systemInfo.getSysName()));
96  			dataMap.put(SnmpOid.SYSDESCR_OID, new SnmpTrapData(SnmpDataType.octetString, this.systemInfo.getSysDescr()));
97  			dataMap.put(SnmpOid.SYSLOCATION_OID, new SnmpTrapData(SnmpDataType.octetString, this.systemInfo.getSysLocation()));
98  			dataMap.put(SnmpOid.SYSCONTACT_OID, new SnmpTrapData(SnmpDataType.octetString, this.systemInfo.getSysContact()));
99  		}
100 		if (mapping instanceof SimpleDataMapTrapMapping) {
101 			final SimpleDataMapTrapMapping simpleMapping = (SimpleDataMapTrapMapping) mapping;
102 			if (notification.getUserData() instanceof Map) {
103 				throw new RuntimeException("UserData field of notification \"" + notification.getType() + "\" contains map - see mapping file of MBean");
104 			}
105 			dataMap.put(simpleMapping.getUserDataOid(), new SnmpTrapData(simpleMapping.getUserDataType(), notification.getUserData()));
106 		}
107 		else if (mapping instanceof MapDataMapTrapMapping) {
108 			if (notification.getUserData() != null) {
109 				final MapDataMapTrapMapping mapMapping = (MapDataMapTrapMapping) mapping;
110 				if (!(notification.getUserData() instanceof Map)) {
111 					throw new RuntimeException("UserData field of notification \"" + notification.getType() + "\" doesn't contain map - see mapping file of MBean");
112 				}
113 				final Map<?, ?> userDataMap = (Map<?, ?>) notification.getUserData();
114 				for (final UserDataEntryDataMapTrapMapping userDataEntry : mapMapping.getUserDataEntryList()) {
115 					if (userDataMap.containsKey(userDataEntry.getKey())) {
116 						dataMap.put(userDataEntry.getOid(), new SnmpTrapData(userDataEntry.getType(), userDataMap.get(userDataEntry.getKey())));
117 					}
118 				}
119 			}
120 		}
121 		return dataMap;
122 	}
123 
124 	/*
125 	 * {@inheritDoc}
126 	 * @see java.lang.Object#toString()
127 	 */
128 	@Override
129 	public final String toString () {
130 		return "SnmpTrapBuilder:" + this.mappingMap;
131 	}
132 
133 }