SLIDER-562: AM stats to be correct on slider web UI
diff --git a/slider-core/src/main/java/org/apache/slider/providers/slideram/SliderAMClientProvider.java b/slider-core/src/main/java/org/apache/slider/providers/slideram/SliderAMClientProvider.java
index 60edce7..125746d 100644
--- a/slider-core/src/main/java/org/apache/slider/providers/slideram/SliderAMClientProvider.java
+++ b/slider-core/src/main/java/org/apache/slider/providers/slideram/SliderAMClientProvider.java
@@ -86,12 +86,15 @@
 
   public static final int KEY_AM = ROLE_AM_PRIORITY_INDEX;
 
+  public static final ProviderRole APPMASTER =
+      new ProviderRole(COMPONENT_AM, KEY_AM,
+          PlacementPolicy.EXCLUDE_FROM_FLEXING);
+
   /**
    * Initialize role list
    */
   static {
-    ROLES.add(new ProviderRole(COMPONENT_AM, KEY_AM,
-                               PlacementPolicy.EXCLUDE_FROM_FLEXING));
+    ROLES.add(APPMASTER);
   }
 
   @Override
diff --git a/slider-core/src/main/java/org/apache/slider/server/appmaster/SliderAppMaster.java b/slider-core/src/main/java/org/apache/slider/server/appmaster/SliderAppMaster.java
index bb198a1..efa1b09 100644
--- a/slider-core/src/main/java/org/apache/slider/server/appmaster/SliderAppMaster.java
+++ b/slider-core/src/main/java/org/apache/slider/server/appmaster/SliderAppMaster.java
@@ -692,9 +692,9 @@
               .getComponent(SliderKeys.COMPONENT_AM));
       certificateManager.setPassphrase(instanceDefinition.getPassphrase());
 
-      if (Boolean.valueOf(instanceDefinition.
+      if (instanceDefinition.
           getAppConfOperations().getComponent(SliderKeys.COMPONENT_AM).
-          getOptionBool(AgentKeys.KEY_AGENT_TWO_WAY_SSL_ENABLED, false))) {
+          getOptionBool(AgentKeys.KEY_AGENT_TWO_WAY_SSL_ENABLED, false)) {
         uploadServerCertForLocalization(clustername, fs);
       }
 
@@ -895,8 +895,8 @@
       // start handling any scheduled events
 
       startQueueProcessing();
-      // Start the Slider AM provider
 
+      // Start the Slider AM provider
       sliderAMProvider.start();
 
       // launch the real provider; this is expected to trigger a callback that
diff --git a/slider-core/src/main/java/org/apache/slider/server/appmaster/state/AppState.java b/slider-core/src/main/java/org/apache/slider/server/appmaster/state/AppState.java
index c6bdc91..24245bb 100644
--- a/slider-core/src/main/java/org/apache/slider/server/appmaster/state/AppState.java
+++ b/slider-core/src/main/java/org/apache/slider/server/appmaster/state/AppState.java
@@ -79,16 +79,8 @@
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import static org.apache.slider.api.ResourceKeys.DEF_YARN_CORES;
-import static org.apache.slider.api.ResourceKeys.DEF_YARN_LABEL_EXPRESSION;
-import static org.apache.slider.api.ResourceKeys.DEF_YARN_MEMORY;
-import static org.apache.slider.api.ResourceKeys.YARN_CORES;
-import static org.apache.slider.api.ResourceKeys.YARN_LABEL_EXPRESSION;
-import static org.apache.slider.api.ResourceKeys.YARN_MEMORY;
-import static org.apache.slider.api.RoleKeys.ROLE_FAILED_INSTANCES;
-import static org.apache.slider.api.RoleKeys.ROLE_FAILED_STARTING_INSTANCES;
-import static org.apache.slider.api.RoleKeys.ROLE_RELEASING_INSTANCES;
-import static org.apache.slider.api.RoleKeys.ROLE_REQUESTED_INSTANCES;
+import static org.apache.slider.api.ResourceKeys.*;
+import static org.apache.slider.api.RoleKeys.*;
 
 
 /**
@@ -615,6 +607,11 @@
                                                           IOException {
     instanceDefinition.resolve();
 
+    // force in the AM desired state values
+    instanceDefinition.getResourceOperations().setComponentOpt(
+        SliderKeys.COMPONENT_AM, ResourceKeys.COMPONENT_INSTANCES, "1"
+    );
+    
     //note the time 
     snapshotTime = now();
     //snapshot all three sectons
@@ -665,8 +662,8 @@
   /**
    * build the role requirements from the cluster specification
    */
-  private void buildRoleRequirementsFromResources() throws
-                                                      BadConfigException {
+  private void buildRoleRequirementsFromResources() throws BadConfigException {
+
     //now update every role's desired count.
     //if there are no instance values, that role count goes to zero
 
@@ -675,6 +672,10 @@
 
     // Add all the existing roles
     for (RoleStatus roleStatus : getRoleStatusMap().values()) {
+      if (roleStatus.getExcludeFromFlexing()) {
+        // skip inflexible roles, e.g AM itself
+        continue;
+      }
       int currentDesired = roleStatus.getDesired();
       String role = roleStatus.getName();
       MapOperations comp =
@@ -711,8 +712,10 @@
    * should be used while setting up the system state -before servicing
    * requests.
    * @param providerRole role to add
+   * @return the role status built up
+   * @throws BadConfigException if a role of that priority already exists
    */
-  public void buildRole(ProviderRole providerRole) throws BadConfigException {
+  public RoleStatus buildRole(ProviderRole providerRole) throws BadConfigException {
     //build role status map
     int priority = providerRole.id;
     if (roleStatusMap.containsKey(priority)) {
@@ -720,10 +723,11 @@
                                    providerRole,
                                    roleStatusMap.get(priority));
     }
-    roleStatusMap.put(priority,
-        new RoleStatus(providerRole));
+    RoleStatus roleStatus = new RoleStatus(providerRole);
+    roleStatusMap.put(priority, roleStatus);
     roles.put(providerRole.name, providerRole);
     rolePriorityMap.put(priority, providerRole);
+    return roleStatus;
   }
 
   /**
@@ -748,6 +752,13 @@
     appMasterNode = am;
     //it is also added to the set of live nodes
     getLiveNodes().put(containerId, am);
+    
+    // patch up the role status
+    RoleStatus roleStatus = roleStatusMap.get(
+        (SliderKeys.ROLE_AM_PRIORITY_INDEX));
+    roleStatus.setDesired(1);
+    roleStatus.incActual();
+    roleStatus.incStarted();
   }
 
   /**
diff --git a/slider-core/src/main/java/org/apache/slider/server/appmaster/state/RoleStatus.java b/slider-core/src/main/java/org/apache/slider/server/appmaster/state/RoleStatus.java
index 0051402..734bbea 100644
--- a/slider-core/src/main/java/org/apache/slider/server/appmaster/state/RoleStatus.java
+++ b/slider-core/src/main/java/org/apache/slider/server/appmaster/state/RoleStatus.java
@@ -267,7 +267,6 @@
            ", startFailed=" + startFailed +
            ", completed=" + completed +
            ", failureMessage='" + failureMessage + '\'' +
-           
            '}';
   }