try {
if (!clusters.getClustersForHost(request.getHostname()).contains(cluster)) {
// case where host exists but not associated with given cluster
LOG.error("Host doesn't belong to cluster");
throw new ParentObjectNotFoundException("Parent Host resource doesn't exist",
new HostNotFoundException(request.getClusterName(), request.getHostname()));
}
} catch (HostNotFoundException e) {
LOG.error("Host not found", e);
// creating new HostNotFoundException to add cluster name
throw new ParentObjectNotFoundException("Parent Host resource doesn't exist",
new HostNotFoundException(request.getClusterName(), request.getHostname()));
}
}
if (request.getComponentName() != null) {
if (request.getServiceName() == null
|| request.getServiceName().isEmpty()) {
StackId stackId = cluster.getDesiredStackVersion();
String serviceName =
ambariMetaInfo.getComponentToService(stackId.getStackName(),
stackId.getStackVersion(), request.getComponentName());
if (LOG.isDebugEnabled()) {
LOG.debug("Looking up service name for component"
+ ", componentName=" + request.getComponentName()
+ ", serviceName=" + serviceName
+ ", stackInfo=" + stackId.getStackId());
}
if (serviceName == null
|| serviceName.isEmpty()) {
LOG.error("Unable to find service for component {}", request.getComponentName());
throw new ServiceComponentHostNotFoundException(
cluster.getClusterName(), null, request.getComponentName(), request.getHostname());
}
request.setServiceName(serviceName);
}
}
Set<Service> services = new HashSet<Service>();
if (request.getServiceName() != null && !request.getServiceName().isEmpty()) {
services.add(cluster.getService(request.getServiceName()));
} else {
services.addAll(cluster.getServices().values());
}
Set<ServiceComponentHostResponse> response =
new HashSet<ServiceComponentHostResponse>();
boolean checkDesiredState = false;
State desiredStateToCheck = null;
boolean filterBasedConfigStaleness = false;
boolean staleConfig = true;
if (request.getStaleConfig() != null) {
filterBasedConfigStaleness = true;
staleConfig = "true".equals(request.getStaleConfig().toLowerCase());
}
if (request.getDesiredState() != null
&& !request.getDesiredState().isEmpty()) {
desiredStateToCheck = State.valueOf(request.getDesiredState());
if (!desiredStateToCheck.isValidDesiredState()) {
throw new IllegalArgumentException("Invalid arguments, invalid desired"
+ " state, desiredState=" + desiredStateToCheck);
}
checkDesiredState = true;
}
Map<String, Host> hosts = clusters.getHostsForCluster(cluster.getClusterName());
for (Service s : services) {
// filter on component name if provided
Set<ServiceComponent> components = new HashSet<ServiceComponent>();
if (request.getComponentName() != null) {
components.add(s.getServiceComponent(request.getComponentName()));
} else {
components.addAll(s.getServiceComponents().values());
}
for (ServiceComponent sc : components) {
if (request.getComponentName() != null) {
if (!sc.getName().equals(request.getComponentName())) {
continue;
}
}
// filter on hostname if provided
// filter on desired state if provided
Map<String, ServiceComponentHost> serviceComponentHostMap =
sc.getServiceComponentHosts();
if (request.getHostname() != null) {
try {
if (serviceComponentHostMap == null
|| !serviceComponentHostMap.containsKey(request.getHostname())) {
ServiceComponentHostNotFoundException e = new ServiceComponentHostNotFoundException(cluster.getClusterName(),
s.getName(), sc.getName(), request.getHostname());
throw e;
}
ServiceComponentHost sch = serviceComponentHostMap.get(request.getHostname());
if (checkDesiredState && (desiredStateToCheck != sch.getDesiredState())) {
continue;
}
if (request.getAdminState() != null) {
String stringToMatch =
sch.getComponentAdminState() == null ? "" : sch.getComponentAdminState().name();
if (!request.getAdminState().equals(stringToMatch)) {
continue;
}
}
ServiceComponentHostResponse r = sch.convertToResponse();
if (filterBasedConfigStaleness && r.isStaleConfig() != staleConfig) {
continue;
}
Host host = hosts.get(sch.getHostName());
if (host == null) {
throw new HostNotFoundException(cluster.getClusterName(), sch.getHostName());
}
r.setMaintenanceState(maintenanceStateHelper.getEffectiveState(sch, host).name());
response.add(r);
} catch (ServiceComponentHostNotFoundException e) {
if (request.getServiceName() != null && request.getComponentName() != null) {
LOG.error("ServiceComponentHost not found ", e);
throw new ServiceComponentHostNotFoundException(cluster.getClusterName(),
request.getServiceName(), request.getComponentName(), request.getHostname());
} else {
LOG.debug("Ignoring not specified host_component ", e);
// ignore this since host_component was not specified
// this is an artifact of how we get host_components and can happen
// in case where we get all host_components for a host
}
}
} else {
for (ServiceComponentHost sch : serviceComponentHostMap.values()) {
if (checkDesiredState && (desiredStateToCheck != sch.getDesiredState())) {
continue;
}
if (request.getAdminState() != null) {
String stringToMatch =
sch.getComponentAdminState() == null ? "" : sch.getComponentAdminState().name();
if (!request.getAdminState().equals(stringToMatch)) {
continue;
}
}
ServiceComponentHostResponse r = sch.convertToResponse();
if (filterBasedConfigStaleness && r.isStaleConfig() != staleConfig) {
continue;
}
Host host = hosts.get(sch.getHostName());
if (host == null) {
throw new HostNotFoundException(cluster.getClusterName(), sch.getHostName());
}
r.setMaintenanceState(maintenanceStateHelper.getEffectiveState(sch, host).name());
response.add(r);
}