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