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 2008-2011 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.guitools.controlpanel.datamodel;
028
029import java.util.Objects;
030import java.util.Set;
031import java.util.SortedSet;
032import java.util.TreeSet;
033
034import org.opends.admin.ads.ADSContext;
035
036/** The class that describes the backend configuration. */
037public class BackendDescriptor
038{
039  private final String backendID;
040  private SortedSet<BaseDNDescriptor> baseDns;
041  private SortedSet<IndexDescriptor> indexes;
042  private SortedSet<VLVIndexDescriptor> vlvIndexes;
043  private int entries;
044  private final boolean isConfigBackend;
045  private final boolean isEnabled;
046  private CustomSearchResult monitoringEntry;
047  private final Type type;
048  private PluggableType pluggableType;
049  private int hashCode;
050
051  /** An enumeration describing the type of backend. */
052  public enum Type
053  {
054    /** The backend is a backup backend. */
055    BACKUP,
056    /** The backend is a LDIF backend. */
057    LDIF,
058    /** The backend is a memory backend. */
059    MEMORY,
060    /** The backend is a monitor backend. */
061    MONITOR,
062    /** The backend is another type of backend (for instance user defined). */
063    OTHER,
064    /** The backend is pluggable. */
065    PLUGGABLE,
066    /** The backend is a task backend. */
067    TASK
068  }
069
070  /** An enumeration describing the different pluggable backends. */
071  public enum PluggableType
072  {
073    /** JE Backend. */
074    JE,
075    /** PDB Backend. */
076    PDB,
077    /** Unknown Type, should never fall through this. */
078    UNKNOWN
079  }
080
081  /**
082   * Constructor for this class.
083   * @param backendID the backend ID of the Backend.
084   * @param baseDns the base DNs associated with the Backend.
085   * @param indexes the indexes defined in the backend.
086   * @param vlvIndexes the VLV indexes defined in the backend.
087   * @param entries the number of entries in the Backend.
088   * @param isEnabled whether the backend is enabled or not.
089   * @param type the type of the backend.
090   */
091  public BackendDescriptor(String backendID,
092      Set<BaseDNDescriptor> baseDns,
093      Set<IndexDescriptor> indexes,
094      Set<VLVIndexDescriptor> vlvIndexes,
095      int entries, boolean isEnabled, Type type)
096  {
097    this.backendID = backendID;
098    this.entries = entries;
099    isConfigBackend = isConfigBackend(backendID);
100    this.type = type;
101    this.isEnabled = isEnabled;
102    updateBaseDnsAndIndexes(baseDns, indexes, vlvIndexes);
103    recalculateHashCode();
104  }
105
106  /**
107   * Returns the ID of the Backend.
108   * @return the ID of the Backend.
109   */
110  public String getBackendID()
111  {
112    return backendID;
113  }
114
115  /**
116   * Returns the Base DN objects associated with the backend.
117   * @return the Base DN objects associated with the backend.
118   */
119  public SortedSet<BaseDNDescriptor> getBaseDns()
120  {
121    return baseDns;
122  }
123
124  /**
125   * Returns the vlv index objects associated with the backend.
126   * @return the vlv index objects associated with the backend.
127   */
128  public SortedSet<VLVIndexDescriptor> getVLVIndexes()
129  {
130    return vlvIndexes;
131  }
132
133
134  /**
135   * Returns the index objects associated with the backend.
136   * @return the index objects associated with the backend.
137   */
138  public SortedSet<IndexDescriptor> getIndexes()
139  {
140    return indexes;
141  }
142
143  /**
144   * Return the number of entries in the backend.
145   * -1 indicates that the number of entries could not be found.
146   * @return the number of entries in the backend.
147   */
148  public int getEntries()
149  {
150    return entries;
151  }
152
153  /** {@inheritDoc} */
154  @Override
155  public boolean equals(Object o)
156  {
157    if (this == o)
158    {
159      return true;
160    }
161    if (o instanceof BackendDescriptor)
162    {
163      BackendDescriptor desc = (BackendDescriptor)o;
164      return getBackendID().equals(desc.getBackendID())
165          && getEntries() == desc.getEntries()
166          && desc.getBaseDns().equals(getBaseDns())
167          && desc.getIndexes().equals(getIndexes())
168          && desc.getVLVIndexes().equals(getVLVIndexes())
169          && Objects.equals(getMonitoringEntry(), desc.getMonitoringEntry());
170    }
171    return false;
172  }
173
174  /**
175   * Returns the monitoring entry information.
176   * @return the monitoring entry information.
177   */
178  public CustomSearchResult getMonitoringEntry()
179  {
180    return monitoringEntry;
181  }
182
183  /** {@inheritDoc} */
184  @Override
185  public int hashCode()
186  {
187    return hashCode;
188  }
189
190  /**
191   * Method called when one of the elements that affect the value of the
192   * hashcode is modified.  It is used to minimize the time spent calculating
193   * hashCode.
194   */
195  private void recalculateHashCode()
196  {
197    hashCode = 0;
198    for (BaseDNDescriptor rep: getBaseDns())
199    {
200      hashCode += rep.hashCode();
201    }
202    hashCode += entries;
203    for (IndexDescriptor index : indexes)
204    {
205      hashCode += index.hashCode();
206    }
207    for (VLVIndexDescriptor index : vlvIndexes)
208    {
209      hashCode += index.hashCode();
210    }
211  }
212
213  /**
214   * Updates the base DNs and indexes contained in this backend so that they
215   * have a reference to this backend.  It also initialize the members of this
216   * class with the base DNs and indexes.
217   * @param baseDns the base DNs associated with the Backend.
218   * @param indexes the indexes defined in the backend.
219   * @param vlvIndexes the VLV indexes defined in the backend.
220   *
221   */
222  private void updateBaseDnsAndIndexes(Set<BaseDNDescriptor> baseDns,
223      Set<IndexDescriptor> indexes, Set<VLVIndexDescriptor> vlvIndexes)
224  {
225    for (BaseDNDescriptor baseDN : baseDns)
226    {
227      baseDN.setBackend(this);
228    }
229    this.baseDns = new TreeSet<>(baseDns);
230    for (IndexDescriptor index : indexes)
231    {
232      index.setBackend(this);
233    }
234    this.indexes = new TreeSet<>(indexes);
235    for (VLVIndexDescriptor index : vlvIndexes)
236    {
237      index.setBackend(this);
238    }
239    this.vlvIndexes = new TreeSet<>(vlvIndexes);
240  }
241
242  /**
243   * An convenience method to know if the provided ID corresponds to a
244   * configuration backend or not.
245   * @param id the backend ID to analyze
246   * @return <CODE>true</CODE> if the the id corresponds to a configuration
247   * backend and <CODE>false</CODE> otherwise.
248   */
249  private boolean isConfigBackend(String id)
250  {
251    return "tasks".equalsIgnoreCase(id) ||
252    "schema".equalsIgnoreCase(id) ||
253    "config".equalsIgnoreCase(id) ||
254    "monitor".equalsIgnoreCase(id) ||
255    "backup".equalsIgnoreCase(id) ||
256    ADSContext.getDefaultBackendName().equalsIgnoreCase(id) ||
257    "ads-truststore".equalsIgnoreCase(id);
258  }
259
260  /**
261   * Tells whether this is a configuration backend or not.
262   * @return <CODE>true</CODE> if this is a configuration backend and
263   * <CODE>false</CODE> otherwise.
264   */
265  public boolean isConfigBackend()
266  {
267    return isConfigBackend;
268  }
269
270  /**
271   * Sets the number of entries contained in this backend.
272   * @param entries the number of entries contained in this backend.
273   */
274  public void setEntries(int entries)
275  {
276    this.entries = entries;
277
278    // Recalculate hashCode
279    recalculateHashCode();
280  }
281
282  /**
283   * Sets the monitoring entry corresponding to this backend.
284   * @param monitoringEntry the monitoring entry corresponding to this backend.
285   */
286  public void setMonitoringEntry(CustomSearchResult monitoringEntry)
287  {
288    this.monitoringEntry = monitoringEntry;
289  }
290
291  /**
292   * Returns the type of the backend.
293   * @return the type of the backend.
294   */
295  public Type getType()
296  {
297    return type;
298  }
299
300  /**
301   * Tells whether this backend is enabled or not.
302   * @return <CODE>true</CODE> if this is backend is enabled
303   * <CODE>false</CODE> otherwise.
304   */
305  public boolean isEnabled()
306  {
307    return isEnabled;
308  }
309
310  /**
311   * Set the type of pluggable backend.
312   * @param pluggableType the type of pluggable backend.
313   */
314  public void setPluggableType(PluggableType pluggableType)
315  {
316    this.pluggableType = pluggableType;
317  }
318
319  /**
320   * Get the type of pluggable backend.
321   * @return the type of pluggable backend.
322   */
323  public PluggableType getPluggableType()
324  {
325    return pluggableType;
326  }
327}