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