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