You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by jo...@apache.org on 2014/11/28 14:08:17 UTC

[1/2] ambari git commit: AMBARI-8460 - Alerts: AlertDefinition and AlertGroup Automatic Creation On Startup (jonathanhurley)

Repository: ambari
Updated Branches:
  refs/heads/trunk 378cf1366 -> 9159421b3


http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
index 6467b31..6876877 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
@@ -29,14 +29,10 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.TimeZone;
 import java.util.UUID;
 
-import junit.framework.Assert;
-
 import org.apache.ambari.server.controller.AlertHistoryRequest;
 import org.apache.ambari.server.controller.internal.AlertHistoryResourceProvider;
 import org.apache.ambari.server.controller.internal.PageRequestImpl;
@@ -60,17 +56,12 @@ import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Host;
-import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.MaintenanceState;
-import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentFactory;
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.ServiceComponentHostFactory;
 import org.apache.ambari.server.state.ServiceFactory;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.State;
 import org.apache.ambari.server.state.alert.Scope;
 import org.apache.ambari.server.state.alert.SourceType;
 import org.junit.After;
@@ -91,7 +82,7 @@ public class AlertsDAOTest {
   final static Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
 
   private Clusters m_clusters;
-  private Long m_clusterId;
+  private Cluster m_cluster;
   private Injector m_injector;
   private OrmTestHelper m_helper;
   private AlertsDAO m_dao;
@@ -101,6 +92,7 @@ public class AlertsDAOTest {
   private ServiceComponentFactory m_componentFactory;
   private ServiceComponentHostFactory m_schFactory;
   private AmbariEventPublisher m_eventPublisher;
+  private EventBus m_synchronizedBus;
 
   private AlertDaoHelper m_alertHelper;
 
@@ -112,7 +104,6 @@ public class AlertsDAOTest {
     m_injector = Guice.createInjector(new InMemoryDefaultTestModule());
     m_injector.getInstance(GuiceJpaInitializer.class);
     m_helper = m_injector.getInstance(OrmTestHelper.class);
-    m_clusterId = m_helper.createCluster();
     m_dao = m_injector.getInstance(AlertsDAO.class);
     m_definitionDao = m_injector.getInstance(AlertDefinitionDAO.class);
     m_serviceFactory = m_injector.getInstance(ServiceFactory.class);
@@ -122,22 +113,27 @@ public class AlertsDAOTest {
     m_clusters = m_injector.getInstance(Clusters.class);
     m_alertHelper = m_injector.getInstance(AlertDaoHelper.class);
 
-    // register a listener
-    EventBus synchronizedBus = new EventBus();
-    synchronizedBus.register(m_injector.getInstance(AlertMaintenanceModeListener.class));
-
     // !!! need a synchronous op for testing
+    m_synchronizedBus = new EventBus();
     Field field = AmbariEventPublisher.class.getDeclaredField("m_eventBus");
     field.setAccessible(true);
-    field.set(m_eventPublisher, synchronizedBus);
+    field.set(m_eventPublisher, m_synchronizedBus);
+
+    // install YARN so there is at least 1 service installed and no
+    // unexpected alerts since the test YARN service doesn't have any alerts
+    m_cluster = m_clusters.getClusterById(m_helper.createCluster());
+    m_helper.initializeClusterWithStack(m_cluster);
+    m_helper.addHost(m_clusters, m_cluster, HOSTNAME);
+    m_helper.installYarnService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
 
     // create 5 definitions
     for (int i = 0; i < 5; i++) {
       AlertDefinitionEntity definition = new AlertDefinitionEntity();
       definition.setDefinitionName("Alert Definition " + i);
-      definition.setServiceName("Service " + i);
-      definition.setComponentName(null);
-      definition.setClusterId(m_clusterId);
+      definition.setServiceName("YARN");
+      definition.setComponentName("Component " + i);
+      definition.setClusterId(m_cluster.getClusterId());
       definition.setHash(UUID.randomUUID().toString());
       definition.setScheduleInterval(Integer.valueOf(60));
       definition.setScope(Scope.SERVICE);
@@ -158,13 +154,13 @@ public class AlertsDAOTest {
       for (int i = 0; i < 10; i++) {
         AlertHistoryEntity history = new AlertHistoryEntity();
         history.setServiceName(definition.getServiceName());
-        history.setClusterId(m_clusterId);
+        history.setClusterId(m_cluster.getClusterId());
         history.setAlertDefinition(definition);
         history.setAlertLabel(definition.getDefinitionName() + " " + i);
         history.setAlertText(definition.getDefinitionName() + " " + i);
         history.setAlertTimestamp(calendar.getTimeInMillis());
+        history.setComponentName(definition.getComponentName());
         history.setHostName("h1");
-        history.setComponentName("Component " + i);
 
         history.setAlertState(AlertState.OK);
         if (i == 0 || i == 5) {
@@ -214,7 +210,7 @@ public class AlertsDAOTest {
    */
   @Test
   public void testFindAll() {
-    List<AlertHistoryEntity> alerts = m_dao.findAll(m_clusterId);
+    List<AlertHistoryEntity> alerts = m_dao.findAll(m_cluster.getClusterId());
     assertNotNull(alerts);
     assertEquals(50, alerts.size());
   }
@@ -235,18 +231,21 @@ public class AlertsDAOTest {
   @Test
   public void testFindCurrentByService() {
     List<AlertCurrentEntity> currentAlerts = m_dao.findCurrent();
+    int currentAlertExpectedCount = currentAlerts.size();
+    assertEquals(5, currentAlertExpectedCount);
+
     AlertCurrentEntity current = currentAlerts.get(0);
     AlertHistoryEntity history = current.getAlertHistory();
 
     assertNotNull(history);
 
-    currentAlerts = m_dao.findCurrentByService(m_clusterId,
+    currentAlerts = m_dao.findCurrentByService(m_cluster.getClusterId(),
         history.getServiceName());
 
     assertNotNull(currentAlerts);
-    assertEquals(1, currentAlerts.size());
+    assertEquals(currentAlertExpectedCount, currentAlerts.size());
 
-    currentAlerts = m_dao.findCurrentByService(m_clusterId, "foo");
+    currentAlerts = m_dao.findCurrentByService(m_cluster.getClusterId(), "foo");
 
     assertNotNull(currentAlerts);
     assertEquals(0, currentAlerts.size());
@@ -256,13 +255,13 @@ public class AlertsDAOTest {
    * Test looking up current by a host name.
    */
   @Test
-  public void testFindCurrentByHost() {
+  public void testFindCurrentByHost() throws Exception {
     // create a host
     AlertDefinitionEntity hostDef = new AlertDefinitionEntity();
     hostDef.setDefinitionName("Host Alert Definition ");
-    hostDef.setServiceName("HostService");
+    hostDef.setServiceName("YARN");
     hostDef.setComponentName(null);
-    hostDef.setClusterId(m_clusterId);
+    hostDef.setClusterId(m_cluster.getClusterId());
     hostDef.setHash(UUID.randomUUID().toString());
     hostDef.setScheduleInterval(Integer.valueOf(60));
     hostDef.setScope(Scope.HOST);
@@ -273,7 +272,7 @@ public class AlertsDAOTest {
     // history for the definition
     AlertHistoryEntity history = new AlertHistoryEntity();
     history.setServiceName(hostDef.getServiceName());
-    history.setClusterId(m_clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setAlertDefinition(hostDef);
     history.setAlertLabel(hostDef.getDefinitionName());
     history.setAlertText(hostDef.getDefinitionName());
@@ -289,12 +288,12 @@ public class AlertsDAOTest {
     m_dao.create(current);
 
     List<AlertCurrentEntity> currentAlerts = m_dao.findCurrentByHost(
-        m_clusterId, history.getHostName());
+        m_cluster.getClusterId(), history.getHostName());
 
     assertNotNull(currentAlerts);
     assertEquals(1, currentAlerts.size());
 
-    currentAlerts = m_dao.findCurrentByHost(m_clusterId, "foo");
+    currentAlerts = m_dao.findCurrentByHost(m_cluster.getClusterId(), "foo");
 
     assertNotNull(currentAlerts);
     assertEquals(0, currentAlerts.size());
@@ -310,21 +309,22 @@ public class AlertsDAOTest {
     allStates.add(AlertState.WARNING);
     allStates.add(AlertState.CRITICAL);
 
-    List<AlertHistoryEntity> history = m_dao.findAll(m_clusterId, allStates);
+    List<AlertHistoryEntity> history = m_dao.findAll(m_cluster.getClusterId(),
+        allStates);
     assertNotNull(history);
     assertEquals(50, history.size());
 
-    history = m_dao.findAll(m_clusterId,
+    history = m_dao.findAll(m_cluster.getClusterId(),
         Collections.singletonList(AlertState.OK));
     assertNotNull(history);
     assertEquals(40, history.size());
 
-    history = m_dao.findAll(m_clusterId,
+    history = m_dao.findAll(m_cluster.getClusterId(),
         Collections.singletonList(AlertState.CRITICAL));
     assertNotNull(history);
     assertEquals(10, history.size());
 
-    history = m_dao.findAll(m_clusterId,
+    history = m_dao.findAll(m_cluster.getClusterId(),
         Collections.singletonList(AlertState.WARNING));
     assertNotNull(history);
     assertEquals(0, history.size());
@@ -339,14 +339,14 @@ public class AlertsDAOTest {
     calendar.set(2014, Calendar.JANUARY, 1);
 
     // on or after 1/1/2014
-    List<AlertHistoryEntity> history = m_dao.findAll(m_clusterId,
+    List<AlertHistoryEntity> history = m_dao.findAll(m_cluster.getClusterId(),
         calendar.getTime(), null);
 
     assertNotNull(history);
     assertEquals(50, history.size());
 
     // on or before 1/1/2014
-    history = m_dao.findAll(m_clusterId, null, calendar.getTime());
+    history = m_dao.findAll(m_cluster.getClusterId(), null, calendar.getTime());
     assertNotNull(history);
     assertEquals(1, history.size());
 
@@ -357,17 +357,17 @@ public class AlertsDAOTest {
     calendar.set(2014, Calendar.JANUARY, 10);
     Date endDate = calendar.getTime();
 
-    history = m_dao.findAll(m_clusterId, startDate, endDate);
+    history = m_dao.findAll(m_cluster.getClusterId(), startDate, endDate);
     assertNotNull(history);
     assertEquals(6, history.size());
 
     // after 3/1
     calendar.set(2014, Calendar.MARCH, 5);
-    history = m_dao.findAll(m_clusterId, calendar.getTime(), null);
+    history = m_dao.findAll(m_cluster.getClusterId(), calendar.getTime(), null);
     assertNotNull(history);
     assertEquals(0, history.size());
 
-    history = m_dao.findAll(m_clusterId, endDate, startDate);
+    history = m_dao.findAll(m_cluster.getClusterId(), endDate, startDate);
     assertNotNull(history);
     assertEquals(0, history.size());
   }
@@ -375,10 +375,10 @@ public class AlertsDAOTest {
   @Test
   public void testFindCurrentByHostAndName() throws Exception {
     AlertCurrentEntity entity = m_dao.findCurrentByHostAndName(
-        m_clusterId.longValue(), "h2", "Alert Definition 1");
+        m_cluster.getClusterId(), "h2", "Alert Definition 1");
     assertNull(entity);
 
-    entity = m_dao.findCurrentByHostAndName(m_clusterId.longValue(), "h1",
+    entity = m_dao.findCurrentByHostAndName(m_cluster.getClusterId(), "h1",
         "Alert Definition 1");
 
     assertNotNull(entity);
@@ -391,16 +391,20 @@ public class AlertsDAOTest {
    */
   @Test
   public void testFindCurrentSummary() throws Exception {
-    AlertSummaryDTO summary = m_dao.findCurrentCounts(m_clusterId.longValue(),
+    AlertSummaryDTO summary = m_dao.findCurrentCounts(m_cluster.getClusterId(),
         null, null);
+
     assertEquals(5, summary.getOkCount());
 
-    AlertHistoryEntity h1 = m_dao.findCurrentByCluster(m_clusterId.longValue()).get(
+    AlertHistoryEntity h1 = m_dao.findCurrentByCluster(m_cluster.getClusterId()).get(
         2).getAlertHistory();
-    AlertHistoryEntity h2 = m_dao.findCurrentByCluster(m_clusterId.longValue()).get(
+
+    AlertHistoryEntity h2 = m_dao.findCurrentByCluster(m_cluster.getClusterId()).get(
         3).getAlertHistory();
-    AlertHistoryEntity h3 = m_dao.findCurrentByCluster(m_clusterId.longValue()).get(
+
+    AlertHistoryEntity h3 = m_dao.findCurrentByCluster(m_cluster.getClusterId()).get(
         4).getAlertHistory();
+
     h1.setAlertState(AlertState.WARNING);
     m_dao.merge(h1);
     h2.setAlertState(AlertState.CRITICAL);
@@ -413,7 +417,7 @@ public class AlertsDAOTest {
     int crit = 0;
     int unk = 0;
 
-    for (AlertCurrentEntity h : m_dao.findCurrentByCluster(m_clusterId.longValue())) {
+    for (AlertCurrentEntity h : m_dao.findCurrentByCluster(m_cluster.getClusterId())) {
       switch (h.getAlertHistory().getAlertState()) {
         case CRITICAL:
           crit++;
@@ -431,7 +435,7 @@ public class AlertsDAOTest {
 
     }
 
-    summary = m_dao.findCurrentCounts(m_clusterId.longValue(), null, null);
+    summary = m_dao.findCurrentCounts(m_cluster.getClusterId(), null, null);
     // !!! db-to-db compare
     assertEquals(ok, summary.getOkCount());
     assertEquals(warn, summary.getWarningCount());
@@ -444,20 +448,19 @@ public class AlertsDAOTest {
     assertEquals(1, summary.getCriticalCount());
     assertEquals(1, summary.getCriticalCount());
 
-    summary = m_dao.findCurrentCounts(m_clusterId.longValue(), "Service 0",
-        null);
-    assertEquals(1, summary.getOkCount());
-    assertEquals(0, summary.getWarningCount());
-    assertEquals(0, summary.getCriticalCount());
-    assertEquals(0, summary.getCriticalCount());
+    summary = m_dao.findCurrentCounts(m_cluster.getClusterId(), "YARN", null);
+    assertEquals(2, summary.getOkCount());
+    assertEquals(1, summary.getWarningCount());
+    assertEquals(1, summary.getCriticalCount());
+    assertEquals(1, summary.getCriticalCount());
 
-    summary = m_dao.findCurrentCounts(m_clusterId.longValue(), null, "h1");
+    summary = m_dao.findCurrentCounts(m_cluster.getClusterId(), null, "h1");
     assertEquals(2, summary.getOkCount());
     assertEquals(1, summary.getWarningCount());
     assertEquals(1, summary.getCriticalCount());
     assertEquals(1, summary.getCriticalCount());
 
-    summary = m_dao.findCurrentCounts(m_clusterId.longValue(), "foo", null);
+    summary = m_dao.findCurrentCounts(m_cluster.getClusterId(), "foo", null);
     assertEquals(0, summary.getOkCount());
     assertEquals(0, summary.getWarningCount());
     assertEquals(0, summary.getCriticalCount());
@@ -469,9 +472,9 @@ public class AlertsDAOTest {
     // definition
     AlertDefinitionEntity definition = new AlertDefinitionEntity();
     definition.setDefinitionName("many_per_cluster");
-    definition.setServiceName("ServiceName");
+    definition.setServiceName("YARN");
     definition.setComponentName(null);
-    definition.setClusterId(m_clusterId);
+    definition.setClusterId(m_cluster.getClusterId());
     definition.setHash(UUID.randomUUID().toString());
     definition.setScheduleInterval(Integer.valueOf(60));
     definition.setScope(Scope.SERVICE);
@@ -487,7 +490,7 @@ public class AlertsDAOTest {
     history.setAlertState(AlertState.OK);
     history.setAlertText("");
     history.setAlertTimestamp(Long.valueOf(1L));
-    history.setClusterId(m_clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setComponentName("");
     history.setHostName("h1");
     history.setServiceName("ServiceName");
@@ -506,7 +509,7 @@ public class AlertsDAOTest {
     history.setAlertState(AlertState.OK);
     history.setAlertText("");
     history.setAlertTimestamp(Long.valueOf(1L));
-    history.setClusterId(m_clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setComponentName("");
     history.setHostName("h2");
     history.setServiceName("ServiceName");
@@ -518,27 +521,27 @@ public class AlertsDAOTest {
     m_dao.merge(current);
 
     AlertSummaryDTO summary = m_dao.findAggregateCounts(
-        m_clusterId.longValue(), "many_per_cluster");
+        m_cluster.getClusterId(), "many_per_cluster");
     assertEquals(2, summary.getOkCount());
     assertEquals(0, summary.getWarningCount());
     assertEquals(0, summary.getCriticalCount());
     assertEquals(0, summary.getUnknownCount());
 
     AlertCurrentEntity c = m_dao.findCurrentByHostAndName(
-        m_clusterId.longValue(),
+        m_cluster.getClusterId(),
         "h2", "many_per_cluster");
     AlertHistoryEntity h = c.getAlertHistory();
     h.setAlertState(AlertState.CRITICAL);
     m_dao.merge(h);
 
-    summary = m_dao.findAggregateCounts(m_clusterId.longValue(),
+    summary = m_dao.findAggregateCounts(m_cluster.getClusterId(),
         "many_per_cluster");
     assertEquals(2, summary.getOkCount());
     assertEquals(0, summary.getWarningCount());
     assertEquals(1, summary.getCriticalCount());
     assertEquals(0, summary.getUnknownCount());
 
-    summary = m_dao.findAggregateCounts(m_clusterId.longValue(), "foo");
+    summary = m_dao.findAggregateCounts(m_cluster.getClusterId(), "foo");
     assertEquals(0, summary.getOkCount());
     assertEquals(0, summary.getWarningCount());
     assertEquals(0, summary.getCriticalCount());
@@ -601,7 +604,10 @@ public class AlertsDAOTest {
    */
   @Test
   public void testMaintenanceMode() throws Exception {
-    Cluster cluster = initializeNewCluster();
+    m_synchronizedBus.register(m_injector.getInstance(AlertMaintenanceModeListener.class));
+
+    m_helper.installHdfsService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
 
     List<AlertCurrentEntity> currents = m_dao.findCurrent();
     for (AlertCurrentEntity current : currents) {
@@ -613,7 +619,7 @@ public class AlertsDAOTest {
     namenode.setDefinitionName("NAMENODE");
     namenode.setServiceName("HDFS");
     namenode.setComponentName("NAMENODE");
-    namenode.setClusterId(cluster.getClusterId());
+    namenode.setClusterId(m_cluster.getClusterId());
     namenode.setHash(UUID.randomUUID().toString());
     namenode.setScheduleInterval(Integer.valueOf(60));
     namenode.setScope(Scope.ANY);
@@ -625,7 +631,7 @@ public class AlertsDAOTest {
     datanode.setDefinitionName("DATANODE");
     datanode.setServiceName("HDFS");
     datanode.setComponentName("DATANODE");
-    datanode.setClusterId(cluster.getClusterId());
+    datanode.setClusterId(m_cluster.getClusterId());
     datanode.setHash(UUID.randomUUID().toString());
     datanode.setScheduleInterval(Integer.valueOf(60));
     datanode.setScope(Scope.HOST);
@@ -637,7 +643,7 @@ public class AlertsDAOTest {
     aggregate.setDefinitionName("DATANODE_UP");
     aggregate.setServiceName("HDFS");
     aggregate.setComponentName(null);
-    aggregate.setClusterId(cluster.getClusterId());
+    aggregate.setClusterId(m_cluster.getClusterId());
     aggregate.setHash(UUID.randomUUID().toString());
     aggregate.setScheduleInterval(Integer.valueOf(60));
     aggregate.setScope(Scope.SERVICE);
@@ -650,7 +656,7 @@ public class AlertsDAOTest {
     nnHistory.setAlertState(AlertState.OK);
     nnHistory.setServiceName(namenode.getServiceName());
     nnHistory.setComponentName(namenode.getComponentName());
-    nnHistory.setClusterId(cluster.getClusterId());
+    nnHistory.setClusterId(m_cluster.getClusterId());
     nnHistory.setAlertDefinition(namenode);
     nnHistory.setAlertLabel(namenode.getDefinitionName());
     nnHistory.setAlertText(namenode.getDefinitionName());
@@ -670,7 +676,7 @@ public class AlertsDAOTest {
     dnHistory.setAlertState(AlertState.WARNING);
     dnHistory.setServiceName(datanode.getServiceName());
     dnHistory.setComponentName(datanode.getComponentName());
-    dnHistory.setClusterId(cluster.getClusterId());
+    dnHistory.setClusterId(m_cluster.getClusterId());
     dnHistory.setAlertDefinition(datanode);
     dnHistory.setAlertLabel(datanode.getDefinitionName());
     dnHistory.setAlertText(datanode.getDefinitionName());
@@ -690,7 +696,7 @@ public class AlertsDAOTest {
     aggregateHistory.setAlertState(AlertState.CRITICAL);
     aggregateHistory.setServiceName(aggregate.getServiceName());
     aggregateHistory.setComponentName(aggregate.getComponentName());
-    aggregateHistory.setClusterId(cluster.getClusterId());
+    aggregateHistory.setClusterId(m_cluster.getClusterId());
     aggregateHistory.setAlertDefinition(aggregate);
     aggregateHistory.setAlertLabel(aggregate.getDefinitionName());
     aggregateHistory.setAlertText(aggregate.getDefinitionName());
@@ -713,7 +719,7 @@ public class AlertsDAOTest {
     }
 
     // turn on HDFS MM
-    Service hdfs = m_clusters.getClusterById(cluster.getClusterId()).getService(
+    Service hdfs = m_clusters.getClusterById(m_cluster.getClusterId()).getService(
         "HDFS");
 
     hdfs.setMaintenanceState(MaintenanceState.ON);
@@ -735,7 +741,7 @@ public class AlertsDAOTest {
 
     // turn on host MM
     Host host = m_clusters.getHost(HOSTNAME);
-    host.setMaintenanceState(cluster.getClusterId(), MaintenanceState.ON);
+    host.setMaintenanceState(m_cluster.getClusterId(), MaintenanceState.ON);
 
     // only NAMENODE and DATANODE should be in MM; the aggregate should not
     // since the host is in MM
@@ -750,7 +756,7 @@ public class AlertsDAOTest {
     }
 
     // turn host MM off
-    host.setMaintenanceState(cluster.getClusterId(), MaintenanceState.OFF);
+    host.setMaintenanceState(m_cluster.getClusterId(), MaintenanceState.OFF);
 
     currents = m_dao.findCurrent();
     assertEquals(3, currents.size());
@@ -760,7 +766,7 @@ public class AlertsDAOTest {
 
     // turn a component MM on
     ServiceComponentHost nnComponent = null;
-    List<ServiceComponentHost> schs = cluster.getServiceComponentHosts(HOSTNAME);
+    List<ServiceComponentHost> schs = m_cluster.getServiceComponentHosts(HOSTNAME);
     for (ServiceComponentHost sch : schs) {
       if ("NAMENODE".equals(sch.getServiceComponentName())) {
         sch.setMaintenanceState(MaintenanceState.ON);
@@ -789,8 +795,9 @@ public class AlertsDAOTest {
    */
   @Test
   public void testAlertHistoryPredicate() throws Exception {
-    Cluster cluster = initializeNewCluster();
-    m_alertHelper.populateData(cluster);
+    m_helper.installHdfsService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+    m_alertHelper.populateData(m_cluster);
 
     Predicate clusterPredicate = null;
     Predicate hdfsPredicate = null;
@@ -881,8 +888,9 @@ public class AlertsDAOTest {
    */
   @Test
   public void testAlertHistoryPagination() throws Exception {
-    Cluster cluster = initializeNewCluster();
-    m_alertHelper.populateData(cluster);
+    m_helper.installHdfsService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+    m_alertHelper.populateData(m_cluster);
 
     AlertHistoryRequest request = new AlertHistoryRequest();
     request.Pagination = null;
@@ -920,8 +928,9 @@ public class AlertsDAOTest {
    */
   @Test
   public void testAlertHistorySorting() throws Exception {
-    Cluster cluster = initializeNewCluster();
-    m_alertHelper.populateData(cluster);
+    m_helper.installHdfsService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+    m_alertHelper.populateData(m_cluster);
 
     List<SortRequestProperty> sortProperties = new ArrayList<SortRequestProperty>();
     SortRequest sortRequest = new SortRequestImpl(sortProperties);
@@ -982,11 +991,14 @@ public class AlertsDAOTest {
     assertNotNull(currentAlerts);
     assertEquals(5, currentAlerts.size());
 
-    m_dao.removeCurrentByService("Service 1");
-    m_dao.removeCurrentByService("Service 2");
+    // assert none removed for HDFS
+    m_dao.removeCurrentByService("HDFS");
+    currentAlerts = m_dao.findCurrent();
+    assertEquals(5, currentAlerts.size());
 
+    m_dao.removeCurrentByService("YARN");
     currentAlerts = m_dao.findCurrent();
-    assertEquals(3, currentAlerts.size());
+    assertEquals(0, currentAlerts.size());
   }
 
   @Test
@@ -1013,7 +1025,7 @@ public class AlertsDAOTest {
     assertEquals(5, currentAlerts.size());
 
     AlertCurrentEntity entity = m_dao.findCurrentByHostAndName(
-        m_clusterId.longValue(), "h1", "Alert Definition 1");
+        m_cluster.getClusterId(), "h1", "Alert Definition 1");
 
     assertNotNull(entity);
 
@@ -1041,81 +1053,4 @@ public class AlertsDAOTest {
     currentAlerts = m_dao.findCurrent();
     assertEquals(4, currentAlerts.size());
   }
-
-  private Cluster initializeNewCluster() throws Exception {
-    String clusterName = "cluster-" + System.currentTimeMillis();
-    m_clusters.addCluster(clusterName);
-
-    Cluster cluster = m_clusters.getCluster(clusterName);
-    StackId stackId = new StackId("HDP", "2.0.6");
-    cluster.setDesiredStackVersion(stackId);
-    cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
-
-    addHost();
-    m_clusters.mapHostToCluster(HOSTNAME, cluster.getClusterName());
-
-    installHdfsService(cluster);
-    return cluster;
-  }
-
-  /**
-   * @throws Exception
-   */
-  private void addHost() throws Exception {
-    m_clusters.addHost(HOSTNAME);
-
-    Host host = m_clusters.getHost(HOSTNAME);
-    Map<String, String> hostAttributes = new HashMap<String, String>();
-    hostAttributes.put("os_family", "redhat");
-    hostAttributes.put("os_release_version", "6.4");
-    host.setHostAttributes(hostAttributes);
-    host.setState(HostState.HEALTHY);
-    host.persist();
-  }
-
-  /**
-   * Calls {@link Service#persist()} to mock a service install along with
-   * creating a single {@link Host} and {@link ServiceComponentHost}.
-   */
-  private void installHdfsService(Cluster cluster) throws Exception {
-    String serviceName = "HDFS";
-    Service service = m_serviceFactory.createNew(cluster, serviceName);
-    cluster.addService(service);
-    service.persist();
-    service = cluster.getService(serviceName);
-    Assert.assertNotNull(service);
-
-    ServiceComponent datanode = m_componentFactory.createNew(service,
-        "DATANODE");
-
-    service.addServiceComponent(datanode);
-    datanode.setDesiredState(State.INSTALLED);
-    datanode.persist();
-
-    ServiceComponentHost sch = m_schFactory.createNew(datanode, HOSTNAME);
-
-    datanode.addServiceComponentHost(sch);
-    sch.setDesiredState(State.INSTALLED);
-    sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
-    sch.setStackVersion(new StackId("HDP-2.0.6"));
-
-    sch.persist();
-
-    ServiceComponent namenode = m_componentFactory.createNew(service,
-        "NAMENODE");
-
-    service.addServiceComponent(namenode);
-    namenode.setDesiredState(State.INSTALLED);
-    namenode.persist();
-
-    sch = m_schFactory.createNew(namenode, HOSTNAME);
-    namenode.addServiceComponentHost(sch);
-    sch.setDesiredState(State.INSTALLED);
-    sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
-    sch.setStackVersion(new StackId("HDP-2.0.6"));
-
-    sch.persist();
-  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
index 8a162fd..0f98a3d 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
@@ -19,10 +19,7 @@ package org.apache.ambari.server.state.alerts;
 
 import static org.junit.Assert.assertEquals;
 
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 
 import org.apache.ambari.server.events.AlertReceivedEvent;
@@ -38,23 +35,18 @@ import org.apache.ambari.server.state.Alert;
 import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Host;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.ServiceComponentHost;
+import org.apache.ambari.server.state.ServiceComponentFactory;
+import org.apache.ambari.server.state.ServiceComponentHostFactory;
+import org.apache.ambari.server.state.ServiceFactory;
 import org.apache.ambari.server.state.alert.Scope;
 import org.apache.ambari.server.state.alert.SourceType;
-import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.Module;
 import com.google.inject.persist.PersistService;
-import com.google.inject.util.Modules;
-
 
 /**
  * Tests the {@link AlertReceivedListener}.
@@ -62,76 +54,66 @@ import com.google.inject.util.Modules;
 public class AlertReceivedListenerTest {
 
   private static final String ALERT_DEFINITION = "alert_definition_";
-  private static final String CLUSTER_NAME = "c1";
-  private static final String SERVICE = "Service";
-  private static final String COMPONENT = "Component";
   private static final String HOST1 = "h1";
   private static final String ALERT_LABEL = "My Label";
+  private Injector m_injector;
+  private AlertsDAO m_dao;
+  private AlertDefinitionDAO m_definitionDao;
 
-  private Long clusterId;
-  private Injector injector;
-  private OrmTestHelper helper;
-  private AlertsDAO dao;
-  private AlertDefinitionDAO definitionDao;
+  private Clusters m_clusters;
+  private Cluster m_cluster;
 
-  private Clusters clusters;
-  private Cluster cluster;
+  private OrmTestHelper m_helper;
+  private ServiceFactory m_serviceFactory;
+  private ServiceComponentFactory m_componentFactory;
+  private ServiceComponentHostFactory m_schFactory;
 
   @Before
   public void setup() throws Exception {
-    clusters = EasyMock.createNiceMock(Clusters.class);
-    cluster = EasyMock.createNiceMock(Cluster.class);
-
-    injector = Guice.createInjector(Modules.override(
-        new InMemoryDefaultTestModule()).with(new MockModule()));
-
-    injector.getInstance(GuiceJpaInitializer.class);
-    helper = injector.getInstance(OrmTestHelper.class);
-    clusterId = helper.createCluster();
-    dao = injector.getInstance(AlertsDAO.class);
-    definitionDao = injector.getInstance(AlertDefinitionDAO.class);
+    m_injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    m_injector.getInstance(GuiceJpaInitializer.class);
 
-    List<Host> hosts = new ArrayList<Host>();
-    Host host = EasyMock.createNiceMock(Host.class);
-    EasyMock.expect(host.getHostName()).andReturn(HOST1).anyTimes();
-    hosts.add(host);
+    m_helper = m_injector.getInstance(OrmTestHelper.class);
+    m_clusters = m_injector.getInstance(Clusters.class);
+    m_serviceFactory = m_injector.getInstance(ServiceFactory.class);
+    m_componentFactory = m_injector.getInstance(ServiceComponentFactory.class);
+    m_schFactory = m_injector.getInstance(ServiceComponentHostFactory.class);
 
-    Map<String,Service> services = new HashMap<String, Service>();
-    services.put("Service 1", EasyMock.createNiceMock(Service.class));
+    // install YARN so there is at least 1 service installed and no
+    // unexpected alerts since the test YARN service doesn't have any alerts
+    m_cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOST1);
 
-    List<ServiceComponentHost> schs = new ArrayList<ServiceComponentHost>();
-    ServiceComponentHost sch = EasyMock.createNiceMock(ServiceComponentHost.class);
-    EasyMock.expect(sch.getServiceComponentName()).andReturn("Component 1").anyTimes();
-    schs.add(sch);
+    m_dao = m_injector.getInstance(AlertsDAO.class);
+    m_definitionDao = m_injector.getInstance(AlertDefinitionDAO.class);
 
-    // setup isValid expectations
-    EasyMock.expect(clusters.getCluster(CLUSTER_NAME)).andReturn(cluster).anyTimes();
-    EasyMock.expect(clusters.getHosts()).andReturn(hosts).anyTimes();
-    EasyMock.expect(cluster.getServices()).andReturn(services).anyTimes();
-    EasyMock.expect(cluster.getServiceComponentHosts(HOST1)).andReturn(schs).anyTimes();
-
-    EasyMock.replay(clusters, cluster, sch, host);
-
-    // create 5 definitions
+    // create 5 definitions, some with HDFS and some with YARN
     for (int i = 0; i < 5; i++) {
+      String serviceName = "HDFS";
+      String componentName = "DATANODE";
+      if (i >= 3) {
+        serviceName = "YARN";
+        componentName = "RESOURCEMANAGER";
+      }
+
       AlertDefinitionEntity definition = new AlertDefinitionEntity();
       definition.setDefinitionName(ALERT_DEFINITION + i);
-      definition.setServiceName(SERVICE + " " + i);
-      definition.setComponentName(COMPONENT + " " + i);
-      definition.setClusterId(clusterId);
+      definition.setServiceName(serviceName);
+      definition.setComponentName(componentName);
+      definition.setClusterId(m_cluster.getClusterId());
       definition.setHash(UUID.randomUUID().toString());
       definition.setScheduleInterval(Integer.valueOf(60));
       definition.setScope(Scope.SERVICE);
       definition.setSource("{\"type\" : \"SCRIPT\"}");
       definition.setSourceType(SourceType.SCRIPT);
-      definitionDao.create(definition);
+      m_definitionDao.create(definition);
     }
   }
 
   @After
   public void teardown() {
-    injector.getInstance(PersistService.class).stop();
-    injector = null;
+    m_injector.getInstance(PersistService.class).stop();
+    m_injector = null;
   }
 
   /**
@@ -140,38 +122,39 @@ public class AlertReceivedListenerTest {
   @Test
   public void testDisabledAlert() {
     String definitionName = ALERT_DEFINITION + "1";
-    String serviceName = "Service 1";
-    String componentName = "Component 1";
+    String componentName = "DATANODE";
 
-    Alert alert1 = new Alert(definitionName, null, serviceName, componentName,
+    Alert alert1 = new Alert(definitionName, null, "HDFS", componentName,
         HOST1, AlertState.OK);
 
-    alert1.setCluster(CLUSTER_NAME);
+    alert1.setCluster(m_cluster.getClusterName());
     alert1.setLabel(ALERT_LABEL);
-    alert1.setText(serviceName + " " + componentName + " is OK");
+    alert1.setText("HDFS " + componentName + " is OK");
     alert1.setTimestamp(1L);
 
     // verify that the listener works with a regular alert
-    AlertReceivedListener listener = injector.getInstance(AlertReceivedListener.class);
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId, alert1);
+    AlertReceivedListener listener = m_injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(), alert1);
     listener.onAlertEvent(event1);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrent();
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
     assertEquals(1, allCurrent.size());
 
     // disable definition
-    AlertDefinitionEntity definition = definitionDao.findByName(clusterId, definitionName);
+    AlertDefinitionEntity definition = m_definitionDao.findByName(
+        m_cluster.getClusterId(), definitionName);
     definition.setEnabled(false);
-    definitionDao.merge(definition);
+    m_definitionDao.merge(definition);
 
     // remove disabled
-    dao.removeCurrentDisabledAlerts();
-    allCurrent = dao.findCurrent();
+    m_dao.removeCurrentDisabledAlerts();
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
 
     // verify no new alerts for disabled
     listener.onAlertEvent(event1);
-    allCurrent = dao.findCurrent();
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
   }
 
@@ -181,36 +164,36 @@ public class AlertReceivedListenerTest {
   @Test
   public void testInvalidHost() {
     String definitionName = ALERT_DEFINITION + "1";
-    String serviceName = "Service 1";
-    String componentName = "Component 1";
+    String componentName = "DATANODE";
 
-    Alert alert1 = new Alert(definitionName, null, serviceName, componentName,
+    Alert alert1 = new Alert(definitionName, null, "HDFS", componentName,
         HOST1, AlertState.OK);
 
-    alert1.setCluster(CLUSTER_NAME);
+    alert1.setCluster(m_cluster.getClusterName());
     alert1.setLabel(ALERT_LABEL);
-    alert1.setText(serviceName + " " + componentName + " is OK");
+    alert1.setText("HDFS " + componentName + " is OK");
     alert1.setTimestamp(1L);
 
     // verify that the listener works with a regular alert
-    AlertReceivedListener listener = injector.getInstance(AlertReceivedListener.class);
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId, alert1);
+    AlertReceivedListener listener = m_injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(), alert1);
     listener.onAlertEvent(event1);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrent();
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
     assertEquals(1, allCurrent.size());
 
     // invalid host
     alert1.setHost("INVALID");
 
     // remove all
-    dao.removeCurrentByHost(HOST1);
-    allCurrent = dao.findCurrent();
+    m_dao.removeCurrentByHost(HOST1);
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
 
     // verify no new alerts for disabled
     listener.onAlertEvent(event1);
-    allCurrent = dao.findCurrent();
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
   }
 
@@ -219,22 +202,22 @@ public class AlertReceivedListenerTest {
    */
   @Test
   public void testInvalidAlertDefinition() {
-    String serviceName = "Service 1";
-    String componentName = "Component 1";
+    String componentName = "DATANODE";
 
-    Alert alert1 = new Alert("missing_alert_definition_name", null,
-        serviceName, componentName, HOST1, AlertState.OK);
+    Alert alert1 = new Alert("missing_alert_definition_name", null, "HDFS",
+        componentName, HOST1, AlertState.OK);
 
     alert1.setLabel(ALERT_LABEL);
-    alert1.setText(serviceName + " " + componentName + " is OK");
+    alert1.setText("HDFS " + componentName + " is OK");
     alert1.setTimestamp(1L);
 
     // bad alert definition name means no current alerts
-    AlertReceivedListener listener = injector.getInstance(AlertReceivedListener.class);
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId, alert1);
+    AlertReceivedListener listener = m_injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(), alert1);
     listener.onAlertEvent(event1);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrent();
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
   }
 
@@ -244,47 +227,36 @@ public class AlertReceivedListenerTest {
   @Test
   public void testInvalidServiceComponentHost() {
     String definitionName = ALERT_DEFINITION + "1";
-    String serviceName = "Service 1";
-    String componentName = "Component 1";
+    String componentName = "DATANODE";
 
-    Alert alert1 = new Alert(definitionName, null, serviceName, componentName,
+    Alert alert1 = new Alert(definitionName, null, "HDFS", componentName,
         HOST1, AlertState.OK);
 
-    alert1.setCluster(CLUSTER_NAME);
+    alert1.setCluster(m_cluster.getClusterName());
     alert1.setLabel(ALERT_LABEL);
-    alert1.setText(serviceName + " " + componentName + " is OK");
+    alert1.setText("HDFS " + componentName + " is OK");
     alert1.setTimestamp(1L);
 
     // verify that the listener works with a regular alert
-    AlertReceivedListener listener = injector.getInstance(AlertReceivedListener.class);
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId, alert1);
+    AlertReceivedListener listener = m_injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(), alert1);
     listener.onAlertEvent(event1);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrent();
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
     assertEquals(1, allCurrent.size());
 
     // invalid host
     alert1.setComponent("INVALID");
 
     // remove all
-    dao.removeCurrentByHost(HOST1);
-    allCurrent = dao.findCurrent();
+    m_dao.removeCurrentByHost(HOST1);
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
 
     // verify no new alerts for disabled
     listener.onAlertEvent(event1);
-    allCurrent = dao.findCurrent();
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
   }
-
-  /**
-   *
-   */
-  private class MockModule implements Module {
-    @Override
-    public void configure(Binder binder) {
-      binder.bind(Clusters.class).toInstance(clusters);
-      binder.bind(Cluster.class).toInstance(cluster);
-    }
-  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
index 2be5b47..023d0ad 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
@@ -50,7 +50,12 @@ import org.apache.ambari.server.orm.entities.AlertNoticeEntity;
 import org.apache.ambari.server.orm.entities.AlertTargetEntity;
 import org.apache.ambari.server.state.Alert;
 import org.apache.ambari.server.state.AlertState;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.MaintenanceState;
+import org.apache.ambari.server.state.ServiceComponentFactory;
+import org.apache.ambari.server.state.ServiceComponentHostFactory;
+import org.apache.ambari.server.state.ServiceFactory;
 import org.apache.ambari.server.state.alert.AggregateDefinitionMapping;
 import org.apache.ambari.server.state.alert.AggregateSource;
 import org.apache.ambari.server.state.alert.AlertDefinition;
@@ -78,80 +83,101 @@ import com.google.inject.persist.PersistService;
 public class AlertDataManagerTest {
 
   private static final String ALERT_DEFINITION = "Alert Definition 1";
-  private static final String SERVICE = "service1";
+  private static final String SERVICE = "HDFS";
   private static final String COMPONENT = "component1";
   private static final String HOST1 = "h1";
   private static final String HOST2 = "h2";
   private static final String ALERT_LABEL = "My Label";
 
-  private Long clusterId;
-  private Injector injector;
-  private OrmTestHelper helper;
-  private AlertsDAO dao;
-  private AlertDispatchDAO dispatchDao;
-  private AlertDefinitionDAO definitionDao;
+  private Injector m_injector;
+  private OrmTestHelper m_helper;
+  private Clusters m_clusters;
+  private Cluster m_cluster;
+  private AlertsDAO m_dao;
+  private AlertDispatchDAO m_dispatchDao;
+  private AlertDefinitionDAO m_definitionDao;
+
+  private ServiceFactory m_serviceFactory;
+  private ServiceComponentFactory m_componentFactory;
+  private ServiceComponentHostFactory m_schFactory;
 
   @Before
   public void setup() throws Exception {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-    helper = injector.getInstance(OrmTestHelper.class);
-    clusterId = helper.createCluster();
-    dao = injector.getInstance(AlertsDAO.class);
-    dispatchDao = injector.getInstance(AlertDispatchDAO.class);
-    definitionDao = injector.getInstance(AlertDefinitionDAO.class);
+    m_injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    m_injector.getInstance(GuiceJpaInitializer.class);
+    m_helper = m_injector.getInstance(OrmTestHelper.class);
+    m_dao = m_injector.getInstance(AlertsDAO.class);
+    m_dispatchDao = m_injector.getInstance(AlertDispatchDAO.class);
+    m_definitionDao = m_injector.getInstance(AlertDefinitionDAO.class);
+    m_clusters = m_injector.getInstance(Clusters.class);
+    m_serviceFactory = m_injector.getInstance(ServiceFactory.class);
+    m_componentFactory = m_injector.getInstance(ServiceComponentFactory.class);
+    m_schFactory = m_injector.getInstance(ServiceComponentHostFactory.class);
+
+    // install YARN so there is at least 1 service installed and no
+    // unexpected alerts since the test YARN service doesn't have any alerts
+    m_cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOST1);
+
+    m_helper.addHost(m_clusters, m_cluster, HOST2);
 
     // create 5 definitions
     for (int i = 0; i < 5; i++) {
       AlertDefinitionEntity definition = new AlertDefinitionEntity();
       definition.setDefinitionName("Alert Definition " + i);
-      definition.setServiceName("Service " + i);
-      definition.setComponentName(null);
-      definition.setClusterId(clusterId);
+      definition.setServiceName(SERVICE);
+      definition.setComponentName(COMPONENT);
+      definition.setClusterId(m_cluster.getClusterId());
       definition.setHash(UUID.randomUUID().toString());
       definition.setScheduleInterval(Integer.valueOf(60));
       definition.setScope(Scope.SERVICE);
       definition.setSource("{\"type\" : \"SCRIPT\"}");
       definition.setSourceType(SourceType.SCRIPT);
-      definitionDao.create(definition);
+      m_definitionDao.create(definition);
     }
   }
 
   @After
   public void teardown() {
-    injector.getInstance(PersistService.class).stop();
-    injector = null;
+    m_injector.getInstance(PersistService.class).stop();
+    m_injector = null;
   }
 
   @Test
   public void testAlertRecords() {
-    Alert alert1 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1, AlertState.OK);
+    Alert alert1 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
+        AlertState.OK);
     alert1.setLabel(ALERT_LABEL);
     alert1.setText("Component component1 is OK");
     alert1.setTimestamp(1L);
 
-    Alert alert2 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST2, AlertState.CRITICAL);
+    Alert alert2 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST2,
+        AlertState.CRITICAL);
     alert2.setLabel(ALERT_LABEL);
     alert2.setText("Component component2 is not OK");
 
-    AlertReceivedListener listener = injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedListener listener = m_injector.getInstance(AlertReceivedListener.class);
 
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId.longValue(),
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(),
         alert1);
 
-    AlertReceivedEvent event2 = new AlertReceivedEvent(clusterId.longValue(),
+    AlertReceivedEvent event2 = new AlertReceivedEvent(
+        m_cluster.getClusterId(),
         alert2);
 
     listener.onAlertEvent(event1);
     listener.onAlertEvent(event2);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrentByService(clusterId.longValue(), SERVICE);
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrentByService(
+        m_cluster.getClusterId(), SERVICE);
     assertEquals(2, allCurrent.size());
 
-    List<AlertHistoryEntity> allHistory = dao.findAll(clusterId.longValue());
+    List<AlertHistoryEntity> allHistory = m_dao.findAll(m_cluster.getClusterId());
     assertEquals(2, allHistory.size());
 
-    AlertCurrentEntity current = dao.findCurrentByHostAndName(clusterId.longValue(), HOST1, ALERT_DEFINITION);
+    AlertCurrentEntity current = m_dao.findCurrentByHostAndName(
+        m_cluster.getClusterId(), HOST1, ALERT_DEFINITION);
     assertNotNull(current);
     assertEquals(HOST1, current.getAlertHistory().getHostName());
     assertEquals(ALERT_DEFINITION, current.getAlertHistory().getAlertDefinition().getDefinitionName());
@@ -165,17 +191,20 @@ public class AlertDataManagerTest {
     Long historyId = current.getAlertHistory().getAlertId();
 
     // no new history since the state is the same
-    Alert alert3 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1, AlertState.OK);
+    Alert alert3 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
+        AlertState.OK);
     alert3.setLabel(ALERT_LABEL);
     alert3.setText("Component component1 is OK");
     alert3.setTimestamp(2L);
 
-    AlertReceivedEvent event3 = new AlertReceivedEvent(clusterId.longValue(),
+    AlertReceivedEvent event3 = new AlertReceivedEvent(
+        m_cluster.getClusterId(),
         alert3);
 
     listener.onAlertEvent(event3);
 
-    current = dao.findCurrentByHostAndName(clusterId.longValue(), HOST1, ALERT_DEFINITION);
+    current = m_dao.findCurrentByHostAndName(m_cluster.getClusterId(), HOST1,
+        ALERT_DEFINITION);
     assertNotNull(current);
     assertEquals(currentId, current.getAlertId());
     assertEquals(historyId, current.getAlertHistory().getAlertId());
@@ -187,24 +216,27 @@ public class AlertDataManagerTest {
     assertEquals(1L, current.getOriginalTimestamp().longValue());
     assertEquals(2L, current.getLatestTimestamp().longValue());
 
-    allCurrent = dao.findCurrentByService(clusterId.longValue(), SERVICE);
+    allCurrent = m_dao.findCurrentByService(m_cluster.getClusterId(), SERVICE);
     assertEquals(2, allCurrent.size());
 
-    allHistory = dao.findAll(clusterId.longValue());
+    allHistory = m_dao.findAll(m_cluster.getClusterId());
     assertEquals(2, allHistory.size());
 
     // change to warning
-    Alert alert4 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1, AlertState.WARNING);
+    Alert alert4 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
+        AlertState.WARNING);
     alert4.setLabel(ALERT_LABEL);
     alert4.setText("Component component1 is about to go down");
     alert4.setTimestamp(3L);
 
-    AlertReceivedEvent event4 = new AlertReceivedEvent(clusterId.longValue(),
+    AlertReceivedEvent event4 = new AlertReceivedEvent(
+        m_cluster.getClusterId(),
         alert4);
 
     listener.onAlertEvent(event4);
 
-    current = dao.findCurrentByHostAndName(clusterId.longValue(), HOST1, ALERT_DEFINITION);
+    current = m_dao.findCurrentByHostAndName(m_cluster.getClusterId(), HOST1,
+        ALERT_DEFINITION);
     assertNotNull(current);
     assertEquals(current.getAlertId(), currentId);
     assertFalse(historyId.equals(current.getAlertHistory().getAlertId()));
@@ -216,10 +248,10 @@ public class AlertDataManagerTest {
     assertEquals(3L, current.getOriginalTimestamp().longValue());
     assertEquals(3L, current.getLatestTimestamp().longValue());
 
-    allCurrent = dao.findCurrentByService(clusterId.longValue(), SERVICE);
+    allCurrent = m_dao.findCurrentByService(m_cluster.getClusterId(), SERVICE);
     assertEquals(2, allCurrent.size());
 
-    allHistory = dao.findAll(clusterId.longValue());
+    allHistory = m_dao.findAll(m_cluster.getClusterId());
     assertEquals(3, allHistory.size());
   }
 
@@ -231,24 +263,24 @@ public class AlertDataManagerTest {
    */
   @Test
   public void testAlertNotices() throws Exception {
-    List<AlertNoticeEntity> notices = dispatchDao.findAllNotices();
+    List<AlertNoticeEntity> notices = m_dispatchDao.findAllNotices();
     assertEquals( 0, notices.size() );
 
-    List<AlertDefinitionEntity> definitions = definitionDao.findAll(clusterId);
+    List<AlertDefinitionEntity> definitions = m_definitionDao.findAll(m_cluster.getClusterId());
     AlertDefinitionEntity definition = definitions.get(0);
 
     AlertHistoryEntity history = new AlertHistoryEntity();
     history.setServiceName(definition.getServiceName());
-    history.setClusterId(clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setAlertDefinition(definition);
     history.setAlertLabel(definition.getDefinitionName());
     history.setAlertText(definition.getDefinitionName());
     history.setAlertTimestamp(System.currentTimeMillis());
     history.setHostName(HOST1);
     history.setAlertState(AlertState.OK);
-    dao.create(history);
+    m_dao.create(history);
 
-    List<AlertHistoryEntity> histories = dao.findAll(clusterId);
+    List<AlertHistoryEntity> histories = m_dao.findAll(m_cluster.getClusterId());
     assertEquals(1, histories.size());
 
     AlertCurrentEntity currentAlert = new AlertCurrentEntity();
@@ -256,26 +288,28 @@ public class AlertDataManagerTest {
     currentAlert.setMaintenanceState(MaintenanceState.OFF);
     currentAlert.setOriginalTimestamp(System.currentTimeMillis());
     currentAlert.setLatestTimestamp(System.currentTimeMillis());
-    dao.create(currentAlert);
+    m_dao.create(currentAlert);
 
-    AlertTargetEntity target = helper.createAlertTarget();
+    AlertTargetEntity target = m_helper.createAlertTarget();
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
     targets.add(target);
 
-    AlertGroupEntity group = helper.createAlertGroup(clusterId, targets);
+    AlertGroupEntity group = m_helper.createAlertGroup(
+        m_cluster.getClusterId(), targets);
     group.addAlertDefinition( definitions.get(0) );
-    dispatchDao.merge(group);
+    m_dispatchDao.merge(group);
 
     Alert alert1 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
         AlertState.OK);
 
-    AlertStateChangeEvent event = new AlertStateChangeEvent(clusterId, alert1,
+    AlertStateChangeEvent event = new AlertStateChangeEvent(
+        m_cluster.getClusterId(), alert1,
         currentAlert, AlertState.CRITICAL);
 
-    AlertStateChangedListener listener = injector.getInstance(AlertStateChangedListener.class);
+    AlertStateChangedListener listener = m_injector.getInstance(AlertStateChangedListener.class);
     listener.onAlertEvent(event);
 
-    notices = dispatchDao.findAllNotices();
+    notices = m_dispatchDao.findAllNotices();
     assertEquals(1, notices.size());
   }
 
@@ -286,22 +320,22 @@ public class AlertDataManagerTest {
     definition.setDefinitionName("to_aggregate");
     definition.setLabel("My Label");
     definition.setLabel("My Description");
-    definition.setServiceName("SERVICE");
+    definition.setServiceName(SERVICE);
     definition.setComponentName(null);
-    definition.setClusterId(clusterId);
+    definition.setClusterId(m_cluster.getClusterId());
     definition.setHash(UUID.randomUUID().toString());
     definition.setScheduleInterval(Integer.valueOf(60));
     definition.setScope(Scope.HOST);
     definition.setSource("{\"type\" : \"SCRIPT\"}");
     definition.setSourceType(SourceType.SCRIPT);
-    definitionDao.create(definition);
+    m_definitionDao.create(definition);
 
     // create aggregate of definition
     AlertDefinitionEntity aggDef = new AlertDefinitionEntity();
     aggDef.setDefinitionName("aggregate_test");
-    aggDef.setServiceName("SERVICE");
+    aggDef.setServiceName(SERVICE);
     aggDef.setComponentName(null);
-    aggDef.setClusterId(clusterId);
+    aggDef.setClusterId(m_cluster.getClusterId());
     aggDef.setHash(UUID.randomUUID().toString());
     aggDef.setScheduleInterval(Integer.valueOf(60));
     aggDef.setScope(Scope.SERVICE);
@@ -336,7 +370,7 @@ public class AlertDataManagerTest {
 
     aggDef.setSource(gson.toJson(source));
     aggDef.setSourceType(SourceType.AGGREGATE);
-    definitionDao.create(aggDef);
+    m_definitionDao.create(aggDef);
 
     // add current and history across four hosts
     for (int i = 0; i < 4; i++) {
@@ -347,21 +381,21 @@ public class AlertDataManagerTest {
       history.setAlertState(AlertState.OK);
       history.setAlertText("OK");
       history.setAlertTimestamp(Long.valueOf(1));
-      history.setClusterId(clusterId);
+      history.setClusterId(m_cluster.getClusterId());
       history.setComponentName(definition.getComponentName());
       history.setHostName("h" + (i+1));
       history.setServiceName(definition.getServiceName());
-      dao.create(history);
+      m_dao.create(history);
 
       AlertCurrentEntity current = new AlertCurrentEntity();
       current.setAlertHistory(history);
       current.setLatestText(history.getAlertText());
       current.setLatestTimestamp(Long.valueOf(1L));
       current.setOriginalTimestamp(Long.valueOf(1L));
-      dao.merge(current);
+      m_dao.merge(current);
     }
 
-    AlertEventPublisher publisher = injector.getInstance(AlertEventPublisher.class);
+    AlertEventPublisher publisher = m_injector.getInstance(AlertEventPublisher.class);
 
     // !!! need a synchronous op for testing
     field = AlertEventPublisher.class.getDeclaredField("m_eventBus");
@@ -377,17 +411,17 @@ public class AlertDataManagerTest {
       }
     });
 
-    AlertAggregateListener listener = injector.getInstance(AlertAggregateListener.class);
+    AlertAggregateListener listener = m_injector.getInstance(AlertAggregateListener.class);
     AlertDefinitionFactory factory = new AlertDefinitionFactory();
-    AggregateDefinitionMapping aggregateMapping = injector.getInstance(AggregateDefinitionMapping.class);
+    AggregateDefinitionMapping aggregateMapping = m_injector.getInstance(AggregateDefinitionMapping.class);
 
     AlertDefinition aggregateDefinition = factory.coerce(aggDef);
-    aggregateMapping.registerAggregate(clusterId.longValue(),
+    aggregateMapping.registerAggregate(m_cluster.getClusterId(),
         aggregateDefinition );
 
     AggregateSource as = (AggregateSource) aggregateDefinition.getSource();
     AlertDefinition aggregatedDefinition = aggregateMapping.getAggregateDefinition(
-        clusterId.longValue(), as.getAlertName());
+        m_cluster.getClusterId(), as.getAlertName());
 
     assertNotNull(aggregatedDefinition);
 
@@ -400,7 +434,8 @@ public class AlertDataManagerTest {
         definition.getComponentName(),
         "h1",
         AlertState.OK);
-    AlertReceivedEvent event = new AlertReceivedEvent(clusterId.longValue(), alert);
+    AlertReceivedEvent event = new AlertReceivedEvent(m_cluster.getClusterId(),
+        alert);
 
     listener.onAlertEvent(event);
     assertNotNull(ref.get());
@@ -408,10 +443,10 @@ public class AlertDataManagerTest {
     assertTrue(ref.get().getText().indexOf("0/4") > -1);
 
     // check if one is critical, still ok
-    AlertCurrentEntity current = dao.findCurrentByHostAndName(
-        clusterId.longValue(), "h1", definition.getDefinitionName());
+    AlertCurrentEntity current = m_dao.findCurrentByHostAndName(
+        m_cluster.getClusterId(), "h1", definition.getDefinitionName());
     current.getAlertHistory().setAlertState(AlertState.CRITICAL);
-    dao.merge(current.getAlertHistory());
+    m_dao.merge(current.getAlertHistory());
 
     listener.onAlertEvent(event);
     assertEquals("aggregate_test", ref.get().getName());
@@ -419,10 +454,11 @@ public class AlertDataManagerTest {
     assertTrue(ref.get().getText().indexOf("1/4") > -1);
 
     // two are either warning or critical, warning
-    current = dao.findCurrentByHostAndName(
-        clusterId.longValue(), "h2", definition.getDefinitionName());
+    current = m_dao.findCurrentByHostAndName(
+m_cluster.getClusterId(), "h2",
+        definition.getDefinitionName());
     current.getAlertHistory().setAlertState(AlertState.WARNING);
-    dao.merge(current.getAlertHistory());
+    m_dao.merge(current.getAlertHistory());
 
     listener.onAlertEvent(event);
     assertEquals("aggregate_test", ref.get().getName());
@@ -430,10 +466,11 @@ public class AlertDataManagerTest {
     assertTrue(ref.get().getText().indexOf("2/4") > -1);
 
     // three make it critical
-    current = dao.findCurrentByHostAndName(
-        clusterId.longValue(), "h3", definition.getDefinitionName());
+    current = m_dao.findCurrentByHostAndName(
+m_cluster.getClusterId(), "h3",
+        definition.getDefinitionName());
     current.getAlertHistory().setAlertState(AlertState.CRITICAL);
-    dao.merge(current.getAlertHistory());
+    m_dao.merge(current.getAlertHistory());
 
     listener.onAlertEvent(event);
     assertEquals("aggregate_test", ref.get().getName());


[2/2] ambari git commit: AMBARI-8460 - Alerts: AlertDefinition and AlertGroup Automatic Creation On Startup (jonathanhurley)

Posted by jo...@apache.org.
AMBARI-8460 - Alerts: AlertDefinition and AlertGroup Automatic Creation On Startup (jonathanhurley)


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

Branch: refs/heads/trunk
Commit: 9159421b39c4ef677a0bdc9445afe2ca2bd545ee
Parents: 378cf13
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Wed Nov 26 15:32:04 2014 -0500
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Fri Nov 28 08:04:39 2014 -0500

----------------------------------------------------------------------
 .../ambari/server/events/AmbariEvent.java       |   5 +
 .../ambari/server/events/HostAddedEvent.java    |  51 ++++
 .../server/events/HostRegisteredEvent.java      |  44 +++
 .../ambari/server/events/HostRemovedEvent.java  |   2 +-
 .../alerts/AlertHashInvalidationListener.java   |   3 +-
 .../listeners/alerts/AlertHostListener.java     | 119 +++++++-
 .../alerts/AlertServiceStateListener.java       |  12 +-
 .../server/orm/dao/AlertDefinitionDAO.java      |  31 ++-
 .../ambari/server/orm/dao/AlertDispatchDAO.java |  77 +++++-
 .../apache/ambari/server/orm/dao/AlertsDAO.java |   3 +-
 .../server/orm/dao/ClusterServiceDAO.java       |  21 +-
 .../dao/ServiceComponentDesiredStateDAO.java    |  21 +-
 .../orm/entities/AlertDefinitionEntity.java     |  18 ++
 .../server/orm/entities/AlertGroupEntity.java   |  17 +-
 .../server/orm/entities/AlertHistoryEntity.java |  17 ++
 .../server/orm/entities/AlertTargetEntity.java  |  13 +
 .../server/state/alert/AlertDefinitionHash.java |  66 +++--
 .../server/state/cluster/ClustersImpl.java      |  15 +-
 .../apache/ambari/server/orm/OrmTestHelper.java | 146 +++++++++-
 .../server/orm/dao/AlertDispatchDAOTest.java    | 276 ++++++++++++-------
 .../ambari/server/orm/dao/AlertsDAOTest.java    | 259 +++++++----------
 .../state/alerts/AlertReceivedListenerTest.java | 194 ++++++-------
 .../state/cluster/AlertDataManagerTest.java     | 185 ++++++++-----
 23 files changed, 1043 insertions(+), 552 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/events/AmbariEvent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/AmbariEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/events/AmbariEvent.java
index de9b6dc..e708473 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/AmbariEvent.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/AmbariEvent.java
@@ -68,6 +68,11 @@ public abstract class AmbariEvent {
     ALERT_DEFINITION_DISABLED,
 
     /**
+     * A host was registered with the server.
+     */
+    HOST_REGISTERED,
+
+    /**
      * A host was added to the cluster.
      */
     HOST_ADDED,

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/events/HostAddedEvent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/HostAddedEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/events/HostAddedEvent.java
new file mode 100644
index 0000000..7832c16
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/HostAddedEvent.java
@@ -0,0 +1,51 @@
+/**
+ * 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.events;
+
+/**
+ * The {@link HostAddedEvent} is fired when a host is added to a cluster.
+ */
+public class HostAddedEvent extends ClusterEvent {
+
+  /**
+   * The host's name.
+   */
+  protected final String m_hostName;
+
+  /**
+   * Constructor.
+   *
+   * @param clusterId
+   *          the ID of the cluster.
+   * @param hostName
+   *          the name of the host.
+   */
+  public HostAddedEvent(long clusterId, String hostName) {
+    super(AmbariEventType.HOST_ADDED, clusterId);
+    m_hostName = hostName;
+  }
+
+  /**
+   * Gets the host's name that the event belongs to.
+   *
+   * @return the hostName
+   */
+  public String getHostName() {
+    return m_hostName;
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/events/HostRegisteredEvent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/HostRegisteredEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/events/HostRegisteredEvent.java
new file mode 100644
index 0000000..07ff526
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/HostRegisteredEvent.java
@@ -0,0 +1,44 @@
+/**
+ * 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.events;
+
+/**
+ * The {@link HostRegisteredEvent} class is fired when a host registered with
+ * the server.
+ */
+public class HostRegisteredEvent extends HostEvent {
+  /**
+   * Constructor.
+   *
+   * @param hostName
+   */
+  public HostRegisteredEvent(String hostName) {
+    super(AmbariEventType.HOST_REGISTERED, hostName);
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    StringBuilder buffer = new StringBuilder("HostRegistered{ ");
+    buffer.append("hostName=").append(m_hostName);
+    buffer.append("}");
+    return buffer.toString();
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/events/HostRemovedEvent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/HostRemovedEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/events/HostRemovedEvent.java
index a0aae66..e005754 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/HostRemovedEvent.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/HostRemovedEvent.java
@@ -28,7 +28,7 @@ public class HostRemovedEvent extends HostEvent {
    * @param hostName
    */
   public HostRemovedEvent(String hostName) {
-    super(AmbariEventType.HOST_ADDED, hostName);
+    super(AmbariEventType.HOST_REMOVED, hostName);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHashInvalidationListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHashInvalidationListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHashInvalidationListener.java
index 0accaf9..655352f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHashInvalidationListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHashInvalidationListener.java
@@ -91,7 +91,8 @@ public class AlertHashInvalidationListener {
       return;
     }
 
-    m_alertDefinitionHash.get().enqueueAgentCommands(clusterId, hosts);
+    AlertDefinitionHash hash = m_alertDefinitionHash.get();
+    hash.enqueueAgentCommands(clusterId, hosts);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHostListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHostListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHostListener.java
index 51e5e67..d478bf5 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHostListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertHostListener.java
@@ -17,13 +17,26 @@
  */
 package org.apache.ambari.server.events.listeners.alerts;
 
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.EagerSingleton;
+import org.apache.ambari.server.events.AlertHashInvalidationEvent;
+import org.apache.ambari.server.events.HostAddedEvent;
 import org.apache.ambari.server.events.HostRemovedEvent;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
+import org.apache.ambari.server.metadata.AgentAlertDefinitions;
+import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.dao.AlertsDAO;
 import org.apache.ambari.server.orm.entities.AlertCurrentEntity;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
+import org.apache.ambari.server.state.alert.AlertDefinition;
+import org.apache.ambari.server.state.alert.AlertDefinitionFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.eventbus.AllowConcurrentEvents;
 import com.google.common.eventbus.Subscribe;
@@ -31,8 +44,9 @@ import com.google.inject.Inject;
 import com.google.inject.Singleton;
 
 /**
- * The {@link AlertHostListener} class handles {@link HostRemovedEvent} and
- * ensures that {@link AlertCurrentEntity} instances are properly cleaned up
+ * The {@link AlertHostListener} class handles {@link HostAddedEvent} and
+ * {@link HostRemovedEvent} and ensures that {@link AlertCurrentEntity}
+ * instances are properly cleaned up
  */
 @Singleton
 @EagerSingleton
@@ -40,7 +54,7 @@ public class AlertHostListener {
   /**
    * Logger.
    */
-  private static Log LOG = LogFactory.getLog(AlertHostListener.class);
+  private final static Logger LOG = LoggerFactory.getLogger(AlertHostListener.class);
 
   /**
    * Used for removing current alerts when a service is removed.
@@ -49,6 +63,37 @@ public class AlertHostListener {
   private AlertsDAO m_alertsDao;
 
   /**
+   * Used for checking to see if definitions already exist for a cluster.
+   */
+  @Inject
+  private AlertDefinitionDAO m_alertDefinitionDao;
+
+  /**
+   * Used to publish events when an alert definition has a lifecycle event.
+   */
+  @Inject
+  private AmbariEventPublisher m_eventPublisher;
+
+  /**
+   * All of the {@link AlertDefinition}s that are scoped for the agents.
+   */
+  @Inject
+  private AgentAlertDefinitions m_agentAlertDefinitions;
+
+  /**
+   * Used when a host is added to a cluster to coerce an {@link AlertDefinition}
+   * into an {@link AlertDefinitionEntity}.
+   */
+  @Inject
+  private AlertDefinitionFactory m_alertDefinitionFactory;
+
+  /**
+   * Used to prevent multiple threads from trying to create host alerts
+   * simultaneously.
+   */
+  private Lock m_hostAlertLock = new ReentrantLock();
+
+  /**
    * Constructor.
    *
    * @param publisher
@@ -59,15 +104,69 @@ public class AlertHostListener {
   }
 
   /**
-   * Removes any current alerts associated with the specified host.
-   *
-   * @param event
-   *          the published event being handled (not {@code null}).
+   * Handles the {@link HostAddedEvent} by performing the following actions:
+   * <ul>
+   * <li>Ensures that all host-level alerts are loaded for the cluster. This is
+   * especially useful when creating a cluster and no alerts were loaded on
+   * Ambari startup</li>
+   * <li>Broadcasts the {@link AlertHashInvalidationEvent} in order to push host
+   * alert definitions</li>
+   * </ul>
+   */
+  @Subscribe
+  @AllowConcurrentEvents
+  public void onAmbariEvent(HostAddedEvent event) {
+    LOG.debug("Received event {}", event);
+
+    long clusterId = event.getClusterId();
+
+    // load the host-only alert definitions
+    List<AlertDefinition> agentDefinitions = m_agentAlertDefinitions.getDefinitions();
+
+    // lock to prevent multiple threads from trying to create alert
+    // definitions at the same time
+    m_hostAlertLock.lock();
+
+    try {
+      for (AlertDefinition agentDefinition : agentDefinitions) {
+        AlertDefinitionEntity definition = m_alertDefinitionDao.findByName(
+            clusterId, agentDefinition.getName());
+
+        // this host definition does not exist, add it
+        if (null == definition) {
+          definition = m_alertDefinitionFactory.coerce(clusterId,
+              agentDefinition);
+
+          try {
+            m_alertDefinitionDao.create(definition);
+          } catch (AmbariException ambariException) {
+            LOG.error(
+                "Unable to create a host alert definition name {} in cluster {}",
+                definition.getDefinitionName(), definition.getClusterId(),
+                ambariException);
+          }
+        }
+      }
+    } finally {
+      m_hostAlertLock.unlock();
+    }
+
+    AlertHashInvalidationEvent invalidationEvent = new AlertHashInvalidationEvent(
+        event.getClusterId(), Collections.singletonList(event.getHostName()));
+
+    m_eventPublisher.publish(invalidationEvent);
+  }
+
+  /**
+   * Handles the {@link HostRemovedEvent} by performing the following actions:
+   * <ul>
+   * <li>Removes all {@link AlertCurrentEntity} for the removed host</li>
+   * </ul>
    */
   @Subscribe
   @AllowConcurrentEvents
   public void onAmbariEvent(HostRemovedEvent event) {
-    LOG.debug(event);
+    LOG.debug("Received event {}", event);
 
     // remove any current alerts for the removed host
     m_alertsDao.removeCurrentByHost(event.getHostName());

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertServiceStateListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertServiceStateListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertServiceStateListener.java
index b56f23d..fd7035d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertServiceStateListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/alerts/AlertServiceStateListener.java
@@ -126,13 +126,11 @@ public class AlertServiceStateListener {
     // create the default alert group for the new service; this MUST be done
     // before adding definitions so that they are properly added to the
     // default group
-    AlertGroupEntity serviceAlertGroup = new AlertGroupEntity();
-    serviceAlertGroup.setClusterId(clusterId);
-    serviceAlertGroup.setDefault(true);
-    serviceAlertGroup.setGroupName(serviceName);
-    serviceAlertGroup.setServiceName(serviceName);
-
-    m_alertDispatchDao.create(serviceAlertGroup);
+    try {
+      m_alertDispatchDao.createDefaultGroup(clusterId, serviceName);
+    } catch (AmbariException ambariException) {
+      LOG.error(ambariException);
+    }
 
     // populate alert definitions for the new service from the database, but
     // don't worry about sending down commands to the agents; the host

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
index 8e8c808..23de17e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAO.java
@@ -24,6 +24,7 @@ import java.util.Set;
 import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
 
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.RootServiceResponseFactory;
 import org.apache.ambari.server.events.AlertDefinitionDeleteEvent;
 import org.apache.ambari.server.events.AlertDefinitionRegistrationEvent;
@@ -56,25 +57,25 @@ public class AlertDefinitionDAO {
    * JPA entity manager
    */
   @Inject
-  Provider<EntityManager> entityManagerProvider;
+  private Provider<EntityManager> entityManagerProvider;
 
   /**
    * DAO utilities for dealing mostly with {@link TypedQuery} results.
    */
   @Inject
-  DaoUtils daoUtils;
+  private DaoUtils daoUtils;
 
   /**
    * Alert history DAO.
    */
   @Inject
-  AlertsDAO alertsDao;
+  private AlertsDAO alertsDao;
 
   /**
    * Alert dispatch DAO.
    */
   @Inject
-  AlertDispatchDAO dispatchDao;
+  private AlertDispatchDAO dispatchDao;
 
   /**
    * Publishes the following events:
@@ -284,16 +285,25 @@ public class AlertDefinitionDAO {
    *          the definition to persist (not {@code null}).
    */
   @Transactional
-  public void create(AlertDefinitionEntity alertDefinition) {
+  public void create(AlertDefinitionEntity alertDefinition)
+      throws AmbariException {
     entityManagerProvider.get().persist(alertDefinition);
 
-    AlertGroupEntity group = dispatchDao.findDefaultServiceGroup(alertDefinition.getServiceName());
+    AlertGroupEntity group = dispatchDao.findDefaultServiceGroup(
+        alertDefinition.getClusterId(), alertDefinition.getServiceName());
 
-    if (null != group) {
-      group.addAlertDefinition(alertDefinition);
-      dispatchDao.merge(group);
+    if (null == group) {
+      // create the default alert group for the new service; this MUST be done
+      // before adding definitions so that they are properly added to the
+      // default group
+      String serviceName = alertDefinition.getServiceName();
+      group = dispatchDao.createDefaultGroup(alertDefinition.getClusterId(),
+          serviceName);
     }
 
+    group.addAlertDefinition(alertDefinition);
+    dispatchDao.merge(group);
+
     // publish the alert definition registration
     AlertDefinition coerced = alertDefinitionFactory.coerce(alertDefinition);
     if (null != coerced) {
@@ -339,7 +349,8 @@ public class AlertDefinitionDAO {
    * @param alertDefinition
    *          the definition to create or update (not {@code null}).
    */
-  public void createOrUpdate(AlertDefinitionEntity alertDefinition) {
+  public void createOrUpdate(AlertDefinitionEntity alertDefinition)
+      throws AmbariException {
     if (null == alertDefinition.getDefinitionId()) {
       create(alertDefinition);
     } else {

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
index b6c1e90..3a3ad15 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
@@ -17,7 +17,11 @@
  */
 package org.apache.ambari.server.orm.dao;
 
+import java.text.MessageFormat;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
@@ -25,9 +29,11 @@ import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Order;
 import javax.persistence.metamodel.SingularAttribute;
 
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.query.JpaPredicateVisitor;
 import org.apache.ambari.server.api.query.JpaSortBuilder;
 import org.apache.ambari.server.controller.AlertNoticeRequest;
+import org.apache.ambari.server.controller.RootServiceResponseFactory.Services;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.utilities.PredicateHelper;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
@@ -35,7 +41,10 @@ import org.apache.ambari.server.orm.entities.AlertGroupEntity;
 import org.apache.ambari.server.orm.entities.AlertNoticeEntity;
 import org.apache.ambari.server.orm.entities.AlertNoticeEntity_;
 import org.apache.ambari.server.orm.entities.AlertTargetEntity;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.NotificationState;
+import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.alert.AlertGroup;
 
 import com.google.inject.Inject;
@@ -53,13 +62,26 @@ public class AlertDispatchDAO {
    * JPA entity manager
    */
   @Inject
-  Provider<EntityManager> entityManagerProvider;
+  private Provider<EntityManager> entityManagerProvider;
 
   /**
    * DAO utilities for dealing mostly with {@link TypedQuery} results.
    */
   @Inject
-  DaoUtils daoUtils;
+  private DaoUtils daoUtils;
+
+  /**
+   * Used to retrieve a cluster and its services when creating a default
+   * {@link AlertGroupEntity} for a service.
+   */
+  @Inject
+  private Provider<Clusters> m_clusters;
+
+  /**
+   * A lock that ensures that group writes are protected. This is useful since
+   * groups can be created through different events/threads in the system.
+   */
+  private final Lock m_groupLock = new ReentrantLock();
 
   /**
    * Gets an alert group with the specified ID.
@@ -256,18 +278,22 @@ public class AlertDispatchDAO {
   }
 
   /**
-   * Gets the default group for the specified service.
+   * Gets the default group for the specified cluster and service.
    *
+   * @param clusterId
+   *          the cluster that the group belongs to
    * @param serviceName
    *          the name of the service (not {@code null}).
    * @return the default group, or {@code null} if the service name is not valid
    *         for an installed service; otherwise {@code null} should not be
    *         possible.
    */
-  public AlertGroupEntity findDefaultServiceGroup(String serviceName) {
+  public AlertGroupEntity findDefaultServiceGroup(long clusterId,
+      String serviceName) {
     TypedQuery<AlertGroupEntity> query = entityManagerProvider.get().createNamedQuery(
         "AlertGroupEntity.findServiceDefaultGroup", AlertGroupEntity.class);
 
+    query.setParameter("clusterId", clusterId);
     query.setParameter("serviceName", serviceName);
     return daoUtils.selectSingle(query);
   }
@@ -364,6 +390,49 @@ public class AlertDispatchDAO {
   }
 
   /**
+   * Creates a default group in the specified cluster and service. If the
+   * service is not valid, then this will throw an {@link AmbariException}.
+   *
+   * @param clusterId
+   *          the cluster that the group is in.
+   * @param serviceName
+   *          the name of the group which is also the service name.
+   */
+  public AlertGroupEntity createDefaultGroup(long clusterId, String serviceName)
+      throws AmbariException {
+    // AMBARI is a special service that we let through, otherwise we need to
+    // verify that the service exists before we create the default group
+    String ambariServiceName = Services.AMBARI.name();
+    if (!ambariServiceName.equals(serviceName)) {
+      Cluster cluster = m_clusters.get().getClusterById(clusterId);
+      Map<String, Service> services = cluster.getServices();
+
+      if (!services.containsKey(serviceName)) {
+        String message = MessageFormat.format(
+            "Unable to create a default alert group for unknown service {0} in cluster {1}",
+            serviceName, cluster.getClusterName());
+        throw new AmbariException(message);
+      }
+    }
+
+    AlertGroupEntity group = new AlertGroupEntity();
+
+    m_groupLock.lock();
+    try {
+      group.setClusterId(clusterId);
+      group.setDefault(true);
+      group.setGroupName(serviceName);
+      group.setServiceName(serviceName);
+
+      create(group);
+    } finally {
+      m_groupLock.unlock();
+    }
+
+    return group;
+  }
+
+  /**
    * Refresh the state of the alert group from the database.
    *
    * @param alertGroup

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java
index 1127dd1..4908eea 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java
@@ -503,8 +503,7 @@ public class AlertsDAO {
    */
   @Transactional
   public int removeCurrentByServiceComponentHost(String serviceName,
-      String componentName,
-      String hostName) {
+      String componentName, String hostName) {
 
     TypedQuery<AlertCurrentEntity> query = entityManagerProvider.get().createNamedQuery(
         "AlertCurrentEntity.removeByHostComponent", AlertCurrentEntity.class);

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterServiceDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterServiceDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterServiceDAO.java
index dcad56e..1306c6c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterServiceDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterServiceDAO.java
@@ -18,18 +18,20 @@
 
 package org.apache.ambari.server.orm.dao;
 
-import com.google.inject.Inject;
-import com.google.inject.Provider;
-import com.google.inject.Singleton;
-import com.google.inject.persist.Transactional;
-import org.apache.ambari.server.orm.RequiresSession;
-import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
-import org.apache.ambari.server.orm.entities.ClusterServiceEntityPK;
+import java.util.List;
 
 import javax.persistence.EntityManager;
 import javax.persistence.NoResultException;
 import javax.persistence.TypedQuery;
-import java.util.List;
+
+import org.apache.ambari.server.orm.RequiresSession;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntityPK;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import com.google.inject.persist.Transactional;
 
 @Singleton
 public class ClusterServiceDAO {
@@ -84,7 +86,8 @@ public class ClusterServiceDAO {
 
   @Transactional
   public void removeByPK(ClusterServiceEntityPK clusterServiceEntityPK) {
-    remove(findByPK(clusterServiceEntityPK));
+    ClusterServiceEntity entity = findByPK(clusterServiceEntityPK);
+    entityManagerProvider.get().remove(entity);
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceComponentDesiredStateDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceComponentDesiredStateDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceComponentDesiredStateDAO.java
index 8cf7ec3..341d1fd 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceComponentDesiredStateDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceComponentDesiredStateDAO.java
@@ -18,18 +18,20 @@
 
 package org.apache.ambari.server.orm.dao;
 
-import com.google.inject.Inject;
-import com.google.inject.Provider;
-import com.google.inject.Singleton;
-import com.google.inject.persist.Transactional;
-import org.apache.ambari.server.orm.RequiresSession;
-import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntityPK;
-import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
+import java.util.List;
 
 import javax.persistence.EntityManager;
 import javax.persistence.NoResultException;
 import javax.persistence.TypedQuery;
-import java.util.List;
+
+import org.apache.ambari.server.orm.RequiresSession;
+import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntityPK;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import com.google.inject.persist.Transactional;
 
 @Singleton
 public class ServiceComponentDesiredStateDAO {
@@ -75,6 +77,7 @@ public class ServiceComponentDesiredStateDAO {
 
   @Transactional
   public void removeByPK(ServiceComponentDesiredStateEntityPK primaryKey) {
-    remove(findByPK(primaryKey));
+    ServiceComponentDesiredStateEntity entity = findByPK(primaryKey);
+    entityManagerProvider.get().remove(entity);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
index a330c90..81fb133 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
@@ -536,4 +536,22 @@ public class AlertDefinitionEntity {
     int result = null != definitionId ? definitionId.hashCode() : 0;
     return result;
   }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    StringBuilder buffer = new StringBuilder();
+    buffer.append(getClass().getSimpleName());
+    buffer.append("{");
+    buffer.append("id=").append(definitionId);
+    buffer.append(", name=").append(definitionName);
+    buffer.append(", serviceName=").append(serviceName);
+    buffer.append(", componentName=").append(componentName);
+    buffer.append(", enabled=").append(enabled);
+    buffer.append(", hash=").append(hash);
+    buffer.append("}");
+    return buffer.toString();
+  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
index ac3586d..6607fb0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
@@ -51,7 +51,7 @@ import javax.persistence.UniqueConstraint;
     @NamedQuery(name = "AlertGroupEntity.findByName", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.groupName = :groupName"),
     @NamedQuery(name = "AlertGroupEntity.findByNameInCluster", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.groupName = :groupName AND alertGroup.clusterId = :clusterId"),
     @NamedQuery(name = "AlertGroupEntity.findByAssociatedDefinition", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE :alertDefinition MEMBER OF alertGroup.alertDefinitions"),
-    @NamedQuery(name = "AlertGroupEntity.findServiceDefaultGroup", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.serviceName = :serviceName AND alertGroup.isDefault = 1") })
+    @NamedQuery(name = "AlertGroupEntity.findServiceDefaultGroup", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.clusterId = :clusterId AND alertGroup.serviceName = :serviceName AND alertGroup.isDefault = 1") })
 public class AlertGroupEntity {
 
   @Id
@@ -353,4 +353,19 @@ public class AlertGroupEntity {
     int result = null != groupId ? groupId.hashCode() : 0;
     return result;
   }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    StringBuilder buffer = new StringBuilder();
+    buffer.append(getClass().getSimpleName());
+    buffer.append("{");
+    buffer.append("id=").append(groupId);
+    buffer.append(", name=").append(groupName);
+    buffer.append(", default=").append(isDefault);
+    buffer.append("}");
+    return buffer.toString();
+  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
index a671ae1..8e96aca 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
@@ -355,4 +355,21 @@ public class AlertHistoryEntity {
     return result;
   }
 
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    StringBuilder buffer = new StringBuilder();
+    buffer.append(getClass().getSimpleName());
+    buffer.append("{");
+    buffer.append("id=").append(alertId);
+    buffer.append(", serviceName=").append(serviceName);
+    buffer.append(", componentName=").append(componentName);
+    buffer.append(", state=").append(alertState);
+    buffer.append(", label=").append(alertLabel);
+    buffer.append("}");
+    return buffer.toString();
+  }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
index d18ec68..9f24dc3 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
@@ -295,4 +295,17 @@ public class AlertTargetEntity {
     return result;
   }
 
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    StringBuilder buffer = new StringBuilder();
+    buffer.append(getClass().getSimpleName());
+    buffer.append("{");
+    buffer.append("id=").append(targetId);
+    buffer.append(", name=").append(targetName);
+    buffer.append("}");
+    return buffer.toString();
+  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java b/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java
index abffa5d..c8b78a0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertDefinitionHash.java
@@ -584,8 +584,8 @@ public class AlertDefinitionHash {
    *         {@code null}).
    */
   private Set<AlertDefinitionEntity> getAlertDefinitionEntities(
-      String clusterName,
-      String hostName) {
+      String clusterName, String hostName) {
+
     Set<AlertDefinitionEntity> definitions = new HashSet<AlertDefinitionEntity>();
 
     try {
@@ -598,45 +598,41 @@ public class AlertDefinitionHash {
       }
 
       long clusterId = cluster.getClusterId();
-      List<ServiceComponentHost> serviceComponents = cluster.getServiceComponentHosts(hostName);
-      if (null == serviceComponents || serviceComponents.size() == 0) {
-        LOG.warn(
-            "Unable to get alert definitions for {} since there are no service components defined",
-            hostName);
-
-        return Collections.emptySet();
-      }
-
-      for (ServiceComponentHost serviceComponent : serviceComponents) {
-        String serviceName = serviceComponent.getServiceName();
-        String componentName = serviceComponent.getServiceComponentName();
 
-        // add all alerts for this service/component pair
-        definitions.addAll(m_definitionDao.findByServiceComponent(
-            clusterId, serviceName, componentName));
-      }
+      // services and components
+      List<ServiceComponentHost> serviceComponents = cluster.getServiceComponentHosts(hostName);
+      if (null == serviceComponents || !serviceComponents.isEmpty()) {
+        for (ServiceComponentHost serviceComponent : serviceComponents) {
+          String serviceName = serviceComponent.getServiceName();
+          String componentName = serviceComponent.getServiceComponentName();
+
+          // add all alerts for this service/component pair
+          definitions.addAll(m_definitionDao.findByServiceComponent(clusterId,
+              serviceName, componentName));
+        }
 
-      // for every service, get the master components and see if the host
-      // is a master
-      Set<String> services = new HashSet<String>();
-      for (Entry<String, Service> entry : cluster.getServices().entrySet()) {
-        Service service = entry.getValue();
-        Map<String, ServiceComponent> components = service.getServiceComponents();
-        for (Entry<String, ServiceComponent> component : components.entrySet()) {
-          if (component.getValue().isMasterComponent()) {
-            Map<String, ServiceComponentHost> hosts = component.getValue().getServiceComponentHosts();
-
-            if( hosts.containsKey( hostName ) ){
-              services.add(service.getName());
+        // for every service, get the master components and see if the host
+        // is a master
+        Set<String> services = new HashSet<String>();
+        for (Entry<String, Service> entry : cluster.getServices().entrySet()) {
+          Service service = entry.getValue();
+          Map<String, ServiceComponent> components = service.getServiceComponents();
+          for (Entry<String, ServiceComponent> component : components.entrySet()) {
+            if (component.getValue().isMasterComponent()) {
+              Map<String, ServiceComponentHost> hosts = component.getValue().getServiceComponentHosts();
+
+              if (hosts.containsKey(hostName)) {
+                services.add(service.getName());
+              }
             }
           }
         }
-      }
 
-      // add all service scoped alerts
-      if( services.size() > 0 ){
-        definitions.addAll(m_definitionDao.findByServiceMaster(clusterId,
-            services));
+        // add all service scoped alerts
+        if (services.size() > 0) {
+          definitions.addAll(m_definitionDao.findByServiceMaster(clusterId,
+              services));
+        }
       }
 
       // add any alerts not bound to a service (host level alerts)

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
index 991d289..d2c7428 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
@@ -39,11 +39,12 @@ import org.apache.ambari.server.HostNotFoundException;
 import org.apache.ambari.server.agent.DiskInfo;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.events.HostAddedEvent;
+import org.apache.ambari.server.events.HostRegisteredEvent;
 import org.apache.ambari.server.events.HostRemovedEvent;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
-import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.ResourceDAO;
@@ -121,8 +122,6 @@ public class ClustersImpl implements Clusters {
   @Inject
   Gson gson;
   @Inject
-  private ConfigGroupHostMappingDAO configGroupHostMappingDAO;
-  @Inject
   private SecurityHelper securityHelper;
 
   /**
@@ -341,12 +340,14 @@ public class ClustersImpl implements Clusters {
   @Override
   public void addHost(String hostname) throws AmbariException {
     checkLoaded();
+
     String duplicateMessage = "Duplicate entry for Host"
         + ", hostName= " + hostname;
 
     if (hosts.containsKey(hostname)) {
       throw new AmbariException(duplicateMessage);
     }
+
     r.lock();
 
     try {
@@ -371,6 +372,10 @@ public class ClustersImpl implements Clusters {
     } finally {
       r.unlock();
     }
+
+    // publish the event
+    HostRegisteredEvent event = new HostRegisteredEvent(hostname);
+    eventPublisher.publish(event);
   }
 
   private boolean isOsSupportedByClusterStack(Cluster c, Host h) throws AmbariException {
@@ -558,6 +563,10 @@ public class ClustersImpl implements Clusters {
 
     clusterDAO.merge(clusterEntity);
     hostDAO.merge(hostEntity);
+
+    // publish the event for adding a host to a cluster
+    HostAddedEvent event = new HostAddedEvent(clusterId, hostName);
+    eventPublisher.publish(event);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
index dc71862..0c93ec2 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
@@ -20,12 +20,15 @@ package org.apache.ambari.server.orm;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
@@ -59,11 +62,22 @@ import org.apache.ambari.server.orm.entities.ResourceEntity;
 import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
 import org.apache.ambari.server.orm.entities.StageEntity;
 import org.apache.ambari.server.orm.entities.UserEntity;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.HostState;
+import org.apache.ambari.server.state.RepositoryVersionState;
+import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.state.ServiceComponent;
+import org.apache.ambari.server.state.ServiceComponentFactory;
+import org.apache.ambari.server.state.ServiceComponentHost;
+import org.apache.ambari.server.state.ServiceComponentHostFactory;
+import org.apache.ambari.server.state.ServiceFactory;
+import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.State;
 import org.apache.ambari.server.state.alert.Scope;
 import org.apache.ambari.server.state.alert.SourceType;
 import org.springframework.security.crypto.password.PasswordEncoder;
-import org.springframework.util.Assert;
 
 import com.google.inject.Inject;
 import com.google.inject.Injector;
@@ -289,11 +303,131 @@ public class OrmTestHelper {
     clusterDAO.create(clusterEntity);
 
     clusterEntity = clusterDAO.findByName(clusterEntity.getClusterName());
-    Assert.notNull(clusterEntity);
-    Assert.isTrue(clusterEntity.getClusterId() > 0);
+    assertNotNull(clusterEntity);
+    assertTrue(clusterEntity.getClusterId() > 0);
     return clusterEntity.getClusterId();
   }
 
+  public Cluster buildNewCluster(Clusters clusters,
+      ServiceFactory serviceFactory, ServiceComponentFactory componentFactory,
+      ServiceComponentHostFactory schFactory, String hostName) throws Exception {
+    String clusterName = "cluster-" + System.currentTimeMillis();
+    clusters.addCluster(clusterName);
+    Cluster cluster = clusters.getCluster(clusterName);
+    cluster = initializeClusterWithStack(cluster);
+
+    addHost(clusters, cluster, hostName);
+
+    installHdfsService(cluster, serviceFactory, componentFactory, schFactory, hostName);
+    installYarnService(cluster, serviceFactory, componentFactory, schFactory,
+        hostName);
+    return cluster;
+  }
+
+  public Cluster initializeClusterWithStack(Cluster cluster) throws Exception {
+    StackId stackId = new StackId("HDP", "2.0.6");
+    cluster.setDesiredStackVersion(stackId);
+    cluster.createClusterVersion(stackId.getStackName(),
+        stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
+    return cluster;
+  }
+
+  /**
+   * @throws Exception
+   */
+  public void addHost(Clusters clusters, Cluster cluster, String hostName)
+      throws Exception {
+    clusters.addHost(hostName);
+
+    Host host = clusters.getHost(hostName);
+    Map<String, String> hostAttributes = new HashMap<String, String>();
+    hostAttributes.put("os_family", "redhat");
+    hostAttributes.put("os_release_version", "6.4");
+    host.setHostAttributes(hostAttributes);
+    host.setState(HostState.HEALTHY);
+    host.persist();
+
+    clusters.mapHostToCluster(hostName, cluster.getClusterName());
+  }
+
+  /**
+   * Calls {@link Service#persist()} to mock a service install along with
+   * creating a single {@link Host} and {@link ServiceComponentHost}.
+   */
+  public void installHdfsService(Cluster cluster,
+      ServiceFactory serviceFactory, ServiceComponentFactory componentFactory,
+      ServiceComponentHostFactory schFactory, String hostName) throws Exception {
+    String serviceName = "HDFS";
+    Service service = serviceFactory.createNew(cluster, serviceName);
+    cluster.addService(service);
+    service.persist();
+    service = cluster.getService(serviceName);
+    assertNotNull(service);
+
+    ServiceComponent datanode = componentFactory.createNew(service, "DATANODE");
+
+    service.addServiceComponent(datanode);
+    datanode.setDesiredState(State.INSTALLED);
+    datanode.persist();
+
+    ServiceComponentHost sch = schFactory.createNew(datanode, hostName);
+
+    datanode.addServiceComponentHost(sch);
+    sch.setDesiredState(State.INSTALLED);
+    sch.setState(State.INSTALLED);
+    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
+    sch.setStackVersion(new StackId("HDP-2.0.6"));
+
+    sch.persist();
+
+    ServiceComponent namenode = componentFactory.createNew(service, "NAMENODE");
+
+    service.addServiceComponent(namenode);
+    namenode.setDesiredState(State.INSTALLED);
+    namenode.persist();
+
+    sch = schFactory.createNew(namenode, hostName);
+    namenode.addServiceComponentHost(sch);
+    sch.setDesiredState(State.INSTALLED);
+    sch.setState(State.INSTALLED);
+    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
+    sch.setStackVersion(new StackId("HDP-2.0.6"));
+
+    sch.persist();
+  }
+
+  /**
+   * Calls {@link Service#persist()} to mock a service install along with
+   * creating a single {@link Host} and {@link ServiceComponentHost}.
+   */
+  public void installYarnService(Cluster cluster,
+      ServiceFactory serviceFactory, ServiceComponentFactory componentFactory,
+      ServiceComponentHostFactory schFactory, String hostName) throws Exception {
+    String serviceName = "YARN";
+    Service service = serviceFactory.createNew(cluster, serviceName);
+    cluster.addService(service);
+    service.persist();
+    service = cluster.getService(serviceName);
+    assertNotNull(service);
+
+    ServiceComponent resourceManager = componentFactory.createNew(service,
+        "RESOURCEMANAGER");
+
+    service.addServiceComponent(resourceManager);
+    resourceManager.setDesiredState(State.INSTALLED);
+    resourceManager.persist();
+
+    ServiceComponentHost sch = schFactory.createNew(resourceManager, hostName);
+
+    resourceManager.addServiceComponentHost(sch);
+    sch.setDesiredState(State.INSTALLED);
+    sch.setState(State.INSTALLED);
+    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
+    sch.setStackVersion(new StackId("HDP-2.0.6"));
+
+    sch.persist();
+  }
+
   /**
    * Creates an alert target.
    *
@@ -324,7 +458,7 @@ public class OrmTestHelper {
     AlertDefinitionEntity definition = new AlertDefinitionEntity();
     definition.setDefinitionName("Alert Definition "
         + System.currentTimeMillis());
-    definition.setServiceName("Service " + System.currentTimeMillis());
+    definition.setServiceName("AMBARI");
     definition.setComponentName(null);
     definition.setClusterId(clusterId);
     definition.setHash(UUID.randomUUID().toString());
@@ -385,8 +519,8 @@ public class OrmTestHelper {
 
     List<AlertGroupEntity> defaultGroups = alertDispatchDAO.findAllGroups(clusterId);
     assertEquals(2, defaultGroups.size());
-    assertNotNull(alertDispatchDAO.findDefaultServiceGroup("HDFS"));
-    assertNotNull(alertDispatchDAO.findDefaultServiceGroup("OOZIE"));
+    assertNotNull(alertDispatchDAO.findDefaultServiceGroup(clusterId, "HDFS"));
+    assertNotNull(alertDispatchDAO.findDefaultServiceGroup(clusterId, "OOZIE"));
 
     return defaultGroups;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
index 0d2d305..3d8f898 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
@@ -19,21 +19,20 @@
 package org.apache.ambari.server.orm.dao;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.lang.reflect.Field;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
-import junit.framework.Assert;
-
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.AlertNoticeRequest;
 import org.apache.ambari.server.controller.internal.AlertNoticeResourceProvider;
 import org.apache.ambari.server.controller.internal.PageRequestImpl;
@@ -44,6 +43,8 @@ import org.apache.ambari.server.controller.spi.SortRequest;
 import org.apache.ambari.server.controller.spi.SortRequest.Order;
 import org.apache.ambari.server.controller.spi.SortRequestProperty;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
+import org.apache.ambari.server.events.listeners.alerts.AlertServiceStateListener;
+import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.orm.AlertDaoHelper;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
@@ -55,25 +56,18 @@ import org.apache.ambari.server.orm.entities.AlertNoticeEntity;
 import org.apache.ambari.server.orm.entities.AlertTargetEntity;
 import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Host;
-import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.NotificationState;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentFactory;
-import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.ServiceComponentHostFactory;
 import org.apache.ambari.server.state.ServiceFactory;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.State;
 import org.apache.ambari.server.state.alert.Scope;
 import org.apache.ambari.server.state.alert.SourceType;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.eventbus.EventBus;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.persist.PersistService;
@@ -86,7 +80,7 @@ public class AlertDispatchDAOTest {
   private final static String HOSTNAME = "c6401.ambari.apache.org";
 
   private Clusters m_clusters;
-  private Long m_clusterId;
+  private Cluster m_cluster;
   private Injector m_injector;
   private AlertDispatchDAO m_dao;
   private AlertDefinitionDAO m_definitionDao;
@@ -97,6 +91,8 @@ public class AlertDispatchDAOTest {
   private ServiceComponentFactory m_componentFactory;
   private ServiceComponentHostFactory m_schFactory;
   private AlertDaoHelper m_alertHelper;
+  private AmbariEventPublisher m_eventPublisher;
+  private EventBus m_synchronizedBus;
 
   /**
    *
@@ -114,15 +110,24 @@ public class AlertDispatchDAOTest {
     m_schFactory = m_injector.getInstance(ServiceComponentHostFactory.class);
     m_clusters = m_injector.getInstance(Clusters.class);
     m_alertHelper = m_injector.getInstance(AlertDaoHelper.class);
+    m_eventPublisher = m_injector.getInstance(AmbariEventPublisher.class);
+
+    // !!! need a synchronous op for testing
+    m_synchronizedBus = new EventBus();
+    Field field = AmbariEventPublisher.class.getDeclaredField("m_eventBus");
+    field.setAccessible(true);
+    field.set(m_eventPublisher, m_synchronizedBus);
+
+    m_cluster = m_clusters.getClusterById(m_helper.createCluster());
+    m_helper.initializeClusterWithStack(m_cluster);
 
-    m_clusterId = m_helper.createCluster();
     Set<AlertTargetEntity> targets = createTargets();
 
     for (int i = 0; i < 10; i++) {
       AlertGroupEntity group = new AlertGroupEntity();
       group.setDefault(false);
       group.setGroupName("Group Name " + i);
-      group.setClusterId(m_clusterId);
+      group.setClusterId(m_cluster.getClusterId());
       for (AlertTargetEntity alertTarget : targets) {
         group.addAlertTarget(alertTarget);
       }
@@ -214,7 +219,8 @@ public class AlertDispatchDAOTest {
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
     targets.add(target);
 
-    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, targets);
+    AlertGroupEntity group = m_helper.createAlertGroup(
+        m_cluster.getClusterId(), targets);
     AlertGroupEntity actual = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
 
@@ -231,7 +237,8 @@ public class AlertDispatchDAOTest {
   public void testGroupDefinitions() throws Exception {
     List<AlertDefinitionEntity> definitions = createDefinitions();
 
-    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, null);
+    AlertGroupEntity group = m_helper.createAlertGroup(
+        m_cluster.getClusterId(), null);
 
     group = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
@@ -272,7 +279,8 @@ public class AlertDispatchDAOTest {
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
     targets.add(target);
 
-    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, targets);
+    AlertGroupEntity group = m_helper.createAlertGroup(
+        m_cluster.getClusterId(), targets);
     AlertTargetEntity actual = m_dao.findTargetById(target.getTargetId());
     assertNotNull(actual);
 
@@ -297,7 +305,8 @@ public class AlertDispatchDAOTest {
   public void testDeleteGroup() throws Exception {
     int targetCount = m_dao.findAllTargets().size();
 
-    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, null);
+    AlertGroupEntity group = m_helper.createAlertGroup(
+        m_cluster.getClusterId(), null);
     AlertTargetEntity target = m_helper.createAlertTarget();
     assertEquals(targetCount + 1, m_dao.findAllTargets().size());
 
@@ -347,7 +356,8 @@ public class AlertDispatchDAOTest {
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
     targets.add(target);
 
-    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, targets);
+    AlertGroupEntity group = m_helper.createAlertGroup(
+        m_cluster.getClusterId(), targets);
     assertEquals(1, group.getAlertTargets().size());
 
     target = m_dao.findTargetById(target.getTargetId());
@@ -377,7 +387,8 @@ public class AlertDispatchDAOTest {
 
     String groupName = "Group Name " + System.currentTimeMillis();
 
-    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, null);
+    AlertGroupEntity group = m_helper.createAlertGroup(
+        m_cluster.getClusterId(), null);
 
     group = m_dao.findGroupById(group.getGroupId());
     group.setGroupName(groupName + "FOO");
@@ -406,7 +417,8 @@ public class AlertDispatchDAOTest {
   @Test
   public void testFindGroupsByDefinition() throws Exception {
     List<AlertDefinitionEntity> definitions = createDefinitions();
-    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, null);
+    AlertGroupEntity group = m_helper.createAlertGroup(
+        m_cluster.getClusterId(), null);
 
     group = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
@@ -420,10 +432,11 @@ public class AlertDispatchDAOTest {
     group = m_dao.findGroupByName(group.getGroupName());
     assertEquals(definitions.size(), group.getAlertDefinitions().size());
 
+    // assert that the definition is now part of 2 groups (the default group
+    // and the newly associated group from above)
     for (AlertDefinitionEntity definition : definitions) {
       List<AlertGroupEntity> groups = m_dao.findGroupsByDefinition(definition);
-      assertEquals(1, groups.size());
-      assertEquals(group.getGroupId(), groups.get(0).getGroupId());
+      assertEquals(2, groups.size());
     }
   }
 
@@ -437,7 +450,7 @@ public class AlertDispatchDAOTest {
 
     AlertHistoryEntity history = new AlertHistoryEntity();
     history.setServiceName(definition.getServiceName());
-    history.setClusterId(m_clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setAlertDefinition(definition);
     history.setAlertLabel("Label");
     history.setAlertState(AlertState.OK);
@@ -468,7 +481,9 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testAlertNoticePredicate() throws Exception {
-    Cluster cluster = initializeNewCluster();
+    Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+
     m_alertHelper.populateData(cluster);
 
     Predicate clusterPredicate = null;
@@ -544,7 +559,9 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testAlertNoticePagination() throws Exception {
-    Cluster cluster = initializeNewCluster();
+    Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+
     m_alertHelper.populateData(cluster);
 
     AlertNoticeRequest request = new AlertNoticeRequest();
@@ -583,7 +600,9 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testAlertNoticeSorting() throws Exception {
-    Cluster cluster = initializeNewCluster();
+    Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+
     m_alertHelper.populateData(cluster);
 
     List<SortRequestProperty> sortProperties = new ArrayList<SortRequestProperty>();
@@ -640,15 +659,135 @@ public class AlertDispatchDAOTest {
   }
 
   /**
+   *
+   */
+  @Test
+  public void testFindDefaultGroup() throws Exception {
+    m_synchronizedBus.register(m_injector.getInstance(AlertServiceStateListener.class));
+
+    List<AlertGroupEntity> groups = m_dao.findAllGroups();
+    assertNotNull(groups);
+    assertEquals(10, groups.size());
+
+    for (AlertGroupEntity group : groups) {
+      assertFalse(group.isDefault());
+    }
+
+    Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+
+    AlertGroupEntity hdfsGroup = m_dao.findDefaultServiceGroup(
+        cluster.getClusterId(), "HDFS");
+
+    assertNotNull(hdfsGroup);
+    assertTrue(hdfsGroup.isDefault());
+  }
+
+  /**
+   * Tests that when creating a new {@link AlertDefinitionEntity}, if the group
+   * for its service does not exist, then it will be created.
+   */
+  @Test
+  public void testDefaultGroupAutomaticCreation() throws Exception {
+    m_synchronizedBus.register(m_injector.getInstance(AlertServiceStateListener.class));
+
+    List<AlertGroupEntity> groups = m_dao.findAllGroups();
+    assertNotNull(groups);
+    assertEquals(10, groups.size());
+
+    for (AlertGroupEntity group : groups) {
+      assertFalse(group.isDefault());
+    }
+
+    Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+
+    AlertGroupEntity hdfsGroup = m_dao.findDefaultServiceGroup(
+        cluster.getClusterId(), "HDFS");
+
+    // remove the HDFS default group
+    m_dao.remove(hdfsGroup);
+    hdfsGroup = m_dao.findDefaultServiceGroup(cluster.getClusterId(), "HDFS");
+    assertNull(hdfsGroup);
+
+    AlertDefinitionEntity datanodeProcess = new AlertDefinitionEntity();
+    datanodeProcess.setClusterId(cluster.getClusterId());
+    datanodeProcess.setDefinitionName("datanode_process");
+    datanodeProcess.setServiceName("HDFS");
+    datanodeProcess.setComponentName("DATANODE");
+    datanodeProcess.setHash(UUID.randomUUID().toString());
+    datanodeProcess.setScheduleInterval(60);
+    datanodeProcess.setScope(Scope.SERVICE);
+    datanodeProcess.setSource("{\"type\" : \"SCRIPT\"}");
+    datanodeProcess.setSourceType(SourceType.SCRIPT);
+    m_definitionDao.create(datanodeProcess);
+
+    // the group should be created and should be default
+    hdfsGroup = m_dao.findDefaultServiceGroup(cluster.getClusterId(), "HDFS");
+    assertNotNull(hdfsGroup);
+    assertTrue(hdfsGroup.isDefault());
+  }
+
+  /**
+   * Tests that when creating a new {@link AlertDefinitionEntity}, if the group
+   * for its service does not exist, then it will not be created if the service
+   * is invalid.
+   */
+  @Test(expected = AmbariException.class)
+  public void testDefaultGroupInvalidServiceNoCreation() throws Exception {
+    m_synchronizedBus.register(m_injector.getInstance(AlertServiceStateListener.class));
+
+    List<AlertGroupEntity> groups = m_dao.findAllGroups();
+    assertNotNull(groups);
+    assertEquals(10, groups.size());
+
+    for (AlertGroupEntity group : groups) {
+      assertFalse(group.isDefault());
+    }
+
+    Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+
+    assertEquals(12, m_dao.findAllGroups().size());
+
+    // create a definition with an invalid service
+    AlertDefinitionEntity datanodeProcess = new AlertDefinitionEntity();
+    datanodeProcess.setClusterId(cluster.getClusterId());
+    datanodeProcess.setDefinitionName("datanode_process");
+    datanodeProcess.setServiceName("INVALID");
+    datanodeProcess.setComponentName("DATANODE");
+    datanodeProcess.setHash(UUID.randomUUID().toString());
+    datanodeProcess.setScheduleInterval(60);
+    datanodeProcess.setScope(Scope.SERVICE);
+    datanodeProcess.setSource("{\"type\" : \"SCRIPT\"}");
+    datanodeProcess.setSourceType(SourceType.SCRIPT);
+
+    try {
+      m_definitionDao.create(datanodeProcess);
+    } finally {
+      // assert no group was added
+      assertEquals(12, m_dao.findAllGroups().size());
+    }
+  }
+
+  /**
    * @return
    */
   private List<AlertDefinitionEntity> createDefinitions() throws Exception {
+    // add a host to the cluster
+    m_helper.addHost(m_clusters, m_cluster, HOSTNAME);
+
+    // install YARN (which doesn't have any alerts defined in the test JSON)
+    // so that the definitions get created correctly
+    m_helper.installYarnService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+
     for (int i = 0; i < 8; i++) {
       AlertDefinitionEntity definition = new AlertDefinitionEntity();
       definition.setDefinitionName("Alert Definition " + i);
-      definition.setServiceName("HDFS");
+      definition.setServiceName("YARN");
       definition.setComponentName(null);
-      definition.setClusterId(m_clusterId);
+      definition.setClusterId(m_cluster.getClusterId());
       definition.setHash(UUID.randomUUID().toString());
       definition.setScheduleInterval(60);
       definition.setScope(Scope.SERVICE);
@@ -680,81 +819,4 @@ public class AlertDispatchDAOTest {
 
     return targets;
   }
-
-  private Cluster initializeNewCluster() throws Exception {
-    String clusterName = "cluster-" + System.currentTimeMillis();
-    m_clusters.addCluster(clusterName);
-
-    Cluster cluster = m_clusters.getCluster(clusterName);
-    StackId stackId = new StackId("HDP", "2.0.6");
-    cluster.setDesiredStackVersion(stackId);
-    cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
-
-    addHost();
-    m_clusters.mapHostToCluster(HOSTNAME, cluster.getClusterName());
-
-    installHdfsService(cluster);
-    return cluster;
-  }
-
-  /**
-   * @throws Exception
-   */
-  private void addHost() throws Exception {
-    m_clusters.addHost(HOSTNAME);
-
-    Host host = m_clusters.getHost(HOSTNAME);
-    Map<String, String> hostAttributes = new HashMap<String, String>();
-    hostAttributes.put("os_family", "redhat");
-    hostAttributes.put("os_release_version", "6.4");
-    host.setHostAttributes(hostAttributes);
-    host.setState(HostState.HEALTHY);
-    host.persist();
-  }
-
-  /**
-   * Calls {@link Service#persist()} to mock a service install along with
-   * creating a single {@link Host} and {@link ServiceComponentHost}.
-   */
-  private void installHdfsService(Cluster cluster) throws Exception {
-    String serviceName = "HDFS";
-    Service service = m_serviceFactory.createNew(cluster, serviceName);
-    cluster.addService(service);
-    service.persist();
-    service = cluster.getService(serviceName);
-    Assert.assertNotNull(service);
-
-    ServiceComponent datanode = m_componentFactory.createNew(service,
-        "DATANODE");
-
-    service.addServiceComponent(datanode);
-    datanode.setDesiredState(State.INSTALLED);
-    datanode.persist();
-
-    ServiceComponentHost sch = m_schFactory.createNew(datanode, HOSTNAME);
-
-    datanode.addServiceComponentHost(sch);
-    sch.setDesiredState(State.INSTALLED);
-    sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
-    sch.setStackVersion(new StackId("HDP-2.0.6"));
-
-    sch.persist();
-
-    ServiceComponent namenode = m_componentFactory.createNew(service,
-        "NAMENODE");
-
-    service.addServiceComponent(namenode);
-    namenode.setDesiredState(State.INSTALLED);
-    namenode.persist();
-
-    sch = m_schFactory.createNew(namenode, HOSTNAME);
-    namenode.addServiceComponentHost(sch);
-    sch.setDesiredState(State.INSTALLED);
-    sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
-    sch.setStackVersion(new StackId("HDP-2.0.6"));
-
-    sch.persist();
-  }
 }