1 20 21 22 package org.snmp4j.agent.mo.snmp; 23 24 27 import org.snmp4j.smi.*; 28 import org.snmp4j.mp.SnmpConstants; 29 import org.snmp4j.agent.*; 30 import org.snmp4j.agent.mo.*; 31 import org.snmp4j.agent.mo.snmp.*; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.TreeMap ; 35 import org.snmp4j.log.LogAdapter; 36 import org.snmp4j.log.LogFactory; 37 38 41 public class SnmpNotificationMIB implements MOGroup { 42 43 private static final LogAdapter logger = 44 LogFactory.getLogger(SnmpNotificationMIB.class); 45 46 48 private static final OID oidSnmpNotifyEntry = 49 new OID(new int[] { 1,3,6,1,6,3,13,1,1,1 }); 50 51 private static final int colSnmpNotifyTag = 2; 53 private static final int colSnmpNotifyType = 3; 54 private static final int colSnmpNotifyStorageType = 4; 55 private static final int colSnmpNotifyRowStatus = 5; 56 57 static final int idxSnmpNotifyTag = 0; 59 static final int idxSnmpNotifyType = 1; 60 static final int idxSnmpNotifyStorageType = 2; 61 static final int idxSnmpNotifyRowStatus = 3; 62 63 private static MOTableSubIndex[] snmpNotifyEntryIndexes = 64 new MOTableSubIndex[] { 65 new MOTableSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 1, 32) 66 }; 67 68 private static MOTableIndex snmpNotifyEntryIndex = 69 new MOTableIndex(snmpNotifyEntryIndexes, true) { 70 public boolean isValidIndex(OID index) { 71 boolean isValidIndex = super.isValidIndex(index); 72 if (isValidIndex) { 73 } 76 return isValidIndex; 77 } 78 }; 79 80 81 private DefaultMOTable snmpNotifyEntry; 82 private DefaultMOMutableTableModel snmpNotifyEntryModel; 83 private static final OID oidSnmpNotifyFilterEntry = 84 new OID(new int[] { 1,3,6,1,6,3,13,1,3,1 }); 85 86 private static final int colSnmpNotifyFilterMask = 2; 88 private static final int colSnmpNotifyFilterType = 3; 89 private static final int colSnmpNotifyFilterStorageType = 4; 90 private static final int colSnmpNotifyFilterRowStatus = 5; 91 92 static final int idxSnmpNotifyFilterSubtree = 1; 94 static final int idxSnmpNotifyFilterMask = 0; 95 static final int idxSnmpNotifyFilterType = 1; 96 static final int idxSnmpNotifyFilterStorageType = 2; 97 static final int idxSnmpNotifyFilterRowStatus = 3; 98 private static MOTableSubIndex[] snmpNotifyFilterEntryIndexes = 99 new MOTableSubIndex[] { 100 new MOTableSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 1, 32), 101 new MOTableSubIndex(SMIConstants.SYNTAX_OBJECT_IDENTIFIER, 0, 128) }; 102 103 private static MOTableIndex snmpNotifyFilterEntryIndex = 104 new MOTableIndex(snmpNotifyFilterEntryIndexes, true) { 105 public boolean isValidIndex(OID index) { 106 boolean isValidIndex = super.isValidIndex(index); 107 if (isValidIndex) { 108 } 111 return isValidIndex; 112 } 113 }; 114 115 116 private DefaultMOTable snmpNotifyFilterEntry; 117 private DefaultMOMutableTableModel snmpNotifyFilterEntryModel; 118 private static final OID oidSnmpNotifyFilterProfileEntry = 119 new OID(new int[] { 1,3,6,1,6,3,13,1,2,1 }); 120 121 private static final int colSnmpNotifyFilterProfileName = 1; 123 private static final int colSnmpNotifyFilterProfileStorType = 2; 124 private static final int colSnmpNotifyFilterProfileRowStatus = 3; 125 126 private static final int idxSnmpNotifyFilterProfileName = 0; 128 private static final int idxSnmpNotifyFilterProfileStorType = 1; 129 private static final int idxSnmpNotifyFilterProfileRowStatus = 2; 130 private static MOTableSubIndex[] snmpNotifyFilterProfileEntryIndexes = 131 new MOTableSubIndex[] { 132 new MOTableSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 1, 32) 133 }; 134 135 private static MOTableIndex snmpNotifyFilterProfileEntryIndex = 136 new MOTableIndex(snmpNotifyFilterProfileEntryIndexes, true) { 137 public boolean isValidIndex(OID index) { 138 boolean isValidIndex = super.isValidIndex(index); 139 if (isValidIndex) { 140 } 143 return isValidIndex; 144 } 145 }; 146 147 148 private DefaultMOTable snmpNotifyFilterProfileEntry; 149 private DefaultMOMutableTableModel snmpNotifyFilterProfileEntryModel; 150 151 public SnmpNotificationMIB() { 152 createSnmpNotifyEntry(); 153 createSnmpNotifyFilterEntry(); 154 createSnmpNotifyFilterProfileEntry(); 155 } 156 157 158 private void createSnmpNotifyEntry() { 159 MOColumn[] snmpNotifyEntryColumns = new MOColumn[4]; 160 snmpNotifyEntryColumns[idxSnmpNotifyTag] = 161 new SnmpTagValue(colSnmpNotifyTag, 162 MOAccessImpl.ACCESS_READ_CREATE, 163 new OctetString(new byte[] { }), 164 true); 165 snmpNotifyEntryColumns[idxSnmpNotifyType] = 166 new Enumerated(colSnmpNotifyType, 167 MOAccessImpl.ACCESS_READ_CREATE, 168 new Integer32(1), 169 true, 170 new int[] { 1, 2 }); 171 snmpNotifyEntryColumns[idxSnmpNotifyStorageType] = 172 new StorageType(colSnmpNotifyStorageType, 173 MOAccessImpl.ACCESS_READ_CREATE, 174 new Integer32(3), 175 true); 176 snmpNotifyEntryColumns[idxSnmpNotifyRowStatus] = 177 new RowStatus(colSnmpNotifyRowStatus); 178 179 snmpNotifyEntry = 180 new DefaultMOTable(oidSnmpNotifyEntry, 181 snmpNotifyEntryIndex, 182 snmpNotifyEntryColumns); 183 snmpNotifyEntryModel = new DefaultMOMutableTableModel(); 184 snmpNotifyEntryModel.setRowFactory(new DefaultMOMutableRow2PCFactory()); 185 snmpNotifyEntry.setModel(snmpNotifyEntryModel); 186 } 187 188 private void createSnmpNotifyFilterEntry() { 189 MOColumn[] snmpNotifyFilterEntryColumns = new MOColumn[4]; 190 snmpNotifyFilterEntryColumns[idxSnmpNotifyFilterMask] = 191 new MOMutableColumn(colSnmpNotifyFilterMask, 192 SMIConstants.SYNTAX_OCTET_STRING, 193 MOAccessImpl.ACCESS_READ_CREATE, 194 new OctetString(new byte[] { }), 195 true); 196 ((MOMutableColumn)snmpNotifyFilterEntryColumns[idxSnmpNotifyFilterMask]). 197 addMOValueValidationListener(new SnmpNotifyFilterMaskValidator()); 198 snmpNotifyFilterEntryColumns[idxSnmpNotifyFilterType] = 199 new Enumerated(colSnmpNotifyFilterType, 200 MOAccessImpl.ACCESS_READ_CREATE, 201 new Integer32(1), 202 true, 203 new int[] { 1, 2 }); 204 snmpNotifyFilterEntryColumns[idxSnmpNotifyFilterStorageType] = 205 new StorageType(colSnmpNotifyFilterStorageType, 206 MOAccessImpl.ACCESS_READ_CREATE, 207 new Integer32(3), 208 true); 209 snmpNotifyFilterEntryColumns[idxSnmpNotifyFilterRowStatus] = 210 new RowStatus(colSnmpNotifyFilterRowStatus); 211 212 snmpNotifyFilterEntry = 213 new DefaultMOTable(oidSnmpNotifyFilterEntry, 214 snmpNotifyFilterEntryIndex, 215 snmpNotifyFilterEntryColumns); 216 snmpNotifyFilterEntryModel = new DefaultMOMutableTableModel(); 217 snmpNotifyFilterEntryModel.setRowFactory(new DefaultMOMutableRow2PCFactory()); 218 snmpNotifyFilterEntry.setModel(snmpNotifyFilterEntryModel); 219 } 220 221 private void createSnmpNotifyFilterProfileEntry() { 222 MOColumn[] snmpNotifyFilterProfileEntryColumns = new MOColumn[3]; 223 snmpNotifyFilterProfileEntryColumns[idxSnmpNotifyFilterProfileName] = 224 new SnmpAdminString(colSnmpNotifyFilterProfileName, 225 MOAccessImpl.ACCESS_READ_CREATE, 226 new OctetString(), 227 true, 1, 32); 228 snmpNotifyFilterProfileEntryColumns[idxSnmpNotifyFilterProfileStorType] = 229 new StorageType(colSnmpNotifyFilterProfileStorType, 230 MOAccessImpl.ACCESS_READ_CREATE, 231 new Integer32(3), 232 true); 233 snmpNotifyFilterProfileEntryColumns[idxSnmpNotifyFilterProfileRowStatus] = 234 new RowStatus(colSnmpNotifyFilterProfileRowStatus); 235 236 snmpNotifyFilterProfileEntry = 237 new DefaultMOTable(oidSnmpNotifyFilterProfileEntry, 238 snmpNotifyFilterProfileEntryIndex, 239 snmpNotifyFilterProfileEntryColumns); 240 snmpNotifyFilterProfileEntryModel = new DefaultMOMutableTableModel(); 241 snmpNotifyFilterProfileEntryModel.setRowFactory(new DefaultMOMutableRow2PCFactory()); 242 snmpNotifyFilterProfileEntry.setModel(snmpNotifyFilterProfileEntryModel); 243 } 244 245 246 public void registerMOs(MOServer server, OctetString context) 247 throws DuplicateRegistrationException 248 { 249 server.register(this.snmpNotifyEntry, context); 251 server.register(this.snmpNotifyFilterEntry, context); 252 server.register(this.snmpNotifyFilterProfileEntry, context); 253 } 254 255 public void unregisterMOs(MOServer server, OctetString context) { 256 server.unregister(this.snmpNotifyEntry, context); 258 server.unregister(this.snmpNotifyFilterEntry, context); 259 server.unregister(this.snmpNotifyFilterProfileEntry, context); 260 } 261 262 public boolean addNotifyEntry(OctetString name, OctetString tag, int type, 263 int storageType) { 264 Variable[] vbs = new Variable[snmpNotifyEntry.getColumnCount()]; 265 int n=0; 266 vbs[n++] = tag; 267 vbs[n++] = new Integer32(type); 268 vbs[n++] = new Integer32(storageType); 269 vbs[n++] = new Integer32(RowStatus.active); 270 OID index = name.toSubIndex(true); 271 MOTableRow row = snmpNotifyEntry.createRow(index, vbs); 272 snmpNotifyEntry.addRow(row); 273 return true; 274 } 275 276 public DefaultMOTable getNotifyTable() { 277 return snmpNotifyEntry; 278 } 279 280 public DefaultMOTable getNotifyFilterTable() { 281 return snmpNotifyFilterEntry; 282 } 283 284 public DefaultMOTable getNotifyFilterProfileTable() { 285 return snmpNotifyFilterProfileEntry; 286 } 287 288 public boolean passesFilter(OID paramsIndex, OID notificationID, 289 VariableBinding[] vbs) { 290 MOTableRowFilter activeFilter = 291 new RowStatus.ActiveRowsFilter(idxSnmpNotifyFilterProfileRowStatus); 292 List profiles = 293 snmpNotifyFilterProfileEntryModel.getRows(paramsIndex, paramsIndex, 294 activeFilter); 295 if (profiles.size() == 0) { 296 return true; 298 } 299 OctetString profileName = (OctetString) 300 ((MOTableRow)profiles.get(0)).getValue(idxSnmpNotifyFilterProfileName); 301 OID profileNameOID = profileName.toSubIndex(true); 302 OID profileNameOIDNext = profileNameOID.successor(); 303 activeFilter = new RowStatus.ActiveRowsFilter(idxSnmpNotifyFilterRowStatus); 304 List filters = 305 snmpNotifyFilterEntryModel.getRows(profileNameOID, 306 profileNameOIDNext, 307 activeFilter); 308 if (filters.size() == 0) { 309 return false; 311 } 312 TreeMap oidMatches = new TreeMap (); 313 TreeMap [] vbMatches = new TreeMap [vbs.length]; 314 for (Iterator it = filters.iterator(); it.hasNext(); ) { 315 MOTableRow row = (MOTableRow) it.next(); 316 Variable[] indexValues = 317 snmpNotifyFilterEntryIndex.getIndexValues(row.getIndex()); 318 OID subtree = (OID) indexValues[idxSnmpNotifyFilterSubtree]; 319 OctetString mask = (OctetString)row.getValue(idxSnmpNotifyFilterMask); 320 Integer32 type = (Integer32)row.getValue(idxSnmpNotifyFilterType); 321 OID subtreeWithLength = new OID(); 322 subtreeWithLength.append(subtree.size()); 323 subtreeWithLength.append(subtree); 324 if (isInSubtree(notificationID, subtree, mask)) { 325 oidMatches.put(subtreeWithLength, type); 326 } 327 for (int i=0; i<vbs.length; i++) { 329 if (isInSubtree(vbs[i].getOid(), subtree, mask)) { 330 if (vbMatches[i] == null) { 331 vbMatches[i] = new TreeMap (); 332 } 333 vbMatches[i].put(subtreeWithLength, type); 334 } 335 } 336 } 337 if (oidMatches.size() == 0) { 338 if (logger.isInfoEnabled()) { 340 logger.info("Filter " + profileName + 341 " has no matches for notification ID " + 342 notificationID); 343 } 344 return false; 345 } 346 else if (((Integer32)oidMatches.get(oidMatches.lastKey())).getValue() == 347 SnmpNotifyFilterTypeEnum.excluded) { 348 if (logger.isInfoEnabled()) { 349 logger.info("Notification ID " + notificationID + 350 " is excluded from filter " + profileName); 351 } 352 return false; 353 } 354 for (int i=0; i<vbMatches.length; i++) { 355 if ((vbMatches[i] != null) && (vbMatches[i].size() > 0)) { 356 if (((Integer32)vbMatches[i].get(vbMatches[i].lastKey())).getValue() == 357 SnmpNotifyFilterTypeEnum.excluded) { 358 if (logger.isInfoEnabled()) { 359 logger.info("Variable binding " + vbs[i] + 360 " is not in filter " + profileName); 361 } 362 return false; 363 } 364 } 365 } 366 return true; 367 } 368 369 private static boolean isInSubtree(OID oid, OID subtree, OctetString mask) { 370 OID maskedSubtree = subtree.mask(mask); 371 OID maskedOID = oid.mask(mask); 372 if (maskedOID.equals(maskedSubtree)) { 373 return true; 374 } 375 return maskedOID.startsWith(maskedSubtree); 376 } 377 378 380 public static final class SnmpNotifyTypeEnum { 381 public static final int trap = 1; 382 public static final int inform = 2; 383 } 384 public static final class SnmpNotifyFilterTypeEnum { 385 public static final int included = 1; 386 public static final int excluded = 2; 387 } 388 389 391 393 397 static class SnmpNotifyFilterMaskValidator implements MOValueValidationListener { 398 399 public void validate(MOValueValidationEvent validationEvent) { 400 Variable newValue = validationEvent.getNewValue(); 401 OctetString os = (OctetString)newValue; 402 if (!(((os.length() >= 0) && (os.length() <= 16)))) { 403 validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_LENGTH); 404 return; 405 } 406 } 409 } 410 411 414 } 417 418 419 | Popular Tags |