ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smoha...@apache.org
Subject [2/2] ambari git commit: AMBARI-14194. Role Based Access Control support for Metrics (Swapan Shridhar via smohanty)
Date Wed, 16 Dec 2015 01:10:15 GMT
AMBARI-14194. Role Based Access Control support for Metrics (Swapan Shridhar via smohanty)


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

Branch: refs/heads/branch-rbac-sso
Commit: 57d08170606f2704df9f121ec5301870a96378b7
Parents: 5024b2b
Author: Sumit Mohanty <smohanty@hortonworks.com>
Authored: Tue Dec 15 17:06:07 2015 -0800
Committer: Sumit Mohanty <smohanty@hortonworks.com>
Committed: Tue Dec 15 17:09:37 2015 -0800

----------------------------------------------------------------------
 .../internal/AbstractPropertyProvider.java      | 170 ++++++++++-
 .../internal/StackDefinedPropertyProvider.java  |  11 +-
 .../controller/jmx/JMXPropertyProvider.java     |   2 +-
 .../metrics/MetricsPropertyProvider.java        |   7 +-
 .../metrics/MetricsPropertyProviderProxy.java   |   9 +-
 .../MetricsReportPropertyProviderProxy.java     |   6 +-
 .../metrics/RestMetricsPropertyProvider.java    |   2 +-
 .../ThreadPoolEnabledPropertyProvider.java      |   8 +-
 .../authorization/AuthorizationHelper.java      |   3 +
 .../StackDefinedPropertyProviderTest.java       | 201 +++++++++----
 .../metrics/JMXPropertyProviderTest.java        | 291 ++++++++++++-------
 .../RestMetricsPropertyProviderTest.java        | 235 +++++++++------
 .../ganglia/GangliaPropertyProviderTest.java    | 160 ++++++++--
 .../timeline/AMSPropertyProviderTest.java       | 229 +++++++++++----
 14 files changed, 991 insertions(+), 343 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractPropertyProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractPropertyProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractPropertyProvider.java
index 4a0c44f..2b7ee4e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractPropertyProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractPropertyProvider.java
@@ -7,7 +7,7 @@
  * "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
+ *        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,
@@ -18,15 +18,27 @@
 
 package org.apache.ambari.server.controller.internal;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.controller.AmbariManagementController;
+import org.apache.ambari.server.controller.AmbariServer;
 import org.apache.ambari.server.controller.metrics.MetricReportingAdapter;
 import org.apache.ambari.server.controller.spi.PropertyProvider;
+import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.TemporalInfo;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.apache.ambari.server.security.authorization.AuthorizationException;
+import org.apache.ambari.server.security.authorization.AuthorizationHelper;
+import org.apache.ambari.server.security.authorization.ResourceType;
+import org.apache.ambari.server.security.authorization.RoleAuthorization;
 import org.apache.hadoop.metrics2.sink.timeline.TimelineMetric;
