mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
03.58.2016 ccd66a6d38f7d3a55a4fddd2945d8ab8920b007e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2009 Sun Microsystems, Inc.
 * Portions Copyright 2012-2014 ForgeRock AS.
 */
package org.opends.server.snmp;
 
 
import javax.management.MBeanServer;
 
import com.sun.management.snmp.agent.SnmpMib;
 
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
 
import javax.management.MBeanServerNotification;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
 
import org.forgerock.i18n.slf4j.LocalizedLogger;
 
/**
 * The class represents the "DsMIB" group implementation.
 * The group is defined with the following oid: 1.3.6.1.2.1.66.
 */
public class DsMIBImpl extends DsMIB implements NotificationListener {
 
   /**
   * The serial version identifier required to satisfy the compiler because
   * this class implements the <CODE>java.io.Serializable</CODE> interface.
   * This value was generated using the <CODE>serialver</CODE> command-line
   * utility included with the Java SDK.
   */
  private static final long serialVersionUID = 6787374593664749374L;
 
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  /**
   * Directory Server MIB access.
   */
  private SnmpMib mib;
 
  /**
   * Register or not the SNMP MBean.
   */
  private boolean registeredSnmpMBean = false;
 
  /**
   * List of DsTableEntries.
   * SNMP dsTable contains the list of Directory Servers
   */
  private Hashtable<ObjectName, DsEntry> dsTableEntries =
          new Hashtable<ObjectName, DsEntry>();
 
  /**
   * List of DsApplIfOpsTableEntries.
   * SNMP dsAppIfOpsTable provides summary statistics on the accesses
   * operations and errors for each application protocol interface
   * of a Directory Server
   */
  private Hashtable<ObjectName, DsEntry> dsApplIfOpsTableEntries =
          new Hashtable<ObjectName, DsEntry>();
 
  /**
   * List of DsIntTableEntries.
   * SNMP dsIntTable provides some useful information on the
   * interactions of the monitored DS with peer DS
   */
  private Hashtable<ObjectName, DsEntry> dsIntTableEntries =
          new Hashtable<ObjectName, DsEntry>();
 
  /**
   * Directory Server MBeanServer.
   */
  private MBeanServer server;
 
  /**
   * cn=monitor Mapping Class SNMP->MBean.
   */
  private SNMPMonitor monitor;
 
  /**
   * Start Table indexes.
   */
  private int applIndex = 1;
  private int applIfOpsIndex = 1;
  private int intIndex = 1;
 
  /**
   * Constructor for the "DsMIB" group.
   * If the group contains a table, the entries created through an SNMP SET
   * will not be registered in Java DMK.
   * @param myMib snmp mib
   * @param server where the MBeans are registered
   * @param registered if the MBeans should be registered in server
   */
  public DsMIBImpl(SnmpMib myMib, MBeanServer server, boolean registered) {
    super(myMib);
    this.mib = myMib;
    this.server = server;
    this.monitor = SNMPMonitor.getMonitor(server);
    this.registeredSnmpMBean = registered;
    this.dsTableEntries.clear();
    this.dsApplIfOpsTableEntries.clear();
    this.dsIntTableEntries.clear();
    // Initialize the MIB
    initDsTables();
 
    logger.trace("DsMIB Group Created");
  }
 
  /**
   * Returns the Set of ObjectName of all the created entries in all the Table.
   * @return The Set of ObjectName
   */
  @SuppressWarnings("unchecked")
  public Set<ObjectName> getEntriesObjectNames() {
    Set<ObjectName> results = new HashSet<ObjectName>();
    results.addAll(this.dsTableEntries.keySet());
    results.addAll(this.dsApplIfOpsTableEntries.keySet());
    results.addAll(this.dsIntTableEntries.keySet());
    return results;
  }
 
  /**
   * Returns the list of Created Entries.
   * @return Set of created entries
   */
  @SuppressWarnings("unchecked")
  public Set<ObjectName> getEntries() {
    Set results = new HashSet();
    results.addAll(this.dsTableEntries.values());
    results.addAll(this.dsApplIfOpsTableEntries.values());
    results.addAll(this.dsIntTableEntries.values());
    return results;
  }
 
