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 Sun Microsystems, Inc.
015 * Portions Copyright 2015 ForgeRock AS.
016 */
017package org.opends.admin.ads;
018
019import java.util.HashSet;
020import java.util.Set;
021
022/**
023 * The object of this class represent a topology of replicas across servers that
024 * have the same suffix DN. If there is more than one replica on the suffix, the
025 * contents of the replicas are replicated.
026 */
027public class SuffixDescriptor
028{
029  private String suffixDN;
030  private final Set<ReplicaDescriptor> replicas = new HashSet<>();
031
032  /**
033   * Returns the DN associated with this suffix descriptor.
034   *
035   * @return the DN associated with this suffix descriptor.
036   */
037  public String getDN()
038  {
039    return suffixDN;
040  }
041
042  /**
043   * Sets the DN associated with this suffix descriptor.
044   *
045   * @param suffixDN
046   *          the DN associated with this suffix descriptor.
047   */
048  public void setDN(String suffixDN)
049  {
050    this.suffixDN = suffixDN;
051  }
052
053  /**
054   * Returns the replicas associated with this SuffixDescriptor.
055   *
056   * @return a Set containing the replicas associated with this
057   *         SuffixDescriptor.
058   */
059  public Set<ReplicaDescriptor> getReplicas()
060  {
061    return new HashSet<>(replicas);
062  }
063
064  /**
065   * Sets the replicas associated with this SuffixDescriptor.
066   *
067   * @param replicas
068   *          a Set containing the replicas associated with this
069   *          SuffixDescriptor.
070   */
071  public void setReplicas(Set<ReplicaDescriptor> replicas)
072  {
073    this.replicas.clear();
074    this.replicas.addAll(replicas);
075  }
076
077  /**
078   * Returns the Set of Replication servers for the whole suffix topology. The
079   * servers are provided in their String representation.
080   *
081   * @return the Set of Replication servers for the whole suffix topology.
082   */
083  public Set<String> getReplicationServers()
084  {
085    Set<String> replicationServers = new HashSet<>();
086    for (ReplicaDescriptor replica : getReplicas())
087    {
088      replicationServers.addAll(replica.getReplicationServers());
089    }
090    return replicationServers;
091  }
092
093  @Override
094  public int hashCode()
095  {
096    return getId().hashCode();
097  }
098
099  /**
100   * Returns an Id that is unique for this suffix.
101   *
102   * @return an Id that is unique for this suffix.
103   */
104  public String getId()
105  {
106    StringBuilder buf = new StringBuilder();
107    buf.append(getDN());
108    for (ReplicaDescriptor replica : getReplicas())
109    {
110      buf.append("-").append(replica.getServer().getId());
111    }
112
113    return buf.toString();
114  }
115}