001/*
002 * Copyright 2010-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2010-2024 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) 2010-2024 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.tasks;
037
038
039
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.Date;
043import java.util.LinkedHashMap;
044import java.util.LinkedList;
045import java.util.List;
046import java.util.Map;
047
048import com.unboundid.ldap.sdk.Attribute;
049import com.unboundid.ldap.sdk.Entry;
050import com.unboundid.util.NotMutable;
051import com.unboundid.util.NotNull;
052import com.unboundid.util.Nullable;
053import com.unboundid.util.StaticUtils;
054import com.unboundid.util.ThreadSafety;
055import com.unboundid.util.ThreadSafetyLevel;
056import com.unboundid.util.Validator;
057
058import static com.unboundid.ldap.sdk.unboundidds.tasks.TaskMessages.*;
059
060
061
062/**
063 * This class defines a Directory Server task that can be used to cause the
064 * server to generate administrative alerts, or to manage the set of degraded or
065 * unavailable alert types.
066 * <BR>
067 * <BLOCKQUOTE>
068 *   <B>NOTE:</B>  This class, and other classes within the
069 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
070 *   supported for use against Ping Identity, UnboundID, and
071 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
072 *   for proprietary functionality or for external specifications that are not
073 *   considered stable or mature enough to be guaranteed to work in an
074 *   interoperable way with other types of LDAP servers.
075 * </BLOCKQUOTE>
076 * <BR>
077 * The properties that are available for use with this type of task include:
078 * <UL>
079 *   <LI>The alert type of the alert notification to generate.  If this is
080 *       provided, then an alert message must also be provided.</LI>
081 *   <LI>The alert message for the alert notification to generate.  If this is
082 *       provided, then an alert type must also be provided.</LI>
083 *   <LI>The names of the alert types to add to the set of degraded alert types
084 *       in the general monitor entry.</LI>
085 *   <LI>The names of the alert types to remove from the set of degraded alert
086 *       types in the general monitor entry.</LI>
087 *   <LI>The names of the alert types to add to the set of unavailable alert
088 *       types in the general monitor entry.</LI>
089 *   <LI>The names of the alert types to remove from the set of unavailable
090 *       alert types in the general monitor entry.</LI>
091 * </UL>
092 */
093@NotMutable()
094@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
095public final class AlertTask
096       extends Task
097{
098  /**
099   * The fully-qualified name of the Java class that is used for the alert task.
100   */
101  @NotNull static final String ALERT_TASK_CLASS =
102       "com.unboundid.directory.server.tasks.AlertTask";
103
104
105
106  /**
107   * The name of the attribute used to specify the alert type for the alert to
108   * generate.
109   */
110  @NotNull private static final String ATTR_ALERT_TYPE = "ds-task-alert-type";
111
112
113
114  /**
115   * The name of the attribute used to specify the message for the alert to
116   * generate.
117   */
118  @NotNull private static final String ATTR_ALERT_MESSAGE =
119       "ds-task-alert-message";
120
121
122
123  /**
124   * The name of the attribute used to specify the alert type(s) to add to the
125   * set of degraded alert types.
126   */
127  @NotNull private static final String ATTR_ADD_DEGRADED_TYPE =
128       "ds-task-alert-add-degraded-type";
129
130
131
132  /**
133   * The name of the attribute used to specify the alert type(s) to remove from
134   * the set of degraded alert types.
135   */
136  @NotNull private static final String ATTR_REMOVE_DEGRADED_TYPE =
137       "ds-task-alert-remove-degraded-type";
138
139
140
141  /**
142   * The name of the attribute used to specify the alert type(s) to add to the
143   * set of unavailable alert types.
144   */
145  @NotNull private static final String ATTR_ADD_UNAVAILABLE_TYPE =
146       "ds-task-alert-add-unavailable-type";
147
148
149
150  /**
151   * The name of the attribute used to specify the alert type(s) to remove from
152   * the set of unavailable alert types.
153   */
154  @NotNull private static final String ATTR_REMOVE_UNAVAILABLE_TYPE =
155       "ds-task-alert-remove-unavailable-type";
156
157
158
159  /**
160   * The name of the object class used in alert task entries.
161   */
162  @NotNull private static final String OC_ALERT_TASK = "ds-task-alert";
163
164
165
166  /**
167   * The task property that will be used for the alert type.
168   */
169  @NotNull private static final TaskProperty PROPERTY_ALERT_TYPE =
170     new TaskProperty(ATTR_ALERT_TYPE, INFO_ALERT_DISPLAY_NAME_TYPE.get(),
171          INFO_ALERT_DESCRIPTION_TYPE.get(), String.class, false, false,
172          false);
173
174
175
176  /**
177   * The task property that will be used for the alert message.
178   */
179  @NotNull private static final TaskProperty PROPERTY_ALERT_MESSAGE =
180     new TaskProperty(ATTR_ALERT_MESSAGE, INFO_ALERT_DISPLAY_NAME_MESSAGE.get(),
181          INFO_ALERT_DESCRIPTION_MESSAGE.get(), String.class, false, false,
182          false);
183
184
185
186  /**
187   * The task property that will be used for the add degraded alert types.
188   */
189  @NotNull private static final TaskProperty PROPERTY_ADD_DEGRADED_TYPE =
190     new TaskProperty(ATTR_ADD_DEGRADED_TYPE,
191          INFO_ALERT_DISPLAY_NAME_ADD_DEGRADED.get(),
192          INFO_ALERT_DESCRIPTION_ADD_DEGRADED.get(), String.class, false, true,
193          false);
194
195
196
197  /**
198   * The task property that will be used for the remove degraded alert types.
199   */
200  @NotNull private static final TaskProperty PROPERTY_REMOVE_DEGRADED_TYPE =
201     new TaskProperty(ATTR_REMOVE_DEGRADED_TYPE,
202          INFO_ALERT_DISPLAY_NAME_REMOVE_DEGRADED.get(),
203          INFO_ALERT_DESCRIPTION_REMOVE_DEGRADED.get(), String.class, false,
204          true, false);
205
206
207
208  /**
209   * The task property that will be used for the add unavailable alert types.
210   */
211  @NotNull private static final TaskProperty PROPERTY_ADD_UNAVAILABLE_TYPE =
212     new TaskProperty(ATTR_ADD_UNAVAILABLE_TYPE,
213          INFO_ALERT_DISPLAY_NAME_ADD_UNAVAILABLE.get(),
214          INFO_ALERT_DESCRIPTION_ADD_UNAVAILABLE.get(), String.class, false,
215          true, false);
216
217
218
219  /**
220   * The task property that will be used for the remove unavailable alert types.
221   */
222  @NotNull private static final TaskProperty PROPERTY_REMOVE_UNAVAILABLE_TYPE =
223     new TaskProperty(ATTR_REMOVE_UNAVAILABLE_TYPE,
224          INFO_ALERT_DISPLAY_NAME_REMOVE_UNAVAILABLE.get(),
225          INFO_ALERT_DESCRIPTION_REMOVE_UNAVAILABLE.get(), String.class, false,
226          true, false);
227
228
229
230  /**
231   * The serial version UID for this serializable class.
232   */
233  private static final long serialVersionUID = 8253375533166941221L;
234
235
236
237  // The alert types to add to the set of degraded alert types.
238  @NotNull private final List<String> addDegradedTypes;
239
240  // The alert types to add to the set of unavailable alert types.
241  @NotNull private final List<String> addUnavailableTypes;
242
243  // The alert types to remove from the set of degraded alert types.
244  @NotNull private final List<String> removeDegradedTypes;
245
246  // The alert types to remove from the set of unavailable alert types.
247  @NotNull private final List<String> removeUnavailableTypes;
248
249  // The message for the alert to be generated.
250  @Nullable private final String alertMessage;
251
252  // The name of the alert type for the alert to be generated.
253  @Nullable private final String alertType;
254
255
256
257  /**
258   * Creates a new uninitialized alert task instance which should only be used
259   * for obtaining general information about this task, including the task name,
260   * description, and supported properties.  Attempts to use a task created with
261   * this constructor for any other reason will likely fail.
262   */
263  public AlertTask()
264  {
265    alertType              = null;
266    alertMessage           = null;
267    addDegradedTypes       = null;
268    addUnavailableTypes    = null;
269    removeDegradedTypes    = null;
270    removeUnavailableTypes = null;
271  }
272
273
274
275  /**
276   * Creates a new alert task that can be used to generate an administrative
277   * alert with the provided information.
278   *
279   * @param  alertType     The alert type to use for the generated alert.  It
280   *                       must not be {@code null}.
281   * @param  alertMessage  The message to use for the generated alert.  It must
282   *                       not be {@code null}.
283   */
284  public AlertTask(@NotNull final String alertType,
285                   @NotNull final String alertMessage)
286  {
287    this(null, alertType, alertMessage, null, null, null, null, null, null,
288         null, null, null);
289  }
290
291
292
293  /**
294   * Creates a new alert task that can be used to generate an administrative
295   * alert and/or update the set of degraded or unavailable alert types for the
296   * Directory Server.  At least one element must be provided.
297   *
298   * @param  alertType               The alert type to use for the generated
299   *                                 alert.  It may be {@code null} if no alert
300   *                                 should be generated, but if it is
301   *                                 non-{@code null} then the alert message
302   *                                 must also be non-{@code null}.
303   * @param  alertMessage            The message to use for the generated alert.
304   *                                 It may be {@code null} if no alert should
305   *                                 be generated, but if it is non-{@code null}
306   *                                 then the alert type must also be
307   *                                 non-{@code null}.
308   * @param  addDegradedTypes        The names of the alert types to add to the
309   *                                 Directory Server's set of degraded alert
310   *                                 types.  It may be {@code null} or empty if
311   *                                 no degraded alert types should be added.
312   * @param  removeDegradedTypes     The names of the alert types to remove from
313   *                                 the Directory Server's set of degraded
314   *                                 alert types.  It may be {@code null} or
315   *                                 empty if no degraded alert types should be
316   *                                 removed.
317   * @param  addUnavailableTypes     The names of the alert types to add to the
318   *                                 Directory Server's set of unavailable alert
319   *                                 types.  It may be {@code null} or empty if
320   *                                 no unavailable alert types should be added.
321   * @param  removeUnavailableTypes  The names of the alert types to remove from
322   *                                 the Directory Server's set of unavailable
323   *                                 alert types.  It may be {@code null} or
324   *                                 empty if no unavailable alert types should
325   *                                 be removed.
326   */
327  public AlertTask(@Nullable final String alertType,
328                   @Nullable final String alertMessage,
329                   @Nullable final List<String> addDegradedTypes,
330                   @Nullable final List<String> removeDegradedTypes,
331                   @Nullable final List<String> addUnavailableTypes,
332                   @Nullable final List<String> removeUnavailableTypes)
333  {
334    this(null, alertType, alertMessage, addDegradedTypes, removeDegradedTypes,
335         addUnavailableTypes, removeUnavailableTypes, null, null, null,
336         null, null);
337  }
338
339
340
341  /**
342   * Creates a new alert task that can be used to generate an administrative
343   * alert and/or update the set of degraded or unavailable alert types for the
344   * Directory Server.  At least one alert-related element must be provided.
345   *
346   * @param  taskID                  The task ID to use for this task.  If it is
347   *                                 {@code null} then a UUID will be generated
348   *                                 for use as the task ID.
349   * @param  alertType               The alert type to use for the generated
350   *                                 alert.  It may be {@code null} if no alert
351   *                                 should be generated, but if it is
352   *                                 non-{@code null} then the alert message
353   *                                 must also be non-{@code null}.
354   * @param  alertMessage            The message to use for the generated alert.
355   *                                 It may be {@code null} if no alert should
356   *                                 be generated, but if it is non-{@code null}
357   *                                 then the alert type must also be
358   *                                 non-{@code null}.
359   * @param  addDegradedTypes        The names of the alert types to add to the
360   *                                 Directory Server's set of degraded alert
361   *                                 types.  It may be {@code null} or empty if
362   *                                 no degraded alert types should be added.
363   * @param  removeDegradedTypes     The names of the alert types to remove from
364   *                                 the Directory Server's set of degraded
365   *                                 alert types.  It may be {@code null} or
366   *                                 empty if no degraded alert types should be
367   *                                 removed.
368   * @param  addUnavailableTypes     The names of the alert types to add to the
369   *                                 Directory Server's set of unavailable alert
370   *                                 types.  It may be {@code null} or empty if
371   *                                 no unavailable alert types should be added.
372   * @param  removeUnavailableTypes  The names of the alert types to remove from
373   *                                 the Directory Server's set of unavailable
374   *                                 alert types.  It may be {@code null} or
375   *                                 empty if no unavailable alert types should
376   *                                 be removed.
377   * @param  scheduledStartTime      The time that this task should start
378   *                                 running.
379   * @param  dependencyIDs           The list of task IDs that will be required
380   *                                 to complete before this task will be
381   *                                 eligible to start.
382   * @param  failedDependencyAction  Indicates what action should be taken if
383   *                                 any of the dependencies for this task do
384   *                                 not complete successfully.
385   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
386   *                                 that should be notified when this task
387   *                                 completes.
388   * @param  notifyOnError           The list of e-mail addresses of individuals
389   *                                 that should be notified if this task does
390   *                                 not complete successfully.
391   */
392  public AlertTask(@Nullable final String taskID,
393              @Nullable final String alertType,
394              @Nullable final String alertMessage,
395              @Nullable final List<String> addDegradedTypes,
396              @Nullable final List<String> removeDegradedTypes,
397              @Nullable final List<String> addUnavailableTypes,
398              @Nullable final List<String> removeUnavailableTypes,
399              @Nullable final Date scheduledStartTime,
400              @Nullable final List<String> dependencyIDs,
401              @Nullable final FailedDependencyAction failedDependencyAction,
402              @Nullable final List<String> notifyOnCompletion,
403              @Nullable final List<String> notifyOnError)
404  {
405    this(taskID, alertType, alertMessage, addDegradedTypes, removeDegradedTypes,
406         addUnavailableTypes, removeUnavailableTypes, scheduledStartTime,
407         dependencyIDs, failedDependencyAction, null, notifyOnCompletion, null,
408         notifyOnError, null, null, null);
409  }
410
411
412
413  /**
414   * Creates a new alert task that can be used to generate an administrative
415   * alert and/or update the set of degraded or unavailable alert types for the
416   * Directory Server.  At least one alert-related element must be provided.
417   *
418   * @param  taskID                  The task ID to use for this task.  If it is
419   *                                 {@code null} then a UUID will be generated
420   *                                 for use as the task ID.
421   * @param  alertType               The alert type to use for the generated
422   *                                 alert.  It may be {@code null} if no alert
423   *                                 should be generated, but if it is
424   *                                 non-{@code null} then the alert message
425   *                                 must also be non-{@code null}.
426   * @param  alertMessage            The message to use for the generated alert.
427   *                                 It may be {@code null} if no alert should
428   *                                 be generated, but if it is non-{@code null}
429   *                                 then the alert type must also be
430   *                                 non-{@code null}.
431   * @param  addDegradedTypes        The names of the alert types to add to the
432   *                                 Directory Server's set of degraded alert
433   *                                 types.  It may be {@code null} or empty if
434   *                                 no degraded alert types should be added.
435   * @param  removeDegradedTypes     The names of the alert types to remove from
436   *                                 the Directory Server's set of degraded
437   *                                 alert types.  It may be {@code null} or
438   *                                 empty if no degraded alert types should be
439   *                                 removed.
440   * @param  addUnavailableTypes     The names of the alert types to add to the
441   *                                 Directory Server's set of unavailable alert
442   *                                 types.  It may be {@code null} or empty if
443   *                                 no unavailable alert types should be added.
444   * @param  removeUnavailableTypes  The names of the alert types to remove from
445   *                                 the Directory Server's set of unavailable
446   *                                 alert types.  It may be {@code null} or
447   *                                 empty if no unavailable alert types should
448   *                                 be removed.
449   * @param  scheduledStartTime      The time that this task should start
450   *                                 running.
451   * @param  dependencyIDs           The list of task IDs that will be required
452   *                                 to complete before this task will be
453   *                                 eligible to start.
454   * @param  failedDependencyAction  Indicates what action should be taken if
455   *                                 any of the dependencies for this task do
456   *                                 not complete successfully.
457   * @param  notifyOnStart           The list of e-mail addresses of individuals
458   *                                 that should be notified when this task
459   *                                 starts running.
460   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
461   *                                 that should be notified when this task
462   *                                 completes.
463   * @param  notifyOnSuccess         The list of e-mail addresses of individuals
464   *                                 that should be notified if this task
465   *                                 completes successfully.
466   * @param  notifyOnError           The list of e-mail addresses of individuals
467   *                                 that should be notified if this task does
468   *                                 not complete successfully.
469   * @param  alertOnStart            Indicates whether the server should send an
470   *                                 alert notification when this task starts.
471   * @param  alertOnSuccess          Indicates whether the server should send an
472   *                                 alert notification if this task completes
473   *                                 successfully.
474   * @param  alertOnError            Indicates whether the server should send an
475   *                                 alert notification if this task fails to
476   *                                 complete successfully.
477   */
478  public AlertTask(@Nullable final String taskID,
479              @Nullable final String alertType,
480              @Nullable final String alertMessage,
481              @Nullable final List<String> addDegradedTypes,
482              @Nullable final List<String> removeDegradedTypes,
483              @Nullable final List<String> addUnavailableTypes,
484              @Nullable final List<String> removeUnavailableTypes,
485              @Nullable final Date scheduledStartTime,
486              @Nullable final List<String> dependencyIDs,
487              @Nullable final FailedDependencyAction failedDependencyAction,
488              @Nullable final List<String> notifyOnStart,
489              @Nullable final List<String> notifyOnCompletion,
490              @Nullable final List<String> notifyOnSuccess,
491              @Nullable final List<String> notifyOnError,
492              @Nullable final Boolean alertOnStart,
493              @Nullable final Boolean alertOnSuccess,
494              @Nullable final Boolean alertOnError)
495  {
496    super(taskID, ALERT_TASK_CLASS, scheduledStartTime, dependencyIDs,
497         failedDependencyAction, notifyOnStart, notifyOnCompletion,
498         notifyOnSuccess, notifyOnError, alertOnStart, alertOnSuccess,
499         alertOnError);
500
501    this.alertType    = alertType;
502    this.alertMessage = alertMessage;
503
504    Validator.ensureTrue((alertType == null) == (alertMessage == null));
505
506
507    this.addDegradedTypes       = getStringList(addDegradedTypes);
508    this.removeDegradedTypes    = getStringList(removeDegradedTypes);
509    this.addUnavailableTypes    = getStringList(addUnavailableTypes);
510    this.removeUnavailableTypes = getStringList(removeUnavailableTypes);
511
512    if (alertType == null)
513    {
514      Validator.ensureFalse(this.addDegradedTypes.isEmpty() &&
515           this.removeDegradedTypes.isEmpty() &&
516           this.addUnavailableTypes.isEmpty() &&
517           this.removeUnavailableTypes.isEmpty());
518    }
519  }
520
521
522
523  /**
524   * Creates a new alert task from the provided entry.
525   *
526   * @param  entry  The entry to use to create this alert task.
527   *
528   * @throws  TaskException  If the provided entry cannot be parsed as an alert
529   *                         task entry.
530   */
531  public AlertTask(@NotNull final Entry entry)
532         throws TaskException
533  {
534    super(entry);
535
536
537    // Get the alert type and message.  If either is present, then both must be.
538    alertType    = entry.getAttributeValue(ATTR_ALERT_TYPE);
539    alertMessage = entry.getAttributeValue(ATTR_ALERT_MESSAGE);
540
541    if ((alertType == null) != (alertMessage == null))
542    {
543      throw new TaskException(ERR_ALERT_TYPE_AND_MESSAGE_INTERDEPENDENT.get());
544    }
545
546
547    // Get the values to add/remove from the degraded/unavailable alert types.
548    addDegradedTypes       = parseStringList(entry, ATTR_ADD_DEGRADED_TYPE);
549    removeDegradedTypes    = parseStringList(entry, ATTR_REMOVE_DEGRADED_TYPE);
550    addUnavailableTypes    = parseStringList(entry, ATTR_ADD_UNAVAILABLE_TYPE);
551    removeUnavailableTypes = parseStringList(entry,
552         ATTR_REMOVE_UNAVAILABLE_TYPE);
553
554    if ((alertType == null) && addDegradedTypes.isEmpty() &&
555        removeDegradedTypes.isEmpty() && addUnavailableTypes.isEmpty() &&
556        removeUnavailableTypes.isEmpty())
557    {
558      throw new TaskException(ERR_ALERT_ENTRY_NO_ELEMENTS.get());
559    }
560  }
561
562
563
564  /**
565   * Creates a new alert task from the provided set of task properties.
566   *
567   * @param  properties  The set of task properties and their corresponding
568   *                     values to use for the task.  It must not be
569   *                     {@code null}.
570   *
571   * @throws  TaskException  If the provided set of properties cannot be used to
572   *                         create a valid alert task.
573   */
574  public AlertTask(@NotNull final Map<TaskProperty,List<Object>> properties)
575         throws TaskException
576  {
577    super(ALERT_TASK_CLASS, properties);
578
579    String type = null;
580    String message = null;
581    final LinkedList<String> addDegraded = new LinkedList<>();
582    final LinkedList<String> removeDegraded = new LinkedList<>();
583    final LinkedList<String> addUnavailable = new LinkedList<>();
584    final LinkedList<String> removeUnavailable = new LinkedList<>();
585    for (final Map.Entry<TaskProperty,List<Object>> entry :
586         properties.entrySet())
587    {
588      final TaskProperty p = entry.getKey();
589      final String attrName = StaticUtils.toLowerCase(p.getAttributeName());
590      final List<Object> values = entry.getValue();
591
592      if (attrName.equals(ATTR_ALERT_TYPE))
593      {
594        type = parseString(p, values, type);
595      }
596      else if (attrName.equals(ATTR_ALERT_MESSAGE))
597      {
598        message = parseString(p, values, message);
599      }
600      else if (attrName.equals(ATTR_ADD_DEGRADED_TYPE))
601      {
602        final String[] s = parseStrings(p, values, null);
603        if (s != null)
604        {
605          addDegraded.addAll(Arrays.asList(s));
606        }
607      }
608      else if (attrName.equals(ATTR_REMOVE_DEGRADED_TYPE))
609      {
610        final String[] s = parseStrings(p, values, null);
611        if (s != null)
612        {
613          removeDegraded.addAll(Arrays.asList(s));
614        }
615      }
616      else if (attrName.equals(ATTR_ADD_UNAVAILABLE_TYPE))
617      {
618        final String[] s = parseStrings(p, values, null);
619        if (s != null)
620        {
621          addUnavailable.addAll(Arrays.asList(s));
622        }
623      }
624      else if (attrName.equals(ATTR_REMOVE_UNAVAILABLE_TYPE))
625      {
626        final String[] s = parseStrings(p, values, null);
627        if (s != null)
628        {
629          removeUnavailable.addAll(Arrays.asList(s));
630        }
631      }
632    }
633
634    alertType              = type;
635    alertMessage           = message;
636    addDegradedTypes       = Collections.unmodifiableList(addDegraded);
637    removeDegradedTypes    = Collections.unmodifiableList(removeDegraded);
638    addUnavailableTypes    = Collections.unmodifiableList(addUnavailable);
639    removeUnavailableTypes = Collections.unmodifiableList(removeUnavailable);
640
641    if ((alertType == null) != (alertMessage == null))
642    {
643      throw new TaskException(ERR_ALERT_TYPE_AND_MESSAGE_INTERDEPENDENT.get());
644    }
645
646    if ((alertType == null) && addDegradedTypes.isEmpty() &&
647        removeDegradedTypes.isEmpty() && addUnavailableTypes.isEmpty() &&
648        removeUnavailableTypes.isEmpty())
649    {
650      throw new TaskException(ERR_ALERT_PROPERTIES_NO_ELEMENTS.get());
651    }
652  }
653
654
655
656  /**
657   * {@inheritDoc}
658   */
659  @Override()
660  @NotNull()
661  public String getTaskName()
662  {
663    return INFO_TASK_NAME_ALERT.get();
664  }
665
666
667
668  /**
669   * {@inheritDoc}
670   */
671  @Override()
672  @NotNull()
673  public String getTaskDescription()
674  {
675    return INFO_TASK_DESCRIPTION_ALERT.get();
676  }
677
678
679
680  /**
681   * Retrieves the name of the alert type to use for the alert notification to
682   * be generated, if appropriate.
683   *
684   * @return  The name of the alert type to use for the alert notification to be
685   *          generated, or {@code null} if no alert should be generated.
686   */
687  @Nullable()
688  public String getAlertType()
689  {
690    return alertType;
691  }
692
693
694
695  /**
696   * Retrieves the message to use for the alert notification to be generated, if
697   * appropriate.
698   *
699   * @return  The message to use for the alert notification to be generated, or
700   *          {@code null} if no alert should be generated.
701   */
702  @Nullable()
703  public String getAlertMessage()
704  {
705    return alertMessage;
706  }
707
708
709
710  /**
711   * Retrieves the names of the alert types that should be added to the set of
712   * degraded alert types.
713   *
714   * @return  The names of the alert types that should be added to the set of
715   *          degraded alert types, or an empty list if no degraded alert types
716   *          should be added.
717   */
718  @NotNull()
719  public List<String> getAddDegradedAlertTypes()
720  {
721    return addDegradedTypes;
722  }
723
724
725
726  /**
727   * Retrieves the names of the alert types that should be removed from the set
728   * of degraded alert types.
729   *
730   * @return  The names of the alert types that should be removed from the set
731   *          of degraded alert types, or an empty list if no degraded alert
732   *          types should be removed.
733   */
734  @NotNull()
735  public List<String> getRemoveDegradedAlertTypes()
736  {
737    return removeDegradedTypes;
738  }
739
740
741
742  /**
743   * Retrieves the names of the alert types that should be added to the set of
744   * unavailable alert types.
745   *
746   * @return  The names of the alert types that should be added to the set of
747   *          unavailable alert types, or an empty list if no unavailable alert
748   *          types should be added.
749   */
750  @NotNull()
751  public List<String> getAddUnavailableAlertTypes()
752  {
753    return addUnavailableTypes;
754  }
755
756
757
758  /**
759   * Retrieves the names of the alert types that should be removed from the set
760   * of unavailable alert types.
761   *
762   * @return  The names of the alert types that should be removed from the set
763   *          of unavailable alert types, or an empty list if no unavailable
764   *          alert types should be removed.
765   */
766  @NotNull()
767  public List<String> getRemoveUnavailableAlertTypes()
768  {
769    return removeUnavailableTypes;
770  }
771
772
773
774  /**
775   * {@inheritDoc}
776   */
777  @Override()
778  @NotNull()
779  protected List<String> getAdditionalObjectClasses()
780  {
781    return Collections.singletonList(OC_ALERT_TASK);
782  }
783
784
785
786  /**
787   * {@inheritDoc}
788   */
789  @Override()
790  @NotNull()
791  protected List<Attribute> getAdditionalAttributes()
792  {
793    final LinkedList<Attribute> attrList = new LinkedList<>();
794
795    if (alertType != null)
796    {
797      attrList.add(new Attribute(ATTR_ALERT_TYPE, alertType));
798      attrList.add(new Attribute(ATTR_ALERT_MESSAGE, alertMessage));
799    }
800
801    if (! addDegradedTypes.isEmpty())
802    {
803      attrList.add(new Attribute(ATTR_ADD_DEGRADED_TYPE, addDegradedTypes));
804    }
805
806    if (! removeDegradedTypes.isEmpty())
807    {
808      attrList.add(new Attribute(ATTR_REMOVE_DEGRADED_TYPE,
809           removeDegradedTypes));
810    }
811
812    if (! addUnavailableTypes.isEmpty())
813    {
814      attrList.add(new Attribute(ATTR_ADD_UNAVAILABLE_TYPE,
815           addUnavailableTypes));
816    }
817
818    if (! removeUnavailableTypes.isEmpty())
819    {
820      attrList.add(new Attribute(ATTR_REMOVE_UNAVAILABLE_TYPE,
821           removeUnavailableTypes));
822    }
823
824    return attrList;
825  }
826
827
828
829  /**
830   * {@inheritDoc}
831   */
832  @Override()
833  @NotNull()
834  public List<TaskProperty> getTaskSpecificProperties()
835  {
836    return Collections.unmodifiableList(Arrays.asList(
837         PROPERTY_ALERT_TYPE, PROPERTY_ALERT_MESSAGE,
838         PROPERTY_ADD_DEGRADED_TYPE, PROPERTY_REMOVE_DEGRADED_TYPE,
839         PROPERTY_ADD_UNAVAILABLE_TYPE, PROPERTY_REMOVE_UNAVAILABLE_TYPE));
840  }
841
842
843
844  /**
845   * {@inheritDoc}
846   */
847  @Override()
848  @NotNull()
849  public Map<TaskProperty,List<Object>> getTaskPropertyValues()
850  {
851    final LinkedHashMap<TaskProperty,List<Object>> props =
852         new LinkedHashMap<>(StaticUtils.computeMapCapacity(6));
853
854    if (alertType != null)
855    {
856      props.put(PROPERTY_ALERT_TYPE,
857           Collections.<Object>singletonList(alertType));
858      props.put(PROPERTY_ALERT_MESSAGE,
859           Collections.<Object>singletonList(alertMessage));
860    }
861
862    if (! addDegradedTypes.isEmpty())
863    {
864      props.put(PROPERTY_ADD_DEGRADED_TYPE,
865           Collections.<Object>unmodifiableList(addDegradedTypes));
866    }
867
868    if (! removeDegradedTypes.isEmpty())
869    {
870      props.put(PROPERTY_REMOVE_DEGRADED_TYPE,
871           Collections.<Object>unmodifiableList(removeDegradedTypes));
872    }
873
874    if (! addUnavailableTypes.isEmpty())
875    {
876      props.put(PROPERTY_ADD_UNAVAILABLE_TYPE,
877           Collections.<Object>unmodifiableList(addUnavailableTypes));
878    }
879
880    if (! removeUnavailableTypes.isEmpty())
881    {
882      props.put(PROPERTY_REMOVE_UNAVAILABLE_TYPE,
883           Collections.<Object>unmodifiableList(removeUnavailableTypes));
884    }
885
886    return Collections.unmodifiableMap(props);
887  }
888
889
890
891  /**
892   * Retrieves an unmodifiable list using information from the provided list.
893   * If the given list is {@code null}, then an empty list will be returned.
894   * Otherwise, an unmodifiable version of the provided list will be returned.
895   *
896   * @param  l  The list to be processed.
897   *
898   * @return  The resulting string list.
899   */
900  @NotNull()
901  private static List<String> getStringList(@Nullable final List<String> l)
902  {
903    if (l == null)
904    {
905      return Collections.emptyList();
906    }
907    else
908    {
909      return Collections.unmodifiableList(l);
910    }
911  }
912}