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-2016 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  @Override
144  public boolean equals(Object o)
145  {
146    if (this == o)
147    {
148      return true;
149    }
150    if (o instanceof BackendDescriptor)
151    {
152      BackendDescriptor desc = (BackendDescriptor)o;
153      return getBackendID().equals(desc.getBackendID())
154          && getEntries() == desc.getEntries()
155          && desc.getBaseDns().equals(getBaseDns())
156          && desc.getIndexes().equals(getIndexes())
157          && desc.getVLVIndexes().equals(getVLVIndexes())
158          && Objects.equals(getMonitoringEntry(), desc.getMonitoringEntry());
159    }
160    return false;
161  }
162
163  /**
164   * Returns the monitoring entry information.
165   * @return the monitoring entry information.
166   */
167  public CustomSearchResult getMonitoringEntry()
168  {
169    return monitoringEntry;
170  }
171
172  @Override
173  public int hashCode()
174  {
175    return hashCode;
176  }
177
178  /**
179   * Method called when one of the elements that affect the value of the
180   * hashcode is modified.  It is used to minimize the time spent calculating
181   * hashCode.
182   */
183  private void recalculateHashCode()
184  {
185    hashCode = 0;
186    for (BaseDNDescriptor rep: getBaseDns())
187    {
188      hashCode += rep.hashCode();
189    }
190    hashCode += entries;
191    for (IndexDescriptor index : indexes)
192    {
193      hashCode += index.hashCode();
194    }
195    for (VLVIndexDescriptor index : vlvIndexes)
196    {
197      hashCode += index.hashCode();
198    }
199  }
200
201  /**
202   * Updates the base DNs and indexes contained in this backend so that they
203   * have a reference to this backend.  It also initialize the members of this
204   * class with the base DNs and indexes.
205   * @param baseDns the base DNs associated with the Backend.
206   * @param indexes the indexes defined in the backend.
207   * @param vlvIndexes the VLV indexes defined in the backend.
208   */
209  private void updateBaseDnsAndIndexes(Set<BaseDNDescriptor> baseDns,
210      Set<IndexDescriptor> indexes, Set<VLVIndexDescriptor> vlvIndexes)
211  {
212    for (BaseDNDescriptor baseDN : baseDns)
213    {
214      baseDN.setBackend(this);
215    }
216    this.baseDns = new TreeSet<>(baseDns);
217    for (IndexDescriptor index : indexes)
218    {
219      index.setBackend(this);
220    }
221    this.indexes = new TreeSet<>(indexes);
222    for (VLVIndexDescriptor index : vlvIndexes)
223    {
224      index.setBackend(this);
225    }
226    this.vlvIndexes = new TreeSet<>(vlvIndexes);
227  }
228
229  /**
230   * An convenience method to know if the provided ID corresponds to a
231   * configuration backend or not.
232   * @param id the backend ID to analyze
233   * @return <CODE>true</CODE> if the the id corresponds to a configuration
234   * backend and <CODE>false</CODE> otherwise.
235   */
236  private boolean isConfigBackend(String id)
237  {
238    return "tasks".equalsIgnoreCase(id) ||
239    "schema".equalsIgnoreCase(id) ||
240    "config".equalsIgnoreCase(id) ||
241    "monitor".equalsIgnoreCase(id) ||
242    "backup".equalsIgnoreCase(id) ||
243    ADSContext.getDefaultBackendName().equalsIgnoreCase(id) ||
244    "ads-truststore".equalsIgnoreCase(id);
245  }
246
247  /**
248   * Tells whether this is a configuration backend or not.
249   * @return <CODE>true</CODE> if this is a configuration backend and
250   * <CODE>false</CODE> otherwise.
251   */
252  public boolean isConfigBackend()
253  {
254    return isConfigBackend;
255  }
256
257  /**
258   * Sets the number of entries contained in this backend.
259   * @param entries the number of entries contained in this backend.
260   */
261  public void setEntries(int entries)
262  {
263    this.entries = entries;
264
265    // Recalculate hashCode
266    recalculateHashCode();
267  }
268
269  /**
270   * Sets the monitoring entry corresponding to this backend.
271   * @param monitoringEntry the monitoring entry corresponding to this backend.
272   */
273  public void setMonitoringEntry(CustomSearchResult monitoringEntry)
274  {
275    this.monitoringEntry = monitoringEntry;
276  }
277
278  /**
279   * Returns the type of the backend.
280   * @return the type of the backend.
281   */
282  public Type getType()
283  {
284    return type;
285  }
286
287  /**
288   * Tells whether this backend is enabled or not.
289   * @return <CODE>true</CODE> if this is backend is enabled
290   * <CODE>false</CODE> otherwise.
291   */
292  public boolean isEnabled()
293  {
294    return isEnabled;
295  }
296
297  /**
298   * Set the type of pluggable backend.
299   * @param pluggableType the type of pluggable backend.
300   */
301  public void setPluggableType(PluggableType pluggableType)
302  {
303    this.pluggableType = pluggableType;
304  }
305
306  /**
307   * Get the type of pluggable backend.
308   * @return the type of pluggable backend.
309   */
310  public PluggableType getPluggableType()
311  {
312    return pluggableType;
313  }
314}