001/*
002 * Copyright 2009-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2019 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.StaticUtils;
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 a
044 * load-balancing algorithm used by the Directory Proxy Server.
045 * <BR>
046 * <BLOCKQUOTE>
047 *   <B>NOTE:</B>  This class, and other classes within the
048 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
049 *   supported for use against Ping Identity, UnboundID, and
050 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
051 *   for proprietary functionality or for external specifications that are not
052 *   considered stable or mature enough to be guaranteed to work in an
053 *   interoperable way with other types of LDAP servers.
054 * </BLOCKQUOTE>
055 * <BR>
056 * Information that it may make available includes:
057 * <UL>
058 *   <LI>The aggregate health check state for servers associated with the
059 *       load-balancing algorithm.</LI>
060 *   <LI>Information about each server associated with the load-balancing
061 *       algorithm, including the address, port, and health check state for the
062 *       server.</LI>
063 *   <LI>The number of available, degraded, and unavailable servers associated
064 *       with the load-balancing algorithm.</LI>
065 * </UL>
066 * The server should present a load-balancing algorithm monitor entry for each
067 * load-balancing algorithm used by a proxying request processor.  These entries
068 * can be retrieved using the
069 * {@link MonitorManager#getLoadBalancingAlgorithmMonitorEntries} method.  These
070 * entries provide specific methods for accessing this information.
071 * Alternately, the information may be accessed using the generic API.  See the
072 * {@link MonitorManager} class documentation for an example that demonstrates
073 * the use of the generic API for accessing monitor data.
074 */
075@NotMutable()
076@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
077public final class LoadBalancingAlgorithmMonitorEntry
078       extends MonitorEntry
079{
080  /**
081   * The structural object class used in LDAP external server monitor entries.
082   */
083  protected static final String LOAD_BALANCING_ALGORITHM_MONITOR_OC =
084       "ds-load-balancing-algorithm-monitor-entry";
085
086
087
088  /**
089   * The name of the attribute used to provide the name of the load-balancing
090   * algorithm.
091   */
092  private static final String ATTR_ALGORITHM_NAME = "algorithm-name";
093
094
095
096  /**
097   * The name of the attribute used to provide the DN of the configuration entry
098   * for the load-balancing algorithm.
099   */
100  private static final String ATTR_CONFIG_ENTRY_DN = "config-entry-dn";
101
102
103
104  /**
105   * The name of the attribute used to provide the aggregate health check state
106   * for the load-balancing algorithm.
107   */
108  private static final String ATTR_HEALTH_CHECK_STATE = "health-check-state";
109
110
111
112  /**
113   * The name of the attribute used to provide information about the health
114   * check states of each of the LDAP external servers associated with the
115   * load-balancing algorithm.
116   */
117  private static final String ATTR_LDAP_EXTERNAL_SERVER =
118       "ldap-external-server";
119
120
121
122  /**
123   * The name of the attribute used to provide the aggregate health check state
124   * for local servers for the load-balancing algorithm.
125   */
126  private static final String ATTR_LOCAL_SERVERS_HEALTH_CHECK_STATE =
127       "local-servers-health-check-state";
128
129
130
131  /**
132   * The name of the attribute used to provide the aggregate health check state
133   * for non-local servers for the load-balancing algorithm.
134   */
135  private static final String ATTR_NON_LOCAL_SERVERS_HEALTH_CHECK_STATE =
136       "non-local-servers-health-check-state";
137
138
139
140  /**
141   * The name of the attribute used to provide the number of servers associated
142   * with the load-balancing algorithm with a health check state of AVAILABLE.
143   */
144  private static final String ATTR_NUM_AVAILABLE = "num-available-servers";
145
146
147
148  /**
149   * The name of the attribute used to provide the number of servers associated
150   * with the load-balancing algorithm with a health check state of DEGRADED.
151   */
152  private static final String ATTR_NUM_DEGRADED = "num-degraded-servers";
153
154
155
156  /**
157   * The name of the attribute used to provide the number of servers associated
158   * with the load-balancing algorithm with a health check state of UNAVAILABLE.
159   */
160  private static final String ATTR_NUM_UNAVAILABLE = "num-unavailable-servers";
161
162
163
164  /**
165   * The serial version UID for this serializable class.
166   */
167  private static final long serialVersionUID = -5251924301718025205L;
168
169
170
171  // The aggregate health check state for the load-balancing algorithm.
172  private final HealthCheckState healthCheckState;
173
174  // The aggregate health check state for local servers for the load-balancing
175  // algorithm.
176  private final HealthCheckState localServersHealthCheckState;
177
178  // The aggregate health check state for non-local servers for the
179  // load-balancing algorithm.
180  private final HealthCheckState nonLocalServersHealthCheckState;
181
182  // The list of server availability objects.
183  private final List<LoadBalancingAlgorithmServerAvailabilityData>
184       serverAvailabilityData;
185
186  // The number of servers with a health check state of AVAILABLE.
187  private final Long numAvailableServers;
188
189  // The number of servers with a health check state of DEGRADED.
190  private final Long numDegradedServers;
191
192  // The number of servers with a health check state of UNAVAILABLE.
193  private final Long numUnavailableServers;
194
195  // The name of the load-balancing algorithm.
196  private final String algorithmName;
197
198  // The DN of the configuration entry for the load-balancing algorithm.
199  private final String configEntryDN;
200
201
202
203  /**
204   * Creates a new load-balancing algorithm monitor entry from the provided
205   * entry.
206   *
207   * @param  entry  The entry to be parsed as a load-balancing algorithm monitor
208   *                entry.  It must not be {@code null}.
209   */
210  public LoadBalancingAlgorithmMonitorEntry(final Entry entry)
211  {
212    super(entry);
213
214    algorithmName = getString(ATTR_ALGORITHM_NAME);
215    configEntryDN = getString(ATTR_CONFIG_ENTRY_DN);
216    numAvailableServers = getLong(ATTR_NUM_AVAILABLE);
217    numDegradedServers = getLong(ATTR_NUM_DEGRADED);
218    numUnavailableServers = getLong(ATTR_NUM_UNAVAILABLE);
219
220    final String hcStateStr = getString(ATTR_HEALTH_CHECK_STATE);
221    if (hcStateStr == null)
222    {
223      healthCheckState = null;
224    }
225    else
226    {
227      healthCheckState = HealthCheckState.forName(hcStateStr);
228    }
229
230    final String localHCStateStr =
231         getString(ATTR_LOCAL_SERVERS_HEALTH_CHECK_STATE);
232    if (localHCStateStr == null)
233    {
234      localServersHealthCheckState = null;
235    }
236    else
237    {
238      localServersHealthCheckState = HealthCheckState.forName(localHCStateStr);
239    }
240
241    final String nonLocalHCStateStr =
242         getString(ATTR_NON_LOCAL_SERVERS_HEALTH_CHECK_STATE);
243    if (nonLocalHCStateStr == null)
244    {
245      nonLocalServersHealthCheckState = null;
246    }
247    else
248    {
249      nonLocalServersHealthCheckState =
250           HealthCheckState.forName(nonLocalHCStateStr);
251    }
252
253    final List<String> externalServerStrings =
254         getStrings(ATTR_LDAP_EXTERNAL_SERVER);
255    final ArrayList<LoadBalancingAlgorithmServerAvailabilityData> serverData =
256         new ArrayList<>(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<>(StaticUtils.computeMapCapacity(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<>(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}