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 2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.server.replication.protocol;
028
029import java.util.zip.DataFormatException;
030
031import org.opends.server.replication.common.ServerState;
032import org.opends.server.types.DN;
033
034/**
035 * Message sent by a replication server to a directory server in reply to the
036 * ServerStartMsg.
037 */
038public class ReplServerStartDSMsg extends StartMsg
039{
040  private final int serverId;
041  private final String serverURL;
042  private final DN baseDN;
043  private final int windowSize;
044  private final ServerState serverState;
045
046  /**
047   * Whether to continue using SSL to encrypt messages after the start
048   * messages have been exchanged.
049   */
050  private final boolean sslEncryption;
051
052  /**
053   * Threshold value used by the RS to determine if a DS must be put in
054   * degraded status because the number of pending changes for him has crossed
055   * this value. This field is only used by a DS.
056   */
057  private int degradedStatusThreshold = -1;
058
059  /**
060   * The weight affected to the replication server.
061   */
062  private final int weight;
063
064  /**
065   * Number of currently connected DS to the replication server.
066   */
067  private final int connectedDSNumber;
068
069  /**
070   * Create a ReplServerStartDSMsg.
071   *
072   * @param serverId replication server id
073   * @param serverURL replication server URL
074   * @param baseDN base DN for which the ReplServerStartDSMsg is created.
075   * @param windowSize The window size.
076   * @param serverState our ServerState for this baseDN.
077   * @param generationId The generationId for this server.
078   * @param sslEncryption Whether to continue using SSL to encrypt messages
079   *                      after the start messages have been exchanged.
080   * @param groupId The group id of the RS
081   * @param degradedStatusThreshold The degraded status threshold
082   * @param weight The weight affected to the replication server.
083   * @param connectedDSNumber Number of currently connected DS to the
084   *        replication server.
085   */
086  public ReplServerStartDSMsg(int serverId, String serverURL, DN baseDN,
087                               int windowSize,
088                               ServerState serverState,
089                               long generationId,
090                               boolean sslEncryption,
091                               byte groupId,
092                               int degradedStatusThreshold,
093                               int weight,
094                               int connectedDSNumber)
095  {
096    super((short) -1 /* version set when sending */, generationId);
097    this.serverId = serverId;
098    this.serverURL = serverURL;
099    this.baseDN = baseDN;
100    this.windowSize = windowSize;
101    this.serverState = serverState;
102    this.sslEncryption = sslEncryption;
103    this.groupId = groupId;
104    this.degradedStatusThreshold = degradedStatusThreshold;
105    this.weight = weight;
106    this.connectedDSNumber = connectedDSNumber;
107  }
108
109  /**
110   * Creates a new ReplServerStartDSMsg by decoding the provided byte array.
111   * @param in A byte array containing the encoded information for the
112   *             ReplServerStartDSMsg
113   * @throws DataFormatException If the in does not contain a properly
114   *                             encoded ReplServerStartDSMsg.
115   */
116  ReplServerStartDSMsg(byte[] in) throws DataFormatException
117  {
118    final ByteArrayScanner scanner = new ByteArrayScanner(in);
119    decodeHeader(scanner, MSG_TYPE_REPL_SERVER_START_DS);
120
121    /* The ReplServerStartDSMsg payload is stored in the form :
122     * <baseDN><serverId><serverURL><windowSize><sslEncryption>
123     * <degradedStatusThreshold><weight><connectedDSNumber>
124     * <serverState>
125     */
126    baseDN = scanner.nextDN();
127    serverId = scanner.nextIntUTF8();
128    serverURL = scanner.nextString();
129    windowSize = scanner.nextIntUTF8();
130    sslEncryption = Boolean.valueOf(scanner.nextString());//FIXME
131    degradedStatusThreshold =scanner.nextIntUTF8();
132    weight = scanner.nextIntUTF8();
133    connectedDSNumber = scanner.nextIntUTF8();
134    serverState = scanner.nextServerStateMustComeLast();
135  }
136
137  /**
138   * Get the Server Id.
139   * @return the server id
140   */
141  public int getServerId()
142  {
143    return this.serverId;
144  }
145
146  /**
147   * Get the server URL.
148   * @return the server URL
149   */
150  public String getServerURL()
151  {
152    return this.serverURL;
153  }
154
155  /**
156   * Get the base DN from this ReplServerStartDSMsg.
157   *
158   * @return the base DN from this ReplServerStartDSMsg.
159   */
160  public DN getBaseDN()
161  {
162    return baseDN;
163  }
164
165  /**
166   * Get the serverState.
167   * @return Returns the serverState.
168   */
169  public ServerState getServerState()
170  {
171    return this.serverState;
172  }
173
174  /** {@inheritDoc} */
175  @Override
176  public byte[] getBytes(short protocolVersion)
177  {
178    /* The ReplServerStartDSMsg is stored in the form :
179     * <operation type><baseDN><serverId><serverURL><windowSize><sslEncryption>
180     * <degradedStatusThreshold><weight><connectedDSNumber>
181     * <serverState>
182     */
183    final ByteArrayBuilder builder = new ByteArrayBuilder();
184    encodeHeader(MSG_TYPE_REPL_SERVER_START_DS, builder, protocolVersion);
185    builder.appendDN(baseDN);
186    builder.appendIntUTF8(serverId);
187    builder.appendString(serverURL);
188    builder.appendIntUTF8(windowSize);
189    builder.appendString(Boolean.toString(sslEncryption));
190    builder.appendIntUTF8(degradedStatusThreshold);
191    builder.appendIntUTF8(weight);
192    builder.appendIntUTF8(connectedDSNumber);
193    builder.appendServerStateMustComeLast(serverState);
194    return builder.toByteArray();
195  }
196
197  /**
198   * Get the window size for the server that created this message.
199   *
200   * @return The window size for the server that created this message.
201   */
202  public int getWindowSize()
203  {
204    return windowSize;
205  }
206
207  /**
208   * Get the SSL encryption value for the server that created the
209   * message.
210   *
211   * @return The SSL encryption value for the server that created the
212   *         message.
213   */
214  public boolean getSSLEncryption()
215  {
216    return sslEncryption;
217  }
218
219  /**
220   * Get the degraded status threshold value.
221   * @return The degraded status threshold value.
222   */
223  public int getDegradedStatusThreshold()
224  {
225    return degradedStatusThreshold;
226  }
227
228  /**
229   * Set the degraded status threshold (For test purpose).
230   * @param degradedStatusThreshold The degraded status threshold to set.
231   */
232  public void setDegradedStatusThreshold(int degradedStatusThreshold)
233  {
234    this.degradedStatusThreshold = degradedStatusThreshold;
235  }
236
237  /** {@inheritDoc} */
238  @Override
239  public String toString()
240  {
241    return "ReplServerStartDSMsg content: " +
242      "\nprotocolVersion: " + protocolVersion +
243      "\ngenerationId: " + generationId +
244      "\nbaseDN: " + baseDN +
245      "\ngroupId: " + groupId +
246      "\nserverId: " + serverId +
247      "\nserverState: " + serverState +
248      "\nserverURL: " + serverURL +
249      "\nsslEncryption: " + sslEncryption +
250      "\ndegradedStatusThreshold: " + degradedStatusThreshold +
251      "\nwindowSize: " + windowSize +
252      "\nweight: " + weight +
253      "\nconnectedDSNumber: " + connectedDSNumber;
254  }
255
256  /**
257   * Gets the weight of the replication server.
258   * @return The weight of the replication server.
259   */
260  public int getWeight()
261  {
262    return weight;
263  }
264
265  /**
266   * Gets the number of directory servers connected to the replication server.
267   * @return The number of directory servers connected to the replication
268   * server.
269   */
270  public int getConnectedDSNumber()
271  {
272    return connectedDSNumber;
273  }
274
275}