AMBARI-14496. Re-factor ServiceState routine inside ServiceResourceProvider 
(dgrinenko via dmitriusan)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/ba2b9700
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/ba2b9700
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/ba2b9700

Branch: refs/heads/trunk
Commit: ba2b970012ad0b7151fb19b7552869f8d6ddce16
Parents: 7f29162
Author: Lisnichenko Dmitro <dlysniche...@hortonworks.com>
Authored: Tue Jan 5 17:32:36 2016 +0200
Committer: Lisnichenko Dmitro <dlysniche...@hortonworks.com>
Committed: Tue Jan 5 17:33:19 2016 +0200

----------------------------------------------------------------------
 .../ambari/server/actionmanager/Stage.java      |    4 +-
 .../AmbariCustomCommandExecutionHelper.java     |    4 +-
 .../controller/AmbariManagementController.java  |  168 +-
 .../internal/ServiceResourceProvider.java       |  559 +------
 .../ServiceCalculatedStateFactory.java          |   90 ++
 .../state/DefaultServiceCalculatedState.java    |  171 ++
 .../state/FlumeServiceCalculatedState.java      |   69 +
 .../state/HBaseServiceCalculatedState.java      |   96 ++
 .../state/HDFSServiceCalculatedState.java       |  115 ++
 .../state/HiveServiceCalculatedState.java       |  111 ++
 .../state/OozieServiceCalculatedState.java      |   96 ++
 .../utilities/state/ServiceCalculatedState.java |   28 +
 .../state/YARNServiceCalculatedState.java       |  102 ++
 .../api/services/ComponentServiceTest.java      |    2 +-
 .../internal/ServiceResourceProviderTest.java   | 1498 ------------------
 .../DefaultServiceCalculatedStateTest.java      |   86 +
 .../state/FlumeServiceCalculatedStateTest.java  |   80 +
 .../GeneralServiceCalculatedStateTest.java      |  144 ++
 .../state/HBaseServiceCalculatedStateTest.java  |   90 ++
 .../state/HDFSServiceCalculatedStateTest.java   |   90 ++
 .../state/HiveServiceCalculatedStateTest.java   |  109 ++
 .../state/OozieServiceCalculatedStateTest.java  |   84 +
 .../state/YarnServiceCalculatedStateTest.java   |   90 ++
 23 files changed, 1743 insertions(+), 2143 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/Stage.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/Stage.java 