  /**
   * handleNotification callback called when an MBeansServer Notification is
   * received.
   * @param notification received
   * @param handback The handback
   */
  public void handleNotification(Notification notification, Object handback) {
    if (notification instanceof MBeanServerNotification) {
      MBeanServerNotification notif = (MBeanServerNotification) notification;
      // Process the ConnectionHandler Registration
      if (notif.getType().equals(
              MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
        ObjectName name = notif.getMBeanName();
        if ((name.getKeyProperty("Rdn1").equals("cn-monitor")) &&
                (isAConnectionHandler(name))) {
          addRowInDsApplIfOpsTable(name);
        }
      }
      // Process the ConnectionHandler unregistration
      else if (notif.getType().equals(
              MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
        ObjectName name = notif.getMBeanName();
        if ((name.getKeyProperty("Rdn1").equals("cn-monitor")) &&
                (isAConnectionHandler(name))) {
          removeRowInDsApplIfOpsTable(name);
        }
      }
    }
  }
 
  /**
   * initDsTables in the dsMib Group.
   */
  private void initDsTables() {
 
    // Initialize the DSTable with one Entry : Current Directory Server
    initializeDsTable();
 
    // Initialize the DsApplIfOpsTable with one entry per Connection Handler.
    initializeDsApplIfOpsTable();
 
    // Register as listener of the MBeanServer Notification to process
    // new Connection Handler MBeans
    try {
      ObjectName name = new ObjectName(
              "JMImplementation:type=MBeanServerDelegate");
      this.server.addNotificationListener(name, this, null, null);
    } catch (Exception ex) {
        logger.traceException(ex);
    }
  }
 
  /**
   * initializeDsTable with the current Directory server instance.
   * Only one entry is created in this table
   **/
  private void initializeDsTable() {
    // Add the Current Directory Server Instance in the DsTable
    addRowInDsTable();
  }
 
  /**
   * initializeDsApplIfOpsTable with the already registered Connection
   * Handlers.
   **/
  private void initializeDsApplIfOpsTable() {
    // Get the list of ConnectionHandlers MBeans
    Set connectionHandlers = this.monitor.getConnectionHandlers();
    for (Iterator iter=connectionHandlers.iterator();iter.hasNext();) {
      ObjectName name = (ObjectName)iter.next();
      // Add the ConnectionHandler in the DsApplIfOpsTable
      addRowInDsApplIfOpsTable(name);
    }
  }
 
 /**
  * addRowInDsTable.
  * @return true if the entry has been added else false
  */
  private boolean addRowInDsTable() {
 
    try {
 
      // Create the entry
      DsTableEntryImpl entry = new DsTableEntryImpl(
              this.mib, this.server, this.applIndex);
 
      // if the entry already exists nothing to do
      if ((this.dsTableEntries.containsKey(entry.getObjectName())) ||
              (entry == null)) {
        return true;
      }
 
      // Add the entry in the table
      this.DsTable.addEntry(entry, entry.getObjectName());
      this.dsTableEntries.put(entry.getObjectName(), entry);
      if (this.registeredSnmpMBean) {
        // Register the SNMP OID MBean
        this.server.registerMBean(entry, entry.getObjectName());
      }
    } catch (Exception ex) {
        logger.traceException(ex);
        return false;
    }
    return true;
  }
 
  /**
   * addRowInDsIntTable Not Supported.
   * @return false (Not Supported for this current delivery)
   */
  private boolean addRowInDsIntTable() {
    return false;
  }
 
  /**
   * addRowInDsApplIfOpsTable.
   * @param connectionHandlerName to add
   * @return true if the entry has been added else false
   */
  private boolean addRowInDsApplIfOpsTable(ObjectName connectionHandlerName) {
    try {
 
      // Created the entry
      DsApplIfOpsEntryImpl entry = new DsApplIfOpsEntryImpl(
              this.mib, this.server, connectionHandlerName,
              this.applIndex, this.applIfOpsIndex);
 
      // If the entry already exists then nothing to do
      if ((this.dsApplIfOpsTableEntries.containsKey(entry.getObjectName())) ||
              (entry == null)) {
        return true;
      }
      // Add the entry in the Table
      this.DsApplIfOpsTable.addEntry(entry);
      this.dsApplIfOpsTableEntries.put(entry.getObjectName(), entry);
      this.applIfOpsIndex++;
      if (this.registeredSnmpMBean) {
        // Register the SNMP OID MBean in the MBeanServer
        this.server.registerMBean(entry, entry.getObjectName());
      }
    } catch (Exception ex) {
        logger.traceException(ex);
        return false;
    }
    return true;
  }
 
  /**
   * removeRowInDsApplIfOpsTable.
   * @param connectionHandlerName
   * @return true if the entry has been removed else false
   */
  private boolean removeRowInDsApplIfOpsTable(ObjectName connectionHandlerName){
    try {
      // Check if the entry is known
      if (!this.dsApplIfOpsTableEntries.containsKey(connectionHandlerName)) {
        return false;
      }
      DsApplIfOpsEntryImpl entry = (DsApplIfOpsEntryImpl)
              this.dsApplIfOpsTableEntries.get(connectionHandlerName);
 
      this.DsApplIfOpsTable.removeEntry(entry);
      this.dsApplIfOpsTableEntries.remove(connectionHandlerName);
      this.server.unregisterMBean(entry.getObjectName());
      return true;
    } catch (Exception ex) {
      return false;
    }
  }
 
 
/**
 * isAConnectionHandler allows to check if the Mbean is a Connection Handler.
 * @param name of the MBean
 * @return true if the MBean is a Connection Handler else false
 */
  private boolean isAConnectionHandler(ObjectName name) {
    // First level
    String Rdn2 = name.getKeyProperty("Rdn2");
    // Second level
    String Rdn3 = name.getKeyProperty("Rdn3");
    if ((Rdn3==null) || (Rdn3.length()==0)) {
      if ((Rdn2.contains("Connection_Handler")) &&
            (!(Rdn2.endsWith("_Statistics")))) {
         return true;
      }
    }
    return false;
  }
}