001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.tools.dsreplication;
028
029import java.util.ArrayList;
030import java.util.LinkedList;
031
032import javax.naming.NamingEnumeration;
033import javax.naming.NamingException;
034import javax.naming.directory.Attribute;
035import javax.naming.directory.BasicAttribute;
036import javax.naming.directory.BasicAttributes;
037
038import org.forgerock.opendj.ldap.ByteString;
039import org.opends.server.admin.client.cli.TaskScheduleArgs;
040import org.opends.server.tools.tasks.TaskClient;
041import org.opends.server.tools.tasks.TaskScheduleUserData;
042import org.opends.server.types.RawAttribute;
043
044/** This class is used to store the information provided by the user to purge historical data. */
045public class PurgeHistoricalUserData extends MonoServerReplicationUserData
046{
047  private int maximumDuration;
048  private boolean online;
049  private TaskScheduleUserData taskSchedule = new TaskScheduleUserData();
050
051  /** Default constructor. */
052  public PurgeHistoricalUserData()
053  {
054  }
055
056  /**
057   * Returns the maximum duration that the purge can take in seconds.
058   * @return the maximum duration that the purge can take in seconds.
059   */
060  public int getMaximumDuration()
061  {
062    return maximumDuration;
063  }
064
065  /**
066   * Sets the maximum duration that the purge can take in seconds.
067   * @param maximumDuration the maximum duration that the purge can take in
068   * seconds.
069   */
070  public void setMaximumDuration(int maximumDuration)
071  {
072    this.maximumDuration = maximumDuration;
073  }
074
075  /**
076   * Whether the task will be executed on an online server (using an LDAP
077   * connection and the tasks backend) or not.
078   * @return {@code true} if the task will be executed on an online server
079   * and {@code false} otherwise.
080   */
081  public boolean isOnline()
082  {
083    return online;
084  }
085
086  /**
087   * Sets whether the task will be executed on an online server or not.
088   * @param online {@code true} if the task will be executed on an online server
089   * and {@code false} otherwise.
090   */
091  public void setOnline(boolean online)
092  {
093    this.online = online;
094  }
095
096  /**
097   * Returns the object describing the schedule of the task.  If the operation
098   * is not online, the value returned by this method should not be taken into
099   * account.
100   * @return the object describing the schedule of the task.
101   */
102  public TaskScheduleUserData getTaskSchedule()
103  {
104    return taskSchedule;
105  }
106
107  /**
108   * Sets the object describing the schedule of the task.
109   * @param taskSchedule the object describing the schedule of the task.
110   */
111  public void setTaskSchedule(TaskScheduleUserData taskSchedule)
112  {
113    this.taskSchedule = taskSchedule;
114  }
115
116  /**
117   * Initializes the contents of the provided purge historical replication user
118   * data object with what was provided in the command-line without prompting to
119   * the user.
120   * @param uData the purge historical replication user data object to be
121   * initialized.
122   * @param argParser the argument parser with the arguments provided by the
123   * user.
124   */
125  public static  void initializeWithArgParser(PurgeHistoricalUserData uData,
126      ReplicationCliArgumentParser argParser)
127  {
128    uData.setBaseDNs(new LinkedList<String>(argParser.getBaseDNs()));
129
130    if (argParser.connectionArgumentsPresent())
131    {
132      uData.setAdminUid(argParser.getAdministratorUIDOrDefault());
133      uData.setAdminPwd(argParser.getBindPasswordAdmin());
134      uData.setHostName(argParser.getHostNameToStatusOrDefault());
135      uData.setPort(argParser.getPortToStatusOrDefault());
136      uData.setOnline(true);
137      TaskScheduleUserData taskSchedule = new TaskScheduleUserData();
138      TaskScheduleArgs taskArgs = argParser.getTaskArgsList();
139      taskSchedule.setStartNow(taskArgs.isStartNow());
140      if (!taskSchedule.isStartNow())
141      {
142        taskSchedule.setStartDate(taskArgs.getStartDateTime());
143        taskSchedule.setDependencyIds(taskArgs.getDependencyIds());
144        taskSchedule.setFailedDependencyAction(
145            taskArgs.getFailedDependencyAction());
146        taskSchedule.setNotifyUponErrorEmailAddresses(
147            taskArgs.getNotifyUponErrorEmailAddresses());
148        taskSchedule.setNotifyUponCompletionEmailAddresses(
149            taskArgs.getNotifyUponCompletionEmailAddresses());
150        taskSchedule.setRecurringDateTime(
151            taskArgs.getRecurringDateTime());
152      }
153      uData.setTaskSchedule(taskSchedule);
154    }
155    else
156    {
157      uData.setOnline(false);
158    }
159
160    uData.setMaximumDuration(argParser.getMaximumDurationOrDefault());
161  }
162
163  /**
164   * Commodity method that returns the list of basic task attributes required
165   * to launch a task corresponding to the provided user data.
166   * @param uData the user data describing the purge historical to be executed.
167   * @return the list of basic task attributes required
168   * to launch a task corresponding to the provided user data.
169   */
170  public static BasicAttributes getTaskAttributes(PurgeHistoricalUserData uData)
171  {
172    PurgeHistoricalScheduleInformation information =
173      new PurgeHistoricalScheduleInformation(uData);
174    return getAttributes(TaskClient.getTaskAttributes(information));
175  }
176
177  private static BasicAttributes getAttributes(ArrayList<RawAttribute> rawAttrs)
178  {
179    BasicAttributes attrs = new BasicAttributes();
180    for (RawAttribute rawAttr : rawAttrs)
181    {
182      BasicAttribute attr = new BasicAttribute(rawAttr.getAttributeType());
183      for (ByteString v : rawAttr.getValues())
184      {
185        attr.add(v.toString());
186      }
187      attrs.put(attr);
188    }
189    return attrs;
190  }
191
192  /**
193   * Returns the DN of the task corresponding to the provided list of
194   * attributes.  The code assumes that the attributes have been generated
195   * calling the method {@link #getTaskAttributes(PurgeHistoricalUserData)}.
196   * @param attrs the attributes of the task entry.
197   * @return the DN of the task entry.
198   */
199  public static String getTaskDN(BasicAttributes attrs)
200  {
201    ArrayList<RawAttribute> rawAttrs = getRawAttributes(attrs);
202    return TaskClient.getTaskDN(rawAttrs);
203  }
204
205  /**
206   * Returns the ID of the task corresponding to the provided list of
207   * attributes.  The code assumes that the attributes have been generated
208   * calling the method {@link #getTaskAttributes(PurgeHistoricalUserData)}.
209   * @param attrs the attributes of the task entry.
210   * @return the ID of the task entry.
211   */
212  public static String getTaskID(BasicAttributes attrs)
213  {
214    ArrayList<RawAttribute> rawAttrs = getRawAttributes(attrs);
215    return TaskClient.getTaskID(rawAttrs);
216  }
217
218  private static ArrayList<RawAttribute> getRawAttributes(BasicAttributes attrs)
219  {
220    try
221    {
222      ArrayList<RawAttribute> rawAttrs = new ArrayList<>();
223      NamingEnumeration<Attribute> nAtt = attrs.getAll();
224      while (nAtt.hasMore())
225      {
226        Attribute attr = nAtt.next();
227        NamingEnumeration<?> values = attr.getAll();
228        ArrayList<ByteString> rawValues = new ArrayList<>();
229        while (values.hasMore())
230        {
231          Object v = values.next();
232          rawValues.add(ByteString.valueOfUtf8(v.toString()));
233        }
234        RawAttribute rAttr = RawAttribute.create(attr.getID(), rawValues);
235        rawAttrs.add(rAttr);
236      }
237      return rawAttrs;
238    }
239    catch (NamingException ne)
240    {
241      // This is a bug.
242      throw new RuntimeException("Unexpected error: "+ne, ne);
243    }
244  }
245}