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