private boolean isValid(Alert alert) {
    Long clusterId = alert.getClusterId();
    String serviceName = alert.getService();
    String componentName = alert.getComponent();
    String hostName = alert.getHostName();

    // AMBARI/AMBARI_SERVER is always a valid service/component combination
    String ambariServiceName = RootService.AMBARI.name();
    String ambariServerComponentName = RootComponent.AMBARI_SERVER.name();
    String ambariAgentComponentName = RootComponent.AMBARI_AGENT.name();
    if (ambariServiceName.equals(serviceName) && ambariServerComponentName.equals(componentName)) {
      return true;
    }

    // if the alert is not bound to a cluster, then it's most likely a
    // host alert and is always valid as long as the host exists
    Clusters clusters = m_clusters.get();
    if (clusterId == null) {
      // no cluster, no host; return true out of respect for the unknown alert
      if (StringUtils.isBlank(hostName)) {
        return true;
      }

      // if a host is reported, it must be registered to some cluster somewhere
      if (!clusters.hostExists(hostName)) {
        LOG.error("Unable to process alert {} for an invalid host named {}",
            alert.getName(), hostName);
        return false;
      }

      // no cluster, valid host; return true
      return true;
    }

    // at this point the following criteria is guaranteed, so get the cluster
    // - a cluster exists
    // - this is not for AMBARI_SERVER component
    final Cluster cluster;
    try {
      cluster = clusters.getCluster(clusterId);
      if (null == cluster) {
        LOG.error("Unable to process alert {} for cluster id={}", alert.getName(), clusterId);
        return false;
      }
    } catch (AmbariException ambariException) {
      String msg = String.format("Unable to process alert %s for cluster id=%s", alert.getName(), clusterId);
      LOG.error(msg, ambariException);
      return false;
    }

    // at this point the following criteria is guaranteed
    // - a cluster exists
    // - this is not for AMBARI_SERVER component
    //
    // if the alert is for AMBARI/AMBARI_AGENT, then it's valid IFF
    // the agent's host is still a part of the reported cluster
    if (ambariServiceName.equals(serviceName) && ambariAgentComponentName.equals(componentName)) {
      // agents MUST report a hostname
      if (StringUtils.isBlank(hostName) || !clusters.hostExists(hostName)
          || !clusters.isHostMappedToCluster(clusterId, hostName)) {
        LOG.warn(
            "Unable to process alert {} for cluster {} and host {} because the host is not a part of the cluster.",
            alert.getName(), hostName);

        return false;
      }

      // AMBARI/AMBARI_AGENT and valid host; return true
      return true;
    }

    // at this point the following criteria is guaranteed
    // - a cluster exists
    // - not for the AMBARI service
    if (StringUtils.isNotBlank(hostName)) {
      // if valid hostname
      if (!clusters.hostExists(hostName)) {
        LOG.warn("Unable to process alert {} for an invalid host named {}",
            alert.getName(), hostName);
        return false;
      }

      if (!cluster.getServices().containsKey(serviceName)) {
        LOG.warn("Unable to process alert {} for an invalid service named {}",
            alert.getName(), serviceName);

        return false;
      }

      // if the alert is for a host/component then verify that the component
      // is actually installed on that host
      if (null != componentName &&
          !cluster.getHosts(serviceName, componentName).contains(hostName)) {
        LOG.warn(
            "Unable to process alert {} for an invalid service {} and component {} on host {}",
            alert.getName(), serviceName, componentName, hostName);
        return false;
      }
    }

    return true;
  }
Gets whether the specified alert is valid for its reported cluster,
service, component, and host. This method is necessary for the following
cases
<ul>
<li>A service/component is removed, but an alert queued for reporting is
received after that event.</li>
<li>A host is removed from the cluster but the agent is still running and
reporting</li>
<li>A cluster is renamed</li>
</ul>

@param alert
         the alert.
@return {@code true} if the alert is for a valid combination of
        cluster/service/component/host.
ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertReceivedListener.java