@Test public void testResolvePort() throws Exception { MetricDefinition metricDefinition = createNiceMock(MetricDefinition.class); expect(metricDefinition.getMetrics()).andReturn(componentMetrics); expect(metricDefinition.getType()).andReturn("org.apache.ambari.server.controller.metrics.RestMetricsPropertyProvider"); expect(metricDefinition.getProperties()).andReturn(metricsProperties); replay(metricDefinition); Map<String, PropertyInfo> metrics = StackDefinedPropertyProvider.getPropertyInfo(metricDefinition); HashMap<String, Map<String, PropertyInfo>> componentMetrics = new HashMap<String, Map<String, PropertyInfo>>(); componentMetrics.put(WRAPPED_METRICS_KEY, metrics); TestStreamProvider streamProvider = new TestStreamProvider(); TestMetricsHostProvider metricsHostProvider = new TestMetricsHostProvider(); RestMetricsPropertyProvider restMetricsPropertyProvider = createRestMetricsPropertyProvider(metricDefinition, componentMetrics, streamProvider, metricsHostProvider); Map<String, String> customMetricsProperties = new HashMap<>(metricsProperties); customMetricsProperties.put("port_property_name", "wrong_property"); String resolvedPort = restMetricsPropertyProvider.resolvePort(c1, "domu-12-31-39-0e-34-e1.compute-1.internal", "STORM_REST_API", customMetricsProperties, "http"); Assert.assertEquals(DEFAULT_STORM_UI_PORT, resolvedPort); customMetricsProperties = new HashMap<>(metricsProperties); customMetricsProperties.put("default_port", "8746"); resolvedPort = restMetricsPropertyProvider.resolvePort(c1, "domu-12-31-39-0e-34-e1.compute-1.internal", "STORM_REST_API", customMetricsProperties, "http"); Assert.assertEquals(DEFAULT_STORM_UI_PORT, resolvedPort); }
protected String resolvePort(Cluster cluster, String hostname, String componentName, Map<String, String> metricsProperties, String protocol) throws AmbariException { String portConfigType = null; String portPropertyNameInMetricsProperties = protocol.equalsIgnoreCase(HTTPS_PROTOCOL) ? HTTPS_PORT_PROPERTY_NAME_PROPERTY : PORT_PROPERTY_NAME_PROPERTY; String portPropertyName = null; if (metricsProperties.containsKey(PORT_CONFIG_TYPE_PROPERTY) && metricsProperties.containsKey(portPropertyNameInMetricsProperties)) { portConfigType = metricsProperties.get(PORT_CONFIG_TYPE_PROPERTY); portPropertyName = metricsProperties.get(portPropertyNameInMetricsProperties); } String portStr = getPropertyValueByNameAndConfigType(portPropertyName, portConfigType, cluster, hostname); if (portStr == null && metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { if (metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { portStr = metricsProperties.get(DEFAULT_PORT_PROPERTY); } else { String message = String.format("Can not determine REST port for " + "component %s. " + "Default REST port property %s is not defined at metrics.json " + "file for service, and there is no any other available ways " + "to determine port information.", componentName, DEFAULT_PORT_PROPERTY); throw new AmbariException(message); } } return portStr; }
RestMetricsPropertyProvider extends ThreadPoolEnabledPropertyProvider { protected String resolvePort(Cluster cluster, String hostname, String componentName, Map<String, String> metricsProperties, String protocol) throws AmbariException { String portConfigType = null; String portPropertyNameInMetricsProperties = protocol.equalsIgnoreCase(HTTPS_PROTOCOL) ? HTTPS_PORT_PROPERTY_NAME_PROPERTY : PORT_PROPERTY_NAME_PROPERTY; String portPropertyName = null; if (metricsProperties.containsKey(PORT_CONFIG_TYPE_PROPERTY) && metricsProperties.containsKey(portPropertyNameInMetricsProperties)) { portConfigType = metricsProperties.get(PORT_CONFIG_TYPE_PROPERTY); portPropertyName = metricsProperties.get(portPropertyNameInMetricsProperties); } String portStr = getPropertyValueByNameAndConfigType(portPropertyName, portConfigType, cluster, hostname); if (portStr == null && metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { if (metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { portStr = metricsProperties.get(DEFAULT_PORT_PROPERTY); } else { String message = String.format("Can not determine REST port for " + "component %s. " + "Default REST port property %s is not defined at metrics.json " + "file for service, and there is no any other available ways " + "to determine port information.", componentName, DEFAULT_PORT_PROPERTY); throw new AmbariException(message); } } return portStr; } }
RestMetricsPropertyProvider extends ThreadPoolEnabledPropertyProvider { protected String resolvePort(Cluster cluster, String hostname, String componentName, Map<String, String> metricsProperties, String protocol) throws AmbariException { String portConfigType = null; String portPropertyNameInMetricsProperties = protocol.equalsIgnoreCase(HTTPS_PROTOCOL) ? HTTPS_PORT_PROPERTY_NAME_PROPERTY : PORT_PROPERTY_NAME_PROPERTY; String portPropertyName = null; if (metricsProperties.containsKey(PORT_CONFIG_TYPE_PROPERTY) && metricsProperties.containsKey(portPropertyNameInMetricsProperties)) { portConfigType = metricsProperties.get(PORT_CONFIG_TYPE_PROPERTY); portPropertyName = metricsProperties.get(portPropertyNameInMetricsProperties); } String portStr = getPropertyValueByNameAndConfigType(portPropertyName, portConfigType, cluster, hostname); if (portStr == null && metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { if (metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { portStr = metricsProperties.get(DEFAULT_PORT_PROPERTY); } else { String message = String.format("Can not determine REST port for " + "component %s. " + "Default REST port property %s is not defined at metrics.json " + "file for service, and there is no any other available ways " + "to determine port information.", componentName, DEFAULT_PORT_PROPERTY); throw new AmbariException(message); } } return portStr; } @AssistedInject  RestMetricsPropertyProvider(
      @Assisted("metricsProperties") Map<String, String> metricsProperties,
      @Assisted("componentMetrics") Map<String, Map<String, PropertyInfo>> componentMetrics,
      @Assisted("streamProvider") StreamProvider streamProvider,
      @Assisted("metricHostProvider") MetricHostProvider metricHostProvider,
      @Assisted("clusterNamePropertyId") String clusterNamePropertyId,
      @Assisted("hostNamePropertyId") @Nullable String hostNamePropertyId,
      @Assisted("componentNamePropertyId") String componentNamePropertyId,
      @Assisted("statePropertyId") @Nullable String statePropertyId,
      @Assisted("componentName") @Nullable String componentName); }
RestMetricsPropertyProvider extends ThreadPoolEnabledPropertyProvider { protected String resolvePort(Cluster cluster, String hostname, String componentName, Map<String, String> metricsProperties, String protocol) throws AmbariException { String portConfigType = null; String portPropertyNameInMetricsProperties = protocol.equalsIgnoreCase(HTTPS_PROTOCOL) ? HTTPS_PORT_PROPERTY_NAME_PROPERTY : PORT_PROPERTY_NAME_PROPERTY; String portPropertyName = null; if (metricsProperties.containsKey(PORT_CONFIG_TYPE_PROPERTY) && metricsProperties.containsKey(portPropertyNameInMetricsProperties)) { portConfigType = metricsProperties.get(PORT_CONFIG_TYPE_PROPERTY); portPropertyName = metricsProperties.get(portPropertyNameInMetricsProperties); } String portStr = getPropertyValueByNameAndConfigType(portPropertyName, portConfigType, cluster, hostname); if (portStr == null && metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { if (metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { portStr = metricsProperties.get(DEFAULT_PORT_PROPERTY); } else { String message = String.format("Can not determine REST port for " + "component %s. " + "Default REST port property %s is not defined at metrics.json " + "file for service, and there is no any other available ways " + "to determine port information.", componentName, DEFAULT_PORT_PROPERTY); throw new AmbariException(message); } } return portStr; } @AssistedInject  RestMetricsPropertyProvider(
      @Assisted("metricsProperties") Map<String, String> metricsProperties,
      @Assisted("componentMetrics") Map<String, Map<String, PropertyInfo>> componentMetrics,
      @Assisted("streamProvider") StreamProvider streamProvider,
      @Assisted("metricHostProvider") MetricHostProvider metricHostProvider,
      @Assisted("clusterNamePropertyId") String clusterNamePropertyId,
      @Assisted("hostNamePropertyId") @Nullable String hostNamePropertyId,
      @Assisted("componentNamePropertyId") String componentNamePropertyId,
      @Assisted("statePropertyId") @Nullable String statePropertyId,
      @Assisted("componentName") @Nullable String componentName); @Override Set<String> checkPropertyIds(Set<String> propertyIds); }
RestMetricsPropertyProvider extends ThreadPoolEnabledPropertyProvider { protected String resolvePort(Cluster cluster, String hostname, String componentName, Map<String, String> metricsProperties, String protocol) throws AmbariException { String portConfigType = null; String portPropertyNameInMetricsProperties = protocol.equalsIgnoreCase(HTTPS_PROTOCOL) ? HTTPS_PORT_PROPERTY_NAME_PROPERTY : PORT_PROPERTY_NAME_PROPERTY; String portPropertyName = null; if (metricsProperties.containsKey(PORT_CONFIG_TYPE_PROPERTY) && metricsProperties.containsKey(portPropertyNameInMetricsProperties)) { portConfigType = metricsProperties.get(PORT_CONFIG_TYPE_PROPERTY); portPropertyName = metricsProperties.get(portPropertyNameInMetricsProperties); } String portStr = getPropertyValueByNameAndConfigType(portPropertyName, portConfigType, cluster, hostname); if (portStr == null && metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { if (metricsProperties.containsKey(DEFAULT_PORT_PROPERTY)) { portStr = metricsProperties.get(DEFAULT_PORT_PROPERTY); } else { String message = String.format("Can not determine REST port for " + "component %s. " + "Default REST port property %s is not defined at metrics.json " + "file for service, and there is no any other available ways " + "to determine port information.", componentName, DEFAULT_PORT_PROPERTY); throw new AmbariException(message); } } return portStr; } @AssistedInject  RestMetricsPropertyProvider(
      @Assisted("metricsProperties") Map<String, String> metricsProperties,
      @Assisted("componentMetrics") Map<String, Map<String, PropertyInfo>> componentMetrics,
      @Assisted("streamProvider") StreamProvider streamProvider,
      @Assisted("metricHostProvider") MetricHostProvider metricHostProvider,
      @Assisted("clusterNamePropertyId") String clusterNamePropertyId,
      @Assisted("hostNamePropertyId") @Nullable String hostNamePropertyId,
      @Assisted("componentNamePropertyId") String componentNamePropertyId,
      @Assisted("statePropertyId") @Nullable String statePropertyId,
      @Assisted("componentName") @Nullable String componentName); @Override Set<String> checkPropertyIds(Set<String> propertyIds); static final String URL_PATH_SEPARATOR; static final String DOCUMENT_PATH_SEPARATOR; }