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.Entry;
032import com.unboundid.util.Debug;
033import com.unboundid.util.NotMutable;
034import com.unboundid.util.ThreadSafety;
035import com.unboundid.util.ThreadSafetyLevel;
036
037import static com.unboundid.ldap.sdk.unboundidds.monitors.MonitorMessages.*;
038
039
040
041/**
042 * This class defines a monitor entry that provides information about a
043 * load-balancing algorithm used by the Directory Proxy Server.
044 * <BR>
045 * <BLOCKQUOTE>
046 *   <B>NOTE:</B>  This class, and other classes within the
047 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
048 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
049 *   server products.  These classes provide support for proprietary
050 *   functionality or for external specifications that are not considered stable
051 *   or mature enough to be guaranteed to work in an interoperable way with
052 *   other types of LDAP servers.
053 * </BLOCKQUOTE>
054 * <BR>
055 * Information that it may make available includes:
056 * <UL>
057 *   <LI>The aggregate health check state for servers associated with the
058 *       load-balancing algorithm.</LI>
059 *   <LI>Information about each server associated with the load-balancing
060 *       algorithm, including the address, port, and health check state for the
061 *       server.</LI>
062 *   <LI>The number of available, degraded, and unavailable servers associated
063 *       with the load-balancing algorithm.</LI>
064 * </UL>
065 * The server should present a load-balancing algorithm monitor entry for each
066 * load-balancing algorithm used by a proxying request processor.  These entries
067 * can be retrieved using the
068 * {@link MonitorManager#getLoadBalancingAlgorithmMonitorEntries} method.  These
069 * entries provide specific methods for accessing this information.
070 * Alternately, the information may be accessed using the generic API.  See the
071 * {@link MonitorManager} class documentation for an example that demonstrates
072 * the use of the generic API for accessing monitor data.
073 */
074@NotMutable()
075@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
076public final class LoadBalancingAlgorithmMonitorEntry
077       extends MonitorEntry
078{
079  /**
080   * The structural object class used in LDAP external server monitor entries.
081   */
082  protected static final String LOAD_BALANCING_ALGORITHM_MONITOR_OC =
083       "ds-load-balancing-algorithm-monitor-entry";
084
085
086
087  /**
088   * The name of the attribute used to provide the name of the load-balancing
089   * algorithm.
090   */
091  private static final String ATTR_ALGORITHM_NAME = "algorithm-name";
092
093
094
095  /**
096   * The name of the attribute used to provide the DN of the configuration entry
097   * for the load-balancing algorithm.
098   */
099  private static final String ATTR_CONFIG_ENTRY_DN = "config-entry-dn";
100
101
102
103  /**
104   * The name of the attribute used to provide the aggregate health check state
105   * for the load-balancing algorithm.
106   */
107  private static final String ATTR_HEALTH_CHECK_STATE = "health-check-state";
108
109
110
111  /**
112   * The name of the attribute used to provide information about the health
113   * check states of each of the LDAP external servers associated with the
114   * load-balancing algorithm.
115   */
116  private static final String ATTR_LDAP_EXTERNAL_SERVER =
117       "ldap-external-server";
118
119
120
121  /**
122   * The name of the attribute used to provide the aggregate health check state
123   * for local servers for the load-balancing algorithm.
124   */
125  private static final String ATTR_LOCAL_SERVERS_HEALTH_CHECK_STATE =
126       "local-servers-health-check-state";
127
128
129
130  /**
131   * The name of the attribute used to provide the aggregate health check state
132   * for non-local servers for the load-balancing algorithm.
133   */
134  private static final String ATTR_NON_LOCAL_SERVERS_HEALTH_CHECK_STATE =
135       "non-local-servers-health-check-state";
136
137
138
139  /**
140   * The name of the attribute used to provide the number of servers associated
141   * with the load-balancing algorithm with a health check state of AVAILABLE.
142   */
143  private static final String ATTR_NUM_AVAILABLE = "num-available-servers";
144
145
146
147  /**
148   * The name of the attribute used to provide the number of servers associated
149   * with the load-balancing algorithm with a health check state of DEGRADED.
150   */
151  private static final String ATTR_NUM_DEGRADED = "num-degraded-servers";
152
153
154
155  /**
156   * The name of the attribute used to provide the number of servers associated
157   * with the load-balancing algorithm with a health check state of UNAVAILABLE.
158   */
159  private static final String ATTR_NUM_UNAVAILABLE = "num-unavailable-servers";
160
161
162
163  /**
164   * The serial version UID for this serializable class.
165   */
166  private static final long serialVersionUID = -5251924301718025205L;
167
168
169
170  // The aggregate health check state for the load-balancing algorithm.
171  private final HealthCheckState healthCheckState;
172
173  // The aggregate health check state for local servers for the load-balancing
174  // algorithm.
175  private final HealthCheckState localServersHealthCheckState;
176
177  // The aggregate health check state for non-local servers for the
178  // load-balancing algorithm.
179  private final HealthCheckState nonLocalServersHealthCheckState;
180
181  // The list of server availability objects.
182  private final List<LoadBalancingAlgorithmServerAvailabilityData>
183       serverAvailabilityData;
184
185  // The number of servers with a health check state of AVAILABLE.
186  private final Long numAvailableServers;
187
188  // The number of servers with a health check state of DEGRADED.
189  private final Long numDegradedServers;
190
191  // The number of servers with a health check state of UNAVAILABLE.
192  private final Long numUnavailableServers;
193
194  // The name of the load-balancing algorithm.
195  private final String algorithmName;
196
197  // The DN of the configuration entry for the load-balancing algorithm.
198  private final String configEntryDN;
199
200
201
202  /**
203   * Creates a new load-balancing algorithm monitor entry from the provided
204   * entry.
205   *
206   * @param  entry  The entry to be parsed as a load-balancing algorithm monitor
207   *                entry.  It must not be {@code null}.
208   */
209  public LoadBalancingAlgorithmMonitorEntry(final Entry entry)
210  {
211    super(entry);
212
213    algorithmName = getString(ATTR_ALGORITHM_NAME);
214    configEntryDN = getString(ATTR_CONFIG_ENTRY_DN);
215    numAvailableServers = getLong(ATTR_NUM_AVAILABLE);
216    numDegradedServers = getLong(ATTR_NUM_DEGRADED);
217    numUnavailableServers = getLong(ATTR_NUM_UNAVAILABLE);
218
219    final String hcStateStr = getString(ATTR_HEALTH_CHECK_STATE);
220    if (hcStateStr == null)
221    {
222      healthCheckState = null;
223    }
224    else
225    {
226      healthCheckState = HealthCheckState.forName(hcStateStr);
227    }
228
229    final String localHCStateStr =
230         getString(ATTR_LOCAL_SERVERS_HEALTH_CHECK_STATE);
231    if (localHCStateStr == null)
232    {
233      localServersHealthCheckState = null;
234    }
235    else
236    {
237      localServersHealthCheckState = HealthCheckState.forName(localHCStateStr);
238    }
239
240    final String nonLocalHCStateStr =
241         getString(ATTR_NON_LOCAL_SERVERS_HEALTH_CHECK_STATE);
242    if (nonLocalHCStateStr == null)
243    {
244      nonLocalServersHealthCheckState = null;
245    }
246    else
247    {
248      nonLocalServersHealthCheckState =
249           HealthCheckState.forName(nonLocalHCStateStr);
250    }
251
252    final List<String> externalServerStrings =
253         getStrings(ATTR_LDAP_EXTERNAL_SERVER);
254    final ArrayList<LoadBalancingAlgorithmServerAvailabilityData> serverData =
255         new ArrayList<LoadBalancingAlgorithmServerAvailabilityData>(
256              externalServerStrings.size());
257    for (final String s : externalServerStrings)
258    {
259      try
260      {
261        serverData.add(new LoadBalancingAlgorithmServerAvailabilityData(s));
262      }
263      catch (final Exception e)
264      {
265        Debug.debugException(e);
266      }
267    }
268    serverAvailabilityData = Collections.unmodifiableList(serverData);
269  }
270
271
272
273  /**
274   * Retrieves the name of the load-balancing algorithm.
275   *
276   * @return  The name of the load-balancing algorithm, or {@code null} if it
277   *          was not included in the monitor entry.
278   */
279  public String getAlgorithmName()
280  {
281    return algorithmName;
282  }
283
284
285
286  /**
287   * Retrieves the DN of the configuration entry for the load-balancing
288   * algorithm.
289   *
290   * @return  The DN of the configuration entry for the load-balancing
291   *          algorithm, or {@code null} if it was not included in the monitor
292   *          entry.
293   */
294  public String getConfigEntryDN()
295  {
296    return configEntryDN;
297  }
298
299
300
301  /**
302   * Retrieves the aggregate health check state for the load-balancing
303   * algorithm.
304   *
305   * @return  The aggregate health check state for the load-balancing algorithm,
306   *          or {@code null} if it was not included in the monitor
307   *          entry.
308   */
309  public HealthCheckState getHealthCheckState()
310  {
311    return healthCheckState;
312  }
313
314
315
316  /**
317   * Retrieves the aggregate health check state for local servers for the
318   * load-balancing algorithm.
319   *
320   * @return  The aggregate health check state for local servers for the
321   *          load-balancing algorithm, or {@code null} if it was not included
322   *          in the monitor entry.
323   */
324  public HealthCheckState getLocalServersHealthCheckState()
325  {
326    return localServersHealthCheckState;
327  }
328
329
330
331  /**
332   * Retrieves the aggregate health check state for non-local servers for the
333   * load-balancing algorithm.
334   *
335   * @return  The aggregate health check state for non-local servers for the
336   *          load-balancing algorithm, or {@code null} if it was not included
337   *          in the monitor entry.
338   */
339  public HealthCheckState getNonLocalServersHealthCheckState()
340  {
341    return nonLocalServersHealthCheckState;
342  }
343
344
345
346  /**
347   * Retrieves a list with information about the healths of the individual LDAP
348   * external servers associated with the load-balancing algorithm.
349   *
350   * @return  A list with information about the healths of the individual LDAP
351   *          external servers associated with the load-balancing algorithm, or
352   *          an empty list if it was not included in the monitor entry.
353   */
354  public List<LoadBalancingAlgorithmServerAvailabilityData>
355              getServerAvailabilityData()
356  {
357    return serverAvailabilityData;
358  }
359
360
361
362  /**
363   * Retrieves the number of servers associated with the load-balancing
364   * algorithm that have a health check state of AVAILABLE.
365   *
366   * @return  The number of servers associated with the load-balancing algorithm
367   *          that have a health check state of AVAILABLE, or {@code null} if it
368   *          was not included in the monitor entry.
369   */
370  public Long getNumAvailableServers()
371  {
372    return numAvailableServers;
373  }
374
375
376
377  /**
378   * Retrieves the number of servers associated with the load-balancing
379   * algorithm that have a health check state of DEGRADED.
380   *
381   * @return  The number of servers associated with the load-balancing algorithm
382   *          that have a health check state of DEGRADED, or {@code null} if it
383   *          was not included in the monitor entry.
384   */
385  public Long getNumDegradedServers()
386  {
387    return numDegradedServers;
388  }
389
390
391
392  /**
393   * Retrieves the number of servers associated with the load-balancing
394   * algorithm that have a health check state of UNAVAILABLE.
395   *
396   * @return  The number of servers associated with the load-balancing algorithm
397   *          that have a health check state of UNAVAILABLE, or {@code null} if
398   *          it was not included in the monitor entry.
399   */
400  public Long getNumUnavailableServers()
401  {
402    return numUnavailableServers;
403  }
404
405
406
407  /**
408   * {@inheritDoc}
409   */
410  @Override()
411  public String getMonitorDisplayName()
412  {
413    return INFO_LOAD_BALANCING_ALGORITHM_MONITOR_DISPNAME.get();
414  }
415
416
417
418  /**
419   * {@inheritDoc}
420   */
421  @Override()
422  public String getMonitorDescription()
423  {
424    return INFO_LOAD_BALANCING_ALGORITHM_MONITOR_DESC.get();
425  }
426
427
428
429  /**
430   * {@inheritDoc}
431   */
432  @Override()
433  public Map<String,MonitorAttribute> getMonitorAttributes()
434  {
435    final LinkedHashMap<String,MonitorAttribute> attrs =
436         new LinkedHashMap<String,MonitorAttribute>(9);
437
438    if (algorithmName != null)
439    {
440      addMonitorAttribute(attrs,
441           ATTR_ALGORITHM_NAME,
442           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_ALGORITHM_NAME.get(),
443           INFO_LOAD_BALANCING_ALGORITHM_DESC_ALGORITHM_NAME.get(),
444           algorithmName);
445    }
446
447    if (configEntryDN != null)
448    {
449      addMonitorAttribute(attrs,
450           ATTR_CONFIG_ENTRY_DN,
451           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_CONFIG_ENTRY_DN.get(),
452           INFO_LOAD_BALANCING_ALGORITHM_DESC_CONFIG_ENTRY_DN.get(),
453           configEntryDN);
454    }
455
456    if (healthCheckState != null)
457    {
458      addMonitorAttribute(attrs,
459           ATTR_HEALTH_CHECK_STATE,
460           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_HEALTH_CHECK_STATE.get(),
461           INFO_LOAD_BALANCING_ALGORITHM_DESC_HEALTH_CHECK_STATE.get(),
462           healthCheckState.name());
463    }
464
465    if (localServersHealthCheckState != null)
466    {
467      addMonitorAttribute(attrs,
468           ATTR_LOCAL_SERVERS_HEALTH_CHECK_STATE,
469           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_L_HEALTH_CHECK_STATE.get(),
470           INFO_LOAD_BALANCING_ALGORITHM_DESC_L_HEALTH_CHECK_STATE.get(),
471           localServersHealthCheckState.name());
472    }
473
474    if (nonLocalServersHealthCheckState != null)
475    {
476      addMonitorAttribute(attrs,
477           ATTR_NON_LOCAL_SERVERS_HEALTH_CHECK_STATE,
478           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_NL_HEALTH_CHECK_STATE.get(),
479           INFO_LOAD_BALANCING_ALGORITHM_DESC_NL_HEALTH_CHECK_STATE.get(),
480           nonLocalServersHealthCheckState.name());
481    }
482
483    if ((serverAvailabilityData != null) &&
484        (! serverAvailabilityData.isEmpty()))
485    {
486      final ArrayList<String> availabilityStrings =
487           new ArrayList<String>(serverAvailabilityData.size());
488      for (final LoadBalancingAlgorithmServerAvailabilityData d :
489           serverAvailabilityData)
490      {
491        availabilityStrings.add(d.toCompactString());
492      }
493      addMonitorAttribute(attrs,
494           ATTR_LDAP_EXTERNAL_SERVER,
495           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_SERVER_DATA.get(),
496           INFO_LOAD_BALANCING_ALGORITHM_DESC_SERVER_DATA.get(),
497           availabilityStrings);
498    }
499
500    if (numAvailableServers != null)
501    {
502      addMonitorAttribute(attrs,
503           ATTR_NUM_AVAILABLE,
504           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_NUM_AVAILABLE.get(),
505           INFO_LOAD_BALANCING_ALGORITHM_DESC_NUM_AVAILABLE.get(),
506           numAvailableServers);
507    }
508
509    if (numDegradedServers != null)
510    {
511      addMonitorAttribute(attrs,
512           ATTR_NUM_DEGRADED,
513           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_NUM_DEGRADED.get(),
514           INFO_LOAD_BALANCING_ALGORITHM_DESC_NUM_DEGRADED.get(),
515           numDegradedServers);
516    }
517
518    if (numUnavailableServers != null)
519    {
520      addMonitorAttribute(attrs,
521           ATTR_NUM_UNAVAILABLE,
522           INFO_LOAD_BALANCING_ALGORITHM_DISPNAME_NUM_UNAVAILABLE.get(),
523           INFO_LOAD_BALANCING_ALGORITHM_DESC_NUM_UNAVAILABLE.get(),
524           numUnavailableServers);
525    }
526
527    return Collections.unmodifiableMap(attrs);
528  }
529}