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 */
026package org.opends.server.replication.common;
027
028/**
029 * This class contains static methods to implement the DS status state machine.
030 * They are used to validate the transitions of the state machine according to
031 * the current status and event, and to compute the new status.
032 * - Status (states of the state machine) are defined in ServerStatus enum
033 * - Events are defined in StateMachineEvent enum
034 */
035public class StatusMachine
036{
037
038  /**
039   * Checks if a given status is valid as an entering status for the state
040   * machine.
041   * @param initStatus Initial status to check.
042   * @return True if the passed status is a valid initial status.
043   */
044  public static boolean isValidInitialStatus(ServerStatus initStatus)
045  {
046    switch (initStatus)
047    {
048      case NORMAL_STATUS:
049      case DEGRADED_STATUS:
050      case BAD_GEN_ID_STATUS:
051        return true;
052    }
053
054    return false;
055  }
056
057  /**
058   * Computes the new status of the state machine according to the current
059   * status and the new generated event.
060   * @param curStatus The current status we start from.
061   * @param event The event that must make the current status evolve.
062   * @return The newly computed status. If the state transition is impossible
063   * according to state machine, special INVALID_STATUS is returned.
064   */
065  public static ServerStatus computeNewStatus(ServerStatus curStatus,
066    StatusMachineEvent event)
067  {
068    switch (curStatus)
069    {
070      // From NOT_CONNECTED_STATUS
071      case NOT_CONNECTED_STATUS:
072        switch (event)
073        {
074          case TO_NOT_CONNECTED_STATUS_EVENT:
075            return ServerStatus.NOT_CONNECTED_STATUS;
076          case TO_NORMAL_STATUS_EVENT:
077            return ServerStatus.NORMAL_STATUS;
078          case TO_DEGRADED_STATUS_EVENT:
079            return ServerStatus.DEGRADED_STATUS;
080          case TO_BAD_GEN_ID_STATUS_EVENT:
081            return ServerStatus.BAD_GEN_ID_STATUS;
082          default:
083            return ServerStatus.INVALID_STATUS;
084        }
085      // From NORMAL_STATUS
086      case NORMAL_STATUS:
087        switch (event)
088        {
089          case TO_NOT_CONNECTED_STATUS_EVENT:
090            return ServerStatus.NOT_CONNECTED_STATUS;
091          case TO_NORMAL_STATUS_EVENT:
092            return ServerStatus.NORMAL_STATUS;
093          case TO_DEGRADED_STATUS_EVENT:
094            return ServerStatus.DEGRADED_STATUS;
095          case TO_FULL_UPDATE_STATUS_EVENT:
096            return ServerStatus.FULL_UPDATE_STATUS;
097          case TO_BAD_GEN_ID_STATUS_EVENT:
098            return ServerStatus.BAD_GEN_ID_STATUS;
099          default:
100            return ServerStatus.INVALID_STATUS;
101        }
102      // From DEGRADED_STATUS
103      case DEGRADED_STATUS:
104        switch (event)
105        {
106          case TO_NOT_CONNECTED_STATUS_EVENT:
107            return ServerStatus.NOT_CONNECTED_STATUS;
108          case TO_NORMAL_STATUS_EVENT:
109            return ServerStatus.NORMAL_STATUS;
110          case TO_DEGRADED_STATUS_EVENT:
111            return ServerStatus.DEGRADED_STATUS;
112          case TO_FULL_UPDATE_STATUS_EVENT:
113            return ServerStatus.FULL_UPDATE_STATUS;
114          case TO_BAD_GEN_ID_STATUS_EVENT:
115            return ServerStatus.BAD_GEN_ID_STATUS;
116          default:
117            return ServerStatus.INVALID_STATUS;
118        }
119      // From FULL_UPDATE_STATUS
120      case FULL_UPDATE_STATUS:
121        switch (event)
122        {
123          case TO_NOT_CONNECTED_STATUS_EVENT:
124            return ServerStatus.NOT_CONNECTED_STATUS;
125          case TO_FULL_UPDATE_STATUS_EVENT:
126            return ServerStatus.FULL_UPDATE_STATUS;
127          default:
128            return ServerStatus.INVALID_STATUS;
129        }
130      // From BAD_GEN_ID_STATUS
131      case BAD_GEN_ID_STATUS:
132        switch (event)
133        {
134          case TO_NOT_CONNECTED_STATUS_EVENT:
135            return ServerStatus.NOT_CONNECTED_STATUS;
136          case TO_FULL_UPDATE_STATUS_EVENT:
137            return ServerStatus.FULL_UPDATE_STATUS;
138          case TO_BAD_GEN_ID_STATUS_EVENT:
139            return ServerStatus.BAD_GEN_ID_STATUS;
140          default:
141            return ServerStatus.INVALID_STATUS;
142        }
143      default:
144        return ServerStatus.INVALID_STATUS;
145    }
146  }
147}