001/*
002 * Copyright 2009-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.unboundidds.monitors;
022
023
024
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.LinkedHashMap;
028import java.util.List;
029import java.util.Map;
030
031import com.unboundid.ldap.sdk.DN;
032import com.unboundid.ldap.sdk.Entry;
033import com.unboundid.util.Debug;
034import com.unboundid.util.NotMutable;
035import com.unboundid.util.ThreadSafety;
036import com.unboundid.util.ThreadSafetyLevel;
037
038import static com.unboundid.ldap.sdk.unboundidds.monitors.MonitorMessages.*;
039
040
041
042/**
043 * This class defines a monitor entry that provides information about the state
044 * of a replication server, including the base DNs for replicated content, the
045 * generation ID for each of those base DNs, the replication server ID, and the
046 * port number on which the replication server is listening.
047 * <BR>
048 * <BLOCKQUOTE>
049 *   <B>NOTE:</B>  This class, and other classes within the
050 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
051 *   supported for use against Ping Identity, UnboundID, and
052 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
053 *   for proprietary functionality or for external specifications that are not
054 *   considered stable or mature enough to be guaranteed to work in an
055 *   interoperable way with other types of LDAP servers.
056 * </BLOCKQUOTE>
057 * <BR>
058 * The server should present at most one replication server monitor entry.  It
059 * can be retrieved using the
060 * {@link MonitorManager#getReplicationServerMonitorEntry} method.  This entry
061 * provides specific methods for accessing information about the replication
062 * server.  Alternately, this information may be accessed using the generic API.
063 * See the {@link MonitorManager} class documentation for an example that
064 * demonstrates the use of the generic API for accessing monitor data.
065 */
066@NotMutable()
067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
068public final class ReplicationServerMonitorEntry
069       extends MonitorEntry
070{
071  /**
072   * The structural object class used in replication server monitor entries.
073   */
074  static final String REPLICATION_SERVER_MONITOR_OC =
075       "ds-replication-server-monitor-entry";
076
077
078
079  /**
080   * The name of the attribute that contains the base DNs for the replicated
081   * data.
082   */
083  private static final String ATTR_BASE_DN = "base-dn";
084
085
086
087  /**
088   * The name of the attribute that contains the generation IDs that correspond
089   * to the replicated base DNs.
090   */
091  private static final String ATTR_BASE_DN_GENERATION_ID =
092       "base-dn-generation-id";
093
094
095
096  /**
097   * The name of the attribute that contains the server ID for the replication
098   * server.
099   */
100  private static final String ATTR_REPLICATION_SERVER_ID =
101       "replication-server-id";
102
103
104
105  /**
106   * The name of the attribute that contains the port number on which the
107   * replication server listens for communication from other servers.
108   */
109  private static final String ATTR_REPLICATION_SERVER_PORT =
110       "replication-server-port";
111
112
113
114  /**
115   * The name of the attribute that indicates whether SSL encryption is
116   * available for use.
117   */
118  private static final String ATTR_SSL_AVAILABLE =
119       "ssl-encryption-available";
120
121
122
123  /**
124   * The serial version UID for this serializable class.
125   */
126  private static final long serialVersionUID = 7488640967498574690L;
127
128
129
130  // Indicates whether SSL encryption is available.
131  private final Boolean sslEncryptionAvailable;
132
133  // The base DNs for the replicated data.
134  private final List<String> baseDNs;
135
136  // The port number on which the replication server listens for communication
137  // from other servers.
138  private final Long replicationServerPort;
139
140  // A map of the generation IDs for each of the replicated base DNs.
141  private final Map<DN,String> generationIDs;
142
143  // The replication server ID for the replication server.
144  private final String replicationServerID;
145
146
147
148  /**
149   * Creates a new replication server monitor entry from the provided entry.
150   *
151   * @param  entry  The entry to be parsed as a replication server monitor
152   *                entry.  It must not be {@code null}.
153   */
154  public ReplicationServerMonitorEntry(final Entry entry)
155  {
156    super(entry);
157
158    baseDNs                = getStrings(ATTR_BASE_DN);
159    replicationServerID    = getString(ATTR_REPLICATION_SERVER_ID);
160    replicationServerPort  = getLong(ATTR_REPLICATION_SERVER_PORT);
161    sslEncryptionAvailable = getBoolean(ATTR_SSL_AVAILABLE);
162
163    final List<String> baseDNsAndIDs = getStrings(ATTR_BASE_DN_GENERATION_ID);
164    final Map<DN,String> idMap = new LinkedHashMap<>(baseDNsAndIDs.size());
165    for (final String s : baseDNsAndIDs)
166    {
167      try
168      {
169        final int lastSpacePos = s.lastIndexOf(' ');
170        final DN dn = new DN(s.substring(0, lastSpacePos));
171        idMap.put(dn, s.substring(lastSpacePos+1));
172      }
173      catch (final Exception e)
174      {
175        Debug.debugException(e);
176      }
177    }
178    generationIDs = Collections.unmodifiableMap(idMap);
179  }
180
181
182
183  /**
184   * Retrieves the base DNs for replicated content managed by this replication
185   * server.
186   *
187   * @return  The base DNs for replicated content managed by this replication
188   *          server, or an empty list if it was not included in the monitor
189   *          entry.
190   */
191  public List<String> getBaseDNs()
192  {
193    return baseDNs;
194  }
195
196
197
198  /**
199   * Retrieves a map of generation IDs for the available base DNs.
200   *
201   * @return  A map of generation IDs for the available base DNs, or an empty
202   *          map if it was not included in the monitor entry.
203   */
204  public Map<DN,String> getGenerationIDs()
205  {
206    return generationIDs;
207  }
208
209
210
211  /**
212   * Retrieves the generation ID for the specified base DN.
213   *
214   * @param  baseDN  The base DN for which to retrieve the generation ID.
215   *
216   * @return  The generation ID for the specified base DN, or {@code null} if
217   *          there no generation ID is available for the provided base DN, or
218   *          the provided base DN is not a valid DN.
219   */
220  public String getGenerationID(final String baseDN)
221  {
222    try
223    {
224      return getGenerationID(new DN(baseDN));
225    }
226    catch (final Exception e)
227    {
228      Debug.debugException(e);
229      return null;
230    }
231  }
232
233
234
235  /**
236   * Retrieves the generation ID for the specified base DN.
237   *
238   * @param  baseDN  The base DN for which to retrieve the generation ID.
239   *
240   * @return  The generation ID for the specified base DN, or {@code null} if
241   *          there no generation ID is available for the provided base DN.
242   */
243  public String getGenerationID(final DN baseDN)
244  {
245    return generationIDs.get(baseDN);
246  }
247
248
249
250  /**
251   * Retrieves the server ID for the replication server.
252   *
253   * @return  The server ID for the replication server, or {@code null} if it
254   *          was not included in the monitor entry.
255   */
256  public String getReplicationServerID()
257  {
258    return replicationServerID;
259  }
260
261
262
263  /**
264   * Retrieves the port number for the replication server.
265   *
266   * @return  The port number for the replication server, or {@code null} if it
267   *          was not included in the monitor entry.
268   */
269  public Long getReplicationServerPort()
270  {
271    return replicationServerPort;
272  }
273
274
275
276  /**
277   * Indicates whether the replication server provides support for SSL
278   * encryption.
279   *
280   * @return  {@code true} if the replication server supports SSL encryption,
281   *          {@code false} if it does not, or {@code null} if that information
282   *          was not included in the monitor entry.
283   */
284  public Boolean sslEncryptionAvailable()
285  {
286    return sslEncryptionAvailable;
287  }
288
289
290
291  /**
292   * {@inheritDoc}
293   */
294  @Override()
295  public String getMonitorDisplayName()
296  {
297    return INFO_REPLICATION_SERVER_MONITOR_DISPNAME.get();
298  }
299
300
301
302  /**
303   * {@inheritDoc}
304   */
305  @Override()
306  public String getMonitorDescription()
307  {
308    return INFO_REPLICATION_SERVER_MONITOR_DESC.get();
309  }
310
311
312
313  /**
314   * {@inheritDoc}
315   */
316  @Override()
317  public Map<String,MonitorAttribute> getMonitorAttributes()
318  {
319    final LinkedHashMap<String,MonitorAttribute> attrs =
320         new LinkedHashMap<>(10);
321
322    if (! baseDNs.isEmpty())
323    {
324      addMonitorAttribute(attrs,
325           ATTR_BASE_DN,
326           INFO_REPLICATION_SERVER_DISPNAME_BASE_DN.get(),
327           INFO_REPLICATION_SERVER_DESC_BASE_DN.get(),
328           baseDNs);
329    }
330
331    if (! generationIDs.isEmpty())
332    {
333      final ArrayList<String> idStrings =
334           new ArrayList<>(generationIDs.size());
335      for (final Map.Entry<DN,String> e : generationIDs.entrySet())
336      {
337        idStrings.add(e.getKey().toNormalizedString() + ' ' + e.getValue());
338      }
339
340      addMonitorAttribute(attrs,
341           ATTR_BASE_DN_GENERATION_ID,
342           INFO_REPLICATION_SERVER_DISPNAME_BASE_DN_GENERATION_ID.get(),
343           INFO_REPLICATION_SERVER_DESC_BASE_DN_GENERATION_ID.get(),
344           idStrings);
345    }
346
347    if (replicationServerID != null)
348    {
349      addMonitorAttribute(attrs,
350           ATTR_REPLICATION_SERVER_ID,
351           INFO_REPLICATION_SERVER_DISPNAME_REPLICATION_SERVER_ID.get(),
352           INFO_REPLICATION_SERVER_DESC_REPLICATION_SERVER_ID.get(),
353           replicationServerID);
354    }
355
356    if (replicationServerPort != null)
357    {
358      addMonitorAttribute(attrs,
359           ATTR_REPLICATION_SERVER_PORT,
360           INFO_REPLICATION_SERVER_DISPNAME_REPLICATION_SERVER_PORT.get(),
361           INFO_REPLICATION_SERVER_DESC_REPLICATION_SERVER_PORT.get(),
362           replicationServerPort);
363    }
364
365    if (sslEncryptionAvailable != null)
366    {
367      addMonitorAttribute(attrs,
368           ATTR_SSL_AVAILABLE,
369           INFO_REPLICATION_SERVER_DISPNAME_SSL_AVAILABLE.get(),
370           INFO_REPLICATION_SERVER_DESC_SSL_AVAILABLE.get(),
371           sslEncryptionAvailable);
372    }
373
374    return Collections.unmodifiableMap(attrs);
375  }
376}