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