KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > snmp > SnmpNotificationMIB


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - SnmpNotificationMIB.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.agent.mo.snmp;
23
24 //--AgentGen BEGIN=_BEGIN
25
//--AgentGen END
26

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 JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.TreeMap JavaDoc;
35 import org.snmp4j.log.LogAdapter;
36 import org.snmp4j.log.LogFactory;
37
38 //--AgentGen BEGIN=_IMPORT
39
//--AgentGen END
40

41 public class SnmpNotificationMIB implements MOGroup {
42
43   private static final LogAdapter logger =
44       LogFactory.getLogger(SnmpNotificationMIB.class);
45
46   // Constants
47

48   private static final OID oidSnmpNotifyEntry =
49     new OID(new int[] { 1,3,6,1,6,3,13,1,1,1 });
50
51   // Column sub-identifer defintions for snmpNotifyEntry:
52
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   // Column index defintions for snmpNotifyEntry:
58
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      //--AgentGen BEGIN=snmpNotifyEntry::isValidIndex
74
//--AgentGen END
75
}
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   // Column sub-identifer defintions for snmpNotifyFilterEntry:
87
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   // Column index defintions for snmpNotifyFilterEntry:
93
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      //--AgentGen BEGIN=snmpNotifyFilterEntry::isValidIndex
109
//--AgentGen END
110
}
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   // Column sub-identifer defintions for snmpNotifyFilterProfileEntry:
122
private static final int colSnmpNotifyFilterProfileName = 1;
123   private static final int colSnmpNotifyFilterProfileStorType = 2;
124   private static final int colSnmpNotifyFilterProfileRowStatus = 3;
125
126   // Column index defintions for snmpNotifyFilterProfileEntry:
127
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      //--AgentGen BEGIN=snmpNotifyFilterProfileEntry::isValidIndex
141
//--AgentGen END
142
}
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     // Scalar Objects
250
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     // Scalar Objects
257
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 JavaDoc profiles =
293         snmpNotifyFilterProfileEntryModel.getRows(paramsIndex, paramsIndex,
294                                                   activeFilter);
295     if (profiles.size() == 0) {
296       // no profile -> passes filter
297
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 JavaDoc filters =
305         snmpNotifyFilterEntryModel.getRows(profileNameOID,
306                                            profileNameOIDNext,
307                                            activeFilter);
308     if (filters.size() == 0) {
309       // no filters -> no trap is sent
310
return false;
311     }
312     TreeMap JavaDoc oidMatches = new TreeMap JavaDoc();
313     TreeMap JavaDoc[] vbMatches = new TreeMap JavaDoc[vbs.length];
314     for (Iterator JavaDoc 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       // filter VBs
328
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 JavaDoc();
332           }
333           vbMatches[i].put(subtreeWithLength, type);
334         }
335       }
336     }
337     if (oidMatches.size() == 0) {
338       // no matches against notification ID
339
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   // Enumerations
379

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   // Scalars
390

391   // Value Validators
392

393   /**
394    * The <code>SnmpNotifyFilterMaskValidator</code> implements the value validation
395    * for <code>SnmpNotifyFilterMask</code>.
396    */

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      //--AgentGen BEGIN=snmpNotifyFilterMask::validate
407
//--AgentGen END
408
}
409   }
410
411 //--AgentGen BEGIN=_CLASSES
412
//--AgentGen END
413

414 //--AgentGen BEGIN=_END
415
//--AgentGen END
416
}
417
418
419
Popular Tags