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