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 2008 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.ServerStatus;
032
033/**
034 * This message is used by the DS to tell the RS he is changing his status
035 * (new status field used), or by the RS to tell the DS he must change his
036 * status (requested status field used).
037 */
038public class ChangeStatusMsg extends ReplicationMsg
039{
040  /** The status we want the DS to enter (used when from RS to DS). */
041  private final ServerStatus requestedStatus;
042  /** The new status the DS just entered (used when from DS to RS). */
043  private ServerStatus newStatus;
044
045  /**
046   * Create a new ChangeStatusMsg.
047   *
048   * @param requestedStatus The requested status
049   * @param newStatus The new status
050   */
051  public ChangeStatusMsg(ServerStatus requestedStatus, ServerStatus newStatus)
052  {
053    this.requestedStatus = requestedStatus;
054    this.newStatus = newStatus;
055  }
056
057  /**
058   * Creates a new ChangeStatusMsg from its encoded form.
059   *
060   * @param encodedMsg The byte array containing the encoded form of the
061   *           ChangeStatusMsg.
062   * @throws DataFormatException If the byte array does not contain a valid
063   *                             encoded form of the ChangeStatusMsg.
064   */
065  ChangeStatusMsg(byte[] encodedMsg) throws DataFormatException
066  {
067    /*
068     * The message is stored in the form:
069     * <message type><requested status><new status>
070     */
071    try
072    {
073      if (encodedMsg[0] != ReplicationMsg.MSG_TYPE_CHANGE_STATUS)
074      {
075        throw new DataFormatException("byte[] is not a valid msg");
076      }
077      requestedStatus = ServerStatus.valueOf(encodedMsg[1]);
078      newStatus = ServerStatus.valueOf(encodedMsg[2]);
079    } catch (IllegalArgumentException e)
080    {
081      throw new DataFormatException(e.getMessage());
082    }
083  }
084
085  /** {@inheritDoc} */
086  @Override
087  public byte[] getBytes(short protocolVersion)
088  {
089    /*
090     * The message is stored in the form:
091     * <message type><requested status><new status>
092     */
093    return new byte[]
094    {
095      ReplicationMsg.MSG_TYPE_CHANGE_STATUS,
096      requestedStatus.getValue(),
097      newStatus.getValue()
098    };
099  }
100
101  /**
102   * Get the requested status.
103   * @return The requested status
104   */
105  public ServerStatus getRequestedStatus()
106  {
107    return requestedStatus;
108  }
109
110  /**
111   * Get the requested status.
112   * @return The new status
113   */
114  public ServerStatus getNewStatus()
115  {
116    return newStatus;
117  }
118
119  /** {@inheritDoc} */
120  @Override
121  public String toString()
122  {
123    return "ChangeStatusMsg content:" +
124      "\nnewStatus: " + newStatus +
125      "\nrequestedStatus: " + requestedStatus;
126  }
127}