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