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 2015 ForgeRock AS.
026 */
027package org.opends.server.replication.common;
028
029/**
030 * The possible events for the status state machine of a DS. See StateMachine
031 * class for further details.
032 */
033public enum StatusMachineEvent
034{
035
036  /**
037   * Invalid event: special event used to be returned by some methods to signal
038   * an error.
039   */
040  INVALID_EVENT((byte) -1),
041  /**
042   * Event used when one wants the DS to enter the NOT_CONNECTED_STATUS.
043   */
044  TO_NOT_CONNECTED_STATUS_EVENT((byte) 0),
045  /**
046   * Event used when one wants the DS to enter the NORMAL_STATUS.
047   */
048  TO_NORMAL_STATUS_EVENT((byte) 1),
049  /**
050   * Event used when one wants the DS to enter the DEGRADED_STATUS.
051   */
052  TO_DEGRADED_STATUS_EVENT((byte) 2),
053  /**
054   * Event used when one wants the DS to enter the FULL_UPDATE_STATUS.
055   */
056  TO_FULL_UPDATE_STATUS_EVENT((byte) 3),
057  /**
058   * Event used when one wants the DS to enter the BAD_GEN_ID_STATUS.
059   */
060  TO_BAD_GEN_ID_STATUS_EVENT((byte) 4);
061  /** The status value. */
062  private byte value = -1;
063
064  private StatusMachineEvent(byte value)
065  {
066    this.value = value;
067  }
068
069  /**
070   * Returns the StatusMachineEvent matching the passed event numeric
071   * representation.
072   * @param value The numeric value for the event to return
073   * @return The matching StatusMachineEvent
074   * @throws java.lang.IllegalArgumentException If provided event value is
075   * wrong
076   */
077  public static StatusMachineEvent valueOf(byte value)
078    throws IllegalArgumentException
079  {
080    switch (value)
081    {
082      case 0:
083        return TO_NOT_CONNECTED_STATUS_EVENT;
084      case 1:
085        return TO_NORMAL_STATUS_EVENT;
086      case 2:
087        return TO_DEGRADED_STATUS_EVENT;
088      case 3:
089        return TO_FULL_UPDATE_STATUS_EVENT;
090      case 4:
091        return TO_BAD_GEN_ID_STATUS_EVENT;
092      default:
093        throw new IllegalArgumentException("Wrong event numeric value: "
094          + value);
095    }
096  }
097
098  /**
099   * Returns the event matching the passed requested status.
100   * When an entity receives a request to enter a particular status, this
101   * order is translated into a state machine event according to what is
102   * requested. Then, according to the current status and the computed event,
103   * the state machine retruns the matching new status
104   * (StateMachine.computeNewStatus).
105   * @param reqStatus The status to translate to an event.
106   * @return The matching event.
107   */
108  public static StatusMachineEvent statusToEvent(ServerStatus reqStatus)
109  {
110   switch (reqStatus)
111    {
112      case NOT_CONNECTED_STATUS:
113        return TO_NOT_CONNECTED_STATUS_EVENT;
114      case NORMAL_STATUS:
115        return TO_NORMAL_STATUS_EVENT;
116      case DEGRADED_STATUS:
117        return TO_DEGRADED_STATUS_EVENT;
118      case FULL_UPDATE_STATUS:
119        return TO_FULL_UPDATE_STATUS_EVENT;
120      case BAD_GEN_ID_STATUS:
121        return TO_BAD_GEN_ID_STATUS_EVENT;
122      default:
123        return INVALID_EVENT;
124    }
125  }
126
127  /**
128   * Get a numeric representation of the event.
129   * @return The numeric representation of the event
130   */
131  public byte getValue()
132  {
133    return value;
134  }
135}