b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/Stage.java
index d0cc843..5a313d8 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/Stage.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/Stage.java
@@ -319,7 +319,7 @@ public class Stage {
 
     Map<String, HostRoleCommand> hrcMap = hostRoleCommands.get(hostName);
     if (hrcMap == null) {
-      hrcMap = new LinkedHashMap<String, HostRoleCommand>();
+      hrcMap = new LinkedHashMap<>();
       hostRoleCommands.put(hostName, hrcMap);
     }
     if (hrcMap.get(role.toString()) != null) {
@@ -330,7 +330,7 @@ public class Stage {
     hrcMap.put(role.toString(), hrc);
     List<ExecutionCommandWrapper> execCmdList = commandsToSend.get(hostName);
     if (execCmdList == null) {
-      execCmdList = new ArrayList<ExecutionCommandWrapper>();
+      execCmdList = new ArrayList<>();
       commandsToSend.put(hostName, execCmdList);
     }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
index 2ed8f9d..1eb9ee5 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
@@ -845,9 +845,7 @@ public class AmbariCustomCommandExecutionHelper {
 
       if (!serviceName.equals(Service.Type.HBASE.name()) || 
hostName.equals(primaryCandidate)) {
         commandParams.put(UPDATE_EXCLUDE_FILE_ONLY, "false");
-        addCustomCommandAction(commandContext, commandFilter, stage,
- commandParams,
-            commandDetail.toString());
+        addCustomCommandAction(commandContext, commandFilter, stage, 
commandParams, commandDetail.toString());
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
index b80488f..cbc5a95 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
@@ -64,7 +64,7 @@ public interface AmbariManagementController {
    *
    * @return the Ambari endpoint URI
    */
-  public String getAmbariServerURI(String path);
+  String getAmbariServerURI(String path);
 
 
   // ----- Create -----------------------------------------------------------
@@ -77,7 +77,7 @@ public interface AmbariManagementController {
    * @throws AmbariException thrown if the cluster cannot be created
    * @throws AuthorizationException thrown if the authenticated user is not 
authorized to perform this operation
    */
-  public void createCluster(ClusterRequest request) throws AmbariException, 
AuthorizationException;
+  void createCluster(ClusterRequest request) throws AmbariException, 
AuthorizationException;
 
   /**
    * Create the host component defined by the attributes in the given request 
object.
@@ -86,7 +86,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException thrown if the host component cannot be created
    */
-  public void createHostComponents(
+  void createHostComponents(
       Set<ServiceComponentHostRequest> requests) throws AmbariException, 
AuthorizationException;
 
   /**
@@ -96,7 +96,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException when the configuration cannot be created.
    */
-  public ConfigurationResponse createConfiguration(ConfigurationRequest 
request)
+  ConfigurationResponse createConfiguration(ConfigurationRequest request)
       throws AmbariException, AuthorizationException;
 
   /**
@@ -114,7 +114,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException when the user cannot be created.
    */
-  public void createUsers(Set<UserRequest> requests) throws AmbariException;
+  void createUsers(Set<UserRequest> requests) throws AmbariException;
 
   /**
    * Creates groups.
@@ -123,7 +123,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException when the groups cannot be created.
    */
-  public void createGroups(Set<GroupRequest> requests) throws AmbariException;
+  void createGroups(Set<GroupRequest> requests) throws AmbariException;
 
   /**
    * Creates members of the group.
@@ -132,7 +132,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException when the members cannot be created.
    */
-  public void createMembers(Set<MemberRequest> requests) throws 
AmbariException;
+  void createMembers(Set<MemberRequest> requests) throws AmbariException;
 
 
   // ----- Read -------------------------------------------------------------
@@ -147,7 +147,7 @@ public interface AmbariManagementController {
    * @throws AmbariException thrown if the resource cannot be read
    * @throws AuthorizationException thrown if the authenticated user is not 
authorized to perform this operation
    */
-  public Set<ClusterResponse> getClusters(Set<ClusterRequest> requests)
+  Set<ClusterResponse> getClusters(Set<ClusterRequest> requests)
       throws AmbariException, AuthorizationException;
 
   /**
@@ -160,7 +160,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException thrown if the resource cannot be read
    */
-  public Set<ServiceComponentHostResponse> getHostComponents(
+  Set<ServiceComponentHostResponse> getHostComponents(
       Set<ServiceComponentHostRequest> requests) throws AmbariException;
 
   /**
@@ -172,7 +172,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the configurations could not be read
    */
-  public Set<ConfigurationResponse> getConfigurations(
+  Set<ConfigurationResponse> getConfigurations(
       Set<ConfigurationRequest> requests) throws AmbariException;
 
   /**
@@ -193,7 +193,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the users could not be read
    */
-  public Set<UserResponse> getUsers(Set<UserRequest> requests)
+  Set<UserResponse> getUsers(Set<UserRequest> requests)
       throws AmbariException, AuthorizationException;
 
   /**
@@ -205,7 +205,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the groups could not be read
    */
-  public Set<GroupResponse> getGroups(Set<GroupRequest> requests)
+  Set<GroupResponse> getGroups(Set<GroupRequest> requests)
       throws AmbariException;
 
   /**
@@ -217,7 +217,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the members could not be read
    */
-  public Set<MemberResponse> getMembers(Set<MemberRequest> requests)
+  Set<MemberResponse> getMembers(Set<MemberRequest> requests)
       throws AmbariException;
 
 
@@ -237,7 +237,7 @@ public interface AmbariManagementController {
    * @throws AmbariException thrown if the resource cannot be updated
    * @throws AuthorizationException thrown if the authenticated user is not 
authorized to perform this operation
    */
-  public RequestStatusResponse updateClusters(Set<ClusterRequest> requests,
+  RequestStatusResponse updateClusters(Set<ClusterRequest> requests,
                                               Map<String, String> 
requestProperties)
       throws AmbariException, AuthorizationException;
 
@@ -248,7 +248,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the resources cannot be updated
    */
-  public void updateUsers(Set<UserRequest> requests) throws AmbariException, 
AuthorizationException;
+  void updateUsers(Set<UserRequest> requests) throws AmbariException, 
AuthorizationException;
 
   /**
    * Updates the groups specified.
@@ -257,7 +257,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the resources cannot be updated
    */
-  public void updateGroups(Set<GroupRequest> requests) throws AmbariException;
+  void updateGroups(Set<GroupRequest> requests) throws AmbariException;
 
   /**
    * Updates the members of the group specified.
@@ -266,7 +266,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the resources cannot be updated
    */
-  public void updateMembers(Set<MemberRequest> requests) throws 
AmbariException;
+  void updateMembers(Set<MemberRequest> requests) throws AmbariException;
 
 
   // ----- Delete -----------------------------------------------------------
@@ -278,7 +278,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException thrown if the resource cannot be deleted
    */
-  public void deleteCluster(ClusterRequest request) throws AmbariException;
+  void deleteCluster(ClusterRequest request) throws AmbariException;
 
   /**
    * Delete the host component identified by the given request object.
@@ -289,7 +289,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException thrown if the resource cannot be deleted
    */
-  public RequestStatusResponse deleteHostComponents(
+  RequestStatusResponse deleteHostComponents(
       Set<ServiceComponentHostRequest> requests) throws AmbariException, 
AuthorizationException;
 
   /**
@@ -299,7 +299,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the resources cannot be deleted
    */
-  public void deleteUsers(Set<UserRequest> requests) throws AmbariException;
+  void deleteUsers(Set<UserRequest> requests) throws AmbariException;
 
   /**
    * Deletes the user groups specified.
@@ -308,7 +308,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the resources cannot be deleted
    */
-  public void deleteGroups(Set<GroupRequest> requests) throws AmbariException;
+  void deleteGroups(Set<GroupRequest> requests) throws AmbariException;
 
   /**
    * Deletes the group members specified.
@@ -317,7 +317,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if the resources cannot be deleted
    */
-  public void deleteMembers(Set<MemberRequest> requests) throws 
AmbariException;
+  void deleteMembers(Set<MemberRequest> requests) throws AmbariException;
 
   /**
    * Create the action defined by the attributes in the given request object.
@@ -328,7 +328,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException thrown if the action cannot be created
    */
-  public RequestStatusResponse createAction(ExecuteActionRequest 
actionRequest, Map<String, String> requestProperties)
+  RequestStatusResponse createAction(ExecuteActionRequest actionRequest, 
Map<String, String> requestProperties)
       throws AmbariException;
 
   /**
@@ -340,7 +340,7 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if the resources cannot be read
    */
-  public Set<StackResponse> getStacks(Set<StackRequest> requests) throws 
AmbariException;
+  Set<StackResponse> getStacks(Set<StackRequest> requests) throws 
AmbariException;
 
   /**
    * Update stacks from the files at stackRoot.
@@ -348,7 +348,7 @@ public interface AmbariManagementController {
    * @return a track action response
    * @throws AmbariException if
    */
-  public RequestStatusResponse updateStacks() throws AmbariException;
+  RequestStatusResponse updateStacks() throws AmbariException;
 
   /**
    * Get supported stacks versions.
@@ -359,7 +359,7 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if the resources cannot be read
    */
-  public Set<StackVersionResponse> getStackVersions(Set<StackVersionRequest> 
requests) throws AmbariException;
+  Set<StackVersionResponse> getStackVersions(Set<StackVersionRequest> 
requests) throws AmbariException;
 
 
   /**
@@ -371,7 +371,7 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if the resources cannot be read
    */
-  public Set<RepositoryResponse> getRepositories(Set<RepositoryRequest> 
requests) throws AmbariException;
+  Set<RepositoryResponse> getRepositories(Set<RepositoryRequest> requests) 
throws AmbariException;
 
   /**
    * Updates repositories by stack name, version and operating system.
@@ -380,7 +380,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException
    */
-  public void updateRepositories(Set<RepositoryRequest> requests) throws 
AmbariException;
+  void updateRepositories(Set<RepositoryRequest> requests) throws 
AmbariException;
 
   /**
    * Verifies repositories' base urls.
@@ -389,7 +389,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if verification of any of urls fails
    */
-  public void verifyRepositories(Set<RepositoryRequest> requests) throws 
AmbariException;
+  void verifyRepositories(Set<RepositoryRequest> requests) throws 
AmbariException;
 
   /**
    * Get repositories by stack name, version.
@@ -400,7 +400,7 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if the resources cannot be read
    */
-  public Set<StackServiceResponse> getStackServices(Set<StackServiceRequest> 
requests) throws AmbariException;
+  Set<StackServiceResponse> getStackServices(Set<StackServiceRequest> 
requests) throws AmbariException;
 
 
   /**
@@ -412,7 +412,7 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if the resources cannot be read
    */
-  public Set<StackConfigurationResponse> 
getStackConfigurations(Set<StackConfigurationRequest> requests) throws 
AmbariException;
+  Set<StackConfigurationResponse> 
getStackConfigurations(Set<StackConfigurationRequest> requests) throws 
AmbariException;
 
 
   /**
@@ -424,7 +424,7 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if the resources cannot be read
    */
-  public Set<StackServiceComponentResponse> 
getStackComponents(Set<StackServiceComponentRequest> requests) throws 
AmbariException;
+  Set<StackServiceComponentResponse> 
getStackComponents(Set<StackServiceComponentRequest> requests) throws 
AmbariException;
 
 
   /**
@@ -436,7 +436,7 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if the resources cannot be read
    */
-  public Set<OperatingSystemResponse> 
getOperatingSystems(Set<OperatingSystemRequest> requests) throws 
AmbariException;
+  Set<OperatingSystemResponse> getOperatingSystems(Set<OperatingSystemRequest> 
requests) throws AmbariException;
 
   /**
    * Get all top-level services of Ambari, not related to certain cluster.
@@ -448,7 +448,7 @@ public interface AmbariManagementController {
    * @throws  AmbariException if the resources cannot be read
    */
 
-  public Set<RootServiceResponse> getRootServices(Set<RootServiceRequest> 
requests) throws AmbariException;
+  Set<RootServiceResponse> getRootServices(Set<RootServiceRequest> requests) 
throws AmbariException;
   /**
    * Get all components of top-level services of Ambari, not related to 
certain cluster.
    *
@@ -458,7 +458,7 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if the resources cannot be read
    */
-  public Set<RootServiceComponentResponse> 
getRootServiceComponents(Set<RootServiceComponentRequest> requests) throws 
AmbariException;
+  Set<RootServiceComponentResponse> 
getRootServiceComponents(Set<RootServiceComponentRequest> requests) throws 
AmbariException;
 
 
   // ----- Common utility methods --------------------------------------------
@@ -473,14 +473,14 @@ public interface AmbariManagementController {
    *
    * @throws  AmbariException if service name is null or empty
    */
-  public String findServiceName(Cluster cluster, String componentName) throws 
AmbariException;
+  String findServiceName(Cluster cluster, String componentName) throws 
AmbariException;
 
   /**
    * Get the clusters for this management controller.
    *
    * @return the clusters
    */
-  public Clusters getClusters();
+  Clusters getClusters();
 
   /**
    * Get config helper
@@ -494,56 +494,56 @@ public interface AmbariManagementController {
    *
    * @return the meta info
    */
-  public AmbariMetaInfo getAmbariMetaInfo();
+  AmbariMetaInfo getAmbariMetaInfo();
 
   /**
    * Get the service factory for this management controller.
    *
    * @return the service factory
    */
-  public ServiceFactory getServiceFactory();
+  ServiceFactory getServiceFactory();
 
   /**
    * Get the service component factory for this management controller.
    *
    * @return the service component factory
    */
-  public ServiceComponentFactory getServiceComponentFactory();
+  ServiceComponentFactory getServiceComponentFactory();
 
   /**
    * Get the root service response factory for this management controller.
    *
    * @return the root service response factory
    */
-  public AbstractRootServiceResponseFactory getRootServiceResponseFactory();
+  AbstractRootServiceResponseFactory getRootServiceResponseFactory();
 
   /**
    * Get the config group factory for this management controller.
    *
    * @return the config group factory
    */
-  public ConfigGroupFactory getConfigGroupFactory();
+  ConfigGroupFactory getConfigGroupFactory();
 
   /**
    * Get the role graph factory for this management controller.
    *
    * @return the role graph factory
    */
-  public RoleGraphFactory getRoleGraphFactory();
+  RoleGraphFactory getRoleGraphFactory();
 
   /**
     * Get the action manager for this management controller.
     *
     * @return the action manager
     */
-  public ActionManager getActionManager();
+  ActionManager getActionManager();
 
   /**
    * Get the authenticated user's name.
    *
    * @return the authenticated user's name
    */
-  public String getAuthName();
+  String getAuthName();
 
   /**
    * Create and persist the request stages and return a response containing the
@@ -563,14 +563,14 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException is thrown if the stages can not be created
    */
-  public RequestStatusResponse createAndPersistStages(Cluster cluster, 
Map<String, String> requestProperties,
-                                                      Map<String, String> 
requestParameters,
-                                                      Map<State, 
List<Service>> changedServices,
-                                                      Map<State, 
List<ServiceComponent>> changedComponents,
-                                                      Map<String, Map<State, 
List<ServiceComponentHost>>> changedHosts,
-                                                      
Collection<ServiceComponentHost> ignoredHosts,
-                                                      boolean runSmokeTest, 
boolean reconfigureClients)
-                                                      throws AmbariException;
+  RequestStatusResponse createAndPersistStages(Cluster cluster, Map<String, 
String> requestProperties,
+                                                Map<String, String> 
requestParameters,
+                                                Map<State, List<Service>> 
changedServices,
+                                                Map<State, 
List<ServiceComponent>> changedComponents,
+                                                Map<String, Map<State, 
List<ServiceComponentHost>>> changedHosts,
+                                                
Collection<ServiceComponentHost> ignoredHosts,
+                                                boolean runSmokeTest, boolean 
reconfigureClients)
+                                                throws AmbariException;
 
   /**
    * Add stages to the request.
@@ -590,54 +590,54 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if stages can't be created
    */
-  public RequestStageContainer addStages(RequestStageContainer requestStages, 
Cluster cluster, Map<String, String> requestProperties,
-                                 Map<String, String> requestParameters,
-                                 Map<State, List<Service>> changedServices,
-                                 Map<State, List<ServiceComponent>> 
changedComponents,
-                                 Map<String, Map<State, 
List<ServiceComponentHost>>> changedHosts,
-                                 Collection<ServiceComponentHost> ignoredHosts,
-                                 boolean runSmokeTest, boolean 
reconfigureClients) throws AmbariException;
+  RequestStageContainer addStages(RequestStageContainer requestStages, Cluster 
cluster, Map<String, String> requestProperties,
+                             Map<String, String> requestParameters,
+                             Map<State, List<Service>> changedServices,
+                             Map<State, List<ServiceComponent>> 
changedComponents,
+                             Map<String, Map<State, 
List<ServiceComponentHost>>> changedHosts,
+                             Collection<ServiceComponentHost> ignoredHosts,
+                             boolean runSmokeTest, boolean reconfigureClients) 
throws AmbariException;
 
   /**
    * Getter for the url of JDK, stored at server resources folder
    */
-  public String getJdkResourceUrl();
+  String getJdkResourceUrl();
 
   /**
    * Getter for the java home, stored in ambari.properties
    */
-  public String getJavaHome();
+  String getJavaHome();
 
   /**
    * Getter for the jdk name, stored in ambari.properties
    */
-  public String getJDKName();
+  String getJDKName();
 
   /**
    * Getter for the jce name, stored in ambari.properties
    */
-  public String getJCEName();
+  String getJCEName();
 
   /**
    * Getter for the name of server database
    */
-  public String getServerDB();
+  String getServerDB();
 
   /**
    * Getter for the url of Oracle JDBC driver, stored at server resources 
folder
    */
-  public String getOjdbcUrl();
+  String getOjdbcUrl();
 
   /**
    * Getter for the url of MySQL JDBC driver, stored at server resources folder
    */
-  public String getMysqljdbcUrl();
+  String getMysqljdbcUrl();
 
   /**
    * Return a healthy host if found otherwise any random host
    * @throws AmbariException
    */
-  public String getHealthyHost(Set<String> hostList) throws AmbariException;
+  String getHealthyHost(Set<String> hostList) throws AmbariException;
 
 
   /**
@@ -650,8 +650,8 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if configuration tags can not be obtained
    */
-  public Map<String, Map<String,String>> findConfigurationTagsWithOverrides(
-          Cluster cluster, String hostName) throws AmbariException;
+  Map<String, Map<String,String>> findConfigurationTagsWithOverrides(
+        Cluster cluster, String hostName) throws AmbariException;
 
   /**
    * Returns parameters for RCA database
@@ -659,18 +659,18 @@ public interface AmbariManagementController {
    * @return the map with parameters for RCA db
    *
    */
-  public Map<String, String> getRcaParameters();
+  Map<String, String> getRcaParameters();
 
   /**
    * Get the Factory to create Request schedules
    * @return the request execution factory
    */
-  public RequestExecutionFactory getRequestExecutionFactory();
+  RequestExecutionFactory getRequestExecutionFactory();
 
   /**
    * Get Execution Schedule Manager
    */
-  public ExecutionScheduleManager getExecutionScheduleManager();
+  ExecutionScheduleManager getExecutionScheduleManager();
 
   /**
    * Get cached clusterUpdateResults, used only for service config versions 
currently
@@ -682,7 +682,7 @@ public interface AmbariManagementController {
   /**
    * Get JobTracker hostname
    */
-  public String getJobTrackerHost(Cluster cluster);
+  String getJobTrackerHost(Cluster cluster);
 
   /**
    * Gets the effective maintenance state for a host component
@@ -690,20 +690,20 @@ public interface AmbariManagementController {
    * @return the maintenance state
    * @throws AmbariException
    */
-  public MaintenanceState getEffectiveMaintenanceState(ServiceComponentHost 
sch)
+  MaintenanceState getEffectiveMaintenanceState(ServiceComponentHost sch)
       throws AmbariException;
 
   /**
    * Get Role Command Order
    */
-  public RoleCommandOrder getRoleCommandOrder(Cluster cluster);
+  RoleCommandOrder getRoleCommandOrder(Cluster cluster);
 
   /**
    * Performs a test if LDAP server is reachable.
    *
    * @return true if connection to LDAP was established
    */
-  public boolean checkLdapConfigured();
+  boolean checkLdapConfigured();
 
   /**
    * Retrieves groups and users from external LDAP.
@@ -711,7 +711,7 @@ public interface AmbariManagementController {
    * @return ldap sync DTO
    * @throws AmbariException if LDAP is configured incorrectly
    */
-  public LdapSyncDto getLdapSyncInfo() throws AmbariException;
+  LdapSyncDto getLdapSyncInfo() throws AmbariException;
 
   /**
    * Synchronizes local users and groups with given data.
@@ -723,7 +723,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if synchronization data was invalid
    */
-  public LdapBatchDto synchronizeLdapUsersAndGroups(
+  LdapBatchDto synchronizeLdapUsersAndGroups(
       LdapSyncRequest userRequest, LdapSyncRequest groupRequest) throws 
AmbariException;
 
   /**
@@ -731,7 +731,7 @@ public interface AmbariManagementController {
    *
    * @return true if LDAP sync is in progress
    */
-  public boolean isLdapSyncInProgress();
+  boolean isLdapSyncInProgress();
 
   /**
    * Get configurations which are specific for a cluster (!not a service).
@@ -739,7 +739,7 @@ public interface AmbariManagementController {
    * @return
    * @throws AmbariException
    */
-  public Set<StackConfigurationResponse> 
getStackLevelConfigurations(Set<StackLevelConfigurationRequest> requests) 
throws AmbariException;
+  Set<StackConfigurationResponse> 
getStackLevelConfigurations(Set<StackLevelConfigurationRequest> requests) 
throws AmbariException;
 
   /**
    * @param serviceInfo service info for a given service
@@ -758,7 +758,7 @@ public interface AmbariManagementController {
    *
    * @throws AmbariException if an error occurs during the rack change 
registration
    */
-  public void registerRackChange(String clusterName) throws AmbariException;
+  void registerRackChange(String clusterName) throws AmbariException;
 
   /**
    * Initialize cluster scoped widgets and widgetLayouts for different stack
@@ -767,13 +767,13 @@ public interface AmbariManagementController {
    * @param cluster @Cluster object
    * @param service @Service object
    */
-  public void initializeWidgetsAndLayouts(Cluster cluster, Service service) 
throws AmbariException;
+  void initializeWidgetsAndLayouts(Cluster cluster, Service service) throws 
AmbariException;
 
   /**
    * Gets an execution command for host component life cycle command
    * @return
    */
-  public ExecutionCommand getExecutionCommand(Cluster cluster,
+  ExecutionCommand getExecutionCommand(Cluster cluster,
                                               ServiceComponentHost scHost,
                                               RoleCommand roleCommand) throws 
AmbariException;
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
index 815f006..a2aca70 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
@@ -32,8 +32,6 @@ import 
org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.KerberosHelper;
 import org.apache.ambari.server.controller.MaintenanceStateHelper;
 import org.apache.ambari.server.controller.RequestStatusResponse;
-import org.apache.ambari.server.controller.ServiceComponentHostRequest;
-import org.apache.ambari.server.controller.ServiceComponentHostResponse;
 import org.apache.ambari.server.controller.ServiceRequest;
 import org.apache.ambari.server.controller.ServiceResponse;
 import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
@@ -46,6 +44,8 @@ import 
org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import 
org.apache.ambari.server.controller.utilities.ServiceCalculatedStateFactory;
+import 
org.apache.ambari.server.controller.utilities.state.ServiceCalculatedState;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.security.authorization.AuthorizationHelper;
@@ -56,7 +56,6 @@ import 
org.apache.ambari.server.serveraction.kerberos.KerberosInvalidConfigurati
 import 
org.apache.ambari.server.serveraction.kerberos.KerberosMissingAdminCredentialsException;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
@@ -68,7 +67,6 @@ import org.apache.commons.lang.StringUtils;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.EnumMap;
 import java.util.EnumSet;
 import java.util.HashMap;
@@ -109,18 +107,6 @@ public class ServiceResourceProvider extends 
AbstractControllerResourceProvider
           SERVICE_CLUSTER_NAME_PROPERTY_ID,
           SERVICE_SERVICE_NAME_PROPERTY_ID}));
 
-  // Service state calculation
-  private static final Map<String, ServiceState> serviceStateMap = new 
HashMap<String, ServiceState>();
-  static {
-    serviceStateMap.put("HDFS", new HDFSServiceState());
-    serviceStateMap.put("HBASE", new HBaseServiceState());
-    serviceStateMap.put("FLUME", new FlumeServiceState());
-    serviceStateMap.put("HIVE", new HiveServiceState());
-    serviceStateMap.put("OOZIE", new OozieServiceState());
-    serviceStateMap.put("YARN", new YARNServiceState());
-  }
-
-  private static final ServiceState DEFAULT_SERVICE_STATE = new 
DefaultServiceState();
 
   private MaintenanceStateHelper maintenanceStateHelper;
 
@@ -915,548 +901,11 @@ public class ServiceResourceProvider extends 
AbstractControllerResourceProvider
     return null;
   }
 
-  // Get the State of a host component
-  private static State getHostComponentState(ServiceComponentHostResponse 
hostComponent) {
-    return State.valueOf(hostComponent.getLiveState());
-  }
-
   // calculate the service state, accounting for the state of the host 
components
   private String calculateServiceState(String clusterName, String serviceName) 
{
-
-    ServiceState serviceState = serviceStateMap.get(serviceName);
-    if (serviceState == null) {
-      serviceState = DEFAULT_SERVICE_STATE;
-    }
-    State state = serviceState.getState(getManagementController(), 
clusterName, serviceName);
-
-    return state.toString();
-  }
-
-
-  // ----- inner class ServiceState ------------------------------------------
-
-  /**
-   * Interface to allow for different state calculations for different 
services.
-   * TODO : see if this functionality can be moved to service definitions.
-   */
-  protected interface ServiceState {
-    public State getState(AmbariManagementController controller, String 
clusterName, String serviceName);
-  }
-
-  /**
-   * Default calculator of service state.
-   * The following rules should apply :
-   * For services that have all components DISABLED, the service state should 
be DISABLED.
-   * For services that have any master components, the service state should
-   * be STARTED if all master components are STARTED.
-   * For services that have all client components, the service state should
-   * be INSTALLED if all of the components are INSTALLED.
-   * For all other cases the state of the service should match the highest 
state of all
-   * of its component states or UNKNOWN if the component states can not be 
determined.
-   */
-  protected static class DefaultServiceState implements ServiceState {
-
-    @Override
-    public State getState(AmbariManagementController controller, String 
clusterName, String serviceName) {
-      AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
-      Clusters       clusters       = controller.getClusters();
-
-      if (clusterName != null && clusterName.length() > 0) {
-        try {
-          Cluster cluster = clusters.getCluster(clusterName);
-          if (cluster != null) {
-            StackId stackId = cluster.getDesiredStackVersion();
-
-            ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
-                serviceName, null, null, null);
-
-            Set<ServiceComponentHostResponse> hostComponentResponses =
-                controller.getHostComponents(Collections.singleton(request));
-
-            State   masterState = null;
-            State   clientState = null;
-            State   otherState = null;
-            State   maxMMState = null; // The worst state among components in 
MM
-
-            boolean hasDisabled  = false;
-            boolean hasMaster    = false;
-            boolean hasOther     = false;
-            boolean hasClient    = false;
-            boolean hasMM        = false;
-
-            for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
-              try {
-                ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
-                    stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
-                    hostComponentResponse.getComponentName());
-
-                State state = getHostComponentState(hostComponentResponse);
-                // Components in MM should not affect service status,
-                // so we tend to ignore them
-                boolean isInMaintenance = ! MaintenanceState.OFF.toString().
-                    equals(hostComponentResponse.getMaintenanceState());
-
-                if (state.equals(State.DISABLED)) {
-                  hasDisabled = true;
-                }
-
-                if (isInMaintenance & !componentInfo.isClient()) {
-                  hasMM = true;
-                  if ( maxMMState == null || state.ordinal() > 
maxMMState.ordinal()) {
-                    maxMMState = state;
-                  }
-                }
-
-                if (componentInfo.isMaster()) {
-                  if (state.equals(State.STARTED) || ! isInMaintenance) {
-                    // We rely on master's state to determine service state
-                    hasMaster = true;
-                  }
-
-                  if (! state.equals(State.STARTED) &&
-                      ! isInMaintenance &&  // Ignore status of MM component
-                      ( masterState == null || state.ordinal() > 
masterState.ordinal())) {
-                    masterState = state;
-                  }
-                } else if (componentInfo.isClient()) {
-                  hasClient = true;
-                  if (!state.equals(State.INSTALLED) &&
-                      (clientState == null || state.ordinal() > 
clientState.ordinal())) {
-                    clientState = state;
-                  }
-                } else {
-                  if (state.equals(State.STARTED) || ! isInMaintenance) {
-                    // We rely on slaves's state to determine service state
-                    hasOther = true;
-                  }
-                  if (! state.equals(State.STARTED) &&
-                      ! isInMaintenance && // Ignore status of MM component
-                      ( otherState == null || state.ordinal() > 
otherState.ordinal())) {
-                    otherState = state;
-                  }
-                }
-              } catch (ObjectNotFoundException e) {
-                // component doesn't exist, nothing to do
-              }
-            }
-
-            return hasMaster   ? masterState == null ? State.STARTED : 
masterState :
-                   hasOther    ? otherState == null ? State.STARTED : 
otherState :
-                   hasClient   ? clientState == null ? State.INSTALLED : 
clientState :
-                   hasDisabled ? State.DISABLED :
-                   hasMM       ? maxMMState : State.UNKNOWN;
-          }
-        } catch (AmbariException e) {
-          LOG.error("Can't determine service state.", e);
-        }
-      }
-      return State.UNKNOWN;
-    }
-  }
-
-  /**
-   * Calculator of Oozie service state.
-   */
-  protected static class OozieServiceState implements ServiceState {
-
-    @Override
-    public State getState(AmbariManagementController controller,String 
clusterName, String serviceName) {
-      AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
-      Clusters       clusters       = controller.getClusters();
-
-      if (clusterName != null && clusterName.length() > 0) {
-        try {
-          Cluster cluster = clusters.getCluster(clusterName);
-          if (cluster != null) {
-            StackId stackId = cluster.getDesiredStackVersion();
-
-            ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
-                    serviceName, null, null, null);
-
-            Set<ServiceComponentHostResponse> hostComponentResponses =
-                    
controller.getHostComponents(Collections.singleton(request));
-
-            int     oozieServerActiveCount = 0;
-            State   nonStartedState        = null;
-
-            for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
-              try {
-                ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
-                        stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
-                        hostComponentResponse.getComponentName());
-
-                if (componentInfo.isMaster()) {
-                  State state = getHostComponentState(hostComponentResponse);
-
-                  switch (state) {
-                    case STARTED:
-                    case DISABLED:
-                      String componentName = 
hostComponentResponse.getComponentName();
-                      if (componentName.equals("OOZIE_SERVER")) {
-                        ++oozieServerActiveCount;
-                      }
-                      break;
-                    default:
-                      nonStartedState = state;
-                  }
-                }
-              } catch (ObjectNotFoundException e) {
-                // component doesn't exist, nothing to do
-              }
-            }
-
-            // should have state INSTALLED when there is no active OOZIE_SERVER
-            if (oozieServerActiveCount > 0) {
-              return State.STARTED;
-            }
-            return nonStartedState == null ? State.INSTALLED : nonStartedState;
-          }
-        } catch (AmbariException e) {
-          LOG.error("Can't determine service state.", e);
-        }
-      }
-      return State.UNKNOWN;
-    }
-  }
-
-  /**
-   * Calculator of HIVE service state.
-   */
-  protected static class HiveServiceState implements ServiceState {
-
-    @Override
-    public State getState(AmbariManagementController controller, String 
clusterName, String serviceName) {
-      AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
-      Clusters       clusters       = controller.getClusters();
-
-      if (clusterName != null && clusterName.length() > 0) {
-        try {
-          Cluster cluster = clusters.getCluster(clusterName);
-          if (cluster != null) {
-            StackId stackId = cluster.getDesiredStackVersion();
-
-            ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
-                    serviceName, null, null, null);
-
-            Set<ServiceComponentHostResponse> hostComponentResponses =
-                    
controller.getHostComponents(Collections.singleton(request));
-
-            int activeHiveMetastoreComponentCount = 0;
-            State nonStartedState = null;
-            boolean embeddedMysqlComponentExists = false;
-            boolean hiveServerComponentStarted = false;
-            boolean webHcatComponentStarted = false;
-            boolean mysqlComponentStarted = false;
-
-            for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
-              try {
-                ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
-                        stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
-                        hostComponentResponse.getComponentName());
-
-                if (componentInfo.isMaster()) {
-                  State state = getHostComponentState(hostComponentResponse);
-
-                  String componentName = 
hostComponentResponse.getComponentName();
-                  if (componentName.equals("MYSQL_SERVER")) {
-                    embeddedMysqlComponentExists = true;
-                  }
-
-                  switch (state) {
-                    case STARTED:
-                    case DISABLED:
-                      if (componentName.equals("HIVE_METASTORE")) {
-                        ++activeHiveMetastoreComponentCount;
-                      } else if (componentName.equals("HIVE_SERVER")) {
-                        hiveServerComponentStarted = true;
-                      } else if (componentName.equals("MYSQL_SERVER")) {
-                        mysqlComponentStarted = true;
-                      } else if (componentName.equals("WEBHCAT_SERVER")) {
-                        webHcatComponentStarted = true;
-                      }
-                      break;
-                    default:
-                      nonStartedState = state;
-                  }
-                }
-              } catch (ObjectNotFoundException e) {
-                // component doesn't exist, nothing to do
-              }
-            }
-
-            if (nonStartedState == null ||
-                (hiveServerComponentStarted && webHcatComponentStarted && 
activeHiveMetastoreComponentCount > 0 &&
-                 (embeddedMysqlComponentExists ? mysqlComponentStarted : 
true))) {
-              return State.STARTED;
-            }
-            return nonStartedState == null ? State.INSTALLED : nonStartedState;
-          }
-        } catch (AmbariException e) {
-          LOG.error("Can't determine service state.", e);
-        }
-      }
-      return State.UNKNOWN;
-    }
-  }
-
-  /**
-   * Calculator of HDFS service state.
-   */
-  protected static class HDFSServiceState implements ServiceState {
-
-    @Override
-    public State getState(AmbariManagementController controller,String 
clusterName, String serviceName) {
-      AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
-      Clusters       clusters       = controller.getClusters();
-
-      if (clusterName != null && clusterName.length() > 0) {
-        try {
-          Cluster cluster = clusters.getCluster(clusterName);
-          if (cluster != null) {
-            StackId stackId = cluster.getDesiredStackVersion();
-
-            ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
-                serviceName, null, null, null);
-
-            Set<ServiceComponentHostResponse> hostComponentResponses =
-                controller.getHostComponents(Collections.singleton(request));
-
-            int     nameNodeCount       = 0;
-            int     nameNodeActiveCount = 0;
-            boolean hasSecondary        = false;
-            boolean hasJournal          = false;
-            State   nonStartedState     = null;
-
-            for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
-              try {
-                ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
-                    stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
-                    hostComponentResponse.getComponentName());
-
-                if (componentInfo.isMaster()) {
-                  String componentName = 
hostComponentResponse.getComponentName();
-                  boolean isNameNode = false;
-
-                  if (componentName.equals("NAMENODE")) {
-                    ++nameNodeCount;
-                    isNameNode = true;
-                  } else if (componentName.equals("SECONDARY_NAMENODE")) {
-                    hasSecondary = true;
-                  } else if (componentName.equals("JOURNALNODE")) {
-                    hasJournal = true;
-                  }
-
-                  State state = getHostComponentState(hostComponentResponse);
-
-                  switch (state) {
-                    case STARTED:
-                    case DISABLED:
-                      if (isNameNode) {
-                        ++nameNodeActiveCount;
-                      }
-                      break;
-                    default:
-                      nonStartedState = state;
-                  }
-                }
-              } catch (ObjectNotFoundException e) {
-                // component doesn't exist, nothing to do
-              }
-            }
-
-            if ( nonStartedState == null ||  // all started
-                ((nameNodeCount > 0 && !hasSecondary || hasJournal) &&
-                    nameNodeActiveCount > 0)) {  // at least one active 
namenode
-              return State.STARTED;
-            }
-            return nonStartedState;
-          }
-        } catch (AmbariException e) {
-          LOG.error("Can't determine service state.", e);
-        }
-      }
-      return State.UNKNOWN;
-    }
-  }
-
-  /**
-   * Calculator of HBase service state.
-   */
-  protected static class HBaseServiceState implements ServiceState {
-
-    @Override
-    public State getState(AmbariManagementController controller,String 
clusterName, String serviceName) {
-      AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
-      Clusters       clusters       = controller.getClusters();
-
-      if (clusterName != null && clusterName.length() > 0) {
-        try {
-          Cluster cluster = clusters.getCluster(clusterName);
-          if (cluster != null) {
-            StackId stackId = cluster.getDesiredStackVersion();
-
-            ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
-                serviceName, null, null, null);
-
-            Set<ServiceComponentHostResponse> hostComponentResponses =
-                controller.getHostComponents(Collections.singleton(request));
-
-            int     hBaseMasterActiveCount = 0;
-            State   nonStartedState        = null;
-
-            for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
-              try {
-                ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
-                    stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
-                    hostComponentResponse.getComponentName());
-
-                if (componentInfo.isMaster()) {
-                  State state = getHostComponentState(hostComponentResponse);
-
-                  switch (state) {
-                    case STARTED:
-                    case DISABLED:
-                      String componentName = 
hostComponentResponse.getComponentName();
-                      if (componentName.equals("HBASE_MASTER")) {
-                        ++hBaseMasterActiveCount;
-                      }
-                      break;
-                    default:
-                      nonStartedState = state;
-                  }
-                }
-              } catch (ObjectNotFoundException e) {
-                // component doesn't exist, nothing to do
-              }
-            }
-
-            // should have state INSTALLED when there is no active HBASE_MASTER
-            if (hBaseMasterActiveCount > 0) {
-              return State.STARTED;
-            }
-            return nonStartedState == null ? State.INSTALLED : nonStartedState;
-          }
-        } catch (AmbariException e) {
-          LOG.error("Can't determine service state.", e);
-        }
-      }
-      return State.UNKNOWN;
-    }
-  }
-
-  /**
-   * Determines the service status for Flume.  Generically, this means that
-   * the state of Flume is the lowest ordinal state calculated.  For example:
-   * <ul>
-   *   <li>If all handlers are STARTED, service is STARTED.</li>
-   *   <li>If one handler is INSTALLED, the service is INSTALLED.</li>
-   * </ul>
-   */
-  protected static class FlumeServiceState implements ServiceState {
-    @Override
-    public State getState(AmbariManagementController controller,
-        String clusterName, String serviceName) {
-      Clusters       clusters       = controller.getClusters();
-
-      if (clusterName != null && clusterName.length() > 0) {
-        try {
-          Cluster cluster = clusters.getCluster(clusterName);
-          if (cluster != null) {
-
-            ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
-                serviceName, null, null, null);
-
-            Set<ServiceComponentHostResponse> hostComponentResponses =
-                controller.getHostComponents(Collections.singleton(request));
-
-            State state = State.UNKNOWN;
-            for (ServiceComponentHostResponse schr : hostComponentResponses) {
-              State schState = getHostComponentState(schr);
-              if (schState.ordinal() < state.ordinal()) {
-                state = schState;
-              }
-            }
-            return state;
-          }
-        } catch (AmbariException e) {
-          LOG.error("Can't determine service state.", e);
-        }
-      }
-
-      return State.UNKNOWN;
-    }
-  }
-  
-  /**
-   * Calculator of YARN service state.
-   */
-  protected static class YARNServiceState implements ServiceState {
-         @Override
-      public State getState(AmbariManagementController controller,String 
clusterName, String serviceName) {
-        AmbariMetaInfo ambariMetaInfo = controller.getAmbariMetaInfo();
-        Clusters       clusters       = controller.getClusters();
-
-        if (clusterName != null && clusterName.length() > 0) {
-          try {
-            Cluster cluster = clusters.getCluster(clusterName);
-            if (cluster != null) {
-              StackId stackId = cluster.getDesiredStackVersion();
-
-              ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
-                  serviceName, null, null, null);
-
-              Set<ServiceComponentHostResponse> hostComponentResponses =
-                  controller.getHostComponents(Collections.singleton(request));
-
-              int     resourceManagerActiveCount      = 0;
-              boolean isAppTimeLineServerActive       = false;
-              State   nonStartedState                 = null;
-
-              for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
-                try {
-                  ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
-                      stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
-                      hostComponentResponse.getComponentName());
-
-                  if (componentInfo.isMaster()) {
-                    String componentName = 
hostComponentResponse.getComponentName();
-
-                    State state = getHostComponentState(hostComponentResponse);
-
-                    switch (state) {
-                      case STARTED:
-                      case DISABLED:
-                        if (componentName.equals("RESOURCEMANAGER")) {
-                          ++resourceManagerActiveCount;
-                        } else if 
(componentName.equals("APP_TIMELINE_SERVER")) {
-                          isAppTimeLineServerActive = true;
-                        }
-                        break;
-                      default:
-                        nonStartedState = state;
-                    }
-                  }
-                } catch (ObjectNotFoundException e) {
-                  // component doesn't exist, nothing to do
-                }
-              }
-
-              if ( nonStartedState == null ||  // all started
-                  (isAppTimeLineServerActive &&
-                      resourceManagerActiveCount > 0)) {  // at least one 
active resource manager
-                return State.STARTED;
-              }
-              return nonStartedState;
-            }
-          } catch (AmbariException e) {
-            LOG.error("Can't determine service state.", e);
-          }      
-        }
-        return State.UNKNOWN;
-      }          
-        
+    ServiceCalculatedState serviceCalculatedState = 
ServiceCalculatedStateFactory.getServiceStateProvider(serviceName);
+    return serviceCalculatedState.getState(clusterName, 
serviceName).toString();
   }
