You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by yu...@apache.org on 2014/10/08 00:52:32 UTC
[04/51] [partial] AMBARI-7621. Import initial contribution for Ambari
support on Windows to branch-windows-dev. (Jayush Luniya and Florian Barca
via yusaku)
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostGroup.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostGroup.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostGroup.java
deleted file mode 100644
index 303bd15..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostGroup.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.controller.internal;
-
-import java.util.Collection;
-import java.util.Map;
-
-/**
- * Host Group definition.
- */
-public interface HostGroup {
-
- /**
- * Get the host group name.
- *
- * @return host group name
- */
- public String getName();
-
- /**
- * Get associated host information.
- *
- * @return collection of hosts associated with the host group
- */
- public Collection<String> getHostInfo();
-
- /**
- * Get the components associated with the host group.
- *
- * @return collection of component names for the host group
- */
- public Collection<String> getComponents();
-
- /**
- * Get the configurations associated with the host group.
- *
- * @return map of configuration type to a map of properties
- */
- public Map<String, Map<String, String>> getConfigurationProperties();
-}
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java
index facc670..6eceea9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java
@@ -330,9 +330,9 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
if (null != o)
hostRequest.setMaintenanceState(o.toString());
- List<ConfigurationRequest> cr = getConfigurationRequests("Hosts", properties);
+ ConfigurationRequest cr = getConfigurationRequest("Hosts", properties);
- hostRequest.setDesiredConfigs(cr);
+ hostRequest.setDesiredConfig(cr);
return hostRequest;
}
@@ -533,10 +533,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
AmbariManagementController controller = getManagementController();
Clusters clusters = controller.getClusters();
-
- // We don't expect batch requests for different clusters, that's why
- // nothing bad should happen if value is overwritten few times
- String maintenanceCluster = null;
+ Set<String> maintenanceClusters = new HashSet<String>();
for (HostRequest request : requests) {
if (request.getHostname() == null
@@ -585,54 +582,54 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
"maintenance state to one of " + EnumSet.of(MaintenanceState.OFF, MaintenanceState.ON));
} else {
h.setMaintenanceState(c.getClusterId(), newState);
- maintenanceCluster = c.getClusterName();
+
+ maintenanceClusters.add(c.getClusterName());
}
}
}
- if (null != request.getClusterName() && null != request.getDesiredConfigs()) {
+ if (null != request.getClusterName() && null != request.getDesiredConfig()) {
Cluster c = clusters.getCluster(request.getClusterName());
if (clusters.getHostsForCluster(request.getClusterName()).containsKey(h.getHostName())) {
- for (ConfigurationRequest cr : request.getDesiredConfigs()) {
+ ConfigurationRequest cr = request.getDesiredConfig();
- if (null != cr.getProperties() && cr.getProperties().size() > 0) {
- LOG.info(MessageFormat.format("Applying configuration with tag ''{0}'' to host ''{1}'' in cluster ''{2}''",
- cr.getVersionTag(),
- request.getHostname(),
- request.getClusterName()));
+ if (null != cr.getProperties() && cr.getProperties().size() > 0) {
+ LOG.info(MessageFormat.format("Applying configuration with tag ''{0}'' to host ''{1}'' in cluster ''{2}''",
+ cr.getVersionTag(),
+ request.getHostname(),
+ request.getClusterName()));
- cr.setClusterName(c.getClusterName());
- controller.createConfiguration(cr);
- }
+ cr.setClusterName(c.getClusterName());
+ controller.createConfiguration(cr);
+ }
- Config baseConfig = c.getConfig(cr.getType(), cr.getVersionTag());
- if (null != baseConfig) {
- String authName = controller.getAuthName();
- DesiredConfig oldConfig = h.getDesiredConfigs(c.getClusterId()).get(cr.getType());
-
- if (h.addDesiredConfig(c.getClusterId(), cr.isSelected(), authName, baseConfig)) {
- Logger logger = LoggerFactory.getLogger("configchange");
- logger.info("cluster '" + c.getClusterName() + "', "
- + "host '" + h.getHostName() + "' "
- + "changed by: '" + authName + "'; "
- + "type='" + baseConfig.getType() + "' "
- + "version='" + baseConfig.getVersion() + "'"
- + "tag='" + baseConfig.getTag() + "'"
- + (null == oldConfig ? "" : ", from='" + oldConfig.getTag() + "'"));
- }
+ Config baseConfig = c.getConfig(cr.getType(), cr.getVersionTag());
+ if (null != baseConfig) {
+ String authName = controller.getAuthName();
+ DesiredConfig oldConfig = h.getDesiredConfigs(c.getClusterId()).get(cr.getType());
+
+ if (h.addDesiredConfig(c.getClusterId(), cr.isSelected(), authName, baseConfig)) {
+ Logger logger = LoggerFactory.getLogger("configchange");
+ logger.info("cluster '" + c.getClusterName() + "', "
+ + "host '" + h.getHostName() + "' "
+ + "changed by: '" + authName + "'; "
+ + "type='" + baseConfig.getType() + "' "
+ + "tag='" + baseConfig.getVersionTag() + "'"
+ + (null == oldConfig ? "" : ", from='" + oldConfig.getVersion() + "'"));
}
}
+
}
}
//todo: if attempt was made to update a property other than those
//todo: that are allowed above, should throw exception
}
- if (maintenanceCluster != null) {
+ if (maintenanceClusters.size() > 0) {
try {
- maintenanceStateHelper.createRequests(controller, requestProperties, maintenanceCluster);
+ maintenanceStateHelper.createRequests(controller, requestProperties, maintenanceClusters);
} catch (Exception e) {
LOG.warn("Could not send maintenance status to Nagios (" + e.getMessage() + ")");
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HttpProxyPropertyProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HttpProxyPropertyProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HttpProxyPropertyProvider.java
index 3cd336c..d39518c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HttpProxyPropertyProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HttpProxyPropertyProvider.java
@@ -26,8 +26,6 @@ import java.util.Map;
import java.util.Set;
import com.google.gson.JsonSyntaxException;
-import com.google.inject.Injector;
-import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.configuration.ComponentSSLConfiguration;
import org.apache.ambari.server.controller.spi.Predicate;
import org.apache.ambari.server.controller.spi.PropertyProvider;
@@ -36,8 +34,6 @@ import org.apache.ambari.server.controller.spi.Resource;
import org.apache.ambari.server.controller.spi.SystemException;
import org.apache.ambari.server.controller.utilities.PropertyHelper;
import org.apache.ambari.server.controller.utilities.StreamProvider;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -55,26 +51,11 @@ public class HttpProxyPropertyProvider extends BaseProvider implements PropertyP
private static final Map<String, String> URL_TEMPLATES = new HashMap<String, String>();
private static final Map<String, String> MAPPINGS = new HashMap<String, String>();
- private static final Map<String, String> PROPERTIES_TO_FILTER = new HashMap<String, String>();
-
- private static final String COMPONENT_RESOURCEMANAGER = "RESOURCEMANAGER";
- private static final String COMPONENT_NAGIOS_SERVER = "NAGIOS_SERVER";
- private static final String CONFIG_YARN_SITE = "yarn-site";
- private static final String CONFIG_CORE_SITE = "core-site";
- private static final String PROPERTY_YARN_HTTP_POLICY = "yarn.http.policy";
- private static final String PROPERTY_HADOOP_SSL_ENABLED = "hadoop.ssl.enabled";
- private static final String PROPERTY_YARN_HTTP_POLICY_VALUE_HTTPS_ONLY = "HTTPS_ONLY";
- private static final String PROPERTY_HADOOP_SSL_ENABLED_VALUE_TRUE = "true";
static {
- URL_TEMPLATES.put(COMPONENT_NAGIOS_SERVER, "http://%s/ambarinagios/nagios/nagios_alerts.php?q1=alerts&" +
- "alert_type=all");
- URL_TEMPLATES.put(COMPONENT_RESOURCEMANAGER, "http://%s:8088/ws/v1/cluster/info");
+ URL_TEMPLATES.put("NAGIOS_SERVER", "http://%s/ambarinagios/nagios/nagios_alerts.php?q1=alerts&alert_type=all");
- MAPPINGS.put(COMPONENT_NAGIOS_SERVER, PropertyHelper.getPropertyId("HostRoles", "nagios_alerts"));
- MAPPINGS.put(COMPONENT_RESOURCEMANAGER, PropertyHelper.getPropertyId("HostRoles", "ha_state"));
-
- PROPERTIES_TO_FILTER.put(COMPONENT_RESOURCEMANAGER, "clusterInfo/haState");
+ MAPPINGS.put("NAGIOS_SERVER", PropertyHelper.getPropertyId("HostRoles", "nagios_alerts"));
}
private final ComponentSSLConfiguration configuration;
@@ -84,14 +65,10 @@ public class HttpProxyPropertyProvider extends BaseProvider implements PropertyP
private String clusterNamePropertyId = null;
private String hostNamePropertyId = null;
private String componentNamePropertyId = null;
-
- private Injector injector;
- private Clusters clusters;
public HttpProxyPropertyProvider(
StreamProvider stream,
ComponentSSLConfiguration configuration,
- Injector inject,
String clusterNamePropertyId,
String hostNamePropertyId,
String componentNamePropertyId) {
@@ -102,8 +79,6 @@ public class HttpProxyPropertyProvider extends BaseProvider implements PropertyP
this.clusterNamePropertyId = clusterNamePropertyId;
this.hostNamePropertyId = hostNamePropertyId;
this.componentNamePropertyId = componentNamePropertyId;
- this.injector = inject;
- this.clusters = injector.getInstance(Clusters.class);
}
/**
@@ -123,13 +98,12 @@ public class HttpProxyPropertyProvider extends BaseProvider implements PropertyP
Object hostName = resource.getPropertyValue(hostNamePropertyId);
Object componentName = resource.getPropertyValue(componentNamePropertyId);
- Object clusterName = resource.getPropertyValue(clusterNamePropertyId);
-
+
if (null != hostName && null != componentName &&
MAPPINGS.containsKey(componentName.toString()) &&
URL_TEMPLATES.containsKey(componentName.toString())) {
- String template = getTemplate(componentName.toString(), clusterName.toString());
+ String template = getTemplate(componentName.toString());
String propertyId = MAPPINGS.get(componentName.toString());
String url = String.format(template, hostName);
@@ -140,59 +114,25 @@ public class HttpProxyPropertyProvider extends BaseProvider implements PropertyP
return resources;
}
- private String getTemplate(String componentName, String clusterName) throws SystemException {
- String template = URL_TEMPLATES.get(componentName);
+ private String getTemplate(String key) {
+
+ String template = URL_TEMPLATES.get(key);
- if (componentName.equals(COMPONENT_NAGIOS_SERVER)) {
+ if (key.equals("NAGIOS_SERVER")) {
if (configuration.isNagiosSSL()) {
template = template.replace("http", "https");
}
- } else if (componentName.equals(COMPONENT_RESOURCEMANAGER)) {
- try {
- Cluster cluster = this.clusters.getCluster(clusterName);
- Map<String, String> yarnConfigProperties = cluster.getDesiredConfigByType(CONFIG_YARN_SITE).getProperties();
- Map<String, String> coreConfigProperties = cluster.getDesiredConfigByType(CONFIG_CORE_SITE).getProperties();
- String yarnHttpPolicy = yarnConfigProperties.get(PROPERTY_YARN_HTTP_POLICY);
- String hadoopSslEnabled = coreConfigProperties.get(PROPERTY_HADOOP_SSL_ENABLED);
- if ((yarnHttpPolicy != null && yarnHttpPolicy.equals(PROPERTY_YARN_HTTP_POLICY_VALUE_HTTPS_ONLY)) ||
- hadoopSslEnabled != null && hadoopSslEnabled.equals(PROPERTY_HADOOP_SSL_ENABLED_VALUE_TRUE)) {
- template = template.replace("http", "https");
- }
- } catch (AmbariException e) {
- LOG.debug(String.format("Could not load cluster with name %s. %s", clusterName, e.getMessage()));
- throw new SystemException(String.format("Could not load cluster with name %s.", clusterName),e);
- }
}
return template;
}
- private Object getPropertyValueToSet(Map<String, Object> propertyValueFromJson, Object componentName) throws SystemException {
- Object result = propertyValueFromJson;
- //TODO need refactoring for universalization
- try {
- if (PROPERTIES_TO_FILTER.get(componentName) != null) {
- for (String key : PROPERTIES_TO_FILTER.get(componentName).split("/")) {
- result = ((Map)result).get(key);
- }
- }
- } catch (ClassCastException e) {
- LOG.error(String.format("Error getting property value for %s. %s", PROPERTIES_TO_FILTER.get(componentName),
- e.getMessage()));
- throw new SystemException(String.format("Error getting property value for %s.",
- PROPERTIES_TO_FILTER.get(componentName)),e);
- }
- return result;
- }
-
private void getHttpResponse(Resource r, String url, String propertyIdToSet) throws SystemException {
InputStream in = null;
try {
in = streamProvider.readFrom(url);
Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> propertyValueFromJson = new Gson().fromJson(IOUtils.toString(in, "UTF-8"), mapType);
- Object propertyValueToSet = getPropertyValueToSet(propertyValueFromJson,
- r.getPropertyValue(componentNamePropertyId));
- r.setProperty(propertyIdToSet, propertyValueToSet);
+ r.setProperty(propertyIdToSet, propertyValueFromJson);
}
catch (IOException ioe) {
LOG.error("Error reading HTTP response from " + url);
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/LdapSyncEventResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/LdapSyncEventResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/LdapSyncEventResourceProvider.java
deleted file mode 100644
index f7499fa..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/LdapSyncEventResourceProvider.java
+++ /dev/null
@@ -1,500 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.controller.internal;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.LdapSyncRequest;
-import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
-import org.apache.ambari.server.controller.spi.NoSuchResourceException;
-import org.apache.ambari.server.controller.spi.Predicate;
-import org.apache.ambari.server.controller.spi.Request;
-import org.apache.ambari.server.controller.spi.RequestStatus;
-import org.apache.ambari.server.controller.spi.Resource;
-import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
-import org.apache.ambari.server.controller.spi.SystemException;
-import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
-import org.apache.ambari.server.controller.utilities.PropertyHelper;
-import org.apache.ambari.server.orm.entities.LdapSyncEventEntity;
-import org.apache.ambari.server.orm.entities.LdapSyncSpecEntity;
-import org.apache.ambari.server.security.ldap.LdapBatchDto;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Queue;
-import java.util.Set;
-import java.util.concurrent.ConcurrentSkipListMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicLong;
-
-/**
- * Resource provider for ldap sync events.
- */
-public class LdapSyncEventResourceProvider extends AbstractControllerResourceProvider {
-
- /**
- * Thread pool
- */
- private static ExecutorService executorService;
-
- // TODO : Do we need to expose the thread pool params in configuration?
- private final static int THREAD_POOL_CORE_SIZE = 2;
- private final static int THREAD_POOL_MAX_SIZE = 5;
- private final static long THREAD_POOL_TIMEOUT = 1000L;
-
- /**
- * Event property id constants.
- */
- public static final String EVENT_ID_PROPERTY_ID = "Event/id";
- public static final String EVENT_STATUS_PROPERTY_ID = "Event/status";
- public static final String EVENT_STATUS_DETAIL_PROPERTY_ID = "Event/status_detail";
- public static final String EVENT_START_TIME_PROPERTY_ID = "Event/sync_time/start";
- public static final String EVENT_END_TIME_PROPERTY_ID = "Event/sync_time/end";
- public static final String USERS_CREATED_PROPERTY_ID = "Event/summary/users/created";
- public static final String USERS_UPDATED_PROPERTY_ID = "Event/summary/users/updated";
- public static final String USERS_REMOVED_PROPERTY_ID = "Event/summary/users/removed";
- public static final String GROUPS_CREATED_PROPERTY_ID = "Event/summary/groups/created";
- public static final String GROUPS_UPDATED_PROPERTY_ID = "Event/summary/groups/updated";
- public static final String GROUPS_REMOVED_PROPERTY_ID = "Event/summary/groups/removed";
- public static final String MEMBERSHIPS_CREATED_PROPERTY_ID = "Event/summary/memberships/created";
- public static final String MEMBERSHIPS_REMOVED_PROPERTY_ID = "Event/summary/memberships/removed";
- public static final String EVENT_SPECS_PROPERTY_ID = "Event/specs";
-
- /**
- * The key property ids for a event resource.
- */
- private static Map<Resource.Type, String> keyPropertyIds = new HashMap<Resource.Type, String>();
- static {
- keyPropertyIds.put(Resource.Type.LdapSyncEvent, EVENT_ID_PROPERTY_ID);
- }
-
- /**
- * The property ids for a event resource.
- */
- private static Set<String> propertyIds = new HashSet<String>();
-
- static {
- propertyIds.add(EVENT_ID_PROPERTY_ID);
- propertyIds.add(EVENT_STATUS_PROPERTY_ID);
- propertyIds.add(EVENT_STATUS_DETAIL_PROPERTY_ID);
- propertyIds.add(EVENT_START_TIME_PROPERTY_ID);
- propertyIds.add(EVENT_END_TIME_PROPERTY_ID);
- propertyIds.add(USERS_CREATED_PROPERTY_ID);
- propertyIds.add(USERS_UPDATED_PROPERTY_ID);
- propertyIds.add(USERS_REMOVED_PROPERTY_ID);
- propertyIds.add(GROUPS_CREATED_PROPERTY_ID);
- propertyIds.add(GROUPS_UPDATED_PROPERTY_ID);
- propertyIds.add(GROUPS_REMOVED_PROPERTY_ID);
- propertyIds.add(MEMBERSHIPS_CREATED_PROPERTY_ID);
- propertyIds.add(MEMBERSHIPS_REMOVED_PROPERTY_ID);
- propertyIds.add(EVENT_SPECS_PROPERTY_ID);
- }
-
- /**
- * Spec property keys.
- */
- private static final String PRINCIPAL_TYPE_SPEC_KEY = "principal_type";
- private static final String SYNC_TYPE_SPEC_KEY = "sync_type";
- private static final String NAMES_SPEC_KEY = "names";
-
- /**
- * Map of all sync events.
- */
- private final Map<Long, LdapSyncEventEntity> events = new ConcurrentSkipListMap<Long, LdapSyncEventEntity>();
-
- /**
- * The queue of events to be processed.
- */
- private final Queue<LdapSyncEventEntity> eventQueue = new LinkedList<LdapSyncEventEntity>();
-
- /**
- * Indicates whether or not the events are currently being processed.
- */
- private volatile boolean processingEvents = false;
-
- /**
- * The next event id.
- */
- private AtomicLong nextEventId = new AtomicLong(1L);
-
- /**
- * The logger.
- */
- protected final static Logger LOG = LoggerFactory.getLogger(LdapSyncEventResourceProvider.class);
-
-
- // ----- Constructors ------------------------------------------------------
-
- /**
- * Construct a event resource provider.
- */
- public LdapSyncEventResourceProvider(AmbariManagementController managementController) {
- super(propertyIds, keyPropertyIds, managementController);
- }
-
-
- // ----- ResourceProvider --------------------------------------------------
-
- @Override
- public RequestStatus createResources(Request event)
- throws SystemException, UnsupportedPropertyException,
- ResourceAlreadyExistsException, NoSuchParentResourceException {
- Set<LdapSyncEventEntity> newEvents = new HashSet<LdapSyncEventEntity>();
-
- for (Map<String, Object> properties : event.getProperties()) {
- newEvents.add(createResources(getCreateCommand(properties)));
- }
- notifyCreate(Resource.Type.ViewInstance, event);
-
- Set<Resource> associatedResources = new HashSet<Resource>();
- for (LdapSyncEventEntity eventEntity : newEvents) {
- Resource resource = new ResourceImpl(Resource.Type.LdapSyncEvent);
- resource.setProperty(EVENT_ID_PROPERTY_ID, eventEntity.getId());
- associatedResources.add(resource);
- synchronized (eventQueue) {
- eventQueue.offer(eventEntity);
- }
- }
-
- ensureEventProcessor();
-
- return getRequestStatus(null, associatedResources);
- }
-
- @Override
- public Set<Resource> getResources(Request event, Predicate predicate)
- throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
-
- Set<Resource> resources = new HashSet<Resource>();
- Set<String> requestedIds = getRequestPropertyIds(event, predicate);
-
- for (LdapSyncEventEntity eventEntity : events.values()) {
- resources.add(toResource(eventEntity, requestedIds));
- }
- return resources;
- }
-
- @Override
- public RequestStatus updateResources(Request event, Predicate predicate)
- throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
- throw new UnsupportedOperationException("Not supported.");
- }
-
- @Override
- public RequestStatus deleteResources(Predicate predicate)
- throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
- modifyResources(getDeleteCommand(predicate));
- notifyDelete(Resource.Type.ViewInstance, predicate);
- return getRequestStatus(null);
- }
-
- @Override
- public Map<Resource.Type, String> getKeyPropertyIds() {
- return keyPropertyIds;
- }
-
-
- // ----- AbstractResourceProvider ------------------------------------------
-
- @Override
- protected Set<String> getPKPropertyIds() {
- return new HashSet<String>(keyPropertyIds.values());
- }
-
-
- // ----- helper methods ----------------------------------------------------
-
- /**
- * Ensure that sync events are being processed.
- */
- protected void ensureEventProcessor() {
-
- if (!processingEvents) {
- synchronized (eventQueue) {
- if (!processingEvents) {
- processingEvents = true;
- getExecutorService().submit(new Runnable() {
- @Override
- public void run() {
- processSyncEvents();
- }
- });
- }
- }
- }
- }
-
- // create a resource from the given event entity
- private Resource toResource(LdapSyncEventEntity eventEntity, Set<String> requestedIds) {
- Resource resource = new ResourceImpl(Resource.Type.LdapSyncEvent);
-
- setResourceProperty(resource, EVENT_ID_PROPERTY_ID, eventEntity.getId(), requestedIds);
- setResourceProperty(resource, EVENT_STATUS_PROPERTY_ID, eventEntity.getStatus().toString().toUpperCase(), requestedIds);
- setResourceProperty(resource, EVENT_STATUS_DETAIL_PROPERTY_ID, eventEntity.getStatusDetail(), requestedIds);
- setResourceProperty(resource, USERS_CREATED_PROPERTY_ID, eventEntity.getUsersCreated(), requestedIds);
- setResourceProperty(resource, USERS_UPDATED_PROPERTY_ID, eventEntity.getUsersUpdated(), requestedIds);
- setResourceProperty(resource, USERS_REMOVED_PROPERTY_ID, eventEntity.getUsersRemoved(), requestedIds);
- setResourceProperty(resource, GROUPS_CREATED_PROPERTY_ID, eventEntity.getGroupsCreated(), requestedIds);
- setResourceProperty(resource, GROUPS_UPDATED_PROPERTY_ID, eventEntity.getGroupsUpdated(), requestedIds);
- setResourceProperty(resource, GROUPS_REMOVED_PROPERTY_ID, eventEntity.getGroupsRemoved(), requestedIds);
- setResourceProperty(resource, MEMBERSHIPS_CREATED_PROPERTY_ID, eventEntity.getMembershipsCreated(), requestedIds);
- setResourceProperty(resource, MEMBERSHIPS_REMOVED_PROPERTY_ID, eventEntity.getMembershipsRemoved(), requestedIds);
-
- Set<Map<String, String>> specs = new HashSet<Map<String, String>>();
-
- List<LdapSyncSpecEntity> specList = eventEntity.getSpecs();
-
- for (LdapSyncSpecEntity spec : specList) {
-
- Map<String, String> specMap = new HashMap<String, String>();
-
- specMap.put(PRINCIPAL_TYPE_SPEC_KEY, spec.getPrincipalType().toString().toLowerCase());
- specMap.put(SYNC_TYPE_SPEC_KEY, spec.getSyncType().toString().toLowerCase());
-
- List<String> names = spec.getPrincipalNames();
-
- if (!names.isEmpty()) {
- specMap.put(NAMES_SPEC_KEY, names.toString().replace("[", "").replace("]", "").replace(", ", ","));
- }
- specs.add(specMap);
- }
- setResourceProperty(resource, EVENT_SPECS_PROPERTY_ID, specs, requestedIds);
-
- setResourceProperty(resource, EVENT_START_TIME_PROPERTY_ID, eventEntity.getStartTime(), requestedIds);
- setResourceProperty(resource, EVENT_END_TIME_PROPERTY_ID, eventEntity.getEndTime(), requestedIds);
-
- return resource;
- }
-
- // create a event entity from the given set of properties
- private LdapSyncEventEntity toEntity(Map<String, Object> properties) {
- LdapSyncEventEntity entity = new LdapSyncEventEntity(getNextEventId());
- List<LdapSyncSpecEntity> specList = new LinkedList<LdapSyncSpecEntity>();
-
- Set<Map<String, String>> specs = (Set<Map<String, String>>) properties.get(EVENT_SPECS_PROPERTY_ID);
-
- for (Map<String, String> specMap : specs) {
-
- LdapSyncSpecEntity.SyncType syncType = null;
- LdapSyncSpecEntity.PrincipalType principalType = null;
-
- List<String> principalNames = Collections.emptyList();
-
- for (Map.Entry<String, String> entry : specMap.entrySet()) {
- String key = entry.getKey();
- if (key.equalsIgnoreCase(PRINCIPAL_TYPE_SPEC_KEY)) {
- principalType = LdapSyncSpecEntity.PrincipalType.valueOfIgnoreCase(entry.getValue());
-
- } else if (key.equalsIgnoreCase(SYNC_TYPE_SPEC_KEY)) {
- syncType = LdapSyncSpecEntity.SyncType.valueOfIgnoreCase(entry.getValue());
-
- } else if (key.equalsIgnoreCase(NAMES_SPEC_KEY)) {
- String names = entry.getValue();
- principalNames = Arrays.asList(names.split("\\s*,\\s*"));
- } else {
- throw new IllegalArgumentException("Unknown spec key " + key + ".");
- }
- }
-
- if (syncType == null || principalType == null) {
- throw new IllegalArgumentException("LDAP event spec must include both sync-type and principal-type.");
- }
-
- LdapSyncSpecEntity spec = new LdapSyncSpecEntity(principalType, syncType, principalNames);
- specList.add(spec);
- }
- entity.setSpecs(specList);
-
- return entity;
- }
-
- // get the next event id
- private long getNextEventId() {
- return nextEventId.getAndIncrement();
- }
-
- // Create a create command with all properties set
- private Command<LdapSyncEventEntity> getCreateCommand(final Map<String, Object> properties) {
- return new Command<LdapSyncEventEntity>() {
- @Override
- public LdapSyncEventEntity invoke() throws AmbariException {
-
- LdapSyncEventEntity eventEntity = toEntity(properties);
-
- events.put(eventEntity.getId(), eventEntity);
-
- return eventEntity;
- }
- };
- }
-
- // Create a delete command with the given predicate
- private Command<Void> getDeleteCommand(final Predicate predicate) {
- return new Command<Void>() {
- @Override
- public Void invoke() throws AmbariException {
- Set<String> requestedIds = getRequestPropertyIds(PropertyHelper.getReadRequest(), predicate);
-
- Set<LdapSyncEventEntity> entities = new HashSet<LdapSyncEventEntity>();
-
- for (LdapSyncEventEntity entity : events.values()){
- Resource resource = toResource(entity, requestedIds);
- if (predicate == null || predicate.evaluate(resource)) {
- entities.add(entity);
- }
- }
- for (LdapSyncEventEntity entity : entities) {
- events.remove(entity.getId());
- }
- return null;
- }
- };
- }
-
- // Get the ldap sync thread pool
- private static synchronized ExecutorService getExecutorService() {
- if (executorService == null) {
- LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
-
- ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
- THREAD_POOL_CORE_SIZE,
- THREAD_POOL_MAX_SIZE,
- THREAD_POOL_TIMEOUT,
- TimeUnit.MILLISECONDS,
- queue);
-
- threadPoolExecutor.allowCoreThreadTimeOut(true);
- executorService = threadPoolExecutor;
- }
- return executorService;
- }
-
- // Process any queued up sync events
- private void processSyncEvents() {
-
- while (processingEvents) {
- LdapSyncEventEntity event;
- synchronized (eventQueue) {
- event = eventQueue.poll();
- if (event == null) {
- processingEvents = false;
- return;
- }
- }
-
- event.setStatus(LdapSyncEventEntity.Status.RUNNING);
- event.setStatusDetail("Running LDAP sync.");
- event.setStartTime(System.currentTimeMillis());
-
- try {
-
- populateLdapSyncEvent(event, syncLdap(event));
-
- event.setStatus(LdapSyncEventEntity.Status.COMPLETE);
- event.setStatusDetail("Completed LDAP sync.");
- } catch (Exception e) {
- event.setStatus(LdapSyncEventEntity.Status.ERROR);
- String msg = "Caught exception running LDAP sync. ";
- event.setStatusDetail(msg + e.getMessage());
- LOG.error(msg, e);
- } finally {
- event.setEndTime(System.currentTimeMillis());
- }
- }
- }
-
- /**
- * Sync the users and groups specified in the given sync event with ldap.
- *
- * @param event the sync event
- *
- * @return the results of the sync
- *
- * @throws AmbariException if the sync could not be completed
- */
- private LdapBatchDto syncLdap(LdapSyncEventEntity event) throws AmbariException {
- LdapSyncRequest userRequest = null;
- LdapSyncRequest groupRequest = null;
-
- for (LdapSyncSpecEntity spec : event.getSpecs()) {
- switch (spec.getPrincipalType()) {
- case USERS:
- userRequest = getLdapRequest(userRequest, spec);
- break;
- case GROUPS:
- groupRequest = getLdapRequest(groupRequest, spec);
- break;
- }
- }
- return getManagementController().synchronizeLdapUsersAndGroups(userRequest, groupRequest);
- }
-
- /**
- * Update the given request with the given ldap event spec.
- *
- * @param request the sync request; may be null
- * @param spec the specification of what to sync
- *
- * @return the updated sync request or a new sync request if the given request is null
- */
- private LdapSyncRequest getLdapRequest(LdapSyncRequest request, LdapSyncSpecEntity spec) {
-
- switch (spec.getSyncType()) {
- case ALL:
- return new LdapSyncRequest(LdapSyncSpecEntity.SyncType.ALL);
- case EXISTING:
- return new LdapSyncRequest(LdapSyncSpecEntity.SyncType.EXISTING);
- case SPECIFIC:
- Set<String> principalNames = new HashSet<String>(spec.getPrincipalNames());
- if (request == null ) {
- request = new LdapSyncRequest(LdapSyncSpecEntity.SyncType.SPECIFIC, principalNames);
- } else {
- request.addPrincipalNames(principalNames);
- }
- }
- return request;
- }
-
- /**
- * Populate the given ldap sync event with the results of an ldap sync.
- *
- * @param event the sync event
- * @param syncInfo the sync results
- */
- private void populateLdapSyncEvent(LdapSyncEventEntity event, LdapBatchDto syncInfo) {
- event.setUsersCreated(syncInfo.getUsersToBeCreated().size());
- event.setUsersUpdated(syncInfo.getUsersToBecomeLdap().size());
- event.setUsersRemoved(syncInfo.getUsersToBeRemoved().size());
- event.setGroupsCreated(syncInfo.getGroupsToBeCreated().size());
- event.setGroupsUpdated(syncInfo.getGroupsToBecomeLdap().size());
- event.setGroupsRemoved(syncInfo.getGroupsToBeRemoved().size());
- event.setMembershipsCreated(syncInfo.getMembershipToAdd().size());
- event.setMembershipsRemoved(syncInfo.getMembershipToRemove().size());
- }
-}
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MemberResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MemberResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MemberResourceProvider.java
index 0f6b66d..27b7e4b 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MemberResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MemberResourceProvider.java
@@ -18,7 +18,6 @@
package org.apache.ambari.server.controller.internal;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@@ -37,7 +36,6 @@ import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
import org.apache.ambari.server.controller.spi.SystemException;
import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
import org.apache.ambari.server.controller.utilities.PropertyHelper;
-
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.persist.Transactional;
@@ -141,17 +139,11 @@ public class MemberResourceProvider extends AbstractControllerResourceProvider {
for (Map<String, Object> propertyMap : request.getProperties()) {
requests.add(getRequest(propertyMap));
}
- if (requests.isEmpty()) {
- // request for removing all users from group
- Map<String, Object> propertyMap = new HashMap<String, Object>();
- propertyMap.put(MEMBER_GROUP_NAME_PROPERTY_ID, getQueryParameterValue(MEMBER_GROUP_NAME_PROPERTY_ID, predicate));
- requests.add(getRequest(propertyMap));
- }
modifyResources(new Command<Void>() {
@Override
public Void invoke() throws AmbariException {
- getManagementController().updateMembers(requests);
+ // do nothing
return null;
}
});
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PermissionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PermissionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PermissionResourceProvider.java
index 287c2e6..faf692f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PermissionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PermissionResourceProvider.java
@@ -27,8 +27,6 @@ import org.apache.ambari.server.controller.spi.Resource;
import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
import org.apache.ambari.server.controller.spi.SystemException;
import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
import java.util.HashMap;
import java.util.HashSet;
@@ -39,12 +37,6 @@ import java.util.Set;
* Resource provider for permission instances.
*/
public class PermissionResourceProvider extends AbstractResourceProvider {
-
- /**
- * Data access object used to obtain permission entities.
- */
- protected static PermissionDAO permissionDAO;
-
/**
* Permission property id constants.
*/
@@ -72,25 +64,49 @@ public class PermissionResourceProvider extends AbstractResourceProvider {
}
- // ----- Constructors ------------------------------------------------------
-
/**
- * Construct a permission resource provider.
+ * Builtin permissions
*/
- public PermissionResourceProvider() {
- super(propertyIds, keyPropertyIds);
+ private static final Set<Resource> builtinPermissions = new HashSet<Resource>();
+
+ static {
+ // AMBARI.ADMIN
+ Resource resource = new ResourceImpl(Resource.Type.Permission);
+ resource.setProperty(PERMISSION_ID_PROPERTY_ID, 0);
+ resource.setProperty(PERMISSION_NAME_PROPERTY_ID, "ADMIN");
+ resource.setProperty(RESOURCE_NAME_PROPERTY_ID, "AMBARI");
+ builtinPermissions.add(resource);
+
+ // CLUSTER.READ
+ resource = new ResourceImpl(Resource.Type.Permission);
+ resource.setProperty(PERMISSION_ID_PROPERTY_ID, 1);
+ resource.setProperty(PERMISSION_NAME_PROPERTY_ID, "READ");
+ resource.setProperty(RESOURCE_NAME_PROPERTY_ID, "CLUSTER");
+ builtinPermissions.add(resource);
+
+ // CLUSTER.OPERATE
+ resource = new ResourceImpl(Resource.Type.Permission);
+ resource.setProperty(PERMISSION_ID_PROPERTY_ID, 2);
+ resource.setProperty(PERMISSION_NAME_PROPERTY_ID, "OPERATE");
+ resource.setProperty(RESOURCE_NAME_PROPERTY_ID, "CLUSTER");
+ builtinPermissions.add(resource);
+
+ // CLUSTER.OPERATE
+ resource = new ResourceImpl(Resource.Type.Permission);
+ resource.setProperty(PERMISSION_ID_PROPERTY_ID, 3);
+ resource.setProperty(PERMISSION_NAME_PROPERTY_ID, "USE");
+ resource.setProperty(RESOURCE_NAME_PROPERTY_ID, "VIEW");
+ builtinPermissions.add(resource);
}
- // ----- PermissionResourceProvider ----------------------------------------
+ // ----- Constructors ------------------------------------------------------
/**
- * Static initialization.
- *
- * @param dao permission data access object
+ * Construct a permission resource provider.
*/
- public static void init(PermissionDAO dao) {
- permissionDAO = dao;
+ public PermissionResourceProvider() {
+ super(propertyIds, keyPropertyIds);
}
@@ -106,16 +122,8 @@ public class PermissionResourceProvider extends AbstractResourceProvider {
@Override
public Set<Resource> getResources(Request request, Predicate predicate)
throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
-
- Set<Resource> resources = new HashSet<Resource>();
- Set<String> requestedIds = getRequestPropertyIds(request, predicate);
-
- for(PermissionEntity permissionEntity : permissionDAO.findAll()){
-
- resources.add(toResource(permissionEntity, requestedIds));
- }
-
- return resources;
+ // TODO : add custom permissions.
+ return new HashSet<Resource>(builtinPermissions);
}
@Override
@@ -142,18 +150,4 @@ public class PermissionResourceProvider extends AbstractResourceProvider {
protected Set<String> getPKPropertyIds() {
return new HashSet<String>(keyPropertyIds.values());
}
-
-
- // ----- helper methods ----------------------------------------------------
-
- // convert the given permission entity to a resource
- private Resource toResource(PermissionEntity entity, Set<String> requestedIds) {
- Resource resource = new ResourceImpl(Resource.Type.Permission);
-
- setResourceProperty(resource, PERMISSION_ID_PROPERTY_ID, entity.getId(), requestedIds);
- setResourceProperty(resource, PERMISSION_NAME_PROPERTY_ID, entity.getPermissionName(), requestedIds);
- setResourceProperty(resource, RESOURCE_NAME_PROPERTY_ID, entity.getResourceType().getName(), requestedIds);
-
- return resource;
- }
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PrivilegeResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PrivilegeResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PrivilegeResourceProvider.java
deleted file mode 100644
index ae9cb52..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PrivilegeResourceProvider.java
+++ /dev/null
@@ -1,491 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing privileges and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.controller.internal;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.DuplicateResourceException;
-import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
-import org.apache.ambari.server.controller.spi.NoSuchResourceException;
-import org.apache.ambari.server.controller.spi.Predicate;
-import org.apache.ambari.server.controller.spi.Request;
-import org.apache.ambari.server.controller.spi.RequestStatus;
-import org.apache.ambari.server.controller.spi.Resource;
-import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
-import org.apache.ambari.server.controller.spi.SystemException;
-import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
-import org.apache.ambari.server.orm.dao.GroupDAO;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.dao.PrincipalDAO;
-import org.apache.ambari.server.orm.dao.PrivilegeDAO;
-import org.apache.ambari.server.orm.dao.ResourceDAO;
-import org.apache.ambari.server.orm.dao.UserDAO;
-import org.apache.ambari.server.orm.entities.GroupEntity;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.PrincipalEntity;
-import org.apache.ambari.server.orm.entities.PrincipalTypeEntity;
-import org.apache.ambari.server.orm.entities.PrivilegeEntity;
-import org.apache.ambari.server.orm.entities.ResourceEntity;
-import org.apache.ambari.server.orm.entities.UserEntity;
-
-/**
- * Abstract resource provider for privilege resources.
- */
-public abstract class PrivilegeResourceProvider<T> extends AbstractResourceProvider {
-
- /**
- * Data access object used to obtain privilege entities.
- */
- protected static PrivilegeDAO privilegeDAO;
-
- /**
- * Data access object used to obtain user entities.
- */
- protected static UserDAO userDAO;
-
- /**
- * Data access object used to obtain group entities.
- */
- protected static GroupDAO groupDAO;
-
- /**
- * Data access object used to obtain principal entities.
- */
- protected static PrincipalDAO principalDAO;
-
- /**
- * Data access object used to obtain permission entities.
- */
- protected static PermissionDAO permissionDAO;
-
- /**
- * Data access object used to obtain resource entities.
- */
- protected static ResourceDAO resourceDAO;
-
- /**
- * Privilege property id constants.
- */
- public static final String PRIVILEGE_ID_PROPERTY_ID = "PrivilegeInfo/privilege_id";
- public static final String PERMISSION_NAME_PROPERTY_ID = "PrivilegeInfo/permission_name";
- public static final String PRINCIPAL_NAME_PROPERTY_ID = "PrivilegeInfo/principal_name";
- public static final String PRINCIPAL_TYPE_PROPERTY_ID = "PrivilegeInfo/principal_type";
-
- /**
- * The privilege resource type.
- */
- private final Resource.Type resourceType;
-
-
- // ----- Constructors ------------------------------------------------------
-
- /**
- * Construct a privilege resource provider.
- */
- public PrivilegeResourceProvider(Set<String> propertyIds,
- Map<Resource.Type, String> keyPropertyIds,
- Resource.Type resourceType) {
- super(propertyIds, keyPropertyIds);
- this.resourceType = resourceType;
- }
-
-
- // ----- PrivilegeResourceProvider ----------------------------------------
-
- /**
- * Static initialization.
- *
- * @param privDAO the privilege data access object
- * @param usrDAO the user data access object
- * @param grpDAO the group data access object
- * @param prinDAO the principal data access object
- * @param permDAO the permission data access object
- * @param resDAO the resource data access object
- */
- public static void init(PrivilegeDAO privDAO, UserDAO usrDAO, GroupDAO grpDAO, PrincipalDAO prinDAO,
- PermissionDAO permDAO, ResourceDAO resDAO) {
- privilegeDAO = privDAO;
- userDAO = usrDAO;
- groupDAO = grpDAO;
- principalDAO = prinDAO;
- permissionDAO = permDAO;
- resourceDAO = resDAO;
- }
-
- /**
- * Get the entities for the owning resources from the given properties.
- *
- * @param properties the set of properties
- *
- * @return the entities
- * @throws AmbariException if resource entities were not found
- */
- public abstract Map<Long, T> getResourceEntities(Map<String, Object> properties) throws AmbariException;
-
- /**
- * Get the id for the resource specified by predicate.
- *
- * @param predicate predicate
- *
- * @return the resource id
- */
- public abstract Long getResourceEntityId(Predicate predicate);
-
-
- // ----- ResourceProvider --------------------------------------------------
-
- @Override
- public RequestStatus createResources(Request request)
- throws SystemException, UnsupportedPropertyException,
- ResourceAlreadyExistsException, NoSuchParentResourceException {
- for (Map<String, Object> properties : request.getProperties()) {
- createResources(getCreateCommand(properties));
- }
- notifyCreate(resourceType, request);
-
- return getRequestStatus(null);
- }
-
- @Override
- public Set<Resource> getResources(Request request, Predicate predicate)
- throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
- Set<Resource> resources = new HashSet<Resource>();
- Set<String> requestedIds = getRequestPropertyIds(request, predicate);
- Set<Long> resourceIds = new HashSet<Long>();
-
- Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
-
- if (propertyMaps.isEmpty()) {
- propertyMaps.add(Collections.<String, Object>emptyMap());
- }
-
- for (Map<String, Object> properties : propertyMaps) {
- Map<Long, T> resourceEntities;
- try {
- resourceEntities = getResourceEntities(properties);
- } catch (AmbariException e) {
- throw new SystemException("Could not get resource list from request", e);
- }
-
- resourceIds.addAll(resourceEntities.keySet());
-
- Set<PrivilegeEntity> entitySet = new HashSet<PrivilegeEntity>();
- List<PrincipalEntity> principalList = new LinkedList<PrincipalEntity>();
-
- List<PrivilegeEntity> entities = privilegeDAO.findAll();
-
- for(PrivilegeEntity privilegeEntity : entities){
- if (resourceIds.contains(privilegeEntity.getResource().getId())) {
- PrincipalEntity principal = privilegeEntity.getPrincipal();
- entitySet.add(privilegeEntity);
- principalList.add(principal);
- }
- }
-
- Map<Long, UserEntity> userEntities = new HashMap<Long, UserEntity>();
- List<UserEntity> userList = userDAO.findUsersByPrincipal(principalList);
-
- for (UserEntity userEntity : userList) {
- userEntities.put(userEntity.getPrincipal().getId(), userEntity);
- }
-
- Map<Long, GroupEntity> groupEntities = new HashMap<Long, GroupEntity>();
- List<GroupEntity> groupList = groupDAO.findGroupsByPrincipal(principalList);
-
- for (GroupEntity groupEntity : groupList) {
- groupEntities.put(groupEntity.getPrincipal().getId(), groupEntity);
- }
-
- for(PrivilegeEntity privilegeEntity : entitySet){
- Resource resource = toResource(privilegeEntity, userEntities, groupEntities, resourceEntities, requestedIds);
- if (resource != null && (predicate == null || predicate.evaluate(resource))) {
- resources.add(resource);
- }
- }
- }
-
- return resources;
- }
-
- @Override
- public RequestStatus updateResources(Request request, Predicate predicate)
- throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
- modifyResources(getUpdateCommand(request, predicate));
- notifyUpdate(resourceType, request, predicate);
- return getRequestStatus(null);
- }
-
- @Override
- public RequestStatus deleteResources(Predicate predicate)
- throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
- modifyResources(getDeleteCommand(predicate));
- notifyDelete(resourceType, predicate);
- return getRequestStatus(null);
- }
-
-
- // ----- AbstractResourceProvider ------------------------------------------
-
- @Override
- protected Set<String> getPKPropertyIds() {
- return new HashSet<String>(getKeyPropertyIds().values());
- }
-
-
- // ----- helper methods ----------------------------------------------------
-
- /**
- * Check to see if the given privilege entity's permission is allowable for the
- * resource type.
- *
- * @param entity the privilege entity
- *
- * @throws AmbariException if the the privilege permission is not allowable for the resource type
- */
- protected boolean checkResourceTypes(PrivilegeEntity entity) throws AmbariException {
- Integer resourceType = entity.getResource().getResourceType().getId();
- Integer permissionResourceType = entity.getPermission().getResourceType().getId();
-
- return resourceType.equals(permissionResourceType);
- }
-
- /**
- * Convert the given privilege entity into a Resource.
- *
- * @param privilegeEntity the privilege entity to be converted
- * @param userEntities the map of user entities keyed by resource id
- * @param groupEntities the map of group entities keyed by resource id
- * @param resourceEntities the map of resource entities keyed by resource id
- * @param requestedIds the requested property ids
- *
- * @return the resource
- */
- protected Resource toResource(PrivilegeEntity privilegeEntity,
- Map<Long, UserEntity> userEntities,
- Map<Long, GroupEntity> groupEntities,
- Map<Long, T> resourceEntities,
- Set<String> requestedIds) {
- Resource resource = new ResourceImpl(resourceType);
-
- setResourceProperty(resource, PRIVILEGE_ID_PROPERTY_ID,
- privilegeEntity.getId(), requestedIds);
- setResourceProperty(resource, PERMISSION_NAME_PROPERTY_ID,
- privilegeEntity.getPermission().getPermissionName(), requestedIds);
-
- PrincipalEntity principal = privilegeEntity.getPrincipal();
- Long principalId = principal.getId();
-
- if (userEntities.containsKey(principalId)) {
- UserEntity userEntity = userEntities.get(principalId);
- setResourceProperty(resource, PRINCIPAL_NAME_PROPERTY_ID, userEntity.getUserName(), requestedIds);
- } else if (groupEntities.containsKey(principalId)){
- GroupEntity groupEntity = groupEntities.get(principalId);
- setResourceProperty(resource, PRINCIPAL_NAME_PROPERTY_ID, groupEntity.getGroupName(), requestedIds);
- }
-
- setResourceProperty(resource, PRINCIPAL_TYPE_PROPERTY_ID, principal.getPrincipalType().getName(), requestedIds);
- return resource;
- }
-
- /**
- * Convert the given map of properties to a privilege entity for the resource
- * identified by the given id.
- *
- * @param properties the property map
- * @param resourceId the resource id
- *
- * @return the new privilege entity
- */
- protected PrivilegeEntity toEntity(Map<String, Object> properties, Long resourceId)
- throws AmbariException {
- PrivilegeEntity entity = new PrivilegeEntity();
- String permissionName = (String) properties.get(PERMISSION_NAME_PROPERTY_ID);
- ResourceEntity resourceEntity = resourceDAO.findById(resourceId);
- PermissionEntity permission = getPermission(permissionName, resourceEntity);
- if (permission == null) {
- throw new AmbariException("Can't find a permission named " + permissionName +
- " for the resource.");
- }
- entity.setPermission(permission);
- entity.setResource(resourceEntity);
-
- String principalName = (String) properties.get(PRINCIPAL_NAME_PROPERTY_ID);
- String principalType = (String) properties.get(PRINCIPAL_TYPE_PROPERTY_ID);
- if (PrincipalTypeEntity.GROUP_PRINCIPAL_TYPE_NAME.equalsIgnoreCase(principalType)) {
- GroupEntity groupEntity = groupDAO.findGroupByName(principalName);
- if (groupEntity != null) {
- entity.setPrincipal(principalDAO.findById(groupEntity.getPrincipal().getId()));
- }
- } else if (PrincipalTypeEntity.USER_PRINCIPAL_TYPE_NAME.equalsIgnoreCase(principalType)) {
- UserEntity userEntity = userDAO.findLocalUserByName(principalName);
- if (userEntity == null) {
- userEntity = userDAO.findLdapUserByName(principalName);
- }
- if (userEntity != null) {
- entity.setPrincipal(principalDAO.findById(userEntity.getPrincipal().getId()));
- }
- } else {
- throw new AmbariException("Unknown principal type " + principalType);
- }
- if (entity.getPrincipal() == null) {
- throw new AmbariException("Could not find " + principalType + " named " + principalName);
- }
- return entity;
- }
-
- // Get a permission with the given permission name for the given resource.
- protected PermissionEntity getPermission(String permissionName, ResourceEntity resourceEntity)
- throws AmbariException {
-
- return permissionDAO.findPermissionByNameAndType(permissionName, resourceEntity.getResourceType());
- }
-
- // Create a create command with the given properties map.
- private Command<Void> getCreateCommand(final Map<String, Object> properties) {
- return new Command<Void>() {
- @Override
- public Void invoke() throws AmbariException {
-
- // for a create there should only be one resource ...
- Set<Long> resourceIds = getResourceEntities(properties).keySet();
- Long resourceId = resourceIds.iterator().next();
-
- PrivilegeEntity entity = toEntity(properties, resourceId);
-
- if (entity.getPrincipal() == null) {
- throw new AmbariException("Can't find principal " + properties.get(PRINCIPAL_TYPE_PROPERTY_ID) +
- " " + properties.get(PRINCIPAL_NAME_PROPERTY_ID) + " for privilege.");
- }
- if (privilegeDAO.exists(entity)) {
- throw new DuplicateResourceException("The privilege already exists.");
- }
- if (!checkResourceTypes(entity)) {
- throw new AmbariException("Can't grant " + entity.getPermission().getResourceType().getName() +
- " permission on a " + entity.getResource().getResourceType().getName() + " resource.");
- }
- privilegeDAO.create(entity);
- return null;
- }
- };
- }
-
- // Create a delete command with the given predicate.
- private Command<Void> getDeleteCommand(final Predicate predicate) {
- return new Command<Void>() {
- @Override
- public Void invoke() throws AmbariException {
- try {
- for (Map<String, Object> resource : getPropertyMaps(predicate)) {
- if (resource.get(PRIVILEGE_ID_PROPERTY_ID) == null) {
- throw new AmbariException("Privilege ID should be provided for this request");
- }
- PrivilegeEntity entity = privilegeDAO.findById(Integer.valueOf(resource.get(PRIVILEGE_ID_PROPERTY_ID).toString()));
- if (entity != null) {
- if (!checkResourceTypes(entity)) {
- throw new AmbariException("Can't remove " + entity.getPermission().getResourceType().getName() +
- " permission from a " + entity.getResource().getResourceType().getName() + " resource.");
- }
- privilegeDAO.remove(entity);
- }
- }
- } catch (Exception e) {
- throw new AmbariException("Caught exception deleting privilege.", e);
- }
- return null;
- }
- };
- }
-
- private Command<Void> getUpdateCommand(final Request request, final Predicate predicate) {
- return new Command<Void>() {
- @Override
- public Void invoke() throws AmbariException {
- Long resource = null;
- final List<PrivilegeEntity> requiredEntities = new ArrayList<PrivilegeEntity>();
- for (Map<String, Object> properties: request.getProperties()) {
- Set<Long> resourceIds = getResourceEntities(properties).keySet();
- Long resourceId = resourceIds.iterator().next();
-
- if (resource != null && resourceId != resource) {
- throw new AmbariException("Can't update privileges of multiple resources in one request");
- }
- resource = resourceId;
-
- PrivilegeEntity entity = toEntity(properties, resourceId);
- requiredEntities.add(entity);
- }
- if (resource == null) {
- // request body is empty, use predicate instead
- resource = getResourceEntityId(predicate);
- // if the predicate does not identify a single resource or the resource is not available for update
- if (resource == null) {
- return null;
- }
- }
- final List<PrivilegeEntity> currentPrivileges = privilegeDAO.findByResourceId(resource);
-
- for (PrivilegeEntity requiredPrivilege: requiredEntities) {
- boolean isInBothLists = false;
- for (PrivilegeEntity currentPrivilege: currentPrivileges) {
- if (requiredPrivilege.getPermission().getPermissionName().equals(currentPrivilege.getPermission().getPermissionName()) &&
- requiredPrivilege.getPrincipal().getId().equals(currentPrivilege.getPrincipal().getId())) {
- isInBothLists = true;
- break;
- }
- }
- if (!isInBothLists) {
- if (!checkResourceTypes(requiredPrivilege)) {
- throw new AmbariException("Can't grant " + requiredPrivilege.getPermission().getResourceType().getName() +
- " permission on a " + requiredPrivilege.getResource().getResourceType().getName() + " resource.");
- }
-
- privilegeDAO.create(requiredPrivilege);
- }
- }
- for (PrivilegeEntity currentPrivilege: currentPrivileges) {
- boolean isInBothLists = false;
- for (PrivilegeEntity requiredPrivilege: requiredEntities) {
- if (requiredPrivilege.getPermission().getPermissionName().equals(currentPrivilege.getPermission().getPermissionName()) &&
- requiredPrivilege.getPrincipal().getId().equals(currentPrivilege.getPrincipal().getId())) {
- isInBothLists = true;
- break;
- }
- }
- if (!isInBothLists) {
- if (!checkResourceTypes(currentPrivilege)) {
- throw new AmbariException("Can't remove " + currentPrivilege.getPermission().getResourceType().getName() +
- " permission from a " + currentPrivilege.getResource().getResourceType().getName() + " resource.");
- }
- privilegeDAO.remove(currentPrivilege);
- }
- }
- return null;
- }
- };
- }
-}
-
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RecommendationResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RecommendationResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RecommendationResourceProvider.java
deleted file mode 100644
index 40a1791..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RecommendationResourceProvider.java
+++ /dev/null
@@ -1,156 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.controller.internal;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.Status;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.api.services.stackadvisor.StackAdvisorException;
-import org.apache.ambari.server.api.services.stackadvisor.StackAdvisorRequest;
-import org.apache.ambari.server.api.services.stackadvisor.StackAdvisorRequestException;
-import org.apache.ambari.server.api.services.stackadvisor.recommendations.RecommendationResponse;
-import org.apache.ambari.server.api.services.stackadvisor.recommendations.RecommendationResponse.BindingHostGroup;
-import org.apache.ambari.server.api.services.stackadvisor.recommendations.RecommendationResponse.HostGroup;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
-import org.apache.ambari.server.controller.spi.Request;
-import org.apache.ambari.server.controller.spi.RequestStatus;
-import org.apache.ambari.server.controller.spi.Resource;
-import org.apache.ambari.server.controller.spi.Resource.Type;
-import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
-import org.apache.ambari.server.controller.spi.SystemException;
-import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
-import org.apache.ambari.server.controller.utilities.PropertyHelper;
-
-public class RecommendationResourceProvider extends StackAdvisorResourceProvider {
-
- protected static final String RECOMMENDATION_ID_PROPERTY_ID = PropertyHelper.getPropertyId(
- "Recommendation", "id");
-
- protected static final String HOSTS_PROPERTY_ID = "hosts";
- protected static final String SERVICES_PROPERTY_ID = "services";
- protected static final String RECOMMEND_PROPERTY_ID = "recommend";
-
- protected static final String BLUEPRINT_CONFIGURATIONS_PROPERTY_ID = PropertyHelper
- .getPropertyId("recommendations/blueprint", "configurations");
-
- protected static final String BLUEPRINT_HOST_GROUPS_PROPERTY_ID = PropertyHelper.getPropertyId(
- "recommendations/blueprint", "host_groups");
- protected static final String BLUEPRINT_HOST_GROUPS_NAME_PROPERTY_ID = "name";
- protected static final String BLUEPRINT_HOST_GROUPS_COMPONENTS_PROPERTY_ID = "components";
-
- protected static final String BINDING_HOST_GROUPS_PROPERTY_ID = PropertyHelper.getPropertyId(
- "recommendations/blueprint_cluster_binding", "host_groups");
- protected static final String BINDING_HOST_GROUPS_NAME_PROPERTY_ID = "name";
- protected static final String BINDING_HOST_GROUPS_HOSTS_PROPERTY_ID = "hosts";
-
- private static Set<String> pkPropertyIds = new HashSet<String>(
- Arrays.asList(new String[] { RECOMMENDATION_ID_PROPERTY_ID }));
-
- protected RecommendationResourceProvider(Set<String> propertyIds,
- Map<Type, String> keyPropertyIds, AmbariManagementController managementController) {
- super(propertyIds, keyPropertyIds, managementController);
- }
-
- @Override
- protected String getRequestTypePropertyId() {
- return RECOMMEND_PROPERTY_ID;
- }
-
- @Override
- public RequestStatus createResources(final Request request) throws SystemException,
- UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException {
- StackAdvisorRequest recommendationRequest = prepareStackAdvisorRequest(request);
-
- final RecommendationResponse response;
- try {
- response = saHelper.recommend(recommendationRequest);
- } catch (StackAdvisorRequestException e) {
- LOG.warn("Error occured during recommnedation", e);
- throw new IllegalArgumentException(e.getMessage(), e);
- } catch (StackAdvisorException e) {
- LOG.warn("Error occured during recommnedation", e);
- throw new SystemException(e.getMessage(), e);
- }
-
- Resource recommendation = createResources(new Command<Resource>() {
- @Override
- public Resource invoke() throws AmbariException {
-
- Resource resource = new ResourceImpl(Resource.Type.Recommendation);
- setResourceProperty(resource, RECOMMENDATION_ID_PROPERTY_ID, response.getId(), getPropertyIds());
- setResourceProperty(resource, STACK_NAME_PROPERTY_ID, response.getVersion().getStackName(),
- getPropertyIds());
- setResourceProperty(resource, STACK_VERSION_PROPERTY_ID, response.getVersion()
- .getStackVersion(), getPropertyIds());
- setResourceProperty(resource, HOSTS_PROPERTY_ID, response.getHosts(), getPropertyIds());
- setResourceProperty(resource, SERVICES_PROPERTY_ID, response.getServices(),
- getPropertyIds());
- setResourceProperty(resource, BLUEPRINT_CONFIGURATIONS_PROPERTY_ID, response
- .getRecommendations().getBlueprint().getConfigurations(), getPropertyIds());
-
- Set<HostGroup> hostGroups = response.getRecommendations().getBlueprint().getHostGroups();
- List<Map<String, Object>> listGroupProps = new ArrayList<Map<String, Object>>();
- for (HostGroup hostGroup : hostGroups) {
- Map<String, Object> mapGroupProps = new HashMap<String, Object>();
- mapGroupProps.put(BLUEPRINT_HOST_GROUPS_NAME_PROPERTY_ID, hostGroup.getName());
- mapGroupProps
- .put(BLUEPRINT_HOST_GROUPS_COMPONENTS_PROPERTY_ID, hostGroup.getComponents());
- listGroupProps.add(mapGroupProps);
- }
- setResourceProperty(resource, BLUEPRINT_HOST_GROUPS_PROPERTY_ID, listGroupProps,
- getPropertyIds());
-
- Set<BindingHostGroup> bindingHostGroups = response.getRecommendations()
- .getBlueprintClusterBinding().getHostGroups();
- List<Map<String, Object>> listBindingGroupProps = new ArrayList<Map<String, Object>>();
- for (BindingHostGroup hostGroup : bindingHostGroups) {
- Map<String, Object> mapGroupProps = new HashMap<String, Object>();
- mapGroupProps.put(BINDING_HOST_GROUPS_NAME_PROPERTY_ID, hostGroup.getName());
- mapGroupProps.put(BINDING_HOST_GROUPS_HOSTS_PROPERTY_ID, hostGroup.getHosts());
- listBindingGroupProps.add(mapGroupProps);
- }
- setResourceProperty(resource, BINDING_HOST_GROUPS_PROPERTY_ID, listBindingGroupProps,
- getPropertyIds());
-
- return resource;
- }
- });
- notifyCreate(Resource.Type.Recommendation, request);
-
- Set<Resource> resources = new HashSet<Resource>(Arrays.asList(recommendation));
- return new RequestStatusImpl(null, resources);
- }
-
- @Override
- protected Set<String> getPKPropertyIds() {
- return pkPropertyIds;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/ambari/blob/7e28d1e3/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
index 561f5d9..c7eecec 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
@@ -24,7 +24,6 @@ import org.apache.ambari.server.actionmanager.HostRoleStatus;
import org.apache.ambari.server.api.services.BaseRequest;
import org.apache.ambari.server.controller.AmbariManagementController;
import org.apache.ambari.server.controller.ExecuteActionRequest;
-import org.apache.ambari.server.controller.RequestRequest;
import org.apache.ambari.server.controller.RequestStatusResponse;
import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
import org.apache.ambari.server.controller.spi.NoSuchResourceException;
@@ -43,7 +42,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -58,7 +56,6 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider
protected static final String REQUEST_CLUSTER_NAME_PROPERTY_ID = "Requests/cluster_name";
protected static final String REQUEST_ID_PROPERTY_ID = "Requests/id";
protected static final String REQUEST_STATUS_PROPERTY_ID = "Requests/request_status";
- protected static final String REQUEST_ABORT_REASON_PROPERTY_ID = "Requests/abort_reason";
protected static final String REQUEST_CONTEXT_ID = "Requests/request_context";
public static final String REQUEST_SOURCE_SCHEDULE = "Requests/request_schedule";
public static final String REQUEST_SOURCE_SCHEDULE_ID = "Requests/request_schedule/schedule_id";
@@ -70,7 +67,6 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider
protected static final String REQUEST_CREATE_TIME_ID = "Requests/create_time";
protected static final String REQUEST_START_TIME_ID = "Requests/start_time";
protected static final String REQUEST_END_TIME_ID = "Requests/end_time";
- protected static final String REQUEST_EXCLUSIVE_ID = "Requests/exclusive";
protected static final String REQUEST_TASK_CNT_ID = "Requests/task_count";
protected static final String REQUEST_FAILED_TASK_CNT_ID = "Requests/failed_task_count";
protected static final String REQUEST_ABORTED_TASK_CNT_ID = "Requests/aborted_task_count";
@@ -84,7 +80,6 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider
protected static final String HOSTS_ID = "hosts";
protected static final String ACTION_ID = "action";
protected static final String INPUTS_ID = "parameters";
- protected static final String EXLUSIVE_ID = "exclusive";
private static Set<String> pkPropertyIds =
new HashSet<String>(Arrays.asList(new String[]{
REQUEST_ID_PROPERTY_ID}));
@@ -164,82 +159,9 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider
}
@Override
- public RequestStatus updateResources(Request requestInfo, Predicate predicate)
- throws SystemException, UnsupportedPropertyException,
- NoSuchResourceException, NoSuchParentResourceException {
- AmbariManagementController amc = getManagementController();
- final Set<RequestRequest> requests = new HashSet<RequestRequest>();
-
- Iterator<Map<String,Object>> iterator = requestInfo.getProperties().iterator();
- if (iterator.hasNext()) {
- for (Map<String, Object> propertyMap : getPropertyMaps(iterator.next(), predicate)) {
- requests.add(getRequest(propertyMap));
- }
- }
- // Validate
- List<org.apache.ambari.server.actionmanager.Request> targets =
- new ArrayList<org.apache.ambari.server.actionmanager.Request>();
- for (RequestRequest updateRequest : requests) {
- ActionManager actionManager = amc.getActionManager();
- List<org.apache.ambari.server.actionmanager.Request> internalRequests =
- actionManager.getRequests(Collections.singletonList(updateRequest.getRequestId()));
- if (internalRequests.size() == 0) {
- throw new IllegalArgumentException(
- String.format("Request %s does not exist", updateRequest.getRequestId()));
- }
- // There should be only one request with this id (or no request at all)
- org.apache.ambari.server.actionmanager.Request internalRequest = internalRequests.get(0);
- // Validate update request (check constraints on state value and presense of abort reason)
- if (updateRequest.getAbortReason() == null || updateRequest.getAbortReason().isEmpty()) {
- throw new IllegalArgumentException("Abort reason can not be empty.");
- }
-
- HostRoleStatus internalRequestStatus = calculateSummaryStatus(
- calculateRequestStatusCounters(internalRequest),
- internalRequest.getCommands().size()
- );
- if (updateRequest.getStatus() != HostRoleStatus.ABORTED) {
- throw new IllegalArgumentException(
- String.format("%s is wrong value. The only allowed value " +
- "for updating request status is ABORTED",
- updateRequest.getStatus()));
- }
- if (internalRequestStatus.isCompletedState()) {
- throw new IllegalArgumentException(
- String.format("Can not set request that is in %s state to %s state.",
- internalRequestStatus.toString(), updateRequest.getStatus()));
- }
- // Validation passed
- targets.add(internalRequest);
- }
- // Perform update
- Iterator<RequestRequest> reqIterator = requests.iterator();
- for (int i = 0; i < targets.size(); i++) {
- org.apache.ambari.server.actionmanager.Request target = targets.get(i);
- String reason = reqIterator.next().getAbortReason();
- amc.getActionManager().cancelRequest(target.getRequestId(), reason);
- }
- return getRequestStatus(null);
- }
-
- private RequestRequest getRequest(Map<String, Object> propertyMap) {
- // Cluster name may be empty for custom actions
- String clusterNameStr = (String) propertyMap.get(REQUEST_CLUSTER_NAME_PROPERTY_ID);
- String requestIdStr = (String) propertyMap.get(REQUEST_ID_PROPERTY_ID);
- long requestId = Integer.valueOf(requestIdStr);
- String requestStatusStr = (String) propertyMap.get(REQUEST_STATUS_PROPERTY_ID);
- HostRoleStatus requestStatus = null;
- if (requestStatusStr != null) {
- // This conversion may throw IllegalArgumentException, it is OK
- // in this case it will be mapped to HTTP 400 Bad Request
- requestStatus = HostRoleStatus.valueOf(requestStatusStr);
- }
- String abortReason = (String) propertyMap.get(REQUEST_ABORT_REASON_PROPERTY_ID);
- RequestRequest requestRequest = new RequestRequest(clusterNameStr, requestId);
- requestRequest.setStatus(requestStatus);
- requestRequest.setAbortReason(abortReason);
- return requestRequest;
-
+ public RequestStatus updateResources(Request request, Predicate predicate)
+ throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
+ throw new UnsupportedOperationException("Not currently supported.");
}
@Override
@@ -319,19 +241,13 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider
params.put(key.substring(keyPrefix.length()), requestInfoProperties.get(key));
}
}
-
- boolean exclusive = false;
- if (requestInfoProperties.containsKey(EXLUSIVE_ID)) {
- exclusive = Boolean.valueOf(requestInfoProperties.get(EXLUSIVE_ID).trim());
- }
-
return new ExecuteActionRequest(
(String) propertyMap.get(REQUEST_CLUSTER_NAME_PROPERTY_ID),
commandName,
actionName,
resourceFilterList,
operationLevel,
- params, exclusive);
+ params);
}
// Get all of the request resources for the given properties
@@ -430,83 +346,72 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider
setResourceProperty(resource, REQUEST_CREATE_TIME_ID, request.getCreateTime(), requestedPropertyIds);
setResourceProperty(resource, REQUEST_START_TIME_ID, request.getStartTime(), requestedPropertyIds);
setResourceProperty(resource, REQUEST_END_TIME_ID, request.getEndTime(), requestedPropertyIds);
- setResourceProperty(resource, REQUEST_EXCLUSIVE_ID, request.isExclusive(), requestedPropertyIds);
-
if (request.getRequestScheduleId() != null) {
setResourceProperty(resource, REQUEST_SOURCE_SCHEDULE_ID, request.getRequestScheduleId(), requestedPropertyIds);
} else {
setResourceProperty(resource, REQUEST_SOURCE_SCHEDULE, null, requestedPropertyIds);
}
- int taskCount = request.getCommands().size();
- Map<HostRoleStatus, Integer> hostRoleStatusCounters = calculateRequestStatusCounters(request);
- HostRoleStatus requestStatus = calculateSummaryStatus(hostRoleStatusCounters, taskCount);
+ List<HostRoleCommand> commands = request.getCommands();
+
+ int taskCount = commands.size();
+ int completedTaskCount = 0;
+ int queuedTaskCount = 0;
+ int pendingTaskCount = 0;
+ int failedTaskCount = 0;
+ int abortedTaskCount = 0;
+ int timedOutTaskCount = 0;
+
+ for (HostRoleCommand hostRoleCommand : commands) {
+ HostRoleStatus status = hostRoleCommand.getStatus();
+ if (status.isCompletedState()) {
+ completedTaskCount++;
+
+ switch (status) {
+ case ABORTED:
+ abortedTaskCount++;
+ break;
+ case FAILED:
+ failedTaskCount++;
+ break;
+ case TIMEDOUT:
+ timedOutTaskCount++;
+ break;
+ }
+ } else if (status.equals(HostRoleStatus.QUEUED)) {
+ queuedTaskCount++;
+ } else if (status.equals(HostRoleStatus.PENDING)) {
+ pendingTaskCount++;
+ }
+ }
+
+ int inProgressTaskCount = taskCount - completedTaskCount - queuedTaskCount - pendingTaskCount;
+
+ LOG.debug("taskCount={}, inProgressTaskCount={}, completedTaskCount={}, queuedTaskCount={}, " +
+ "pendingTaskCount={}, failedTaskCount={}, abortedTaskCount={}, timedOutTaskCount={}",
+ taskCount, inProgressTaskCount, completedTaskCount, queuedTaskCount, pendingTaskCount,
+ failedTaskCount, abortedTaskCount, timedOutTaskCount);
+ // determine request status
+ HostRoleStatus requestStatus = failedTaskCount > 0 ? HostRoleStatus.FAILED :
+ abortedTaskCount > 0 ? HostRoleStatus.ABORTED :
+ timedOutTaskCount > 0 ? HostRoleStatus.TIMEDOUT :
+ inProgressTaskCount > 0 ? HostRoleStatus.IN_PROGRESS :
+ completedTaskCount == taskCount ? HostRoleStatus.COMPLETED :
+ HostRoleStatus.PENDING;
double progressPercent =
- ((hostRoleStatusCounters.get(HostRoleStatus.QUEUED) * 0.09 +
- hostRoleStatusCounters.get(HostRoleStatus.IN_PROGRESS) * 0.35 +
- hostRoleStatusCounters.get(HostRoleStatus.COMPLETED)) / (double) taskCount) * 100.0;
+ ((queuedTaskCount * 0.09 + inProgressTaskCount * 0.35 + completedTaskCount) / (double) taskCount) * 100.0;
setResourceProperty(resource, REQUEST_STATUS_PROPERTY_ID, requestStatus.toString(), requestedPropertyIds);
setResourceProperty(resource, REQUEST_TASK_CNT_ID, taskCount, requestedPropertyIds);
- setResourceProperty(resource, REQUEST_FAILED_TASK_CNT_ID,
- hostRoleStatusCounters.get(HostRoleStatus.FAILED), requestedPropertyIds);
- setResourceProperty(resource, REQUEST_ABORTED_TASK_CNT_ID,
- hostRoleStatusCounters.get(HostRoleStatus.ABORTED), requestedPropertyIds);
- setResourceProperty(resource, REQUEST_TIMED_OUT_TASK_CNT_ID,
- hostRoleStatusCounters.get(HostRoleStatus.TIMEDOUT), requestedPropertyIds);
- setResourceProperty(resource, REQUEST_QUEUED_TASK_CNT_ID,
- hostRoleStatusCounters.get(HostRoleStatus.QUEUED), requestedPropertyIds);
- setResourceProperty(resource, REQUEST_COMPLETED_TASK_CNT_ID,
- hostRoleStatusCounters.get(HostRoleStatus.COMPLETED), requestedPropertyIds);
+ setResourceProperty(resource, REQUEST_FAILED_TASK_CNT_ID, failedTaskCount, requestedPropertyIds);
+ setResourceProperty(resource, REQUEST_ABORTED_TASK_CNT_ID, abortedTaskCount, requestedPropertyIds);
+ setResourceProperty(resource, REQUEST_TIMED_OUT_TASK_CNT_ID, timedOutTaskCount, requestedPropertyIds);
+ setResourceProperty(resource, REQUEST_QUEUED_TASK_CNT_ID, queuedTaskCount, requestedPropertyIds);
+ setResourceProperty(resource, REQUEST_COMPLETED_TASK_CNT_ID, completedTaskCount, requestedPropertyIds);
setResourceProperty(resource, REQUEST_PROGRESS_PERCENT_ID, progressPercent, requestedPropertyIds);
return resource;
}
- /**
- * Returns counts of tasks that are in various states.
- */
- private Map<HostRoleStatus, Integer> calculateRequestStatusCounters(org.apache.ambari.server.actionmanager.Request request) {
- List<HostRoleCommand> commands = request.getCommands();
- Map<HostRoleStatus, Integer> counters = new HashMap<HostRoleStatus, Integer>();
- int totalTasks = commands.size();
- // initialize
- for (HostRoleStatus hostRoleStatus : HostRoleStatus.values()) {
- counters.put(hostRoleStatus, 0);
- }
- // calculate counts
- for (HostRoleCommand hostRoleCommand : commands) {
- HostRoleStatus status = hostRoleCommand.getStatus();
- if (status.isCompletedState() &&
- status != HostRoleStatus.COMPLETED ) { // we don't want to count twice
- // Increase total number of completed tasks;
- counters.put(HostRoleStatus.COMPLETED, counters.get(HostRoleStatus.COMPLETED) + 1);
- }
- // Increment counter for particular status
- counters.put(status, counters.get(status) + 1);
- }
- // We overwrite the value to have the sum converged
- counters.put(HostRoleStatus.IN_PROGRESS,
- totalTasks - counters.get(HostRoleStatus.COMPLETED)-
- counters.get(HostRoleStatus.QUEUED)-
- counters.get(HostRoleStatus.PENDING));
- return counters;
- }
-
- /**
- * @param counters counts of tasks that are in various states.
- * @param totalTasks total number of tasks in request
- * @return summary request status based on statuses of tasks in different
- * states.
- */
- private HostRoleStatus calculateSummaryStatus(Map<HostRoleStatus, Integer> counters, int totalTasks) {
- return counters.get(HostRoleStatus.FAILED) > 0 ? HostRoleStatus.FAILED :
- // TODO (dlysnichenko): maybe change order of FAILED and ABORTED?
- counters.get(HostRoleStatus.ABORTED) > 0 ? HostRoleStatus.ABORTED :
- counters.get(HostRoleStatus.TIMEDOUT) > 0 ? HostRoleStatus.TIMEDOUT :
- counters.get(HostRoleStatus.IN_PROGRESS) > 0 ? HostRoleStatus.IN_PROGRESS :
- counters.get(HostRoleStatus.COMPLETED) == totalTasks ? HostRoleStatus.COMPLETED :
- HostRoleStatus.PENDING;
- }
}