+
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.text.DecimalFormat;
+import java.util.EnumSet;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -97,6 +109,136 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
   // ----- helper methods ----------------------------------------------------
 
   /**
+   * Retrieves passed-in Resource's Type
+   *
+   * @param resources Set of Resources.
+   * @return Type of resource from the Set.
+   */
+  protected String getResourceTypeFromResources(Set<Resource> resources) {
+    String resType = null;
+    if (resources != null) {
+      Iterator<Resource> itr = resources.iterator();
+      if (itr.hasNext()) {
+        // Pick the 1st resource, as the passed in resources will have same Type,
+        // in a given call.
+        Resource res = itr.next();
+        if (res != null) {
+          resType = res.getType().toString();
+        }
+      }
+    }
+    return resType;
+  }
+
+  /**
+   * Retrieves all the cluster names to which the passed-in Resource's belong.
+   *
+   * @param resources Set of Resources.
+   * @return Cluster's Name
+   */
+  protected Set<String> getClustersNameFromResources(Set<Resource> resources, String clusterNamePropertyId) {
+    Set<String> clusNames = new HashSet<String>();
+    if (resources != null) {
+      Iterator<Resource> itr = resources.iterator();
+      while (itr.hasNext()) {
+        Resource res = itr.next();
+        if (res != null) {
+          clusNames.add((String) res.getPropertyValue(clusterNamePropertyId));
+        }
+      }
+    }
+    return clusNames;
+  }
+
+  /**
+   * Retrieves all the 'Cluster's Resource Ids' from the passed-in Resources.
+   *
+   * @param resources Set of Resources.
+   * @param clusterNamePropertyId ClusterName PropertyId.
+   * @return cluster Id.
+   */
+  protected Set<Long> getClustersResourceId(Set<Resource> resources, String clusterNamePropertyId) {
+    Set<Long> clusterResId = new HashSet<Long>();
+    if (clusterNamePropertyId != null) {
+      try {
+        AmbariManagementController amc = AmbariServer.getController();
+        Set<String> clusterNames = getClustersNameFromResources(resources, clusterNamePropertyId);
+        Iterator<String> clusNameItr = clusterNames.iterator();
+        while (clusNameItr.hasNext()) {
+          clusterResId.add(amc.getClusters().getCluster(clusNameItr.next()).getResourceId());
+        }
+      } catch (AmbariException e) {
+        LOG.error("Cluster Id couldn't be retrieved.");
+      } catch (Exception e) {
+        LOG.error("Cluster Id couldn't be retrieved");
+      }
+    }
+    if(LOG.isDebugEnabled()) {
+      LOG.debug("Retrieved Cluster Ids = " + clusterResId.toString());
+    }
+    return clusterResId;
+  }
+
+
+  /**
+   * Check the User's authorization for retrieving the Metrics.
+   *
+   * @param resources Set of Resources.
+   * @param clusterNamePropertyId ClusterName PropertyId.
+   * @return boolean
+   * @throws AuthorizationException
+   */
+  protected boolean checkAuthorizationForMetrics(Set<Resource> resources, String clusterNamePropertyId) throws AuthorizationException {
+    String resType = null;
+
+    // Get the Type
+    resType = getResourceTypeFromResources(resources);
+    if (resType == null) {
+      return false;
+    }
+
+    // Get the cluster Id.
+    Set<Long> clusterResIds = getClustersResourceId(resources, clusterNamePropertyId);
+    if (clusterResIds.size() == 0) {
+      return false;
+    }
+
+    if(LOG.isDebugEnabled()) {
+      LOG.debug("Retrieved cluster's Resource Id = " + clusterResIds + ", Resource Type = " + resType);
+    }
+    Iterator<Long> clusResIdsItr = clusterResIds.iterator();
+    while (clusResIdsItr.hasNext()) {
+      Long clusResId = clusResIdsItr.next();
+      Resource.InternalType resTypeVal = Resource.InternalType.valueOf(resType);
+      switch (resTypeVal) {
+        case Cluster:
+          if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, clusResId, EnumSet.of(RoleAuthorization.CLUSTER_VIEW_METRICS))) {
+            throw new AuthorizationException("The authenticated user does not have authorization to view cluster metrics");
+          }
+          break;
+        case Host:
+          if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, clusResId, EnumSet.of(RoleAuthorization.HOST_VIEW_METRICS))) {
+            throw new AuthorizationException("The authenticated user does not have authorization to view Host metrics");
+          }
+          break;
+        case Component :
+          if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, clusResId, EnumSet.of(RoleAuthorization.SERVICE_VIEW_METRICS))) {
+            throw new AuthorizationException("The authenticated user does not have authorization to view Service metrics");
+          }
+          break;
+        case HostComponent:
+          if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, clusResId, EnumSet.of(RoleAuthorization.SERVICE_VIEW_METRICS))) {
+            throw new AuthorizationException("The authenticated user does not have authorization to view Service metrics");
+          }
+          break;
+        default:
+          LOG.error("Unsuported Resource Type for Metrics");
+          return false;
+      }
+    }
+    return true;
+  }
+  /**
    * Get a map of metric / property info based on the given component name and property id.
    * Note that the property id may map to multiple metrics if the property id is a category.
    *
@@ -139,7 +281,7 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
       }
     }
 
-    if (!propertyId.endsWith("/")){
+    if (!propertyId.endsWith("/")) {
       propertyId += "/";
     }
 
@@ -198,13 +340,13 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
         matcher = FIND_ARGUMENT_METHOD_REGEX.matcher(argName);
         while (matcher.find()) {
           // find the end of the method
-          int openParenIndex  = argName.indexOf('(', matcher.start());
+          int openParenIndex = argName.indexOf('(', matcher.start());
           int closeParenIndex = indexOfClosingParenthesis(argName, openParenIndex);
 
           String methodName = argName.substring(matcher.start() + 1, openParenIndex);
-          String args       = argName.substring(openParenIndex + 1, closeParenIndex);
+          String args = argName.substring(openParenIndex + 1, closeParenIndex);
 
-          List<Object>   argList    = new LinkedList<Object>();
+          List<Object> argList = new LinkedList<Object>();
           List<Class<?>> paramTypes = new LinkedList<Class<?>>();
 
           // for each argument of the method ...
@@ -217,7 +359,7 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
             value = invokeArgumentMethod(value, methodName, argList, paramTypes);
           } catch (Exception e) {
             throw new IllegalArgumentException("Can't apply method " + methodName + " for argument " +
-                argName + " in " + propertyId, e);
+              argName + " in " + propertyId, e);
           }
         }
         if (value.equals(val)) {
@@ -234,7 +376,7 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
    * Find the index of the closing parenthesis in the given string.
    */
   private static int indexOfClosingParenthesis(String s, int index) {
-    int depth  = 0;
+    int depth = 0;
     int length = s.length();
 
     while (index < length) {
@@ -242,8 +384,8 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
       if (c == '(') {
         ++depth;
       } else if (c == ')') {
-        if (--depth ==0 ){
-         return index;
+        if (--depth == 0) {
+          return index;
         }
       }
     }
@@ -258,7 +400,7 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
 
     // only supports strings and integers
     if (arg.contains("\"")) {
-      argList.add(arg.substring(1, arg.length() -1));
+      argList.add(arg.substring(1, arg.length() - 1));
       paramTypes.add(String.class);
     } else {
       Integer number = Integer.parseInt(arg);
@@ -272,7 +414,7 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
    */
   private static String invokeArgumentMethod(String argValue, String methodName, List<Object> argList,
                                              List<Class<?>> paramTypes)
-      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+    throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
     // invoke the method through reflection
     Method method = String.class.getMethod(methodName, paramTypes.toArray(new Class<?>[paramTypes.size()]));
 
@@ -296,7 +438,7 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
     }
 
     if (!componentMetricMap.containsKey(propertyId) && regexKey != null
-        && !regexKey.equals(propertyId)) {
+      && !regexKey.equals(propertyId)) {
 
       PropertyInfo propertyInfo = componentMetricMap.get(regexKey);
       if (propertyInfo != null) {
@@ -320,7 +462,7 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
   protected PropertyInfo updatePropertyInfo(String propertyKey, String id, PropertyInfo propertyInfo) {
     List<String> regexGroups = getRegexGroups(propertyKey, id);
     String propertyId = propertyInfo.getPropertyId();
-    if(propertyId != null) {
+    if (propertyId != null) {
       for (String regexGroup : regexGroups) {
         regexGroup = regexGroup.replace("/", ".");
         propertyId = propertyId.replaceFirst(FIND_REGEX_IN_METRIC_REGEX, regexGroup);
@@ -354,7 +496,7 @@ public abstract class AbstractPropertyProvider extends BaseProvider implements P
 
     String category = PropertyHelper.getPropertyCategory(propertyId);
     while (category != null) {
-      if(categoryIds.contains(category)) {
+      if (categoryIds.contains(category)) {
         return true;
       }
       category = PropertyHelper.getPropertyCategory(category);

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProvider.java
index a1b4f3f..edd11c1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProvider.java
@@ -33,6 +33,7 @@ import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.utilities.StreamProvider;
+import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.StackId;
@@ -40,6 +41,7 @@ import org.apache.ambari.server.state.stack.Metric;
 import org.apache.ambari.server.state.stack.MetricDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -50,8 +52,6 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
-import static org.apache.ambari.server.controller.metrics.MetricsServiceProvider.MetricsService;
-
 /**
  * This class analyzes a service's metrics to determine if additional
  * metrics should be fetched.  It's okay to maintain state here since these
@@ -206,8 +206,11 @@ public class StackDefinedPropertyProvider implements PropertyProvider {
         pp.populateResources(resources, request, predicate);
       }
 
-    } catch (Exception e) {
-      e.printStackTrace();
+    } catch (AuthorizationException e) {
+      // Need to rethrow the catched 'AuthorizationException'.
+      throw e;
+    }
+    catch (Exception e) {
       throw new SystemException("Error loading deferred resources", e);
     }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java
index 2748dd4..2079e72 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java
@@ -121,7 +121,7 @@ public class JMXPropertyProvider extends ThreadPoolEnabledPropertyProvider {
                              String componentNamePropertyId,
                              String statePropertyId) {
 
-    super(componentMetrics, hostNamePropertyId, metricHostProvider);
+    super(componentMetrics, hostNamePropertyId, metricHostProvider, clusterNamePropertyId);
 
     this.streamProvider           = streamProvider;
     this.jmxHostProvider          = jmxHostProvider;

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProvider.java
index f1c5c81..a346051 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProvider.java
@@ -26,8 +26,6 @@ import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.SystemException;
-import org.apache.ambari.server.controller.utilities.PredicateHelper;
-import org.apache.ambari.server.controller.utilities.StreamProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import java.util.Map;
@@ -129,12 +127,15 @@ public abstract class MetricsPropertyProvider extends AbstractPropertyProvider {
   @Override
   public Set<Resource> populateResources(Set<Resource> resources,
                 Request request, Predicate predicate) throws SystemException {
-
     Set<String> ids = getRequestPropertyIds(request, predicate);
     if (ids.isEmpty()) {
       return resources;
     }
 
+    if(!checkAuthorizationForMetrics(resources, clusterNamePropertyId)) {
+      return resources;
+    }
+
     // Re-initialize in case of reuse.
     metricsPaddingMethod = DEFAULT_PADDING_METHOD;
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProviderProxy.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProviderProxy.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProviderProxy.java
index ac11556..c48aa23 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProviderProxy.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsPropertyProviderProxy.java
@@ -34,11 +34,10 @@ import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.SystemException;
-import org.apache.ambari.server.controller.utilities.StreamProvider;
+
 import java.util.Map;
 import java.util.Set;
 
-import static org.apache.ambari.server.controller.metrics.MetricsPaddingMethod.ZERO_PADDING_PARAM;
 import static org.apache.ambari.server.controller.metrics.MetricsServiceProvider.MetricsService;
 import static org.apache.ambari.server.controller.metrics.MetricsServiceProvider.MetricsService.GANGLIA;
 import static org.apache.ambari.server.controller.metrics.MetricsServiceProvider.MetricsService.TIMELINE_METRICS;
@@ -49,6 +48,7 @@ public class MetricsPropertyProviderProxy extends AbstractPropertyProvider {
   private AMSPropertyProvider amsPropertyProvider;
   private GangliaPropertyProvider gangliaPropertyProvider;
   private TimelineMetricCacheProvider cacheProvider;
+  private String clusterNamePropertyId;
 
   public MetricsPropertyProviderProxy(
     InternalType type,
@@ -65,6 +65,7 @@ public class MetricsPropertyProviderProxy extends AbstractPropertyProvider {
     super(componentPropertyInfoMap);
     this.metricsServiceProvider = serviceProvider;
     this.cacheProvider = cacheProvider;
+    this.clusterNamePropertyId = clusterNamePropertyId;
 
     switch (type) {
       case Host:
@@ -183,6 +184,10 @@ public class MetricsPropertyProviderProxy extends AbstractPropertyProvider {
   public Set<Resource> populateResources(Set<Resource> resources, Request request,
                                          Predicate predicate) throws SystemException {
 
+    if(!checkAuthorizationForMetrics(resources, clusterNamePropertyId)) {
+      return resources;
+    }
+
     MetricsService metricsService = metricsServiceProvider.getMetricsServiceType();
 
     if (metricsService != null) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsReportPropertyProviderProxy.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsReportPropertyProviderProxy.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsReportPropertyProviderProxy.java
index 4d2ce01..f28c34d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsReportPropertyProviderProxy.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/MetricsReportPropertyProviderProxy.java
@@ -28,7 +28,6 @@ import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.SystemException;
-import org.apache.ambari.server.controller.utilities.StreamProvider;
 
 import java.util.Map;
 import java.util.Set;
@@ -42,6 +41,7 @@ public class MetricsReportPropertyProviderProxy extends AbstractPropertyProvider
   private MetricsReportPropertyProvider gangliaMetricsReportProvider;
   private final MetricsServiceProvider metricsServiceProvider;
   private TimelineMetricCacheProvider cacheProvider;
+  private String clusterNamePropertyId;
 
   public MetricsReportPropertyProviderProxy(
     Map<String, Map<String, PropertyInfo>> componentPropertyInfoMap,
@@ -56,6 +56,7 @@ public class MetricsReportPropertyProviderProxy extends AbstractPropertyProvider
     super(componentPropertyInfoMap);
     this.metricsServiceProvider = serviceProvider;
     this.cacheProvider = cacheProvider;
+    this.clusterNamePropertyId = clusterNamePropertyId;
 
     createReportPropertyProviders(componentPropertyInfoMap,
       streamProvider,
@@ -105,6 +106,9 @@ public class MetricsReportPropertyProviderProxy extends AbstractPropertyProvider
   public Set<Resource> populateResources(Set<Resource> resources, Request request,
                                          Predicate predicate) throws SystemException {
 
+    if(!checkAuthorizationForMetrics(resources, clusterNamePropertyId)) {
+      return resources;
+    }
     MetricsService metricsService = metricsServiceProvider.getMetricsServiceType();
 
     if (metricsService != null) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java
index b32adda..fc76b1e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java
@@ -127,7 +127,7 @@ public class RestMetricsPropertyProvider extends ThreadPoolEnabledPropertyProvid
     String statePropertyId,
     String componentName){
 
-    super(componentMetrics, hostNamePropertyId, metricHostProvider);
+    super(componentMetrics, hostNamePropertyId, metricHostProvider, clusterNamePropertyId);
     this.metricsProperties = metricsProperties;
     this.streamProvider = streamProvider;
     this.clusterNamePropertyId = clusterNamePropertyId;

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/ThreadPoolEnabledPropertyProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/ThreadPoolEnabledPropertyProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/ThreadPoolEnabledPropertyProvider.java
index 8a35636..1e961a6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/ThreadPoolEnabledPropertyProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/ThreadPoolEnabledPropertyProvider.java
@@ -51,6 +51,7 @@ public abstract class ThreadPoolEnabledPropertyProvider extends AbstractProperty
   public static final Set<String> healthyStates = Collections.singleton("STARTED");
   protected final String hostNamePropertyId;
   private final MetricHostProvider metricHostProvider;
+  private final String clusterNamePropertyId;
 
   /**
    * Executor service is shared between all childs of current class
@@ -78,10 +79,12 @@ public abstract class ThreadPoolEnabledPropertyProvider extends AbstractProperty
    */
   public ThreadPoolEnabledPropertyProvider(Map<String, Map<String, PropertyInfo>> componentMetrics,
                                            String hostNamePropertyId,
-                                           MetricHostProvider metricHostProvider) {
+                                           MetricHostProvider metricHostProvider,
+                                           String clusterNamePropertyId) {
     super(componentMetrics);
     this.hostNamePropertyId = hostNamePropertyId;
     this.metricHostProvider = metricHostProvider;
+    this.clusterNamePropertyId = clusterNamePropertyId;
   }
 
   // ----- Thread pool -------------------------------------------------------
@@ -117,6 +120,9 @@ public abstract class ThreadPoolEnabledPropertyProvider extends AbstractProperty
   public Set<Resource> populateResources(Set<Resource> resources, Request request, Predicate predicate)
       throws SystemException {
 
+    if(!checkAuthorizationForMetrics(resources, clusterNamePropertyId)) {
+      return resources;
+    }
     // Get a valid ticket for the request.
     Ticket ticket = new Ticket();
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/main/java/org/apache/ambari/server/security/authorization/AuthorizationHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/security/authorization/AuthorizationHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/security/authorization/AuthorizationHelper.java
index 0c675b8..b136182 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/security/authorization/AuthorizationHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/security/authorization/AuthorizationHelper.java
@@ -160,6 +160,9 @@ public class AuthorizationHelper {
         if (ResourceType.AMBARI == privilegeResourceType) {
           // This resource type indicates administrative access
           resourceOK = true;
+        } else if (ResourceType.VIEW == privilegeResourceType) {
+          // For a VIEW USER.
+          resourceOK = true;
         } else if ((resourceType == null) || (resourceType == privilegeResourceType)) {
           resourceOK = (resourceId == null) || resourceId.equals(privilegeResource.getId());
         } else {

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java
index bb6673c..0ae3e6a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java
@@ -17,22 +17,17 @@
  */
 package org.apache.ambari.server.controller.internal;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
 import com.google.inject.Binder;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
 import com.google.inject.Module;
+import com.google.inject.persist.PersistService;
 import com.google.inject.util.Modules;
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.configuration.ComponentSSLConfiguration;
 import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.controller.AmbariManagementController;
+import org.apache.ambari.server.controller.AmbariServer;
 import org.apache.ambari.server.controller.jmx.TestStreamProvider;
 import org.apache.ambari.server.controller.metrics.JMXPropertyProviderTest;
 import org.apache.ambari.server.controller.metrics.MetricsServiceProvider;
@@ -48,10 +43,11 @@ import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.TemporalInfo;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
-import org.apache.ambari.server.controller.utilities.StreamProvider;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
+import org.apache.ambari.server.security.TestAuthenticationFactory;
+import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Host;
@@ -63,10 +59,23 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.springframework.security.core.context.SecurityContextHolder;
 
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
 
 /**
  * Tests the stack defined property provider.
@@ -75,6 +84,7 @@ public class StackDefinedPropertyProviderTest {
   private static final String HOST_COMPONENT_HOST_NAME_PROPERTY_ID = "HostRoles/host_name";
   private static final String HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID = "HostRoles/component_name";
   private static final String HOST_COMPONENT_STATE_PROPERTY_ID = "HostRoles/state";
+  private static final String CLUSTER_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "cluster_name");
 
   private Clusters clusters = null;
   private Injector injector = null;
@@ -111,9 +121,9 @@ public class StackDefinedPropertyProviderTest {
     clusters = injector.getInstance(Clusters.class);
     StackId stackId = new StackId("HDP-2.0.5");
 
-    clusters.addCluster("c1", stackId);
+    clusters.addCluster("c2", stackId);
 
-    Cluster cluster = clusters.getCluster("c1");
+    Cluster cluster = clusters.getCluster("c2");
 
     cluster.setDesiredStackVersion(stackId);
     helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
@@ -128,7 +138,25 @@ public class StackDefinedPropertyProviderTest {
     host.setHostAttributes(hostAttributes);
     host.persist();
 
-    clusters.mapHostToCluster("h1", "c1");
+    clusters.mapHostToCluster("h1", "c2");
+
+    // Setting up Mocks for Controller, Clusters etc, queried as part of user's Role context
+    // while fetching Metrics.
+    AmbariManagementController amc = createNiceMock(AmbariManagementController.class);
+    Field field = AmbariServer.class.getDeclaredField("clusterController");
+    field.setAccessible(true);
+    field.set(null, amc);
+    Clusters clustersMock = createNiceMock(Clusters.class);
+    Cluster clusterMock = createNiceMock(Cluster.class);
+    expect(amc.getClusters()).andReturn(clustersMock).anyTimes();
+    expect(clustersMock.getCluster(CLUSTER_NAME_PROPERTY_ID)).andReturn(clusterMock).anyTimes();
+    expect(clusterMock.getResourceId()).andReturn(2L).anyTimes();
+    try {
+      expect(clustersMock.getCluster(anyObject(String.class))).andReturn(clusterMock).anyTimes();
+    } catch (AmbariException e) {
+      e.printStackTrace();
+    }
+    replay(amc, clustersMock, clusterMock);
   }
 
   @After
@@ -137,6 +165,95 @@ public class StackDefinedPropertyProviderTest {
   }
 
   @Test
+  public void testStackDefinedPropertyProviderAsClusterAdministrator() throws Exception {
+    //Setup user with Role 'ClusterAdministrator'.
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createClusterAdministrator("ClusterAdmin", 2L));
+
+    testPopulateHostComponentResources();
+    testCustomProviders();
+    testPopulateResources_HDP2();
+    testPopulateResources_HDP2_params();
+    testPopulateResources_HDP2_params_singleProperty();
+    testPopulateResources_HDP2_params_category();
+    testPopulateResources_HDP2_params_category2();
+    testPopulateResources_jmx_JournalNode();
+    testPopulateResources_jmx_Storm();
+    testPopulateResources_NoRegionServer();
+    testPopulateResources_HBaseMaster2();
+    testPopulateResources_params_category5();
+    testPopulateResources_ganglia_JournalNode();
+    testPopulateResources_resourcemanager_clustermetrics();
+    testPopulateResourcesWithAggregateFunctionMetrics();
+  }
+
+  @Test
+  public void testStackDefinedPropertyProviderAsAdministrator() throws Exception {
+    //Setup user with Role 'Administrator'
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("Admin"));
+
+    testPopulateHostComponentResources();
+    testCustomProviders();
+    testPopulateResources_HDP2();
+    testPopulateResources_HDP2_params();
+    testPopulateResources_HDP2_params_singleProperty();
+    testPopulateResources_HDP2_params_category();
+    testPopulateResources_HDP2_params_category2();
+    testPopulateResources_jmx_JournalNode();
+    testPopulateResources_jmx_Storm();
+    testPopulateResources_NoRegionServer();
+    testPopulateResources_HBaseMaster2();
+    testPopulateResources_params_category5();
+    testPopulateResources_ganglia_JournalNode();
+    testPopulateResources_resourcemanager_clustermetrics();
+    testPopulateResourcesWithAggregateFunctionMetrics();
+  }
+
+  @Test
+  public void testStackDefinedPropertyProviderAsServiceAdministrator() throws Exception {
+    //Setup user with 'ServiceAdministrator'
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createServiceAdministrator("ServiceAdmin", 2L));
+
+    testPopulateHostComponentResources();
+    testCustomProviders();
+    testPopulateResources_HDP2();
+    testPopulateResources_HDP2_params();
+    testPopulateResources_HDP2_params_singleProperty();
+    testPopulateResources_HDP2_params_category();
+    testPopulateResources_HDP2_params_category2();
+    testPopulateResources_jmx_JournalNode();
+    testPopulateResources_jmx_Storm();
+    testPopulateResources_NoRegionServer();
+    testPopulateResources_HBaseMaster2();
+    testPopulateResources_params_category5();
+    testPopulateResources_ganglia_JournalNode();
+    testPopulateResources_resourcemanager_clustermetrics();
+    testPopulateResourcesWithAggregateFunctionMetrics();
+  }
+
+  @Test(expected = AuthorizationException.class)
+  public void testStackDefinedPropertyProviderAsViewUser() throws Exception {
+    // Setup user with 'ViewUser'
+    // ViewUser doesn't have the 'CLUSTER_VIEW_METRICS', 'HOST_VIEW_METRICS' and 'SERVICE_VIEW_METRICS', thus
+    // can't retrieve the Metrics.
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createViewUser("ViewUser", 2L));
+
+    testPopulateHostComponentResources();
+    testCustomProviders();
+    testPopulateResources_HDP2();
+    testPopulateResources_HDP2_params();
+    testPopulateResources_HDP2_params_singleProperty();
+    testPopulateResources_HDP2_params_category();
+    testPopulateResources_HDP2_params_category2();
+    testPopulateResources_jmx_JournalNode();
+    testPopulateResources_jmx_Storm();
+    testPopulateResources_NoRegionServer();
+    testPopulateResources_HBaseMaster2();
+    testPopulateResources_params_category5();
+    testPopulateResources_ganglia_JournalNode();
+    testPopulateResources_resourcemanager_clustermetrics();
+    testPopulateResourcesWithAggregateFunctionMetrics();
+  }
+
   public void testPopulateHostComponentResources() throws Exception {
     JMXPropertyProviderTest.TestJMXHostProvider tj = new JMXPropertyProviderTest.TestJMXHostProvider(true);
     JMXPropertyProviderTest.TestMetricHostProvider tm = new JMXPropertyProviderTest.TestMetricHostProvider();
@@ -150,7 +267,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty("HostRoles/host_name", "h1");
     resource.setProperty("HostRoles/component_name", "NAMENODE");
     resource.setProperty("HostRoles/state", "STARTED");
@@ -171,7 +288,6 @@ public class StackDefinedPropertyProviderTest {
   }
 
 
-  @Test
   public void testCustomProviders() throws Exception {
 
     StackDefinedPropertyProvider sdpp = new StackDefinedPropertyProvider(
@@ -181,7 +297,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty("HostRoles/host_name", "h1");
     resource.setProperty("HostRoles/component_name", "DATANODE");
     resource.setProperty("HostRoles/state", "STARTED");
@@ -326,7 +442,6 @@ public class StackDefinedPropertyProviderTest {
     }
   }
 
-  @Test
   public void testPopulateResources_HDP2() throws Exception {
 
     URLStreamProvider  streamProvider = new TestStreamProvider();
@@ -350,7 +465,7 @@ public class StackDefinedPropertyProviderTest {
     // resourcemanager
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "RESOURCEMANAGER");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -378,7 +493,7 @@ public class StackDefinedPropertyProviderTest {
     //namenode
     resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "NAMENODE");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -389,7 +504,6 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertEquals(1, propertyProvider.populateResources(Collections.singleton(resource), request, null).size());
   }
 
-  @Test
   public void testPopulateResources_HDP2_params() throws Exception {
     TestStreamProvider  streamProvider = new TestStreamProvider();
     JMXPropertyProviderTest.TestJMXHostProvider hostProvider = new JMXPropertyProviderTest.TestJMXHostProvider(false);
@@ -411,7 +525,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "h1");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "RESOURCEMANAGER");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -442,7 +556,6 @@ public class StackDefinedPropertyProviderTest {
   }
 
 
-  @Test
   public void testPopulateResources_HDP2_params_singleProperty() throws Exception {
     TestStreamProvider  streamProvider = new TestStreamProvider();
     JMXPropertyProviderTest.TestJMXHostProvider hostProvider = new JMXPropertyProviderTest.TestJMXHostProvider(false);
@@ -464,7 +577,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "h1");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "RESOURCEMANAGER");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -480,7 +593,6 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertNull(resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/yarn/Queue/root", "AvailableVCores")));
   }
 
-  @Test
   public void testPopulateResources_HDP2_params_category() throws Exception {
     TestStreamProvider  streamProvider = new TestStreamProvider();
     JMXPropertyProviderTest.TestJMXHostProvider hostProvider = new JMXPropertyProviderTest.TestJMXHostProvider(false);
@@ -502,7 +614,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "h1");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "RESOURCEMANAGER");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -533,7 +645,6 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertEquals(1,    resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/yarn/Queue/root/second_queue", "AppsSubmitted")));
   }
 
-  @Test
   public void testPopulateResources_HDP2_params_category2() throws Exception {
     TestStreamProvider  streamProvider = new TestStreamProvider();
     JMXPropertyProviderTest.TestJMXHostProvider hostProvider = new JMXPropertyProviderTest.TestJMXHostProvider(false);
@@ -555,7 +666,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "h1");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "RESOURCEMANAGER");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -592,7 +703,6 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertNull(resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/yarn/Queue/root/second_queue", "AppsSubmitted")));
   }
 
-  @Test
   public void testPopulateResources_jmx_JournalNode() throws Exception {
     TestStreamProvider  streamProvider = new TestStreamProvider();
     JMXPropertyProviderTest.TestJMXHostProvider hostProvider = new JMXPropertyProviderTest.TestJMXHostProvider(false);
@@ -614,7 +724,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "JOURNALNODE");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -712,10 +822,9 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertEquals(8444, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/dfs/journalnode/cluster/mycluster", "lastWrittenTxId")));
   }
 
-  @Test
   public void testPopulateResources_jmx_Storm() throws Exception {
     // Adjust stack version for cluster
-    Cluster cluster = clusters.getCluster("c1");
+    Cluster cluster = clusters.getCluster("c2");
     cluster.setDesiredStackVersion(new StackId("HDP-2.1.1"));
 
     TestStreamProvider  streamProvider = new TestStreamProvider();
@@ -739,7 +848,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "STORM_REST_API");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -760,7 +869,6 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertEquals(4637.0, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/api/cluster/summary", "nimbus.uptime")));
   }
 
-  @Test
   public void testPopulateResources_NoRegionServer() throws Exception {
     TestStreamProvider  streamProvider = new TestStreamProvider();
     JMXPropertyProviderTest.TestJMXHostProvider hostProvider = new JMXPropertyProviderTest.TestJMXHostProvider(false);
@@ -782,7 +890,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "HBASE_REGIONSERVER");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -797,7 +905,6 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertEquals(preSize, resource.getPropertiesMap().size());
   }
 
-  @Test
   public void testPopulateResources_HBaseMaster2() throws Exception {
     TestStreamProvider  streamProvider = new TestStreamProvider();
     JMXPropertyProviderTest.TestJMXHostProvider hostProvider = new JMXPropertyProviderTest.TestJMXHostProvider(false);
@@ -819,7 +926,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "HBASE_MASTER");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -838,7 +945,6 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertTrue(map.get("metrics/hbase/master").containsKey("IsActiveMaster"));
   }
 
-  @Test
   public void testPopulateResources_params_category5() throws Exception {
     org.apache.ambari.server.controller.metrics.ganglia.TestStreamProvider streamProvider =
         new org.apache.ambari.server.controller.metrics.ganglia.TestStreamProvider("temporal_ganglia_data_yarn_queues.txt");
@@ -863,7 +969,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "dev01.ambari.apache.org");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "RESOURCEMANAGER");
 
@@ -886,7 +992,6 @@ public class StackDefinedPropertyProviderTest {
     Assert.assertNotNull(resource.getPropertyValue(RM_AVAILABLE_MEMORY_PROPERTY));
   }
 
-  @Test
   public void testPopulateResources_ganglia_JournalNode() throws Exception {
     org.apache.ambari.server.controller.metrics.ganglia.TestStreamProvider streamProvider =
         new org.apache.ambari.server.controller.metrics.ganglia.TestStreamProvider("journalnode_ganglia_data.txt");
@@ -911,7 +1016,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "JOURNALNODE");
 
@@ -1001,7 +1106,6 @@ public class StackDefinedPropertyProviderTest {
     }
   }
 
-  @Test
   public void testPopulateResources_resourcemanager_clustermetrics() throws Exception {
 
     String[] metrics = new String[] {
@@ -1035,7 +1139,7 @@ public class StackDefinedPropertyProviderTest {
     for (String metric : metrics) {
       Resource resource = new ResourceImpl(Resource.Type.HostComponent);
 
-      resource.setProperty("HostRoles/cluster_name", "c1");
+      resource.setProperty("HostRoles/cluster_name", "c2");
       resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal");
       resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "RESOURCEMANAGER");
 
@@ -1053,7 +1157,6 @@ public class StackDefinedPropertyProviderTest {
 
   }
 
-  @Test
   public void testPopulateResourcesWithAggregateFunctionMetrics() throws Exception {
 
     String metric = "metrics/rpc/NumOpenConnections._sum";
@@ -1086,7 +1189,7 @@ public class StackDefinedPropertyProviderTest {
 
     Resource resource = new ResourceImpl(Resource.Type.Component);
 
-    resource.setProperty("HostRoles/cluster_name", "c1");
+    resource.setProperty("HostRoles/cluster_name", "c2");
     resource.setProperty("HostRoles/service_name", "HBASE");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "HBASE_REGIONSERVER");
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/57d08170/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/JMXPropertyProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/JMXPropertyProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/JMXPropertyProviderTest.java
index f0c1280..f76c322 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/JMXPropertyProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/JMXPropertyProviderTest.java
@@ -18,6 +18,9 @@
 
 package org.apache.ambari.server.controller.metrics;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.controller.AmbariManagementController;
+import org.apache.ambari.server.controller.AmbariServer;
 import org.apache.ambari.server.controller.internal.ResourceImpl;
 import org.apache.ambari.server.controller.jmx.JMXHostProvider;
 import org.apache.ambari.server.controller.jmx.JMXPropertyProvider;
@@ -27,58 +30,150 @@ import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.TemporalInfo;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.apache.ambari.server.security.TestAuthenticationFactory;
+import org.apache.ambari.server.security.authorization.AuthorizationException;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.junit.After;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
+import org.springframework.security.core.context.SecurityContextHolder;
+
+import java.lang.reflect.Field;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+
 import static org.apache.ambari.server.controller.metrics.MetricsServiceProvider.MetricsService;
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
 
 /**
  * JMX property provider tests.
  */
 public class JMXPropertyProviderTest {
+  protected static final String CLUSTER_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "cluster_name");
   protected static final String HOST_COMPONENT_HOST_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "host_name");
   protected static final String HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "component_name");
   protected static final String HOST_COMPONENT_STATE_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "state");
 
   public static final int NUMBER_OF_RESOURCES = 400;
 
+  @Before
+  public void setUpCommonMocks() throws AmbariException, NoSuchFieldException, IllegalAccessException {
+    AmbariManagementController amc = createNiceMock(AmbariManagementController.class);
+    Field field = AmbariServer.class.getDeclaredField("clusterController");
+    field.setAccessible(true);
+    field.set(null, amc);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    expect(amc.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster(CLUSTER_NAME_PROPERTY_ID)).andReturn(cluster).anyTimes();
+    expect(cluster.getResourceId()).andReturn(2L).anyTimes();
+
+    try {
+      expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster).anyTimes();
+    } catch (AmbariException e) {
+      e.printStackTrace();
+    }
+
+    replay(amc, clusters, cluster);
+  }
+
+  @After
+  public void clearAuthentication() {
+    SecurityContextHolder.getContext().setAuthentication(null);
+  }
+
   @Test
+  public void testJMXPropertyProviderAsClusterAdministrator() throws Exception {
+    //Setup user with Role 'ClusterAdministrator'.
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createClusterAdministrator("ClusterAdmin", 2L));
+    testPopulateResources();
+    testPopulateResources_singleProperty();
+    testPopulateResources_category();
+    testPopulateResourcesWithUnknownPort();
+    testPopulateResourcesUnhealthyResource();
+    testPopulateResourcesMany();
+    testPopulateResourcesTimeout();
+  }
+
+  @Test
+  public void testJMXPropertyProviderAsAdministrator() throws Exception {
+    //Setup user with Role 'Administrator'
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("Admin"));
+    testPopulateResources();
+    testPopulateResources_singleProperty();
+    testPopulateResources_category();
+    testPopulateResourcesWithUnknownPort();
+    testPopulateResourcesUnhealthyResource();
+    testPopulateResourcesMany();
+    testPopulateResourcesTimeout();
+  }
+
+  @Test
+  public void testJMXPropertyProviderAsServiceAdministrator() throws Exception {
+    //Setup user with 'ServiceAdministrator'
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createServiceAdministrator("ServiceAdmin", 2L));
+    testPopulateResources();
+    testPopulateResources_singleProperty();
+    testPopulateResources_category();
+    testPopulateResourcesWithUnknownPort();
+    testPopulateResourcesUnhealthyResource();
+    testPopulateResourcesMany();
+    testPopulateResourcesTimeout();
+  }
+
+  @Test(expected = AuthorizationException.class)
+  public void testJMXPropertyProviderAsViewUser() throws Exception {
+    // Setup user with 'ViewUser'
+    // ViewUser doesn't have the 'CLUSTER_VIEW_METRICS', 'HOST_VIEW_METRICS' and 'SERVICE_VIEW_METRICS', thus
+    // can't retrieve the Metrics.
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createViewUser("ViewUser", 2L));
+    testPopulateResources();
+    testPopulateResources_singleProperty();
+    testPopulateResources_category();
+    testPopulateResourcesWithUnknownPort();
+    testPopulateResourcesUnhealthyResource();
+    testPopulateResourcesMany();
+    testPopulateResourcesTimeout();
+  }
+
   public void testPopulateResources() throws Exception {
-    TestStreamProvider  streamProvider = new TestStreamProvider();
+    TestStreamProvider streamProvider = new TestStreamProvider();
     TestJMXHostProvider hostProvider = new TestJMXHostProvider(false);
     TestMetricHostProvider metricsHostProvider = new TestMetricHostProvider();
-
     JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
-        PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
-        streamProvider,
-        hostProvider,
-        metricsHostProvider,
-        PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
-        PropertyHelper.getPropertyId("HostRoles", "host_name"),
-        PropertyHelper.getPropertyId("HostRoles", "component_name"),
-        PropertyHelper.getPropertyId("HostRoles", "state"));
+      PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
+      streamProvider,
+      hostProvider,
+      metricsHostProvider,
+      PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
+      PropertyHelper.getPropertyId("HostRoles", "host_name"),
+      PropertyHelper.getPropertyId("HostRoles", "component_name"),
+      PropertyHelper.getPropertyId("HostRoles", "state"));
 
     // namenode
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
-
+    resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "NAMENODE");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
 
     // request with an empty set should get all supported properties
     Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet());
-
     Assert.assertEquals(1, propertyProvider.populateResources(Collections.singleton(resource), request, null).size());
 
     Assert.assertEquals(propertyProvider.getSpec("http", "domu-12-31-39-0e-34-e1.compute-1.internal", "50070", "/jmx"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_namenode_jmx.json for values
-    Assert.assertEquals(13670605,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
-    Assert.assertEquals(28,      resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/dfs/namenode", "CreateFileOps")));
+    Assert.assertEquals(13670605, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
+    Assert.assertEquals(28, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/dfs/namenode", "CreateFileOps")));
     Assert.assertEquals(1006632960, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
     Assert.assertEquals(473433016, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
     Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
@@ -90,7 +185,7 @@ public class JMXPropertyProviderTest {
 
     // datanode
     resource = new ResourceImpl(Resource.Type.HostComponent);
-
+    resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-14-ee-b3.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "DATANODE");
 
@@ -102,7 +197,7 @@ public class JMXPropertyProviderTest {
     Assert.assertEquals(propertyProvider.getSpec("http", "domu-12-31-39-14-ee-b3.compute-1.internal", "50075", "/jmx"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_datanode_jmx.json for values
-    Assert.assertEquals(856,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
+    Assert.assertEquals(856, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
     Assert.assertEquals(954466304, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
     Assert.assertEquals(9772616, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
     Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
@@ -114,7 +209,7 @@ public class JMXPropertyProviderTest {
 
     // hbase master
     resource = new ResourceImpl(Resource.Type.HostComponent);
-
+    resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-14-ee-b3.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "HBASE_MASTER");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -132,7 +227,7 @@ public class JMXPropertyProviderTest {
 
     Assert.assertEquals(propertyProvider.getSpec("http", "domu-12-31-39-14-ee-b3.compute-1.internal", "60010", "/jmx"), streamProvider.getLastSpec());
 
-    Assert.assertEquals(8, PropertyHelper.getProperties(resource).size());
+    Assert.assertEquals(9, PropertyHelper.getProperties(resource).size());
     Assert.assertEquals(1069416448, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
     Assert.assertEquals(4806976, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
     Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
@@ -142,61 +237,59 @@ public class JMXPropertyProviderTest {
     Assert.assertNull(resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "gcCount")));
   }
 
-  @Test
   public void testPopulateResources_singleProperty() throws Exception {
-    TestStreamProvider  streamProvider = new TestStreamProvider();
+    TestStreamProvider streamProvider = new TestStreamProvider();
     TestJMXHostProvider hostProvider = new TestJMXHostProvider(false);
     TestMetricHostProvider metricsHostProvider = new TestMetricHostProvider();
 
     JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
-        PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
-        streamProvider,
-        hostProvider,
-        metricsHostProvider,
-        PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
-        PropertyHelper.getPropertyId("HostRoles", "host_name"),
-        PropertyHelper.getPropertyId("HostRoles", "component_name"),
-        PropertyHelper.getPropertyId("HostRoles", "state"));
+      PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
+      streamProvider,
+      hostProvider,
+      metricsHostProvider,
+      PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
+      PropertyHelper.getPropertyId("HostRoles", "host_name"),
+      PropertyHelper.getPropertyId("HostRoles", "component_name"),
+      PropertyHelper.getPropertyId("HostRoles", "state"));
 
     // namenode
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
-
+    resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "NAMENODE");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
 
     // only ask for one property
     Map<String, TemporalInfo> temporalInfoMap = new HashMap<String, TemporalInfo>();
-    Request  request = PropertyHelper.getReadRequest(Collections.singleton("metrics/rpc/ReceivedBytes"), temporalInfoMap);
+    Request request = PropertyHelper.getReadRequest(Collections.singleton("metrics/rpc/ReceivedBytes"), temporalInfoMap);
 
     Assert.assertEquals(1, propertyProvider.populateResources(Collections.singleton(resource), request, null).size());
 
     Assert.assertEquals(propertyProvider.getSpec("http", "domu-12-31-39-0e-34-e1.compute-1.internal", "50070", "/jmx"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_namenode_jmx.json for values
-    Assert.assertEquals(13670605,  resource.getPropertyValue("metrics/rpc/ReceivedBytes"));
+    Assert.assertEquals(13670605, resource.getPropertyValue("metrics/rpc/ReceivedBytes"));
     Assert.assertNull(resource.getPropertyValue("metrics/dfs/namenode/CreateFileOps"));
   }
 
-  @Test
   public void testPopulateResources_category() throws Exception {
-    TestStreamProvider  streamProvider = new TestStreamProvider();
+    TestStreamProvider streamProvider = new TestStreamProvider();
     TestJMXHostProvider hostProvider = new TestJMXHostProvider(false);
     TestMetricHostProvider metricsHostProvider = new TestMetricHostProvider();
 
     JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
-        PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
-        streamProvider,
-        hostProvider,
-        metricsHostProvider,
-        PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
-        PropertyHelper.getPropertyId("HostRoles", "host_name"),
-        PropertyHelper.getPropertyId("HostRoles", "component_name"),
-        PropertyHelper.getPropertyId("HostRoles", "state"));
+      PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
+      streamProvider,
+      hostProvider,
+      metricsHostProvider,
+      PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
+      PropertyHelper.getPropertyId("HostRoles", "host_name"),
+      PropertyHelper.getPropertyId("HostRoles", "component_name"),
+      PropertyHelper.getPropertyId("HostRoles", "state"));
 
     // namenode
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
-
+    resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "NAMENODE");
     resource.setProperty(HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED");
@@ -204,37 +297,36 @@ public class JMXPropertyProviderTest {
     // request with an empty set should get all supported properties
     // only ask for one property
     Map<String, TemporalInfo> temporalInfoMap = new HashMap<String, TemporalInfo>();
-    Request  request = PropertyHelper.getReadRequest(Collections.singleton("metrics/dfs"), temporalInfoMap);
+    Request request = PropertyHelper.getReadRequest(Collections.singleton("metrics/dfs"), temporalInfoMap);
 
     Assert.assertEquals(1, propertyProvider.populateResources(Collections.singleton(resource), request, null).size());
 
-    Assert.assertEquals(propertyProvider.getSpec("http","domu-12-31-39-0e-34-e1.compute-1.internal", "50070","/jmx"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("http", "domu-12-31-39-0e-34-e1.compute-1.internal", "50070", "/jmx"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_namenode_jmx.json for values
-    Assert.assertEquals(184320,  resource.getPropertyValue("metrics/dfs/FSNamesystem/CapacityUsed"));
-    Assert.assertEquals(21,  resource.getPropertyValue("metrics/dfs/FSNamesystem/UnderReplicatedBlocks"));
+    Assert.assertEquals(184320, resource.getPropertyValue("metrics/dfs/FSNamesystem/CapacityUsed"));
+    Assert.assertEquals(21, resource.getPropertyValue("metrics/dfs/FSNamesystem/UnderReplicatedBlocks"));
     Assert.assertNull(resource.getPropertyValue("metrics/rpc/ReceivedBytes"));
   }
 
-  @Test
   public void testPopulateResourcesWithUnknownPort() throws Exception {
-    TestStreamProvider  streamProvider = new TestStreamProvider();
+    TestStreamProvider streamProvider = new TestStreamProvider();
     TestJMXHostProvider hostProvider = new TestJMXHostProvider(true);
     TestMetricHostProvider metricsHostProvider = new TestMetricHostProvider();
 
     JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
-        PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
-        streamProvider,
-        hostProvider,
-        metricsHostProvider,
-        PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
-        PropertyHelper.getPropertyId("HostRoles", "host_name"),
-        PropertyHelper.getPropertyId("HostRoles", "component_name"),
-        PropertyHelper.getPropertyId("HostRoles", "state"));
+      PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
+      streamProvider,
+      hostProvider,
+      metricsHostProvider,
+      PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
+      PropertyHelper.getPropertyId("HostRoles", "host_name"),
+      PropertyHelper.getPropertyId("HostRoles", "component_name"),
+      PropertyHelper.getPropertyId("HostRoles", "state"));
 
     // namenode
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
-
+    resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "NAMENODE");
 
@@ -243,32 +335,31 @@ public class JMXPropertyProviderTest {
 
     Assert.assertEquals(1, propertyProvider.populateResources(Collections.singleton(resource), request, null).size());
 
-    Assert.assertEquals(propertyProvider.getSpec("http","domu-12-31-39-0e-34-e1.compute-1.internal", "50070","/jmx"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("http", "domu-12-31-39-0e-34-e1.compute-1.internal", "50070", "/jmx"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_namenode_jmx.json for values
-    Assert.assertEquals(13670605,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
-    Assert.assertEquals(28,      resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/dfs/namenode", "CreateFileOps")));
+    Assert.assertEquals(13670605, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
+    Assert.assertEquals(28, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/dfs/namenode", "CreateFileOps")));
     Assert.assertEquals(1006632960, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
     Assert.assertEquals(473433016, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
     Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
     Assert.assertEquals(23634400, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed")));
   }
 
-  @Test
   public void testPopulateResourcesUnhealthyResource() throws Exception {
-    TestStreamProvider  streamProvider = new TestStreamProvider();
+    TestStreamProvider streamProvider = new TestStreamProvider();
     TestJMXHostProvider hostProvider = new TestJMXHostProvider(true);
     TestMetricHostProvider metricsHostProvider = new TestMetricHostProvider();
 
     JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
-        PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
-        streamProvider,
-        hostProvider,
-        metricsHostProvider,
-        PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
-        PropertyHelper.getPropertyId("HostRoles", "host_name"),
-        PropertyHelper.getPropertyId("HostRoles", "component_name"),
-        PropertyHelper.getPropertyId("HostRoles", "state"));
+      PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
+      streamProvider,
+      hostProvider,
+      metricsHostProvider,
+      PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
+      PropertyHelper.getPropertyId("HostRoles", "host_name"),
+      PropertyHelper.getPropertyId("HostRoles", "component_name"),
+      PropertyHelper.getPropertyId("HostRoles", "state"));
 
     // namenode
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
@@ -286,45 +377,42 @@ public class JMXPropertyProviderTest {
     Assert.assertNull(streamProvider.getLastSpec());
   }
 
-  @Test
   public void testPopulateResourcesMany() throws Exception {
     // Set the provider to take 50 millis to return the JMX values
-    TestStreamProvider  streamProvider = new TestStreamProvider(50L);
+    TestStreamProvider streamProvider = new TestStreamProvider(50L);
     TestJMXHostProvider hostProvider = new TestJMXHostProvider(true);
     TestMetricHostProvider metricsHostProvider = new TestMetricHostProvider();
     Set<Resource> resources = new HashSet<Resource>();
 
     JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
-        PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
-        streamProvider,
-        hostProvider,
-        metricsHostProvider,
-        PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
-        PropertyHelper.getPropertyId("HostRoles", "host_name"),
-        PropertyHelper.getPropertyId("HostRoles", "component_name"),
-        PropertyHelper.getPropertyId("HostRoles", "state"));
+      PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
+      streamProvider,
+      hostProvider,
+      metricsHostProvider,
+      PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
+      PropertyHelper.getPropertyId("HostRoles", "host_name"),
+      PropertyHelper.getPropertyId("HostRoles", "component_name"),
+      PropertyHelper.getPropertyId("HostRoles", "state"));
 
     for (int i = 0; i < NUMBER_OF_RESOURCES; ++i) {
       // datanode
       Resource resource = new ResourceImpl(Resource.Type.HostComponent);
-
+      resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
       resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-14-ee-b3.compute-1.internal");
       resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "DATANODE");
       resource.setProperty("unique_id", i);
 
       resources.add(resource);
     }
-
     // request with an empty set should get all supported properties
     Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet());
 
     Set<Resource> resourceSet = propertyProvider.populateResources(resources, request, null);
 
     Assert.assertEquals(NUMBER_OF_RESOURCES, resourceSet.size());
-
     for (Resource resource : resourceSet) {
       // see test/resources/hdfs_datanode_jmx.json for values
-      Assert.assertEquals(856,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
+      Assert.assertEquals(856, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
       Assert.assertEquals(954466304, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
       Assert.assertEquals(9772616, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
       Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
@@ -332,30 +420,29 @@ public class JMXPropertyProviderTest {
     }
   }
 
-  @Test
   public void testPopulateResourcesTimeout() throws Exception {
     // Set the provider to take 100 millis to return the JMX values
-    TestStreamProvider  streamProvider = new TestStreamProvider(100L);
+    TestStreamProvider streamProvider = new TestStreamProvider(100L);
     TestJMXHostProvider hostProvider = new TestJMXHostProvider(true);
     TestMetricHostProvider metricsHostProvider = new TestMetricHostProvider();
     Set<Resource> resources = new HashSet<Resource>();
 
     JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
-        PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
-        streamProvider,
-        hostProvider,
-        metricsHostProvider,
-        "HostRoles/cluster_name",
-        "HostRoles/host_name",
-        "HostRoles/component_name",
-        "HostRoles/state");
+      PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
+      streamProvider,
+      hostProvider,
+      metricsHostProvider,
+      "HostRoles/cluster_name",
+      "HostRoles/host_name",
+      "HostRoles/component_name",
+      "HostRoles/state");
 
     // set the provider timeout to 50 millis
     propertyProvider.setPopulateTimeout(50L);
 
     // datanode
     Resource resource = new ResourceImpl(Resource.Type.HostComponent);
-
+    resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-14-ee-b3.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "DATANODE");
 
@@ -398,7 +485,7 @@ public class JMXPropertyProviderTest {
 
     @Override
     public String getPort(String clusterName, String componentName) throws
-        SystemException {
+      SystemException {
 
       if (unknownPort) {
         return null;
@@ -409,10 +496,16 @@ public class JMXPropertyProviderTest {
       else if (componentName.equals("DATANODE"))
         return "50075";
       else if (componentName.equals("HBASE_MASTER"))
-        return null == clusterName ? "60010" : "60011";
-      else  if (componentName.equals("JOURNALNODE"))
+        if(clusterName == "c2") {
+          return "60011";
+        } else {
+          // Caters the case where 'clusterName' is null or
+          // any other name (includes hardcoded name "c1").
+          return "60010";
+        }
+      else if (componentName.equals("JOURNALNODE"))
         return "8480";
-      else  if (componentName.equals("STORM_REST_API"))
+      else if (componentName.equals("STORM_REST_API"))
         return "8745";
       else
         return null;


Mime
View raw message