-       
 
   /**
    * Determine whether a service state change is valid.

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/ServiceCalculatedStateFactory.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/ServiceCalculatedStateFactory.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/ServiceCalculatedStateFactory.java
new file mode 100644
index 0000000..77c4d81
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/ServiceCalculatedStateFactory.java
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.controller.utilities;
+
+import net.sf.ehcache.util.concurrent.ConcurrentHashMap;
+import 
org.apache.ambari.server.controller.utilities.state.DefaultServiceCalculatedState;
+import 
org.apache.ambari.server.controller.utilities.state.FlumeServiceCalculatedState;
+import 
org.apache.ambari.server.controller.utilities.state.HBaseServiceCalculatedState;
+import 
org.apache.ambari.server.controller.utilities.state.HDFSServiceCalculatedState;
+import 
org.apache.ambari.server.controller.utilities.state.HiveServiceCalculatedState;
+import 
org.apache.ambari.server.controller.utilities.state.OozieServiceCalculatedState;
+import 
org.apache.ambari.server.controller.utilities.state.ServiceCalculatedState;
+import 
org.apache.ambari.server.controller.utilities.state.YARNServiceCalculatedState;
+import org.apache.ambari.server.state.Service;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.util.Map;
+
+
+public class ServiceCalculatedStateFactory {
+
+  private final static ServiceCalculatedState DEFAULT_SERVICE_CALCULATED_STATE 
= new DefaultServiceCalculatedState();
+  private final static Logger LOG = 
LoggerFactory.getLogger(ServiceCalculatedStateFactory.class);
+
+  // cache for the service calculated state objects
+  private final static Map<Service.Type, ServiceCalculatedState> 
serviceStateProviders = new ConcurrentHashMap<>();
+
+
+  public static ServiceCalculatedState getServiceStateProvider(String service){
+    ServiceCalculatedState suggestedServiceProvider;
+    Service.Type serviceType = null;
+
+    try {
+      serviceType = Service.Type.valueOf(service);
+    } catch (Exception e){
+      LOG.debug(String.format("Could not parse service name \"%s\", will use 
default state provider", service));
+    }
+
+    if (serviceType == null) {  // service is unknown, return default service 
state provider
+      return DEFAULT_SERVICE_CALCULATED_STATE;
+    }
+
+    if (serviceStateProviders.containsKey(serviceType)) {   // use cache on hit
+      suggestedServiceProvider = serviceStateProviders.get(serviceType);
+    } else {
+      switch (serviceType) {
+        case HDFS:
+          suggestedServiceProvider = new HDFSServiceCalculatedState();
+          break;
+        case FLUME:
+          suggestedServiceProvider = new FlumeServiceCalculatedState();
+          break;
+        case OOZIE:
+          suggestedServiceProvider = new OozieServiceCalculatedState();
+          break;
+        case YARN:
+          suggestedServiceProvider = new YARNServiceCalculatedState();
+          break;
+        case HIVE:
+          suggestedServiceProvider = new HiveServiceCalculatedState();
+          break;
+        case HBASE:
+          suggestedServiceProvider = new HBaseServiceCalculatedState();
+          break;
+        default:
+          suggestedServiceProvider = DEFAULT_SERVICE_CALCULATED_STATE;
+          break;
+      }
+      // lazy cache initialization, create just required objects
+      serviceStateProviders.put(serviceType, suggestedServiceProvider);
+    }
+    return suggestedServiceProvider;
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/DefaultServiceCalculatedState.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/DefaultServiceCalculatedState.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/DefaultServiceCalculatedState.java
new file mode 100644
index 0000000..beca114
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/DefaultServiceCalculatedState.java
@@ -0,0 +1,171 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.controller.utilities.state;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.ObjectNotFoundException;
+import org.apache.ambari.server.StaticallyInject;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.controller.AmbariManagementController;
+import org.apache.ambari.server.controller.ServiceComponentHostRequest;
+import org.apache.ambari.server.controller.ServiceComponentHostResponse;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.ComponentInfo;
+import org.apache.ambari.server.state.MaintenanceState;
+import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.State;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Default calculator of service state.
+ * The following rules should apply :
+ * For services that have all components DISABLED, the service state should be 
DISABLED.
+ * For services that have any master components, the service state should
+ * be STARTED if all master components are STARTED.
+ * For services that have all client components, the service state should
+ * be INSTALLED if all of the components are INSTALLED.
+ * For all other cases the state of the service should match the highest state 
of all
+ * of its component states or UNKNOWN if the component states can not be 
determined.
+ */
+@StaticallyInject
+public class DefaultServiceCalculatedState implements ServiceCalculatedState {
+
+  protected final static Logger LOG = 
LoggerFactory.getLogger(DefaultServiceCalculatedState.class);
+
+  @Inject
+  protected static Provider<Clusters> clustersProvider = null;
+
+  @Inject
+  protected static Provider<AmbariManagementController> 
managementControllerProvider = null;
+
+
+  // Get the State of a host component
+  protected State getHostComponentState(ServiceComponentHostResponse 
hostComponent) {
+    return State.valueOf(hostComponent.getLiveState());
+  }
+
+  protected Cluster getCluster(String clusterName) throws AmbariException {
+    if (clustersProvider != null && clusterName != null && 
clusterName.length() > 0) {
+      Clusters clusters = clustersProvider.get();
+      if (clusters != null){
+          return clusters.getCluster(clusterName);
+      }
+    }
+    return null;
+  }
+
+  public State getState(String clusterName, String serviceName) {
+      try {
+        Cluster cluster = getCluster(clusterName);
+        if (cluster != null && managementControllerProvider != null) {
+          AmbariMetaInfo ambariMetaInfo = 
managementControllerProvider.get().getAmbariMetaInfo();
+          StackId stackId = cluster.getDesiredStackVersion();
+
+          ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
+            serviceName, null, null, null);
+
+          Set<ServiceComponentHostResponse> hostComponentResponses =
+            
managementControllerProvider.get().getHostComponents(Collections.singleton(request));
+
+          State   masterState = null;
+          State   clientState = null;
+          State   otherState = null;
+          State   maxMMState = null; // The worst state among components in MM
+
+          boolean hasDisabled  = false;
+          boolean hasMaster    = false;
+          boolean hasOther     = false;
+          boolean hasClient    = false;
+          boolean hasMM        = false;
+
+          for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
+            try {
+              ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
+                stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
+                hostComponentResponse.getComponentName());
+
+              State state = getHostComponentState(hostComponentResponse);
+              // Components in MM should not affect service status,
+              // so we tend to ignore them
+              boolean isInMaintenance = ! MaintenanceState.OFF.toString().
+                equals(hostComponentResponse.getMaintenanceState());
+
+              if (state.equals(State.DISABLED)) {
+                hasDisabled = true;
+              }
+
+              if (isInMaintenance & !componentInfo.isClient()) {
+                hasMM = true;
+                if ( maxMMState == null || state.ordinal() > 
maxMMState.ordinal()) {
+                  maxMMState = state;
+                }
+              }
+
+              if (componentInfo.isMaster()) {
+                if (state.equals(State.STARTED) || ! isInMaintenance) {
+                  // We rely on master's state to determine service state
+                  hasMaster = true;
+                }
+
+                if (! state.equals(State.STARTED) &&
+                  ! isInMaintenance &&  // Ignore status of MM component
+                  ( masterState == null || state.ordinal() > 
masterState.ordinal())) {
+                  masterState = state;
+                }
+              } else if (componentInfo.isClient()) {
+                hasClient = true;
+                if (!state.equals(State.INSTALLED) &&
+                  (clientState == null || state.ordinal() > 
clientState.ordinal())) {
+                  clientState = state;
+                }
+              } else {
+                if (state.equals(State.STARTED) || ! isInMaintenance) {
+                  // We rely on slaves's state to determine service state
+                  hasOther = true;
+                }
+                if (! state.equals(State.STARTED) &&
+                  ! isInMaintenance && // Ignore status of MM component
+                  ( otherState == null || state.ordinal() > 
otherState.ordinal())) {
+                  otherState = state;
+                }
+              }
+            } catch (ObjectNotFoundException e) {
+              // component doesn't exist, nothing to do
+            }
+          }
+
+          return hasMaster   ? masterState == null ? State.STARTED : 
masterState :
+            hasOther    ? otherState == null ? State.STARTED : otherState :
+              hasClient   ? clientState == null ? State.INSTALLED : 
clientState :
+                hasDisabled ? State.DISABLED :
+                  hasMM       ? maxMMState : State.UNKNOWN;
+        }
+      } catch (AmbariException e) {
+        LOG.error("Can't determine service state.", e);
+      }
+    return State.UNKNOWN;
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/FlumeServiceCalculatedState.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/FlumeServiceCalculatedState.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/FlumeServiceCalculatedState.java
new file mode 100644
index 0000000..bd0d42a
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/FlumeServiceCalculatedState.java
@@ -0,0 +1,69 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.controller.utilities.state;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.StaticallyInject;
+import org.apache.ambari.server.controller.ServiceComponentHostRequest;
+import org.apache.ambari.server.controller.ServiceComponentHostResponse;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.State;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Determines the service status for Flume.  Generically, this means that
+ * the state of Flume is the lowest ordinal state calculated.  For example:
+ * <ul>
+ *   <li>If all handlers are STARTED, service is STARTED.</li>
+ *   <li>If one handler is INSTALLED, the service is INSTALLED.</li>
+ * </ul>
+ */
+@StaticallyInject
+public final class FlumeServiceCalculatedState extends 
DefaultServiceCalculatedState
+  implements ServiceCalculatedState {
+
+  @Override
+  public State getState(String clusterName, String serviceName) {
+    try {
+      Cluster cluster = getCluster(clusterName);
+      if (cluster != null && managementControllerProvider != null) {
+
+        ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
+          serviceName, null, null, null);
+
+        Set<ServiceComponentHostResponse> hostComponentResponses =
+          
managementControllerProvider.get().getHostComponents(Collections.singleton(request));
+
+        State state = State.UNKNOWN;
+        for (ServiceComponentHostResponse schr : hostComponentResponses) {
+          State schState = getHostComponentState(schr);
+          if (schState.ordinal() < state.ordinal()) {
+            state = schState;
+          }
+        }
+        return state;
+      }
+    } catch (AmbariException e) {
+      LOG.error("Can't determine service state.", e);
+    }
+    return State.UNKNOWN;
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HBaseServiceCalculatedState.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HBaseServiceCalculatedState.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HBaseServiceCalculatedState.java
new file mode 100644
index 0000000..1514bdb
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HBaseServiceCalculatedState.java
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.controller.utilities.state;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.ObjectNotFoundException;
+import org.apache.ambari.server.StaticallyInject;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.controller.ServiceComponentHostRequest;
+import org.apache.ambari.server.controller.ServiceComponentHostResponse;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.ComponentInfo;
+import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.State;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Calculator of HBase service state.
+ */
+@StaticallyInject
+public final class HBaseServiceCalculatedState extends 
DefaultServiceCalculatedState
+  implements ServiceCalculatedState {
+
+  @Override
+  public State getState(String clusterName, String serviceName) {
+    try {
+      Cluster cluster = getCluster(clusterName);
+      if (cluster != null && managementControllerProvider != null) {
+        AmbariMetaInfo ambariMetaInfo = 
managementControllerProvider.get().getAmbariMetaInfo();
+        StackId stackId = cluster.getDesiredStackVersion();
+
+        ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
+          serviceName, null, null, null);
+
+        Set<ServiceComponentHostResponse> hostComponentResponses =
+          
managementControllerProvider.get().getHostComponents(Collections.singleton(request));
+
+        int     hBaseMasterActiveCount = 0;
+        State   nonStartedState        = null;
+
+        for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
+          try {
+            ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
+              stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
+              hostComponentResponse.getComponentName());
+
+            if (componentInfo.isMaster()) {
+              State state = getHostComponentState(hostComponentResponse);
+
+              switch (state) {
+                case STARTED:
+                case DISABLED:
+                  String componentName = 
hostComponentResponse.getComponentName();
+                  if (componentName.equals("HBASE_MASTER")) {
+                    ++hBaseMasterActiveCount;
+                  }
+                  break;
+                default:
+                  nonStartedState = state;
+              }
+            }
+          } catch (ObjectNotFoundException e) {
+            // component doesn't exist, nothing to do
+          }
+        }
+
+        // should have state INSTALLED when there is no active HBASE_MASTER
+        if (hBaseMasterActiveCount > 0) {
+          return State.STARTED;
+        }
+        return nonStartedState == null ? State.INSTALLED : nonStartedState;
+      }
+    } catch (AmbariException e) {
+      LOG.error("Can't determine service state.", e);
+    }
+    return State.UNKNOWN;
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HDFSServiceCalculatedState.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HDFSServiceCalculatedState.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HDFSServiceCalculatedState.java
new file mode 100644
index 0000000..e317dcb
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HDFSServiceCalculatedState.java
@@ -0,0 +1,115 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.controller.utilities.state;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.ObjectNotFoundException;
+import org.apache.ambari.server.StaticallyInject;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.controller.ServiceComponentHostRequest;
+import org.apache.ambari.server.controller.ServiceComponentHostResponse;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.ComponentInfo;
+import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.State;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Calculator of HDFS service state.
+ */
+@StaticallyInject
+public final class HDFSServiceCalculatedState extends 
DefaultServiceCalculatedState
+  implements ServiceCalculatedState {
+
+  @Override
+  public State getState(String clusterName, String serviceName) {
+    try {
+      Cluster cluster = getCluster(clusterName);
+      if (cluster != null && managementControllerProvider != null) {
+        AmbariMetaInfo ambariMetaInfo = 
managementControllerProvider.get().getAmbariMetaInfo();
+        StackId stackId = cluster.getDesiredStackVersion();
+
+        ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
+          serviceName, null, null, null);
+
+        Set<ServiceComponentHostResponse> hostComponentResponses =
+          
managementControllerProvider.get().getHostComponents(Collections.singleton(request));
+
+        int     nameNodeCount       = 0;
+        int     nameNodeActiveCount = 0;
+        boolean hasSecondary        = false;
+        boolean hasJournal          = false;
+        State   nonStartedState     = null;
+
+        for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
+          try {
+            ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
+              stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
+              hostComponentResponse.getComponentName());
+
+            if (componentInfo.isMaster()) {
+              String componentName = hostComponentResponse.getComponentName();
+              boolean isNameNode = false;
+
+              switch (componentName) {
+                case "NAMENODE":
+                  ++nameNodeCount;
+                  isNameNode = true;
+                  break;
+                case "SECONDARY_NAMENODE":
+                  hasSecondary = true;
+                  break;
+                case "JOURNALNODE":
+                  hasJournal = true;
+                  break;
+              }
+
+              State state = getHostComponentState(hostComponentResponse);
+
+              switch (state) {
+                case STARTED:
+                case DISABLED:
+                  if (isNameNode) {
+                    ++nameNodeActiveCount;
+                  }
+                  break;
+                default:
+                  nonStartedState = state;
+              }
+            }
+          } catch (ObjectNotFoundException e) {
+            // component doesn't exist, nothing to do
+          }
+        }
+
+        if ( nonStartedState == null ||  // all started
+          ((nameNodeCount > 0 && !hasSecondary || hasJournal) &&
+            nameNodeActiveCount > 0)) {  // at least one active namenode
+          return State.STARTED;
+        }
+        return nonStartedState;
+      }
+    } catch (AmbariException e) {
+      LOG.error("Can't determine service state.", e);
+    }
+    return State.UNKNOWN;
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/ba2b9700/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HiveServiceCalculatedState.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HiveServiceCalculatedState.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HiveServiceCalculatedState.java
new file mode 100644
index 0000000..0f280b3
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/state/HiveServiceCalculatedState.java
@@ -0,0 +1,111 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.controller.utilities.state;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.ObjectNotFoundException;
+import org.apache.ambari.server.StaticallyInject;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.controller.ServiceComponentHostRequest;
+import org.apache.ambari.server.controller.ServiceComponentHostResponse;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.ComponentInfo;
+import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.State;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Calculator of HIVE service state.
+ */
+@StaticallyInject
+public final class HiveServiceCalculatedState extends 
DefaultServiceCalculatedState
+  implements ServiceCalculatedState {
+
+  @Override
+  public State getState(String clusterName, String serviceName) {
+    try {
+      Cluster cluster = getCluster(clusterName);
+      if (cluster != null && managementControllerProvider != null) {
+        AmbariMetaInfo ambariMetaInfo = 
managementControllerProvider.get().getAmbariMetaInfo();
+        StackId stackId = cluster.getDesiredStackVersion();
+
+        ServiceComponentHostRequest request = new 
ServiceComponentHostRequest(clusterName,
+          serviceName, null, null, null);
+
+        Set<ServiceComponentHostResponse> hostComponentResponses =
+          
managementControllerProvider.get().getHostComponents(Collections.singleton(request));
+
+        int activeHiveMetastoreComponentCount = 0;
+        State nonStartedState = null;
+        boolean embeddedMysqlComponentExists = false;
+        boolean hiveServerComponentStarted = false;
+        boolean webHcatComponentStarted = false;
+        boolean mysqlComponentStarted = false;
+
+        for (ServiceComponentHostResponse hostComponentResponse : 
hostComponentResponses ) {
+          try {
+            ComponentInfo componentInfo = 
ambariMetaInfo.getComponent(stackId.getStackName(),
+              stackId.getStackVersion(), 
hostComponentResponse.getServiceName(),
+              hostComponentResponse.getComponentName());
+
+            if (componentInfo.isMaster()) {
+              State state = getHostComponentState(hostComponentResponse);
+
+              String componentName = hostComponentResponse.getComponentName();
+              if (componentName.equals("MYSQL_SERVER")) {
+                embeddedMysqlComponentExists = true;
+              }
+
+              switch (state) {
+                case STARTED:
+                case DISABLED:
+                  if (componentName.equals("HIVE_METASTORE")) {
+                    ++activeHiveMetastoreComponentCount;
+                  } else if (componentName.equals("HIVE_SERVER")) {
+                    hiveServerComponentStarted = true;
+                  } else if (componentName.equals("MYSQL_SERVER")) {
+                    mysqlComponentStarted = true;
+                  } else if (componentName.equals("WEBHCAT_SERVER")) {
+                    webHcatComponentStarted = true;
+                  }
+                  break;
+                default:
+                  nonStartedState = state;
+              }
+            }
+          } catch (ObjectNotFoundException e) {
+            // component doesn't exist, nothing to do
+          }
+        }
+
+        if (nonStartedState == null ||
+          (hiveServerComponentStarted && webHcatComponentStarted && 
activeHiveMetastoreComponentCount > 0 &&
+            (embeddedMysqlComponentExists ? mysqlComponentStarted : true))) {
+          return State.STARTED;
+        }
+        return nonStartedState == null ? State.INSTALLED : nonStartedState;
+      }
+    } catch (AmbariException e) {
+      LOG.error("Can't determine service state.", e);
+    }
+    return State.UNKNOWN;
+  }
+}
\ No newline at end of file

Reply via email to